




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


lowerCAmelCase_					    = logging.get_logger(__name__)

lowerCAmelCase_					    = {
    """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""",
}






class  _lowerCAmelCase     (  _lowercase	):
      A__		     =		'instructblip_vision_model'

      def __init__(   self      ,				__UpperCAmelCase=1408      ,				__UpperCAmelCase=6144      ,				__UpperCAmelCase=39      ,				__UpperCAmelCase=16      ,				__UpperCAmelCase=224      ,				__UpperCAmelCase=14      ,				__UpperCAmelCase="gelu"      ,				__UpperCAmelCase=1e-6      ,				__UpperCAmelCase=0.0      ,				__UpperCAmelCase=1e-10      ,				__UpperCAmelCase=True      ,				**__UpperCAmelCase      ,				):
        super().__init__(**__UpperCAmelCase		)

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







      @classmethod
      def 	__magic_name__(   cls      ,				__UpperCAmelCase      ,				**__UpperCAmelCase		):
        cls._set_token_in_kwargs(__UpperCAmelCase		)

        lowerCAmelCase__       ,			lowerCAmelCase__				:  Dict		       =     cls.get_config_dict(__UpperCAmelCase      ,				**__UpperCAmelCase		)

        # get the vision config dict if we are loading from InstructBlipConfig
        if config_dict.get('''model_type'''		) == "instructblip":
          lowerCAmelCase__				:  Optional[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  _lowerCAmelCase     (  _lowercase	):
      A__		     =		'instructblip_qformer'

      def __init__(   self      ,				__UpperCAmelCase=3_0522      ,				__UpperCAmelCase=768      ,				__UpperCAmelCase=12      ,				__UpperCAmelCase=12      ,				__UpperCAmelCase=3072      ,				__UpperCAmelCase="gelu"      ,				__UpperCAmelCase=0.1      ,				__UpperCAmelCase=0.1      ,				__UpperCAmelCase=512      ,				__UpperCAmelCase=0.02      ,				__UpperCAmelCase=1e-12      ,				__UpperCAmelCase=0      ,				__UpperCAmelCase="absolute"      ,				__UpperCAmelCase=2      ,				__UpperCAmelCase=1408      ,				**__UpperCAmelCase      ,				):
        super().__init__(pad_token_id=__UpperCAmelCase      ,				**__UpperCAmelCase		)

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







      @classmethod
      def 	__magic_name__(   cls      ,				__UpperCAmelCase      ,				**__UpperCAmelCase		):
        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 InstructBlipConfig
        if config_dict.get('''model_type'''		) == "instructblip":
          lowerCAmelCase__				:  str		       =     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  _lowerCAmelCase     (  _lowercase	):
      A__		     =		'instructblip'
      A__		     =		True

      def __init__(   self      ,				__UpperCAmelCase=None      ,				__UpperCAmelCase=None      ,				__UpperCAmelCase=None      ,				__UpperCAmelCase=32      ,				**__UpperCAmelCase		):
        super().__init__(**__UpperCAmelCase		)

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

        if qformer_config is None:
          lowerCAmelCase__				:  Tuple		       =     {}
          logger.info('''qformer_config is None. Initializing the InstructBlipQFormerConfig with default values.'''		)

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

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

        lowerCAmelCase__				:  Any		       =     self.text_config.tie_word_embeddings
        lowerCAmelCase__				:  Dict		       =     self.text_config.is_encoder_decoder

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

      @classmethod
      def 	__magic_name__(   cls      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				__UpperCAmelCase      ,				**__UpperCAmelCase      ,				):
        return cls(
            vision_config=vision_config.to_dict()      ,				qformer_config=qformer_config.to_dict()      ,				text_config=text_config.to_dict()      ,				**__UpperCAmelCase      ,				)







      def 	__magic_name__(   self		):
        lowerCAmelCase__				:  Optional[Any]		       =     copy.deepcopy(self.__dict__		)
        lowerCAmelCase__				:  Dict		       =     self.vision_config.to_dict()
        lowerCAmelCase__				:  List[Any]		       =     self.qformer_config.to_dict()
        lowerCAmelCase__				:  Union[str, Any]		       =     self.text_config.to_dict()
        lowerCAmelCase__				:  Tuple		       =     self.__class__.model_type
        return output







def      __lowerCAmelCase			(					UpperCamelCase      )     ->	bool:
  if not isinstance(UpperCamelCase	,      UpperCamelCase      ):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''      )
  if len(UpperCamelCase      ) == 0:
    raise ValueError('''Input list must be a non empty list'''      )
  if len(UpperCamelCase      ) == 1:
    return True
  lowerCAmelCase__				:  Tuple		       =     series[1] - series[0]
  for index in range(len(UpperCamelCase      ) - 1      ):
    if series[index + 1] - series[index] != common_diff:
      return False
  return True



def      __lowerCAmelCase			(					UpperCamelCase      )     ->	float:
  if not isinstance(UpperCamelCase	,      UpperCamelCase      ):
    raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''      )
  if len(UpperCamelCase      ) == 0:
    raise ValueError('''Input list must be a non empty list'''      )
  lowerCAmelCase__				:  int		       =     0
  for val in series:
    answer += val
  return answer / len(UpperCamelCase      )


if __name__ == "__main__":
     import doctest

     doctest.testmod()

