





"""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,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


UpperCAmelCase     :     List[str]							=	logging.get_logger(__name__)


if is_vision_available():
     import PIL







class     __lowerCAmelCase	(   _snake_case):
      _lowercase    : Optional[Any]            =      ["""pixel_values"""]







      def __init__(   self		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = PILImageResampling.BICUBIC		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = 1 / 2_5_5		,				lowerCAmelCase__ = True		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = True		,				**lowerCAmelCase__		,				)       ->						None:



        '''simple docstring'''




        super().__init__(**UpperCamelCase__							)
        a__     : Optional[Any]  =size if size is not None else {"shortest_edge": 2_2_4}
        a__     : Union[str, Any]  =get_size_dict(UpperCamelCase__		,				default_to_square=UpperCamelCase__							)
        a__     : List[Any]  =crop_size if crop_size is not None else {"height": 2_2_4, "width": 2_2_4}
        a__     : Union[str, Any]  =get_size_dict(UpperCamelCase__		,				default_to_square=UpperCamelCase__		,				param_name="crop_size"							)

        a__     : Any  =do_resize
        a__     : Dict  =size
        a__     : Tuple  =resample
        a__     : Union[str, Any]  =do_center_crop
        a__     : Dict  =crop_size
        a__     : List[Any]  =do_rescale
        a__     : Optional[Any]  =rescale_factor
        a__     : List[Any]  =do_normalize
        a__     : str  =image_mean if image_mean is not None else OPENAI_CLIP_MEAN
        a__     : Optional[Any]  =image_std if image_std is not None else OPENAI_CLIP_STD
        a__     : str  =do_convert_rgb







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__ = PILImageResampling.BICUBIC		,				lowerCAmelCase__ = None		,				**lowerCAmelCase__		,				)       ->						np.ndarray:



        '''simple docstring'''




        a__     : Dict  =get_size_dict(UpperCamelCase__		,				default_to_square=UpperCamelCase__							)
        if "shortest_edge" not in size:
          raise ValueError(F'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''							)
        a__     : Any  =get_resize_output_image_size(UpperCamelCase__		,				size=size["shortest_edge"]		,				default_to_square=UpperCamelCase__							)
        return resize(UpperCamelCase__		,				size=UpperCamelCase__		,				resample=UpperCamelCase__		,				data_format=UpperCamelCase__		,				**UpperCamelCase__							)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__ = None		,				**lowerCAmelCase__		,				)       ->						np.ndarray:



        '''simple docstring'''




        a__     : List[Any]  =get_size_dict(UpperCamelCase__							)
        if "height" not in size or "width" not in size:
          raise ValueError(F'''The `size` parameter must contain the keys (height, width). Got {size.keys()}'''							)
        return center_crop(UpperCamelCase__		,				size=(size["height"], size["width"])		,				data_format=UpperCamelCase__		,				**UpperCamelCase__							)







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__ = None		,				**lowerCAmelCase__		,				)       ->						int:



        '''simple docstring'''




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







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__ = None		,				**lowerCAmelCase__		,				)       ->						np.ndarray:



        '''simple docstring'''




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







      def        _lowercase  (   self		,				lowerCAmelCase__		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = None		,				lowerCAmelCase__ = ChannelDimension.FIRST		,				**lowerCAmelCase__		,				)       ->						PIL.Image.Image:



        '''simple docstring'''




        a__     : List[Any]  =do_resize if do_resize is not None else self.do_resize
        a__     : Tuple  =size if size is not None else self.size
        a__     : int  =get_size_dict(UpperCamelCase__		,				param_name="size"		,				default_to_square=UpperCamelCase__							)
        a__     : Union[str, Any]  =resample if resample is not None else self.resample
        a__     : Dict  =do_center_crop if do_center_crop is not None else self.do_center_crop
        a__     : Union[str, Any]  =crop_size if crop_size is not None else self.crop_size
        a__     : Dict  =get_size_dict(UpperCamelCase__		,				param_name="crop_size"		,				default_to_square=UpperCamelCase__							)
        a__     : Optional[int]  =do_rescale if do_rescale is not None else self.do_rescale
        a__     : int  =rescale_factor if rescale_factor is not None else self.rescale_factor
        a__     : Optional[int]  =do_normalize if do_normalize is not None else self.do_normalize
        a__     : Union[str, Any]  =image_mean if image_mean is not None else self.image_mean
        a__     : Union[str, Any]  =image_std if image_std is not None else self.image_std
        a__     : Union[str, Any]  =do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

        a__     : Any  =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:
          raise ValueError("Size 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."							)

        # PIL RGBA images are converted to RGB
        if do_convert_rgb:
          a__     : Tuple  =[convert_to_rgb(UpperCamelCase__							) for image in images]

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

        if do_resize:
          a__     : int  =[self.resize(image=UpperCamelCase__		,				size=UpperCamelCase__		,				resample=UpperCamelCase__							) for image in images]

        if do_center_crop:
          a__     : Any  =[self.center_crop(image=UpperCamelCase__		,				size=UpperCamelCase__							) for image in images]

        if do_rescale:
          a__     : List[str]  =[self.rescale(image=UpperCamelCase__		,				scale=UpperCamelCase__							) for image in images]

        if do_normalize:
          a__     : Tuple  =[self.normalize(image=UpperCamelCase__		,				mean=UpperCamelCase__		,				std=UpperCamelCase__							) for image in images]

        a__     : Tuple  =[to_channel_dimension_format(UpperCamelCase__		,				UpperCamelCase__							) for image in images]

        a__     : List[str]  ={"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


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

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







class     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_vision_model"""







      def __init__(   self		,				lowerCAmelCase__=1_4_0_8		,				lowerCAmelCase__=6_1_4_4		,				lowerCAmelCase__=3_9		,				lowerCAmelCase__=1_6		,				lowerCAmelCase__=2_2_4		,				lowerCAmelCase__=1_4		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=1E-6		,				lowerCAmelCase__=0.0		,				lowerCAmelCase__=1E-10		,				lowerCAmelCase__=True		,				**lowerCAmelCase__		,				)       ->						Optional[Any]:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : Optional[Any]  =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__     : 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     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip_qformer"""







      def __init__(   self		,				lowerCAmelCase__=3_0_5_2_2		,				lowerCAmelCase__=7_6_8		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=1_2		,				lowerCAmelCase__=3_0_7_2		,				lowerCAmelCase__="gelu"		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=0.1		,				lowerCAmelCase__=5_1_2		,				lowerCAmelCase__=0.02		,				lowerCAmelCase__=1E-12		,				lowerCAmelCase__=0		,				lowerCAmelCase__="absolute"		,				lowerCAmelCase__=2		,				lowerCAmelCase__=1_4_0_8		,				**lowerCAmelCase__		,				)       ->						Dict:



        '''simple docstring'''




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

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				**lowerCAmelCase__							)       ->						"PretrainedConfig":



        '''simple docstring'''




        cls._set_token_in_kwargs(lowerCAmelCase__							)

        a__ ,							a__     : str  =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__     : Optional[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     __lowerCAmelCase	(   UpperCamelCase__):
      _lowercase    : Dict            =      """instructblip"""
      _lowercase    : List[Any]            =      True







      def __init__(   self		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=None		,				lowerCAmelCase__=3_2		,				**lowerCAmelCase__							)       ->						str:



        '''simple docstring'''




        super().__init__(**lowerCAmelCase__							)

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

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

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

        a__     : Dict  =InstructBlipVisionConfig(**lowerCAmelCase__							)
        a__     : Union[str, Any]  =InstructBlipQFormerConfig(**lowerCAmelCase__							)
        a__     : Tuple  =text_config["model_type"] if "model_type" in text_config else "opt"
        a__     : List[str]  =CONFIG_MAPPING[text_model_type](**lowerCAmelCase__							)

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

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







      @classmethod
      def        _lowercase  (   cls		,				lowerCAmelCase__		,				lowerCAmelCase__		,				lowerCAmelCase__		,				**lowerCAmelCase__		,				)       ->						int:



        '''simple docstring'''




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







      def        _lowercase  (   self							)       ->						Union[str, Any]:



        '''simple docstring'''




        a__     : int  =copy.deepcopy(self.__dict__							)
        a__     : int  =self.vision_config.to_dict()
        a__     : str  =self.qformer_config.to_dict()
        a__     : str  =self.text_config.to_dict()
        a__     : List[str]  =self.__class__.model_type
        return output






