
'''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 							A_  (    __SCREAMING_SNAKE_CASE    :	Union[str, Any]      ,  __SCREAMING_SNAKE_CASE    :	Dict			)					->							Any:





  """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      :     str					, lowerCamelCase      :     Union[str, Any]					, lowerCamelCase      :     List[str]=True					, lowerCamelCase      :     Tuple=None							):



   """simple docstring"""





   __A		:  List[Any]       				=		optimizer
   __A		:  str       				=		scaler
   __A		:  Dict       				=		AcceleratorState()
   __A		:  Optional[int]       				=		GradientState()
   __A		:  Optional[int]       				=		device_placement
   __A		:  List[str]       				=		False
   __A		:  Optional[Any]       				=		None

   # Handle device placement
   if device_placement:
     __A		:  Any       				=		self.optimizer.state_dict()
     if self.accelerator_state.distributed_type == DistributedType.TPU:
       xm.send_cpu_data_to_device(_lowerCAmelCase					, self.accelerator_state.device							)
     else:
       __A		:  Tuple       				=		move_to_device(_lowerCAmelCase					, self.accelerator_state.device							)
     self.optimizer.load_state_dict(_lowerCAmelCase							)
 @property
 def 		lowercase_( self      :     Dict							):



   """simple docstring"""





   return self.optimizer.state
 @state.setter
 def 		lowercase_( self      :     List[str]					, lowerCamelCase      :     List[str]							):



   """simple docstring"""





   __A		:  str       				=		state
 @property
 def 		lowercase_( self      :     Dict							):



   """simple docstring"""





   return self.optimizer.param_groups
 @param_groups.setter
 def 		lowercase_( self      :     str					, lowerCamelCase      :     Optional[Any]							):



   """simple docstring"""





   __A		:  Tuple       				=		param_groups
 @property
 def 		lowercase_( self      :     Tuple							):



   """simple docstring"""





   return self.optimizer.defaults
 @defaults.setter
 def 		lowercase_( self      :     str					, lowerCamelCase      :     Tuple							):



   """simple docstring"""





   __A		:  Union[str, Any]       				=		defaults
 def 		lowercase_( self      :     Union[str, Any]					, lowerCamelCase      :     List[str]							):



   """simple docstring"""





   self.optimizer.add_param_group(_lowerCAmelCase							)
 def 		lowercase_( self      :     Tuple					, lowerCamelCase      :     List[Any]							):



   """simple docstring"""





   if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
     xm.send_cpu_data_to_device(_lowerCAmelCase					, self.accelerator_state.device							)
   self.optimizer.load_state_dict(_lowerCAmelCase							)
 def 		lowercase_( self      :     str							):



   """simple docstring"""





   return self.optimizer.state_dict()
 def 		lowercase_( self      :     Tuple					, lowerCamelCase      :     Any=None							):



   """simple docstring"""





   if self.gradient_state.sync_gradients:
     __A		:  Union[str, Any]       				=		"""set_to_none""" in inspect.signature(self.optimizer.zero_grad							).parameters
     if accept_arg:
       if set_to_none is None:
         __A		:  Dict       				=		False
       self.optimizer.zero_grad(set_to_none=_lowerCAmelCase							)
     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      :     Dict					, lowerCamelCase      :     int=None							):



   """simple docstring"""





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

     else:
       self.optimizer.step(_lowerCAmelCase							)
 def 		lowercase_( self      :     Dict					, lowerCamelCase      :     Any							):



   """simple docstring"""





   for param_group in self.optimizer.param_groups:
     __A		:  Optional[Any]       				=		[parameters_map.get(_lowerCAmelCase					, _lowerCAmelCase							) for p in param_group["""params"""]]
 @property
 def 		lowercase_( self      :     Optional[Any]							):



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



   """simple docstring"""





   return self._is_overflow
 def __getstate__( self      :     Tuple							):



   """simple docstring"""





   return self.__dict__.copy()






 def __setstate__( self      :     Dict					, lowerCamelCase      :     int							):



   """simple docstring"""





   self.__dict__.update(_lowerCAmelCase							)







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


UpperCamelCase			:					Optional[int]         =							{
    '''configuration_dpr''': ['''DPR_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DPRConfig'''],
    '''tokenization_dpr''': [
        '''DPRContextEncoderTokenizer''',
        '''DPRQuestionEncoderTokenizer''',
        '''DPRReaderOutput''',
        '''DPRReaderTokenizer''',
    ],
}


try:
					if not is_tokenizers_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					UpperCamelCase			:					List[str]         =							[
					    '''DPRContextEncoderTokenizerFast''',
					    '''DPRQuestionEncoderTokenizerFast''',
					    '''DPRReaderTokenizerFast''',
					]

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					UpperCamelCase			:					Optional[Any]         =							[
					    '''DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''DPRContextEncoder''',
					    '''DPRPretrainedContextEncoder''',
					    '''DPRPreTrainedModel''',
					    '''DPRPretrainedQuestionEncoder''',
					    '''DPRPretrainedReader''',
					    '''DPRQuestionEncoder''',
					    '''DPRReader''',
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					UpperCamelCase			:					Optional[int]         =							[
					    '''TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST''',
					    '''TFDPRContextEncoder''',
					    '''TFDPRPretrainedContextEncoder''',
					    '''TFDPRPretrainedQuestionEncoder''',
					    '''TFDPRPretrainedReader''',
					    '''TFDPRQuestionEncoder''',
					    '''TFDPRReader''',
					]


if TYPE_CHECKING:
					from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
					from .tokenization_dpr import (
					    DPRContextEncoderTokenizer,
					    DPRQuestionEncoderTokenizer,
					    DPRReaderOutput,
					    DPRReaderTokenizer,
					)

					try:
										if not is_tokenizers_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_dpr_fast import (
										    DPRContextEncoderTokenizerFast,
										    DPRQuestionEncoderTokenizerFast,
										    DPRReaderTokenizerFast,
										)

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_dpr import (
										    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    DPRContextEncoder,
										    DPRPretrainedContextEncoder,
										    DPRPreTrainedModel,
										    DPRPretrainedQuestionEncoder,
										    DPRPretrainedReader,
										    DPRQuestionEncoder,
										    DPRReader,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_dpr import (
										    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFDPRContextEncoder,
										    TFDPRPretrainedContextEncoder,
										    TFDPRPretrainedQuestionEncoder,
										    TFDPRPretrainedReader,
										    TFDPRQuestionEncoder,
										    TFDPRReader,
										)

else:
					import sys

					UpperCamelCase			:					List[Any]         =							_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)

