

import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available







class 				lowercase       ( lowercase_    ):
				__SCREAMING_SNAKE_CASE      :				Union[str, Any]     =    ['''image_processor''', '''tokenizer''']
				__SCREAMING_SNAKE_CASE      :				Optional[int]     =    '''OwlViTImageProcessor'''
				__SCREAMING_SNAKE_CASE      :				Dict     =    ('''CLIPTokenizer''', '''CLIPTokenizerFast''')




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

											snake_case_           =					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__(snake_case   ,     snake_case )




				def __call__(      self   ,     snake_case=None   ,     snake_case=None   ,     snake_case=None   ,     snake_case="max_length"   ,     snake_case="np"   ,     **snake_case ):

											if text is None and query_images is None and images is None:
																		raise ValueError(
																		    'You have to specify at least one text or query image or image. All three cannot be none.' )

											if text is not None:
																		if isinstance(snake_case   ,     snake_case ) or (isinstance(snake_case   ,     snake_case ) and not isinstance(text[0]   ,     snake_case )):
																									snake_case_           =					[self.tokenizer(snake_case   ,     padding=snake_case   ,     return_tensors=snake_case   ,     **snake_case )]

																		elif isinstance(snake_case   ,     snake_case ) and isinstance(text[0]   ,     snake_case ):
																									snake_case_           =					[]

																									# Maximum number of queries across batch
																									snake_case_           =					max([len(snake_case ) for t in text] )

																									# Pad all batch samples to max number of text queries
																									for t in text:
																																if len(snake_case ) != max_num_queries:
																																							snake_case_           =					t + [' '] * (max_num_queries - len(snake_case ))

																																snake_case_           =					self.tokenizer(snake_case   ,     padding=snake_case   ,     return_tensors=snake_case   ,     **snake_case )
																																encodings.append(snake_case )
																		else:
																									raise TypeError('Input text should be a string, a list of strings or a nested list of strings' )

																		if return_tensors == "np":
																									snake_case_           =					np.concatenate([encoding['input_ids'] for encoding in encodings]   ,     axis=0 )
																									snake_case_           =					np.concatenate([encoding['attention_mask'] for encoding in encodings]   ,     axis=0 )

																		elif return_tensors == "jax" and is_flax_available():
																									import jax.numpy as jnp

																									snake_case_           =					jnp.concatenate([encoding['input_ids'] for encoding in encodings]   ,     axis=0 )
																									snake_case_           =					jnp.concatenate([encoding['attention_mask'] for encoding in encodings]   ,     axis=0 )

																		elif return_tensors == "pt" and is_torch_available():
																									import torch

																									snake_case_           =					torch.cat([encoding['input_ids'] for encoding in encodings]   ,     dim=0 )
																									snake_case_           =					torch.cat([encoding['attention_mask'] for encoding in encodings]   ,     dim=0 )

																		elif return_tensors == "tf" and is_tf_available():
																									import tensorflow as tf

																									snake_case_           =					tf.stack([encoding['input_ids'] for encoding in encodings]   ,     axis=0 )
																									snake_case_           =					tf.stack([encoding['attention_mask'] for encoding in encodings]   ,     axis=0 )

																		else:
																									raise ValueError('Target return tensor type could not be returned' )

																		snake_case_           =					BatchEncoding()
																		snake_case_           =					input_ids
																		snake_case_           =					attention_mask

											if query_images is not None:
																		snake_case_           =					BatchEncoding()
																		snake_case_           =					self.image_processor(
																		    snake_case   ,     return_tensors=snake_case   ,     **snake_case ).pixel_values
																		snake_case_           =					query_pixel_values

											if images is not None:
																		snake_case_           =					self.image_processor(snake_case   ,     return_tensors=snake_case   ,     **snake_case )

											if text is not None and images is not None:
																		snake_case_           =					image_features.pixel_values
																		return encoding
											elif query_images is not None and images is not None:
																		snake_case_           =					image_features.pixel_values
																		return encoding
											elif text is not None or query_images is not None:
																		return encoding
											else:
																		return BatchEncoding(data=dict(**snake_case )   ,     tensor_type=snake_case )




				def 					a							(      self   ,     *snake_case   ,     **snake_case ):
											return self.image_processor.post_process(*snake_case   ,     **snake_case )




				def 					a							(      self   ,     *snake_case   ,     **snake_case ):
											return self.image_processor.post_process_object_detection(*snake_case   ,     **snake_case )




				def 					a							(      self   ,     *snake_case   ,     **snake_case ):
											return self.image_processor.post_process_image_guided_detection(*snake_case   ,     **snake_case )




				def 					a							(      self   ,     *snake_case   ,     **snake_case ):
											return self.tokenizer.batch_decode(*snake_case   ,     **snake_case )




				def 					a							(      self   ,     *snake_case   ,     **snake_case ):
											return self.tokenizer.decode(*snake_case   ,     **snake_case )




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




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







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




