




'''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			(     _UpperCamelCase:					Tuple      ,		_UpperCamelCase:					Tuple					)		->				List[str]:

     """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      _a       (		torch.optim.Optimizer							):






     def __init__(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE=True      ,_SCREAMING_SNAKE_CASE=None					)							->    List[Any]:
          _snake_case								=   optimizer
          _snake_case								=   scaler
          _snake_case								=   AcceleratorState()
          _snake_case								=   GradientState()
          _snake_case								=   device_placement
          _snake_case								=   False
          _snake_case								=   None

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






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






     @state.setter
     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    Dict:
          _snake_case								=   state






     @property
     def        _lowercase			(				self					)							->    int:
          return self.optimizer.param_groups






     @param_groups.setter
     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    List[str]:
          _snake_case								=   param_groups






     @property
     def        _lowercase			(				self					)							->    Optional[Any]:
          return self.optimizer.defaults






     @defaults.setter
     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    Dict:
          _snake_case								=   defaults






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    Any:
          self.optimizer.add_param_group(_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    Dict:
          if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
               xm.send_cpu_data_to_device(_SCREAMING_SNAKE_CASE      ,self.accelerator_state.device					)
          self.optimizer.load_state_dict(_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self					)							->    str:
          return self.optimizer.state_dict()






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

               else:
                    self.optimizer.step(_SCREAMING_SNAKE_CASE					)






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE					)							->    str:
          for param_group in self.optimizer.param_groups:
               _snake_case								=   [parameters_map.get(_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE					) for p in param_group["params"]]






     @property
     def        _lowercase			(				self					)							->    List[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."      ,_SCREAMING_SNAKE_CASE      ,)
          return self._is_overflow






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






     def __getstate__(				self					)							->    List[Any]:
          return self.__dict__.copy()






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








'''simple docstring'''





from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_clip import CLIPTokenizer


UpperCamelCase_		:    Optional[int]									=		logging.get_logger(__name__)

UpperCamelCase_		:    str									=		{'''vocab_file''': '''vocab.json''', '''merges_file''': '''merges.txt''', '''tokenizer_file''': '''tokenizer.json'''}

UpperCamelCase_		:    List[str]									=		{
    '''vocab_file''': {
        '''openai/clip-vit-base-patch32''': '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''openai/clip-vit-base-patch32''': '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/merges.txt''',
    },
    '''tokenizer_file''': {
        '''openai/clip-vit-base-patch32''': (
            '''https://huggingface.co/openai/clip-vit-base-patch32/resolve/main/tokenizer.json'''
        ),
    },
}

UpperCamelCase_		:    List[Any]									=		{
    '''openai/clip-vit-base-patch32''': 77,
}

class      _a       (		__lowerCAmelCase							):
     SCREAMING_SNAKE_CASE_							:					Any					       =		VOCAB_FILES_NAMES
     SCREAMING_SNAKE_CASE_							:					List[Any]					       =		PRETRAINED_VOCAB_FILES_MAP
     SCREAMING_SNAKE_CASE_							:					Dict					       =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     SCREAMING_SNAKE_CASE_							:					int					       =		["""input_ids""", """attention_mask"""]
     SCREAMING_SNAKE_CASE_							:					Optional[Any]					       =		CLIPTokenizer






     def __init__(				self      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE=None      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,_SCREAMING_SNAKE_CASE="<|startoftext|>"      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,_SCREAMING_SNAKE_CASE="<|endoftext|>"      ,**_SCREAMING_SNAKE_CASE      ,)							->    List[Any]:
          super().__init__(
              _SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE      ,tokenizer_file=_SCREAMING_SNAKE_CASE      ,unk_token=_SCREAMING_SNAKE_CASE      ,bos_token=_SCREAMING_SNAKE_CASE      ,eos_token=_SCREAMING_SNAKE_CASE      ,pad_token=_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE      ,)

          if not isinstance(self.backend_tokenizer.pre_tokenizer      ,pre_tokenizers.Sequence					):
               raise ValueError(
                   "The `backend_tokenizer` provided does not match the expected format. The CLIP tokenizer has been"
                   " heavily modified from transformers version 4.17.0. You need to convert the tokenizer you are using"
                   " to be compatible with this version.The easiest way to do so is"
                   " `CLIPTokenizerFast.from_pretrained(\"path_to_local_folder_or_hub_repo, from_slow=True)`. If you want"
                   " to use your existing tokenizer, you will have to revert to a version prior to 4.17.0 of"
                   " transformers."					)

          self._wrap_decode_method_backend_tokenizer()






     def        _lowercase			(				self					)							->    Tuple:
          _snake_case							=   self.backend_tokenizer.decode

          def new_decode_method(*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					):
               _snake_case							=   orig_decode_method(*_SCREAMING_SNAKE_CASE      ,**_SCREAMING_SNAKE_CASE					)
               _snake_case							=   text.replace(self.backend_tokenizer.model.end_of_word_suffix      ," "					).strip()
               return text

          _snake_case							=   new_decode_method






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    List[int]:
          _snake_case							=   [self.bos_token_id]
          _snake_case							=   [self.eos_token_id]

          if token_ids_a is None:
               return bos_token + token_ids_a + eos_token
          return bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    List[int]:
          _snake_case							=   [self.bos_token_id]
          _snake_case							=   [self.eos_token_id]

          if token_ids_a is None:
               return len(bos_token + token_ids_a + eos_token					) * [0]
          return len(bos_token + token_ids_a + eos_token + eos_token + token_ids_a + eos_token					) * [0]






     def        _lowercase			(				self      ,_SCREAMING_SNAKE_CASE      ,_SCREAMING_SNAKE_CASE = None					)							->    Tuple[str]:
          _snake_case							=   self._tokenizer.model.save(_SCREAMING_SNAKE_CASE      ,name=_SCREAMING_SNAKE_CASE					)
          return tuple(_SCREAMING_SNAKE_CASE					)


