






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 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """blip_2_vision_model"""



						def __init__(self:					Union[str, Any] ,					__UpperCAmelCase:					List[Any]=1408 ,					__UpperCAmelCase:					List[Any]=6144 ,					__UpperCAmelCase:					Any=39 ,					__UpperCAmelCase:					Any=16 ,					__UpperCAmelCase:					List[str]=224 ,					__UpperCAmelCase:					Union[str, Any]=14 ,					__UpperCAmelCase:					Tuple="gelu" ,					__UpperCAmelCase:					Dict=0.0_00_01 ,					__UpperCAmelCase:					Tuple=0.0 ,					__UpperCAmelCase:					List[str]=1E-10 ,					__UpperCAmelCase:					Optional[int]=True ,					**__UpperCAmelCase:					Tuple ,					)    ->     Dict:




											'''simple docstring'''



											super().__init__(**__UpperCAmelCase   )

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



						@classmethod
						def        UpperCAmelCase__					(cls:					Dict ,					__UpperCAmelCase:					Union[str, os.PathLike] ,					**__UpperCAmelCase:					int   )    ->     "PretrainedConfig":




											'''simple docstring'''



											cls._set_token_in_kwargs(__UpperCAmelCase   )

											__a      ,			__a					:						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":
																__a					:						Optional[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(__UpperCAmelCase ,					**__UpperCAmelCase   )




class 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """blip_2_qformer"""



						def __init__(self:					Any ,					__UpperCAmelCase:					Optional[Any]=30522 ,					__UpperCAmelCase:					Tuple=768 ,					__UpperCAmelCase:					List[str]=12 ,					__UpperCAmelCase:					int=12 ,					__UpperCAmelCase:					str=3072 ,					__UpperCAmelCase:					Optional[Any]="gelu" ,					__UpperCAmelCase:					str=0.1 ,					__UpperCAmelCase:					Tuple=0.1 ,					__UpperCAmelCase:					Any=512 ,					__UpperCAmelCase:					Tuple=0.02 ,					__UpperCAmelCase:					Tuple=1E-12 ,					__UpperCAmelCase:					List[Any]=0 ,					__UpperCAmelCase:					Optional[Any]="absolute" ,					__UpperCAmelCase:					List[str]=2 ,					__UpperCAmelCase:					Optional[Any]=1408 ,					**__UpperCAmelCase:					Any ,					)    ->     Dict:




											'''simple docstring'''



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

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



						@classmethod
						def        UpperCAmelCase__					(cls:					Optional[Any] ,					__UpperCAmelCase:					Union[str, os.PathLike] ,					**__UpperCAmelCase:					Union[str, Any]   )    ->     "PretrainedConfig":




											'''simple docstring'''



											cls._set_token_in_kwargs(__UpperCAmelCase   )

											__a      ,			__a					:						Dict      =					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":
																__a					:						Optional[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 							snake_case_       (    __UpperCamelCase      ):

						"""simple docstring"""



						snake_case__        =      """blip-2"""
						snake_case__        =      True



						def __init__(self:					Union[str, Any] ,					__UpperCAmelCase:					Union[str, Any]=None ,					__UpperCAmelCase:					List[str]=None ,					__UpperCAmelCase:					List[str]=None ,					__UpperCAmelCase:					Dict=32 ,					**__UpperCAmelCase:					str   )    ->     Union[str, Any]:




											'''simple docstring'''



											super().__init__(**__UpperCAmelCase   )

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

											if qformer_config is None:
																__a					:						Optional[int]      =					{}
																logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values."   )

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

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

											__a					:						List[Any]      =					self.text_config.tie_word_embeddings
											__a					:						Tuple      =					self.text_config.is_encoder_decoder

											__a					:						int      =					num_query_tokens
											__a					:						str      =					self.vision_config.hidden_size
											__a					:						Optional[int]      =					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
											__a					:						List[Any]      =					1.0
											__a					:						Optional[int]      =					0.02



						@classmethod
						def        UpperCAmelCase__					(cls:					List[str] ,					__UpperCAmelCase:					BlipaVisionConfig ,					__UpperCAmelCase:					BlipaQFormerConfig ,					__UpperCAmelCase:					PretrainedConfig ,					**__UpperCAmelCase:					List[str] ,					)    ->     Optional[int]:




											'''simple docstring'''



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



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




											'''simple docstring'''



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













from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
			import tensorflow as tf

			from transformers import TFBlipTextModel
			from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class 							snake_case_       :

						"""simple docstring"""



						def __init__(self:					Optional[Any] ,					__UpperCAmelCase:					List[str] ,					__UpperCAmelCase:					int=12 ,					__UpperCAmelCase:					Dict=7 ,					__UpperCAmelCase:					str=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Tuple=True ,					__UpperCAmelCase:					Optional[int]=99 ,					__UpperCAmelCase:					str=32 ,					__UpperCAmelCase:					Any=32 ,					__UpperCAmelCase:					int=2 ,					__UpperCAmelCase:					List[str]=4 ,					__UpperCAmelCase:					Tuple=37 ,					__UpperCAmelCase:					Optional[int]=0.1 ,					__UpperCAmelCase:					List[Any]=0.1 ,					__UpperCAmelCase:					Dict=512 ,					__UpperCAmelCase:					Union[str, Any]=0.02 ,					__UpperCAmelCase:					List[Any]=0 ,					__UpperCAmelCase:					str=None ,					)    ->     Tuple:




											'''simple docstring'''



											__a					:						Union[str, Any]      =					parent
											__a					:						Dict      =					batch_size
											__a					:						Optional[Any]      =					seq_length
											__a					:						int      =					is_training
											__a					:						Union[str, Any]      =					use_input_mask
											__a					:						str      =					use_labels
											__a					:						Tuple      =					vocab_size
											__a					:						Dict      =					hidden_size
											__a					:						Optional[int]      =					projection_dim
											__a					:						Tuple      =					num_hidden_layers
											__a					:						Dict      =					num_attention_heads
											__a					:						Any      =					intermediate_size
											__a					:						Optional[int]      =					dropout
											__a					:						Dict      =					attention_dropout
											__a					:						List[Any]      =					max_position_embeddings
											__a					:						Any      =					initializer_range
											__a					:						Tuple      =					scope
											__a					:						Dict      =					bos_token_id



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




											'''simple docstring'''



											__a					:						Optional[Any]      =					ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size   )

											__a					:						Any      =					None
											if self.use_input_mask:
																__a					:						Tuple      =					random_attention_mask([self.batch_size, self.seq_length]   )

											if input_mask is not None:
																__a					:						Tuple      =					input_mask.numpy()
																__a      ,			__a					:						int      =					input_mask.shape
																__a					:						Dict      =					np.random.randint(1 ,					seq_length - 1 ,					size=(batch_size,)   )
																for batch_idx, start_index in enumerate(__UpperCAmelCase   ):
																					__a					:						Any      =					1
																					__a					:						int      =					0

											__a					:						Union[str, Any]      =					self.get_config()

											return config, input_ids, tf.convert_to_tensor(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[int]:




											'''simple docstring'''



											return BlipTextConfig(
											    vocab_size=self.vocab_size ,					hidden_size=self.hidden_size ,					projection_dim=self.projection_dim ,					num_hidden_layers=self.num_hidden_layers ,					num_attention_heads=self.num_attention_heads ,					intermediate_size=self.intermediate_size ,					dropout=self.dropout ,					attention_dropout=self.attention_dropout ,					max_position_embeddings=self.max_position_embeddings ,					initializer_range=self.initializer_range ,					bos_token_id=self.bos_token_id ,					)



						def        UpperCAmelCase__					(self:					List[str] ,					__UpperCAmelCase:					Any ,					__UpperCAmelCase:					Dict ,					__UpperCAmelCase:					str   )    ->     List[str]:




											'''simple docstring'''



											__a					:						int      =					TFBlipTextModel(config=__UpperCAmelCase   )
											__a					:						List[str]      =					model(__UpperCAmelCase ,					attention_mask=__UpperCAmelCase ,					training=__UpperCAmelCase   )
											__a					:						Any      =					model(__UpperCAmelCase ,					training=__UpperCAmelCase   )
											self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)   )
											self.parent.assertEqual(result.pooler_output.shape ,					(self.batch_size, self.hidden_size)   )



						def        UpperCAmelCase__					(self:					List[str]   )    ->     str:




											'''simple docstring'''



											__a					:						Tuple      =					self.prepare_config_and_inputs()
											__a      ,			__a      ,			__a					:						Dict      =					config_and_inputs
											__a					:						Union[str, Any]      =					{"input_ids": input_ids, "attention_mask": input_mask}
											return config, inputs_dict




@require_tf
class 							snake_case_       (    __UpperCamelCase		,							unittest.TestCase      ):

						"""simple docstring"""



						snake_case__        =      (TFBlipTextModel,) if is_tf_available() else ()
						snake_case__        =      False
						snake_case__        =      False
						snake_case__        =      False



						def        UpperCAmelCase__					(self:					List[str]   )    ->     Optional[int]:




											'''simple docstring'''



											__a					:						str      =					BlipTextModelTester(self   )
											__a					:						str      =					ConfigTester(self ,					config_class=__UpperCAmelCase ,					hidden_size=37   )



						def        UpperCAmelCase__					(self:					str   )    ->     str:




											'''simple docstring'''



											self.config_tester.run_common_tests()



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




											'''simple docstring'''



											__a					:						int      =					self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Tuple   )    ->     Dict:




											'''simple docstring'''



											pass



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




											'''simple docstring'''



											pass



						@unittest.skip(reason="Blip does not use inputs_embeds"   )
						def        UpperCAmelCase__					(self:					Any   )    ->     Dict:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					List[Any]   )    ->     List[str]:




											'''simple docstring'''



											pass



						@unittest.skip(reason="BlipTextModel has no base class and is not available in MODEL_MAPPING"   )
						def        UpperCAmelCase__					(self:					Union[str, Any]   )    ->     int:




											'''simple docstring'''



											pass



						@slow
						def        UpperCAmelCase__					(self:					Optional[Any]   )    ->     Tuple:




											'''simple docstring'''



											for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__a					:						List[str]      =					TFBlipTextModel.from_pretrained(__UpperCAmelCase   )
																self.assertIsNotNone(__UpperCAmelCase   )



						def        UpperCAmelCase__					(self:					Dict ,					__UpperCAmelCase:					Optional[Any]=True   )    ->     Any:




											'''simple docstring'''



											super().test_pt_tf_model_equivalence(allow_missing_keys=__UpperCAmelCase   )





