class_docstr: This class is used to wrap the user optimizers and handle properly the backward and optimizer_step logic across
  accelerators, AMP, accumulate_grad_batches.
  
  Note: The purpose of this wrapper is only to define new methods and redirect the `.step()` call. The internal
  state ``__dict__`` is not kept in sync with the internal state of the original optimizer, but the Trainer never
  relies on the internal state of the wrapper.
docstr: This function is just a helper for advanced users.
  
  Considering the current optimizer as A and all other optimizers as B.
  Toggling means all parameters from B exclusive to A will have ``requires_grad`` set to False.
  
  When performing gradient accumulation, there is no need to perform grad synchronization
  during the accumulation phase.
  Setting `sync_grad` to False will block this synchronization and improve performance.
docstr_tok: ["This","function","is","just","a","helper","for","advanced","users",".","Considering","the","current","optimizer","as","A","and","all","other","optimizers","as","B.","Toggling","means","all","parameters","from","B","exclusive","to","A","will","have","``","requires_grad","``","set","to","False",".","When","performing","gradient","accumulation",",","there","is","no","need","to","perform","grad","synchronization","during","the","accumulation","phase",".","Setting","`","sync_grad","`","to","False","will","block","this","synchronization","and","improve","performance","."]
code: def toggle_model(
      self, sync_grad: bool = True
  ) -> Generator[None, None, None]:
      """This function is just a helper for advanced users.
  
      Considering the current optimizer as A and all other optimizers as B.
      Toggling means all parameters from B exclusive to A will have ``requires_grad`` set to False.
  
      When performing gradient accumulation, there is no need to perform grad synchronization
      during the accumulation phase.
      Setting `sync_grad` to False will block this synchronization and improve performance.
  
      """
      # local import here to avoid circular import
      from lightning.pytorch.loops.utilities import (
          _block_parallel_sync_behavior,
      )
  
      assert self._strategy is not None
      lightning_module = self._strategy.lightning_module
      assert lightning_module is not None
      with _block_parallel_sync_behavior(
          self._strategy, block=(not sync_grad)
      ):
          lightning_module.toggle_optimizer(self)
          yield
          lightning_module.untoggle_optimizer(self)
  
imports: from contextlib import contextmanager
  from dataclasses import fields
  from typing import Any, Callable, Dict, Generator, List, Optional, Tuple, Union, overload
  from weakref import proxy
  import torch
  from torch import optim
  from torch.optim import Optimizer
  from typing_extensions import override
  import lightning.pytorch
  from lightning.fabric.utilities.types import Optimizable, ReduceLROnPlateau, _Stateful
  from lightning.pytorch.utilities.exceptions import MisconfigurationException
  from lightning.pytorch.utilities.model_helpers import is_overridden
  from lightning.pytorch.utilities.rank_zero import rank_zero_warn
  from lightning.pytorch.utilities.signature_utils import is_param_in_hook_signature
  from lightning.pytorch.utilities.types import LRSchedulerConfig, LRSchedulerTypeTuple
label_desc: Use image node_id 3 for calling the LightningOptimizer obj's underlying member method code with example usage: obj.toggle_model(sync_grad) without return types