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	(    _lowercase						):
						if isinstance(_lowercase		,  collections.abc.Iterable						):
												return x
						return (x, x)





@require_tf
class 					lowercase__						:


						def        __A       (       self	:				str    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												pass


						def        __A       (       self	:				str						):





												'''simple docstring'''





												pass


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





												'''simple docstring'''





												pass


						def        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				Optional[int]    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				int=None    ,		**UpperCamelCase__	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							VisionTextDualEncoderConfig.from_vision_text_configs(UpperCamelCase__    ,		UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    int							=							TFVisionTextDualEncoderModel(UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Any							=							model(input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)

												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        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				List[str]    ,		UpperCamelCase__	:				Union[str, Any]=None    ,		**UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Tuple							=							self.get_vision_text_model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[str]							=							TFVisionTextDualEncoderModel(vision_model=UpperCamelCase__    ,		text_model=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							model(input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)

												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        __A       (       self	:				Dict    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				str=None    ,		**UpperCamelCase__	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    str							=							self.get_vision_text_model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							{'''vision_model''': vision_model, '''text_model''': text_model}
												SCREAMING_SNAKE_CASE							:    int							=							TFVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)

												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        __A       (       self	:				Tuple    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				Dict    ,		UpperCamelCase__	:				str=None    ,		**UpperCamelCase__	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    str							=							self.get_vision_text_model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Dict							=							TFVisionTextDualEncoderModel(vision_model=UpperCamelCase__    ,		text_model=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Dict							=							model(input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							output[0].numpy()

												with tempfile.TemporaryDirectory() as tmpdirname:
																		model.save_pretrained(UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Any							=							TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase__						)

																		SCREAMING_SNAKE_CASE							:    List[str]							=							model(input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    str							=							after_output[0].numpy()
																		SCREAMING_SNAKE_CASE							:    str							=							np.amax(np.abs(out_a - out_a						)						)
																		self.assertLessEqual(UpperCamelCase__    ,		1E-5						)


						def        __A       (       self	:				Dict    ,		UpperCamelCase__	:				Tuple    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Any    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Tuple=None    ,		**UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    List[str]							=							self.get_vision_text_model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TFVisionTextDualEncoderModel(vision_model=UpperCamelCase__    ,		text_model=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							model(
												    input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		output_attentions=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							output.vision_model_output.attentions
												self.assertEqual(len(UpperCamelCase__						)    ,		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							:    str							=							to_atuple(vision_model.config.image_size						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							to_atuple(vision_model.config.patch_size						)
												SCREAMING_SNAKE_CASE							:    str							=							(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
												SCREAMING_SNAKE_CASE							:    Tuple							=							num_patches + 1
												self.assertEqual(vision_attentions[0].shape[-3:]    ,		(vision_config.num_attention_heads, seq_len, seq_len)						)

												SCREAMING_SNAKE_CASE							:    List[str]							=							output.text_model_output.attentions
												self.assertEqual(len(UpperCamelCase__						)    ,		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        __A       (       self	:				Optional[Any]    ,		UpperCamelCase__	:				np.ndarray    ,		UpperCamelCase__	:				np.ndarray    ,		UpperCamelCase__	:				float						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							np.abs((a - b)						).max()
												self.assertLessEqual(UpperCamelCase__    ,		UpperCamelCase__    ,		f"""Difference between torch and flax is {diff} (>= {tol})."""						)


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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.prepare_config_and_inputs()
												self.check_vision_text_dual_encoder_model(**UpperCamelCase__						)


						def        __A       (       self	:				Optional[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							self.prepare_config_and_inputs()
												self.check_model_from_pretrained_configs(**UpperCamelCase__						)


						def        __A       (       self	:				Tuple						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							self.prepare_config_and_inputs()
												self.check_vision_text_dual_encoder_from_pretrained(**UpperCamelCase__						)


						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Dict							=							self.prepare_config_and_inputs()
												self.check_save_load(**UpperCamelCase__						)


						def        __A       (       self	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							self.prepare_config_and_inputs()
												self.check_vision_text_output_attention(**UpperCamelCase__						)



						@slow
						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    str							=							self.get_pretrained_model_and_inputs()

												SCREAMING_SNAKE_CASE							:    str							=							model_a(**UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    int							=							outputs[0].numpy()

												with tempfile.TemporaryDirectory() as tmp_dirname:
																		model_a.save_pretrained(UpperCamelCase__						)
																		SCREAMING_SNAKE_CASE							:    Tuple							=							TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase__						)

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






@require_tf
class 					lowercase__						(   UpperCamelCase_	,							unittest.TestCase):


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TFVisionTextDualEncoderModel.from_vision_text_pretrained(
												    '''hf-internal-testing/tiny-random-vit'''    ,		'''hf-internal-testing/tiny-random-bert'''						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							13
												SCREAMING_SNAKE_CASE							:    str							=							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[Any]							=							ids_tensor([batch_size, 4]    ,		model.text_model.config.vocab_size						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							random_attention_mask([batch_size, 4]						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							{'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

												return model, inputs


						def        __A       (       self	:				int    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[str]							=							TFViTModel(UpperCamelCase__    ,		name='''vision_model'''						)
												SCREAMING_SNAKE_CASE							:    int							=							TFBertModel(UpperCamelCase__    ,		name='''text_model'''						)
												return vision_model, text_model



						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    List[Any]							=							TFViTModelTester(self						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							TFBertModelTester(self						)
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							vit_model_tester.prepare_config_and_inputs()
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							bert_model_tester.prepare_config_and_inputs()

												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    int							=							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
												)				,  
												)							:    Optional[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):


						def        __A       (       self	:				List[Any]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							TFVisionTextDualEncoderModel.from_vision_text_pretrained(
												    '''Rocketknight1/tiny-random-deit-tf'''    ,		'''hf-internal-testing/tiny-random-roberta'''						)
												SCREAMING_SNAKE_CASE							:    int							=							13
												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							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							:    Any							=							ids_tensor([batch_size, 4]    ,		model.text_model.config.vocab_size						)
												SCREAMING_SNAKE_CASE							:    int							=							random_attention_mask([batch_size, 4]						)
												SCREAMING_SNAKE_CASE							:    int							=							{'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

												return model, inputs


						def        __A       (       self	:				List[Any]    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Union[str, Any]    ,		UpperCamelCase__	:				List[Any]    ,		UpperCamelCase__	:				Optional[Any]    ,		UpperCamelCase__	:				Any=None    ,		**UpperCamelCase__	:				Any						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    str							=							self.get_vision_text_model(UpperCamelCase__    ,		UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							TFVisionTextDualEncoderModel(vision_model=UpperCamelCase__    ,		text_model=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Union[str, Any]							=							model(
												    input_ids=UpperCamelCase__    ,		pixel_values=UpperCamelCase__    ,		attention_mask=UpperCamelCase__    ,		output_attentions=UpperCamelCase__						)

												SCREAMING_SNAKE_CASE							:    Optional[int]							=							output.vision_model_output.attentions
												self.assertEqual(len(UpperCamelCase__						)    ,		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							:    Union[str, Any]							=							to_atuple(vision_model.config.image_size						)
												SCREAMING_SNAKE_CASE							:    List[Any]							=							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							:    str							=							output.text_model_output.attentions
												self.assertEqual(len(UpperCamelCase__						)    ,		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        __A       (       self	:				Optional[int]    ,		UpperCamelCase__	:				str    ,		UpperCamelCase__	:				str						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							TFDeiTModel(UpperCamelCase__    ,		name='''vision_model'''						)
												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TFRobertaModel(UpperCamelCase__    ,		name='''text_model'''						)
												return vision_model, text_model



						def        __A       (       self	:				List[str]						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Tuple							=							TFDeiTModelTester(self						)
												SCREAMING_SNAKE_CASE							:    str							=							TFRobertaModelTester(self						)
												SCREAMING_SNAKE_CASE							:    Tuple							=							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							:    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
												)				,  
												)							:    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):


						def        __A       (       self	:				int						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Optional[int]							=							TFVisionTextDualEncoderModel.from_vision_text_pretrained(
												    '''Rocketknight1/tiny-random-clip-tf'''    ,		'''hf-internal-testing/tiny-random-bert'''						)
												SCREAMING_SNAKE_CASE							:    int							=							13
												SCREAMING_SNAKE_CASE							:    str							=							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							:    Dict							=							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							:    List[Any]							=							{'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

												return model, inputs


						def        __A       (       self	:				Tuple    ,		UpperCamelCase__	:				int    ,		UpperCamelCase__	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    int							=							TFCLIPVisionModel(UpperCamelCase__    ,		name='''vision_model'''						)
												SCREAMING_SNAKE_CASE							:    str							=							TFBertModel(UpperCamelCase__    ,		name='''text_model'''						)
												return vision_model, text_model



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





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    Any							=							TFCLIPVisionModelTester(self						)
												SCREAMING_SNAKE_CASE							:    str							=							TFBertModelTester(self						)
												SCREAMING_SNAKE_CASE							:    Dict							=							clip_model_tester.prepare_config_and_inputs()
												SCREAMING_SNAKE_CASE							:    List[str]							=							bert_model_tester.prepare_config_and_inputs()

												SCREAMING_SNAKE_CASE				,  SCREAMING_SNAKE_CASE							:    Optional[int]							=							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
												)				,  
												)							:    str							=							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):


						@slow
						def        __A       (       self	:				Dict						):





												'''simple docstring'''





												SCREAMING_SNAKE_CASE							:    str							=							TFVisionTextDualEncoderModel.from_pretrained(
												    '''clip-italian/clip-italian'''    ,		logit_scale_init_value=1.0    ,		from_pt=UpperCamelCase__						)
												SCREAMING_SNAKE_CASE							:    str							=							VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''						)

												SCREAMING_SNAKE_CASE							:    List[str]							=							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							processor(
												    text=['''una foto di un gatto''', '''una foto di un cane''']    ,		images=UpperCamelCase__    ,		padding=UpperCamelCase__    ,		return_tensors='''np'''						)

												SCREAMING_SNAKE_CASE							:    Dict							=							model(**UpperCamelCase__						)

												# 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							:    Any							=							np.array([[1.228_4727, 0.310_4122]]						)

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






import pytest

from datasets.splits import SplitDict, SplitInfo
from datasets.utils.py_utils import asdict
@pytest.mark.parametrize(
    '''split_dict'''		,  [
        SplitDict(),
        SplitDict({'''train''': SplitInfo(name='''train'''		,  num_bytes=1_337		,  num_examples=42		,  dataset_name='''my_dataset'''						)}						),
        SplitDict({'''train''': SplitInfo(name='''train'''		,  num_bytes=1_337		,  num_examples=42						)}						),
        SplitDict({'''train''': SplitInfo()}						),
    ]		,  )
def       A	(    _lowercase						):
						SCREAMING_SNAKE_CASE							:    Tuple							=							split_dict._to_yaml_list()
						assert len(_lowercase						) == len(_lowercase						)
						SCREAMING_SNAKE_CASE							:    Tuple							=							SplitDict._from_yaml_list(_lowercase						)
						for split_name, split_info in split_dict.items():
												# dataset_name field is deprecated, and is therefore not part of the YAML dump
												SCREAMING_SNAKE_CASE							:    Any							=							None
												# the split name of split_dict takes over the name of the split info object
												SCREAMING_SNAKE_CASE							:    Optional[Any]							=							split_name
						assert split_dict == reloaded



@pytest.mark.parametrize(
    '''split_info'''		,  [SplitInfo(), SplitInfo(dataset_name=_lowercase						), SplitInfo(dataset_name='''my_dataset'''						)]						)
def       A	(    _lowercase						):
						# For backward compatibility, we need asdict(split_dict) to return split info dictrionaries with the "dataset_name"
						# field even if it's deprecated. This way old versionso of `datasets` can still reload dataset_infos.json files
						SCREAMING_SNAKE_CASE							:    List[Any]							=							asdict(SplitDict({'''train''': split_info}						)						)
						assert "dataset_name" in split_dict_asdict["train"]
						assert split_dict_asdict["train"]["dataset_name"] == split_info.dataset_name





