

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

SCREAMING_SNAKE_CASE_            = {
    '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 			a					(		UpperCAmelCase			):
						_lowercase        =     "owlvit_text_model"


						def __init__(    self  ,   A_=49408  ,   A_=512  ,   A_=2048  ,   A_=12  ,   A_=8  ,   A_=16  ,   A_="quick_gelu"  ,   A_=1e-5  ,   A_=0.0  ,   A_=0.02  ,   A_=1.0  ,   A_=0  ,   A_=49406  ,   A_=49407  ,   **A_  ,   ):







										'''simple docstring'''


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

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





						@classmethod
						def     _UpperCAmelCase			(    cls  ,   A_  ,   **A_		):







										'''simple docstring'''


										cls._set_token_in_kwargs(A_		)

										_UpperCAmelCase						,							_UpperCAmelCase    :						Tuple							=      cls.get_config_dict(A_  ,   **A_		)

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







class 			a					(		UpperCAmelCase			):
						_lowercase        =     "owlvit_vision_model"


						def __init__(    self  ,   A_=768  ,   A_=3072  ,   A_=12  ,   A_=12  ,   A_=3  ,   A_=768  ,   A_=32  ,   A_="quick_gelu"  ,   A_=1e-5  ,   A_=0.0  ,   A_=0.02  ,   A_=1.0  ,   **A_  ,   ):







										'''simple docstring'''


										super().__init__(**A_		)

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





						@classmethod
						def     _UpperCAmelCase			(    cls  ,   A_  ,   **A_		):







										'''simple docstring'''


										cls._set_token_in_kwargs(A_		)

										_UpperCAmelCase						,							_UpperCAmelCase    :						Tuple							=      cls.get_config_dict(A_  ,   **A_		)

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







class 			a					(		UpperCAmelCase			):
						_lowercase        =     "owlvit"
						_lowercase        =     True


						def __init__(    self  ,   A_=None  ,   A_=None  ,   A_=512  ,   A_=2.65_92  ,   A_=True  ,   **A_  ,   ):







										'''simple docstring'''


										super().__init__(**A_		)

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

										if vision_config is None:
														_UpperCAmelCase    :						Optional[Any]							=      {}
														logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."		)

										_UpperCAmelCase    :						Union[str, Any]							=      OwlViTTextConfig(**A_		)
										_UpperCAmelCase    :						Optional[int]							=      OwlViTVisionConfig(**A_		)

										_UpperCAmelCase    :						Optional[Any]							=      projection_dim
										_UpperCAmelCase    :						List[Any]							=      logit_scale_init_value
										_UpperCAmelCase    :						List[Any]							=      return_dict
										_UpperCAmelCase    :						List[Any]							=      1.0


						@classmethod
						def     _UpperCAmelCase			(    cls  ,   A_  ,   **A_		):







										'''simple docstring'''


										cls._set_token_in_kwargs(A_		)

										_UpperCAmelCase						,							_UpperCAmelCase    :						int							=      cls.get_config_dict(A_  ,   **A_		)

										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(A_  ,   **A_		)


						@classmethod
						def     _UpperCAmelCase			(    cls  ,   A_  ,   A_  ,   **A_		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[int]							=      {}
										_UpperCAmelCase    :						List[Any]							=      text_config
										_UpperCAmelCase    :						Union[str, Any]							=      vision_config

										return cls.from_dict(A_  ,   **A_		)





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


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







class 			a					(		UpperCAmelCase			):


						@property
						def     _UpperCAmelCase			(    self		):







										'''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     _UpperCAmelCase			(    self		):







										'''simple docstring'''


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


						@property
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return 1e-4


						def     _UpperCAmelCase			(    self  ,   A_  ,   A_ = -1  ,   A_ = -1  ,   A_ = None  ,   ):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      super().generate_dummy_inputs(
										    processor.tokenizer  ,   batch_size=A_  ,   seq_length=A_  ,   framework=A_		)
										_UpperCAmelCase    :						Union[str, Any]							=      super().generate_dummy_inputs(
										    processor.image_processor  ,   batch_size=A_  ,   framework=A_		)
										return {**text_input_dict, **image_input_dict}





						@property
						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										return 14





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import is_speech_available, is_vision_available
from transformers.testing_utils import require_torch


if is_vision_available():
					from transformers import TvltImageProcessor

if is_speech_available():
					from transformers import TvltFeatureExtractor

from transformers import TvltProcessor

@require_torch
class 			a					(		unittest.TestCase			):


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						int							=      "ZinengTang/tvlt-base"
										_UpperCAmelCase    :						int							=      tempfile.mkdtemp()


						def     _UpperCAmelCase			(    self  ,   **A_		):







										'''simple docstring'''


										return TvltImageProcessor.from_pretrained(self.checkpoint  ,   **A_		)


						def     _UpperCAmelCase			(    self  ,   **A_		):







										'''simple docstring'''


										return TvltFeatureExtractor.from_pretrained(self.checkpoint  ,   **A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										shutil.rmtree(self.tmpdirname		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Dict							=      self.get_image_processor()
										_UpperCAmelCase    :						Optional[int]							=      self.get_feature_extractor()

										_UpperCAmelCase    :						Tuple							=      TvltProcessor(image_processor=A_  ,   feature_extractor=A_		)
										processor.save_pretrained(self.tmpdirname		)
										_UpperCAmelCase    :						str							=      TvltProcessor.from_pretrained(self.tmpdirname		)

										self.assertIsInstance(processor.feature_extractor  ,   A_		)
										self.assertIsInstance(processor.image_processor  ,   A_		)


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[Any]							=      self.get_image_processor()
										_UpperCAmelCase    :						int							=      self.get_feature_extractor()

										_UpperCAmelCase    :						Tuple							=      TvltProcessor(image_processor=A_  ,   feature_extractor=A_		)

										_UpperCAmelCase    :						List[str]							=      np.ones([12000]		)

										_UpperCAmelCase    :						int							=      feature_extractor(A_  ,   return_tensors="np"		)
										_UpperCAmelCase    :						int							=      processor(audio=A_  ,   return_tensors="np"		)

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


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      self.get_image_processor()
										_UpperCAmelCase    :						int							=      self.get_feature_extractor()

										_UpperCAmelCase    :						Optional[Any]							=      TvltProcessor(image_processor=A_  ,   feature_extractor=A_		)

										_UpperCAmelCase    :						Union[str, Any]							=      np.ones([3, 224, 224]		)

										_UpperCAmelCase    :						Tuple							=      image_processor(A_  ,   return_tensors="np"		)
										_UpperCAmelCase    :						List[str]							=      processor(images=A_  ,   return_tensors="np"		)

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


						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						List[str]							=      self.get_image_processor()
										_UpperCAmelCase    :						Any							=      self.get_feature_extractor()

										_UpperCAmelCase    :						Dict							=      TvltProcessor(image_processor=A_  ,   feature_extractor=A_		)

										_UpperCAmelCase    :						str							=      np.ones([12000]		)
										_UpperCAmelCase    :						Optional[Any]							=      np.ones([3, 224, 224]		)

										_UpperCAmelCase    :						List[Any]							=      processor(audio=A_  ,   images=A_		)

										self.assertListEqual(list(inputs.keys()		)  ,   ["audio_values", "audio_mask", "pixel_values", "pixel_mask"]		)

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





						def     _UpperCAmelCase			(    self		):







										'''simple docstring'''


										_UpperCAmelCase    :						Optional[Any]							=      self.get_image_processor()
										_UpperCAmelCase    :						int							=      self.get_feature_extractor()

										_UpperCAmelCase    :						str							=      TvltProcessor(image_processor=A_  ,   feature_extractor=A_		)

										self.assertListEqual(
										    processor.model_input_names  ,   image_processor.model_input_names + feature_extractor.model_input_names  ,   msg="`processor` and `image_processor`+`feature_extractor` model input names do not match"  ,   )


