

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 collections.abc import Callable



class   __lowercase				:




			"""simple docstring"""







			def __init__( self  :     Tuple   ,						lowerCAmelCase__  :     Callable | None = None):
									# Stores actual heap items.
									SCREAMING_SNAKE_CASE_: list  		=       []
									# Stores indexes of each item for supporting updates and deletion.
									SCREAMING_SNAKE_CASE_: dict  		=       {}
									# Stores current size of heap.
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       0
									# Stores function used to evaluate the score of an item on which basis ordering
									# will be done.
									SCREAMING_SNAKE_CASE_: Any  		=       key or (lambda lowerCAmelCase__: x)



			def        _SCREAMING_SNAKE_CASE			( self  :     Dict   ,						lowerCAmelCase__  :     int):
									return int((i - 1) / 2) if i > 0 else None



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       int(2 * i + 1)
									return left if 0 < left < self.size else None



			def        _SCREAMING_SNAKE_CASE			( self  :     Union[str, Any]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Union[str, Any]  		=       int(2 * i + 2)
									return right if 0 < right < self.size else None



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: str  		=       (
									    self.pos_map[self.arr[j][0]],
									    self.pos_map[self.arr[i][0]],
									)
									# Then swap the items in the list.
									SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: List[str]  		=       self.arr[j], self.arr[i]



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     int):
									return self.arr[i][1] < self.arr[j][1]



			def        _SCREAMING_SNAKE_CASE			( self  :     str   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Any  		=       self._left(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self._right(lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_: List[Any]  		=       i

									if left is not None and not self._cmp(lowerCAmelCase__   ,						lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_: Optional[int]  		=       left
									if right is not None and not self._cmp(lowerCAmelCase__   ,						lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_: Tuple  		=       right

									return valid_parent



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self._parent(lowerCAmelCase__)
									while parent is not None and not self._cmp(lowerCAmelCase__   ,						lowerCAmelCase__):
															self._swap(lowerCAmelCase__   ,						lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: List[str]  		=       parent, self._parent(lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       self._get_valid_parent(lowerCAmelCase__)
									while valid_parent != index:
															self._swap(lowerCAmelCase__   ,						lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_		,    SCREAMING_SNAKE_CASE_: Dict  		=       valid_parent, self._get_valid_parent(lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[int]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     int):
									if item not in self.pos_map:
															return
									SCREAMING_SNAKE_CASE_: Any  		=       self.pos_map[item]
									SCREAMING_SNAKE_CASE_: int  		=       [item, self.key(lowerCAmelCase__)]
									# Make sure heap is right in both up and down direction.
									# Ideally only one of them will make any change.
									self._heapify_up(lowerCAmelCase__)
									self._heapify_down(lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     List[str]   ,						lowerCAmelCase__  :     int):
									if item not in self.pos_map:
															return
									SCREAMING_SNAKE_CASE_: Optional[Any]  		=       self.pos_map[item]
									del self.pos_map[item]
									SCREAMING_SNAKE_CASE_: List[str]  		=       self.arr[self.size - 1]
									SCREAMING_SNAKE_CASE_: Tuple  		=       index
									self.size -= 1
									# Make sure heap is right in both up and down direction. Ideally only one
									# of them will make any change- so no performance loss in calling both.
									if self.size > index:
															self._heapify_up(lowerCAmelCase__)
															self._heapify_down(lowerCAmelCase__)



			def        _SCREAMING_SNAKE_CASE			( self  :     Optional[Any]   ,						lowerCAmelCase__  :     int   ,						lowerCAmelCase__  :     int):
									SCREAMING_SNAKE_CASE_: Optional[int]  		=       len(self.arr)
									if arr_len == self.size:
															self.arr.append([item, self.key(lowerCAmelCase__)])
									else:
															SCREAMING_SNAKE_CASE_: str  		=       [item, self.key(lowerCAmelCase__)]
									SCREAMING_SNAKE_CASE_: List[Any]  		=       self.size
									self.size += 1
									self._heapify_up(self.size - 1)



			def        _SCREAMING_SNAKE_CASE			( self  :     List[Any]):
									return self.arr[0] if self.size else None




			def        _SCREAMING_SNAKE_CASE			( self  :     Dict):
									SCREAMING_SNAKE_CASE_: Dict  		=       self.get_top()
									if top_item_tuple:
															self.delete_item(top_item_tuple[0])
									return top_item_tuple







def 			A_      (    ):
						pass


if __name__ == "__main__":
						import doctest

						doctest.testmod()






