





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 _a       (			lowerCamelCase:     List[str]      ,       lowerCamelCase:     Any				)			->		Any:






      if isinstance(lowerCamelCase      ,       (list, tuple)				):
            return honor_type(lowerCamelCase      ,       (move_to_device(lowerCamelCase      ,       lowerCamelCase				) for t in state)				)
      elif isinstance(lowerCamelCase      ,       lowerCamelCase				):
            return type(lowerCamelCase				)({k: move_to_device(lowerCamelCase      ,       lowerCamelCase				) for k, v in state.items()}				)
      elif isinstance(lowerCamelCase      ,       torch.Tensor				):
            return state.to(lowerCamelCase				)
      return state







class A_      (    torch.optim.Optimizer     ):




   def __init__(self			:Dict					,     _UpperCamelCase			:List[str]					,     _UpperCamelCase			:Optional[int]=True					,     _UpperCamelCase			:List[Any]=None				)->      List[Any]:
         __A      =       optimizer
         __A      =       scaler
         __A      =       AcceleratorState()
         __A      =       GradientState()
         __A      =       device_placement
         __A      =       False
         __A      =       None

         # Handle device placement
         if device_placement:
               __A      =       self.optimizer.state_dict()
               if self.accelerator_state.distributed_type == DistributedType.TPU:
                     xm.send_cpu_data_to_device(_UpperCamelCase					,     self.accelerator_state.device				)
               else:
                     __A      =       move_to_device(_UpperCamelCase					,     self.accelerator_state.device				)
               self.optimizer.load_state_dict(_UpperCamelCase				)




   @property
   def _lowerCAmelCase   (self			:Optional[Any]				)->      Optional[Any]:
         return self.optimizer.state




   @state.setter
   def _lowerCAmelCase   (self			:Optional[Any]					,     _UpperCamelCase			:Dict				)->      Union[str, Any]:
         __A      =       state




   @property
   def _lowerCAmelCase   (self			:Optional[int]				)->      Tuple:
         return self.optimizer.param_groups




   @param_groups.setter
   def _lowerCAmelCase   (self			:Optional[Any]					,     _UpperCamelCase			:int				)->      int:
         __A      =       param_groups




   @property
   def _lowerCAmelCase   (self			:Dict				)->      List[Any]:
         return self.optimizer.defaults




   @defaults.setter
   def _lowerCAmelCase   (self			:Any					,     _UpperCamelCase			:int				)->      Optional[Any]:
         __A      =       defaults




   def _lowerCAmelCase   (self			:Dict					,     _UpperCamelCase			:Dict				)->      List[Any]:
         self.optimizer.add_param_group(_UpperCamelCase				)




   def _lowerCAmelCase   (self			:Dict					,     _UpperCamelCase			:Union[str, Any]				)->      Tuple:
         if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
               xm.send_cpu_data_to_device(_UpperCamelCase					,     self.accelerator_state.device				)
         self.optimizer.load_state_dict(_UpperCamelCase				)




   def _lowerCAmelCase   (self			:Union[str, Any]				)->      Any:
         return self.optimizer.state_dict()




   def _lowerCAmelCase   (self			:str					,     _UpperCamelCase			:Dict=None				)->      Dict:
         if self.gradient_state.sync_gradients:
               __A      =       '''set_to_none''' in inspect.signature(self.optimizer.zero_grad				).parameters
               if accept_arg:
                     if set_to_none is None:
                           __A      =       False
                     self.optimizer.zero_grad(set_to_none=_UpperCamelCase				)
               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 _lowerCAmelCase   (self			:Tuple					,     _UpperCamelCase			:Optional[Any]=None				)->      Union[str, Any]:
         if self.gradient_state.sync_gradients:
               if self.accelerator_state.distributed_type == DistributedType.TPU:
                     __A      =       {'''closure''': closure} if closure is not None else {}
                     xm.optimizer_step(self.optimizer					,     optimizer_args=_UpperCamelCase				)
               elif self.scaler is not None:
                     __A      =       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
                           __A      =       self.scaler.get_scale()
                           __A      =       True
                     self.scaler.step(self.optimizer					,     _UpperCamelCase				)
                     self.scaler.update()
                     __A      =       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.
                           __A      =       scale_after < self._last_scale
                     __A      =       scale_after

               else:
                     self.optimizer.step(_UpperCamelCase				)




   def _lowerCAmelCase   (self			:List[str]					,     _UpperCamelCase			:Union[str, Any]				)->      Any:
         for param_group in self.optimizer.param_groups:
               __A      =       [parameters_map.get(_UpperCamelCase					,     _UpperCamelCase				) for p in param_group['''params''']]




   @property
   def _lowerCAmelCase   (self			:Union[str, Any]				)->      Any:
         warnings.warn(
             '''The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use '''
             '''`optimizer.step_was_skipped` instead.'''					,     _UpperCamelCase					,     )
         return self._is_overflow




   @property
   def _lowerCAmelCase   (self			:List[str]				)->      Any:
         return self._is_overflow




   def __getstate__(self			:Optional[int]				)->      str:
         return self.__dict__.copy()






   def __setstate__(self			:Optional[int]					,     _UpperCamelCase			:List[str]				)->      List[str]:
         self.__dict__.update(_UpperCamelCase				)












from unittest.mock import patch

import datasets
from datasets import Dataset



def _a       (			)			->		Optional[Any]:







      '''simple docstring'''






      __A      =       Dataset.from_dict({'''col_1''': [3, 2, 0, 1]}				)

      with patch('''tqdm.auto.tqdm'''				) as mock_tqdm:
            datasets.disable_progress_bar()
            dset.map(lambda lowerCamelCase				: {"col_2": x["col_1"] + 1}				)
            mock_tqdm.assert_not_called()

            mock_tqdm.reset_mock()

            datasets.enable_progress_bar()
            dset.map(lambda lowerCamelCase				: {"col_2": x["col_1"] + 1}				)
            mock_tqdm.assert_called()





