






"""simple docstring"""







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 						lowercase__(A					,		A						)				->List[Any]:




						"""simple docstring"""



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







class     __UpperCAmelCase(   torch.optim.Optimizer							):




						"""simple docstring"""




						def __init__(   self       ,  snake_case__       ,  snake_case__=True       ,  snake_case__=None			):
												'''simple docstring'''
												lowercase__				:      str=       optimizer
												lowercase__				:      Any=       scaler
												lowercase__				:      Union[str, Any]=       AcceleratorState()
												lowercase__				:      List[Any]=       GradientState()
												lowercase__				:      List[str]=       device_placement
												lowercase__				:      Tuple=       False
												lowercase__				:      Optional[int]=       None

												# Handle device placement
												if device_placement:
																		lowercase__				:      Optional[int]=       self.optimizer.state_dict()
																		if self.accelerator_state.distributed_type == DistributedType.TPU:
																								xm.send_cpu_data_to_device(snake_case__       ,  self.accelerator_state.device			)
																		else:
																								lowercase__				:      Optional[Any]=       move_to_device(snake_case__       ,  self.accelerator_state.device			)
																		self.optimizer.load_state_dict(snake_case__			)




						@property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return self.optimizer.state




						@state.setter
						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      List[str]=       state




						@property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return self.optimizer.param_groups




						@param_groups.setter
						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      int=       param_groups




						@property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return self.optimizer.defaults




						@defaults.setter
						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												lowercase__				:      Dict=       defaults




						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												self.optimizer.add_param_group(snake_case__			)




						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
																		xm.send_cpu_data_to_device(snake_case__       ,  self.accelerator_state.device			)
												self.optimizer.load_state_dict(snake_case__			)




						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return self.optimizer.state_dict()




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

																		else:
																								self.optimizer.step(snake_case__			)




						def 		UpperCAmelCase_       (   self       ,  snake_case__			):
												'''simple docstring'''
												for param_group in self.optimizer.param_groups:
																		lowercase__				:      Optional[Any]=       [parameters_map.get(snake_case__       ,  snake_case__			) for p in param_group["params"]]




						@property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												warnings.warn(
												    "The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use "
												    "`optimizer.step_was_skipped` instead."       ,  snake_case__       ,  )
												return self._is_overflow




						@property
						def 		UpperCAmelCase_       (   self			):
												'''simple docstring'''
												return self._is_overflow




						def __getstate__(   self			):
												'''simple docstring'''
												return self.__dict__.copy()




						def __setstate__(   self       ,  snake_case__			):
												'''simple docstring'''
												self.__dict__.update(snake_case__			)














"""simple docstring"""







def 						lowercase__(A						)				->int:




						"""simple docstring"""



						lowercase__				:      str=       0
						for plain_chr in plain_text:
												lowercase__				:      List[str]=       (
												    ord(A						) + (hash_value << 6) + (hash_value << 16) - hash_value
												)
						return hash_value






