






import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor


class        __UpperCamelCase    (     unittest.TestCase   ):







	def __init__(  self      :							Dict       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							int=3       ,				_lowerCAmelCase      :							int=32       ,				_lowerCAmelCase      :							Any=3       ,				_lowerCAmelCase      :							Any=10       ,				_lowerCAmelCase      :							Union[str, Any]=[10, 20, 30, 40]       ,				_lowerCAmelCase      :							List[str]=[1, 1, 2, 1]       ,				_lowerCAmelCase      :							Dict=True       ,				_lowerCAmelCase      :							Optional[int]=True       ,				_lowerCAmelCase      :							Optional[Any]="relu"       ,				_lowerCAmelCase      :							Union[str, Any]=3       ,				_lowerCAmelCase      :							Tuple=None       ,				)		-> List[str]:
		"""simple docstring"""







		__lowercase             =  parent
		__lowercase             =  batch_size
		__lowercase             =  image_size
		__lowercase             =  num_channels
		__lowercase             =  embeddings_size
		__lowercase             =  hidden_sizes
		__lowercase             =  depths
		__lowercase             =  is_training
		__lowercase             =  use_labels
		__lowercase             =  hidden_act
		__lowercase             =  num_labels
		__lowercase             =  scope
		__lowercase             =  len(_lowerCAmelCase			)







	def  _a   (  self      :							Tuple			)		-> List[str]:
		"""simple docstring"""







		__lowercase             =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

		__lowercase             =  self.get_config()

		return config, pixel_values







	def  _a   (  self      :							Optional[Any]			)		-> int:
		"""simple docstring"""







		return RegNetConfig(
		    num_channels=self.num_channels       ,				embeddings_size=self.embeddings_size       ,				hidden_sizes=self.hidden_sizes       ,				depths=self.depths       ,				hidden_act=self.hidden_act       ,				num_labels=self.num_labels       ,				image_size=self.image_size       ,				)







	def  _a   (  self      :							Union[str, Any]       ,				_lowerCAmelCase      :							Tuple       ,				_lowerCAmelCase      :							str			)		-> Dict:
		"""simple docstring"""







		__lowercase             =  FlaxRegNetModel(config=_lowerCAmelCase			)
		__lowercase             =  model(_lowerCAmelCase			)

		# Output shape (b, c, h, w)
		self.parent.assertEqual(
		    result.last_hidden_state.shape       ,				(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)       ,				)







	def  _a   (  self      :							Tuple       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							int			)		-> Any:
		"""simple docstring"""







		__lowercase             =  self.num_labels
		__lowercase             =  FlaxRegNetForImageClassification(config=_lowerCAmelCase			)
		__lowercase             =  model(_lowerCAmelCase			)
		self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.num_labels)			)







	def  _a   (  self      :							Dict			)		-> str:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		__lowercase       ,						__lowercase             =  config_and_inputs
		__lowercase             =  {"""pixel_values""": pixel_values}
		return config, inputs_dict


@require_flax
class        __UpperCamelCase    (     _lowerCAmelCase   ,			unittest.TestCase   ):
	__snake_case	:Union[str, Any]   =       (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

	__snake_case	:Tuple   =       False
	__snake_case	:List[Any]   =       False
	__snake_case	:Dict   =       False







	def  _a   (  self      :							Any			)		-> None:
		"""simple docstring"""







		__lowercase             =  FlaxRegNetModelTester(self			)
		__lowercase             =  ConfigTester(self       ,				config_class=_lowerCAmelCase       ,				has_text_modality=_lowerCAmelCase			)







	def  _a   (  self      :							int			)		-> Dict:
		"""simple docstring"""







		self.create_and_test_config_common_properties()
		self.config_tester.create_and_test_config_to_json_string()
		self.config_tester.create_and_test_config_to_json_file()
		self.config_tester.create_and_test_config_from_and_save_pretrained()
		self.config_tester.create_and_test_config_with_num_labels()
		self.config_tester.check_config_can_be_init_without_params()
		self.config_tester.check_config_arguments_init()







	def  _a   (  self      :							Optional[int]			)		-> str:
		"""simple docstring"""







		return







	def  _a   (  self      :							Union[str, Any]			)		-> Optional[Any]:
		"""simple docstring"""







		__lowercase             =  self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*_lowerCAmelCase			)







	def  _a   (  self      :							int			)		-> str:
		"""simple docstring"""







		__lowercase             =  self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase			)







	@unittest.skip(reason="""RegNet does not use inputs_embeds"""			)
	def  _a   (  self      :							List[str]			)		-> int:
		"""simple docstring"""







		pass







	@unittest.skip(reason="""RegNet does not support input and output embeddings"""			)
	def  _a   (  self      :							Tuple			)		-> Dict:
		"""simple docstring"""







		pass







	def  _a   (  self      :							str			)		-> Tuple:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__lowercase             =  model_class(_lowerCAmelCase			)
			__lowercase             =  inspect.signature(model.__call__			)
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			__lowercase             =  [*signature.parameters.keys()]

			__lowercase             =  ["""pixel_values"""]
			self.assertListEqual(arg_names[:1]       ,				_lowerCAmelCase			)







	def  _a   (  self      :							Union[str, Any]			)		-> Optional[int]:
		"""simple docstring"""







		def check_hidden_states_output(_lowerCAmelCase      :							Dict       ,				_lowerCAmelCase      :							int       ,				_lowerCAmelCase      :							int			):
			__lowercase             =  model_class(_lowerCAmelCase			)

			__lowercase             =  model(**self._prepare_for_class(_lowerCAmelCase       ,				_lowerCAmelCase			)			)

			__lowercase             =  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

			__lowercase             =  self.model_tester.num_stages
			self.assertEqual(len(_lowerCAmelCase			)       ,				expected_num_stages + 1			)

		__lowercase       ,						__lowercase             =  self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			__lowercase             =  True
			check_hidden_states_output(_lowerCAmelCase       ,				_lowerCAmelCase       ,				_lowerCAmelCase			)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			__lowercase             =  True

			check_hidden_states_output(_lowerCAmelCase       ,				_lowerCAmelCase       ,				_lowerCAmelCase			)







	def  _a   (  self      :							List[Any]			)		-> Optional[int]:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			with self.subTest(model_class.__name__			):
				__lowercase             =  self._prepare_for_class(_lowerCAmelCase       ,				_lowerCAmelCase			)
				__lowercase             =  model_class(_lowerCAmelCase			)

				@jax.jit
				def model_jitted(_lowerCAmelCase      :							Any       ,				**_lowerCAmelCase      :							Optional[int]			):
					return model(pixel_values=_lowerCAmelCase       ,				**_lowerCAmelCase			)

				with self.subTest("""JIT Enabled"""			):
					__lowercase             =  model_jitted(**_lowerCAmelCase			).to_tuple()

				with self.subTest("""JIT Disabled"""			):
					with jax.disable_jit():
						__lowercase             =  model_jitted(**_lowerCAmelCase			).to_tuple()

				self.assertEqual(len(_lowerCAmelCase			)       ,				len(_lowerCAmelCase			)			)
				for jitted_output, output in zip(_lowerCAmelCase       ,				_lowerCAmelCase			):
					self.assertEqual(jitted_output.shape       ,				output.shape			)





def 				snake_case  (     ):




	'''simple docstring'''







	__lowercase             =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
	return image




@require_flax
class        __UpperCamelCase    (     unittest.TestCase   ):







	@cached_property
	def  _a   (  self      :							Dict			)		-> Tuple:
		"""simple docstring"""







		return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""			) if is_vision_available() else None







	@slow
	def  _a   (  self      :							str			)		-> Optional[int]:
		"""simple docstring"""







		__lowercase             =  FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""			)

		__lowercase             =  self.default_image_processor
		__lowercase             =  prepare_img()
		__lowercase             =  image_processor(images=_lowerCAmelCase       ,				return_tensors="""np"""			)

		__lowercase             =  model(**_lowerCAmelCase			)

		# verify the logits
		__lowercase             =  (1, 1000)
		self.assertEqual(outputs.logits.shape       ,				_lowerCAmelCase			)

		__lowercase             =  jnp.array([-0.4_180, -1.5_051, -3.4_836]			)

		self.assertTrue(jnp.allclose(outputs.logits[0, :3]       ,				_lowerCAmelCase       ,				atol=1e-4			)			)














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 				snake_case  (     lowerCamelCase					):




	'''simple docstring'''







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


@require_tf
class        __UpperCamelCase    :







	def  _a   (  self      :							List[Any]       ,				_lowerCAmelCase      :							str       ,				_lowerCAmelCase      :							Optional[Any]			)		-> Union[str, Any]:
		"""simple docstring"""







		pass







	def  _a   (  self      :							Dict			)		-> Optional[int]:
		"""simple docstring"""







		pass







	def  _a   (  self      :							Any			)		-> Optional[Any]:
		"""simple docstring"""







		pass







	def  _a   (  self      :							int       ,				_lowerCAmelCase      :							Optional[int]       ,				_lowerCAmelCase      :							Any       ,				_lowerCAmelCase      :							Any       ,				_lowerCAmelCase      :							Optional[Any]       ,				_lowerCAmelCase      :							Dict=None       ,				**_lowerCAmelCase      :							int			)		-> str:
		"""simple docstring"""







		__lowercase             =  VisionTextDualEncoderConfig.from_vision_text_configs(_lowerCAmelCase       ,				_lowerCAmelCase			)

		__lowercase             =  TFVisionTextDualEncoderModel(_lowerCAmelCase			)

		__lowercase             =  model(input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase			)

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







	def  _a   (  self      :							str       ,				_lowerCAmelCase      :							Tuple       ,				_lowerCAmelCase      :							str       ,				_lowerCAmelCase      :							List[Any]       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							int=None       ,				**_lowerCAmelCase      :							Dict			)		-> Tuple:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_vision_text_model(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase       ,				text_model=_lowerCAmelCase			)

		__lowercase             =  model(input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase			)

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







	def  _a   (  self      :							Optional[Any]       ,				_lowerCAmelCase      :							List[Any]       ,				_lowerCAmelCase      :							Any       ,				_lowerCAmelCase      :							Dict       ,				_lowerCAmelCase      :							str       ,				_lowerCAmelCase      :							Any=None       ,				**_lowerCAmelCase      :							str			)		-> Tuple:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_vision_text_model(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  {"""vision_model""": vision_model, """text_model""": text_model}
		__lowercase             =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**_lowerCAmelCase			)

		__lowercase             =  model(input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase			)

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







	def  _a   (  self      :							Dict       ,				_lowerCAmelCase      :							Union[str, Any]       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							Tuple       ,				_lowerCAmelCase      :							Optional[int]       ,				_lowerCAmelCase      :							List[Any]=None       ,				**_lowerCAmelCase      :							Any			)		-> Optional[int]:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_vision_text_model(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase       ,				text_model=_lowerCAmelCase			)

		__lowercase             =  model(input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase			)
		__lowercase             =  output[0].numpy()

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

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







	def  _a   (  self      :							int       ,				_lowerCAmelCase      :							Any       ,				_lowerCAmelCase      :							Optional[int]       ,				_lowerCAmelCase      :							str       ,				_lowerCAmelCase      :							Optional[int]       ,				_lowerCAmelCase      :							Optional[int]=None       ,				**_lowerCAmelCase      :							Optional[int]			)		-> List[Any]:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_vision_text_model(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase       ,				text_model=_lowerCAmelCase			)

		__lowercase             =  model(
		    input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase       ,				output_attentions=_lowerCAmelCase			)

		__lowercase             =  output.vision_model_output.attentions
		self.assertEqual(len(_lowerCAmelCase			)       ,				vision_config.num_hidden_layers			)

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

		__lowercase             =  output.text_model_output.attentions
		self.assertEqual(len(_lowerCAmelCase			)       ,				text_config.num_hidden_layers			)

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







	def  _a   (  self      :							List[Any]       ,				_lowerCAmelCase      :							np.ndarray       ,				_lowerCAmelCase      :							np.ndarray       ,				_lowerCAmelCase      :							float			)		-> Optional[int]:
		"""simple docstring"""







		__lowercase             =  np.abs((a - b)			).max()
		self.assertLessEqual(_lowerCAmelCase       ,				_lowerCAmelCase       ,				F'Difference between torch and flax is {diff} (>= {tol}).'			)







	def  _a   (  self      :							List[Any]			)		-> List[Any]:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		self.check_vision_text_dual_encoder_model(**_lowerCAmelCase			)







	def  _a   (  self      :							int			)		-> List[Any]:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		self.check_model_from_pretrained_configs(**_lowerCAmelCase			)







	def  _a   (  self      :							Union[str, Any]			)		-> int:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		self.check_vision_text_dual_encoder_from_pretrained(**_lowerCAmelCase			)







	def  _a   (  self      :							List[str]			)		-> str:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		self.check_save_load(**_lowerCAmelCase			)







	def  _a   (  self      :							Dict			)		-> Union[str, Any]:
		"""simple docstring"""







		__lowercase             =  self.prepare_config_and_inputs()
		self.check_vision_text_output_attention(**_lowerCAmelCase			)







	@slow
	def  _a   (  self      :							Optional[int]			)		-> Optional[Any]:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_pretrained_model_and_inputs()

		__lowercase             =  model_a(**_lowerCAmelCase			)
		__lowercase             =  outputs[0].numpy()

		with tempfile.TemporaryDirectory() as tmp_dirname:
			model_a.save_pretrained(_lowerCAmelCase			)
			__lowercase             =  TFVisionTextDualEncoderModel.from_pretrained(_lowerCAmelCase			)

			__lowercase             =  model_a(**_lowerCAmelCase			)
			__lowercase             =  after_outputs[0].numpy()
			__lowercase             =  np.amax(np.abs(out_a - out_a			)			)
			self.assertLessEqual(_lowerCAmelCase       ,				1e-5			)


@require_tf
class        __UpperCamelCase    (     _lowerCAmelCase   ,			unittest.TestCase   ):







	def  _a   (  self      :							Optional[int]			)		-> List[str]:
		"""simple docstring"""







		__lowercase             =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
		    """hf-internal-testing/tiny-random-vit"""       ,				"""hf-internal-testing/tiny-random-bert"""			)
		__lowercase             =  13
		__lowercase             =  floats_tensor(
		    [
		        batch_size,
		        model.vision_model.config.num_channels,
		        model.vision_model.config.image_size,
		        model.vision_model.config.image_size,
		    ]			)
		__lowercase             =  ids_tensor([batch_size, 4]       ,				model.text_model.config.vocab_size			)
		__lowercase             =  random_attention_mask([batch_size, 4]			)
		__lowercase             =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

		return model, inputs







	def  _a   (  self      :							Tuple       ,				_lowerCAmelCase      :							Tuple       ,				_lowerCAmelCase      :							List[str]			)		-> List[str]:
		"""simple docstring"""







		__lowercase             =  TFViTModel(_lowerCAmelCase       ,				name="""vision_model"""			)
		__lowercase             =  TFBertModel(_lowerCAmelCase       ,				name="""text_model"""			)
		return vision_model, text_model







	def  _a   (  self      :							Dict			)		-> Optional[Any]:
		"""simple docstring"""







		__lowercase             =  TFViTModelTester(self			)
		__lowercase             =  TFBertModelTester(self			)
		__lowercase             =  vit_model_tester.prepare_config_and_inputs()
		__lowercase             =  bert_model_tester.prepare_config_and_inputs()

		__lowercase       ,						__lowercase       ,						__lowercase             =  vision_config_and_inputs

		(
		    (
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						
		)             =  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        __UpperCamelCase    (     _lowerCAmelCase   ,			unittest.TestCase   ):







	def  _a   (  self      :							Tuple			)		-> Any:
		"""simple docstring"""







		__lowercase             =  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
		    """Rocketknight1/tiny-random-deit-tf"""       ,				"""hf-internal-testing/tiny-random-roberta"""			)
		__lowercase             =  13
		__lowercase             =  floats_tensor(
		    [
		        batch_size,
		        model.vision_model.config.num_channels,
		        model.vision_model.config.image_size,
		        model.vision_model.config.image_size,
		    ]			)
		__lowercase             =  ids_tensor([batch_size, 4]       ,				model.text_model.config.vocab_size			)
		__lowercase             =  random_attention_mask([batch_size, 4]			)
		__lowercase             =  {"""pixel_values""": pixel_values, """input_ids""": input_ids, """attention_mask""": attention_mask}

		return model, inputs







	def  _a   (  self      :							Optional[Any]       ,				_lowerCAmelCase      :							Union[str, Any]       ,				_lowerCAmelCase      :							str       ,				_lowerCAmelCase      :							int       ,				_lowerCAmelCase      :							Tuple       ,				_lowerCAmelCase      :							int=None       ,				**_lowerCAmelCase      :							Tuple			)		-> Dict:
		"""simple docstring"""







		__lowercase       ,						__lowercase             =  self.get_vision_text_model(_lowerCAmelCase       ,				_lowerCAmelCase			)
		__lowercase             =  TFVisionTextDualEncoderModel(vision_model=_lowerCAmelCase       ,				text_model=_lowerCAmelCase			)

		__lowercase             =  model(
		    input_ids=_lowerCAmelCase       ,				pixel_values=_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase       ,				output_attentions=_lowerCAmelCase			)

		__lowercase             =  output.vision_model_output.attentions
		self.assertEqual(len(_lowerCAmelCase			)       ,				vision_config.num_hidden_layers			)

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

		__lowercase             =  output.text_model_output.attentions
		self.assertEqual(len(_lowerCAmelCase			)       ,				text_config.num_hidden_layers			)

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







	def  _a   (  self      :							Tuple       ,				_lowerCAmelCase      :							List[str]       ,				_lowerCAmelCase      :							Dict			)		-> int:
		"""simple docstring"""







		__lowercase             =  TFDeiTModel(_lowerCAmelCase       ,				name="""vision_model"""			)
		__lowercase             =  TFRobertaModel(_lowerCAmelCase       ,				name="""text_model"""			)
		return vision_model, text_model







	def  _a   (  self      :							Tuple			)		-> str:
		"""simple docstring"""







		__lowercase             =  TFDeiTModelTester(self			)
		__lowercase             =  TFRobertaModelTester(self			)
		__lowercase             =  vit_model_tester.prepare_config_and_inputs()
		__lowercase             =  bert_model_tester.prepare_config_and_inputs()

		__lowercase       ,						__lowercase       ,						__lowercase             =  vision_config_and_inputs

		(
		    (
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						
		)             =  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        __UpperCamelCase    (     _lowerCAmelCase   ,			unittest.TestCase   ):







	def  _a   (  self      :							int			)		-> Union[str, Any]:
		"""simple docstring"""







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

		return model, inputs







	def  _a   (  self      :							Optional[int]       ,				_lowerCAmelCase      :							Dict       ,				_lowerCAmelCase      :							Union[str, Any]			)		-> Dict:
		"""simple docstring"""







		__lowercase             =  TFCLIPVisionModel(_lowerCAmelCase       ,				name="""vision_model"""			)
		__lowercase             =  TFBertModel(_lowerCAmelCase       ,				name="""text_model"""			)
		return vision_model, text_model







	def  _a   (  self      :							Union[str, Any]			)		-> Optional[int]:
		"""simple docstring"""







		__lowercase             =  TFCLIPVisionModelTester(self			)
		__lowercase             =  TFBertModelTester(self			)
		__lowercase             =  clip_model_tester.prepare_config_and_inputs()
		__lowercase             =  bert_model_tester.prepare_config_and_inputs()

		__lowercase       ,						__lowercase             =  vision_config_and_inputs

		(
		    (
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						(
		    __lowercase
		)       ,						
		)             =  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        __UpperCamelCase    (     unittest.TestCase   ):







	@slow
	def  _a   (  self      :							int			)		-> Tuple:
		"""simple docstring"""







		__lowercase             =  TFVisionTextDualEncoderModel.from_pretrained(
		    """clip-italian/clip-italian"""       ,				logit_scale_init_value=1.0       ,				from_pt=_lowerCAmelCase			)
		__lowercase             =  VisionTextDualEncoderProcessor.from_pretrained("""clip-italian/clip-italian"""			)

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

		__lowercase             =  model(**_lowerCAmelCase			)

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

		__lowercase             =  np.array([[1.2_284_727, 0.3_104_122]]			)

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

