class_docstr: Mixed precision training using Pytorch's Automatic Mixed Precision (AMP)
class_docstr_tok: ["Mixed","precision","training","using","Pytorch","'s","Automatic","Mixed","Precision","(","AMP",")"]
code: def before_fit(self):
      if self.amp_mode == AMPMode.BF16:
          if (
              torch.cuda.is_available()
              and not torch.cuda.is_bf16_supported()
          ):
              raise ValueError(
                  "Unsupported GPU for bfloat16 mixed precision training"
              )
          dtype = torch.bfloat16
      elif self.amp_mode == AMPMode.FP16:
          dtype = torch.float16
      else:
          raise ValueError(f"Unrecognized precision: {self.amp_mode}")
      # `GradScaler` is not needed for bfloat16 as fp32 and bf16 have the same range
      self.kwargs["enabled"] = dtype == torch.float16
      self.autocast, self.learn.scaler, self.scales = (
          autocast(dtype=dtype),
          GradScaler(**self.kwargs),
          L(),
      )
  
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 2 for calling the MixedPrecision obj's underlying member method code with example usage: obj.before_fit() without return types