
"""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	(						__lowerCamelCase,						__lowerCamelCase    ):
   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 ):




  '''simple docstring'''







  def __init__(						self							,  lowercase_							,  lowercase_=True							,  lowercase_=None    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       optimizer
     UpperCAmelCase_   :   Any     	=       scaler
     UpperCAmelCase_   :   str     	=       AcceleratorState()
     UpperCAmelCase_   :   Tuple     	=       GradientState()
     UpperCAmelCase_   :   Dict     	=       device_placement
     UpperCAmelCase_   :   List[str]     	=       False
     UpperCAmelCase_   :   List[str]     	=       None

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







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.optimizer.state







  @state.setter
  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       state







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.optimizer.param_groups







  @param_groups.setter
  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       param_groups







  @property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.optimizer.defaults







  @defaults.setter
  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       defaults







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     self.optimizer.add_param_group(lowercase_    )







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
        xm.send_cpu_data_to_device(lowercase_							,  self.accelerator_state.device    )
     self.optimizer.load_state_dict(lowercase_    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return self.optimizer.state_dict()







  def 			UpperCamelCase__   (						self							,  lowercase_=None    ):

     """simple docstring"""
     if self.gradient_state.sync_gradients:
        UpperCAmelCase_   :   int     	=       "set_to_none" in inspect.signature(self.optimizer.zero_grad    ).parameters
        if accept_arg:
           if set_to_none is None:
              UpperCAmelCase_   :   Optional[int]     	=       False
           self.optimizer.zero_grad(set_to_none=lowercase_    )
        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							,  lowercase_=None    ):

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

        else:
           self.optimizer.step(lowercase_    )







  def 			UpperCamelCase__   (						self							,  lowercase_    ):

     """simple docstring"""
     for param_group in self.optimizer.param_groups:
        UpperCAmelCase_   :   List[Any]     	=       [parameters_map.get(lowercase_							,  lowercase_    ) 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."							,  lowercase_							,  )
     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							,  lowercase_    ):

     """simple docstring"""
     self.__dict__.update(lowercase_    )






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__)

