


"""simple docstring"""




import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
   from ... import PreTrainedTokenizerBase, TensorType

UpperCAmelCase_				:			str   =	logging.get_logger(__name__)
class 			SCREAMING_SNAKE_CASE__  (    _SCREAMING_SNAKE_CASE      ):
      snake_case__    :					Optional[int]					       =						'''vision-encoder-decoder'''
      snake_case__    :					str					       =						True
      def __init__(	self    :	Any					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Optional[int]:
             super().__init__(**SCREAMING_SNAKE_CASE__					)
             if "encoder" not in kwargs or "decoder" not in kwargs:
                    raise ValueError(
                        F"""A configuraton of type {self.model_type} cannot be instantiated because """
                        F"""not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}"""					)

             a_ :		List[Any]							=  kwargs.pop('encoder'					)
             a_ :		Tuple							=  encoder_config.pop('model_type'					)
             a_ :		int							=  kwargs.pop('decoder'					)
             a_ :		Dict							=  decoder_config.pop('model_type'					)

             a_ :		List[str]							=  AutoConfig.for_model(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
             a_ :		Dict							=  AutoConfig.for_model(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)
             a_ :		Any							=  True
      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							**SCREAMING_SNAKE_CASE__    :	Any					)		->      PretrainedConfig:
             logger.info('Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'					)
             a_ :		Optional[Any]							=  True
             a_ :		Optional[Any]							=  True

             return cls(encoder=encoder_config.to_dict()					,							decoder=decoder_config.to_dict()					,							**SCREAMING_SNAKE_CASE__					)

      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      Optional[int]:
             a_ :		Any							=  copy.deepcopy(self.__dict__					)
             a_ :		str							=  self.encoder.to_dict()
             a_ :		List[str]							=  self.decoder.to_dict()
             a_ :		str							=  self.__class__.model_type
             return output
class 			SCREAMING_SNAKE_CASE__  (    _SCREAMING_SNAKE_CASE      ):
      snake_case__    :					int					       =						version.parse('''1.11'''      )
      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      Mapping[str, Mapping[int, str]]:
             return OrderedDict(
                 [
                     ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
                 ]					)
      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					)		->      float:
             return 1E-4

      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					)		->      Mapping[str, Mapping[int, str]]:
             return OrderedDict({'last_hidden_state': {0: 'batch', 1: 'encoder_sequence'}}					)
class 			SCREAMING_SNAKE_CASE__  (    _SCREAMING_SNAKE_CASE      ):
      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      Mapping[str, Mapping[int, str]]:
             a_ :		List[str]							=  OrderedDict()
             a_ :		str							=  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
             a_ :		Any							=  {0: 'batch', 1: 'past_decoder_sequence + sequence'}
             a_ :		Any							=  {0: 'batch', 1: 'encoder_sequence'}

             return common_inputs

      def 	SCREAMING_SNAKE_CASE							(	self    :	Any					,							SCREAMING_SNAKE_CASE__    :	int					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any] = -1					,							SCREAMING_SNAKE_CASE__    :	Dict = -1					,							SCREAMING_SNAKE_CASE__    :	List[Any] = False					,							SCREAMING_SNAKE_CASE__    :	str = None					,							)		->      Mapping[str, Any]:
             import torch

             a_ :		Dict							=  OrderedDict()

             a_ :		Any							=  super().generate_dummy_inputs(
                 SCREAMING_SNAKE_CASE__					,							batch_size=SCREAMING_SNAKE_CASE__					,							seq_length=SCREAMING_SNAKE_CASE__					,							is_pair=SCREAMING_SNAKE_CASE__					,							framework=SCREAMING_SNAKE_CASE__					)

             a_							,		a_ :		int							=  dummy_input['input_ids'].shape
             a_ :		Any							=  (batch, encoder_sequence, self._config.encoder_hidden_size)
             a_ :		Optional[Any]							=  dummy_input.pop('input_ids'					)
             a_ :		Any							=  dummy_input.pop('attention_mask'					)
             a_ :		List[str]							=  torch.zeros(SCREAMING_SNAKE_CASE__					)

             return common_inputs
class 			SCREAMING_SNAKE_CASE__  (    _SCREAMING_SNAKE_CASE      ):
      @property
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      None:
             pass
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]					)		->      OnnxConfig:
             return VisionEncoderDecoderEncoderOnnxConfig(SCREAMING_SNAKE_CASE__					)

      def 	SCREAMING_SNAKE_CASE							(	self    :	Dict					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Any = "default"					)		->      OnnxConfig:
             a_ :		List[Any]							=  encoder_config.hidden_size
             return VisionEncoderDecoderDecoderOnnxConfig(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)






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_				:			str   =	logging.get_logger(__name__)

UpperCAmelCase_				:			str   =	{
    'Salesforce/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}
class 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					int					       =						'''instructblip_vision_model'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	List[Any]=1_4_0_8					,							SCREAMING_SNAKE_CASE__    :	int=6_1_4_4					,							SCREAMING_SNAKE_CASE__    :	Dict=3_9					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=1_6					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2_2_4					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=1_4					,							SCREAMING_SNAKE_CASE__    :	List[Any]="gelu"					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-6					,							SCREAMING_SNAKE_CASE__    :	Tuple=0.0					,							SCREAMING_SNAKE_CASE__    :	List[str]=1E-10					,							SCREAMING_SNAKE_CASE__    :	str=True					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      int:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

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

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

             a_							,		a_ :		Dict							=  cls.get_config_dict(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             # get the vision config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		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 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					List[Any]					       =						'''instructblip_qformer'''
      def __init__(	self    :	Optional[int]					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=3_0_5_2_2					,							SCREAMING_SNAKE_CASE__    :	str=7_6_8					,							SCREAMING_SNAKE_CASE__    :	List[str]=1_2					,							SCREAMING_SNAKE_CASE__    :	Any=1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=3_0_7_2					,							SCREAMING_SNAKE_CASE__    :	List[str]="gelu"					,							SCREAMING_SNAKE_CASE__    :	Dict=0.1					,							SCREAMING_SNAKE_CASE__    :	int=0.1					,							SCREAMING_SNAKE_CASE__    :	Optional[int]=5_1_2					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=0.02					,							SCREAMING_SNAKE_CASE__    :	int=1E-12					,							SCREAMING_SNAKE_CASE__    :	Dict=0					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]="absolute"					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2					,							SCREAMING_SNAKE_CASE__    :	str=1_4_0_8					,							**SCREAMING_SNAKE_CASE__    :	Dict					,							)		->      Optional[Any]:
             super().__init__(pad_token_id=SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

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

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

             a_							,		a_ :		Union[str, Any]							=  cls.get_config_dict(SCREAMING_SNAKE_CASE__					,							**SCREAMING_SNAKE_CASE__					)

             # get the qformer config dict if we are loading from InstructBlipConfig
             if config_dict.get('model_type'					) == "instructblip":
                    a_ :		Dict							=  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 			SCREAMING_SNAKE_CASE__  (    lowercase__      ):
      snake_case__    :					Union[str, Any]					       =						'''instructblip'''
      snake_case__    :					List[str]					       =						True
      def __init__(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=None					,							SCREAMING_SNAKE_CASE__    :	Dict=None					,							SCREAMING_SNAKE_CASE__    :	List[Any]=None					,							SCREAMING_SNAKE_CASE__    :	int=3_2					,							**SCREAMING_SNAKE_CASE__    :	Union[str, Any]					)		->      Tuple:
             super().__init__(**SCREAMING_SNAKE_CASE__					)

             if vision_config is None:
                    a_ :		Dict							=  {}
                    logger.info('vision_config is None. initializing the InstructBlipVisionConfig with default values.'					)

             if qformer_config is None:
                    a_ :		List[str]							=  {}
                    logger.info('qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'					)

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

             a_ :		int							=  InstructBlipVisionConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		int							=  InstructBlipQFormerConfig(**SCREAMING_SNAKE_CASE__					)
             a_ :		Optional[Any]							=  text_config['model_type'] if 'model_type' in text_config else 'opt'
             a_ :		List[Any]							=  CONFIG_MAPPING[text_model_type](**SCREAMING_SNAKE_CASE__					)

             a_ :		Tuple							=  self.text_config.tie_word_embeddings
             a_ :		List[Any]							=  self.text_config.is_encoder_decoder

             a_ :		Optional[Any]							=  num_query_tokens
             a_ :		int							=  self.vision_config.hidden_size
             a_ :		List[str]							=  self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
             a_ :		Optional[int]							=  1.0
             a_ :		Any							=  0.02
      @classmethod
      def 	SCREAMING_SNAKE_CASE							(	cls    :	List[Any]					,							SCREAMING_SNAKE_CASE__    :	InstructBlipVisionConfig					,							SCREAMING_SNAKE_CASE__    :	InstructBlipQFormerConfig					,							SCREAMING_SNAKE_CASE__    :	PretrainedConfig					,							**SCREAMING_SNAKE_CASE__    :	List[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 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      Union[str, Any]:
             a_ :		List[Any]							=  copy.deepcopy(self.__dict__					)
             a_ :		Optional[int]							=  self.vision_config.to_dict()
             a_ :		Optional[Any]							=  self.qformer_config.to_dict()
             a_ :		Union[str, Any]							=  self.text_config.to_dict()
             a_ :		Optional[Any]							=  self.__class__.model_type
             return output


