




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


SCREAMING_SNAKE_CASE__       :      Optional[Any]  =   logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__       :      Optional[int]  =   {
    '''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 		UpperCamelCase__	(A__     ):

    '''simple docstring'''



    lowerCamelCase_      :	Optional[int]	     =      '''owlvit_text_model'''







    def __init__(    self     ,  UpperCamelCase__=4_9408     ,  UpperCamelCase__=512     ,  UpperCamelCase__=2048     ,  UpperCamelCase__=12     ,  UpperCamelCase__=8     ,  UpperCamelCase__=16     ,  UpperCamelCase__="quick_gelu"     ,  UpperCamelCase__=1e-5     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=0.02     ,  UpperCamelCase__=1.0     ,  UpperCamelCase__=0     ,  UpperCamelCase__=4_9406     ,  UpperCamelCase__=4_9407     ,  **UpperCamelCase__     ,  )     ->      Optional[Any]:
           super().__init__(pad_token_id=UpperCamelCase__     ,  bos_token_id=UpperCamelCase__     ,  eos_token_id=UpperCamelCase__     ,  **UpperCamelCase__     )

           lowerCamelCase   :      Tuple			 =					vocab_size
           lowerCamelCase   :      int			 =					hidden_size
           lowerCamelCase   :      List[str]			 =					intermediate_size
           lowerCamelCase   :      Tuple			 =					num_hidden_layers
           lowerCamelCase   :      str			 =					num_attention_heads
           lowerCamelCase   :      Any			 =					max_position_embeddings
           lowerCamelCase   :      List[Any]			 =					hidden_act
           lowerCamelCase   :      str			 =					layer_norm_eps
           lowerCamelCase   :      List[Any]			 =					attention_dropout
           lowerCamelCase   :      Tuple			 =					initializer_range
           lowerCamelCase   :      Optional[Any]			 =					initializer_factor







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      Tuple:
           cls._set_token_in_kwargs(UpperCamelCase__     )

           lowerCamelCase   :      Any			 =					cls.get_config_dict(UpperCamelCase__     ,  **UpperCamelCase__     )

           # get the text config dict if we are loading from OwlViTConfig
           if config_dict.get("model_type"     ) == "owlvit":
                  lowerCamelCase   :      Dict			 =					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 		UpperCamelCase__	(A__     ):

    '''simple docstring'''



    lowerCamelCase_      :	str	     =      '''owlvit_vision_model'''







    def __init__(    self     ,  UpperCamelCase__=768     ,  UpperCamelCase__=3072     ,  UpperCamelCase__=12     ,  UpperCamelCase__=12     ,  UpperCamelCase__=3     ,  UpperCamelCase__=768     ,  UpperCamelCase__=32     ,  UpperCamelCase__="quick_gelu"     ,  UpperCamelCase__=1e-5     ,  UpperCamelCase__=0.0     ,  UpperCamelCase__=0.02     ,  UpperCamelCase__=1.0     ,  **UpperCamelCase__     ,  )     ->      Union[str, Any]:
           super().__init__(**UpperCamelCase__     )

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







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      str:
           cls._set_token_in_kwargs(UpperCamelCase__     )

           lowerCamelCase   :      List[Any]			 =					cls.get_config_dict(UpperCamelCase__     ,  **UpperCamelCase__     )

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




class 		UpperCamelCase__	(A__     ):

    '''simple docstring'''



    lowerCamelCase_      :	str	     =      '''owlvit'''
    lowerCamelCase_      :	Tuple	     =      True







    def __init__(    self     ,  UpperCamelCase__=None     ,  UpperCamelCase__=None     ,  UpperCamelCase__=512     ,  UpperCamelCase__=2.6592     ,  UpperCamelCase__=True     ,  **UpperCamelCase__     ,  )     ->      Dict:
           super().__init__(**UpperCamelCase__     )

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

           if vision_config is None:
                  lowerCamelCase   :      List[str]			 =					{}
                  logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."     )

           lowerCamelCase   :      Dict			 =					OwlViTTextConfig(**UpperCamelCase__     )
           lowerCamelCase   :      Dict			 =					OwlViTVisionConfig(**UpperCamelCase__     )

           lowerCamelCase   :      Dict			 =					projection_dim
           lowerCamelCase   :      Union[str, Any]			 =					logit_scale_init_value
           lowerCamelCase   :      int			 =					return_dict
           lowerCamelCase   :      Tuple			 =					1.0







    @classmethod
    def     _lowercase     (    cls     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      Any:
           cls._set_token_in_kwargs(UpperCamelCase__     )

           lowerCamelCase   :      Dict			 =					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     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  **UpperCamelCase__     )     ->      int:
           lowerCamelCase   :      List[Any]			 =					{}
           lowerCamelCase   :      Union[str, Any]			 =					text_config
           lowerCamelCase   :      Optional[int]			 =					vision_config

           return cls.from_dict(UpperCamelCase__     ,  **UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Optional[int]			 =					copy.deepcopy(self.__dict__     )
           lowerCamelCase   :      Union[str, Any]			 =					self.text_config.to_dict()
           lowerCamelCase   :      List[str]			 =					self.vision_config.to_dict()
           lowerCamelCase   :      Optional[int]			 =					self.__class__.model_type
           return output




class 		UpperCamelCase__	(A__     ):

    '''simple docstring'''



    @property
    def     _lowercase     (    self     )     ->      Dict:
           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     )     ->      Optional[Any]:
           return OrderedDict(
               [
                   ("logits_per_image", {0: "batch"}),
                   ("logits_per_text", {0: "batch"}),
                   ("text_embeds", {0: "batch"}),
                   ("image_embeds", {0: "batch"}),
               ]     )







    @property
    def     _lowercase     (    self     )     ->      str:
           return 1e-4







    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__ = -1     ,  UpperCamelCase__ = -1     ,  UpperCamelCase__ = None     ,  )     ->      List[str]:
           lowerCamelCase   :      Optional[int]			 =					super().generate_dummy_inputs(
               processor.tokenizer     ,  batch_size=UpperCamelCase__     ,  seq_length=UpperCamelCase__     ,  framework=UpperCamelCase__     )
           lowerCamelCase   :      List[str]			 =					super().generate_dummy_inputs(
               processor.image_processor     ,  batch_size=UpperCamelCase__     ,  framework=UpperCamelCase__     )
           return {**text_input_dict, **image_input_dict}







    @property
    def     _lowercase     (    self     )     ->      Optional[Any]:
           return 14














import unittest

from transformers import load_tool
from transformers.utils import is_torch_available


if is_torch_available():
 import torch

from transformers.testing_utils import require_torch

from .test_tools_common import ToolTesterMixin





@require_torch
class 			__a  (    unittest.TestCase       ,   A__       ):







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  load_tool("text-to-speech"						)
      self.tool.setup()







 def 					__lowercase					(		self					:   Union[str, Any]						):


      '''simple docstring'''






      torch.manual_seed(0						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.tool("hey"						)
      UpperCamelCase__			:   Any       				=  result.to_raw()
      self.assertTrue(
          torch.allclose(
              resulting_tensor[:3]							,   torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5]						)							,   )						)







 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      torch.manual_seed(0						)
      UpperCamelCase__			:   List[Any]       				=  self.tool("hey"						)
      UpperCamelCase__			:   int       				=  result.to_raw()
      self.assertTrue(
          torch.allclose(
              resulting_tensor[:3]							,   torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5]						)							,   )						)