'''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       (			UpperCAmelCase__				:  str		)			->	Tuple:


							'''simple docstring'''






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




@require_tf
class _SCREAMING_SNAKE_CASE:




						def      __lowerCamelCase       (    self :       Optional[int]							,							UpperCamelCase_ :       int							,							UpperCamelCase_ :       Dict      )     ->				Union[str, Any]:
													pass




						def      __lowerCamelCase       (    self :       Dict      )     ->				Union[str, Any]:
													pass




						def      __lowerCamelCase       (    self :       Any      )     ->				str:
													pass




						def      __lowerCamelCase       (    self :       List[Any]							,							UpperCamelCase_ :       Union[str, Any]							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       Optional[int]							,							UpperCamelCase_ :       Union[str, Any]							,							UpperCamelCase_ :       Union[str, Any]=None							,							**UpperCamelCase_ :       Any      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = VisionTextDualEncoderConfig.from_vision_text_configs(UpperCamelCase_							,							UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :List[str]						    = TFVisionTextDualEncoderModel(UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :int						    = model(input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_      )

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




						def      __lowerCamelCase       (    self :       int							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Dict=None							,							**UpperCamelCase_ :       str      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Dict						    = self.get_vision_text_model(UpperCamelCase_							,							UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Dict						    = TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_							,							text_model=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = model(input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_      )

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




						def      __lowerCamelCase       (    self :       Optional[Any]							,							UpperCamelCase_ :       Union[str, Any]							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Optional[int]							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       Tuple=None							,							**UpperCamelCase_ :       Any      )     ->				Optional[int]:
													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :str						    = self.get_vision_text_model(UpperCamelCase_							,							UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = {'vision_model': vision_model, 'text_model': text_model}
													SCREAMING_SNAKE_CASE__     :str						    = TFVisionTextDualEncoderModel.from_vision_text_pretrained(**UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Dict						    = model(input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_      )

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




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

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

													with tempfile.TemporaryDirectory() as tmpdirname:
																				model.save_pretrained(UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase_      )

																				SCREAMING_SNAKE_CASE__     :str						    = model(input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :Any						    = after_output[0].numpy()
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = np.amax(np.abs(out_a - out_a      )      )
																				self.assertLessEqual(UpperCamelCase_							,							1e-5      )




						def      __lowerCamelCase       (    self :       Tuple							,							UpperCamelCase_ :       Any							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Optional[Any]							,							UpperCamelCase_ :       Tuple							,							UpperCamelCase_ :       Any=None							,							**UpperCamelCase_ :       Tuple      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = self.get_vision_text_model(UpperCamelCase_							,							UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :int						    = TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_							,							text_model=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Dict						    = model(
													    input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_							,							output_attentions=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :List[str]						    = output.vision_model_output.attentions
													self.assertEqual(len(UpperCamelCase_      )							,							vision_config.num_hidden_layers      )

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

													SCREAMING_SNAKE_CASE__     :Optional[int]						    = output.text_model_output.attentions
													self.assertEqual(len(UpperCamelCase_      )							,							text_config.num_hidden_layers      )

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




						def      __lowerCamelCase       (    self :       Dict							,							UpperCamelCase_ :       np.ndarray							,							UpperCamelCase_ :       np.ndarray							,							UpperCamelCase_ :       float      )     ->				List[str]:
													SCREAMING_SNAKE_CASE__     :int						    = np.abs((a - b)      ).max()
													self.assertLessEqual(UpperCamelCase_							,							UpperCamelCase_							,							f'''Difference between torch and flax is {diff} (>= {tol}).'''      )




						def      __lowerCamelCase       (    self :       int      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :List[str]						    = self.prepare_config_and_inputs()
													self.check_vision_text_dual_encoder_model(**UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = self.prepare_config_and_inputs()
													self.check_model_from_pretrained_configs(**UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       Optional[int]      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = self.prepare_config_and_inputs()
													self.check_vision_text_dual_encoder_from_pretrained(**UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       List[Any]      )     ->				str:
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = self.prepare_config_and_inputs()
													self.check_save_load(**UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       Tuple      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :List[str]						    = self.prepare_config_and_inputs()
													self.check_vision_text_output_attention(**UpperCamelCase_      )




						@slow
						def      __lowerCamelCase       (    self :       Tuple      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Optional[int]						    = self.get_pretrained_model_and_inputs()

													SCREAMING_SNAKE_CASE__     :List[str]						    = model_a(**UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :List[Any]						    = outputs[0].numpy()

													with tempfile.TemporaryDirectory() as tmp_dirname:
																				model_a.save_pretrained(UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :Optional[Any]						    = TFVisionTextDualEncoderModel.from_pretrained(UpperCamelCase_      )

																				SCREAMING_SNAKE_CASE__     :str						    = model_a(**UpperCamelCase_      )
																				SCREAMING_SNAKE_CASE__     :List[Any]						    = after_outputs[0].numpy()
																				SCREAMING_SNAKE_CASE__     :Optional[int]						    = np.amax(np.abs(out_a - out_a      )      )
																				self.assertLessEqual(UpperCamelCase_							,							1e-5      )




@require_tf
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):




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

													return model, inputs




						def      __lowerCamelCase       (    self :       Optional[int]							,							UpperCamelCase_ :       List[Any]							,							UpperCamelCase_ :       Any      )     ->				str:
													SCREAMING_SNAKE_CASE__     :List[str]						    = TFViTModel(UpperCamelCase_							,							name='vision_model'      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = TFBertModel(UpperCamelCase_							,							name='text_model'      )
													return vision_model, text_model




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				int:
													SCREAMING_SNAKE_CASE__     :Tuple						    = TFViTModelTester(self      )
													SCREAMING_SNAKE_CASE__     :List[str]						    = TFBertModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = vit_model_tester.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = bert_model_tester.prepare_config_and_inputs()

													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = vision_config_and_inputs

													(
													    (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      
													)     :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(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):




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

													return model, inputs




						def      __lowerCamelCase       (    self :       Any							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       Dict							,							UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       Optional[int]=None							,							**UpperCamelCase_ :       Dict      )     ->				Dict:
													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Optional[Any]						    = self.get_vision_text_model(UpperCamelCase_							,							UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :List[Any]						    = TFVisionTextDualEncoderModel(vision_model=UpperCamelCase_							,							text_model=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = model(
													    input_ids=UpperCamelCase_							,							pixel_values=UpperCamelCase_							,							attention_mask=UpperCamelCase_							,							output_attentions=UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :int						    = output.vision_model_output.attentions
													self.assertEqual(len(UpperCamelCase_      )							,							vision_config.num_hidden_layers      )

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

													SCREAMING_SNAKE_CASE__     :Optional[int]						    = output.text_model_output.attentions
													self.assertEqual(len(UpperCamelCase_      )							,							text_config.num_hidden_layers      )

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




						def      __lowerCamelCase       (    self :       str							,							UpperCamelCase_ :       List[str]							,							UpperCamelCase_ :       Tuple      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :Tuple						    = TFDeiTModel(UpperCamelCase_							,							name='vision_model'      )
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = TFRobertaModel(UpperCamelCase_							,							name='text_model'      )
													return vision_model, text_model




						def      __lowerCamelCase       (    self :       int      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__     :List[str]						    = TFDeiTModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = TFRobertaModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Dict						    = vit_model_tester.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE__     :List[Any]						    = bert_model_tester.prepare_config_and_inputs()

													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Any						    = vision_config_and_inputs

													(
													    (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      
													)     :Optional[Any]						    = text_config_and_inputs

													return {
													    "text_config": text_config,
													    "vision_config": vision_config,
													    "pixel_values": pixel_values,
													    "attention_mask": input_mask,
													    "input_ids": input_ids,
													    "text_token_type_ids": token_type_ids,
													    "text_sequence_labels": sequence_labels,
													    "text_token_labels": token_labels,
													    "text_choice_labels": choice_labels,
													}




@require_tf
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):




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

													return model, inputs




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




						def      __lowerCamelCase       (    self :       Dict      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :Any						    = TFCLIPVisionModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = TFBertModelTester(self      )
													SCREAMING_SNAKE_CASE__     :Tuple						    = clip_model_tester.prepare_config_and_inputs()
													SCREAMING_SNAKE_CASE__     :str						    = bert_model_tester.prepare_config_and_inputs()

													SCREAMING_SNAKE_CASE__    ,      SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = vision_config_and_inputs

													(
													    (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      (
													    SCREAMING_SNAKE_CASE__
													)    ,      
													)     :Tuple						    = text_config_and_inputs

													return {
													    "text_config": text_config,
													    "vision_config": vision_config,
													    "pixel_values": pixel_values,
													    "attention_mask": input_mask,
													    "input_ids": input_ids,
													    "text_token_type_ids": token_type_ids,
													    "text_sequence_labels": sequence_labels,
													    "text_token_labels": token_labels,
													    "text_choice_labels": choice_labels,
													}




@require_vision
@require_tf
class _SCREAMING_SNAKE_CASE(							unittest.TestCase					):




						@slow
						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Tuple:
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = TFVisionTextDualEncoderModel.from_pretrained(
													    'clip-italian/clip-italian'							,							logit_scale_init_value=1.0							,							from_pt=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Dict						    = VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'      )

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

													SCREAMING_SNAKE_CASE__     :Any						    = model(**UpperCamelCase_      )

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

													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = np.array([[1.228_4727, 0.310_4122]]      )

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







'''simple docstring'''







import unittest

from transformers import CamembertTokenizer, CamembertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


UpperCamelCase_      	=	get_tests_dir('''fixtures/test_sentencepiece.model''')
UpperCamelCase_      	=	get_tests_dir('''fixtures/test_sentencepiece_bpe.model''')

UpperCamelCase_      	=	'''pt''' if is_torch_available() else '''tf'''




@require_sentencepiece
@require_tokenizers
class _SCREAMING_SNAKE_CASE(							_SCREAMING_SNAKE_CASE						,						unittest.TestCase					):
						A_			:							Tuple     		= CamembertTokenizer
						A_			:							Dict     		= CamembertTokenizerFast
						A_			:							Any     		= True
						A_			:							List[Any]     		= True




						def      __lowerCamelCase       (    self :       int      )     ->				Tuple:
													super().setUp()

													# We have a SentencePiece fixture for testing
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = CamembertTokenizer(UpperCamelCase_      )
													tokenizer.save_pretrained(self.tmpdirname      )




						def      __lowerCamelCase       (    self :       int      )     ->				int:
													SCREAMING_SNAKE_CASE__     :List[Any]						    = '<pad>'
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = 1

													self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase_      )							,							UpperCamelCase_      )
													self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase_      )							,							UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       int      )     ->				int:
													SCREAMING_SNAKE_CASE__     :Any						    = list(self.get_tokenizer().get_vocab().keys()      )

													self.assertEqual(vocab_keys[0]							,							'<s>NOTUSED'      )
													self.assertEqual(vocab_keys[1]							,							'<pad>'      )
													self.assertEqual(vocab_keys[-1]							,							'<mask>'      )
													self.assertEqual(len(UpperCamelCase_      )							,							10_04      )




						def      __lowerCamelCase       (    self :       List[str]      )     ->				List[str]:
													self.assertEqual(self.get_tokenizer().vocab_size							,							10_05      )




						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				List[Any]:
													SCREAMING_SNAKE_CASE__     :Any						    = CamembertTokenizer(UpperCamelCase_      )
													tokenizer.save_pretrained(self.tmpdirname      )
													SCREAMING_SNAKE_CASE__     :Dict						    = CamembertTokenizerFast.from_pretrained(self.tmpdirname      )

													SCREAMING_SNAKE_CASE__     :Optional[int]						    = 'I was born in 92000, and this is falsé.'

													SCREAMING_SNAKE_CASE__     :List[str]						    = tokenizer.encode(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :str						    = rust_tokenizer.encode(UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :int						    = tokenizer.encode(UpperCamelCase_							,							add_special_tokens=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Dict						    = rust_tokenizer.encode(UpperCamelCase_							,							add_special_tokens=UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )

													# <unk> tokens are not the same for `rust` than for `slow`.
													# Because spm gives back raw token instead of `unk` in EncodeAsPieces
													# tokens = tokenizer.tokenize(sequence)
													SCREAMING_SNAKE_CASE__     :int						    = tokenizer.convert_ids_to_tokens(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Tuple						    = rust_tokenizer.tokenize(UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )




						def      __lowerCamelCase       (    self :       Optional[Any]      )     ->				List[str]:
													if not self.test_rust_tokenizer:
																				return

													SCREAMING_SNAKE_CASE__     :List[str]						    = self.get_tokenizer()
													SCREAMING_SNAKE_CASE__     :Dict						    = self.get_rust_tokenizer()

													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = 'I was born in 92000, and this is falsé.'

													SCREAMING_SNAKE_CASE__     :Optional[int]						    = tokenizer.tokenize(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :str						    = rust_tokenizer.tokenize(UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = tokenizer.encode(UpperCamelCase_							,							add_special_tokens=UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[int]						    = rust_tokenizer.encode(UpperCamelCase_							,							add_special_tokens=UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )

													SCREAMING_SNAKE_CASE__     :str						    = self.get_rust_tokenizer()
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = tokenizer.encode(UpperCamelCase_      )
													SCREAMING_SNAKE_CASE__     :Optional[Any]						    = rust_tokenizer.encode(UpperCamelCase_      )
													self.assertListEqual(UpperCamelCase_							,							UpperCamelCase_      )




						@slow
						def      __lowerCamelCase       (    self :       Union[str, Any]      )     ->				Any:
													# fmt: off
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = {'input_ids': [[5, 54, 71_96, 2_97, 30, 23, 7_76, 18, 11, 32_15, 37_05, 82_52, 22, 31_64, 11_81, 21_16, 29, 16, 8_13, 25, 7_91, 33_14, 20, 34_46, 38, 2_75_75, 1_20, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_68, 17, 11, 90_88, 20, 15_17, 8, 2_28_04, 1_88_18, 10, 38, 6_29, 6_07, 6_07, 1_42, 19, 71_96, 8_67, 56, 1_03_26, 24, 22_67, 20, 4_16, 50_72, 1_56_12, 2_33, 7_34, 7, 23_99, 27, 16, 30_15, 16_49, 7, 24, 20, 43_38, 23_99, 27, 13, 34_00, 14, 13, 61_89, 8, 9_30, 9, 6]], 'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}  # noqa: E501
													# fmt: on

													# camembert is a french model. So we also use french texts.
													SCREAMING_SNAKE_CASE__     :Union[str, Any]						    = [
													    'Le transformeur est un modèle d\'apprentissage profond introduit en 2017, '
													    'utilisé principalement dans le domaine du traitement automatique des langues (TAL).',
													    'À l\'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus '
													    'pour gérer des données séquentielles, telles que le langage naturel, pour des tâches '
													    'telles que la traduction et la synthèse de texte.',
													]

													self.tokenizer_integration_test_util(
													    expected_encoding=UpperCamelCase_							,							model_name='camembert-base'							,							revision='3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf'							,							sequences=UpperCamelCase_							,							)






