docstr: Clips gradient norm and updates dynamic loss scaler.
docstr_tok: ["Clips","gradient","norm","and","updates","dynamic","loss","scaler","."]
code: def clip_grad_norm(self, max_norm, aggregate_norm_fn=None):
      """Clips gradient norm and updates dynamic loss scaler."""
      max_norm = float(max_norm)
      grad_norm = (
          self._multiply_factor
          * self.wrapped_optimizer.clip_grad_norm(0, aggregate_norm_fn)
      )
  
      if self.scaler is not None:
          grad_norm_cpu = float(grad_norm)
          if grad_norm_cpu > max_norm > 0.0:
              self._multiply_factor *= max_norm / grad_norm_cpu
  
          # detect overflow and adjust loss scale
          self.scaler.check_overflow(grad_norm_cpu)
      elif max_norm > 0.0:
          clip_coef = (max_norm / (grad_norm + 1e-6)).clamp_(max=1)
          self._multiply_factor *= clip_coef
  
      return grad_norm
  
imports: from collections import defaultdict
  from itertools import chain
  import torch
  from omegaconf import DictConfig
  from fairseq import optim
  from .dynamic_loss_scaler import DynamicLossScaler
label_desc: Use image node_id 8 for calling the _MemoryEfficientFP16OptimizerMixin obj's underlying member method code with example usage: obj.clip_grad_norm(max_norm, aggregate_norm_fn) and returns: grad_norm