


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


lowerCamelCase				:  Any    		=      logging.get_logger(__name__)

lowerCamelCase				:  Any    		=      {
    'salesforce/blip2-opt-2.7b': 'https://huggingface.co/salesforce/blip2-opt-2.7b/resolve/main/config.json',
}
class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      lowerCAmelCase__   :	int       =				"""blip_2_vision_model"""



      def __init__(self       :       int  ,		UpperCamelCase       :       Optional[Any]=1408  ,		UpperCamelCase       :       Tuple=6144  ,		UpperCamelCase       :       str=39  ,		UpperCamelCase       :       Any=16  ,		UpperCamelCase       :       Optional[Any]=224  ,		UpperCamelCase       :       Tuple=14  ,		UpperCamelCase       :       Optional[Any]="gelu"  ,		UpperCamelCase       :       Any=0.0_00_01  ,		UpperCamelCase       :       Optional[int]=0.0  ,		UpperCamelCase       :       Union[str, Any]=1E-10  ,		UpperCamelCase       :       Optional[Any]=True  ,		**UpperCamelCase       :       int  ,		):

             '''simple docstring'''







             super().__init__(**UpperCamelCase					)

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






      @classmethod
      def  UpperCamelCase__						(cls       :       List[str]  ,		UpperCamelCase       :       Union[str, os.PathLike]  ,		**UpperCamelCase       :       Dict					):

             '''simple docstring'''







             cls._set_token_in_kwargs(UpperCamelCase					)

             lowercase__     ,lowercase__      =					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":
                    lowercase__      =					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_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Any       =				"""blip_2_qformer"""



      def __init__(self       :       Tuple  ,		UpperCamelCase       :       Optional[Any]=30522  ,		UpperCamelCase       :       Any=768  ,		UpperCamelCase       :       str=12  ,		UpperCamelCase       :       Tuple=12  ,		UpperCamelCase       :       Tuple=3072  ,		UpperCamelCase       :       List[Any]="gelu"  ,		UpperCamelCase       :       Tuple=0.1  ,		UpperCamelCase       :       Optional[int]=0.1  ,		UpperCamelCase       :       int=512  ,		UpperCamelCase       :       int=0.02  ,		UpperCamelCase       :       Optional[int]=1E-12  ,		UpperCamelCase       :       Any=0  ,		UpperCamelCase       :       Union[str, Any]="absolute"  ,		UpperCamelCase       :       List[str]=2  ,		UpperCamelCase       :       Any=1408  ,		**UpperCamelCase       :       Any  ,		):

             '''simple docstring'''







             super().__init__(pad_token_id=UpperCamelCase  ,		**UpperCamelCase					)

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






      @classmethod
      def  UpperCamelCase__						(cls       :       Any  ,		UpperCamelCase       :       Union[str, os.PathLike]  ,		**UpperCamelCase       :       Optional[Any]					):

             '''simple docstring'''







             cls._set_token_in_kwargs(UpperCamelCase					)

             lowercase__     ,lowercase__      =					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":
                    lowercase__      =					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_	):





      '''simple docstring'''






      lowerCAmelCase__   :	Optional[Any]       =				"""blip-2"""
      lowerCAmelCase__   :	Optional[int]       =				True



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

             '''simple docstring'''







             super().__init__(**UpperCamelCase					)

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

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

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

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

             lowercase__      =					self.text_config.tie_word_embeddings
             lowercase__      =					self.text_config.is_encoder_decoder

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



      @classmethod
      def  UpperCamelCase__						(cls       :       Optional[Any]  ,		UpperCamelCase       :       BlipaVisionConfig  ,		UpperCamelCase       :       BlipaQFormerConfig  ,		UpperCamelCase       :       PretrainedConfig  ,		**UpperCamelCase       :       Optional[Any]  ,		):

             '''simple docstring'''







             return cls(
                 vision_config=vision_config.to_dict()  ,		qformer_config=qformer_config.to_dict()  ,		text_config=text_config.to_dict()  ,		**UpperCamelCase  ,		)






      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					copy.deepcopy(self.__dict__					)
             lowercase__      =					self.vision_config.to_dict()
             lowercase__      =					self.qformer_config.to_dict()
             lowercase__      =					self.text_config.to_dict()
             lowercase__      =					self.__class__.model_type
             return output








'''simple docstring'''

# 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     __lowerCAmelCase		(TensorFormatter[Mapping, """torch.Tensor""", Mapping]	):





      '''simple docstring'''






      def __init__(self       :       Optional[Any]  ,		UpperCamelCase       :       Tuple=None  ,		**UpperCamelCase       :       List[str]					):

             '''simple docstring'''







             super().__init__(features=UpperCamelCase					)
             lowercase__      =					torch_tensor_kwargs
             import torch  # noqa import torch at initialization



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       Union[str, Any]					):

             '''simple docstring'''







             import torch

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



      def  UpperCamelCase__						(self       :       List[Any]  ,		UpperCamelCase       :       Optional[Any]					):

             '''simple docstring'''







             import torch

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

             lowercase__      =					{}

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

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



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       Dict					):

             '''simple docstring'''







             import torch

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



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       dict					):

             '''simple docstring'''







             return map_nested(self._recursive_tensorize  ,		UpperCamelCase  ,		map_list=UpperCamelCase					)



      def  UpperCamelCase__						(self       :       Dict  ,		UpperCamelCase       :       pa.Table					):

             '''simple docstring'''







             lowercase__      =					self.numpy_arrow_extractor().extract_row(UpperCamelCase					)
             lowercase__      =					self.python_features_decoder.decode_row(UpperCamelCase					)
             return self.recursive_tensorize(UpperCamelCase					)



      def  UpperCamelCase__						(self       :       List[Any]  ,		UpperCamelCase       :       pa.Table					):

             '''simple docstring'''







             lowercase__      =					self.numpy_arrow_extractor().extract_column(UpperCamelCase					)
             lowercase__      =					self.python_features_decoder.decode_column(UpperCamelCase  ,		pa_table.column_names[0]					)
             lowercase__      =					self.recursive_tensorize(UpperCamelCase					)
             lowercase__      =					self._consolidate(UpperCamelCase					)
             return column






      def  UpperCamelCase__						(self       :       Tuple  ,		UpperCamelCase       :       pa.Table					):

             '''simple docstring'''







             lowercase__      =					self.numpy_arrow_extractor().extract_batch(UpperCamelCase					)
             lowercase__      =					self.python_features_decoder.decode_batch(UpperCamelCase					)
             lowercase__      =					self.recursive_tensorize(UpperCamelCase					)
             for column_name in batch:
                    lowercase__      =					self._consolidate(batch[column_name]					)
             return batch




