code: 
  
  import inspect
  import warnings
  
  import torch
  
  from .state import AcceleratorState, GradientState
  from .utils import DistributedType, honor_type, is_tpu_available
  
  
  if is_tpu_available(check_device=False):
   import torch_xla.core.xla_model as xm
  def 		SCREAMING_SNAKE_CASE__				(							_UpperCAmelCase,      _UpperCAmelCase				)					-> Optional[int]:
  
  
  
      '''simple docstring'''
  
  
  
  
  
  
      if isinstance(snake_case__,      (list, tuple)				):
          return honor_type(snake_case__,      (move_to_device(snake_case__,      snake_case__				) for t in state)				)
      elif isinstance(snake_case__,      snake_case__				):
          return type(snake_case__				)({k: move_to_device(snake_case__,      snake_case__				) for k, v in state.items()}				)
      elif isinstance(snake_case__,      torch.Tensor				):
          return state.to(snake_case__				)
      return state
  
  
  
  
  
  
  class    __A				(							torch.optim.Optimizer ):
  
  
  
  
  
  
      def __init__(	self			:    Any    ,				UpperCAmelCase_			:    Optional[Any]    ,				UpperCAmelCase_			:    List[Any]=True    ,				UpperCAmelCase_			:    Optional[Any]=None    ):
          lowerCAmelCase    			=      optimizer
          lowerCAmelCase    			=      scaler
          lowerCAmelCase    			=      AcceleratorState()
          lowerCAmelCase    			=      GradientState()
          lowerCAmelCase    			=      device_placement
          lowerCAmelCase    			=      False
          lowerCAmelCase    			=      None
  
          # Handle device placement
          if device_placement:
              lowerCAmelCase    			=      self.optimizer.state_dict()
              if self.accelerator_state.distributed_type == DistributedType.TPU:
                  xm.send_cpu_data_to_device(SCREAMING_SNAKE_CASE_    ,				self.accelerator_state.device    )
              else:
                  lowerCAmelCase    			=      move_to_device(SCREAMING_SNAKE_CASE_    ,				self.accelerator_state.device    )
              self.optimizer.load_state_dict(SCREAMING_SNAKE_CASE_    )
  
  
  
  
  
  
      @property
      def 	lowercase__ (	self			:    List[Any]    ):
          return self.optimizer.state
  
  
  
  
  
  
      @state.setter
      def 	lowercase__ (	self			:    str    ,				UpperCAmelCase_			:    Tuple    ):
          lowerCAmelCase    			=      state
  
  
  
  
  
  
      @property
      def 	lowercase__ (	self			:    Optional[int]    ):
          return self.optimizer.param_groups
  
  
  
  
  
  
      @param_groups.setter
      def 	lowercase__ (	self			:    str    ,				UpperCAmelCase_			:    List[str]    ):
          lowerCAmelCase    			=      param_groups
  
  
  
  
  
  
      @property
      def 	lowercase__ (	self			:    Any    ):
          return self.optimizer.defaults
  
  
  
  
  
  
      @defaults.setter
      def 	lowercase__ (	self			:    str    ,				UpperCAmelCase_			:    int    ):
          lowerCAmelCase    			=      defaults
  
  
  
  
  
  
      def 	lowercase__ (	self			:    str    ,				UpperCAmelCase_			:    Any    ):
          self.optimizer.add_param_group(SCREAMING_SNAKE_CASE_    )
  
  
  
  
  
  
      def 	lowercase__ (	self			:    List[Any]    ,				UpperCAmelCase_			:    List[str]    ):
          if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
              xm.send_cpu_data_to_device(SCREAMING_SNAKE_CASE_    ,				self.accelerator_state.device    )
          self.optimizer.load_state_dict(SCREAMING_SNAKE_CASE_    )
  
  
  
  
  
  
      def 	lowercase__ (	self			:    Union[str, Any]    ):
          return self.optimizer.state_dict()
  
  
  
  
  
  
      def 	lowercase__ (	self			:    List[str]    ,				UpperCAmelCase_			:    Optional[Any]=None    ):
          if self.gradient_state.sync_gradients:
              lowerCAmelCase    			=      'set_to_none' in inspect.signature(self.optimizer.zero_grad    ).parameters
              if accept_arg:
                  if set_to_none is None:
                      lowerCAmelCase    			=      False
                  self.optimizer.zero_grad(set_to_none=SCREAMING_SNAKE_CASE_    )
              else:
                  if set_to_none is not None:
                      raise ValueError('`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer.'    )
                  self.optimizer.zero_grad()
  
  
  
  
  
  
      def 	lowercase__ (	self			:    Optional[int]    ,				UpperCAmelCase_			:    Dict=None    ):
          if self.gradient_state.sync_gradients:
              if self.accelerator_state.distributed_type == DistributedType.TPU:
                  lowerCAmelCase    			=      {'closure': closure} if closure is not None else {}
                  xm.optimizer_step(self.optimizer    ,				optimizer_args=SCREAMING_SNAKE_CASE_    )
              elif self.scaler is not None:
                  lowerCAmelCase    			=      False
                  if self._last_scale is None:
                      # `get_scale` is an async operation requiring full synchronization
                      # on CPU and GPUs before finishing. As a result, we store away
                      # the prior one to reduce the call overhead
                      lowerCAmelCase    			=      self.scaler.get_scale()
                      lowerCAmelCase    			=      True
                  self.scaler.step(self.optimizer    ,				SCREAMING_SNAKE_CASE_    )
                  self.scaler.update()
                  lowerCAmelCase    			=      self.scaler.get_scale()
                  if not new_scale:
                      # If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
                      lowerCAmelCase    			=      scale_after < self._last_scale
                  lowerCAmelCase    			=      scale_after
  
              else:
                  self.optimizer.step(SCREAMING_SNAKE_CASE_    )
  
  
  
  
  
  
      def 	lowercase__ (	self			:    int    ,				UpperCAmelCase_			:    Union[str, Any]    ):
          for param_group in self.optimizer.param_groups:
              lowerCAmelCase    			=      [parameters_map.get(SCREAMING_SNAKE_CASE_    ,				SCREAMING_SNAKE_CASE_    ) for p in param_group['params']]
  
  
  
  
  
  
      @property
      def 	lowercase__ (	self			:    Dict    ):
          warnings.warn(
              'The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use '
              '`optimizer.step_was_skipped` instead.'    ,				SCREAMING_SNAKE_CASE_    ,				)
          return self._is_overflow
  
  
  
  
  
  
      @property
      def 	lowercase__ (	self			:    Any    ):
          return self._is_overflow
  
  
  
  
  
  
      def __getstate__(	self			:    List[str]    ):
          return self.__dict__.copy()
  
  
  
  
  
  
      def __setstate__(	self			:    int    ,				UpperCAmelCase_			:    Any    ):
          self.__dict__.update(SCREAMING_SNAKE_CASE_    )
  
  
  
  
  
  
style_context: 
  
  __A   :     List[Any]      =			'''
  # Installation de Transformers
  ! pip install transformers datasets
  # Pour installer à partir du code source au lieu de la dernière version, commentez la commande ci-dessus et décommentez la suivante.
  # ! pip install git+https://github.com/huggingface/transformers.git
  '''
  
  __A   :     Dict      =			[{'''type''': '''code''', '''content''': INSTALL_CONTENT}]
  __A   :     Tuple      =			{
      '''{processor_class}''': '''FakeProcessorClass''',
      '''{model_class}''': '''FakeModelClass''',
      '''{object_class}''': '''FakeObjectClass''',
  }
  
  
  
  
  
  