class_docstr: Run training in mixed precision
class_docstr_tok: ["Run","training","in","mixed","precision"]
code: def before_step(self):
      # First, check for an overflow
      if self.dynamic and grad_overflow(self.model_pgs):
          self.loss_scale /= self.div_factor
          self.learn.loss_grad /= (
              self.div_factor
          )  # to record correct loss
          self.model.zero_grad()
          raise CancelBatchException()  # skip step and zero_grad
      to_master_grads(self.model_pgs, self.master_pgs, self.flat_master)
      for master_params in self.master_pgs:
          for param in master_params:
              if param.grad is not None:
                  param.grad.div_(self.loss_scale)
      if self.clip is not None:
          for group in self.master_pgs:
              nn.utils.clip_grad_norm_(group, self.clip)
      # Check if it's been long enough without overflow
      if self.dynamic:
          self.count += 1
          if self.count == self.scale_wait:
              self.count = 0
              self.loss_scale *= self.div_factor
  
imports: from __future__ import annotations
  from ..basics import *
  from .progress import *
  from torch.cuda.amp import GradScaler, autocast
  from torch.cuda.amp.grad_scaler import OptState
  from ..fp16_utils import convert_network, model_grads_to_master_grads, master_params_to_model_params
  from torch.nn.utils import parameters_to_vector
label_desc: Use image node_id 6 for calling the NonNativeMixedPrecision obj's underlying member method code with example usage: obj.before_step() without return types