class_docstr: Extends nn.Module so that its parameter tensors have an extra field called .grad_sample.
class_docstr_tok: ["Extends","nn.Module","so","that","its","parameter","tensors","have","an","extra","field","called",".grad_sample","."]
docstr: Clear gradients.
  
  Clears ``p.grad`` and ``p.grad_sample`` for all of it's parameters
  
  Notes:
      ``set_to_none`` argument only affects ``p.grad``. ``p.grad_sample`` is
      never zeroed out and always set to None.
      Normal grads can do this, because their shape is always the same.
      Grad samples do not behave like this, as we accumulate gradients from different
      batches in a list
  
  Args:
      set_to_none: instead of setting to zero, set the grads to None. (only
      affects regular gradients. Per sample gradients are always set to None)
docstr_tok: ["Clear","gradients",".","Clears","``","p.grad","``","and","``","p.grad_sample","``","for","all","of","it","'s","parameters","Notes",":","``","set_to_none","``","argument","only","affects","``","p.grad","``",".","``","p.grad_sample","``","is","never","zeroed","out","and","always","set","to","None",".","Normal","grads","can","do","this",",","because","their","shape","is","always","the","same",".","Grad","samples","do","not","behave","like","this",",","as","we","accumulate","gradients","from","different","batches","in","a","list","Args",":","set_to_none",":","instead","of","setting","to","zero",",","set","the","grads","to","None",".","(","only","affects","regular","gradients",".","Per","sample","gradients","are","always","set","to","None",")"]
code: def zero_grad(self, set_to_none: bool = False):
      """
      Clear gradients.
  
      Clears ``p.grad`` and ``p.grad_sample`` for all of it's parameters
  
      Notes:
          ``set_to_none`` argument only affects ``p.grad``. ``p.grad_sample`` is
          never zeroed out and always set to None.
          Normal grads can do this, because their shape is always the same.
          Grad samples do not behave like this, as we accumulate gradients from different
          batches in a list
  
      Args:
          set_to_none: instead of setting to zero, set the grads to None. (only
          affects regular gradients. Per sample gradients are always set to None)
      """
      if set_to_none is False:
          logger.debug(
              "Despite set_to_none is set to False, "
              "opacus will set p.grad_sample to None due to "
              "non-trivial gradient accumulation behaviour"
          )
      self.set_grad_sample_to_none()
      super().zero_grad(set_to_none)
  
imports: import logging
  from abc import ABC, abstractmethod
  from typing import Optional
  import torch.nn
  from opacus.utils.module_utils import trainable_parameters
  from torch.utils.hooks import RemovableHandle
label_desc: Use image node_id 4 for calling the AbstractGradSampleModule obj's underlying member method code with example usage: obj.zero_grad(set_to_none) without return types