



"""simple docstring"""





import inspect
import unittest

from transformers import YolosConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import YolosForObjectDetection, YolosModel
							from transformers.models.yolos.modeling_yolos import YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import AutoImageProcessor
class 		SCREAMING_SNAKE_CASE_							:
				"""simple docstring"""




				def __init__(     self  :Union[str, Any]						,    __lowercase  :Optional[int]						,    __lowercase  :Any=13						,    __lowercase  :Optional[Any]=[30, 30]						,    __lowercase  :Union[str, Any]=2						,    __lowercase  :Union[str, Any]=3						,    __lowercase  :str=True						,    __lowercase  :Tuple=True						,    __lowercase  :Any=32						,    __lowercase  :int=5						,    __lowercase  :Optional[Any]=4						,    __lowercase  :List[str]=37						,    __lowercase  :Optional[Any]="gelu"						,    __lowercase  :List[Any]=0.1						,    __lowercase  :Tuple=0.1						,    __lowercase  :Any=10						,    __lowercase  :Union[str, Any]=0.02						,    __lowercase  :int=3						,    __lowercase  :Any=None						,    __lowercase  :List[Any]=8						,    __lowercase  :int=10						,    ):
											__lowerCamelCase      :      Tuple         =parent
											__lowerCamelCase      :      Any         =batch_size
											__lowerCamelCase      :      int         =image_size
											__lowerCamelCase      :      Dict         =patch_size
											__lowerCamelCase      :      Union[str, Any]         =num_channels
											__lowerCamelCase      :      Tuple         =is_training
											__lowerCamelCase      :      Any         =use_labels
											__lowerCamelCase      :      Optional[Any]         =hidden_size
											__lowerCamelCase      :      List[str]         =num_hidden_layers
											__lowerCamelCase      :      Optional[Any]         =num_attention_heads
											__lowerCamelCase      :      int         =intermediate_size
											__lowerCamelCase      :      Dict         =hidden_act
											__lowerCamelCase      :      List[Any]         =hidden_dropout_prob
											__lowerCamelCase      :      List[Any]         =attention_probs_dropout_prob
											__lowerCamelCase      :      Tuple         =type_sequence_label_size
											__lowerCamelCase      :      Dict         =initializer_range
											__lowerCamelCase      :      List[str]         =num_labels
											__lowerCamelCase      :      Tuple         =scope
											__lowerCamelCase      :      Optional[int]         =n_targets
											__lowerCamelCase      :      Tuple         =num_detection_tokens
											# we set the expected sequence length (which is used in several tests)
											# expected sequence length = num_patches + 1 (we add 1 for the [CLS] token) + num_detection_tokens
											__lowerCamelCase      :      List[Any]         =(image_size[1] // patch_size) * (image_size[0] // patch_size)
											__lowerCamelCase      :      List[Any]         =num_patches + 1 + self.num_detection_tokens




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      Tuple         =floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]			)

											__lowerCamelCase      :      Union[str, Any]         =None
											if self.use_labels:
																		# labels is a list of Dict (each Dict being the labels for a given example in the batch)
																		__lowerCamelCase      :      Optional[int]         =[]
																		for i in range(self.batch_size			):
																									__lowerCamelCase      :      int         ={}
																									__lowerCamelCase      :      Dict         =torch.randint(
																									    high=self.num_labels						,    size=(self.n_targets,)						,    device=__lowercase			)
																									__lowerCamelCase      :      Any         =torch.rand(self.n_targets						,    4						,    device=__lowercase			)
																									labels.append(__lowercase			)

											__lowerCamelCase      :      Dict         =self.get_config()

											return config, pixel_values, labels




				def 					__lowercase  (     self  :List[Any]			):
											return YolosConfig(
											    image_size=self.image_size						,    patch_size=self.patch_size						,    num_channels=self.num_channels						,    hidden_size=self.hidden_size						,    num_hidden_layers=self.num_hidden_layers						,    num_attention_heads=self.num_attention_heads						,    intermediate_size=self.intermediate_size						,    hidden_act=self.hidden_act						,    hidden_dropout_prob=self.hidden_dropout_prob						,    attention_probs_dropout_prob=self.attention_probs_dropout_prob						,    is_decoder=__lowercase						,    initializer_range=self.initializer_range						,    num_detection_tokens=self.num_detection_tokens						,    num_labels=self.num_labels						,    )




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Any						,    __lowercase  :Union[str, Any]						,    __lowercase  :List[str]			):
											__lowerCamelCase      :      Optional[int]         =YolosModel(config=__lowercase			)
											model.to(__lowercase			)
											model.eval()
											__lowerCamelCase      :      Optional[Any]         =model(__lowercase			)
											self.parent.assertEqual(
											    result.last_hidden_state.shape						,    (self.batch_size, self.expected_seq_len, self.hidden_size)			)




				def 					__lowercase  (     self  :Union[str, Any]						,    __lowercase  :Optional[int]						,    __lowercase  :Any						,    __lowercase  :str			):
											__lowerCamelCase      :      Union[str, Any]         =YolosForObjectDetection(__lowercase			)
											model.to(__lowercase			)
											model.eval()

											__lowerCamelCase      :      Union[str, Any]         =model(pixel_values=__lowercase			)
											__lowerCamelCase      :      int         =model(__lowercase			)

											self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)			)
											self.parent.assertEqual(result.pred_boxes.shape						,    (self.batch_size, self.num_detection_tokens, 4)			)

											__lowerCamelCase      :      int         =model(pixel_values=__lowercase						,    labels=__lowercase			)

											self.parent.assertEqual(result.loss.shape						,    ()			)
											self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_detection_tokens, self.num_labels + 1)			)
											self.parent.assertEqual(result.pred_boxes.shape						,    (self.batch_size, self.num_detection_tokens, 4)			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Dict         =self.prepare_config_and_inputs()
											__lowerCamelCase      :      List[Any]         =config_and_inputs
											__lowerCamelCase      :      Tuple         ={'''pixel_values''': pixel_values}
											return config, inputs_dict

@require_torch
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       List[str]	 =		(YolosModel, YolosForObjectDetection) if is_torch_available() else ()
				__snake_case   :       Any	 =		(
				    {"""feature-extraction""": YolosModel, """object-detection""": YolosForObjectDetection} if is_torch_available() else {}
				)

				__snake_case   :       Optional[int]	 =		False
				__snake_case   :       List[str]	 =		False
				__snake_case   :       Union[str, Any]	 =		False
				__snake_case   :       Optional[int]	 =		False




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Any						,    __lowercase  :Tuple=False			):
											__lowerCamelCase      :      str         =super()._prepare_for_class(__lowercase						,    __lowercase						,    return_labels=__lowercase			)

											if return_labels:
																		if model_class.__name__ == "YolosForObjectDetection":
																									__lowerCamelCase      :      str         =[]
																									for i in range(self.model_tester.batch_size			):
																																__lowerCamelCase      :      List[str]         ={}
																																__lowerCamelCase      :      int         =torch.ones(
																																    size=(self.model_tester.n_targets,)						,    device=__lowercase						,    dtype=torch.long			)
																																__lowerCamelCase      :      Optional[int]         =torch.ones(
																																    self.model_tester.n_targets						,    4						,    device=__lowercase						,    dtype=torch.float			)
																																labels.append(__lowercase			)
																									__lowerCamelCase      :      int         =labels

											return inputs_dict




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Tuple         =YolosModelTester(self			)
											__lowerCamelCase      :      int         =ConfigTester(self						,    config_class=__lowercase						,    has_text_modality=__lowercase						,    hidden_size=37			)




				def 					__lowercase  (     self  :Tuple			):
											self.config_tester.run_common_tests()




				def 					__lowercase  (     self  :List[Any]			):
											# YOLOS does not use inputs_embeds
											pass




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      List[str]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		self.assertIsInstance(model.get_input_embeddings()						,    (nn.Module)			)
																		__lowerCamelCase      :      Dict         =model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(__lowercase						,    nn.Linear			)			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      Union[str, Any]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		__lowerCamelCase      :      Any         =inspect.signature(model.forward			)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		__lowerCamelCase      :      Optional[Any]         =[*signature.parameters.keys()]

																		__lowerCamelCase      :      Optional[Any]         =['''pixel_values''']
																		self.assertListEqual(arg_names[:1]						,    __lowercase			)




				def 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Any         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__lowercase			)




				def 					__lowercase  (     self  :Optional[int]			):
											__lowerCamelCase      :      Dict         =self.model_tester.prepare_config_and_inputs_for_common()
											__lowerCamelCase      :      Union[str, Any]         =True

											# in YOLOS, the seq_len is different
											__lowerCamelCase      :      List[str]         =self.model_tester.expected_seq_len
											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      int         =True
																		__lowerCamelCase      :      Tuple         =False
																		__lowerCamelCase      :      Any         =True
																		__lowerCamelCase      :      List[Any]         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      str         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)
																		__lowerCamelCase      :      str         =outputs.attentions
																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)

																		# check that output_attentions also work using config
																		del inputs_dict["output_attentions"]
																		__lowerCamelCase      :      int         =True
																		__lowerCamelCase      :      int         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      Any         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)
																		__lowerCamelCase      :      Optional[int]         =outputs.attentions
																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)

																		self.assertListEqual(
																		    list(attentions[0].shape[-3:]			)						,    [self.model_tester.num_attention_heads, seq_len, seq_len]						,    )
																		__lowerCamelCase      :      Optional[Any]         =len(__lowercase			)

																		# Check attention is always last and order is fine
																		__lowerCamelCase      :      List[str]         =True
																		__lowerCamelCase      :      Union[str, Any]         =True
																		__lowerCamelCase      :      Union[str, Any]         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()
																		with torch.no_grad():
																									__lowerCamelCase      :      Optional[int]         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)

																		__lowerCamelCase      :      Union[str, Any]         =1
																		self.assertEqual(out_len + added_hidden_states						,    len(__lowercase			)			)

																		__lowerCamelCase      :      Union[str, Any]         =outputs.attentions

																		self.assertEqual(len(__lowercase			)						,    self.model_tester.num_hidden_layers			)
																		self.assertListEqual(
																		    list(self_attentions[0].shape[-3:]			)						,    [self.model_tester.num_attention_heads, seq_len, seq_len]						,    )




				def 					__lowercase  (     self  :Tuple			):
											def check_hidden_states_output(__lowercase  :Optional[Any]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Union[str, Any]			):
																		__lowerCamelCase      :      Tuple         =model_class(__lowercase			)
																		model.to(__lowercase			)
																		model.eval()

																		with torch.no_grad():
																									__lowerCamelCase      :      Any         =model(**self._prepare_for_class(__lowercase						,    __lowercase			)			)

																		__lowerCamelCase      :      List[str]         =outputs.hidden_states

																		__lowerCamelCase      :      List[str]         =getattr(
																		    self.model_tester						,    '''expected_num_hidden_layers'''						,    self.model_tester.num_hidden_layers + 1			)
																		self.assertEqual(len(__lowercase			)						,    __lowercase			)

																		# YOLOS has a different seq_length
																		__lowerCamelCase      :      List[Any]         =self.model_tester.expected_seq_len

																		self.assertListEqual(
																		    list(hidden_states[0].shape[-2:]			)						,    [seq_length, self.model_tester.hidden_size]						,    )

											__lowerCamelCase      :      Optional[Any]         =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		__lowerCamelCase      :      Optional[int]         =True
																		check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		__lowerCamelCase      :      Optional[Any]         =True

																		check_hidden_states_output(__lowercase						,    __lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      List[str]         =self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_object_detection(*__lowercase			)




				@slow
				def 					__lowercase  (     self  :Optional[Any]			):
											for model_name in YOLOS_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		__lowerCamelCase      :      List[str]         =YolosModel.from_pretrained(__lowercase			)
																		self.assertIsNotNone(__lowercase			)



def 				lowerCAmelCase_			(      ):


							'''simple docstring'''







							__lowerCamelCase      :      Dict         =Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
							return image
@require_torch
@require_vision
class 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				@cached_property
				def 					__lowercase  (     self  :Optional[Any]			):
											return AutoImageProcessor.from_pretrained('''hustvl/yolos-small'''			) if is_vision_available() else None




				@slow
				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      int         =YolosForObjectDetection.from_pretrained('''hustvl/yolos-small'''			).to(__lowercase			)

											__lowerCamelCase      :      int         =self.default_image_processor
											__lowerCamelCase      :      Optional[int]         =prepare_img()
											__lowerCamelCase      :      Any         =image_processor(images=__lowercase						,    return_tensors='''pt'''			).to(__lowercase			)

											# forward pass
											with torch.no_grad():
																		__lowerCamelCase      :      Optional[int]         =model(inputs.pixel_values			)

											# verify outputs
											__lowerCamelCase      :      int         =torch.Size((1, 100, 92)			)
											self.assertEqual(outputs.logits.shape						,    __lowercase			)

											__lowerCamelCase      :      Optional[Any]         =torch.tensor(
											    [[-24.0248, -10.3024, -14.8290], [-42.0392, -16.8200, -27.4334], [-27.2743, -11.8154, -18.7148]]						,    device=__lowercase						,    )
											__lowerCamelCase      :      List[str]         =torch.tensor(
											    [[0.2559, 0.5455, 0.4706], [0.2989, 0.7279, 0.1875], [0.7732, 0.4017, 0.4462]]						,    device=__lowercase			)
											self.assertTrue(torch.allclose(outputs.logits[0, :3, :3]						,    __lowercase						,    atol=1e-4			)			)
											self.assertTrue(torch.allclose(outputs.pred_boxes[0, :3, :3]						,    __lowercase						,    atol=1e-4			)			)

											# verify postprocessing
											__lowerCamelCase      :      Optional[Any]         =image_processor.post_process_object_detection(
											    __lowercase						,    threshold=0.3						,    target_sizes=[image.size[::-1]]			)[0]
											__lowerCamelCase      :      Dict         =torch.tensor([0.9994, 0.9790, 0.9964, 0.9972, 0.9861]			).to(__lowercase			)
											__lowerCamelCase      :      Optional[Any]         =[75, 75, 17, 63, 17]
											__lowerCamelCase      :      Optional[Any]         =torch.tensor([335.0609, 79.3848, 375.4216, 187.2495]			).to(__lowercase			)

											self.assertEqual(len(results['''scores''']			)						,    5			)
											self.assertTrue(torch.allclose(results['''scores''']						,    __lowercase						,    atol=1e-4			)			)
											self.assertSequenceEqual(results['''labels'''].tolist()						,    __lowercase			)
											self.assertTrue(torch.allclose(results['''boxes'''][0, :]						,    __lowercase			)			)











"""simple docstring"""





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 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE       :							Optional[int]   ):


							'''simple docstring'''







							if isinstance(SCREAMING_SNAKE_CASE    ,						collections.abc.Iterable   ):
														return x
							return (x, x)
@require_tf
class 		SCREAMING_SNAKE_CASE_							:
				"""simple docstring"""




				def 					__lowercase  (     self  :Optional[int]						,    __lowercase  :Union[str, Any]						,    __lowercase  :List[Any]			):
											pass




				def 					__lowercase  (     self  :Any			):
											pass




				def 					__lowercase  (     self  :str			):
											pass




				def 					__lowercase  (     self  :str						,    __lowercase  :Dict						,    __lowercase  :List[Any]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Any=None						,    **__lowercase  :Tuple			):
											__lowerCamelCase      :      Optional[Any]         =VisionTextDualEncoderConfig.from_vision_text_configs(__lowercase						,    __lowercase			)

											__lowerCamelCase      :      Dict         =TFVisionTextDualEncoderModel(__lowercase			)

											__lowerCamelCase      :      List[Any]         =model(input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase			)

											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 					__lowercase  (     self  :str						,    __lowercase  :Any						,    __lowercase  :Dict						,    __lowercase  :Any						,    __lowercase  :int						,    __lowercase  :str=None						,    **__lowercase  :List[str]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Any         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      List[str]         =TFVisionTextDualEncoderModel(vision_model=__lowercase						,    text_model=__lowercase			)

											__lowerCamelCase      :      Optional[int]         =model(input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase			)

											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 					__lowercase  (     self  :int						,    __lowercase  :Dict						,    __lowercase  :List[Any]						,    __lowercase  :Any						,    __lowercase  :Optional[Any]						,    __lowercase  :Tuple=None						,    **__lowercase  :List[Any]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      int         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      Dict         ={'''vision_model''': vision_model, '''text_model''': text_model}
											__lowerCamelCase      :      List[Any]         =TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowercase			)

											__lowerCamelCase      :      Optional[Any]         =model(input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase			)

											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 					__lowercase  (     self  :Optional[Any]						,    __lowercase  :Dict						,    __lowercase  :int						,    __lowercase  :Dict						,    __lowercase  :List[Any]						,    __lowercase  :Tuple=None						,    **__lowercase  :Optional[Any]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Tuple         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      List[Any]         =TFVisionTextDualEncoderModel(vision_model=__lowercase						,    text_model=__lowercase			)

											__lowerCamelCase      :      Any         =model(input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase			)
											__lowerCamelCase      :      Optional[Any]         =output[0].numpy()

											with tempfile.TemporaryDirectory() as tmpdirname:
																		model.save_pretrained(__lowercase			)
																		__lowerCamelCase      :      Dict         =TFVisionTextDualEncoderModel.from_pretrained(__lowercase			)

																		__lowerCamelCase      :      Optional[Any]         =model(input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase			)
																		__lowerCamelCase      :      Any         =after_output[0].numpy()
																		__lowerCamelCase      :      Tuple         =np.amax(np.abs(out_a - out_a			)			)
																		self.assertLessEqual(__lowercase						,    1e-5			)




				def 					__lowercase  (     self  :int						,    __lowercase  :Any						,    __lowercase  :List[Any]						,    __lowercase  :Any						,    __lowercase  :str						,    __lowercase  :int=None						,    **__lowercase  :List[Any]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Any         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      Union[str, Any]         =TFVisionTextDualEncoderModel(vision_model=__lowercase						,    text_model=__lowercase			)

											__lowerCamelCase      :      Any         =model(
											    input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase						,    output_attentions=__lowercase			)

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

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

											__lowerCamelCase      :      List[Any]         =output.text_model_output.attentions
											self.assertEqual(len(__lowercase			)						,    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 					__lowercase  (     self  :List[Any]						,    __lowercase  :np.ndarray						,    __lowercase  :np.ndarray						,    __lowercase  :float			):
											__lowerCamelCase      :      Dict         =np.abs((a - b)			).max()
											self.assertLessEqual(__lowercase						,    __lowercase						,    f'Difference between torch and flax is {diff} (>= {tol}).'			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      Any         =self.prepare_config_and_inputs()
											self.check_vision_text_dual_encoder_model(**__lowercase			)




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      List[Any]         =self.prepare_config_and_inputs()
											self.check_model_from_pretrained_configs(**__lowercase			)




				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      Dict         =self.prepare_config_and_inputs()
											self.check_vision_text_dual_encoder_from_pretrained(**__lowercase			)




				def 					__lowercase  (     self  :Any			):
											__lowerCamelCase      :      List[Any]         =self.prepare_config_and_inputs()
											self.check_save_load(**__lowercase			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      Any         =self.prepare_config_and_inputs()
											self.check_vision_text_output_attention(**__lowercase			)




				@slow
				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Optional[Any]         =self.get_pretrained_model_and_inputs()

											__lowerCamelCase      :      Optional[Any]         =model_a(**__lowercase			)
											__lowerCamelCase      :      int         =outputs[0].numpy()

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

																		__lowerCamelCase      :      Tuple         =model_a(**__lowercase			)
																		__lowerCamelCase      :      Optional[int]         =after_outputs[0].numpy()
																		__lowerCamelCase      :      Optional[Any]         =np.amax(np.abs(out_a - out_a			)			)
																		self.assertLessEqual(__lowercase						,    1e-5			)

@require_tf
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""




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

											return model, inputs




				def 					__lowercase  (     self  :Optional[Any]						,    __lowercase  :List[Any]						,    __lowercase  :List[str]			):
											__lowerCamelCase      :      Optional[Any]         =TFViTModel(__lowercase						,    name='''vision_model'''			)
											__lowerCamelCase      :      Union[str, Any]         =TFBertModel(__lowercase						,    name='''text_model'''			)
											return vision_model, text_model




				def 					__lowercase  (     self  :Union[str, Any]			):
											__lowerCamelCase      :      str         =TFViTModelTester(self			)
											__lowerCamelCase      :      Any         =TFBertModelTester(self			)
											__lowerCamelCase      :      Optional[Any]         =vit_model_tester.prepare_config_and_inputs()
											__lowerCamelCase      :      Tuple         =bert_model_tester.prepare_config_and_inputs()

											__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase      :      Dict         =vision_config_and_inputs

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      int         =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 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""




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

											return model, inputs




				def 					__lowercase  (     self  :Any						,    __lowercase  :Tuple						,    __lowercase  :List[Any]						,    __lowercase  :Union[str, Any]						,    __lowercase  :Any						,    __lowercase  :int=None						,    **__lowercase  :List[str]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      Union[str, Any]         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      Optional[int]         =TFVisionTextDualEncoderModel(vision_model=__lowercase						,    text_model=__lowercase			)

											__lowerCamelCase      :      Any         =model(
											    input_ids=__lowercase						,    pixel_values=__lowercase						,    attention_mask=__lowercase						,    output_attentions=__lowercase			)

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

											__lowerCamelCase      :      Any         =output.text_model_output.attentions
											self.assertEqual(len(__lowercase			)						,    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 					__lowercase  (     self  :Any						,    __lowercase  :Any						,    __lowercase  :Optional[Any]			):
											__lowerCamelCase      :      str         =TFDeiTModel(__lowercase						,    name='''vision_model'''			)
											__lowerCamelCase      :      List[str]         =TFRobertaModel(__lowercase						,    name='''text_model'''			)
											return vision_model, text_model




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      Optional[int]         =TFDeiTModelTester(self			)
											__lowerCamelCase      :      Any         =TFRobertaModelTester(self			)
											__lowerCamelCase      :      Dict         =vit_model_tester.prepare_config_and_inputs()
											__lowerCamelCase      :      Optional[int]         =bert_model_tester.prepare_config_and_inputs()

											__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase      :      Optional[int]         =vision_config_and_inputs

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      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 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""




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

											return model, inputs




				def 					__lowercase  (     self  :List[Any]						,    __lowercase  :str						,    __lowercase  :Tuple			):
											__lowerCamelCase      :      int         =TFCLIPVisionModel(__lowercase						,    name='''vision_model'''			)
											__lowerCamelCase      :      Optional[Any]         =TFBertModel(__lowercase						,    name='''text_model'''			)
											return vision_model, text_model




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      List[str]         =TFCLIPVisionModelTester(self			)
											__lowerCamelCase      :      Union[str, Any]         =TFBertModelTester(self			)
											__lowerCamelCase      :      str         =clip_model_tester.prepare_config_and_inputs()
											__lowerCamelCase      :      Tuple         =bert_model_tester.prepare_config_and_inputs()

											__lowerCamelCase    ,						__lowerCamelCase      :      Any         =vision_config_and_inputs

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      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 		SCREAMING_SNAKE_CASE_							(    unittest.TestCase   ):
				"""simple docstring"""




				@slow
				def 					__lowercase  (     self  :List[Any]			):
											__lowerCamelCase      :      Union[str, Any]         =TFVisionTextDualEncoderModel.from_pretrained(
											    '''clip-italian/clip-italian'''						,    logit_scale_init_value=1.0						,    from_pt=__lowercase			)
											__lowerCamelCase      :      str         =VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''			)

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

											__lowerCamelCase      :      str         =model(**__lowercase			)

											# 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])						,    )

											__lowerCamelCase      :      int         =np.array([[1.2284727, 0.3104122]]			)

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




