class_docstr: ## L2 Weight decay
docstr: ### Perform weight decay and return the gradient
docstr_tok: ["#","#","#","Perform","weight","decay","and","return","the","gradient"]
code: def __call__(
      self,
      param: torch.nn.Parameter,
      grad: torch.Tensor,
      group: Dict[str, any],
  ):
      """
      ### Perform weight decay and return the gradient
      """
  
      # If we are doing the decay on the parameter directly
      if self.weight_decouple:
          # If the weight decay coefficient is absolute
          if self.absolute:
              param.data.mul_(1.0 - group["weight_decay"])
          # Otherwise,
          else:
              param.data.mul_(1.0 - group["lr"] * group["weight_decay"])
          # Return the unmodified gradient
          return grad
      else:
          if group["weight_decay"] != 0:
              # Add the weight decay to the gradient and return the modified gradient
              return grad.add(param.data, alpha=group["weight_decay"])
          else:
              return grad
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/optimizers/__init__.py
imports: from typing import Dict, Tuple, Any
  import torch
  from torch import nn
  from torch.optim.optimizer import Optimizer
label_desc: Use image node_id 3 for calling the WeightDecay obj's underlying member method code with example usage: obj.__call__(param, grad, group) and returns: grad, grad, grad