




"""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      a__				(	__magic_name__   ):
			lowercase_    =	"blip_2_vision_model"


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


						"""simple docstring"""




						super().__init__(**UpperCamelCase_)

						__UpperCAmelCase	: Any		=					hidden_size
						__UpperCAmelCase	: Dict		=					intermediate_size
						__UpperCAmelCase	: Any		=					num_hidden_layers
						__UpperCAmelCase	: Union[str, Any]		=					num_attention_heads
						__UpperCAmelCase	: Union[str, Any]		=					patch_size
						__UpperCAmelCase	: int		=					image_size
						__UpperCAmelCase	: List[Any]		=					initializer_range
						__UpperCAmelCase	: Optional[int]		=					attention_dropout
						__UpperCAmelCase	: Optional[Any]		=					layer_norm_eps
						__UpperCAmelCase	: Union[str, Any]		=					hidden_act
						__UpperCAmelCase	: Union[str, Any]		=					qkv_bias


			@classmethod
			def       a_     (      cls      :    Tuple      ,  UpperCamelCase_      :    Union[str, os.PathLike]      ,  **UpperCamelCase_      :    Union[str, Any]):


						"""simple docstring"""




						cls._set_token_in_kwargs(UpperCamelCase_)

						__UpperCAmelCase	,     __UpperCAmelCase	: int		=					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":
									__UpperCAmelCase	: Dict		=					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      a__				(	__magic_name__   ):
			lowercase_    =	"blip_2_qformer"


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


						"""simple docstring"""




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

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


			@classmethod
			def       a_     (      cls      :    Optional[int]      ,  UpperCamelCase_      :    Union[str, os.PathLike]      ,  **UpperCamelCase_      :    Tuple):


						"""simple docstring"""




						cls._set_token_in_kwargs(UpperCamelCase_)

						__UpperCAmelCase	,     __UpperCAmelCase	: Optional[Any]		=					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":
									__UpperCAmelCase	: List[str]		=					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      a__				(	__magic_name__   ):
			lowercase_    =	"blip-2"
			lowercase_    =	True


			def __init__(      self      :    Dict      ,  UpperCamelCase_      :    Dict=None      ,  UpperCamelCase_      :    List[str]=None      ,  UpperCamelCase_      :    int=None      ,  UpperCamelCase_      :    Optional[Any]=32      ,  **UpperCamelCase_      :    str):


						"""simple docstring"""




						super().__init__(**UpperCamelCase_)

						if vision_config is None:
									__UpperCAmelCase	: Tuple		=					{}
									logger.info("vision_config is None. initializing the Blip2VisionConfig with default values.")

						if qformer_config is None:
									__UpperCAmelCase	: Optional[Any]		=					{}
									logger.info("qformer_config is None. Initializing the Blip2QFormerConfig with default values.")

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

						__UpperCAmelCase	: str		=					BlipaVisionConfig(**UpperCamelCase_)
						__UpperCAmelCase	: List[Any]		=					BlipaQFormerConfig(**UpperCamelCase_)
						__UpperCAmelCase	: List[str]		=					text_config["model_type"] if "model_type" in text_config else "opt"
						__UpperCAmelCase	: Union[str, Any]		=					CONFIG_MAPPING[text_model_type](**UpperCamelCase_)

						__UpperCAmelCase	: int		=					self.text_config.tie_word_embeddings
						__UpperCAmelCase	: Tuple		=					self.text_config.is_encoder_decoder

						__UpperCAmelCase	: int		=					num_query_tokens
						__UpperCAmelCase	: Optional[Any]		=					self.vision_config.hidden_size
						__UpperCAmelCase	: Tuple		=					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
						__UpperCAmelCase	: int		=					1.0
						__UpperCAmelCase	: List[str]		=					0.02


			@classmethod
			def       a_     (      cls      :    Union[str, Any]      ,  UpperCamelCase_      :    BlipaVisionConfig      ,  UpperCamelCase_      :    BlipaQFormerConfig      ,  UpperCamelCase_      :    PretrainedConfig      ,  **UpperCamelCase_      :    List[str]      ,  ):


						"""simple docstring"""




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


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					copy.deepcopy(self.__dict__)
						__UpperCAmelCase	: Tuple		=					self.vision_config.to_dict()
						__UpperCAmelCase	: Dict		=					self.qformer_config.to_dict()
						__UpperCAmelCase	: Union[str, Any]		=					self.text_config.to_dict()
						__UpperCAmelCase	: 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 (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    is_valid_image,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
					import PIL


A   =       logging.get_logger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			List[List[ImageInput]]:


			"""simple docstring"""
			if isinstance(UpperCamelCase				,       (list, tuple)							) and isinstance(videos[0]				,       (list, tuple)							) and is_valid_image(videos[0][0]							):
						return videos

			elif isinstance(UpperCamelCase				,       (list, tuple)							) and is_valid_image(videos[0]							):
						return [videos]

			elif is_valid_image(UpperCamelCase							):
						return [[videos]]

			raise ValueError(f"Could not make batched video from {videos}"							)





class      a__				(	__magic_name__   ):
			lowercase_    =	["pixel_values"]


			def __init__(      self      :    Tuple      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Union[int, float] = 1 / 255      ,  UpperCamelCase_      :    bool = True      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  **UpperCamelCase_      :    List[str]      ,  ):


						"""simple docstring"""




						super().__init__(**UpperCamelCase_)
						__UpperCAmelCase	: Dict		=					size if size is not None else {"shortest_edge": 224}
						__UpperCAmelCase	: Union[str, Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Optional[int]		=					crop_size if crop_size is not None else {"height": 224, "width": 224}
						__UpperCAmelCase	: List[str]		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

						__UpperCAmelCase	: Optional[Any]		=					do_resize
						__UpperCAmelCase	: Union[str, Any]		=					size
						__UpperCAmelCase	: str		=					do_center_crop
						__UpperCAmelCase	: str		=					crop_size
						__UpperCAmelCase	: Any		=					resample
						__UpperCAmelCase	: List[Any]		=					do_rescale
						__UpperCAmelCase	: Union[str, Any]		=					rescale_factor
						__UpperCAmelCase	: List[Any]		=					do_normalize
						__UpperCAmelCase	: Dict		=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
						__UpperCAmelCase	: Tuple		=					image_std if image_std is not None else IMAGENET_STANDARD_STD


			def       a_     (      self      :    int      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    PILImageResampling = PILImageResampling.BILINEAR      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    List[Any]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						if "shortest_edge" in size:
									__UpperCAmelCase	: Dict		=					get_resize_output_image_size(UpperCamelCase_      ,  size["shortest_edge"]      ,  default_to_square=UpperCamelCase_)
						elif "height" in size and "width" in size:
									__UpperCAmelCase	: Dict		=					(size["height"], size["width"])
						else:
									raise ValueError(F"Size must have 'height' and 'width' or 'shortest_edge' as keys. Got {size.keys()}")
						return resize(UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Dict[str, int]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Tuple      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_)
						if "height" not in size or "width" not in size:
									raise ValueError(F"Size must have 'height' and 'width' as keys. Got {size.keys()}")
						return center_crop(UpperCamelCase_      ,  size=(size["height"], size["width"])      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[int, float]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						return rescale(UpperCamelCase_      ,  scale=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    np.ndarray      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Union[float, List[float]]      ,  UpperCamelCase_      :    Optional[Union[str, ChannelDimension]] = None      ,  **UpperCamelCase_      :    Union[str, Any]      ,  ):


						"""simple docstring"""




						return normalize(UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  **UpperCamelCase_)


			def       a_     (      self      :    List[str]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[ChannelDimension] = ChannelDimension.FIRST      ,  ):


						"""simple docstring"""




						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_center_crop and crop_size is None:
									raise ValueError("Crop size must be specified if do_center_crop is True.")

						if do_rescale and rescale_factor is None:
									raise ValueError("Rescale factor must be specified if do_rescale is True.")

						if do_normalize and (image_mean is None or image_std is None):
									raise ValueError("Image mean and std must be specified if do_normalize is True.")

						# All transformations expect numpy arrays.
						__UpperCAmelCase	: List[str]		=					to_numpy_array(UpperCamelCase_)

						if do_resize:
									__UpperCAmelCase	: Tuple		=					self.resize(image=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_)

						if do_center_crop:
									__UpperCAmelCase	: str		=					self.center_crop(UpperCamelCase_      ,  size=UpperCamelCase_)

						if do_rescale:
									__UpperCAmelCase	: Union[str, Any]		=					self.rescale(image=UpperCamelCase_      ,  scale=UpperCamelCase_)

						if do_normalize:
									__UpperCAmelCase	: Optional[int]		=					self.normalize(image=UpperCamelCase_      ,  mean=UpperCamelCase_      ,  std=UpperCamelCase_)

						__UpperCAmelCase	: Any		=					to_channel_dimension_format(UpperCamelCase_      ,  UpperCamelCase_)
						return image


			def       a_     (      self      :    List[Any]      ,  UpperCamelCase_      :    ImageInput      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    PILImageResampling = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Dict[str, int] = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    float = None      ,  UpperCamelCase_      :    bool = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[float, List[float]]] = None      ,  UpperCamelCase_      :    Optional[Union[str, TensorType]] = None      ,  UpperCamelCase_      :    ChannelDimension = ChannelDimension.FIRST      ,  **UpperCamelCase_      :    Optional[int]      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					do_resize if do_resize is not None else self.do_resize
						__UpperCAmelCase	: Optional[Any]		=					resample if resample is not None else self.resample
						__UpperCAmelCase	: Any		=					do_center_crop if do_center_crop is not None else self.do_center_crop
						__UpperCAmelCase	: str		=					do_rescale if do_rescale is not None else self.do_rescale
						__UpperCAmelCase	: str		=					rescale_factor if rescale_factor is not None else self.rescale_factor
						__UpperCAmelCase	: Tuple		=					do_normalize if do_normalize is not None else self.do_normalize
						__UpperCAmelCase	: str		=					image_mean if image_mean is not None else self.image_mean
						__UpperCAmelCase	: Optional[Any]		=					image_std if image_std is not None else self.image_std

						__UpperCAmelCase	: Tuple		=					size if size is not None else self.size
						__UpperCAmelCase	: str		=					get_size_dict(UpperCamelCase_      ,  default_to_square=UpperCamelCase_)
						__UpperCAmelCase	: Any		=					crop_size if crop_size is not None else self.crop_size
						__UpperCAmelCase	: int		=					get_size_dict(UpperCamelCase_      ,  param_name="crop_size")

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

						__UpperCAmelCase	: Dict		=					make_batched(UpperCamelCase_)

						__UpperCAmelCase	: List[str]		=					[
						    [
						        self._preprocess_image(
						            image=UpperCamelCase_      ,  do_resize=UpperCamelCase_      ,  size=UpperCamelCase_      ,  resample=UpperCamelCase_      ,  do_center_crop=UpperCamelCase_      ,  crop_size=UpperCamelCase_      ,  do_rescale=UpperCamelCase_      ,  rescale_factor=UpperCamelCase_      ,  do_normalize=UpperCamelCase_      ,  image_mean=UpperCamelCase_      ,  image_std=UpperCamelCase_      ,  data_format=UpperCamelCase_      ,  )
						        for img in video
						    ]
						    for video in videos
						]

						__UpperCAmelCase	: int		=					{"pixel_values": videos}
						return BatchFeature(data=UpperCamelCase_      ,  tensor_type=UpperCamelCase_)


