





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


lowerCAmelCase__       = logging.get_logger(__name__)

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






class 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""blip_2_vision_model"""


 def __init__(						self		: Union[str, Any]			,						lowerCAmelCase__		: Dict=1_4_0_8			,						lowerCAmelCase__		: Optional[int]=6_1_4_4			,						lowerCAmelCase__		: Union[str, Any]=3_9			,						lowerCAmelCase__		: str=1_6			,						lowerCAmelCase__		: Dict=2_2_4			,						lowerCAmelCase__		: str=1_4			,						lowerCAmelCase__		: int="gelu"			,						lowerCAmelCase__		: Optional[int]=0.00_001			,						lowerCAmelCase__		: str=0.0			,						lowerCAmelCase__		: List[str]=1e-1_0			,						lowerCAmelCase__		: List[str]=True			,						**lowerCAmelCase__		: List[str]			,						)    ->      int:
   super().__init__(**lowerCAmelCase__    )

   UpperCAmelCase					      =  hidden_size
   UpperCAmelCase					      =  intermediate_size
   UpperCAmelCase					      =  num_hidden_layers
   UpperCAmelCase					      =  num_attention_heads
   UpperCAmelCase					      =  patch_size
   UpperCAmelCase					      =  image_size
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  attention_dropout
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  qkv_bias


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

   UpperCAmelCase      ,	UpperCAmelCase					      =  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":
     UpperCAmelCase					      =  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 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""blip_2_qformer"""


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

   UpperCAmelCase					      =  vocab_size
   UpperCAmelCase					      =  hidden_size
   UpperCAmelCase					      =  num_hidden_layers
   UpperCAmelCase					      =  num_attention_heads
   UpperCAmelCase					      =  hidden_act
   UpperCAmelCase					      =  intermediate_size
   UpperCAmelCase					      =  hidden_dropout_prob
   UpperCAmelCase					      =  attention_probs_dropout_prob
   UpperCAmelCase					      =  max_position_embeddings
   UpperCAmelCase					      =  initializer_range
   UpperCAmelCase					      =  layer_norm_eps
   UpperCAmelCase					      =  position_embedding_type
   UpperCAmelCase					      =  cross_attention_frequency
   UpperCAmelCase					      =  encoder_hidden_size


 @classmethod
 def _UpperCamelCase	(						cls		: List[str]			,						lowerCAmelCase__		: Union[str, os.PathLike]			,						**lowerCAmelCase__		: Dict    )    ->      "PretrainedConfig":
   cls._set_token_in_kwargs(lowerCAmelCase__    )

   UpperCAmelCase      ,	UpperCAmelCase					      =  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":
     UpperCAmelCase					      =  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 						__magic_name__    (       _snake_case    ):
 UpperCAmelCase							    =						"""blip-2"""
 UpperCAmelCase							    =						True


 def __init__(						self		: List[Any]			,						lowerCAmelCase__		: Tuple=None			,						lowerCAmelCase__		: int=None			,						lowerCAmelCase__		: Tuple=None			,						lowerCAmelCase__		: Tuple=3_2			,						**lowerCAmelCase__		: Any    )    ->      Optional[int]:
   super().__init__(**lowerCAmelCase__    )

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

   if qformer_config is None:
     UpperCAmelCase					      =  {}
     logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values."    )

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

   UpperCAmelCase					      =  BlipaVisionConfig(**lowerCAmelCase__    )
   UpperCAmelCase					      =  BlipaQFormerConfig(**lowerCAmelCase__    )
   UpperCAmelCase					      =  text_config["model_type"] if "model_type" in text_config else "opt"
   UpperCAmelCase					      =  CONFIG_MAPPING[text_model_type](**lowerCAmelCase__    )

   UpperCAmelCase					      =  self.text_config.tie_word_embeddings
   UpperCAmelCase					      =  self.text_config.is_encoder_decoder

   UpperCAmelCase					      =  num_query_tokens
   UpperCAmelCase					      =  self.vision_config.hidden_size
   UpperCAmelCase					      =  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
   UpperCAmelCase					      =  1.0
   UpperCAmelCase					      =  0.02


 @classmethod
 def _UpperCamelCase	(						cls		: Optional[Any]			,						lowerCAmelCase__		: BlipaVisionConfig			,						lowerCAmelCase__		: BlipaQFormerConfig			,						lowerCAmelCase__		: PretrainedConfig			,						**lowerCAmelCase__		: int			,						)    ->      str:
   return cls(
       vision_config=vision_config.to_dict()			,						qformer_config=qformer_config.to_dict()			,						text_config=text_config.to_dict()			,						**lowerCAmelCase__			,						)


 def _UpperCamelCase	(						self		: Dict    )    ->      Optional[Any]:
   UpperCAmelCase					      =  copy.deepcopy(self.__dict__    )
   UpperCAmelCase					      =  self.vision_config.to_dict()
   UpperCAmelCase					      =  self.qformer_config.to_dict()
   UpperCAmelCase					      =  self.text_config.to_dict()
   UpperCAmelCase					      =  self.__class__.model_type
   return output






def      _lowerCAmelCase(	__A			):

  if not isinstance(__A   ,       __A			):
    raise TypeError("only integers accepted as input"			)
  else:
    UpperCAmelCase					      =  str(abs(__A			)			)
    UpperCAmelCase					      =  [list(__A			) for char in range(len(__A			)			)]
    for index in range(len(__A			)			):
      num_transpositions[index].pop(__A			)
    return max(
        int("".join(list(__A			)			)			) for transposition in num_transpositions			)


if __name__ == "__main__":
      __import__("doctest").testmod()



