





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


lowercase__				:int				   =       logging.get_logger(__name__)

lowercase__				:Optional[Any]				   =       {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}




class 							snake_case							(			__UpperCAmelCase ):

      '''simple docstring'''



      _A     :       Optional[int]         =       'blip_2_vision_model'

      def __init__(							self       :      Any						,				__lowercase       :      List[str]=1_408						,				__lowercase       :      str=6_144						,				__lowercase       :      Optional[int]=39						,				__lowercase       :      List[str]=16						,				__lowercase       :      Optional[int]=224						,				__lowercase       :      str=14						,				__lowercase       :      List[Any]="gelu"						,				__lowercase       :      Optional[Any]=0.0_0_0_0_1						,				__lowercase       :      Dict=0.0						,				__lowercase       :      List[str]=1e-10						,				__lowercase       :      Union[str, Any]=True						,				**__lowercase       :      Optional[int]						,				):


        '''simple docstring'''




        super().__init__(**__lowercase )

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







      @classmethod
      def       A_					(							cls       :      Optional[Any]						,				__lowercase       :      Union[str, os.PathLike]						,				**__lowercase       :      Optional[Any] ):


        '''simple docstring'''




        cls._set_token_in_kwargs(__lowercase )

        __UpperCAmelCase       ,     __UpperCAmelCase    :						Optional[Any]       =						cls.get_config_dict(__lowercase						,				**__lowercase )

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




class 							snake_case							(			__UpperCAmelCase ):

      '''simple docstring'''



      _A     :       int         =       'blip_2_qformer'

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


        '''simple docstring'''




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

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







      @classmethod
      def       A_					(							cls       :      int						,				__lowercase       :      Union[str, os.PathLike]						,				**__lowercase       :      Optional[int] ):


        '''simple docstring'''




        cls._set_token_in_kwargs(__lowercase )

        __UpperCAmelCase       ,     __UpperCAmelCase    :						List[str]       =						cls.get_config_dict(__lowercase						,				**__lowercase )

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




class 							snake_case							(			__UpperCAmelCase ):

      '''simple docstring'''



      _A     :       Union[str, Any]         =       'blip-2'
      _A     :       str         =       True

      def __init__(							self       :      Any						,				__lowercase       :      Union[str, Any]=None						,				__lowercase       :      Union[str, Any]=None						,				__lowercase       :      Any=None						,				__lowercase       :      List[str]=32						,				**__lowercase       :      List[Any] ):


        '''simple docstring'''




        super().__init__(**__lowercase )

        if vision_config is None:
          __UpperCAmelCase    :						Any       =						{}
          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    :						Any       =						BlipaVisionConfig(**__lowercase )
        __UpperCAmelCase    :						List[str]       =						BlipaQFormerConfig(**__lowercase )
        __UpperCAmelCase    :						Any       =						text_config['''model_type'''] if '''model_type''' in text_config else '''opt'''
        __UpperCAmelCase    :						Optional[Any]       =						CONFIG_MAPPING[text_model_type](**__lowercase )

        __UpperCAmelCase    :						str       =						self.text_config.tie_word_embeddings
        __UpperCAmelCase    :						Dict       =						self.text_config.is_encoder_decoder

        __UpperCAmelCase    :						Union[str, Any]       =						num_query_tokens
        __UpperCAmelCase    :						Union[str, Any]       =						self.vision_config.hidden_size
        __UpperCAmelCase    :						Optional[Any]       =						self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
        __UpperCAmelCase    :						Union[str, Any]       =						1.0
        __UpperCAmelCase    :						Optional[Any]       =						0.0_2

      @classmethod
      def       A_					(							cls       :      str						,				__lowercase       :      BlipaVisionConfig						,				__lowercase       :      BlipaQFormerConfig						,				__lowercase       :      PretrainedConfig						,				**__lowercase       :      Dict						,				):


        '''simple docstring'''




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







      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




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






"""simple docstring"""





from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase__				:int				   =       logging.get_logger(__name__)

lowercase__				:List[str]				   =       {
    'EleutherAI/gpt-neox-20b': 'https://huggingface.co/EleutherAI/gpt-neox-20b/resolve/main/config.json',
    # See all GPTNeoX models at https://huggingface.co/models?filter=gpt_neox
}




class 							snake_case							(			__UpperCAmelCase ):

      '''simple docstring'''



      _A     :       List[Any]         =       'gpt_neox'

      def __init__(							self       :      Union[str, Any]						,				__lowercase       :      Any=50_432						,				__lowercase       :      int=6_144						,				__lowercase       :      List[str]=44						,				__lowercase       :      List[Any]=64						,				__lowercase       :      int=24_576						,				__lowercase       :      List[str]="gelu"						,				__lowercase       :      Optional[int]=0.2_5						,				__lowercase       :      Any=10_000						,				__lowercase       :      Union[str, Any]=0.0						,				__lowercase       :      List[str]=0.0						,				__lowercase       :      Union[str, Any]=0.1						,				__lowercase       :      Optional[int]=2_048						,				__lowercase       :      Union[str, Any]=0.0_2						,				__lowercase       :      Optional[int]=1e-5						,				__lowercase       :      str=True						,				__lowercase       :      str=0						,				__lowercase       :      Tuple=2						,				__lowercase       :      Tuple=False						,				__lowercase       :      int=True						,				__lowercase       :      Any=None						,				**__lowercase       :      List[str]						,				):


        '''simple docstring'''




        super().__init__(bos_token_id=__lowercase						,				eos_token_id=__lowercase						,				**__lowercase )
        __UpperCAmelCase    :						int       =						vocab_size
        __UpperCAmelCase    :						Union[str, Any]       =						max_position_embeddings
        __UpperCAmelCase    :						List[str]       =						hidden_size
        __UpperCAmelCase    :						Tuple       =						num_hidden_layers
        __UpperCAmelCase    :						Any       =						num_attention_heads
        __UpperCAmelCase    :						Union[str, Any]       =						intermediate_size
        __UpperCAmelCase    :						str       =						hidden_act
        __UpperCAmelCase    :						str       =						rotary_pct
        __UpperCAmelCase    :						str       =						rotary_emb_base
        __UpperCAmelCase    :						Dict       =						attention_dropout
        __UpperCAmelCase    :						str       =						hidden_dropout
        __UpperCAmelCase    :						Any       =						classifier_dropout
        __UpperCAmelCase    :						Tuple       =						initializer_range
        __UpperCAmelCase    :						Any       =						layer_norm_eps
        __UpperCAmelCase    :						Optional[int]       =						use_cache
        __UpperCAmelCase    :						List[Any]       =						tie_word_embeddings
        __UpperCAmelCase    :						Any       =						use_parallel_residual
        __UpperCAmelCase    :						List[str]       =						rope_scaling
        self._rope_scaling_validation()

        if self.hidden_size % self.num_attention_heads != 0:
          raise ValueError(
              '''The hidden size is not divisble by the number of attention heads! Make sure to update them!''' )







      def       A_					(							self       :      int ):


        '''simple docstring'''




        if self.rope_scaling is None:
          return

        if not isinstance(self.rope_scaling						,				__lowercase ) or len(self.rope_scaling ) != 2:
          raise ValueError(
              '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
              f'''got {self.rope_scaling}''' )
        __UpperCAmelCase    :						Union[str, Any]       =						self.rope_scaling.get('''type'''						,				__lowercase )
        __UpperCAmelCase    :						List[Any]       =						self.rope_scaling.get('''factor'''						,				__lowercase )
        if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
          raise ValueError(
              f'''`rope_scaling`\'s name field must be one of [\'linear\', \'dynamic\'], got {rope_scaling_type}''' )
        if rope_scaling_factor is None or not isinstance(__lowercase						,				__lowercase ) or rope_scaling_factor <= 1.0:
          raise ValueError(f'''`rope_scaling`\'s factor field must be an float > 1, got {rope_scaling_factor}''' )