

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_     (							UpperCAmelCase_							:     int	)						->				Any:




	'''simple docstring'''




	if isinstance(UpperCAmelCase_							,       collections.abc.Iterable	):
		return x
	return (x, x)







@require_tf
class 	lowercase    :


		"""simple docstring"""




		def 		A__					(				self	,       __snake_case	,       __snake_case):
			pass


		def 		A__					(				self):
			pass


		def 		A__					(				self):
			pass


		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		int			=						VisionTextDualEncoderConfig.from_vision_text_configs(__snake_case	,       __snake_case)

			_UpperCamelCase   :		int			=						TFVisionTextDualEncoderModel(__snake_case)

			_UpperCamelCase   :		str			=						model(input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case)

			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	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		Optional[Any]			=						self.get_vision_text_model(__snake_case	,       __snake_case)
			_UpperCamelCase   :		List[str]			=						TFVisionTextDualEncoderModel(vision_model=__snake_case	,       text_model=__snake_case)

			_UpperCamelCase   :		str			=						model(input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case)

			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	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		List[Any]			=						self.get_vision_text_model(__snake_case	,       __snake_case)
			_UpperCamelCase   :		List[str]			=						{'vision_model': vision_model, 'text_model': text_model}
			_UpperCamelCase   :		Dict			=						TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__snake_case)

			_UpperCamelCase   :		List[Any]			=						model(input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case)

			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	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		Union[str, Any]			=						self.get_vision_text_model(__snake_case	,       __snake_case)
			_UpperCamelCase   :		List[Any]			=						TFVisionTextDualEncoderModel(vision_model=__snake_case	,       text_model=__snake_case)

			_UpperCamelCase   :		Optional[Any]			=						model(input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case)
			_UpperCamelCase   :		int			=						output[0].numpy()

			with tempfile.TemporaryDirectory() as tmpdirname:
				model.save_pretrained(__snake_case)
				_UpperCamelCase   :		Optional[int]			=						TFVisionTextDualEncoderModel.from_pretrained(__snake_case)

				_UpperCamelCase   :		Dict			=						model(input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case)
				_UpperCamelCase   :		str			=						after_output[0].numpy()
				_UpperCamelCase   :		Optional[int]			=						np.amax(np.abs(out_a - out_a))
				self.assertLessEqual(__snake_case	,       1e-5)


		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		List[Any]			=						self.get_vision_text_model(__snake_case	,       __snake_case)
			_UpperCamelCase   :		Tuple			=						TFVisionTextDualEncoderModel(vision_model=__snake_case	,       text_model=__snake_case)

			_UpperCamelCase   :		Tuple			=						model(
			    input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case	,       output_attentions=__snake_case)

			_UpperCamelCase   :		str			=						output.vision_model_output.attentions
			self.assertEqual(len(__snake_case)	,       vision_config.num_hidden_layers)

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

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


		def 		A__					(				self):
			_UpperCamelCase   :		str			=						self.prepare_config_and_inputs()
			self.check_vision_text_dual_encoder_model(**__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Optional[Any]			=						self.prepare_config_and_inputs()
			self.check_model_from_pretrained_configs(**__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Optional[Any]			=						self.prepare_config_and_inputs()
			self.check_vision_text_dual_encoder_from_pretrained(**__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Dict			=						self.prepare_config_and_inputs()
			self.check_save_load(**__snake_case)


		def 		A__					(				self):
			_UpperCamelCase   :		Tuple			=						self.prepare_config_and_inputs()
			self.check_vision_text_output_attention(**__snake_case)






		@slow
		def 		A__					(				self):
			_UpperCamelCase   :		Optional[Any]			=						self.get_pretrained_model_and_inputs()

			_UpperCamelCase   :		Optional[Any]			=						model_a(**__snake_case)
			_UpperCamelCase   :		str			=						outputs[0].numpy()

			with tempfile.TemporaryDirectory() as tmp_dirname:
				model_a.save_pretrained(__snake_case)
				_UpperCamelCase   :		Union[str, Any]			=						TFVisionTextDualEncoderModel.from_pretrained(__snake_case)

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







@require_tf
class 	lowercase    (				_lowercase    ,							unittest.TestCase							):


		"""simple docstring"""




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

			return model, inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case):
			_UpperCamelCase   :		Tuple			=						TFViTModel(__snake_case	,       name='vision_model')
			_UpperCamelCase   :		Dict			=						TFBertModel(__snake_case	,       name='text_model')
			return vision_model, text_model






		def 		A__					(				self):
			_UpperCamelCase   :		Tuple			=						TFViTModelTester(self)
			_UpperCamelCase   :		int			=						TFBertModelTester(self)
			_UpperCamelCase   :		List[Any]			=						vit_model_tester.prepare_config_and_inputs()
			_UpperCamelCase   :		Dict			=						bert_model_tester.prepare_config_and_inputs()

			_UpperCamelCase   :		Tuple			=						vision_config_and_inputs

			(
			    _UpperCamelCase		
			)   :		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 	lowercase    (				_lowercase    ,							unittest.TestCase							):


		"""simple docstring"""




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

			return model, inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case=None	,       **__snake_case):
			_UpperCamelCase   :		Optional[int]			=						self.get_vision_text_model(__snake_case	,       __snake_case)
			_UpperCamelCase   :		Tuple			=						TFVisionTextDualEncoderModel(vision_model=__snake_case	,       text_model=__snake_case)

			_UpperCamelCase   :		List[str]			=						model(
			    input_ids=__snake_case	,       pixel_values=__snake_case	,       attention_mask=__snake_case	,       output_attentions=__snake_case)

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

			_UpperCamelCase   :		Tuple			=						output.text_model_output.attentions
			self.assertEqual(len(__snake_case)	,       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	,       __snake_case	,       __snake_case):
			_UpperCamelCase   :		str			=						TFDeiTModel(__snake_case	,       name='vision_model')
			_UpperCamelCase   :		Dict			=						TFRobertaModel(__snake_case	,       name='text_model')
			return vision_model, text_model






		def 		A__					(				self):
			_UpperCamelCase   :		List[str]			=						TFDeiTModelTester(self)
			_UpperCamelCase   :		int			=						TFRobertaModelTester(self)
			_UpperCamelCase   :		int			=						vit_model_tester.prepare_config_and_inputs()
			_UpperCamelCase   :		Optional[Any]			=						bert_model_tester.prepare_config_and_inputs()

			_UpperCamelCase   :		str			=						vision_config_and_inputs

			(
			    _UpperCamelCase		
			)   :		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    (				_lowercase    ,							unittest.TestCase							):


		"""simple docstring"""




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

			return model, inputs


		def 		A__					(				self	,       __snake_case	,       __snake_case):
			_UpperCamelCase   :		Dict			=						TFCLIPVisionModel(__snake_case	,       name='vision_model')
			_UpperCamelCase   :		Tuple			=						TFBertModel(__snake_case	,       name='text_model')
			return vision_model, text_model






		def 		A__					(				self):
			_UpperCamelCase   :		int			=						TFCLIPVisionModelTester(self)
			_UpperCamelCase   :		str			=						TFBertModelTester(self)
			_UpperCamelCase   :		Union[str, Any]			=						clip_model_tester.prepare_config_and_inputs()
			_UpperCamelCase   :		Tuple			=						bert_model_tester.prepare_config_and_inputs()

			_UpperCamelCase   :		List[str]			=						vision_config_and_inputs

			(
			    _UpperCamelCase		
			)   :		Optional[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_vision
@require_tf
class 	lowercase    (				unittest.TestCase							):


		"""simple docstring"""




		@slow
		def 		A__					(				self):
			_UpperCamelCase   :		Tuple			=						TFVisionTextDualEncoderModel.from_pretrained(
			    'clip-italian/clip-italian'	,       logit_scale_init_value=1.0	,       from_pt=__snake_case)
			_UpperCamelCase   :		str			=						VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian')

			_UpperCamelCase   :		Dict			=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png')
			_UpperCamelCase   :		str			=						processor(
			    text=['una foto di un gatto', 'una foto di un cane']	,       images=__snake_case	,       padding=__snake_case	,       return_tensors='np')

			_UpperCamelCase   :		Union[str, Any]			=						model(**__snake_case)

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

			_UpperCamelCase   :		List[Any]			=						np.array([[1.2_2_8_4_7_2_7, 0.3_1_0_4_1_2_2]])

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







# tests directory-specific settings - this file is run automatically
# by pytest before any tests are run

import doctest
import sys
import warnings
from os.path import abspath, dirname, join

import _pytest

from transformers.testing_utils import HfDoctestModule, HfDocTestParser


# allow having multiple repository checkouts and not needing to remember to rerun
# 'pip install -e .[dev]' when switching between checkouts and running tests.
lowerCAmelCase__								=		abspath(join(dirname(__file__), """src"""))
sys.path.insert(1, git_repo_path)

# silence FutureWarning warnings in tests since often we can't act on them until
# they become normal warnings - i.e. the tests still need to test the current functionality
warnings.simplefilter(action="""ignore""", category=FutureWarning)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Any	)						->				Union[str, Any]:




	'''simple docstring'''




	config.addinivalue_line(
	    'markers'							,       'is_pt_tf_cross_test: mark test to run only when PT and TF interactions are tested'	)
	config.addinivalue_line(
	    'markers'							,       'is_pt_flax_cross_test: mark test to run only when PT and FLAX interactions are tested'	)
	config.addinivalue_line('markers'							,       'is_pipeline_test: mark test to run only when pipelines are tested'	)
	config.addinivalue_line('markers'							,       'is_staging_test: mark test to run only in the staging environment'	)
	config.addinivalue_line('markers'							,       'accelerate_tests: mark test that require accelerate'	)
	config.addinivalue_line('markers'							,       'tool_tests: mark the tool tests that are run on their specific schedule'	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Optional[int]	)						->				List[Any]:




	'''simple docstring'''




	from transformers.testing_utils import pytest_addoption_shared

	pytest_addoption_shared(UpperCAmelCase_	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Any	)						->				List[Any]:




	'''simple docstring'''




	from transformers.testing_utils import pytest_terminal_summary_main

	_UpperCamelCase   :		str			=						terminalreporter.config.getoption('--make-reports'	)
	if make_reports:
		pytest_terminal_summary_main(UpperCAmelCase_							,       id=UpperCAmelCase_	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Dict	)						->				Tuple:




	'''simple docstring'''




	if exitstatus == 5:
		_UpperCamelCase   :		List[Any]			=						0


# Doctest custom flag to ignore output.
lowerCAmelCase__								=		doctest.register_optionflag("""IGNORE_RESULT""")

lowerCAmelCase__								=		doctest.OutputChecker







class 	lowercase    (				_lowercase							):


		"""simple docstring"""




		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case):
			if IGNORE_RESULT & optionflags:
				return True
			return OutputChecker.check_output(self	,       __snake_case	,       __snake_case	,       __snake_case)

lowerCAmelCase__								=		CustomOutputChecker
lowerCAmelCase__								=		HfDoctestModule







lowerCAmelCase__								=		HfDocTestParser




