






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


lowerCamelCase			:					List[Any]         =	logging.get_logger(__name__)



class 							_UpperCamelCase			(a_			):
      snake_case_  =							["""pixel_values"""]


      def __init__(	self					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = PIL.Image.BICUBIC					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = 1 / 2_5_5					,						__UpperCamelCase = True					,						__UpperCamelCase = True					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	None:
           super().__init__(**__UpperCamelCase	)
           __lowerCAmelCase								=					size if size is not None else {"height": 2_5_6, "width": 2_5_6}
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           __lowerCAmelCase								=					crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase					,						param_name="crop_size"	)

           __lowerCAmelCase								=					do_resize
           __lowerCAmelCase								=					size
           __lowerCAmelCase								=					resample
           __lowerCAmelCase								=					do_center_crop
           __lowerCAmelCase								=					crop_size
           __lowerCAmelCase								=					do_rescale
           __lowerCAmelCase								=					rescale_factor
           __lowerCAmelCase								=					do_normalize
           __lowerCAmelCase								=					image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
           __lowerCAmelCase								=					image_std if image_std is not None else IMAGENET_STANDARD_STD


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = PIL.Image.BICUBIC					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           if "height" not in size or "width" not in size:
                raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""	)
           return resize(
               __UpperCamelCase					,						size=(size["height"], size["width"])					,						resample=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           if "height" not in size or "width" not in size:
                raise ValueError(F"""The size dictionary must have keys 'height' and 'width'. Got {size.keys()}"""	)
           return center_crop(__UpperCamelCase					,						size=(size["height"], size["width"])					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	Optional[int]:
           return rescale(__UpperCamelCase					,						scale=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase = None					,						**__UpperCamelCase					,						)->	np.ndarray:
           return normalize(__UpperCamelCase					,						mean=__UpperCamelCase					,						std=__UpperCamelCase					,						data_format=__UpperCamelCase					,						**__UpperCamelCase	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase=None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = None					,						__UpperCamelCase = ChannelDimension.FIRST					,						**__UpperCamelCase					,						)->	PIL.Image.Image:
           __lowerCAmelCase								=					do_resize if do_resize is not None else self.do_resize
           __lowerCAmelCase								=					resample if resample is not None else self.resample
           __lowerCAmelCase								=					do_center_crop if do_center_crop is not None else self.do_center_crop
           __lowerCAmelCase								=					do_rescale if do_rescale is not None else self.do_rescale
           __lowerCAmelCase								=					rescale_factor if rescale_factor is not None else self.rescale_factor
           __lowerCAmelCase								=					do_normalize if do_normalize is not None else self.do_normalize
           __lowerCAmelCase								=					image_mean if image_mean is not None else self.image_mean
           __lowerCAmelCase								=					image_std if image_std is not None else self.image_std

           __lowerCAmelCase								=					size if size is not None else self.size
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase	)
           __lowerCAmelCase								=					crop_size if crop_size is not None else self.crop_size
           __lowerCAmelCase								=					get_size_dict(__UpperCamelCase					,						param_name="crop_size"	)

           __lowerCAmelCase								=					make_list_of_images(__UpperCamelCase	)

           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."	)

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

           if do_resize:
                __lowerCAmelCase								=					[self.resize(image=__UpperCamelCase					,						size=__UpperCamelCase					,						resample=__UpperCamelCase	) for image in images]

           if do_center_crop:
                __lowerCAmelCase								=					[self.center_crop(image=__UpperCamelCase					,						size=__UpperCamelCase	) for image in images]

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

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

           __lowerCAmelCase								=					[to_channel_dimension_format(__UpperCamelCase					,						__UpperCamelCase	) for image in images]

           __lowerCAmelCase								=					{"pixel_values": images}
           return BatchFeature(data=__UpperCamelCase					,						tensor_type=__UpperCamelCase	)














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			:					Union[str, Any]         =	logging.get_logger(__name__)

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



class 							_UpperCamelCase			(a_			):
      snake_case_  =							"""instructblip_vision_model"""


      def __init__(	self					,						__UpperCamelCase=1_4_0_8					,						__UpperCamelCase=6_1_4_4					,						__UpperCamelCase=3_9					,						__UpperCamelCase=1_6					,						__UpperCamelCase=2_2_4					,						__UpperCamelCase=1_4					,						__UpperCamelCase="gelu"					,						__UpperCamelCase=1e-6					,						__UpperCamelCase=0.0					,						__UpperCamelCase=1e-10					,						__UpperCamelCase=True					,						**__UpperCamelCase					,						)->	Union[str, Any]:
           super().__init__(**__UpperCamelCase	)

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


      @classmethod
      def  __UpperCAmelCase		(	cls					,						__UpperCamelCase					,						**__UpperCamelCase	)->	"PretrainedConfig":
           cls._set_token_in_kwargs(__UpperCamelCase	)

           __lowerCAmelCase			, __lowerCAmelCase								=					cls.get_config_dict(__UpperCamelCase					,						**__UpperCamelCase	)

           # get the vision config dict if we are loading from InstructBlipConfig
           if config_dict.get("model_type"	) == "instructblip":
                __lowerCAmelCase								=					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 							_UpperCamelCase			(a_			):
      snake_case_  =							"""instructblip_qformer"""


      def __init__(	self					,						__UpperCamelCase=3_0_5_2_2					,						__UpperCamelCase=7_6_8					,						__UpperCamelCase=1_2					,						__UpperCamelCase=1_2					,						__UpperCamelCase=3_0_7_2					,						__UpperCamelCase="gelu"					,						__UpperCamelCase=0.1					,						__UpperCamelCase=0.1					,						__UpperCamelCase=5_1_2					,						__UpperCamelCase=0.0_2					,						__UpperCamelCase=1e-12					,						__UpperCamelCase=0					,						__UpperCamelCase="absolute"					,						__UpperCamelCase=2					,						__UpperCamelCase=1_4_0_8					,						**__UpperCamelCase					,						)->	Any:
           super().__init__(pad_token_id=__UpperCamelCase					,						**__UpperCamelCase	)

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


      @classmethod
      def  __UpperCAmelCase		(	cls					,						__UpperCamelCase					,						**__UpperCamelCase	)->	"PretrainedConfig":
           cls._set_token_in_kwargs(__UpperCamelCase	)

           __lowerCAmelCase			, __lowerCAmelCase								=					cls.get_config_dict(__UpperCamelCase					,						**__UpperCamelCase	)

           # get the qformer config dict if we are loading from InstructBlipConfig
           if config_dict.get("model_type"	) == "instructblip":
                __lowerCAmelCase								=					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 							_UpperCamelCase			(a_			):
      snake_case_  =							"""instructblip"""
      snake_case_  =							True


      def __init__(	self					,						__UpperCamelCase=None					,						__UpperCamelCase=None					,						__UpperCamelCase=None					,						__UpperCamelCase=3_2					,						**__UpperCamelCase	)->	Tuple:
           super().__init__(**__UpperCamelCase	)

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

           if qformer_config is None:
                __lowerCAmelCase								=					{}
                logger.info("qformer_config is None. Initializing the InstructBlipQFormerConfig with default values."	)

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

           __lowerCAmelCase								=					InstructBlipVisionConfig(**__UpperCamelCase	)
           __lowerCAmelCase								=					InstructBlipQFormerConfig(**__UpperCamelCase	)
           __lowerCAmelCase								=					text_config["model_type"] if "model_type" in text_config else "opt"
           __lowerCAmelCase								=					CONFIG_MAPPING[text_model_type](**__UpperCamelCase	)

           __lowerCAmelCase								=					self.text_config.tie_word_embeddings
           __lowerCAmelCase								=					self.text_config.is_encoder_decoder

           __lowerCAmelCase								=					num_query_tokens
           __lowerCAmelCase								=					self.vision_config.hidden_size
           __lowerCAmelCase								=					self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
           __lowerCAmelCase								=					1.0
           __lowerCAmelCase								=					0.0_2


      @classmethod
      def  __UpperCAmelCase		(	cls					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						**__UpperCamelCase					,						)->	int:
           return cls(
               vision_config=vision_config.to_dict()					,						qformer_config=qformer_config.to_dict()					,						text_config=text_config.to_dict()					,						**__UpperCamelCase					,						)


      def  __UpperCAmelCase		(	self	)->	Dict:
           __lowerCAmelCase								=					copy.deepcopy(self.__dict__	)
           __lowerCAmelCase								=					self.vision_config.to_dict()
           __lowerCAmelCase								=					self.qformer_config.to_dict()
           __lowerCAmelCase								=					self.text_config.to_dict()
           __lowerCAmelCase								=					self.__class__.model_type
           return output






