




"""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 _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Tuple     				=     "blip_2_vision_model"






   def __init__(		self       :		Union[str, Any]   ,     UpperCamelCase       :		Any=14_08   ,     UpperCamelCase       :		List[str]=61_44   ,     UpperCamelCase       :		Dict=39   ,     UpperCamelCase       :		str=16   ,     UpperCamelCase       :		int=2_24   ,     UpperCamelCase       :		List[Any]=14   ,     UpperCamelCase       :		Optional[int]="gelu"   ,     UpperCamelCase       :		Any=0.0_0001   ,     UpperCamelCase       :		Union[str, Any]=0.0   ,     UpperCamelCase       :		Any=1E-1_0   ,     UpperCamelCase       :		Dict=True   ,     **UpperCamelCase       :		Tuple   ,     )			->						str:

         """simple docstring"""
         super().__init__(**UpperCamelCase   )

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






   @classmethod
   def 	_lowerCAmelCase   (		cls       :		Optional[int]   ,     UpperCamelCase       :		Union[str, os.PathLike]   ,     **UpperCamelCase       :		Optional[int]   )			->						"PretrainedConfig":

         """simple docstring"""
         cls._set_token_in_kwargs(UpperCamelCase   )

         lowerCAmelCase__ ,   lowerCAmelCase__      :			Optional[int]   =	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__      :			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 _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Optional[int]     				=     "blip_2_qformer"






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

         """simple docstring"""
         super().__init__(pad_token_id=UpperCamelCase   ,     **UpperCamelCase   )

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






   @classmethod
   def 	_lowerCAmelCase   (		cls       :		Dict   ,     UpperCamelCase       :		Union[str, os.PathLike]   ,     **UpperCamelCase       :		Any   )			->						"PretrainedConfig":

         """simple docstring"""
         cls._set_token_in_kwargs(UpperCamelCase   )

         lowerCAmelCase__ ,   lowerCAmelCase__      :			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__      :			Tuple   =	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 _lowerCamelCase    (					a_						):
   _lowerCamelCase    :Optional[Any]     				=     "blip-2"
   _lowerCamelCase    :str     				=     True






   def __init__(		self       :		Dict   ,     UpperCamelCase       :		List[Any]=None   ,     UpperCamelCase       :		Optional[Any]=None   ,     UpperCamelCase       :		List[Any]=None   ,     UpperCamelCase       :		Tuple=32   ,     **UpperCamelCase       :		List[str]   )			->						Dict:

         """simple docstring"""
         super().__init__(**UpperCamelCase   )

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

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

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

         lowerCAmelCase__      :			Dict   =	BlipaVisionConfig(**UpperCamelCase   )
         lowerCAmelCase__      :			List[str]   =	BlipaQFormerConfig(**UpperCamelCase   )
         lowerCAmelCase__      :			Union[str, Any]   =	text_config["""model_type"""] if """model_type""" in text_config else """opt"""
         lowerCAmelCase__      :			Optional[int]   =	CONFIG_MAPPING[text_model_type](**UpperCamelCase   )

         lowerCAmelCase__      :			Union[str, Any]   =	self.text_config.tie_word_embeddings
         lowerCAmelCase__      :			int   =	self.text_config.is_encoder_decoder

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






   @classmethod
   def 	_lowerCAmelCase   (		cls       :		Tuple   ,     UpperCamelCase       :		BlipaVisionConfig   ,     UpperCamelCase       :		BlipaQFormerConfig   ,     UpperCamelCase       :		PretrainedConfig   ,     **UpperCamelCase       :		List[str]   ,     )			->						str:

         """simple docstring"""
         return cls(
             vision_config=vision_config.to_dict()   ,     qformer_config=qformer_config.to_dict()   ,     text_config=text_config.to_dict()   ,     **UpperCamelCase   ,     )






   def 	_lowerCAmelCase   (		self       :		int   )			->						List[Any]:

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











"""simple docstring"""


import unittest

from .lib import (
    Matrix,
    Vector,
    axpy,
    square_zero_matrix,
    unit_basis_vector,
    zero_vector,
)


class _lowerCamelCase    (					unittest.TestCase						):






   def 	_lowerCAmelCase   (		self       :		Dict   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         self.assertEqual(x.component(0   )   ,     1   )
         self.assertEqual(x.component(2   )   ,     3   )
         lowerCAmelCase__      :			Union[str, Any]   =	Vector()






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

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Vector([0, 0, 0, 0, 0, 1]   )
         self.assertEqual(str(UpperCamelCase   )   ,     """(0,0,0,0,0,1)"""   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			List[Any]   =	Vector([1, 2, 3, 4]   )
         self.assertEqual(len(UpperCamelCase   )   ,     4   )






   def 	_lowerCAmelCase   (		self       :		List[str]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Vector([1, 2]   )
         lowerCAmelCase__      :			Optional[int]   =	Vector([1, 2, 3, 4, 5]   )
         lowerCAmelCase__      :			Union[str, Any]   =	Vector([0, 0, 0, 0, 0, 0, 0, 0, 0, 0]   )
         lowerCAmelCase__      :			List[Any]   =	Vector([1, -1, 1, -1, 2, -3, 4, -5]   )
         self.assertAlmostEqual(x.euclidean_length()   ,     2.236   ,     3   )
         self.assertAlmostEqual(y.euclidean_length()   ,     7.416   ,     3   )
         self.assertEqual(z.euclidean_length()   ,     0   )
         self.assertAlmostEqual(w.euclidean_length()   ,     7.616   ,     3   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			int   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Optional[Any]   =	Vector([1, 1, 1]   )
         self.assertEqual((x + y).component(0   )   ,     2   )
         self.assertEqual((x + y).component(1   )   ,     3   )
         self.assertEqual((x + y).component(2   )   ,     4   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Dict   =	Vector([1, 1, 1]   )
         self.assertEqual((x - y).component(0   )   ,     0   )
         self.assertEqual((x - y).component(1   )   ,     1   )
         self.assertEqual((x - y).component(2   )   ,     2   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Any   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Any   =	Vector([2, -1, 4]   )  # for test of dot product
         lowerCAmelCase__      :			Any   =	Vector([1, -2, -1]   )
         self.assertEqual(str(x * 3.0   )   ,     """(3.0,6.0,9.0)"""   )
         self.assertEqual((a * b)   ,     0   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         self.assertEqual(str(zero_vector(10   )   ).count("""0"""   )   ,     10   )






   def 	_lowerCAmelCase   (		self       :		Tuple   )			->						None:

         """simple docstring"""
         self.assertEqual(str(unit_basis_vector(3   ,     1   )   )   ,     """(0,1,0)"""   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         lowerCAmelCase__      :			Optional[Any]   =	Vector([1, 0, 1]   )
         self.assertEqual(str(axpy(2   ,     UpperCamelCase   ,     UpperCamelCase   )   )   ,     """(3,4,7)"""   )






   def 	_lowerCAmelCase   (		self       :		Optional[int]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 0, 0, 0, 0, 0]   )
         lowerCAmelCase__      :			Any   =	x.copy()
         self.assertEqual(str(UpperCamelCase   )   ,     str(UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		Optional[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Vector([1, 0, 0]   )
         x.change_component(0   ,     0   )
         x.change_component(1   ,     1   )
         self.assertEqual(str(UpperCamelCase   )   ,     """(0,1,0)"""   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[int]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual("""|1,2,3|\n|2,4,5|\n|6,7,8|\n"""   ,     str(UpperCamelCase   )   )






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

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Dict   =	[[-3, -14, -10], [-5, -10, -5], [-2, -1, 0]]
         for x in range(a.height()   ):
               for y in range(a.width()   ):
                     self.assertEqual(minors[x][y]   ,     a.minor(UpperCamelCase   ,     UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			str   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			int   =	[[-3, 14, -10], [5, -10, 5], [-2, 1, 0]]
         for x in range(a.height()   ):
               for y in range(a.width()   ):
                     self.assertEqual(cofactors[x][y]   ,     a.cofactor(UpperCamelCase   ,     UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Union[str, Any]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual(-5   ,     a.determinant()   )






   def 	_lowerCAmelCase   (		self       :		int   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Tuple   =	Vector([1, 2, 3]   )
         self.assertEqual("""(14,32,50)"""   ,     str(a * x   )   )
         self.assertEqual("""|2,4,6|\n|8,10,12|\n|14,16,18|\n"""   ,     str(a * 2   )   )






   def 	_lowerCAmelCase   (		self       :		str   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Dict   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         a.change_component(0   ,     2   ,     5   )
         self.assertEqual("""|1,2,5|\n|2,4,5|\n|6,7,8|\n"""   ,     str(UpperCamelCase   )   )






   def 	_lowerCAmelCase   (		self       :		Tuple   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         self.assertEqual(7   ,     a.component(2   ,     1   )   ,     0.01   )






   def 	_lowerCAmelCase   (		self       :		Any   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Tuple   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			List[Any]   =	Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]   ,     3   ,     3   )
         self.assertEqual("""|2,4,10|\n|4,8,10|\n|12,14,18|\n"""   ,     str(a + b   )   )






   def 	_lowerCAmelCase   (		self       :		List[Any]   )			->						None:

         """simple docstring"""
         lowerCAmelCase__      :			Optional[Any]   =	Matrix([[1, 2, 3], [2, 4, 5], [6, 7, 8]]   ,     3   ,     3   )
         lowerCAmelCase__      :			Dict   =	Matrix([[1, 2, 7], [2, 4, 5], [6, 7, 10]]   ,     3   ,     3   )
         self.assertEqual("""|0,0,-4|\n|0,0,0|\n|0,0,-2|\n"""   ,     str(a - b   )   )






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

         """simple docstring"""
         self.assertEqual(
             """|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n|0,0,0,0,0|\n"""   ,     str(square_zero_matrix(5   )   )   ,     )


if __name__ == "__main__":
  unittest.main()





