






'''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


SCREAMING_SNAKE_CASE						=	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE						=	{
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}
class UpperCAmelCase_					(    _snake_case				):




     """simple docstring"""




     UpperCamelCase_								=			'''blip_2_vision_model'''






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


      '''simple docstring'''

      super().__init__(**lowerCAmelCase__		)

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






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


      '''simple docstring'''

      cls._set_token_in_kwargs(lowerCAmelCase__		)

      lowercase  ,		lowercase					:   Any				  =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":
       lowercase					:   str				  =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 UpperCAmelCase_					(    _snake_case				):




     """simple docstring"""




     UpperCamelCase_								=			'''blip_2_qformer'''






     def __init__( self			:		Any     ,      UpperCAmelCase			:		Dict=3_0522     ,      UpperCAmelCase			:		str=768     ,      UpperCAmelCase			:		Union[str, Any]=12     ,      UpperCAmelCase			:		Any=12     ,      UpperCAmelCase			:		Tuple=3072     ,      UpperCAmelCase			:		Any="gelu"     ,      UpperCAmelCase			:		Union[str, Any]=0.1     ,      UpperCAmelCase			:		List[Any]=0.1     ,      UpperCAmelCase			:		Optional[Any]=512     ,      UpperCAmelCase			:		List[str]=0.0_2     ,      UpperCAmelCase			:		Union[str, Any]=1e-12     ,      UpperCAmelCase			:		Dict=0     ,      UpperCAmelCase			:		Optional[int]="absolute"     ,      UpperCAmelCase			:		Union[str, Any]=2     ,      UpperCAmelCase			:		List[str]=1408     ,      **UpperCAmelCase			:		Tuple     ,      )  ->  Union[str, Any]:


      '''simple docstring'''

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

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






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


      '''simple docstring'''

      cls._set_token_in_kwargs(lowerCAmelCase__		)

      lowercase  ,		lowercase					:   Dict				  =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":
       lowercase					:   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(lowerCAmelCase__     ,      **lowerCAmelCase__		)





class UpperCAmelCase_					(    _snake_case				):




     """simple docstring"""




     UpperCamelCase_								=			'''blip-2'''
     UpperCamelCase_								=			True






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


      '''simple docstring'''

      super().__init__(**lowerCAmelCase__		)

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

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

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

      lowercase					:   Dict				  =BlipaVisionConfig(**lowerCAmelCase__		)
      lowercase					:   Union[str, Any]				  =BlipaQFormerConfig(**lowerCAmelCase__		)
      lowercase					:   Tuple				  =text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
      lowercase					:   Union[str, Any]				  =CONFIG_MAPPING[text_model_type](**lowerCAmelCase__		)

      lowercase					:   Any				  =self.text_config.tie_word_embeddings
      lowercase					:   List[Any]				  =self.text_config.is_encoder_decoder

      lowercase					:   List[Any]				  =num_query_tokens
      lowercase					:   List[str]				  =self.vision_config.hidden_size
      lowercase					:   Dict				  =self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
      lowercase					:   Any				  =1.0
      lowercase					:   List[Any]				  =0.0_2






     @classmethod
     def 				A__      ( cls			:		Optional[Any]     ,      UpperCAmelCase			:		BlipaVisionConfig     ,      UpperCAmelCase			:		BlipaQFormerConfig     ,      UpperCAmelCase			:		PretrainedConfig     ,      **UpperCAmelCase			:		int     ,      )  ->  str:


      '''simple docstring'''

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






     def 				A__      ( self			:		Dict		)  ->  Optional[Any]:


      '''simple docstring'''

      lowercase					:   str				  =copy.deepcopy(self.__dict__		)
      lowercase					:   Tuple				  =self.vision_config.to_dict()
      lowercase					:   Union[str, Any]				  =self.qformer_config.to_dict()
      lowercase					:   str				  =self.text_config.to_dict()
      lowercase					:   int				  =self.__class__.model_type
      return output











'''simple docstring'''



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
)


SCREAMING_SNAKE_CASE						=	{
    'configuration_mega': ['MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP', 'MegaConfig', 'MegaOnnxConfig'],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      SCREAMING_SNAKE_CASE						=	[
          'MEGA_PRETRAINED_MODEL_ARCHIVE_LIST',
          'MegaForCausalLM',
          'MegaForMaskedLM',
          'MegaForMultipleChoice',
          'MegaForQuestionAnswering',
          'MegaForSequenceClassification',
          'MegaForTokenClassification',
          'MegaModel',
          'MegaPreTrainedModel',
      ]

if TYPE_CHECKING:
      from .configuration_mega import MEGA_PRETRAINED_CONFIG_ARCHIVE_MAP, MegaConfig, MegaOnnxConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_mega import (
                MEGA_PRETRAINED_MODEL_ARCHIVE_LIST,
                MegaForCausalLM,
                MegaForMaskedLM,
                MegaForMultipleChoice,
                MegaForQuestionAnswering,
                MegaForSequenceClassification,
                MegaForTokenClassification,
                MegaModel,
                MegaPreTrainedModel,
            )

else:
      import sys

      SCREAMING_SNAKE_CASE						=	_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



