Runs model forward and calculates loss according to given loss_fn. NOTE: The input signature here needs to be a Pipelineable object for prefetching purposes during training using torchrec's pipeline. However the underlying model signature needs to be exportable to onnx, requiring g
(self, batch: "RecapBatch")
| 27 | self.loss_fn = loss_fn |
| 28 | |
| 29 | def forward(self, batch: "RecapBatch"): # type: ignore[name-defined] |
| 30 | """Runs model forward and calculates loss according to given loss_fn. |
| 31 | |
| 32 | NOTE: The input signature here needs to be a Pipelineable object for |
| 33 | prefetching purposes during training using torchrec's pipeline. However |
| 34 | the underlying model signature needs to be exportable to onnx, requiring |
| 35 | generic python types. see https://pytorch.org/docs/stable/onnx.html#types. |
| 36 | |
| 37 | """ |
| 38 | outputs = self.model(batch) |
| 39 | losses = self.loss_fn(outputs["logits"], batch.labels.float(), batch.weights.float()) |
| 40 | |
| 41 | outputs.update( |
| 42 | { |
| 43 | "loss": losses, |
| 44 | "labels": batch.labels, |
| 45 | "weights": batch.weights, |
| 46 | } |
| 47 | ) |
| 48 | |
| 49 | # Allow multiple losses. |
| 50 | return losses, outputs |
| 51 | |
| 52 | |
| 53 | def maybe_shard_model( |