
"""simple docstring"""




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__   :					Any							=   logging.get_logger(__name__)

A__   :					str							=   {
    'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}


class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       UpperCamelCase_   =		'''altclip_text_model'''
       def __init__(     self ,				A_=25_0002 ,				A_=1024 ,				A_=24 ,				A_=16 ,				A_=4096 ,				A_="gelu" ,				A_=0.1 ,				A_=0.1 ,				A_=514 ,				A_=1 ,				A_=0.02 ,				A_=0.02 ,				A_=1E-05 ,				A_=1 ,				A_=0 ,				A_=2 ,				A_="absolute" ,				A_=True ,				A_=768 ,				**A_ ,				)     ->	int:





         """simple docstring"""




         super().__init__(pad_token_id=A_ ,				bos_token_id=A_ ,				eos_token_id=A_ ,				**A_       )

         _lowercase:			Any       =     vocab_size
         _lowercase:			Dict       =     hidden_size
         _lowercase:			Optional[Any]       =     num_hidden_layers
         _lowercase:			str       =     num_attention_heads
         _lowercase:			str       =     hidden_act
         _lowercase:			Optional[Any]       =     intermediate_size
         _lowercase:			Dict       =     hidden_dropout_prob
         _lowercase:			Dict       =     attention_probs_dropout_prob
         _lowercase:			Any       =     max_position_embeddings
         _lowercase:			List[str]       =     type_vocab_size
         _lowercase:			Tuple       =     initializer_range
         _lowercase:			int       =     initializer_factor
         _lowercase:			str       =     layer_norm_eps
         _lowercase:			str       =     position_embedding_type
         _lowercase:			Optional[int]       =     use_cache
         _lowercase:			Optional[int]       =     project_dim


class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       UpperCamelCase_   =		'''altclip_vision_model'''
       def __init__(     self ,				A_=768 ,				A_=3072 ,				A_=512 ,				A_=12 ,				A_=12 ,				A_=3 ,				A_=224 ,				A_=32 ,				A_="quick_gelu" ,				A_=1E-5 ,				A_=0.0 ,				A_=0.02 ,				A_=1.0 ,				**A_ ,				)     ->	str:





         """simple docstring"""




         super().__init__(**A_       )

         _lowercase:			Union[str, Any]       =     hidden_size
         _lowercase:			str       =     intermediate_size
         _lowercase:			Union[str, Any]       =     projection_dim
         _lowercase:			Dict       =     num_hidden_layers
         _lowercase:			Union[str, Any]       =     num_attention_heads
         _lowercase:			Any       =     num_channels
         _lowercase:			List[str]       =     patch_size
         _lowercase:			str       =     image_size
         _lowercase:			int       =     initializer_range
         _lowercase:			Any       =     initializer_factor
         _lowercase:			Tuple       =     attention_dropout
         _lowercase:			Optional[int]       =     layer_norm_eps
         _lowercase:			Dict       =     hidden_act



       @classmethod
       def 							lowercase_							(     cls ,				A_ ,				**A_       )     ->	"PretrainedConfig":





         """simple docstring"""




         cls._set_token_in_kwargs(A_       )

         _lowercase ,				_lowercase:			List[Any]       =     cls.get_config_dict(A_ ,				**A_       )

         # get the vision config dict if we are loading from AltCLIPConfig
         if config_dict.get('''model_type'''       ) == "altclip":
           _lowercase:			Tuple       =     config_dict['''vision_config''']

         if "model_type" in config_dict and hasattr(cls ,				'''model_type'''       ) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''       )

         return cls.from_dict(A_ ,				**A_       )


class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       UpperCamelCase_   =		'''altclip'''
       UpperCamelCase_   =		True
       def __init__(     self ,				A_=None ,				A_=None ,				A_=768 ,				A_=2.65_92 ,				**A_       )     ->	Optional[int]:





         """simple docstring"""




         _lowercase:			Union[str, Any]       =     kwargs.pop('''text_config_dict''' ,				A_       )
         _lowercase:			List[str]       =     kwargs.pop('''vision_config_dict''' ,				A_       )

         super().__init__(**A_       )

         # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
         # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
         # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
         if text_config_dict is not None:
           if text_config is None:
             _lowercase:			Tuple       =     {}

           # This is the complete result when using `text_config_dict`.
           _lowercase:			Optional[int]       =     AltCLIPTextConfig(**A_       ).to_dict()

           # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
           for key, value in _text_config_dict.items():
             if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
               # If specified in `text_config_dict`
               if key in text_config_dict:
                 _lowercase:			Optional[int]       =     (
                     f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
                     f'''The value `text_config_dict["{key}"]` will be used instead.'''
                 )
               # If inferred from default argument values (just to be super careful)
               else:
                 _lowercase:			Tuple       =     (
                     f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
                     f'''value `text_config["{key}"]` will be overriden.'''
                 )
               logger.warning(A_       )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
           text_config.update(_text_config_dict       )

         if vision_config_dict is not None:
           if vision_config is None:
             _lowercase:			Optional[int]       =     {}

           # This is the complete result when using `vision_config_dict`.
           _lowercase:			Optional[int]       =     AltCLIPVisionConfig(**A_       ).to_dict()
           # convert keys to string instead of integer
           if "id2label" in _vision_config_dict:
             _lowercase:			Union[str, Any]       =     {
                 str(A_       ): value for key, value in _vision_config_dict['''id2label'''].items()
             }

           # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
           for key, value in _vision_config_dict.items():
             if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
               # If specified in `vision_config_dict`
               if key in vision_config_dict:
                 _lowercase:			Optional[int]       =     (
                     f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
                     f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
                 )
               # If inferred from default argument values (just to be super careful)
               else:
                 _lowercase:			List[str]       =     (
                     f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
                     f'''The value `vision_config["{key}"]` will be overriden.'''
                 )
               logger.warning(A_       )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
           vision_config.update(_vision_config_dict       )

         if text_config is None:
           _lowercase:			Dict       =     {}
           logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.'''       )

         if vision_config is None:
           _lowercase:			Union[str, Any]       =     {}
           logger.info('''`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.'''       )

         _lowercase:			List[str]       =     AltCLIPTextConfig(**A_       )
         _lowercase:			List[str]       =     AltCLIPVisionConfig(**A_       )

         _lowercase:			int       =     projection_dim
         _lowercase:			Any       =     logit_scale_init_value
         _lowercase:			Optional[int]       =     1.0
       @classmethod
       def 							lowercase_							(     cls ,				A_ ,				A_ ,				**A_       )     ->	Union[str, Any]:





         """simple docstring"""




         return cls(text_config=text_config.to_dict() ,				vision_config=vision_config.to_dict() ,				**A_       )



       def 							lowercase_							(     self       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			Optional[int]       =     copy.deepcopy(self.__dict__       )
         _lowercase:			Dict       =     self.text_config.to_dict()
         _lowercase:			Optional[Any]       =     self.vision_config.to_dict()
         _lowercase:			List[Any]       =     self.__class__.model_type
         return output


"""simple docstring"""




import os
import re
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


A__   :					str							=   logging.get_logger(__name__)

A__   :					Tuple							=   {
    'vocab_file': 'vocab.txt',
    'merges_file': 'bpe.codes',
}

A__   :					List[Any]							=   {
    'vocab_file': {
        'vinai/phobert-base': 'https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt',
        'vinai/phobert-large': 'https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt',
    },
    'merges_file': {
        'vinai/phobert-base': 'https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes',
        'vinai/phobert-large': 'https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes',
    },
}

A__   :					Any							=   {
    'vinai/phobert-base': 2_5_6,
    'vinai/phobert-large': 2_5_6,
}




def  _lowerCAmelCase		(		_UpperCamelCase						):






  """simple docstring"""



  _lowercase:			Optional[int]       =     set()
  _lowercase:			Union[str, Any]       =     word[0]
  for char in word[1:]:
    pairs.add((prev_char, char)						)
    _lowercase:			Tuple       =     char

  _lowercase:			Optional[Any]       =     set(_UpperCamelCase						)
  return pairs







class 			__magic_name__       (       SCREAMING_SNAKE_CASE__ ):
       UpperCamelCase_   =		VOCAB_FILES_NAMES
       UpperCamelCase_   =		PRETRAINED_VOCAB_FILES_MAP
       UpperCamelCase_   =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       def __init__(     self ,				A_ ,				A_ ,				A_="<s>" ,				A_="</s>" ,				A_="</s>" ,				A_="<s>" ,				A_="<unk>" ,				A_="<pad>" ,				A_="<mask>" ,				**A_ ,				)     ->	Dict:





         """simple docstring"""




         super().__init__(
             bos_token=A_ ,				eos_token=A_ ,				unk_token=A_ ,				sep_token=A_ ,				cls_token=A_ ,				pad_token=A_ ,				mask_token=A_ ,				**A_ ,				)

         _lowercase:			List[Any]       =     vocab_file
         _lowercase:			Union[str, Any]       =     merges_file

         _lowercase:			int       =     {}
         _lowercase:			Optional[int]       =     0
         _lowercase:			Optional[Any]       =     1
         _lowercase:			List[str]       =     2
         _lowercase:			List[Any]       =     3

         self.add_from_file(A_       )

         _lowercase:			Tuple       =     {v: k for k, v in self.encoder.items()}

         with open(A_ ,				encoding='''utf-8'''       ) as merges_handle:
           _lowercase:			int       =     merges_handle.read().split('''\n'''       )[:-1]
         _lowercase:			Optional[int]       =     [tuple(merge.split()[:-1]       ) for merge in merges]
         _lowercase:			List[str]       =     dict(zip(A_ ,				range(len(A_       )       )       )       )
         _lowercase:			Union[str, Any]       =     {}
       def 							lowercase_							(     self ,				A_ ,				A_ = None       )     ->	List[int]:





         """simple docstring"""




         if token_ids_a is None:
           return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         _lowercase:			Optional[Any]       =     [self.cls_token_id]
         _lowercase:			Tuple       =     [self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep
       def 							lowercase_							(     self ,				A_ ,				A_ = None ,				A_ = False       )     ->	List[int]:





         """simple docstring"""




         if already_has_special_tokens:
           return super().get_special_tokens_mask(
               token_ids_a=A_ ,				token_ids_a=A_ ,				already_has_special_tokens=A_       )

         if token_ids_a is None:
           return [1] + ([0] * len(A_       )) + [1]
         return [1] + ([0] * len(A_       )) + [1, 1] + ([0] * len(A_       )) + [1]
       def 							lowercase_							(     self ,				A_ ,				A_ = None       )     ->	List[int]:





         """simple docstring"""




         _lowercase:			Tuple       =     [self.sep_token_id]
         _lowercase:			List[Any]       =     [self.cls_token_id]

         if token_ids_a is None:
           return len(cls + token_ids_a + sep       ) * [0]
         return len(cls + token_ids_a + sep + sep + token_ids_a + sep       ) * [0]
       @property
       def 							lowercase_							(     self       )     ->	int:





         """simple docstring"""




         return len(self.encoder       )
       def 							lowercase_							(     self       )     ->	int:





         """simple docstring"""




         return dict(self.encoder ,				**self.added_tokens_encoder       )
       def 							lowercase_							(     self ,				A_       )     ->	Optional[Any]:





         """simple docstring"""




         if token in self.cache:
           return self.cache[token]
         _lowercase:			List[Any]       =     tuple(A_       )
         _lowercase:			Tuple       =     tuple(list(word[:-1]       ) + [word[-1] + '''</w>''']       )
         _lowercase:			List[Any]       =     get_pairs(A_       )

         if not pairs:
           return token

         while True:
           _lowercase:			List[str]       =     min(A_ ,				key=lambda A_       : self.bpe_ranks.get(A_ ,				float('''inf'''       )       )       )
           if bigram not in self.bpe_ranks:
             break
           _lowercase ,				_lowercase:			Optional[int]       =     bigram
           _lowercase:			Tuple       =     []
           _lowercase:			List[str]       =     0
           while i < len(A_       ):
             try:
               _lowercase:			Tuple       =     word.index(A_ ,				A_       )
             except ValueError:
               new_word.extend(word[i:]       )
               break
             else:
               new_word.extend(word[i:j]       )
               _lowercase:			List[Any]       =     j

             if word[i] == first and i < len(A_       ) - 1 and word[i + 1] == second:
               new_word.append(first + second       )
               i += 2
             else:
               new_word.append(word[i]       )
               i += 1
           _lowercase:			Union[str, Any]       =     tuple(A_       )
           _lowercase:			int       =     new_word
           if len(A_       ) == 1:
             break
           else:
             _lowercase:			List[Any]       =     get_pairs(A_       )
         _lowercase:			Tuple       =     '''@@ '''.join(A_       )
         _lowercase:			Dict       =     word[:-4]
         _lowercase:			Tuple       =     word
         return word
       def 							lowercase_							(     self ,				A_       )     ->	List[Any]:





         """simple docstring"""




         _lowercase:			Dict       =     []

         _lowercase:			str       =     re.findall(R'''\S+\n?''' ,				A_       )

         for token in words:
           split_tokens.extend(list(self.bpe(A_       ).split(''' '''       )       )       )
         return split_tokens
       def 							lowercase_							(     self ,				A_       )     ->	List[str]:





         """simple docstring"""




         return self.encoder.get(A_ ,				self.encoder.get(self.unk_token       )       )
       def 							lowercase_							(     self ,				A_       )     ->	Tuple:





         """simple docstring"""




         return self.decoder.get(A_ ,				self.unk_token       )
       def 							lowercase_							(     self ,				A_       )     ->	Union[str, Any]:





         """simple docstring"""




         _lowercase:			Union[str, Any]       =     ''' '''.join(A_       ).replace('''@@ ''' ,				''''''       ).strip()
         return out_string
       def 							lowercase_							(     self ,				A_ ,				A_ = None       )     ->	Tuple[str]:





         """simple docstring"""




         if not os.path.isdir(A_       ):
           logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''       )
           return
         _lowercase:			List[Any]       =     os.path.join(
             A_ ,				(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']       )
         _lowercase:			str       =     os.path.join(
             A_ ,				(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''merges_file''']       )

         if os.path.abspath(self.vocab_file       ) != os.path.abspath(A_       ):
           copyfile(self.vocab_file ,				A_       )

         if os.path.abspath(self.merges_file       ) != os.path.abspath(A_       ):
           copyfile(self.merges_file ,				A_       )

         return out_vocab_file, out_merge_file



       def 							lowercase_							(     self ,				A_       )     ->	str:





         """simple docstring"""




         if isinstance(A_ ,				A_       ):
           try:
             with open(A_ ,				'''r''' ,				encoding='''utf-8'''       ) as fd:
               self.add_from_file(A_       )
           except FileNotFoundError as fnfe:
             raise fnfe
           except UnicodeError:
             raise Exception(f'''Incorrect encoding detected in {f}, please rebuild the dataset'''       )
           return

         _lowercase:			Dict       =     f.readlines()
         for lineTmp in lines:
           _lowercase:			List[Any]       =     lineTmp.strip()
           _lowercase:			Optional[Any]       =     line.rfind(''' '''       )
           if idx == -1:
             raise ValueError('''Incorrect dictionary format, expected \'<token> <cnt>\''''       )
           _lowercase:			List[str]       =     line[:idx]
           _lowercase:			List[str]       =     len(self.encoder       )
