Handle forward for each training case (distributed, single gpu, etc...) :param batch: :param batch_idx: :return:
(self, batch, batch_idx, opt_idx, hiddens)
| 1562 | return 0, grad_norm_dic, all_log_metrics |
| 1563 | |
| 1564 | def training_forward(self, batch, batch_idx, opt_idx, hiddens): |
| 1565 | """ |
| 1566 | Handle forward for each training case (distributed, single gpu, etc...) |
| 1567 | :param batch: |
| 1568 | :param batch_idx: |
| 1569 | :return: |
| 1570 | """ |
| 1571 | # --------------- |
| 1572 | # FORWARD |
| 1573 | # --------------- |
| 1574 | # enable not needing to add opt_idx to training_step |
| 1575 | args = [batch, batch_idx, opt_idx] |
| 1576 | |
| 1577 | # distributed forward |
| 1578 | if self.use_ddp or self.use_dp: |
| 1579 | output = self.model(*args) |
| 1580 | # single GPU forward |
| 1581 | elif self.single_gpu: |
| 1582 | gpu_id = 0 |
| 1583 | if isinstance(self.data_parallel_device_ids, list): |
| 1584 | gpu_id = self.data_parallel_device_ids[0] |
| 1585 | batch = self.transfer_batch_to_gpu(copy.copy(batch), gpu_id) |
| 1586 | args[0] = batch |
| 1587 | output = self.model.training_step(*args) |
| 1588 | # CPU forward |
| 1589 | else: |
| 1590 | output = self.model.training_step(*args) |
| 1591 | |
| 1592 | # allow any mode to define training_end |
| 1593 | model_ref = self.get_model() |
| 1594 | output_ = model_ref.training_end(output) |
| 1595 | if output_ is not None: |
| 1596 | output = output_ |
| 1597 | |
| 1598 | # format and reduce outputs accordingly |
| 1599 | output = self.process_output(output, train=True) |
| 1600 | |
| 1601 | return output |
| 1602 | |
| 1603 | # --------------- |
| 1604 | # Utils |
no test coverage detected