

from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_UpperCAmelCase	:     int		       = logging.get_logger(__name__)

_UpperCAmelCase	:     int		       = {
    """EleutherAI/gpt-j-6B""": """https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json""",
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}







class 				lowercase       ( UpperCamelCase__    ):
    __SCREAMING_SNAKE_CASE      :				Union[str, Any]     =    '''gptj'''
    __SCREAMING_SNAKE_CASE      :				Dict     =    {
        '''max_position_embeddings''': '''n_positions''',
        '''hidden_size''': '''n_embd''',
        '''num_attention_heads''': '''n_head''',
        '''num_hidden_layers''': '''n_layer''',
    }




    def __init__(      self   ,     snake_case=5_0400   ,     snake_case=2048   ,     snake_case=4096   ,     snake_case=28   ,     snake_case=16   ,     snake_case=64   ,     snake_case=None   ,     snake_case="gelu_new"   ,     snake_case=0.0   ,     snake_case=0.0   ,     snake_case=0.0   ,     snake_case=1e-5   ,     snake_case=0.02   ,     snake_case=True   ,     snake_case=5_0256   ,     snake_case=5_0256   ,     snake_case=False   ,     **snake_case   ,     ):
           snake_case_           =					vocab_size
           snake_case_           =					n_positions
           snake_case_           =					n_embd
           snake_case_           =					n_layer
           snake_case_           =					n_head
           snake_case_           =					n_inner
           snake_case_           =					rotary_dim
           snake_case_           =					activation_function
           snake_case_           =					resid_pdrop
           snake_case_           =					embd_pdrop
           snake_case_           =					attn_pdrop
           snake_case_           =					layer_norm_epsilon
           snake_case_           =					initializer_range
           snake_case_           =					use_cache

           snake_case_           =					bos_token_id
           snake_case_           =					eos_token_id

           super().__init__(
               bos_token_id=__A   ,     eos_token_id=__A   ,     tie_word_embeddings=__A   ,     **__A )







class 				lowercase       ( UpperCamelCase__    ):




    def __init__(      self   ,     snake_case   ,     snake_case = "default"   ,     snake_case = None   ,     snake_case = False   ,     ):
           super().__init__(__A   ,     task=__A   ,     patching_specs=__A   ,     use_past=__A )
           if not getattr(self._config   ,     'pad_token_id'   ,     __A ):
                  # TODO: how to do that better?
                  snake_case_           =					0




    @property
    def 					a							(      self ):
           snake_case_           =					OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}} )
           if self.use_past:
                  self.fill_with_past_key_values_(__A   ,     direction='inputs' )
                  snake_case_           =					{0: 'batch', 1: 'past_sequence + sequence'}
           else:
                  snake_case_           =					{0: 'batch', 1: 'sequence'}

           return common_inputs




    @property
    def 					a							(      self ):
           return self._config.n_layer




    @property
    def 					a							(      self ):
           return self._config.n_head




    def 					a							(      self   ,     snake_case   ,     snake_case = -1   ,     snake_case = -1   ,     snake_case = False   ,     snake_case = None   ,     ):
           snake_case_           =					super(__A   ,     self ).generate_dummy_inputs(
               __A   ,     batch_size=__A   ,     seq_length=__A   ,     is_pair=__A   ,     framework=__A )

           # We need to order the input in the way they appears in the forward()
           snake_case_           =					OrderedDict({'input_ids': common_inputs['input_ids']} )

           # Need to add the past_keys
           if self.use_past:
                  if not is_torch_available():
                         raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.' )
                  else:
                         import torch

                         snake_case_       ,					snake_case_           =					common_inputs['input_ids'].shape
                         # Not using the same length for past_key_values
                         snake_case_           =					seqlen + 2
                         snake_case_           =					(
                             batch,
                             self.num_attention_heads,
                             past_key_values_length,
                             self._config.hidden_size // self.num_attention_heads,
                         )
                         snake_case_           =					[
                             (torch.zeros(__A ), torch.zeros(__A )) for _ in range(self.num_layers )
                         ]

           snake_case_           =					common_inputs['attention_mask']
           if self.use_past:
                  snake_case_           =					ordered_inputs['attention_mask'].dtype
                  snake_case_           =					torch.cat(
                      [ordered_inputs['attention_mask'], torch.ones(__A   ,     __A   ,     dtype=__A )]   ,     dim=1 )

           return ordered_inputs




    @property
    def 					a							(      self ):
           return 13







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

_UpperCAmelCase	:     Dict		       = {
    """salesforce/blip2-opt-2.7b""": """https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json""",
}







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				Optional[int]     =    '''blip_2_vision_model'''




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

											snake_case_           =					hidden_size
											snake_case_           =					intermediate_size
											snake_case_           =					num_hidden_layers
											snake_case_           =					num_attention_heads
											snake_case_           =					patch_size
											snake_case_           =					image_size
											snake_case_           =					initializer_range
											snake_case_           =					attention_dropout
											snake_case_           =					layer_norm_eps
											snake_case_           =					hidden_act
											snake_case_           =					qkv_bias




				@classmethod
				def 					a							(      cls   ,     snake_case   ,     **snake_case ):
											cls._set_token_in_kwargs(snake_case )

											snake_case_       ,					snake_case_           =					cls.get_config_dict(snake_case   ,     **snake_case )

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







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				Any     =    '''blip_2_qformer'''




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

											snake_case_           =					vocab_size
											snake_case_           =					hidden_size
											snake_case_           =					num_hidden_layers
											snake_case_           =					num_attention_heads
											snake_case_           =					hidden_act
											snake_case_           =					intermediate_size
											snake_case_           =					hidden_dropout_prob
											snake_case_           =					attention_probs_dropout_prob
											snake_case_           =					max_position_embeddings
											snake_case_           =					initializer_range
											snake_case_           =					layer_norm_eps
											snake_case_           =					position_embedding_type
											snake_case_           =					cross_attention_frequency
											snake_case_           =					encoder_hidden_size




				@classmethod
				def 					a							(      cls   ,     snake_case   ,     **snake_case ):
											cls._set_token_in_kwargs(snake_case )

											snake_case_       ,					snake_case_           =					cls.get_config_dict(snake_case   ,     **snake_case )

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







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				Optional[int]     =    '''blip-2'''
				__SCREAMING_SNAKE_CASE      :				Dict     =    True




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

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

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

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

											snake_case_           =					BlipaVisionConfig(**snake_case )
											snake_case_           =					BlipaQFormerConfig(**snake_case )
											snake_case_           =					text_config['model_type'] if 'model_type' in text_config else 'opt'
											snake_case_           =					CONFIG_MAPPING[text_model_type](**snake_case )

											snake_case_           =					self.text_config.tie_word_embeddings
											snake_case_           =					self.text_config.is_encoder_decoder

											snake_case_           =					num_query_tokens
											snake_case_           =					self.vision_config.hidden_size
											snake_case_           =					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
											snake_case_           =					1.0
											snake_case_           =					0.02




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




				def 					a							(      self ):
											snake_case_           =					copy.deepcopy(self.__dict__ )
											snake_case_           =					self.vision_config.to_dict()
											snake_case_           =					self.qformer_config.to_dict()
											snake_case_           =					self.text_config.to_dict()
											snake_case_           =					self.__class__.model_type
											return output




