

"""simple docstring"""


import json
import os
import unittest

from transformers import DebertaTokenizer, DebertaTokenizerFast
from transformers.models.deberta.tokenization_deberta import VOCAB_FILES_NAMES
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin





class    snake_case     (    __snake_case,							unittest.TestCase  ):
				SCREAMING_SNAKE_CASE_       :				str       				=  DebertaTokenizer
				SCREAMING_SNAKE_CASE_       :				int       				=  True
				SCREAMING_SNAKE_CASE_       :				int       				=  DebertaTokenizerFast

				def 							lowercase_					(     self							:    Union[str, Any])->       Any:



						'''simple docstring'''


						super().setUp()

						# Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
						__lowerCAmelCase:	Tuple   				=      [
						    "l",
						    "o",
						    "w",
						    "e",
						    "r",
						    "s",
						    "t",
						    "i",
						    "d",
						    "n",
						    "\u0120",
						    "\u0120l",
						    "\u0120n",
						    "\u0120lo",
						    "\u0120low",
						    "er",
						    "\u0120lowest",
						    "\u0120newer",
						    "\u0120wider",
						    "[UNK]",
						]
						__lowerCAmelCase:	Optional[int]   				=      dict(zip(UpperCamelCase__					,  range(len(UpperCamelCase__))))
						__lowerCAmelCase:	List[str]   				=      ["#version: 0.2", "\u0120 l", "\u0120l o", "\u0120lo w", "e r", ""]
						__lowerCAmelCase:	Optional[int]   				=      {"unk_token": "[UNK]"}

						__lowerCAmelCase:	Optional[Any]   				=      os.path.join(self.tmpdirname					,  VOCAB_FILES_NAMES["vocab_file"])
						__lowerCAmelCase:	int   				=      os.path.join(self.tmpdirname					,  VOCAB_FILES_NAMES["merges_file"])
						with open(self.vocab_file					,  "w"					,  encoding="utf-8") as fp:
								fp.write(json.dumps(UpperCamelCase__) + "\n")
						with open(self.merges_file					,  "w"					,  encoding="utf-8") as fp:
								fp.write("\n".join(UpperCamelCase__))

				def 							lowercase_					(     self							:    Tuple					,  **UpperCamelCase__							:    List[Any])->       Tuple:



						'''simple docstring'''


						kwargs.update(self.special_tokens_map)
						return self.tokenizer_class.from_pretrained(self.tmpdirname					,  **UpperCamelCase__)

				def 							lowercase_					(     self							:    Any					,  UpperCamelCase__							:    Any)->       Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	List[str]   				=      "lower newer"
						__lowerCAmelCase:	Union[str, Any]   				=      "lower newer"
						return input_text, output_text

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



						'''simple docstring'''


						__lowerCAmelCase:	Optional[Any]   				=      self.get_tokenizer()
						__lowerCAmelCase:	Optional[Any]   				=      "lower newer"
						__lowerCAmelCase:	List[Any]   				=      ["l", "o", "w", "er", "\u0120", "n", "e", "w", "er"]
						__lowerCAmelCase:	str   				=      tokenizer.tokenize(UpperCamelCase__)
						self.assertListEqual(UpperCamelCase__					,  UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      tokens + [tokenizer.unk_token]
						__lowerCAmelCase:	Optional[Any]   				=      [0, 1, 2, 1_5, 1_0, 9, 3, 2, 1_5, 1_9]
						self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase__)					,  UpperCamelCase__)

				def 							lowercase_					(     self							:    int)->       Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[int]   				=      self.get_tokenizer()
						__lowerCAmelCase:	Optional[Any]   				=      tokenizer("Hello"					,  "World")
						__lowerCAmelCase:	List[Any]   				=      [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
						self.assertListEqual(tokd["token_type_ids"]					,  UpperCamelCase__)

				@slow
				def 							lowercase_					(     self							:    int)->       Any:



						'''simple docstring'''


						__lowerCAmelCase:	Optional[Any]   				=      self.tokenizer_class.from_pretrained("microsoft/deberta-base")

						__lowerCAmelCase:	Optional[Any]   				=      tokenizer.encode("sequence builders"					,  add_special_tokens=UpperCamelCase__)
						__lowerCAmelCase:	str   				=      tokenizer.encode("multi-sequence build"					,  add_special_tokens=UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      tokenizer.encode(
						    "sequence builders"					,  add_special_tokens=UpperCamelCase__					,  add_prefix_space=UpperCamelCase__)
						__lowerCAmelCase:	Union[str, Any]   				=      tokenizer.encode(
						    "sequence builders"					,  "multi-sequence build"					,  add_special_tokens=UpperCamelCase__					,  add_prefix_space=UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      tokenizer.build_inputs_with_special_tokens(UpperCamelCase__)
						__lowerCAmelCase:	Optional[int]   				=      tokenizer.build_inputs_with_special_tokens(UpperCamelCase__					,  UpperCamelCase__)

						assert encoded_sentence == encoded_text_from_decode
						assert encoded_pair == encoded_pair_from_decode




				@slow
				def 							lowercase_					(     self							:    Union[str, Any])->       Union[str, Any]:



						'''simple docstring'''


						__lowerCAmelCase:	int   				=      [self.tokenizer_class]
						if self.test_rust_tokenizer:
								tokenizer_classes.append(self.rust_tokenizer_class)

						for tokenizer_class in tokenizer_classes:
								__lowerCAmelCase:	List[Any]   				=      tokenizer_class.from_pretrained("microsoft/deberta-base")

								__lowerCAmelCase:	str   				=      [
								    "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations",
								    "ALBERT incorporates two parameter reduction techniques",
								    "The first one is a factorized embedding parameterization. By decomposing the large vocabulary"
								    " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"
								    " vocabulary embedding.",
								]

								__lowerCAmelCase:	Tuple   				=      tokenizer(UpperCamelCase__					,  padding=UpperCamelCase__)
								__lowerCAmelCase:	Optional[Any]   				=      [tokenizer.decode(UpperCamelCase__					,  skip_special_tokens=UpperCamelCase__) for seq in encoding["input_ids"]]

								# fmt: off
								__lowerCAmelCase:	List[Any]   				=      {
								    "input_ids": [
								        [1, 2_1_1_8, 1_1_1_2_6, 5_6_5, 3_5, 8_3, 2_5_1_9_1, 1_6_3, 1_8_8_5_4, 1_3, 1_2_1_5_6, 1_2, 1_6_1_0_1, 2_5_3_7_6, 1_3_8_0_7, 9, 2_2_2_0_5, 2_7_8_9_3, 1_6_3_5, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 2_1_1_8, 1_1_1_2_6, 5_6_5, 2_4_5_3_6, 8_0, 4_3_7_9_7, 4_8_7_8, 7_3_7_3, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 1_3_3, 7_8, 6_5, 1_6, 1_0, 3_7_2_4, 1_5_3_8, 3_3_1_8_3, 1_1_3_0_3, 4_3_7_9_7, 1_9_3_8, 4, 8_7_0, 2_4_1_6_5, 2_9_1_0_5, 5, 7_3_9, 3_2_6_4_4, 3_3_1_8_3, 1_1_3_0_3, 3_6_1_7_3, 8_8, 8_0, 6_5_0, 7_8_2_1, 4_5_9_4_0, 6, 5_2, 2_5_5_9, 5, 1_8_3_6, 9, 5, 7_3_9_7, 1_3_1_7_1, 3_1, 5, 1_8_3_6, 9, 3_2_6_4_4, 3_3_1_8_3, 1_1_3_0_3, 4, 2]
								    ],
								    "token_type_ids": [
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
								    ],
								    "attention_mask": [
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
								        [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
								    ]
								}
								# fmt: on

								__lowerCAmelCase:	Any   				=      [
								    "ALBERT: A Lite BERT for Self-supervised Learning of Language Representations",
								    "ALBERT incorporates two parameter reduction techniques",
								    "The first one is a factorized embedding parameterization. By decomposing the large vocabulary"
								    " embedding matrix into two small matrices, we separate the size of the hidden layers from the size of"
								    " vocabulary embedding.",
								]

								self.assertDictEqual(encoding.data					,  UpperCamelCase__)

								for expected, decoded in zip(UpperCamelCase__					,  UpperCamelCase__):
										self.assertEqual(UpperCamelCase__					,  UpperCamelCase__)





"""simple docstring"""


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


__A						       =      logging.get_logger(__name__)

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





class    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				Union[str, Any]       				=  """blip_2_vision_model"""

				def __init__(     self							:    Optional[int]					,  UpperCamelCase__							:    Optional[int]=1_4_0_8					,  UpperCamelCase__							:    Tuple=6_1_4_4					,  UpperCamelCase__							:    Tuple=3_9					,  UpperCamelCase__							:    str=1_6					,  UpperCamelCase__							:    Optional[Any]=2_2_4					,  UpperCamelCase__							:    List[str]=1_4					,  UpperCamelCase__							:    Optional[int]="gelu"					,  UpperCamelCase__							:    Optional[int]=0.00001					,  UpperCamelCase__							:    Tuple=0.0					,  UpperCamelCase__							:    Dict=1e-10					,  UpperCamelCase__							:    List[str]=True					,  **UpperCamelCase__							:    List[str]					,  )->       int:



						'''simple docstring'''


						super().__init__(**UpperCamelCase__)

						__lowerCAmelCase:	List[str]   				=      hidden_size
						__lowerCAmelCase:	Tuple   				=      intermediate_size
						__lowerCAmelCase:	str   				=      num_hidden_layers
						__lowerCAmelCase:	Any   				=      num_attention_heads
						__lowerCAmelCase:	Any   				=      patch_size
						__lowerCAmelCase:	Tuple   				=      image_size
						__lowerCAmelCase:	str   				=      initializer_range
						__lowerCAmelCase:	List[str]   				=      attention_dropout
						__lowerCAmelCase:	List[Any]   				=      layer_norm_eps
						__lowerCAmelCase:	Any   				=      hidden_act
						__lowerCAmelCase:	int   				=      qkv_bias




				@classmethod
				def 							lowercase_					(     cls							:    List[str]					,  UpperCamelCase__							:    Union[str, os.PathLike]					,  **UpperCamelCase__							:    List[str])->       "PretrainedConfig":



						'''simple docstring'''


						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:	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(UpperCamelCase__					,  **UpperCamelCase__)





class    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				List[str]       				=  """blip_2_qformer"""

				def __init__(     self							:    Optional[int]					,  UpperCamelCase__							:    Dict=3_0_5_2_2					,  UpperCamelCase__							:    Union[str, Any]=7_6_8					,  UpperCamelCase__							:    Union[str, Any]=1_2					,  UpperCamelCase__							:    Union[str, Any]=1_2					,  UpperCamelCase__							:    Optional[int]=3_0_7_2					,  UpperCamelCase__							:    Optional[Any]="gelu"					,  UpperCamelCase__							:    Tuple=0.1					,  UpperCamelCase__							:    Optional[Any]=0.1					,  UpperCamelCase__							:    List[str]=5_1_2					,  UpperCamelCase__							:    List[Any]=0.02					,  UpperCamelCase__							:    Dict=1e-12					,  UpperCamelCase__							:    List[str]=0					,  UpperCamelCase__							:    Optional[int]="absolute"					,  UpperCamelCase__							:    List[Any]=2					,  UpperCamelCase__							:    Optional[Any]=1_4_0_8					,  **UpperCamelCase__							:    List[str]					,  )->       str:



						'''simple docstring'''


						super().__init__(pad_token_id=UpperCamelCase__					,  **UpperCamelCase__)

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




				@classmethod
				def 							lowercase_					(     cls							:    int					,  UpperCamelCase__							:    Union[str, os.PathLike]					,  **UpperCamelCase__							:    Tuple)->       "PretrainedConfig":



						'''simple docstring'''


						cls._set_token_in_kwargs(UpperCamelCase__)

						__lowerCAmelCase				,			__lowerCAmelCase:	Optional[int]   				=      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:	Any   				=      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    snake_case     (    __snake_case  ):
				SCREAMING_SNAKE_CASE_       :				Dict       				=  """blip-2"""
				SCREAMING_SNAKE_CASE_       :				str       				=  True

				def __init__(     self							:    List[Any]					,  UpperCamelCase__							:    Tuple=None					,  UpperCamelCase__							:    Dict=None					,  UpperCamelCase__							:    Optional[int]=None					,  UpperCamelCase__							:    Union[str, Any]=3_2					,  **UpperCamelCase__							:    Dict)->       List[Any]:



						'''simple docstring'''


						super().__init__(**UpperCamelCase__)

						if vision_config is None:
								__lowerCAmelCase:	Dict   				=      {}
								logger.info("vision_config is None. initializing the Blip2VisionConfig with default values.")

						if qformer_config is None:
								__lowerCAmelCase:	Union[str, Any]   				=      {}
								logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values.")

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

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

						__lowerCAmelCase:	List[Any]   				=      self.text_config.tie_word_embeddings
						__lowerCAmelCase:	List[Any]   				=      self.text_config.is_encoder_decoder

						__lowerCAmelCase:	int   				=      num_query_tokens
						__lowerCAmelCase:	Union[str, Any]   				=      self.vision_config.hidden_size
						__lowerCAmelCase:	Any   				=      self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						__lowerCAmelCase:	Dict   				=      1.0
						__lowerCAmelCase:	Dict   				=      0.02

				@classmethod
				def 							lowercase_					(     cls							:    str					,  UpperCamelCase__							:    BlipaVisionConfig					,  UpperCamelCase__							:    BlipaQFormerConfig					,  UpperCamelCase__							:    PretrainedConfig					,  **UpperCamelCase__							:    Union[str, Any]					,  )->       Tuple:



						'''simple docstring'''


						return cls(
						    vision_config=vision_config.to_dict()					,  qformer_config=qformer_config.to_dict()					,  text_config=text_config.to_dict()					,  **UpperCamelCase__					,  )




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



						'''simple docstring'''


						__lowerCAmelCase:	str   				=      copy.deepcopy(self.__dict__)
						__lowerCAmelCase:	Optional[int]   				=      self.vision_config.to_dict()
						__lowerCAmelCase:	List[Any]   				=      self.qformer_config.to_dict()
						__lowerCAmelCase:	str   				=      self.text_config.to_dict()
						__lowerCAmelCase:	Tuple   				=      self.__class__.model_type
						return output


