


import copy
import os
from typing import Union

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


UpperCamelCase								=	logging.get_logger(__name__)

UpperCamelCase								=	{
    "Salesforce/blip-vqa-base": "https://huggingface.co/Salesforce/blip-vqa-base/resolve/main/config.json",
    "Salesforce/blip-vqa-capfit-large": (
        "https://huggingface.co/Salesforce/blip-vqa-base-capfit/resolve/main/config.json"
    ),
    "Salesforce/blip-image-captioning-base": (
        "https://huggingface.co/Salesforce/blip-image-captioning-base/resolve/main/config.json"
    ),
    "Salesforce/blip-image-captioning-large": (
        "https://huggingface.co/Salesforce/blip-image-captioning-large/resolve/main/config.json"
    ),
    "Salesforce/blip-itm-base-coco": "https://huggingface.co/Salesforce/blip-itm-base-coco/resolve/main/config.json",
    "Salesforce/blip-itm-large-coco": "https://huggingface.co/Salesforce/blip-itm-large-coco/resolve/main/config.json",
    "Salesforce/blip-itm-base-flikr": "https://huggingface.co/Salesforce/blip-itm-base-flikr/resolve/main/config.json",
    "Salesforce/blip-itm-large-flikr": (
        "https://huggingface.co/Salesforce/blip-itm-large-flikr/resolve/main/config.json"
    ),
}






class   _lowerCamelCase (					_SCREAMING_SNAKE_CASE			):




       """simple docstring"""
       snake_case      =    "blip_text_model"



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=3_0524				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=3072				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=8				,       _SCREAMING_SNAKE_CASE=512				,       _SCREAMING_SNAKE_CASE="gelu"				,       _SCREAMING_SNAKE_CASE=1e-12				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=0.0_2				,       _SCREAMING_SNAKE_CASE=3_0522				,       _SCREAMING_SNAKE_CASE=2				,       _SCREAMING_SNAKE_CASE=0				,       _SCREAMING_SNAKE_CASE=102				,       _SCREAMING_SNAKE_CASE=True				,       _SCREAMING_SNAKE_CASE=True				,       **_SCREAMING_SNAKE_CASE				,       )->Any:




         '''simple docstring'''





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

         A_      :				Union[str, Any]		     =					vocab_size
         A_      :				Dict		     =					hidden_size
         A_      :				Union[str, Any]		     =					encoder_hidden_size
         A_      :				List[str]		     =					intermediate_size
         A_      :				List[Any]		     =					projection_dim
         A_      :				List[Any]		     =					hidden_dropout_prob
         A_      :				int		     =					num_hidden_layers
         A_      :				Tuple		     =					num_attention_heads
         A_      :				Dict		     =					max_position_embeddings
         A_      :				int		     =					layer_norm_eps
         A_      :				Any		     =					hidden_act
         A_      :				List[str]		     =					initializer_range
         A_      :				str		     =					attention_probs_dropout_prob
         A_      :				int		     =					is_decoder
         A_      :				Dict		     =					use_cache







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(A_		)

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

         # get the text config dict if we are loading from BlipConfig
         if config_dict.get('''model_type'''		) == "blip":
           A_      :				str		     =					config_dict['''text_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   _lowerCamelCase (					_SCREAMING_SNAKE_CASE			):




       """simple docstring"""
       snake_case      =    "blip_vision_model"



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=768				,       _SCREAMING_SNAKE_CASE=3072				,       _SCREAMING_SNAKE_CASE=512				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=12				,       _SCREAMING_SNAKE_CASE=384				,       _SCREAMING_SNAKE_CASE=16				,       _SCREAMING_SNAKE_CASE="gelu"				,       _SCREAMING_SNAKE_CASE=1e-5				,       _SCREAMING_SNAKE_CASE=0.0				,       _SCREAMING_SNAKE_CASE=1e-10				,       **_SCREAMING_SNAKE_CASE				,       )->List[str]:




         '''simple docstring'''





         super().__init__(**A_		)

         A_      :				List[Any]		     =					hidden_size
         A_      :				Tuple		     =					intermediate_size
         A_      :				str		     =					projection_dim
         A_      :				Optional[int]		     =					num_hidden_layers
         A_      :				Optional[Any]		     =					num_attention_heads
         A_      :				Any		     =					patch_size
         A_      :				List[Any]		     =					image_size
         A_      :				Optional[int]		     =					initializer_range
         A_      :				Dict		     =					attention_dropout
         A_      :				int		     =					layer_norm_eps
         A_      :				str		     =					hidden_act







       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->"PretrainedConfig":




         '''simple docstring'''





         cls._set_token_in_kwargs(A_		)

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

         # get the vision config dict if we are loading from BlipConfig
         if config_dict.get('''model_type'''		) == "blip":
           A_      :				int		     =					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   _lowerCamelCase (					_SCREAMING_SNAKE_CASE			):




       """simple docstring"""
       snake_case      =    "blip"
       snake_case      =    True



       def __init__(      self				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=None				,       _SCREAMING_SNAKE_CASE=512				,       _SCREAMING_SNAKE_CASE=2.6_5_9_2				,       _SCREAMING_SNAKE_CASE=256				,       **_SCREAMING_SNAKE_CASE				,       )->str:




         '''simple docstring'''





         super().__init__(**A_		)

         if text_config is None:
           A_      :				Union[str, Any]		     =					{}
           logger.info('''`text_config` is `None`. Initializing the `BlipTextConfig` with default values.'''		)

         if vision_config is None:
           A_      :				int		     =					{}
           logger.info('''`vision_config` is `None`. Initializing the `BlipVisionConfig` with default values.'''		)

         A_      :				Dict		     =					BlipTextConfig(**A_		)
         A_      :				List[str]		     =					BlipVisionConfig(**A_		)

         A_      :				Tuple		     =					self.vision_config.hidden_size

         A_      :				Any		     =					projection_dim
         A_      :				List[Any]		     =					logit_scale_init_value
         A_      :				List[Any]		     =					1.0
         A_      :				Dict		     =					0.0_2
         A_      :				List[str]		     =					image_text_hidden_size



       @classmethod
       def        _snake_case       (      cls				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       **_SCREAMING_SNAKE_CASE		)->Optional[Any]:




         '''simple docstring'''





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







       def        _snake_case       (      self		)->int:




         '''simple docstring'''





         A_      :				str		     =					copy.deepcopy(self.__dict__		)
         A_      :				Dict		     =					self.text_config.to_dict()
         A_      :				Optional[Any]		     =					self.vision_config.to_dict()
         A_      :				Union[str, Any]		     =					self.__class__.model_type
         return output






import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
					import jieba

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


_UpperCAmelCase					: List[Any]      =       logging.get_logger(__name__)

_UpperCAmelCase					: int      =       {"vocab_file": "vocab.txt"}

_UpperCAmelCase					: str      =       {
    "vocab_file": {
        "openbmb/cpm-ant-10b": "https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt",
    },
}

_UpperCAmelCase					: Optional[Any]      =       {
    "openbmb/cpm-ant-10b": 1_024,
}


def       A       (					lowercase )   ->  Dict:
		'''simple docstring'''

		UpperCamelCase		=     collections.OrderedDict()
		with open(lowercase    ,       'r'    ,       encoding='utf-8' ) as reader:
				UpperCamelCase		=     reader.readlines()
		for index, token in enumerate(lowercase ):
				UpperCamelCase		=     token.rstrip('\n' )
				UpperCamelCase		=     index
		return vocab


class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):



		def __init__( self				,      A_				,      A_="<unk>"				,      A_=200       )  ->					Dict:
				"""simple docstring"""
				UpperCamelCase		=     vocab
				UpperCamelCase		=     unk_token
				UpperCamelCase		=     max_input_chars_per_word



		def __UpperCamelCase ( self				,      A_       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     list(A_       )
				if len(A_       ) > self.max_input_chars_per_word:
						return [self.unk_token]

				UpperCamelCase		=     0
				UpperCamelCase		=     []
				while start < len(A_       ):
						UpperCamelCase		=     len(A_       )
						UpperCamelCase		=     None
						while start < end:
								UpperCamelCase		=     ''.join(chars[start:end]       )
								if substr in self.vocab:
										UpperCamelCase		=     substr
										break
								end -= 1
						if cur_substr is None:
								sub_tokens.append(self.unk_token       )
								start += 1
						else:
								sub_tokens.append(A_       )
								UpperCamelCase		=     end

				return sub_tokens






class 				lowercase					(		_SCREAMING_SNAKE_CASE   ):
		__lowercase       :       List[str]	     =				VOCAB_FILES_NAMES
		__lowercase       :       Dict	     =				PRETRAINED_VOCAB_FILES_MAP
		__lowercase       :       int	     =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
		__lowercase       :       Any	     =				["input_ids", "attention_mask"]
		__lowercase       :       Tuple	     =				False



		def __init__( self				,      A_				,      A_="<d>"				,      A_="</d>"				,      A_="<s>"				,      A_="</s>"				,      A_="<pad>"				,      A_="<unk>"				,      A_="</n>"				,      A_="</_>"				,      A_="left"				,      **A_				,      )  ->					Tuple:
				"""simple docstring"""
				requires_backends(self				,      ['jieba']       )
				super().__init__(
				    bod_token=A_				,      eod_token=A_				,      bos_token=A_				,      eos_token=A_				,      pad_token=A_				,      unk_token=A_				,      line_token=A_				,      space_token=A_				,      padding_side=A_				,      **A_				,      )
				UpperCamelCase		=     bod_token
				UpperCamelCase		=     eod_token
				UpperCamelCase		=     load_vocab(A_       )
				UpperCamelCase		=     self.encoder[space_token]
				UpperCamelCase		=     self.encoder[line_token]

				del self.encoder[space_token]
				del self.encoder[line_token]

				UpperCamelCase		=     collections.OrderedDict(sorted(self.encoder.items()				,      key=lambda A_       : x[1]       )       )
				UpperCamelCase		=     {v: k for k, v in self.encoder.items()}

				UpperCamelCase		=     WordpieceTokenizer(vocab=self.encoder				,      unk_token=self.unk_token       )



		@property
		def __UpperCamelCase ( self       )  ->					Optional[int]:
				"""simple docstring"""
				return self.encoder[self.bod_token]



		@property
		def __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				return self.encoder[self.eod_token]



		@property
		def __UpperCamelCase ( self       )  ->					Union[str, Any]:
				"""simple docstring"""
				return self.encoder["\n"]



		@property
		def __UpperCamelCase ( self       )  ->					int:
				"""simple docstring"""
				return len(self.encoder       )



		def __UpperCamelCase ( self       )  ->					List[Any]:
				"""simple docstring"""
				return dict(self.encoder				,      **self.added_tokens_encoder       )



		def __UpperCamelCase ( self				,      A_       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     []
				for x in jieba.cut(A_				,      cut_all=A_       ):
						output_tokens.extend(self.wordpiece_tokenizer.tokenize(A_       )       )
				return output_tokens



		def __UpperCamelCase ( self				,      A_				,      **A_       )  ->					Dict:
				"""simple docstring"""
				UpperCamelCase		=     [i for i in token_ids if i >= 0]
				UpperCamelCase		=     [
				    x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
				]
				return super()._decode(A_				,      **A_       )



		def __UpperCamelCase ( self				,      A_       )  ->					int:
				"""simple docstring"""
				return token in self.encoder



		def __UpperCamelCase ( self				,      A_       )  ->					str:
				"""simple docstring"""
				return "".join(A_       )



		def __UpperCamelCase ( self				,      A_       )  ->					Tuple:
				"""simple docstring"""
				return self.encoder.get(A_				,      self.encoder.get(self.unk_token       )       )



		def __UpperCamelCase ( self				,      A_       )  ->					Optional[Any]:
				"""simple docstring"""
				return self.decoder.get(A_				,      self.unk_token       )



		def __UpperCamelCase ( self				,      A_				,      A_ = None       )  ->					Tuple[str]:
				"""simple docstring"""
				if os.path.isdir(A_       ):
						UpperCamelCase		=     os.path.join(
						    A_				,      (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']       )
				else:
						UpperCamelCase		=     (filename_prefix + '-' if filename_prefix else '') + save_directory
				UpperCamelCase		=     0
				if " " in self.encoder:
						UpperCamelCase		=     self.encoder[' ']
						del self.encoder[" "]
				if "\n" in self.encoder:
						UpperCamelCase		=     self.encoder['\n']
						del self.encoder["\n"]
				UpperCamelCase		=     collections.OrderedDict(sorted(self.encoder.items()				,      key=lambda A_       : x[1]       )       )
				with open(A_				,      'w'				,      encoding='utf-8'       ) as writer:
						for token, token_index in self.encoder.items():
								if index != token_index:
										logger.warning(
										    F'''Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive.'''
										    ' Please check that the vocabulary is not corrupted!'       )
										UpperCamelCase		=     token_index
								writer.write(token + '\n'       )
								index += 1
				return (vocab_file,)



		def __UpperCamelCase ( self				,      A_				,      A_ = None       )  ->					List[int]:
				"""simple docstring"""
				if token_ids_a is None:
						return [self.bos_token_id] + token_ids_a
				return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a



		def __UpperCamelCase ( 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 not None:
						return [1] + ([0] * len(A_       )) + [1] + ([0] * len(A_       ))
				return [1] + ([0] * len(A_       ))
