

from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
						from transformers import (
						    TFBertModel,
						    TFCLIPVisionModel,
						    TFDeiTModel,
						    TFRobertaModel,
						    TFVisionTextDualEncoderModel,
						    TFViTModel,
						    VisionTextDualEncoderConfig,
						)

if is_vision_available():
						from PIL import Image

						from transformers import VisionTextDualEncoderProcessor







def 			A_      (    _UpperCAmelCase    ):
						if isinstance(_UpperCAmelCase				,	collections.abc.Iterable    ):
												return x
						return (x, x)



@require_tf
class   __lowercase				:




			"""simple docstring"""







			def        _SCREAMING_SNAKE_CASE			( self  :     Any   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     int):
									pass



			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									pass



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									pass



			def        _SCREAMING_SNAKE_CASE			( self  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Dict=None   ,						**lowerCAmelCase__  :     Tuple):
									SCREAMING_SNAKE_CASE_: int  		=       VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase__   ,						lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: List[str]  		=       TFVisionTextDualEncoderModel(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: int  		=       model(input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__)

									self.assertEqual(output["text_embeds"].shape   ,						(input_ids.shape[0], config.projection_dim))
									self.assertEqual(output["image_embeds"].shape   ,						(pixel_values.shape[0], config.projection_dim))



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     str   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Any=None   ,						**lowerCAmelCase__  :     List[str]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: int  		=       self.get_vision_text_model(lowerCAmelCase__   ,						lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__   ,						text_model=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: str  		=       model(input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__)

									self.assertEqual(output["text_embeds"].shape   ,						(input_ids.shape[0], model.config.projection_dim))
									self.assertEqual(output["image_embeds"].shape   ,						(pixel_values.shape[0], model.config.projection_dim))



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Optional[int]=None   ,						**lowerCAmelCase__  :     str):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Optional[int]  		=       self.get_vision_text_model(lowerCAmelCase__   ,						lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Dict  		=       {"vision_model": vision_model, "text_model": text_model}
									SCREAMING_SNAKE_CASE_: Dict  		=       TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: List[Any]  		=       model(input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__)

									self.assertEqual(output["text_embeds"].shape   ,						(input_ids.shape[0], model.config.projection_dim))
									self.assertEqual(output["image_embeds"].shape   ,						(pixel_values.shape[0], model.config.projection_dim))



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Union[str, Any]=None   ,						**lowerCAmelCase__  :     Dict):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Optional[int]  		=       self.get_vision_text_model(lowerCAmelCase__   ,						lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__   ,						text_model=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model(input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: List[str]  		=       output[0].numpy()

									with tempfile.TemporaryDirectory() as tmpdirname:
															model.save_pretrained(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__)

															SCREAMING_SNAKE_CASE_: str  		=       model(input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       after_output[0].numpy()
															SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       np.amax(np.abs(out_a - out_a))
															self.assertLessEqual(lowerCAmelCase__   ,						1E-5)



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     List[str]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     List[Any]=None   ,						**lowerCAmelCase__  :     List[Any]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Tuple  		=       self.get_vision_text_model(lowerCAmelCase__   ,						lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: str  		=       TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__   ,						text_model=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Tuple  		=       model(
									    input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__   ,						output_attentions=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Tuple  		=       output.vision_model_output.attentions
									self.assertEqual(len(lowerCAmelCase__)   ,						vision_config.num_hidden_layers)

									# in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
									SCREAMING_SNAKE_CASE_: List[str]  		=       to_atuple(vision_model.config.image_size)
									SCREAMING_SNAKE_CASE_: Dict  		=       to_atuple(vision_model.config.patch_size)
									SCREAMING_SNAKE_CASE_: int  		=       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
									SCREAMING_SNAKE_CASE_: str  		=       num_patches + 1
									self.assertEqual(vision_attentions[0].shape[-3:]   ,						(vision_config.num_attention_heads, seq_len, seq_len))

									SCREAMING_SNAKE_CASE_: Dict  		=       output.text_model_output.attentions
									self.assertEqual(len(lowerCAmelCase__)   ,						text_config.num_hidden_layers)

									self.assertEqual(
									    text_attentions[0].shape[-3:]   ,						(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])   ,						)



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     np.ndarray   ,						lowerCAmelCase__  :     float):
									SCREAMING_SNAKE_CASE_: int  		=       np.abs((a - b)).max()
									self.assertLessEqual(lowerCAmelCase__   ,						lowerCAmelCase__   ,						F"Difference between torch and flax is {diff} (>= {tol}).")



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self.prepare_config_and_inputs()
									self.check_vision_text_dual_encoder_model(**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									SCREAMING_SNAKE_CASE_: Tuple  		=       self.prepare_config_and_inputs()
									self.check_model_from_pretrained_configs(**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self.prepare_config_and_inputs()
									self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     str):
									SCREAMING_SNAKE_CASE_: Any  		=       self.prepare_config_and_inputs()
									self.check_save_load(**lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]):
									SCREAMING_SNAKE_CASE_: Any  		=       self.prepare_config_and_inputs()
									self.check_vision_text_output_attention(**lowerCAmelCase__)




			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: int  		=       self.get_pretrained_model_and_inputs()

									SCREAMING_SNAKE_CASE_: Dict  		=       model_a(**lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: Tuple  		=       outputs[0].numpy()

									with tempfile.TemporaryDirectory() as tmp_dirname:
															model_a.save_pretrained(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_: List[Any]  		=       TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase__)

															SCREAMING_SNAKE_CASE_: Any  		=       model_a(**lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_: str  		=       after_outputs[0].numpy()
															SCREAMING_SNAKE_CASE_: Optional[Any]  		=       np.amax(np.abs(out_a - out_a))
															self.assertLessEqual(lowerCAmelCase__   ,						1E-5)







@require_tf
class   __lowercase				( UpperCAmelCase_		,       unittest.TestCase	):




			"""simple docstring"""







			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: Tuple  		=       TFVisionTextDualEncoderModel.from_vision_text_pretrained(
									    "hf-internal-testing/tiny-random-vit"   ,						"hf-internal-testing/tiny-random-bert")
									SCREAMING_SNAKE_CASE_: List[str]  		=       13
									SCREAMING_SNAKE_CASE_: int  		=       floats_tensor(
									    [
									        batch_size,
									        model.vision_model.config.num_channels,
									        model.vision_model.config.image_size,
									        model.vision_model.config.image_size,
									    ])
									SCREAMING_SNAKE_CASE_: List[str]  		=       ids_tensor([batch_size, 4]   ,						model.text_model.config.vocab_size)
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       random_attention_mask([batch_size, 4])
									SCREAMING_SNAKE_CASE_: Dict  		=       {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

									return model, inputs



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     Union[str, Any]):
									SCREAMING_SNAKE_CASE_: Any  		=       TFViTModel(lowerCAmelCase__   ,						name="vision_model")
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       TFBertModel(lowerCAmelCase__   ,						name="text_model")
									return vision_model, text_model




			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       TFViTModelTester(self)
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       TFBertModelTester(self)
									SCREAMING_SNAKE_CASE_: str  		=       vit_model_tester.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_: List[Any]  		=       bert_model_tester.prepare_config_and_inputs()

									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: List[Any]  		=       vision_config_and_inputs

									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): List[Any]  		=       text_config_and_inputs

									return {
									    "text_config": text_config,
									    "vision_config": vision_config,
									    "pixel_values": pixel_values,
									    "attention_mask": input_mask,
									    "input_ids": input_ids,
									    "text_token_type_ids": token_type_ids,
									    "text_sequence_labels": sequence_labels,
									    "text_token_labels": token_labels,
									    "text_choice_labels": choice_labels,
									}







@require_tf
class   __lowercase				( UpperCAmelCase_		,       unittest.TestCase	):




			"""simple docstring"""







			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									# DeiT repo doesn't have TF weights, but we don't actually use the weights at all so let's
									# just reinitialize it.
									SCREAMING_SNAKE_CASE_: Any  		=       TFVisionTextDualEncoderModel.from_vision_text_pretrained(
									    "Rocketknight1/tiny-random-deit-tf"   ,						"hf-internal-testing/tiny-random-roberta")
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       13
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       floats_tensor(
									    [
									        batch_size,
									        model.vision_model.config.num_channels,
									        model.vision_model.config.image_size,
									        model.vision_model.config.image_size,
									    ])
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       ids_tensor([batch_size, 4]   ,						model.text_model.config.vocab_size)
									SCREAMING_SNAKE_CASE_: Dict  		=       random_attention_mask([batch_size, 4])
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

									return model, inputs



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     Tuple   ,						lowerCAmelCase__  :     Dict   ,						lowerCAmelCase__  :     List[Any]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     Optional[Any]=None   ,						**lowerCAmelCase__  :     str):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Optional[int]  		=       self.get_vision_text_model(lowerCAmelCase__   ,						lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: int  		=       TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase__   ,						text_model=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       model(
									    input_ids=lowerCAmelCase__   ,						pixel_values=lowerCAmelCase__   ,						attention_mask=lowerCAmelCase__   ,						output_attentions=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       output.vision_model_output.attentions
									self.assertEqual(len(lowerCAmelCase__)   ,						vision_config.num_hidden_layers)

									# in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
									SCREAMING_SNAKE_CASE_: Dict  		=       to_atuple(vision_model.config.image_size)
									SCREAMING_SNAKE_CASE_: int  		=       to_atuple(vision_model.config.patch_size)
									SCREAMING_SNAKE_CASE_: List[str]  		=       (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       num_patches + 2
									self.assertEqual(vision_attentions[0].shape[-3:]   ,						(vision_config.num_attention_heads, seq_len, seq_len))

									SCREAMING_SNAKE_CASE_: List[Any]  		=       output.text_model_output.attentions
									self.assertEqual(len(lowerCAmelCase__)   ,						text_config.num_hidden_layers)

									self.assertEqual(
									    text_attentions[0].shape[-3:]   ,						(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])   ,						)



			def        _SCREAMING_SNAKE_CASE			( self  :     int   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     str):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFDeiTModel(lowerCAmelCase__   ,						name="vision_model")
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFRobertaModel(lowerCAmelCase__   ,						name="text_model")
									return vision_model, text_model




			def        _SCREAMING_SNAKE_CASE			( self  :     Any):
									SCREAMING_SNAKE_CASE_: Dict  		=       TFDeiTModelTester(self)
									SCREAMING_SNAKE_CASE_: List[str]  		=       TFRobertaModelTester(self)
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       vit_model_tester.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       bert_model_tester.prepare_config_and_inputs()

									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: str  		=       vision_config_and_inputs

									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): Tuple  		=       text_config_and_inputs

									return {
									    "text_config": text_config,
									    "vision_config": vision_config,
									    "pixel_values": pixel_values,
									    "attention_mask": input_mask,
									    "input_ids": input_ids,
									    "text_token_type_ids": token_type_ids,
									    "text_sequence_labels": sequence_labels,
									    "text_token_labels": token_labels,
									    "text_choice_labels": choice_labels,
									}







@require_tf
class   __lowercase				( UpperCAmelCase_		,       unittest.TestCase	):




			"""simple docstring"""







			def        _SCREAMING_SNAKE_CASE			( self  :     Tuple):
									SCREAMING_SNAKE_CASE_: str  		=       TFVisionTextDualEncoderModel.from_vision_text_pretrained(
									    "Rocketknight1/tiny-random-clip-tf"   ,						"hf-internal-testing/tiny-random-bert")
									SCREAMING_SNAKE_CASE_: List[str]  		=       13
									SCREAMING_SNAKE_CASE_: Tuple  		=       floats_tensor(
									    [
									        batch_size,
									        model.vision_model.config.num_channels,
									        model.vision_model.config.image_size,
									        model.vision_model.config.image_size,
									    ])
									SCREAMING_SNAKE_CASE_: Tuple  		=       ids_tensor([batch_size, 4]   ,						model.text_model.config.vocab_size)
									SCREAMING_SNAKE_CASE_: Dict  		=       random_attention_mask([batch_size, 4])
									SCREAMING_SNAKE_CASE_: Any  		=       {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

									return model, inputs



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]   ,						lowerCAmelCase__  :     Any   ,						lowerCAmelCase__  :     Any):
									SCREAMING_SNAKE_CASE_: List[str]  		=       TFCLIPVisionModel(lowerCAmelCase__   ,						name="vision_model")
									SCREAMING_SNAKE_CASE_: List[str]  		=       TFBertModel(lowerCAmelCase__   ,						name="text_model")
									return vision_model, text_model




			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFCLIPVisionModelTester(self)
									SCREAMING_SNAKE_CASE_: Any  		=       TFBertModelTester(self)
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       clip_model_tester.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_: str  		=       bert_model_tester.prepare_config_and_inputs()

									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: List[str]  		=       vision_config_and_inputs

									(
									    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    (
									    SCREAMING_SNAKE_CASE_
									)		,    
									): List[Any]  		=       text_config_and_inputs

									return {
									    "text_config": text_config,
									    "vision_config": vision_config,
									    "pixel_values": pixel_values,
									    "attention_mask": input_mask,
									    "input_ids": input_ids,
									    "text_token_type_ids": token_type_ids,
									    "text_sequence_labels": sequence_labels,
									    "text_token_labels": token_labels,
									    "text_choice_labels": choice_labels,
									}







@require_vision
@require_tf
class   __lowercase				( unittest.TestCase	):




			"""simple docstring"""







			@slow
			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       TFVisionTextDualEncoderModel.from_pretrained(
									    "clip-italian/clip-italian"   ,						logit_scale_init_value=1.0   ,						from_pt=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian")

									SCREAMING_SNAKE_CASE_: Optional[int]  		=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
									SCREAMING_SNAKE_CASE_: int  		=       processor(
									    text=["una foto di un gatto", "una foto di un cane"]   ,						images=lowerCAmelCase__   ,						padding=lowerCAmelCase__   ,						return_tensors="np")

									SCREAMING_SNAKE_CASE_: List[str]  		=       model(**lowerCAmelCase__)

									# verify the logits
									self.assertEqual(outputs.logits_per_image.shape   ,						(inputs.pixel_values.shape[0], inputs.input_ids.shape[0]))
									self.assertEqual(
									    outputs.logits_per_text.shape   ,						(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])   ,						)

									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       np.array([[1.228_4727, 0.310_4122]])

									self.assertTrue(np.allclose(outputs.logits_per_image.numpy()   ,						lowerCAmelCase__   ,						atol=1E-3))










from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 		_snake_case  (      _snake_case							):
 SCREAMING_SNAKE_CASE__      =       'ClapFeatureExtractor'
 SCREAMING_SNAKE_CASE__      =       ('RobertaTokenizer', 'RobertaTokenizerFast')




 def __init__(		self					,  _lowerCamelCase					,  _lowerCamelCase    ):
       super().__init__(_lowerCamelCase					,  _lowerCamelCase    )




 def __call__(		self					,  _lowerCamelCase=None					,  _lowerCamelCase=None					,  _lowerCamelCase=None					,  **_lowerCamelCase    ):
       a						:Dict 							=       kwargs.pop('''sampling_rate'''					,  _lowerCamelCase    )

       if text is None and audios is None:
             raise ValueError('''You have to specify either text or audios. Both cannot be none.'''    )

       if text is not None:
             a						:Optional[int] 							=       self.tokenizer(_lowerCamelCase					,  return_tensors=_lowerCamelCase					,  **_lowerCamelCase    )

       if audios is not None:
             a						:Tuple 							=       self.feature_extractor(
                 _lowerCamelCase					,  sampling_rate=_lowerCamelCase					,  return_tensors=_lowerCamelCase					,  **_lowerCamelCase    )

       if text is not None and audios is not None:
             a						:Union[str, Any] 							=       audio_features.input_features
             return encoding
       elif text is not None:
             return encoding
       else:
             return BatchEncoding(data=dict(**_lowerCamelCase    )					,  tensor_type=_lowerCamelCase    )




 def        SCREAMING_SNAKE_CASE__				(		self					,  *_lowerCamelCase					,  **_lowerCamelCase    ):
       return self.tokenizer.batch_decode(*_lowerCamelCase					,  **_lowerCamelCase    )




 def        SCREAMING_SNAKE_CASE__				(		self					,  *_lowerCamelCase					,  **_lowerCamelCase    ):
       return self.tokenizer.decode(*_lowerCamelCase					,  **_lowerCamelCase    )




 @property
 def        SCREAMING_SNAKE_CASE__				(		self    ):
       a						:List[Any] 							=       self.tokenizer.model_input_names
       a						:str 							=       self.feature_extractor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names    )    )





