




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


a							      =     logging.get_logger(__name__)

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





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	List[str]     =      '''blip_2_vision_model'''

 def __init__(					self			:			Dict       ,				_UpperCAmelCase			:			str=1_408       ,				_UpperCAmelCase			:			List[str]=6_144       ,				_UpperCAmelCase			:			Optional[int]=39       ,				_UpperCAmelCase			:			List[Any]=16       ,				_UpperCAmelCase			:			List[Any]=224       ,				_UpperCAmelCase			:			Optional[int]=14       ,				_UpperCAmelCase			:			Optional[int]="gelu"       ,				_UpperCAmelCase			:			Dict=0.0_0001       ,				_UpperCAmelCase			:			List[str]=0.0       ,				_UpperCAmelCase			:			int=1E-1_0       ,				_UpperCAmelCase			:			int=True       ,				**_UpperCAmelCase			:			List[str]       ,				):
       super().__init__(**_UpperCAmelCase						)

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



 @classmethod
 def      lowerCAmelCase_				(					cls			:			int       ,				_UpperCAmelCase			:			Union[str, os.PathLike]       ,				**_UpperCAmelCase			:			List[str]						):
       cls._set_token_in_kwargs(_UpperCAmelCase						)

       _A  ,       _A     =  cls.get_config_dict(_UpperCAmelCase       ,				**_UpperCAmelCase						)

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



 '''simple docstring'''
 UpperCAmelCase       :	Dict     =      '''blip_2_qformer'''

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

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



 @classmethod
 def      lowerCAmelCase_				(					cls			:			Union[str, Any]       ,				_UpperCAmelCase			:			Union[str, os.PathLike]       ,				**_UpperCAmelCase			:			Tuple						):
       cls._set_token_in_kwargs(_UpperCAmelCase						)

       _A  ,       _A     =  cls.get_config_dict(_UpperCAmelCase       ,				**_UpperCAmelCase						)

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



 '''simple docstring'''
 UpperCAmelCase       :	List[Any]     =      '''blip-2'''
 UpperCAmelCase       :	str     =      True

 def __init__(					self			:			Tuple       ,				_UpperCAmelCase			:			int=None       ,				_UpperCAmelCase			:			Union[str, Any]=None       ,				_UpperCAmelCase			:			Optional[int]=None       ,				_UpperCAmelCase			:			Any=32       ,				**_UpperCAmelCase			:			List[str]						):
       super().__init__(**_UpperCAmelCase						)

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

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

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

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

       _A     =  self.text_config.tie_word_embeddings
       _A     =  self.text_config.is_encoder_decoder

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

 @classmethod
 def      lowerCAmelCase_				(					cls			:			Optional[int]       ,				_UpperCAmelCase			:			BlipaVisionConfig       ,				_UpperCAmelCase			:			BlipaQFormerConfig       ,				_UpperCAmelCase			:			PretrainedConfig       ,				**_UpperCAmelCase			:			Union[str, Any]       ,				):
       return cls(
           vision_config=vision_config.to_dict()       ,				qformer_config=qformer_config.to_dict()       ,				text_config=text_config.to_dict()       ,				**_UpperCAmelCase       ,				)



 def      lowerCAmelCase_				(					self			:			Any						):
       _A     =  copy.deepcopy(self.__dict__						)
       _A     =  self.vision_config.to_dict()
       _A     =  self.qformer_config.to_dict()
       _A     =  self.text_config.to_dict()
       _A     =  self.__class__.model_type
       return output







"""simple docstring"""

import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding





class 							lowercase_			(	__lowerCAmelCase				):



 '''simple docstring'''
 UpperCAmelCase       :	Any     =      ['''image_processor''', '''tokenizer''']
 UpperCAmelCase       :	Optional[int]     =      '''ViTImageProcessor'''
 UpperCAmelCase       :	int     =      ('''CLIPTokenizer''', '''CLIPTokenizerFast''')

 def __init__(					self			:			Tuple       ,				_UpperCAmelCase			:			int=None       ,				_UpperCAmelCase			:			Tuple=None       ,				**_UpperCAmelCase			:			Dict						):
       _A     =  None
       if "feature_extractor" in kwargs:
             warnings.warn(
                 'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                 ' instead.'       ,				_UpperCAmelCase       ,				)
             _A     =  kwargs.pop('feature_extractor'						)

       _A     =  image_processor if image_processor is not None else feature_extractor
       if image_processor is None:
             raise ValueError('You need to specify an `image_processor`.'						)
       if tokenizer is None:
             raise ValueError('You need to specify a `tokenizer`.'						)

       super().__init__(_UpperCAmelCase       ,				_UpperCAmelCase						)

 def __call__(					self			:			Optional[Any]       ,				_UpperCAmelCase			:			int=None       ,				_UpperCAmelCase			:			int=None       ,				_UpperCAmelCase			:			Optional[Any]=None       ,				_UpperCAmelCase			:			Optional[int]=None       ,				**_UpperCAmelCase			:			Union[str, Any]						):
       if text is None and visual_prompt is None and images is None:
             raise ValueError('You have to specify either text, visual prompt or images.'						)

       if text is not None and visual_prompt is not None:
             raise ValueError('You have to specify exactly one type of prompt. Either text or visual prompt.'						)

       if text is not None:
             _A     =  self.tokenizer(_UpperCAmelCase       ,				return_tensors=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       if visual_prompt is not None:
             _A     =  self.image_processor(_UpperCAmelCase       ,				return_tensors=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       if images is not None:
             _A     =  self.image_processor(_UpperCAmelCase       ,				return_tensors=_UpperCAmelCase       ,				**_UpperCAmelCase						)

       if visual_prompt is not None and images is not None:
             _A     =  {
                 'pixel_values': image_features.pixel_values,
                 'conditional_pixel_values': prompt_features.pixel_values,
             }
             return encoding
       elif text is not None and images is not None:
             _A     =  image_features.pixel_values
             return encoding
       elif text is not None:
             return encoding
       elif visual_prompt is not None:
             _A     =  {
                 'conditional_pixel_values': prompt_features.pixel_values,
             }
             return encoding
       else:
             return BatchEncoding(data=dict(**_UpperCAmelCase						)       ,				tensor_type=_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			str       ,				*_UpperCAmelCase			:			List[Any]       ,				**_UpperCAmelCase			:			Union[str, Any]						):
       return self.tokenizer.batch_decode(*_UpperCAmelCase       ,				**_UpperCAmelCase						)

 def      lowerCAmelCase_				(					self			:			Union[str, Any]       ,				*_UpperCAmelCase			:			List[str]       ,				**_UpperCAmelCase			:			Union[str, Any]						):
       return self.tokenizer.decode(*_UpperCAmelCase       ,				**_UpperCAmelCase						)

 @property
 def      lowerCAmelCase_				(					self			:			Dict						):
       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'       ,				_UpperCAmelCase       ,				)
       return self.image_processor_class



 @property
 def      lowerCAmelCase_				(					self			:			Tuple						):
       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'       ,				_UpperCAmelCase       ,				)
       return self.image_processor

