class_docstr: Hooks to be used in LightningModule.
class_docstr_tok: ["Hooks","to","be","used","in","LightningModule","."]
docstr: Called before ``optimizer.step()``.
  
  If using gradient accumulation, the hook is called once the gradients have been accumulated.
  See: :paramref:`~lightning.pytorch.trainer.trainer.Trainer.accumulate_grad_batches`.
  
  If using AMP, the loss will be unscaled before calling this hook.
  See these `docs <https://pytorch.org/docs/stable/notes/amp_examples.html#working-with-unscaled-gradients>`__
  for more information on the scaling of gradients.
  
  If clipping gradients, the gradients will not have been clipped yet.
  
  Args:
      optimizer: Current optimizer being used.
  
  Example::
  
      def on_before_optimizer_step(self, optimizer):
          # example to inspect gradient information in tensorboard
          if self.trainer.global_step % 25 == 0:  # don't make the tf file huge
              for k, v in self.named_parameters():
                  self.logger.experiment.add_histogram(
                      tag=k, values=v.grad, global_step=self.trainer.global_step
                  )
code: def on_before_optimizer_step(self, optimizer: Optimizer) -> None:
      """Called before ``optimizer.step()``.
  
      If using gradient accumulation, the hook is called once the gradients have been accumulated.
      See: :paramref:`~lightning.pytorch.trainer.trainer.Trainer.accumulate_grad_batches`.
  
      If using AMP, the loss will be unscaled before calling this hook.
      See these `docs <https://pytorch.org/docs/stable/notes/amp_examples.html#working-with-unscaled-gradients>`__
      for more information on the scaling of gradients.
  
      If clipping gradients, the gradients will not have been clipped yet.
  
      Args:
          optimizer: Current optimizer being used.
  
      Example::
  
          def on_before_optimizer_step(self, optimizer):
              # example to inspect gradient information in tensorboard
              if self.trainer.global_step % 25 == 0:  # don't make the tf file huge
                  for k, v in self.named_parameters():
                      self.logger.experiment.add_histogram(
                          tag=k, values=v.grad, global_step=self.trainer.global_step
                      )
  
      """
  
imports: from typing import Any, Dict, Optional
  import torch
  from torch import Tensor
  from torch.optim.optimizer import Optimizer
  from lightning.fabric.utilities.imports import _TORCH_GREATER_EQUAL_2_0
  from lightning.pytorch.utilities import move_data_to_device
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.types import EVAL_DATALOADERS, STEP_OUTPUT, TRAIN_DATALOADERS
label_desc: Use image node_id 36 for calling the ModelHooks obj's underlying member method code with example usage: obj.on_before_optimizer_step(optimizer) without return types