'''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 			UpperCamelCase_							(      _UpperCAmelCase						:   Union[str, Any]    ,			_UpperCAmelCase						:   Union[str, Any]					)					->					Union[str, Any]:


       """simple docstring"""







       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 			lowerCamelCase_			(torch.optim.Optimizer					):
      '''simple docstring'''

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

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




      @property
      def   _A			( self		:       Optional[int] ):
             return self.optimizer.state




      @state.setter
      def   _A			( self		:       Any		,      A		:       Union[str, Any] ):
             _UpperCAmelCase  :						List[str]							   =					state




      @property
      def   _A			( self		:       Dict ):
             return self.optimizer.param_groups




      @param_groups.setter
      def   _A			( self		:       int		,      A		:       Optional[int] ):
             _UpperCAmelCase  :						int							   =					param_groups




      @property
      def   _A			( self		:       List[Any] ):
             return self.optimizer.defaults




      @defaults.setter
      def   _A			( self		:       Tuple		,      A		:       Optional[Any] ):
             _UpperCAmelCase  :						Tuple							   =					defaults




      def   _A			( self		:       Union[str, Any]		,      A		:       Union[str, Any] ):
             self.optimizer.add_param_group(A )




      def   _A			( self		:       Any		,      A		:       Union[str, 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		:       Any ):
             return self.optimizer.state_dict()




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

                    else:
                           self.optimizer.step(A )




      def   _A			( self		:       Any		,      A		:       int ):
             for param_group in self.optimizer.param_groups:
                    _UpperCAmelCase  :						Any							   =					[parameters_map.get(A		,      A ) for p in param_group["params"]]




      @property
      def   _A			( 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."		,      A		,      )
             return self._is_overflow




      @property
      def   _A			( self		:       Optional[int] ):
             return self._is_overflow




      def __getstate__( self		:       List[str] ):
             return self.__dict__.copy()





      def __setstate__( self		:       Any		,      A		:       Optional[int] ):
             self.__dict__.update(A )


'''simple docstring'''




from typing import TYPE_CHECKING

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


__SCREAMING_SNAKE_CASE :  Union[str, Any]  =						{
    """configuration_gptsan_japanese""": ["""GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP""", """GPTSanJapaneseConfig"""],
    """tokenization_gptsan_japanese""": ["""GPTSanJapaneseTokenizer"""],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __SCREAMING_SNAKE_CASE :  Optional[int]  =						[
      """GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """GPTSanJapaneseForConditionalGeneration""",
      """GPTSanJapaneseModel""",
      """GPTSanJapanesePreTrainedModel""",
  ]
  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						[
      """GPTSanJapaneseTokenizer""",
  ]


if TYPE_CHECKING:
  from .configuration_gptsan_japanese import GPTSAN_JAPANESE_PRETRAINED_CONFIG_ARCHIVE_MAP, GPTSanJapaneseConfig
  from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_gptsan_japanese import (
        GPTSAN_JAPANESE_PRETRAINED_MODEL_ARCHIVE_LIST,
        GPTSanJapaneseForConditionalGeneration,
        GPTSanJapaneseModel,
        GPTSanJapanesePreTrainedModel,
    )
    from .tokenization_gptsan_japanese import GPTSanJapaneseTokenizer


else:
  import sys

  __SCREAMING_SNAKE_CASE :  Union[str, Any]  =						_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)

