





'''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


__UpperCAmelCase			    =    logging.get_logger(__name__)

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




class 			UpperCamelCase__ ( _a       ):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__            =     """blip_2_vision_model"""


					def __init__(		self							:       Any	,		lowerCamelCase_							:       Any=14_08	,		lowerCamelCase_							:       Any=61_44	,		lowerCamelCase_							:       Any=39	,		lowerCamelCase_							:       List[str]=16	,		lowerCamelCase_							:       List[Any]=2_24	,		lowerCamelCase_							:       List[str]=14	,		lowerCamelCase_							:       Any="gelu"	,		lowerCamelCase_							:       List[str]=0.00_001	,		lowerCamelCase_							:       Dict=0.0	,		lowerCamelCase_							:       List[str]=1e-10	,		lowerCamelCase_							:       int=True	,		**lowerCamelCase_							:       str	,		):

							'''simple docstring'''



							super().__init__(**__lowerCamelCase		)

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



					@classmethod
					def lowerCamelCase_       (		cls							:       Tuple	,		lowerCamelCase_							:       Union[str, os.PathLike]	,		**lowerCamelCase_							:       List[str]		):

							'''simple docstring'''



							cls._set_token_in_kwargs(__lowerCamelCase		)

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

							# get the vision config dict if we are loading from Blip2Config
							if config_dict.get("""model_type"""		) == "blip-2":
									SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       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(__lowerCamelCase	,		**__lowerCamelCase		)







class 			UpperCamelCase__ ( _a       ):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__            =     """blip_2_qformer"""


					def __init__(		self							:       Optional[int]	,		lowerCamelCase_							:       Optional[Any]=3_05_22	,		lowerCamelCase_							:       Any=7_68	,		lowerCamelCase_							:       Union[str, Any]=12	,		lowerCamelCase_							:       Optional[Any]=12	,		lowerCamelCase_							:       List[str]=30_72	,		lowerCamelCase_							:       Union[str, Any]="gelu"	,		lowerCamelCase_							:       str=0.1	,		lowerCamelCase_							:       Any=0.1	,		lowerCamelCase_							:       int=5_12	,		lowerCamelCase_							:       Tuple=0.02	,		lowerCamelCase_							:       Union[str, Any]=1e-12	,		lowerCamelCase_							:       Any=0	,		lowerCamelCase_							:       Tuple="absolute"	,		lowerCamelCase_							:       Optional[Any]=2	,		lowerCamelCase_							:       Optional[Any]=14_08	,		**lowerCamelCase_							:       str	,		):

							'''simple docstring'''



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

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



					@classmethod
					def lowerCamelCase_       (		cls							:       Optional[Any]	,		lowerCamelCase_							:       Union[str, os.PathLike]	,		**lowerCamelCase_							:       int		):

							'''simple docstring'''



							cls._set_token_in_kwargs(__lowerCamelCase		)

							SCREAMING_SNAKE_CASE      :							str						=       cls.get_config_dict(__lowerCamelCase	,		**__lowerCamelCase		)

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







class 			UpperCamelCase__ ( _a       ):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__            =     """blip-2"""
					SCREAMING_SNAKE_CASE__            =     True


					def __init__(		self							:       int	,		lowerCamelCase_							:       Dict=None	,		lowerCamelCase_							:       Optional[Any]=None	,		lowerCamelCase_							:       List[str]=None	,		lowerCamelCase_							:       Dict=32	,		**lowerCamelCase_							:       Dict		):

							'''simple docstring'''



							super().__init__(**__lowerCamelCase		)

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

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

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

							SCREAMING_SNAKE_CASE      :							Tuple						=       BlipaVisionConfig(**__lowerCamelCase		)
							SCREAMING_SNAKE_CASE      :							str						=       BlipaQFormerConfig(**__lowerCamelCase		)
							SCREAMING_SNAKE_CASE      :							Any						=       text_config["""model_type"""] if """model_type""" in text_config else """opt"""
							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       CONFIG_MAPPING[text_model_type](**__lowerCamelCase		)

							SCREAMING_SNAKE_CASE      :							List[str]						=       self.text_config.tie_word_embeddings
							SCREAMING_SNAKE_CASE      :							Union[str, Any]						=       self.text_config.is_encoder_decoder

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


					@classmethod
					def lowerCamelCase_       (		cls							:       Tuple	,		lowerCamelCase_							:       BlipaVisionConfig	,		lowerCamelCase_							:       BlipaQFormerConfig	,		lowerCamelCase_							:       PretrainedConfig	,		**lowerCamelCase_							:       List[Any]	,		):

							'''simple docstring'''



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



					def lowerCamelCase_       (		self							:       Optional[int]		):

							'''simple docstring'''



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











import unittest
from pathlib import Path
from tempfile import TemporaryDirectory

from transformers import AutoConfig, TFAutoModel, is_tensorflow_text_available, is_tf_available
from transformers.models.bert.tokenization_bert import BertTokenizer
from transformers.testing_utils import require_tensorflow_text, require_tf, slow


if is_tf_available():
	import tensorflow as tf

if is_tensorflow_text_available():
	from transformers.models.bert import TFBertTokenizer


UpperCAmelCase_ :						Optional[Any]      =      ['''bert-base-uncased''', '''bert-base-cased''']
UpperCAmelCase_ :						List[str]      =      '''hf-internal-testing/tiny-bert-tf-only'''

if is_tf_available():

	class     _SCREAMING_SNAKE_CASE       (			tf.keras.Model      ):





		def __init__(		self				:					List[str] ,  __lowerCamelCase				:					Union[str, Any]	):
			super().__init__()
			UpperCamelCase     :Any          =	tokenizer
			UpperCamelCase     :List[str]          =	AutoConfig.from_pretrained(__lowerCamelCase	)
			UpperCamelCase     :List[str]          =	TFAutoModel.from_config(__lowerCamelCase	)





		def 						_A (		self				:					Tuple ,  __lowerCamelCase				:					str	):
			UpperCamelCase     :str          =	self.tokenizer(__lowerCamelCase	)
			UpperCamelCase     :Any          =	self.bert(**__lowerCamelCase	)
			return out["pooler_output"]

@require_tf
@require_tensorflow_text
class     _SCREAMING_SNAKE_CASE       (			unittest.TestCase      ):





	def 						_A (		self				:					Dict	):
		super().setUp()

		UpperCamelCase     :int          =	[
		    BertTokenizer.from_pretrained(__lowerCamelCase	) for checkpoint in (TOKENIZER_CHECKPOINTS * 2)
		]  # repeat for when fast_bert_tokenizer=false
		UpperCamelCase     :Any          =	[TFBertTokenizer.from_pretrained(__lowerCamelCase	) for checkpoint in TOKENIZER_CHECKPOINTS] + [
		    TFBertTokenizer.from_pretrained(__lowerCamelCase ,  use_fast_bert_tokenizer=__lowerCamelCase	)
		    for checkpoint in TOKENIZER_CHECKPOINTS
		]
		assert len(self.tokenizers	) == len(self.tf_tokenizers	)

		UpperCamelCase     :Any          =	[
		    """This is a straightforward English test sentence.""",
		    """This one has some weird characters\rto\nsee\r\nif  those\u00E9break things.""",
		    """Now we're going to add some Chinese: 一 二 三 一二三""",
		    """And some much more rare Chinese: 齉 堃 齉堃""",
		    """Je vais aussi écrire en français pour tester les accents""",
		    """Classical Irish also has some unusual characters, so in they go: Gaelaċ, ꝼ""",
		]
		UpperCamelCase     :Union[str, Any]          =	list(zip(self.test_sentences ,  self.test_sentences[::-1]	)	)





	def 						_A (		self				:					Optional[int]	):
		for tokenizer, tf_tokenizer in zip(self.tokenizers ,  self.tf_tokenizers	):
			for test_inputs in (self.test_sentences, self.paired_sentences):
				UpperCamelCase     :Any          =	tokenizer(__lowerCamelCase ,  return_tensors="""tf""" ,  padding="""longest"""	)
				UpperCamelCase     :str          =	tf_tokenizer(__lowerCamelCase	)

				for key in python_outputs.keys():
					self.assertTrue(tf.reduce_all(python_outputs[key].shape == tf_outputs[key].shape	)	)
					self.assertTrue(tf.reduce_all(tf.cast(python_outputs[key] ,  tf.intaa	) == tf_outputs[key]	)	)





	@slow
	def 						_A (		self				:					Dict	):
		for tf_tokenizer in self.tf_tokenizers:
			UpperCamelCase     :str          =	tf_tokenizer(self.paired_sentences	)
			UpperCamelCase     :Any          =	tf_tokenizer(
			    text=[sentence[0] for sentence in self.paired_sentences] ,  text_pair=[sentence[1] for sentence in self.paired_sentences] ,  )
			for key in merged_outputs.keys():
				self.assertTrue(tf.reduce_all(tf.cast(merged_outputs[key] ,  tf.intaa	) == separated_outputs[key]	)	)





	@slow
	def 						_A (		self				:					List[str]	):
		for tf_tokenizer in self.tf_tokenizers:
			UpperCamelCase     :List[Any]          =	tf.function(__lowerCamelCase	)
			for test_inputs in (self.test_sentences, self.paired_sentences):
				UpperCamelCase     :Any          =	tf.constant(__lowerCamelCase	)
				UpperCamelCase     :List[str]          =	compiled_tokenizer(__lowerCamelCase	)
				UpperCamelCase     :Optional[Any]          =	tf_tokenizer(__lowerCamelCase	)

				for key in eager_outputs.keys():
					self.assertTrue(tf.reduce_all(eager_outputs[key] == compiled_outputs[key]	)	)





	@slow
	def 						_A (		self				:					Tuple	):
		for tf_tokenizer in self.tf_tokenizers:
			UpperCamelCase     :List[str]          =	ModelToSave(tokenizer=__lowerCamelCase	)
			UpperCamelCase     :Union[str, Any]          =	tf.convert_to_tensor(self.test_sentences	)
			UpperCamelCase     :Union[str, Any]          =	model(__lowerCamelCase	)  # Build model with some sample inputs
			with TemporaryDirectory() as tempdir:
				UpperCamelCase     :List[str]          =	Path(__lowerCamelCase	) / """saved.model"""
				model.save(__lowerCamelCase	)
				UpperCamelCase     :List[Any]          =	tf.keras.models.load_model(__lowerCamelCase	)
			UpperCamelCase     :Dict          =	loaded_model(__lowerCamelCase	)
			# We may see small differences because the loaded model is compiled, so we need an epsilon for the test
			self.assertLessEqual(tf.reduce_max(tf.abs(out - loaded_output	)	) ,  1E-5	)
