



"""simple docstring"""





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
		from PIL import Image

		from transformers import (
		    AutoProcessor,
		    BertTokenizerFast,
		    BlipImageProcessor,
		    GPTaTokenizer,
		    InstructBlipProcessor,
		    PreTrainedTokenizerFast,
		)
@require_vision
class    lowercase__     (					unittest.TestCase   ):







							'''simple docstring'''


							def   lowercase__       (							self    :						Any					)				->		int:


													'''simple docstring'''




													UpperCAmelCase_    				=							tempfile.mkdtemp()

													UpperCAmelCase_    				=							BlipImageProcessor()
													UpperCAmelCase_    				=							GPTaTokenizer.from_pretrained("hf-internal-testing/tiny-random-GPT2Model"					)
													UpperCAmelCase_    				=							BertTokenizerFast.from_pretrained("hf-internal-testing/tiny-random-bert"					)

													UpperCAmelCase_    				=							InstructBlipProcessor(_a	,       _a	,       _a					)

													processor.save_pretrained(self.tmpdirname					)



							def   lowercase__       (							self    :						str	,       **_UpperCAmelCase    :						Optional[int]					)				->		Dict:


													'''simple docstring'''




													return AutoProcessor.from_pretrained(self.tmpdirname	,       **_a					).tokenizer



							def   lowercase__       (							self    :						Dict	,       **_UpperCAmelCase    :						Optional[int]					)				->		Union[str, Any]:


													'''simple docstring'''




													return AutoProcessor.from_pretrained(self.tmpdirname	,       **_a					).image_processor



							def   lowercase__       (							self    :						Dict	,       **_UpperCAmelCase    :						Optional[Any]					)				->		Tuple:


													'''simple docstring'''




													return AutoProcessor.from_pretrained(self.tmpdirname	,       **_a					).qformer_tokenizer



							def   lowercase__       (							self    :						Optional[Any]					)				->		List[Any]:


													'''simple docstring'''




													shutil.rmtree(self.tmpdirname					)



							def   lowercase__       (							self    :						int					)				->		Optional[Any]:


													'''simple docstring'''




													UpperCAmelCase_    				=							[np.random.randint(255	,       size=(3, 30, 400)	,       dtype=np.uinta					)]

													UpperCAmelCase_    				=							[Image.fromarray(np.moveaxis(_a	,       0	,       -1					)					) for x in image_inputs]

													return image_inputs



							def   lowercase__       (							self    :						Dict					)				->		Optional[Any]:


													'''simple docstring'''




													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=self.get_tokenizer()	,       image_processor=self.get_image_processor()	,       qformer_tokenizer=self.get_qformer_tokenizer()	,       )
													processor.save_pretrained(self.tmpdirname					)

													UpperCAmelCase_    				=							self.get_tokenizer(bos_token="(BOS)"	,       eos_token="(EOS)"					)
													UpperCAmelCase_    				=							self.get_image_processor(do_normalize=_a	,       padding_value=1.0					)

													UpperCAmelCase_    				=							InstructBlipProcessor.from_pretrained(
													    self.tmpdirname	,       bos_token="(BOS)"	,       eos_token="(EOS)"	,       do_normalize=_a	,       padding_value=1.0					)

													self.assertEqual(processor.tokenizer.get_vocab()	,       tokenizer_add_kwargs.get_vocab()					)
													self.assertIsInstance(processor.tokenizer	,       _a					)

													self.assertEqual(processor.image_processor.to_json_string()	,       image_processor_add_kwargs.to_json_string()					)
													self.assertIsInstance(processor.image_processor	,       _a					)
													self.assertIsInstance(processor.qformer_tokenizer	,       _a					)



							def   lowercase__       (							self    :						Tuple					)				->		Any:


													'''simple docstring'''




													UpperCAmelCase_    				=							self.get_image_processor()
													UpperCAmelCase_    				=							self.get_tokenizer()
													UpperCAmelCase_    				=							self.get_qformer_tokenizer()

													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=_a	,       image_processor=_a	,       qformer_tokenizer=_a					)

													UpperCAmelCase_    				=							self.prepare_image_inputs()

													UpperCAmelCase_    				=							image_processor(_a	,       return_tensors="np"					)
													UpperCAmelCase_    				=							processor(images=_a	,       return_tensors="np"					)

													for key in input_feat_extract.keys():
																			self.assertAlmostEqual(input_feat_extract[key].sum()	,       input_processor[key].sum()	,       delta=1e-2					)



							def   lowercase__       (							self    :						Tuple					)				->		Any:


													'''simple docstring'''




													UpperCAmelCase_    				=							self.get_image_processor()
													UpperCAmelCase_    				=							self.get_tokenizer()
													UpperCAmelCase_    				=							self.get_qformer_tokenizer()

													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=_a	,       image_processor=_a	,       qformer_tokenizer=_a					)

													UpperCAmelCase_    				=							"""lower newer"""

													UpperCAmelCase_    				=							processor(text=_a					)

													UpperCAmelCase_    				=							tokenizer(_a	,       return_token_type_ids=_a					)
													UpperCAmelCase_    				=							qformer_tokenizer(_a	,       return_token_type_ids=_a					)

													for key in encoded_tokens.keys():
																			self.assertListEqual(encoded_tokens[key]	,       encoded_processor[key]					)

													for key in encoded_tokens_qformer.keys():
																			self.assertListEqual(encoded_tokens_qformer[key]	,       encoded_processor["qformer_" + key]					)



							def   lowercase__       (							self    :						Optional[int]					)				->		Tuple:


													'''simple docstring'''




													UpperCAmelCase_    				=							self.get_image_processor()
													UpperCAmelCase_    				=							self.get_tokenizer()
													UpperCAmelCase_    				=							self.get_qformer_tokenizer()

													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=_a	,       image_processor=_a	,       qformer_tokenizer=_a					)

													UpperCAmelCase_    				=							"""lower newer"""
													UpperCAmelCase_    				=							self.prepare_image_inputs()

													UpperCAmelCase_    				=							processor(text=_a	,       images=_a					)

													self.assertListEqual(
													    list(inputs.keys()					)	,       ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"]	,       )

													# test if it raises when no input is passed
													with pytest.raises(_a					):
																			processor()



							def   lowercase__       (							self    :						List[Any]					)				->		str:


													'''simple docstring'''




													UpperCAmelCase_    				=							self.get_image_processor()
													UpperCAmelCase_    				=							self.get_tokenizer()
													UpperCAmelCase_    				=							self.get_qformer_tokenizer()

													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=_a	,       image_processor=_a	,       qformer_tokenizer=_a					)

													UpperCAmelCase_    				=							[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

													UpperCAmelCase_    				=							processor.batch_decode(_a					)
													UpperCAmelCase_    				=							tokenizer.batch_decode(_a					)

													self.assertListEqual(_a	,       _a					)







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


													'''simple docstring'''




													UpperCAmelCase_    				=							self.get_image_processor()
													UpperCAmelCase_    				=							self.get_tokenizer()
													UpperCAmelCase_    				=							self.get_qformer_tokenizer()

													UpperCAmelCase_    				=							InstructBlipProcessor(
													    tokenizer=_a	,       image_processor=_a	,       qformer_tokenizer=_a					)

													UpperCAmelCase_    				=							"""lower newer"""
													UpperCAmelCase_    				=							self.prepare_image_inputs()

													UpperCAmelCase_    				=							processor(text=_a	,       images=_a					)

													self.assertListEqual(
													    list(inputs.keys()					)	,       ["input_ids", "attention_mask", "qformer_input_ids", "qformer_attention_mask", "pixel_values"]	,       )









"""simple docstring"""





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
  from ...processing_utils import ProcessorMixin
  from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase      =		logging.get_logger(__name__)

lowerCamelCase      =		{
    """google/owlvit-base-patch32""": """https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json""",
    """google/owlvit-base-patch16""": """https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json""",
    """google/owlvit-large-patch14""": """https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json""",
}
class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit_text_model'''



       def __init__(							self    :						List[Any]	,       _UpperCAmelCase    :						str=49408	,       _UpperCAmelCase    :						str=512	,       _UpperCAmelCase    :						Optional[Any]=2048	,       _UpperCAmelCase    :						Optional[int]=12	,       _UpperCAmelCase    :						Tuple=8	,       _UpperCAmelCase    :						List[str]=16	,       _UpperCAmelCase    :						List[str]="quick_gelu"	,       _UpperCAmelCase    :						Dict=1e-5	,       _UpperCAmelCase    :						Union[str, Any]=0.0	,       _UpperCAmelCase    :						List[Any]=0.02	,       _UpperCAmelCase    :						Optional[int]=1.0	,       _UpperCAmelCase    :						Dict=0	,       _UpperCAmelCase    :						Dict=49406	,       _UpperCAmelCase    :						Union[str, Any]=49407	,       **_UpperCAmelCase    :						List[str]	,       )				->		List[str]:


             '''simple docstring'''




             super().__init__(pad_token_id=_UpperCAmelCase	,       bos_token_id=_UpperCAmelCase	,       eos_token_id=_UpperCAmelCase	,       **_UpperCAmelCase					)

             UpperCAmelCase_    				=							vocab_size
             UpperCAmelCase_    				=							hidden_size
             UpperCAmelCase_    				=							intermediate_size
             UpperCAmelCase_    				=							num_hidden_layers
             UpperCAmelCase_    				=							num_attention_heads
             UpperCAmelCase_    				=							max_position_embeddings
             UpperCAmelCase_    				=							hidden_act
             UpperCAmelCase_    				=							layer_norm_eps
             UpperCAmelCase_    				=							attention_dropout
             UpperCAmelCase_    				=							initializer_range
             UpperCAmelCase_    				=							initializer_factor







       @classmethod
       def   lowercase__       (							cls    :						int	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						List[str]					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             # get the text config dict if we are loading from OwlViTConfig
             if config_dict.get("model_type"					) == "owlvit":
                   UpperCAmelCase_    				=							config_dict["text_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    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit_vision_model'''



       def __init__(							self    :						str	,       _UpperCAmelCase    :						List[str]=768	,       _UpperCAmelCase    :						Optional[Any]=3072	,       _UpperCAmelCase    :						Tuple=12	,       _UpperCAmelCase    :						Optional[Any]=12	,       _UpperCAmelCase    :						List[Any]=3	,       _UpperCAmelCase    :						List[str]=768	,       _UpperCAmelCase    :						int=32	,       _UpperCAmelCase    :						Dict="quick_gelu"	,       _UpperCAmelCase    :						Optional[Any]=1e-5	,       _UpperCAmelCase    :						Optional[int]=0.0	,       _UpperCAmelCase    :						List[Any]=0.02	,       _UpperCAmelCase    :						List[str]=1.0	,       **_UpperCAmelCase    :						List[str]	,       )				->		Dict:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase					)

             UpperCAmelCase_    				=							hidden_size
             UpperCAmelCase_    				=							intermediate_size
             UpperCAmelCase_    				=							num_hidden_layers
             UpperCAmelCase_    				=							num_attention_heads
             UpperCAmelCase_    				=							num_channels
             UpperCAmelCase_    				=							image_size
             UpperCAmelCase_    				=							patch_size
             UpperCAmelCase_    				=							hidden_act
             UpperCAmelCase_    				=							layer_norm_eps
             UpperCAmelCase_    				=							attention_dropout
             UpperCAmelCase_    				=							initializer_range
             UpperCAmelCase_    				=							initializer_factor







       @classmethod
       def   lowercase__       (							cls    :						Any	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						Union[str, Any]					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             # get the vision config dict if we are loading from OwlViTConfig
             if config_dict.get("model_type"					) == "owlvit":
                   UpperCAmelCase_    				=							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    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  '''owlvit'''
       UpperCamelCase		=  True



       def __init__(							self    :						Tuple	,       _UpperCAmelCase    :						Any=None	,       _UpperCAmelCase    :						Dict=None	,       _UpperCAmelCase    :						Tuple=512	,       _UpperCAmelCase    :						Any=2.6592	,       _UpperCAmelCase    :						Union[str, Any]=True	,       **_UpperCAmelCase    :						Union[str, Any]	,       )				->		Tuple:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase					)

             if text_config is None:
                   UpperCAmelCase_    				=							{}
                   logger.info("text_config is None. Initializing the OwlViTTextConfig with default values."					)

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

             UpperCAmelCase_    				=							OwlViTTextConfig(**_UpperCAmelCase					)
             UpperCAmelCase_    				=							OwlViTVisionConfig(**_UpperCAmelCase					)

             UpperCAmelCase_    				=							projection_dim
             UpperCAmelCase_    				=							logit_scale_init_value
             UpperCAmelCase_    				=							return_dict
             UpperCAmelCase_    				=							1.0



       @classmethod
       def   lowercase__       (							cls    :						Dict	,       _UpperCAmelCase    :						Union[str, os.PathLike]	,       **_UpperCAmelCase    :						Tuple					)				->		"PretrainedConfig":


             '''simple docstring'''




             cls._set_token_in_kwargs(_UpperCAmelCase					)

             UpperCAmelCase_				,					UpperCAmelCase_    				=							cls.get_config_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)

             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					)



       @classmethod
       def   lowercase__       (							cls    :						List[str]	,       _UpperCAmelCase    :						Dict	,       _UpperCAmelCase    :						Dict	,       **_UpperCAmelCase    :						Any					)				->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							{}
             UpperCAmelCase_    				=							text_config
             UpperCAmelCase_    				=							vision_config

             return cls.from_dict(_UpperCAmelCase	,       **_UpperCAmelCase					)







       def   lowercase__       (							self    :						str					)				->		List[str]:


             '''simple docstring'''




             UpperCAmelCase_    				=							copy.deepcopy(self.__dict__					)
             UpperCAmelCase_    				=							self.text_config.to_dict()
             UpperCAmelCase_    				=							self.vision_config.to_dict()
             UpperCAmelCase_    				=							self.__class__.model_type
             return output



class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       @property
       def   lowercase__       (							self    :						Union[str, Any]					)				->		Mapping[str, Mapping[int, str]]:


             '''simple docstring'''




             return OrderedDict(
                 [
                     ("input_ids", {0: "batch", 1: "sequence"}),
                     ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
                     ("attention_mask", {0: "batch", 1: "sequence"}),
                 ]					)



       @property
       def   lowercase__       (							self    :						Tuple					)				->		Mapping[str, Mapping[int, str]]:


             '''simple docstring'''




             return OrderedDict(
                 [
                     ("logits_per_image", {0: "batch"}),
                     ("logits_per_text", {0: "batch"}),
                     ("text_embeds", {0: "batch"}),
                     ("image_embeds", {0: "batch"}),
                 ]					)



       @property
       def   lowercase__       (							self    :						Any					)				->		float:


             '''simple docstring'''




             return 1e-4



       def   lowercase__       (							self    :						List[str]	,       _UpperCAmelCase    :						"ProcessorMixin"	,       _UpperCAmelCase    :						int = -1	,       _UpperCAmelCase    :						int = -1	,       _UpperCAmelCase    :						Optional["TensorType"] = None	,       )				->		Mapping[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							super().generate_dummy_inputs(
                 processor.tokenizer	,       batch_size=_UpperCAmelCase	,       seq_length=_UpperCAmelCase	,       framework=_UpperCAmelCase					)
             UpperCAmelCase_    				=							super().generate_dummy_inputs(
                 processor.image_processor	,       batch_size=_UpperCAmelCase	,       framework=_UpperCAmelCase					)
             return {**text_input_dict, **image_input_dict}







       @property
       def   lowercase__       (							self    :						Dict					)				->		int:


             '''simple docstring'''




             return 14




