






"""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   (    __snake_case    :			str		, __snake_case    :			int     ):
		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  _UpperCAmelCase	(			torch.optim.Optimizer							):




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

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




					@property
					def   A  (    self					:						Any       )  ->  List[str]:
							return self.optimizer.state




					@state.setter
					def   A  (    self					:						List[Any]   ,						A					:						Dict       )  ->  Tuple:
							lowercase_			:			Optional[Any]								=       state




					@property
					def   A  (    self					:						List[str]       )  ->  Any:
							return self.optimizer.param_groups




					@param_groups.setter
					def   A  (    self					:						Dict   ,						A					:						Optional[Any]       )  ->  List[Any]:
							lowercase_			:			Union[str, Any]								=       param_groups




					@property
					def   A  (    self					:						Union[str, Any]       )  ->  Dict:
							return self.optimizer.defaults




					@defaults.setter
					def   A  (    self					:						int   ,						A					:						Optional[int]       )  ->  Optional[int]:
							lowercase_			:			Any								=       defaults




					def   A  (    self					:						str   ,						A					:						List[str]       )  ->  str:
							self.optimizer.add_param_group(A       )




					def   A  (    self					:						List[str]   ,						A					:						str       )  ->  Optional[Any]:
							if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
									xm.send_cpu_data_to_device(A   ,						self.accelerator_state.device       )
							self.optimizer.load_state_dict(A       )




					def   A  (    self					:						List[Any]       )  ->  Optional[int]:
							return self.optimizer.state_dict()




					def   A  (    self					:						List[str]   ,						A					:						List[str]=None       )  ->  int:
							if self.gradient_state.sync_gradients:
									lowercase_			:			List[str]								=       '''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=A       )
									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   A  (    self					:						Union[str, Any]   ,						A					:						List[Any]=None       )  ->  Tuple:
							if self.gradient_state.sync_gradients:
									if self.accelerator_state.distributed_type == DistributedType.TPU:
											lowercase_			:			int								=       {'''closure''': closure} if closure is not None else {}
											xm.optimizer_step(self.optimizer   ,						optimizer_args=A       )
									elif self.scaler is not None:
											lowercase_			:			List[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_			:			Optional[int]								=       self.scaler.get_scale()
													lowercase_			:			int								=       True
											self.scaler.step(self.optimizer   ,						A       )
											self.scaler.update()
											lowercase_			:			Tuple								=       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_			:			Union[str, Any]								=       scale_after < self._last_scale
											lowercase_			:			Optional[int]								=       scale_after

									else:
											self.optimizer.step(A       )




					def   A  (    self					:						Optional[Any]   ,						A					:						List[Any]       )  ->  Tuple:
							for param_group in self.optimizer.param_groups:
									lowercase_			:			Dict								=       [parameters_map.get(A   ,						A       ) for p in param_group['''params''']]




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




					@property
					def   A  (    self					:						Dict       )  ->  Optional[Any]:
							return self._is_overflow




					def __getstate__(    self					:						Tuple       )  ->  int:
							return self.__dict__.copy()






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











"""simple docstring"""



from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


__A		:	Any      =		{
    '''configuration_markuplm''': ['''MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''MarkupLMConfig'''],
    '''feature_extraction_markuplm''': ['''MarkupLMFeatureExtractor'''],
    '''processing_markuplm''': ['''MarkupLMProcessor'''],
    '''tokenization_markuplm''': ['''MarkupLMTokenizer'''],
}

try:
						if not is_tokenizers_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:	List[str]      =		['''MarkupLMTokenizerFast''']

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__A		:	Optional[int]      =		[
						    '''MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''MarkupLMForQuestionAnswering''',
						    '''MarkupLMForSequenceClassification''',
						    '''MarkupLMForTokenClassification''',
						    '''MarkupLMModel''',
						    '''MarkupLMPreTrainedModel''',
						]


if TYPE_CHECKING:
						from .configuration_markuplm import MARKUPLM_PRETRAINED_CONFIG_ARCHIVE_MAP, MarkupLMConfig
						from .feature_extraction_markuplm import MarkupLMFeatureExtractor
						from .processing_markuplm import MarkupLMProcessor
						from .tokenization_markuplm import MarkupLMTokenizer

						try:
												if not is_tokenizers_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .tokenization_markuplm_fast import MarkupLMTokenizerFast

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_markuplm import (
												    MARKUPLM_PRETRAINED_MODEL_ARCHIVE_LIST,
												    MarkupLMForQuestionAnswering,
												    MarkupLMForSequenceClassification,
												    MarkupLMForTokenClassification,
												    MarkupLMModel,
												    MarkupLMPreTrainedModel,
												)


else:
						import sys

						__A		:	Optional[Any]      =		_LazyModule(__name__, globals()['''__file__'''], _import_structure)



