class_docstr: Internal wrapper around a torch optimizer.
  
  Conditionally will perform `step` and `zero_grad` if gradients should be synchronized when performing gradient
  accumulation.
  
  Args:
      optimizer (`torch.optim.optimizer.Optimizer`):
          The optimizer to wrap.
      device_placement (`bool`, *optional*, defaults to `True`):
          Whether or not the optimizer should handle device placement. If so, it will place the state dictionary of
          `optimizer` on the right device.
      scaler (`torch.cuda.amp.grad_scaler.GradScaler`, *optional*):
          The scaler to use in the step function if training with mixed precision.
class_docstr_tok: ["Internal","wrapper","around","a","torch","optimizer",".","Conditionally","will","perform","`","step","`","and","`","zero_grad","`","if","gradients","should","be","synchronized","when","performing","gradient","accumulation",".","Args",":","optimizer","(","`","torch.optim.optimizer.Optimizer","`",")",":","The","optimizer","to","wrap",".","device_placement","(","`","bool","`",",","*","optional","*",",","defaults","to","`","True","`",")",":","Whether","or","not","the","optimizer","should","handle","device","placement",".","If","so",",","it","will","place","the","state","dictionary","of","`","optimizer","`","on","the","right","device",".","scaler","(","`","torch.cuda.amp.grad_scaler.GradScaler","`",",","*","optional","*",")",":","The","scaler","to","use","in","the","step","function","if","training","with","mixed","precision","."]
code: def __init__(self, optimizer, device_placement=True, scaler=None):
      self.optimizer = optimizer
      self.scaler = scaler
      self.accelerator_state = AcceleratorState()
      self.gradient_state = GradientState()
      self.device_placement = device_placement
      self._is_overflow = False
  
      if self.scaler is not None:
          self._accelerate_step_called = False
          self._optimizer_original_step_method = self.optimizer.step
          self._optimizer_patched_step_method = patch_optimizer_step(
              self, self.optimizer.step
          )
  
      # Handle device placement
      if device_placement:
          state_dict = self.optimizer.state_dict()
          if (
              self.accelerator_state.distributed_type
              == DistributedType.TPU
          ):
              xm.send_cpu_data_to_device(
                  state_dict, self.accelerator_state.device
              )
          else:
              state_dict = move_to_device(
                  state_dict, self.accelerator_state.device
              )
          self.optimizer.load_state_dict(state_dict)
  
imports: import inspect
  import warnings
  import torch
  from .state import AcceleratorState, GradientState
  from .utils import DistributedType, honor_type, is_tpu_available
label_desc: Use image node_id 1 to create a new AcceleratedOptimizer object from inherited base classes: torch.optim with example: obj = AcceleratedOptimizer(optimizer, device_placement, scaler)