




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


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)

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






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Optional[int]	      =					"blip_2_vision_model"


				def __init__(							self:   List[str]		,					_lowerCamelCase:   Optional[int]=14_08		,					_lowerCamelCase:   Any=61_44		,					_lowerCamelCase:   Optional[Any]=39		,					_lowerCamelCase:   str=16		,					_lowerCamelCase:   Union[str, Any]=2_24		,					_lowerCamelCase:   List[str]=14		,					_lowerCamelCase:   str="gelu"		,					_lowerCamelCase:   Optional[Any]=0.0_00_01		,					_lowerCamelCase:   int=0.0		,					_lowerCamelCase:   Optional[int]=1E-10		,					_lowerCamelCase:   Any=True		,					**_lowerCamelCase:   Tuple		,					):
											super().__init__(**_lowerCamelCase		)

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





				@classmethod
				def     _A							(							cls:   Dict		,					_lowerCamelCase:   Union[str, os.PathLike]		,					**_lowerCamelCase:   Tuple		):
											cls._set_token_in_kwargs(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    cls.get_config_dict(_lowerCamelCase		,					**_lowerCamelCase		)

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






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							str	      =					"blip_2_qformer"


				def __init__(							self:   Dict		,					_lowerCamelCase:   Optional[int]=3_05_22		,					_lowerCamelCase:   Any=7_68		,					_lowerCamelCase:   List[str]=12		,					_lowerCamelCase:   str=12		,					_lowerCamelCase:   str=30_72		,					_lowerCamelCase:   List[str]="gelu"		,					_lowerCamelCase:   Optional[int]=0.1		,					_lowerCamelCase:   Union[str, Any]=0.1		,					_lowerCamelCase:   List[Any]=5_12		,					_lowerCamelCase:   Union[str, Any]=0.02		,					_lowerCamelCase:   Union[str, Any]=1E-12		,					_lowerCamelCase:   Union[str, Any]=0		,					_lowerCamelCase:   str="absolute"		,					_lowerCamelCase:   int=2		,					_lowerCamelCase:   List[str]=14_08		,					**_lowerCamelCase:   List[str]		,					):
											super().__init__(pad_token_id=_lowerCamelCase		,					**_lowerCamelCase		)

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





				@classmethod
				def     _A							(							cls:   Optional[int]		,					_lowerCamelCase:   Union[str, os.PathLike]		,					**_lowerCamelCase:   Optional[Any]		):
											cls._set_token_in_kwargs(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    cls.get_config_dict(_lowerCamelCase		,					**_lowerCamelCase		)

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






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							int	      =					"blip-2"
				SCREAMING_SNAKE_CASE__					:							List[str]	      =					True


				def __init__(							self:   List[str]		,					_lowerCamelCase:   Dict=None		,					_lowerCamelCase:   Any=None		,					_lowerCamelCase:   Any=None		,					_lowerCamelCase:   List[str]=32		,					**_lowerCamelCase:   Tuple		):
											super().__init__(**_lowerCamelCase		)

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

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

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

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

											SCREAMING_SNAKE_CASE_   	=    self.text_config.tie_word_embeddings
											SCREAMING_SNAKE_CASE_   	=    self.text_config.is_encoder_decoder

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


				@classmethod
				def     _A							(							cls:   List[str]		,					_lowerCamelCase:   BlipaVisionConfig		,					_lowerCamelCase:   BlipaQFormerConfig		,					_lowerCamelCase:   PretrainedConfig		,					**_lowerCamelCase:   str		,					):
											return cls(
											    vision_config=vision_config.to_dict()		,					qformer_config=qformer_config.to_dict()		,					text_config=text_config.to_dict()		,					**_lowerCamelCase		,					)





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








# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
			import torch






class  __magic_name__						(  TensorFormatter[Mapping, "torch.Tensor", Mapping]):
				'''simple docstring'''







				def __init__(							self:   int		,					_lowerCamelCase:   int=None		,					**_lowerCamelCase:   List[Any]		):
											super().__init__(features=_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    torch_tensor_kwargs
											import torch  # noqa import torch at initialization


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Optional[Any]		):
											import torch

											if isinstance(_lowerCamelCase		,					_lowerCamelCase		) and column:
																		if all(
																		    isinstance(_lowerCamelCase		,					torch.Tensor		) and x.shape == column[0].shape and x.dtype == column[0].dtype
																		    for x in column		):
																									return torch.stack(_lowerCamelCase		)
											return column


				def     _A							(							self:   Tuple		,					_lowerCamelCase:   List[str]		):
											import torch

											if isinstance(_lowerCamelCase		,					(str, bytes, type(_lowerCamelCase		))		):
																		return value
											elif isinstance(_lowerCamelCase		,					(np.character, np.ndarray)		) and np.issubdtype(value.dtype		,					np.character		):
																		return value.tolist()

											SCREAMING_SNAKE_CASE_   	=    {}

											if isinstance(_lowerCamelCase		,					(np.number, np.ndarray)		) and np.issubdtype(value.dtype		,					np.integer		):
																		SCREAMING_SNAKE_CASE_   	=    {'''dtype''': torch.intaa}
											elif isinstance(_lowerCamelCase		,					(np.number, np.ndarray)		) and np.issubdtype(value.dtype		,					np.floating		):
																		SCREAMING_SNAKE_CASE_   	=    {'''dtype''': torch.floataa}
											elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																		import PIL.Image

																		if isinstance(_lowerCamelCase		,					PIL.Image.Image		):
																									SCREAMING_SNAKE_CASE_   	=    np.asarray(_lowerCamelCase		)
											return torch.tensor(_lowerCamelCase		,					**{**default_dtype, **self.torch_tensor_kwargs}		)


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Dict		):
											import torch

											# support for torch, tf, jax etc.
											if hasattr(_lowerCamelCase		,					'''__array__'''		) and not isinstance(_lowerCamelCase		,					torch.Tensor		):
																		SCREAMING_SNAKE_CASE_   	=    data_struct.__array__()
											# support for nested types like struct of list of struct
											if isinstance(_lowerCamelCase		,					np.ndarray		):
																		if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
																									return self._consolidate([self.recursive_tensorize(_lowerCamelCase		) for substruct in data_struct]		)
											elif isinstance(_lowerCamelCase		,					(list, tuple)		):
																		return self._consolidate([self.recursive_tensorize(_lowerCamelCase		) for substruct in data_struct]		)
											return self._tensorize(_lowerCamelCase		)


				def     _A							(							self:   str		,					_lowerCamelCase:   dict		):
											return map_nested(self._recursive_tensorize		,					_lowerCamelCase		,					map_list=_lowerCamelCase		)


				def     _A							(							self:   Optional[int]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_row(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_row(_lowerCamelCase		)
											return self.recursive_tensorize(_lowerCamelCase		)


				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_column(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_column(_lowerCamelCase		,					pa_table.column_names[0]		)
											SCREAMING_SNAKE_CASE_   	=    self.recursive_tensorize(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self._consolidate(_lowerCamelCase		)
											return column





				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_batch(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_batch(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.recursive_tensorize(_lowerCamelCase		)
											for column_name in batch:
																		SCREAMING_SNAKE_CASE_   	=    self._consolidate(batch[column_name]		)
											return batch


