

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/instruct-blip-flan-t5': 'https://huggingface.co/Salesforce/instruct-blip-flan-t5/resolve/main/config.json',
}






class _UpperCamelCase				(				__A ):
			'''simple docstring'''







			lowerCamelCase__							     ='instructblip_vision_model'
			def __init__(      self				:			List[str]   ,   a				:			Optional[int]=1408   ,   a				:			List[str]=6144   ,   a				:			str=39   ,   a				:			Optional[int]=16   ,   a				:			List[Any]=224   ,   a				:			Dict=14   ,   a				:			Union[str, Any]="gelu"   ,   a				:			Tuple=1e-6   ,   a				:			Any=0.0   ,   a				:			str=1e-10   ,   a				:			Tuple=True   ,   **a				:			Dict   ,   )				->							Union[str, Any]:

				"""simple docstring"""

				super().__init__(**a						)

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






			@classmethod
			def 				__UpperCamelCase			(      cls				:			int   ,   a				:			Union[str, os.PathLike]   ,   **a				:			Tuple						)				->							"PretrainedConfig":

				"""simple docstring"""

				cls._set_token_in_kwargs(a						)

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							cls.get_config_dict(a   ,   **a						)

				# get the vision config dict if we are loading from InstructBlipConfig
				if config_dict.get("model_type"						) == "instructblip":
					SCREAMING_SNAKE_CASE :   List[str]					  =							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(a   ,   **a						)






class _UpperCamelCase				(				__A ):
			'''simple docstring'''







			lowerCamelCase__							     ='instructblip_qformer'
			def __init__(      self				:			List[Any]   ,   a				:			str=3_0522   ,   a				:			List[Any]=768   ,   a				:			Tuple=12   ,   a				:			Dict=12   ,   a				:			List[Any]=3072   ,   a				:			List[Any]="gelu"   ,   a				:			Optional[Any]=0.1   ,   a				:			List[str]=0.1   ,   a				:			List[Any]=512   ,   a				:			List[str]=0.02   ,   a				:			List[str]=1e-12   ,   a				:			Optional[Any]=0   ,   a				:			Union[str, Any]="absolute"   ,   a				:			Any=2   ,   a				:			Union[str, Any]=1408   ,   **a				:			Tuple   ,   )				->							str:

				"""simple docstring"""

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

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






			@classmethod
			def 				__UpperCamelCase			(      cls				:			Dict   ,   a				:			Union[str, os.PathLike]   ,   **a				:			Optional[int]						)				->							"PretrainedConfig":

				"""simple docstring"""

				cls._set_token_in_kwargs(a						)

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Optional[Any]					  =							cls.get_config_dict(a   ,   **a						)

				# get the qformer config dict if we are loading from InstructBlipConfig
				if config_dict.get("model_type"						) == "instructblip":
					SCREAMING_SNAKE_CASE :   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(a   ,   **a						)






class _UpperCamelCase				(				__A ):
			'''simple docstring'''







			lowerCamelCase__							     ='instructblip'
			lowerCamelCase__							     =True
			def __init__(      self				:			int   ,   a				:			Optional[Any]=None   ,   a				:			str=None   ,   a				:			List[Any]=None   ,   a				:			Optional[int]=32   ,   **a				:			List[Any]						)				->							List[str]:

				"""simple docstring"""

				super().__init__(**a						)

				if vision_config is None:
					SCREAMING_SNAKE_CASE :   Optional[int]					  =							{}
					logger.info("vision_config is None. initializing the InstructBlipVisionConfig with default values."						)

				if qformer_config is None:
					SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							{}
					logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."						)

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

				SCREAMING_SNAKE_CASE :   int					  =							InstructBlipVisionConfig(**a						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							InstructBlipQFormerConfig(**a						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							text_config["model_type"] if "model_type" in text_config else "opt"
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							CONFIG_MAPPING[text_model_type](**a						)

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.text_config.tie_word_embeddings
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.text_config.is_encoder_decoder

				SCREAMING_SNAKE_CASE :   str					  =							num_query_tokens
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							self.vision_config.hidden_size
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							1.0
				SCREAMING_SNAKE_CASE :   Tuple					  =							0.02
			@classmethod
			def 				__UpperCamelCase			(      cls				:			int   ,   a				:			InstructBlipVisionConfig   ,   a				:			InstructBlipQFormerConfig   ,   a				:			PretrainedConfig   ,   **a				:			int   ,   )				->							List[str]:

				"""simple docstring"""

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






			def 				__UpperCamelCase			(      self				:			List[str]						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							copy.deepcopy(self.__dict__						)
				SCREAMING_SNAKE_CASE :   Any					  =							self.vision_config.to_dict()
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.qformer_config.to_dict()
				SCREAMING_SNAKE_CASE :   Tuple					  =							self.text_config.to_dict()
				SCREAMING_SNAKE_CASE :   int					  =							self.__class__.model_type
				return output






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,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
      import PIL


a__      :     Dict         =		logging.get_logger(__name__)





class UpperCAmelCase_				( _SCREAMING_SNAKE_CASE ):
  __lowerCAmelCase   :						Optional[int]							=   ["""pixel_values"""]


  def __init__(							self ,__snake_case = True ,__snake_case = None ,__snake_case = None ,__snake_case = PILImageResampling.BILINEAR ,__snake_case = True ,__snake_case = 1 / 2_5_5 ,__snake_case = True ,__snake_case = None ,__snake_case = None ,**__snake_case ,):



       """simple docstring"""





       super().__init__(**__snake_case )
       A_          =       size if size is not None else {'''shortest_edge''': 3_8_4}
       A_          =       get_size_dict(__snake_case ,default_to_square=__snake_case )

       A_          =       do_resize
       A_          =       size
       # Default value set here for backwards compatibility where the value in config is None
       A_          =       crop_pct if crop_pct is not None else 2_2_4 / 2_5_6
       A_          =       resample
       A_          =       do_rescale
       A_          =       rescale_factor
       A_          =       do_normalize
       A_          =       image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
       A_          =       image_std if image_std is not None else IMAGENET_STANDARD_STD


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case = PILImageResampling.BICUBIC ,__snake_case = None ,**__snake_case ,):



       """simple docstring"""





       A_          =       get_size_dict(__snake_case ,default_to_square=__snake_case )
       if "shortest_edge" not in size:
            raise ValueError(f'Size dictionary must contain \'shortest_edge\' key. Got {size.keys()}' )
       A_          =       size['''shortest_edge''']

       if shortest_edge < 3_8_4:
            # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct
            A_          =       int(shortest_edge / crop_pct )
            A_          =       get_resize_output_image_size(__snake_case ,size=__snake_case ,default_to_square=__snake_case )
            A_          =       resize(image=__snake_case ,size=__snake_case ,resample=__snake_case ,data_format=__snake_case ,**__snake_case )
            # then crop to (shortest_edge, shortest_edge)
            return center_crop(image=__snake_case ,size=(shortest_edge, shortest_edge) ,data_format=__snake_case ,**__snake_case )
       else:
            # warping (no cropping) when evaluated at 384 or larger
            return resize(
                __snake_case ,size=(shortest_edge, shortest_edge) ,resample=__snake_case ,data_format=__snake_case ,**__snake_case )


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case = None ,**__snake_case ,):



       """simple docstring"""





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


  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case ,__snake_case ,__snake_case = None ,**__snake_case ,):



       """simple docstring"""





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




  def 						__UpperCAmelCase     (							self ,__snake_case ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = None ,__snake_case = ChannelDimension.FIRST ,**__snake_case ,):



       """simple docstring"""





       A_          =       do_resize if do_resize is not None else self.do_resize
       A_          =       crop_pct if crop_pct is not None else self.crop_pct
       A_          =       resample if resample is not None else self.resample
       A_          =       do_rescale if do_rescale is not None else self.do_rescale
       A_          =       rescale_factor if rescale_factor is not None else self.rescale_factor
       A_          =       do_normalize if do_normalize is not None else self.do_normalize
       A_          =       image_mean if image_mean is not None else self.image_mean
       A_          =       image_std if image_std is not None else self.image_std

       A_          =       size if size is not None else self.size
       A_          =       get_size_dict(__snake_case ,default_to_square=__snake_case )

       A_          =       make_list_of_images(__snake_case )

       if not valid_images(__snake_case ):
            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_resize and size["shortest_edge"] < 3_8_4 and crop_pct is None:
            raise ValueError('''crop_pct must be specified if size < 384.''' )

       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.
       A_          =       [to_numpy_array(__snake_case ) for image in images]

       if do_resize:
            A_          =       [self.resize(image=__snake_case ,size=__snake_case ,crop_pct=__snake_case ,resample=__snake_case ) for image in images]

       if do_rescale:
            A_          =       [self.rescale(image=__snake_case ,scale=__snake_case ) for image in images]

       if do_normalize:
            A_          =       [self.normalize(image=__snake_case ,mean=__snake_case ,std=__snake_case ) for image in images]

       A_          =       [to_channel_dimension_format(__snake_case ,__snake_case ) for image in images]

       A_          =       {'''pixel_values''': images}
       return BatchFeature(data=__snake_case ,tensor_type=__snake_case )



