class_docstr: Performs loss scaling during backward pass to prevent underflow of half
  precision gradients.
class_docstr_tok: ["Performs","loss","scaling","during","backward","pass","to","prevent","underflow","of","half","precision","gradients","."]
code: def _update_scale(self) -> LossScaleResult:
      old_scale = self._grad_scaler.get_scale()
  
      self._grad_scaler.update()
  
      new_scale = self._grad_scaler.get_scale()
  
      if self._are_close(old_scale, new_scale):
          return LossScaleResult(old_scale, new_scale)
  
      if new_scale > old_scale:
          self._log_info("No gradient overflow detected in the last %s step(s), increasing loss scale from %s to %s.", self.scale_window, old_scale, new_scale)  # fmt: skip
  
          return LossScaleResult(old_scale, new_scale)
  
      if self.min_scale > new_scale:
          self._grad_scaler.update(self.min_scale)
  
          if self._are_close(old_scale, self.min_scale):
              self._log_warning("Overflow detected, ignoring gradient, loss scale is already at minimum (%s). Your loss is probably exploding. Try lowering the learning rate, using gradient clipping, or increasing the batch size.", self.min_scale)  # fmt: skip
          else:
              self._log_warning("Overflow detected, ignoring gradient, decreasing loss scale from %s to %s (minimum). Your loss is probably exploding. Try lowering the learning rate, using gradient clipping, or increasing the batch size.", old_scale, self.min_scale)  # fmt: skip
  
          return LossScaleResult(
              old_scale, new_scale, overflow=True, min_=True
          )
      else:
          self._log_info("Overflow detected, ignoring gradient, decreasing loss scale from %s to %s.", old_scale, new_scale)  # fmt: skip
  
          return LossScaleResult(old_scale, new_scale, overflow=True)
  
imports: from __future__ import annotations
  import logging
  import math
  from dataclasses import dataclass
  from logging import Logger
  from typing import Any, Callable, Dict, Mapping, Optional, Tuple, cast
  import torch
  from torch import Tensor
  from torch.cuda.amp.grad_scaler import GradScaler
  from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler
  from torch.optim import Optimizer
  from fairseq2.gang import Gang
label_desc: Use image node_id 5 for calling the DynamicLossScaler obj's underlying member method code with example usage: obj._update_scale() and returns: LossScaleResult, LossScaleResult, LossScaleResult, LossScaleResult