






'''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 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase ) ->			List[str]:
							if isinstance(UpperCamelCase							,     (list, tuple) ):
														return honor_type(UpperCamelCase							,     (move_to_device(UpperCamelCase							,     UpperCamelCase ) for t in state) )
							elif isinstance(UpperCamelCase							,     UpperCamelCase ):
														return type(UpperCamelCase )({k: move_to_device(UpperCamelCase							,     UpperCamelCase ) for k, v in state.items()} )
							elif isinstance(UpperCamelCase							,     torch.Tensor ):
														return state.to(UpperCamelCase )
							return state



class _lowercase      (							torch.optim.Optimizer						):




							def __init__(				self:							List[str]     ,				UpperCamelCase__:							Optional[Any]     ,				UpperCamelCase__:							Dict=True     ,				UpperCamelCase__:							str=None	):
														lowerCamelCase__   :      Any			    =			optimizer
														lowerCamelCase__   :      Union[str, Any]			    =			scaler
														lowerCamelCase__   :      Tuple			    =			AcceleratorState()
														lowerCamelCase__   :      List[str]			    =			GradientState()
														lowerCamelCase__   :      Any			    =			device_placement
														lowerCamelCase__   :      Any			    =			False
														lowerCamelCase__   :      int			    =			None

														# Handle device placement
														if device_placement:
																					lowerCamelCase__   :      Union[str, Any]			    =			self.optimizer.state_dict()
																					if self.accelerator_state.distributed_type == DistributedType.TPU:
																												xm.send_cpu_data_to_device(UpperCamelCase__     ,				self.accelerator_state.device	)
																					else:
																												lowerCamelCase__   :      List[str]			    =			move_to_device(UpperCamelCase__     ,				self.accelerator_state.device	)
																					self.optimizer.load_state_dict(UpperCamelCase__	)




							@property
							def 						lowerCamelCase_							(				self:							Union[str, Any]	):
														return self.optimizer.state




							@state.setter
							def 						lowerCamelCase_							(				self:							Optional[int]     ,				UpperCamelCase__:							Any	):
														lowerCamelCase__   :      List[str]			    =			state




							@property
							def 						lowerCamelCase_							(				self:							Any	):
														return self.optimizer.param_groups




							@param_groups.setter
							def 						lowerCamelCase_							(				self:							List[Any]     ,				UpperCamelCase__:							List[Any]	):
														lowerCamelCase__   :      List[str]			    =			param_groups




							@property
							def 						lowerCamelCase_							(				self:							Optional[Any]	):
														return self.optimizer.defaults




							@defaults.setter
							def 						lowerCamelCase_							(				self:							Optional[int]     ,				UpperCamelCase__:							Dict	):
														lowerCamelCase__   :      Dict			    =			defaults




							def 						lowerCamelCase_							(				self:							List[str]     ,				UpperCamelCase__:							Union[str, Any]	):
														self.optimizer.add_param_group(UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Optional[Any]     ,				UpperCamelCase__:							Union[str, Any]	):
														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]	):
														return self.optimizer.state_dict()




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

																					else:
																												self.optimizer.step(UpperCamelCase__	)




							def 						lowerCamelCase_							(				self:							Optional[Any]     ,				UpperCamelCase__:							List[str]	):
														for param_group in self.optimizer.param_groups:
																					lowerCamelCase__   :      Optional[Any]			    =			[parameters_map.get(UpperCamelCase__     ,				UpperCamelCase__	) for p in param_group["""params"""]]




							@property
							def 						lowerCamelCase_							(				self:							Union[str, 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[Any]	):
														return self._is_overflow




							def __getstate__(				self:							str	):
														return self.__dict__.copy()




							def __setstate__(				self:							int     ,				UpperCamelCase__:							Tuple	):
														self.__dict__.update(UpperCamelCase__	)









'''simple docstring'''







def 					SCREAMING_SNAKE_CASE_			(UpperCamelCase							,     UpperCamelCase = 13 ) ->			str:
							lowerCamelCase__   :      Dict			    =			""""""
							for c in s:
														if "A" <= c <= "Z":
																					out += chr(ord("""A""" ) + (ord(UpperCamelCase ) - ord("""A""" ) + n) % 26 )
														elif "a" <= c <= "z":
																					out += chr(ord("""a""" ) + (ord(UpperCamelCase ) - ord("""a""" ) + n) % 26 )
														else:
																					out += c
							return out





def 					SCREAMING_SNAKE_CASE_			() ->			None:
							lowerCamelCase__   :      Dict			    =			input("""Enter message: """ )

							lowerCamelCase__   :      str			    =			dencrypt(UpperCamelCase							,     13 )
							print("""Encryption:"""							,     UpperCamelCase )

							lowerCamelCase__   :      List[str]			    =			dencrypt(UpperCamelCase							,     13 )
							print("""Decryption: """							,     UpperCamelCase )


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			main()

