



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     lowerCAmelCase_		(  a__							):
       UpperCAmelCase__    : Optional[Any]       					=  "blip_2_vision_model"
       def __init__(      self,      SCREAMING_SNAKE_CASE_=1408,      SCREAMING_SNAKE_CASE_=6144,      SCREAMING_SNAKE_CASE_=39,      SCREAMING_SNAKE_CASE_=16,      SCREAMING_SNAKE_CASE_=224,      SCREAMING_SNAKE_CASE_=14,      SCREAMING_SNAKE_CASE_="gelu",      SCREAMING_SNAKE_CASE_=0.0_00_01,      SCREAMING_SNAKE_CASE_=0.0,      SCREAMING_SNAKE_CASE_=1e-10,      SCREAMING_SNAKE_CASE_=True,      **SCREAMING_SNAKE_CASE_,      )					->		List[Any]:
          super().__init__(**SCREAMING_SNAKE_CASE_	)

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






       @classmethod
       def  snake_case_	(      cls,      SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)					->		"PretrainedConfig":
          cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_	)

          UpperCamelCase     ,					UpperCamelCase   :  Optional[Any]						     =     cls.get_config_dict(SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)

          # get the vision config dict if we are loading from Blip2Config
          if config_dict.get('model_type'	) == "blip-2":
             UpperCamelCase   :  List[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(SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)






class     lowerCAmelCase_		(  a__							):
       UpperCAmelCase__    : Union[str, Any]       					=  "blip_2_qformer"
       def __init__(      self,      SCREAMING_SNAKE_CASE_=3_0522,      SCREAMING_SNAKE_CASE_=768,      SCREAMING_SNAKE_CASE_=12,      SCREAMING_SNAKE_CASE_=12,      SCREAMING_SNAKE_CASE_=3072,      SCREAMING_SNAKE_CASE_="gelu",      SCREAMING_SNAKE_CASE_=0.1,      SCREAMING_SNAKE_CASE_=0.1,      SCREAMING_SNAKE_CASE_=512,      SCREAMING_SNAKE_CASE_=0.02,      SCREAMING_SNAKE_CASE_=1e-12,      SCREAMING_SNAKE_CASE_=0,      SCREAMING_SNAKE_CASE_="absolute",      SCREAMING_SNAKE_CASE_=2,      SCREAMING_SNAKE_CASE_=1408,      **SCREAMING_SNAKE_CASE_,      )					->		Tuple:
          super().__init__(pad_token_id=SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)

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






       @classmethod
       def  snake_case_	(      cls,      SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)					->		"PretrainedConfig":
          cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE_	)

          UpperCamelCase     ,					UpperCamelCase   :  str						     =     cls.get_config_dict(SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_	)

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






class     lowerCAmelCase_		(  a__							):
       UpperCAmelCase__    : Tuple       					=  "blip-2"
       UpperCAmelCase__    : str       					=  True
       def __init__(      self,      SCREAMING_SNAKE_CASE_=None,      SCREAMING_SNAKE_CASE_=None,      SCREAMING_SNAKE_CASE_=None,      SCREAMING_SNAKE_CASE_=32,      **SCREAMING_SNAKE_CASE_	)					->		Optional[int]:
          super().__init__(**SCREAMING_SNAKE_CASE_	)

          if vision_config is None:
             UpperCamelCase   :  int						     =     {}
             logger.info('vision_config is None. initializing the Blip2VisionConfig with default values.'	)

          if qformer_config is None:
             UpperCamelCase   :  int						     =     {}
             logger.info('qformer_config is None. Initializing the Blip2QFormerConfig with default values.'	)

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

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

          UpperCamelCase   :  str						     =     self.text_config.tie_word_embeddings
          UpperCamelCase   :  Optional[int]						     =     self.text_config.is_encoder_decoder

          UpperCamelCase   :  str						     =     num_query_tokens
          UpperCamelCase   :  Dict						     =     self.vision_config.hidden_size
          UpperCamelCase   :  List[str]						     =     self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
          UpperCamelCase   :  Optional[Any]						     =     1.0
          UpperCamelCase   :  Optional[Any]						     =     0.02
       @classmethod
       def  snake_case_	(      cls,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      **SCREAMING_SNAKE_CASE_,      )					->		Union[str, Any]:
          return cls(
              vision_config=vision_config.to_dict(),      qformer_config=qformer_config.to_dict(),      text_config=text_config.to_dict(),      **SCREAMING_SNAKE_CASE_,      )






       def  snake_case_	(      self	)					->		Union[str, Any]:
          UpperCamelCase   :  Optional[int]						     =     copy.deepcopy(self.__dict__	)
          UpperCamelCase   :  Union[str, Any]						     =     self.vision_config.to_dict()
          UpperCamelCase   :  List[Any]						     =     self.qformer_config.to_dict()
          UpperCamelCase   :  str						     =     self.text_config.to_dict()
          UpperCamelCase   :  List[Any]						     =     self.__class__.model_type
          return output








from abc import ABC, abstractmethod
from argparse import ArgumentParser



class     lowerCAmelCase_		(  a__							):
       @staticmethod
       @abstractmethod
       def  snake_case_	(      SCREAMING_SNAKE_CASE_	)					->		int:
          raise NotImplementedError()






       @abstractmethod
       def  snake_case_	(      self	)					->		str:
          raise NotImplementedError()



