





import warnings
from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


_SCREAMING_SNAKE_CASE							=						logging.get_logger(__name__)
class SCREAMING_SNAKE_CASE_  (    lowerCAmelCase__    ):
						__magic_name__:	List[Any]       					=				["""input_ids""", """attention_mask"""]






						def __init__(					self					:					Any						,      _A					:					int="</s>"						,      _A					:					Optional[Any]="<unk>"						,      _A					:					Any="<pad>"						,      _A					:					Any=125						,      _A					:					Tuple=None						,      **_A					:					Dict						,      )							->			None:







							"""simple docstring"""


							if extra_ids > 0 and additional_special_tokens is None:
								snake_case_						:	str      =		[F"""<extra_id_{i}>""" for i in range(UpperCamelCase__		)]
							elif extra_ids > 0 and additional_special_tokens is not None:
								# Check that we have the right number of extra_id special tokens
								snake_case_						:	Union[str, Any]      =		len(set(filter(lambda _A		: bool('extra_id' in str(UpperCamelCase__		)		)						,      UpperCamelCase__		)		)		)
								if extra_tokens != extra_ids:
									raise ValueError(
									    F"""Both extra_ids ({extra_ids}) and additional_special_tokens ({additional_special_tokens}) are"""
									    ' provided to ByT5Tokenizer. In this case the additional_special_tokens must include the'
									    ' extra_ids tokens'		)

							snake_case_						:	str      =		AddedToken(UpperCamelCase__						,      lstrip=UpperCamelCase__						,      rstrip=UpperCamelCase__		) if isinstance(UpperCamelCase__						,      UpperCamelCase__		) else pad_token
							snake_case_						:	Optional[int]      =		AddedToken(UpperCamelCase__						,      lstrip=UpperCamelCase__						,      rstrip=UpperCamelCase__		) if isinstance(UpperCamelCase__						,      UpperCamelCase__		) else eos_token
							snake_case_						:	Any      =		AddedToken(UpperCamelCase__						,      lstrip=UpperCamelCase__						,      rstrip=UpperCamelCase__		) if isinstance(UpperCamelCase__						,      UpperCamelCase__		) else unk_token

							super().__init__(
							    eos_token=UpperCamelCase__						,      unk_token=UpperCamelCase__						,      pad_token=UpperCamelCase__						,      extra_ids=UpperCamelCase__						,      additional_special_tokens=UpperCamelCase__						,      **UpperCamelCase__						,      )

							snake_case_						:	Any      =		extra_ids

							snake_case_						:	Optional[Any]      =		2**8  # utf is 8 bits

							# define special tokens dict
							snake_case_						:	Dict[int, str]      =		{
							    self.pad_token: 0,
							    self.eos_token: 1,
							    self.unk_token: 2,
							}
							snake_case_						:	int      =		len(self.special_tokens_encoder		)
							snake_case_						:	Tuple      =		len(UpperCamelCase__		)
							for i, token in enumerate(UpperCamelCase__		):
								snake_case_						:	List[str]      =		self.vocab_size + i - n
							snake_case_						:	Dict[str, int]      =		{v: k for k, v in self.special_tokens_encoder.items()}






						@property
						def UpperCAmelCase_ (					self					:					Any		)							->			str:







							"""simple docstring"""


							return self._utf_vocab_size + self._num_special_tokens + self._extra_ids






						def UpperCAmelCase_ (					self					:					List[str]						,      _A					:					Union[str, Any]						,      _A					:					Optional[Any] = None						,      _A					:					List[str] = False		)							->			List[int]:







							"""simple docstring"""


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

							# normal case: some special tokens
							if token_ids_a is None:
								return ([0] * len(UpperCamelCase__		)) + [1]
							return ([0] * len(UpperCamelCase__		)) + [1] + ([0] * len(UpperCamelCase__		)) + [1]






						def UpperCAmelCase_ (					self					:					List[str]						,      _A					:					str		)							->			List[int]:







							"""simple docstring"""


							if len(UpperCamelCase__		) > 0 and token_ids[-1] == self.eos_token_id:
								warnings.warn(
								    F"""This sequence already has {self.eos_token}. In future versions this behavior may lead to duplicated"""
								    ' eos tokens being added.'		)
								return token_ids
							else:
								return token_ids + [self.eos_token_id]






						def UpperCAmelCase_ (					self					:					Optional[Any]						,      _A					:					List[str]						,      _A					:					Any = None		)							->			List[int]:







							"""simple docstring"""


							snake_case_						:	Tuple      =		[self.eos_token_id]

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






						def UpperCAmelCase_ (					self					:					List[str]						,      _A					:					int						,      _A					:					Optional[int] = None		)							->			List[int]:







							"""simple docstring"""


							snake_case_						:	List[str]      =		self._add_eos_if_not_present(UpperCamelCase__		)
							if token_ids_a is None:
								return token_ids_a
							else:
								snake_case_						:	List[str]      =		self._add_eos_if_not_present(UpperCamelCase__		)
								return token_ids_a + token_ids_a






						def UpperCAmelCase_ (					self					:					int						,      _A					:					Union[str, Any]		)							->			List[str]:







							"""simple docstring"""


							snake_case_						:	Any      =		[chr(UpperCamelCase__		) for i in text.encode('utf-8'		)]
							return tokens






						def UpperCAmelCase_ (					self					:					Optional[int]						,      _A					:					Union[str, Any]		)							->			Union[str, Any]:







							"""simple docstring"""


							if token in self.special_tokens_encoder:
								snake_case_						:	Tuple      =		self.special_tokens_encoder[token]
							elif token in self.added_tokens_encoder:
								snake_case_						:	List[str]      =		self.added_tokens_encoder[token]
							elif len(UpperCamelCase__		) != 1:
								snake_case_						:	Dict      =		self.unk_token_id
							else:
								snake_case_						:	int      =		ord(UpperCamelCase__		) + self._num_special_tokens
							return token_id






						def UpperCAmelCase_ (					self					:					str						,      _A					:					Dict		)							->			Any:







							"""simple docstring"""


							if index in self.special_tokens_decoder:
								snake_case_						:	List[str]      =		self.special_tokens_decoder[index]
							else:
								snake_case_						:	Any      =		chr(index - self._num_special_tokens		)
							return token






						def UpperCAmelCase_ (					self					:					Optional[int]						,      _A					:					Tuple		)							->			List[Any]:







							"""simple docstring"""


							snake_case_						:	str      =		B""
							for token in tokens:
								if token in self.special_tokens_decoder:
									snake_case_						:	List[str]      =		self.special_tokens_decoder[token].encode('utf-8'		)
								elif token in self.added_tokens_decoder:
									snake_case_						:	int      =		self.special_tokens_decoder[token].encode('utf-8'		)
								elif token in self.special_tokens_encoder:
									snake_case_						:	Optional[int]      =		token.encode('utf-8'		)
								elif token in self.added_tokens_encoder:
									snake_case_						:	Optional[Any]      =		token.encode('utf-8'		)
								else:
									snake_case_						:	Optional[Any]      =		bytes([ord(UpperCamelCase__		)]		)
								bstring += tok_string
							snake_case_						:	Union[str, Any]      =		bstring.decode('utf-8'						,      errors='ignore'		)
							return string






						def UpperCAmelCase_ (					self					:					List[Any]						,      _A					:					str						,      _A					:					Optional[int] = None		)							->			Tuple[str]:







							"""simple docstring"""


							return ()







import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
from ...utils import logging
from ..auto import CONFIG_MAPPING


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

SCREAMING_SNAKE_CASE__       :      Dict  =   {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}




class 		UpperCamelCase__	(lowerCAmelCase__     ):

    '''simple docstring'''



    lowerCamelCase_      :	Union[str, Any]	     =      """blip_2_vision_model"""







    def __init__(    self     ,  UpperCamelCase__=1408     ,  UpperCamelCase__=6144     ,  UpperCamelCase__=39     ,  UpperCamelCase__=16     ,  UpperCamelCase__=224     ,  UpperCamelCase__=14     ,  UpperCamelCase__="gelu"     ,  UpperCamelCase__=0.00001     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=1e-10     ,  UpperCamelCase__=True     ,  **UpperCamelCase__     ,  )     ->      Optional[Any]:
           super().__init__(**UpperCamelCase__     )

           lowerCamelCase   :      Dict			 =					hidden_size
           lowerCamelCase   :      Union[str, Any]			 =					intermediate_size
           lowerCamelCase   :      List[str]			 =					num_hidden_layers
           lowerCamelCase   :      List[str]			 =					num_attention_heads
           lowerCamelCase   :      Dict			 =					patch_size
           lowerCamelCase   :      Tuple			 =					image_size
           lowerCamelCase   :      Dict			 =					initializer_range
           lowerCamelCase   :      Union[str, Any]			 =					attention_dropout
           lowerCamelCase   :      Dict			 =					layer_norm_eps
           lowerCamelCase   :      Optional[Any]			 =					hidden_act
           lowerCamelCase   :      str			 =					qkv_bias







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      "PretrainedConfig":
           cls._set_token_in_kwargs(UpperCamelCase__     )

           lowerCamelCase ,      lowerCamelCase   :      List[str]			 =					cls.get_config_dict(UpperCamelCase__     ,  **UpperCamelCase__     )

           # get the vision config dict if we are loading from Blip2Config
           if config_dict.get("model_type"     ) == "blip-2":
                  lowerCamelCase   :      Optional[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(UpperCamelCase__     ,  **UpperCamelCase__     )




class 		UpperCamelCase__	(lowerCAmelCase__     ):

    '''simple docstring'''



    lowerCamelCase_      :	Dict	     =      """blip_2_qformer"""







    def __init__(    self     ,  UpperCamelCase__=3_0522     ,  UpperCamelCase__=768     ,  UpperCamelCase__=12     ,  UpperCamelCase__=12     ,  UpperCamelCase__=3072     ,  UpperCamelCase__="gelu"     ,  UpperCamelCase__=0.1     ,  UpperCamelCase__=0.1     ,  UpperCamelCase__=512     ,  UpperCamelCase__=0.02     ,  UpperCamelCase__=1e-12     ,  UpperCamelCase__=0     ,  UpperCamelCase__="absolute"     ,  UpperCamelCase__=2     ,  UpperCamelCase__=1408     ,  **UpperCamelCase__     ,  )     ->      int:
           super().__init__(pad_token_id=UpperCamelCase__     ,  **UpperCamelCase__     )

           lowerCamelCase   :      Optional[int]			 =					vocab_size
           lowerCamelCase   :      int			 =					hidden_size
           lowerCamelCase   :      Dict			 =					num_hidden_layers
           lowerCamelCase   :      Union[str, Any]			 =					num_attention_heads
           lowerCamelCase   :      int			 =					hidden_act
           lowerCamelCase   :      Optional[Any]			 =					intermediate_size
           lowerCamelCase   :      Dict			 =					hidden_dropout_prob
           lowerCamelCase   :      Dict			 =					attention_probs_dropout_prob
           lowerCamelCase   :      Dict			 =					max_position_embeddings
           lowerCamelCase   :      List[str]			 =					initializer_range
           lowerCamelCase   :      List[str]			 =					layer_norm_eps
           lowerCamelCase   :      int			 =					position_embedding_type
           lowerCamelCase   :      Tuple			 =					cross_attention_frequency
           lowerCamelCase   :      Optional[int]			 =					encoder_hidden_size







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      "PretrainedConfig":
           cls._set_token_in_kwargs(UpperCamelCase__     )

           lowerCamelCase ,      lowerCamelCase   :      str			 =					cls.get_config_dict(UpperCamelCase__     ,  **UpperCamelCase__     )

           # get the qformer config dict if we are loading from Blip2Config
           if config_dict.get("model_type"     ) == "blip-2":
                  lowerCamelCase   :      int			 =					config_dict["qformer_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(UpperCamelCase__     ,  **UpperCamelCase__     )




class 		UpperCamelCase__	(lowerCAmelCase__     ):

    '''simple docstring'''



    lowerCamelCase_      :	List[str]	     =      """blip-2"""
    lowerCamelCase_      :	int	     =      True







    def __init__(    self     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  UpperCamelCase__=32     ,  **UpperCamelCase__     )     ->      str:
           super().__init__(**UpperCamelCase__     )

           if vision_config is None:
                  lowerCamelCase   :      List[Any]			 =					{}
                  logger.info("vision_config is None. initializing the Blip2VisionConfig with default values."     )

           if qformer_config is None:
                  lowerCamelCase   :      List[Any]			 =					{}
                  logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values."     )

           if text_config is None:
                  lowerCamelCase   :      Any			 =					{}
                  logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)."     )

           lowerCamelCase   :      Optional[int]			 =					BlipaVisionConfig(**UpperCamelCase__     )
           lowerCamelCase   :      str			 =					BlipaQFormerConfig(**UpperCamelCase__     )
           lowerCamelCase   :      List[str]			 =					text_config["model_type"] if "model_type" in text_config else "opt"
           lowerCamelCase   :      str			 =					CONFIG_MAPPING[text_model_type](**UpperCamelCase__     )

           lowerCamelCase   :      Optional[Any]			 =					self.text_config.tie_word_embeddings
           lowerCamelCase   :      int			 =					self.text_config.is_encoder_decoder

           lowerCamelCase   :      Optional[Any]			 =					num_query_tokens
           lowerCamelCase   :      int			 =					self.vision_config.hidden_size
           lowerCamelCase   :      Tuple			 =					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
           lowerCamelCase   :      Dict			 =					1.0
           lowerCamelCase   :      List[Any]			 =					0.02







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  **UpperCamelCase__     ,  )     ->      str:
           return cls(
               vision_config=vision_config.to_dict()     ,  qformer_config=qformer_config.to_dict()     ,  text_config=text_config.to_dict()     ,  **UpperCamelCase__     ,  )







    def     _lowercase     (    self     )     ->      Optional[Any]:
           lowerCamelCase   :      Tuple			 =					copy.deepcopy(self.__dict__     )
           lowerCamelCase   :      Tuple			 =					self.vision_config.to_dict()
           lowerCamelCase   :      int			 =					self.qformer_config.to_dict()
           lowerCamelCase   :      Optional[Any]			 =					self.text_config.to_dict()
           lowerCamelCase   :      int			 =					self.__class__.model_type
           return output






