




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			:	Tuple							 =     logging.get_logger(__name__)

_lowerCamelCase			:	Tuple							 =     {
    """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_					(				UpperCAmelCase__  ):

    '''simple docstring'''






    UpperCAmelCase__													=   '''owlvit_text_model'''


    def __init__(    self   :			int      ,				UpperCAmelCase__   :			List[Any]=49_408      ,				UpperCAmelCase__   :			int=512      ,				UpperCAmelCase__   :			Optional[int]=2_048      ,				UpperCAmelCase__   :			List[Any]=12      ,				UpperCAmelCase__   :			List[Any]=8      ,				UpperCAmelCase__   :			Optional[int]=16      ,				UpperCAmelCase__   :			List[Any]="quick_gelu"      ,				UpperCAmelCase__   :			str=1e-5      ,				UpperCAmelCase__   :			List[str]=0.0      ,				UpperCAmelCase__   :			Optional[int]=0.02      ,				UpperCAmelCase__   :			Tuple=1.0      ,				UpperCAmelCase__   :			int=0      ,				UpperCAmelCase__   :			Dict=49_406      ,				UpperCAmelCase__   :			Tuple=49_407      ,				**UpperCAmelCase__   :			Tuple      ,				)		->List[str]:





           '''simple docstring'''

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

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





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





           '''simple docstring'''

           cls._set_token_in_kwargs(UpperCAmelCase__)

           A__				,							A__   				=					cls.get_config_dict(UpperCAmelCase__      ,				**UpperCAmelCase__)

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

    '''simple docstring'''






    UpperCAmelCase__													=   '''owlvit_vision_model'''


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





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)

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





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





           '''simple docstring'''

           cls._set_token_in_kwargs(UpperCAmelCase__)

           A__				,							A__   				=					cls.get_config_dict(UpperCAmelCase__      ,				**UpperCAmelCase__)

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

    '''simple docstring'''






    UpperCAmelCase__													=   '''owlvit'''
    UpperCAmelCase__													=   True


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





           '''simple docstring'''

           super().__init__(**UpperCAmelCase__)

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

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

           A__   				=					OwlViTTextConfig(**UpperCAmelCase__)
           A__   				=					OwlViTVisionConfig(**UpperCAmelCase__)

           A__   				=					projection_dim
           A__   				=					logit_scale_init_value
           A__   				=					return_dict
           A__   				=					1.0


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





           '''simple docstring'''

           cls._set_token_in_kwargs(UpperCAmelCase__)

           A__				,							A__   				=					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  SCREAMING_SNAKE_CASE							(    cls   :			str      ,				UpperCAmelCase__   :			Dict      ,				UpperCAmelCase__   :			Dict      ,				**UpperCAmelCase__   :			Optional[Any])		->List[Any]:





           '''simple docstring'''

           A__   				=					{}
           A__   				=					text_config
           A__   				=					vision_config

           return cls.from_dict(UpperCAmelCase__      ,				**UpperCAmelCase__)





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





           '''simple docstring'''

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


class        UpperCamelCase_					(				UpperCAmelCase__  ):

    '''simple docstring'''






    @property
    def  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->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  SCREAMING_SNAKE_CASE							(    self   :			List[str])		->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  SCREAMING_SNAKE_CASE							(    self   :			Dict)		->float:





           '''simple docstring'''

           return 1e-4


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





           '''simple docstring'''

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





    @property
    def  SCREAMING_SNAKE_CASE							(    self   :			Tuple)		->int:





           '''simple docstring'''

           return 14








import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
  from transformers import AutoModelForSeqaSeqLM, AutoTokenizer


@require_torch
@require_sentencepiece
@require_tokenizers
class        UpperCamelCase_					(				unittest.TestCase  ):

    '''simple docstring'''






    @slow
    def  SCREAMING_SNAKE_CASE							(    self   :			Optional[Any])		->Any:





           '''simple docstring'''

           A__   				=					AutoModelForSeqaSeqLM.from_pretrained('''google/mt5-small'''      ,				return_dict=UpperCAmelCase__).to(UpperCAmelCase__)
           A__   				=					AutoTokenizer.from_pretrained('''google/mt5-small''')

           A__   				=					tokenizer('''Hello there'''      ,				return_tensors='''pt''').input_ids
           A__   				=					tokenizer('''Hi I am'''      ,				return_tensors='''pt''').input_ids

           A__   				=					model(input_ids.to(UpperCAmelCase__)      ,				labels=labels.to(UpperCAmelCase__)).loss
           A__   				=					-(labels.shape[-1] * loss.item())

           A__   				=					-84.9127
           self.assertTrue(abs(mtf_score - EXPECTED_SCORE) < 1e-4)


