r"""Apply loss scaler for manual backward pass. Use this method when calling loss.backward() directly instead of engine.backward(). This applies the appropriate loss scaler for mixed precision training, allowing you to manually control the backward pass while still benefitin
(self, loss)
| 3419 | optimizer.independent_gradient_partition_epilogue() |
| 3420 | |
| 3421 | def scale(self, loss): |
| 3422 | r"""Apply loss scaler for manual backward pass. |
| 3423 | |
| 3424 | Use this method when calling loss.backward() directly instead of engine.backward(). |
| 3425 | This applies the appropriate loss scaler for mixed precision training, allowing you |
| 3426 | to manually control the backward pass while still benefiting from DeepSpeed's |
| 3427 | gradient scaling functionality. |
| 3428 | |
| 3429 | Example:: |
| 3430 | |
| 3431 | output = engine(input) |
| 3432 | loss = criterion(output, target) |
| 3433 | scaled_loss = engine.scale(loss) |
| 3434 | scaled_loss.backward() # Manual backward call |
| 3435 | engine.step() |
| 3436 | |
| 3437 | Arguments: |
| 3438 | loss: Scalar loss tensor to be scaled |
| 3439 | |
| 3440 | Returns: |
| 3441 | Scaled loss tensor ready for .backward() call |
| 3442 | |
| 3443 | Raises: |
| 3444 | AssertionError: If loss is not a scalar tensor with grad_fn, or if no optimizer |
| 3445 | is configured. |
| 3446 | """ |
| 3447 | assert self.optimizer is not None and not isinstance(self.optimizer, DummyOptim), \ |
| 3448 | "must provide optimizer during init in order to use scale" |
| 3449 | assert maybe_loss_for_backward(loss), \ |
| 3450 | "loss must be a scalar tensor with grad_fn. For non-scalar tensors, use tensor.backward(grad)" |
| 3451 | |
| 3452 | # Apply loss scaler based on optimizer type |
| 3453 | scaled_loss = loss |
| 3454 | if isinstance(self.optimizer, ZeROOptimizer): |
| 3455 | scaled_loss = self.optimizer.scale_if_loss(scaled_loss) |
| 3456 | elif self.torch_autocast_z0_gradscaler: |
| 3457 | scaled_loss = self.torch_autocast_z0_gradscaler.scale(scaled_loss) |
| 3458 | |
| 3459 | # Mark that scale() was called for validation in backward hook |
| 3460 | self._manual_backward_expected = True |
| 3461 | |
| 3462 | return scaled_loss |
| 3463 | |
| 3464 | @instrument_w_nvtx |
| 3465 | def backward(self, loss, retain_graph=False, scale_wrt_gas=True): |
no test coverage detected