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)
| 3463 | |
| 3464 | @instrument_w_nvtx |
| 3465 | def backward(self, loss, retain_graph=False, scale_wrt_gas=True): |
| 3466 | r"""Execute backward pass on the loss |
| 3467 | Arguments: |
| 3468 | loss: Torch tensor on which to execute backward propagation |
| 3469 | retain_graph: bool, default: false |
| 3470 | forward on user defined choice of retain_graph |
| 3471 | scale_wrt_gas: bool, default: true |
| 3472 | whether to scale gradients and return value by gradient accumulation steps |
| 3473 | |
| 3474 | With ``managed_gradient_accumulation=false``, ``backward()`` only accumulates |
| 3475 | gradients locally (it does not trigger the accumulation-boundary reduction); the |
| 3476 | reduction and optimizer update happen in ``step()``. |
| 3477 | """ |
| 3478 | assert self.optimizer is not None and not isinstance(self.optimizer, DummyOptim), \ |
| 3479 | "must provide optimizer during init in order to use backward" |
| 3480 | assert maybe_loss_for_backward( |
| 3481 | loss), "loss must be a scalar tensor. If you need to pass output gradients, backward() of output tensors" |
| 3482 | |
| 3483 | with self._running_engine_backward_lock: |
| 3484 | self._running_engine_backward_count += 1 |
| 3485 | self._running_engine_backward = True |
| 3486 | engine_backward_graph_state = _ENGINE_BACKWARD_GRAPH_TRACKER.new_state() |
| 3487 | engine_backward_graph_hook = None |
| 3488 | try: |
| 3489 | # Unmanaged mode: count this backward so step() can advance global_samples by the actual micro-batch count. |
| 3490 | if not self.managed_gradient_accumulation(): |
| 3491 | self._unmanaged_backward_count += 1 |
| 3492 | |
| 3493 | # Set flag to prevent hooks from firing (we'll manually call prologue/epilogue) |
| 3494 | backward_kwargs = {"retain_graph": retain_graph} |
| 3495 | |
| 3496 | loss = loss / self.gradient_accumulation_steps() if scale_wrt_gas else loss |
| 3497 | gas_scaled_loss = loss |
| 3498 | |
| 3499 | # TODO: handle these scaling with direct calls to loss.backward() |
| 3500 | if isinstance(self.optimizer, ZeROOptimizer): |
| 3501 | loss = self.optimizer.scale_if_loss(loss) |
| 3502 | elif self.torch_autocast_z0_gradscaler: |
| 3503 | loss = self.torch_autocast_z0_gradscaler.scale(loss) |
| 3504 | |
| 3505 | if loss.requires_grad: |
| 3506 | engine_backward_graph_hook = loss.register_hook( |
| 3507 | lambda grad: _ENGINE_BACKWARD_GRAPH_TRACKER.register_current_graph( |
| 3508 | grad, engine_backward_graph_state)) |
| 3509 | |
| 3510 | with compiled_autograd(self._is_compiled_autograd_enabled, self._compile_kwargs): |
| 3511 | loss.backward(**backward_kwargs) |
| 3512 | |
| 3513 | # backward_epilogue is not called in a hook when self._support_torch_style_backward is False |
| 3514 | self._backward_epilogue() |
| 3515 | |
| 3516 | return gas_scaled_loss |
| 3517 | finally: |
| 3518 | if engine_backward_graph_hook is not None: |
| 3519 | engine_backward_graph_hook.remove() |
| 3520 | _ENGINE_BACKWARD_GRAPH_TRACKER.unregister(engine_backward_graph_state) |
| 3521 | with self._running_engine_backward_lock: |
| 3522 | self._running_engine_backward_count -= 1 |
nothing calls this directly
no test coverage detected