docstr: Zero fp32 and fp16 parameter grads.
docstr_tok: ["Zero","fp32","and","fp16","parameter","grads","."]
code: def zero_grad(self, set_grads_to_None=False):
      """
      Zero fp32 and fp16 parameter grads.
      """
      # In principle, only the .grad attributes of the model params need to be zeroed,
      # because gradients are copied into the FP32 master params.  However, we zero
      # all gradients owned by the optimizer, just to be safe:
      for group in self.optimizer.param_groups:
          for p in group["params"]:
              if set_grads_to_None:
                  p.grad = None
              else:
                  if p.grad is not None:
                      p.grad.detach_()
                      p.grad.zero_()
  
      # Zero fp16 gradients owned by the model:
      for fp16_group in self.fp16_groups:
          for param in fp16_group:
              if set_grads_to_None:
                  param.grad = None
              else:
                  if param.grad is not None:
                      param.grad.detach_()  # as in torch.optim.optimizer.zero_grad()
                      param.grad.zero_()
  
imports: import torch
  from torch import nn
  from torch.autograd import Variable
  from torch.nn.parameter import Parameter
  from torch._utils import _flatten_dense_tensors, _unflatten_dense_tensors
  from ..amp._amp_state import _amp_state, maybe_print
  from ..amp.scaler import LossScaler
  from ..multi_tensor_apply import multi_tensor_applier
  from .fp16util import model_grads_to_master_grads, master_params_to_model_params, clip_grad_norm
label_desc: Use image node_id 5 for calling the FP16_Optimizer obj's underlying member method code with example usage: obj.zero_grad(set_grads_to_None) without return types