r"""Execute backward pass on the loss Arguments: loss: Torch tensor on which to execute backward propagation retain_graph: bool, default: false forward on user defined choice of retain_graph scale_wrt_gas: bool, default: true
(self, loss, retain_graph=False, scale_wrt_gas=True)
| 3568 | |
| 3569 | @instrument_w_nvtx |
| 3570 | def backward(self, loss, retain_graph=False, scale_wrt_gas=True): |
| 3571 | r"""Execute backward pass on the loss |
| 3572 | Arguments: |
| 3573 | loss: Torch tensor on which to execute backward propagation |
| 3574 | retain_graph: bool, default: false |
| 3575 | forward on user defined choice of retain_graph |
| 3576 | scale_wrt_gas: bool, default: true |
| 3577 | whether to scale gradients and return value by gradient accumulation steps |
| 3578 | |
| 3579 | With ``managed_gradient_accumulation=false``, ``backward()`` only accumulates |
| 3580 | gradients locally (it does not trigger the accumulation-boundary reduction); the |
| 3581 | reduction and optimizer update happen in ``step()``. |
| 3582 | """ |
| 3583 | assert self.optimizer is not None and not isinstance(self.optimizer, DummyOptim), \ |
| 3584 | "must provide optimizer during init in order to use backward" |
| 3585 | assert maybe_loss_for_backward( |
| 3586 | loss), "loss must be a scalar tensor. If you need to pass output gradients, backward() of output tensors" |
| 3587 | |
| 3588 | with self._running_engine_backward_lock: |
| 3589 | self._running_engine_backward_count += 1 |
| 3590 | self._running_engine_backward = True |
| 3591 | engine_backward_graph_state = _ENGINE_BACKWARD_GRAPH_TRACKER.new_state() |
| 3592 | engine_backward_graph_hook = None |
| 3593 | try: |
| 3594 | # Unmanaged mode: count this backward so step() can advance global_samples by the actual micro-batch count. |
| 3595 | if not self.managed_gradient_accumulation(): |
| 3596 | self._unmanaged_backward_count += 1 |
| 3597 | |
| 3598 | # Set flag to prevent hooks from firing (we'll manually call prologue/epilogue) |
| 3599 | backward_kwargs = {"retain_graph": retain_graph} |
| 3600 | |
| 3601 | # fp32 copy for the monitor, divided by gas only after the cast so low precision losses do not underflow |
| 3602 | monitor_loss = None |
| 3603 | if self.monitor.enabled: |
| 3604 | monitor_loss = loss.detach().float().mean() |
| 3605 | if scale_wrt_gas: |
| 3606 | monitor_loss = monitor_loss / self.gradient_accumulation_steps() |
| 3607 | |
| 3608 | loss = loss / self.gradient_accumulation_steps() if scale_wrt_gas else loss |
| 3609 | gas_scaled_loss = loss |
| 3610 | |
| 3611 | # TODO: handle these scaling with direct calls to loss.backward() |
| 3612 | if isinstance(self.optimizer, ZeROOptimizer): |
| 3613 | loss = self.optimizer.scale_if_loss(loss) |
| 3614 | elif self.torch_autocast_z0_gradscaler: |
| 3615 | loss = self.torch_autocast_z0_gradscaler.scale(loss) |
| 3616 | |
| 3617 | if loss.requires_grad: |
| 3618 | engine_backward_graph_hook = loss.register_hook( |
| 3619 | lambda grad: _ENGINE_BACKWARD_GRAPH_TRACKER.register_current_graph( |
| 3620 | grad, engine_backward_graph_state)) |
| 3621 | |
| 3622 | with compiled_autograd(self._is_compiled_autograd_enabled, self._compile_kwargs): |
| 3623 | loss.backward(**backward_kwargs) |
| 3624 | |
| 3625 | # backward_epilogue is not called in a hook when self._support_torch_style_backward is False |
| 3626 | self._backward_epilogue() |
| 3627 |
nothing calls this directly
no test coverage detected