





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tensorflow_text_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__A             =       {
    "configuration_bert": ["BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "BertConfig", "BertOnnxConfig"],
    "tokenization_bert": ["BasicTokenizer", "BertTokenizer", "WordpieceTokenizer"],
}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A             =       ["BertTokenizerFast"]

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A             =       [
         "BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
         "BertForMaskedLM",
         "BertForMultipleChoice",
         "BertForNextSentencePrediction",
         "BertForPreTraining",
         "BertForQuestionAnswering",
         "BertForSequenceClassification",
         "BertForTokenClassification",
         "BertLayer",
         "BertLMHeadModel",
         "BertModel",
         "BertPreTrainedModel",
         "load_tf_weights_in_bert",
     ]

try:
     if not is_tf_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A             =       [
         "TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
         "TFBertEmbeddings",
         "TFBertForMaskedLM",
         "TFBertForMultipleChoice",
         "TFBertForNextSentencePrediction",
         "TFBertForPreTraining",
         "TFBertForQuestionAnswering",
         "TFBertForSequenceClassification",
         "TFBertForTokenClassification",
         "TFBertLMHeadModel",
         "TFBertMainLayer",
         "TFBertModel",
         "TFBertPreTrainedModel",
     ]
try:
     if not is_tensorflow_text_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A             =       ["TFBertTokenizer"]

try:
     if not is_flax_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     __A             =       [
         "FlaxBertForCausalLM",
         "FlaxBertForMaskedLM",
         "FlaxBertForMultipleChoice",
         "FlaxBertForNextSentencePrediction",
         "FlaxBertForPreTraining",
         "FlaxBertForQuestionAnswering",
         "FlaxBertForSequenceClassification",
         "FlaxBertForTokenClassification",
         "FlaxBertModel",
         "FlaxBertPreTrainedModel",
     ]

if TYPE_CHECKING:
     from .configuration_bert import BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, BertConfig, BertOnnxConfig
     from .tokenization_bert import BasicTokenizer, BertTokenizer, WordpieceTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_bert_fast import BertTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_bert import (
              BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
              BertForMaskedLM,
              BertForMultipleChoice,
              BertForNextSentencePrediction,
              BertForPreTraining,
              BertForQuestionAnswering,
              BertForSequenceClassification,
              BertForTokenClassification,
              BertLayer,
              BertLMHeadModel,
              BertModel,
              BertPreTrainedModel,
              load_tf_weights_in_bert,
          )

     try:
          if not is_tf_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_tf_bert import (
              TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
              TFBertEmbeddings,
              TFBertForMaskedLM,
              TFBertForMultipleChoice,
              TFBertForNextSentencePrediction,
              TFBertForPreTraining,
              TFBertForQuestionAnswering,
              TFBertForSequenceClassification,
              TFBertForTokenClassification,
              TFBertLMHeadModel,
              TFBertMainLayer,
              TFBertModel,
              TFBertPreTrainedModel,
          )

     try:
          if not is_tensorflow_text_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_bert_tf import TFBertTokenizer

     try:
          if not is_flax_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_flax_bert import (
              FlaxBertForCausalLM,
              FlaxBertForMaskedLM,
              FlaxBertForMultipleChoice,
              FlaxBertForNextSentencePrediction,
              FlaxBertForPreTraining,
              FlaxBertForQuestionAnswering,
              FlaxBertForSequenceClassification,
              FlaxBertForTokenClassification,
              FlaxBertModel,
              FlaxBertPreTrainedModel,
          )

else:
     import sys

     __A             =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)








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      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "blip_2_vision_model"

 def __init__(self       :     Union[str, Any] ,				UpperCAmelCase_       :     int=1_408 ,				UpperCAmelCase_       :     List[str]=6_144 ,				UpperCAmelCase_       :     List[Any]=39 ,				UpperCAmelCase_       :     Tuple=16 ,				UpperCAmelCase_       :     List[str]=224 ,				UpperCAmelCase_       :     Any=14 ,				UpperCAmelCase_       :     Dict="gelu" ,				UpperCAmelCase_       :     str=0.0_0001 ,				UpperCAmelCase_       :     List[str]=0.0 ,				UpperCAmelCase_       :     str=1E-1_0 ,				UpperCAmelCase_       :     Any=True ,				**UpperCAmelCase_       :     Optional[Any] ,				) ->Optional[int]:



   '''simple docstring'''



   super().__init__(**UpperCAmelCase_)

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







 @classmethod
 def 							SCREAMING_SNAKE_CASE_      (cls       :     Optional[int] ,				UpperCAmelCase_       :     Union[str, os.PathLike] ,				**UpperCAmelCase_       :     List[Any]) ->"PretrainedConfig":



   '''simple docstring'''



   cls._set_token_in_kwargs(UpperCAmelCase_)

   lowerCamelCase__						,      lowerCamelCase__:   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":
     lowerCamelCase__:   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      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "blip_2_qformer"

 def __init__(self       :     str ,				UpperCAmelCase_       :     Any=30_522 ,				UpperCAmelCase_       :     Union[str, Any]=768 ,				UpperCAmelCase_       :     Tuple=12 ,				UpperCAmelCase_       :     Union[str, Any]=12 ,				UpperCAmelCase_       :     Optional[int]=3_072 ,				UpperCAmelCase_       :     Any="gelu" ,				UpperCAmelCase_       :     Optional[int]=0.1 ,				UpperCAmelCase_       :     List[Any]=0.1 ,				UpperCAmelCase_       :     Optional[int]=512 ,				UpperCAmelCase_       :     List[Any]=0.02 ,				UpperCAmelCase_       :     Optional[Any]=1E-1_2 ,				UpperCAmelCase_       :     Dict=0 ,				UpperCAmelCase_       :     Optional[int]="absolute" ,				UpperCAmelCase_       :     List[Any]=2 ,				UpperCAmelCase_       :     int=1_408 ,				**UpperCAmelCase_       :     Optional[int] ,				) ->List[str]:



   '''simple docstring'''



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

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







 @classmethod
 def 							SCREAMING_SNAKE_CASE_      (cls       :     Union[str, Any] ,				UpperCAmelCase_       :     Union[str, os.PathLike] ,				**UpperCAmelCase_       :     List[Any]) ->"PretrainedConfig":



   '''simple docstring'''



   cls._set_token_in_kwargs(UpperCAmelCase_)

   lowerCamelCase__						,      lowerCamelCase__:   Tuple    						=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__:   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(UpperCAmelCase_ ,				**UpperCAmelCase_)



class      _SCREAMING_SNAKE_CASE		(			__SCREAMING_SNAKE_CASE					):







 '''simple docstring'''
 lowercase_	=    "blip-2"
 lowercase_	=    True

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



   '''simple docstring'''



   super().__init__(**UpperCAmelCase_)

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

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

   if text_config is None:
     lowerCamelCase__:   Union[str, Any]    						={}
     logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`).")

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

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

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

 @classmethod
 def 							SCREAMING_SNAKE_CASE_      (cls       :     Any ,				UpperCAmelCase_       :     BlipaVisionConfig ,				UpperCAmelCase_       :     BlipaQFormerConfig ,				UpperCAmelCase_       :     PretrainedConfig ,				**UpperCAmelCase_       :     int ,				) ->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 							SCREAMING_SNAKE_CASE_      (self       :     List[str]) ->int:



   '''simple docstring'''



   lowerCamelCase__:   List[Any]    						=copy.deepcopy(self.__dict__)
   lowerCamelCase__:   Any    						=self.vision_config.to_dict()
   lowerCamelCase__:   Any    						=self.qformer_config.to_dict()
   lowerCamelCase__:   Any    						=self.text_config.to_dict()
   lowerCamelCase__:   int    						=self.__class__.model_type
   return output

