


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


_UpperCamelCase 							=  logging.get_logger(__name__)

_UpperCamelCase 							=  {
    """Salesforce/instruct-blip-flan-t5""": """https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json""",
}







class 							__magic_name__	(       lowerCAmelCase ):

     """simple docstring"""






     lowerCamelCase__       						=				'instructblip_vision_model'


     def __init__(   self			,     lowerCamelCase=1408			,     lowerCamelCase=6144			,     lowerCamelCase=39			,     lowerCamelCase=16			,     lowerCamelCase=224			,     lowerCamelCase=14			,     lowerCamelCase="gelu"			,     lowerCamelCase=1E-6			,     lowerCamelCase=0.0			,     lowerCamelCase=1E-10			,     lowerCamelCase=True			,     **lowerCamelCase			,     ):


          '''simple docstring'''





          super().__init__(**lowerCamelCase		)

          __A	:       Optional[Any]      =							hidden_size
          __A	:       int      =							intermediate_size
          __A	:       Optional[Any]      =							num_hidden_layers
          __A	:       Optional[Any]      =							num_attention_heads
          __A	:       Tuple      =							patch_size
          __A	:       Optional[int]      =							image_size
          __A	:       int      =							initializer_range
          __A	:       Tuple      =							attention_dropout
          __A	:       Tuple      =							layer_norm_eps
          __A	:       int      =							hidden_act
          __A	:       Tuple      =							qkv_bias


     @classmethod
     def lowerCAmelCase__  (   cls			,     lowerCamelCase			,     **lowerCamelCase		):


          '''simple docstring'''





          cls._set_token_in_kwargs(lowerCamelCase		)

          __A  ,__A	:       Dict      =							cls.get_config_dict(lowerCamelCase			,     **lowerCamelCase		)

          # get the vision config dict if we are loading from InstructBlipConfig
          if config_dict.get("model_type"		) == "instructblip":
               __A	:       Optional[Any]      =							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__	(       lowerCAmelCase ):

     """simple docstring"""






     lowerCamelCase__       						=				'instructblip_qformer'


     def __init__(   self			,     lowerCamelCase=3_0522			,     lowerCamelCase=768			,     lowerCamelCase=12			,     lowerCamelCase=12			,     lowerCamelCase=3072			,     lowerCamelCase="gelu"			,     lowerCamelCase=0.1			,     lowerCamelCase=0.1			,     lowerCamelCase=512			,     lowerCamelCase=0.02			,     lowerCamelCase=1E-12			,     lowerCamelCase=0			,     lowerCamelCase="absolute"			,     lowerCamelCase=2			,     lowerCamelCase=1408			,     **lowerCamelCase			,     ):


          '''simple docstring'''





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

          __A	:       Union[str, Any]      =							vocab_size
          __A	:       str      =							hidden_size
          __A	:       List[Any]      =							num_hidden_layers
          __A	:       Optional[int]      =							num_attention_heads
          __A	:       int      =							hidden_act
          __A	:       Optional[int]      =							intermediate_size
          __A	:       Any      =							hidden_dropout_prob
          __A	:       List[Any]      =							attention_probs_dropout_prob
          __A	:       Optional[int]      =							max_position_embeddings
          __A	:       Tuple      =							initializer_range
          __A	:       Optional[Any]      =							layer_norm_eps
          __A	:       List[Any]      =							position_embedding_type
          __A	:       List[Any]      =							cross_attention_frequency
          __A	:       Optional[int]      =							encoder_hidden_size


     @classmethod
     def lowerCAmelCase__  (   cls			,     lowerCamelCase			,     **lowerCamelCase		):


          '''simple docstring'''





          cls._set_token_in_kwargs(lowerCamelCase		)

          __A  ,__A	:       Union[str, Any]      =							cls.get_config_dict(lowerCamelCase			,     **lowerCamelCase		)

          # get the qformer config dict if we are loading from InstructBlipConfig
          if config_dict.get("model_type"		) == "instructblip":
               __A	:       int      =							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__	(       lowerCAmelCase ):

     """simple docstring"""






     lowerCamelCase__       						=				'instructblip'
     lowerCamelCase__       						=				True


     def __init__(   self			,     lowerCamelCase=None			,     lowerCamelCase=None			,     lowerCamelCase=None			,     lowerCamelCase=32			,     **lowerCamelCase		):


          '''simple docstring'''





          super().__init__(**lowerCamelCase		)

          if vision_config is None:
               __A	:       Any      =							{}
               logger.info("vision_config is None. initializing the InstructBlipVisionConfig with default values."		)

          if qformer_config is None:
               __A	:       List[str]      =							{}
               logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."		)

          if text_config is None:
               __A	:       Union[str, Any]      =							{}
               logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`)."		)

          __A	:       Any      =							InstructBlipVisionConfig(**lowerCamelCase		)
          __A	:       List[str]      =							InstructBlipQFormerConfig(**lowerCamelCase		)
          __A	:       Union[str, Any]      =							text_config["model_type"] if "model_type" in text_config else "opt"
          __A	:       str      =							CONFIG_MAPPING[text_model_type](**lowerCamelCase		)

          __A	:       Any      =							self.text_config.tie_word_embeddings
          __A	:       Optional[Any]      =							self.text_config.is_encoder_decoder

          __A	:       str      =							num_query_tokens
          __A	:       Dict      =							self.vision_config.hidden_size
          __A	:       List[Any]      =							self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
          __A	:       List[str]      =							1.0
          __A	:       str      =							0.02


     @classmethod
     def lowerCAmelCase__  (   cls			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase			,     **lowerCamelCase			,     ):


          '''simple docstring'''





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


     def lowerCAmelCase__  (   self		):


          '''simple docstring'''





          __A	:       Dict      =							copy.deepcopy(self.__dict__		)
          __A	:       int      =							self.vision_config.to_dict()
          __A	:       Optional[Any]      =							self.qformer_config.to_dict()
          __A	:       Dict      =							self.text_config.to_dict()
          __A	:       Dict      =							self.__class__.model_type
          return output





'''simple docstring'''


from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import rescale, resize, to_channel_dimension_format
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


_UpperCamelCase 							=  logging.get_logger(__name__)


def 		_lowercase (SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE							):




     '''simple docstring'''





     __A	:       int      =							b.T
     __A	:       Optional[Any]      =							np.sum(np.square(SCREAMING_SNAKE_CASE							)       ,					axis=1							)
     __A	:       List[Any]      =							np.sum(np.square(SCREAMING_SNAKE_CASE							)       ,					axis=0							)
     __A	:       List[Any]      =							np.matmul(SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE							)
     __A	:       str      =							aa[:, None] - 2 * ab + ba[None, :]
     return d


def 		_lowercase (SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE							):




     '''simple docstring'''





     __A	:       Union[str, Any]      =							x.reshape(-1       ,					3							)
     __A	:       Any      =							squared_euclidean_distance(SCREAMING_SNAKE_CASE       ,					SCREAMING_SNAKE_CASE							)
     return np.argmin(SCREAMING_SNAKE_CASE       ,					axis=1							)







class 							__magic_name__	(       lowerCAmelCase ):

     """simple docstring"""






     lowerCamelCase__       						=				['pixel_values']


     def __init__(   self			,     lowerCamelCase = None			,     lowerCamelCase = True			,     lowerCamelCase = None			,     lowerCamelCase = PILImageResampling.BILINEAR			,     lowerCamelCase = True			,     lowerCamelCase = True			,     **lowerCamelCase			,     ):


          '''simple docstring'''





          super().__init__(**lowerCamelCase		)
          __A	:       Optional[int]      =							size if size is not None else {"height": 256, "width": 256}
          __A	:       int      =							get_size_dict(lowerCamelCase		)
          __A	:       str      =							np.array(lowerCamelCase		) if clusters is not None else None
          __A	:       List[Any]      =							do_resize
          __A	:       Dict      =							size
          __A	:       str      =							resample
          __A	:       Optional[Any]      =							do_normalize
          __A	:       Dict      =							do_color_quantize


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase			,     lowerCamelCase = PILImageResampling.BILINEAR			,     lowerCamelCase = None			,     **lowerCamelCase			,     ):


          '''simple docstring'''





          __A	:       Optional[Any]      =							get_size_dict(lowerCamelCase		)
          if "height" not in size or "width" not in size:
               raise ValueError(f"Size dictionary must contain both height and width keys. Got {size.keys()}"		)
          return resize(
              lowerCamelCase			,     size=(size["height"], size["width"])			,     resample=lowerCamelCase			,     data_format=lowerCamelCase			,     **lowerCamelCase		)


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase = None			,     ):


          '''simple docstring'''





          __A	:       List[str]      =							rescale(image=lowerCamelCase			,     scale=1 / 127.5			,     data_format=lowerCamelCase		)
          __A	:       List[Any]      =							image - 1
          return image


     def lowerCAmelCase__  (   self			,     lowerCamelCase			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = None			,     lowerCamelCase = ChannelDimension.FIRST			,     **lowerCamelCase			,     ):


          '''simple docstring'''





          __A	:       List[Any]      =							do_resize if do_resize is not None else self.do_resize
          __A	:       int      =							size if size is not None else self.size
          __A	:       Tuple      =							get_size_dict(lowerCamelCase		)
          __A	:       Tuple      =							resample if resample is not None else self.resample
          __A	:       Union[str, Any]      =							do_normalize if do_normalize is not None else self.do_normalize
          __A	:       Tuple      =							do_color_quantize if do_color_quantize is not None else self.do_color_quantize
          __A	:       Any      =							clusters if clusters is not None else self.clusters
          __A	:       Dict      =							np.array(lowerCamelCase		)

          __A	:       Optional[int]      =							make_list_of_images(lowerCamelCase		)

          if not valid_images(lowerCamelCase		):
               raise ValueError(
                   "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                   "torch.Tensor, tf.Tensor or jax.ndarray."		)

          if do_resize and size is None or resample is None:
               raise ValueError("Size and resample must be specified if do_resize is True."		)

          if do_color_quantize and clusters is None:
               raise ValueError("Clusters must be specified if do_color_quantize is True."		)

          # All transformations expect numpy arrays.
          __A	:       Optional[Any]      =							[to_numpy_array(lowerCamelCase		) for image in images]

          if do_resize:
               __A	:       Dict      =							[self.resize(image=lowerCamelCase			,     size=lowerCamelCase			,     resample=lowerCamelCase		) for image in images]

          if do_normalize:
               __A	:       List[Any]      =							[self.normalize(image=lowerCamelCase		) for image in images]

          if do_color_quantize:
               __A	:       Union[str, Any]      =							[to_channel_dimension_format(lowerCamelCase			,     ChannelDimension.LAST		) for image in images]
               # color quantize from (batch_size, height, width, 3) to (batch_size, height, width)
               __A	:       int      =							np.array(lowerCamelCase		)
               __A	:       Any      =							color_quantize(lowerCamelCase			,     lowerCamelCase		).reshape(images.shape[:-1]		)

               # flatten to (batch_size, height*width)
               __A	:       Tuple      =							images.shape[0]
               __A	:       List[Any]      =							images.reshape(lowerCamelCase			,     -1		)

               # We need to convert back to a list of images to keep consistent behaviour across processors.
               __A	:       Optional[int]      =							list(lowerCamelCase		)
          else:
               __A	:       Tuple      =							[to_channel_dimension_format(lowerCamelCase			,     lowerCamelCase		) for image in images]

          __A	:       Optional[int]      =							{"input_ids": images}
          return BatchFeature(data=lowerCamelCase			,     tensor_type=lowerCamelCase		)

