




"""simple docstring"""


import json
import os
import re
import shutil
import tempfile
import unittest
from typing import Tuple

from transformers import AddedToken, BatchEncoding, PerceiverTokenizer
from transformers.utils import cached_property, is_tf_available, is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


if is_torch_available():
     A_						=				'''pt'''
elif is_tf_available():
     A_						=				'''tf'''
else:
     A_						=				'''jax'''






class 					lowercase(     __a       ,			unittest.TestCase ):







 '''simple docstring'''





 lowercase__ 					=					PerceiverTokenizer
 lowercase__ 					=					False


 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   super().setUp()
   _snake_case							:		Any								=  PerceiverTokenizer()
   tokenizer.save_pretrained(self.tmpdirname		)


 @cached_property
 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   return PerceiverTokenizer.from_pretrained("""deepmind/language-perceiver"""		)


 def  UpperCamelCase_   (				self:		List[str],      **a_:		Any		):




   '''simple docstring'''



   return self.tokenizer_class.from_pretrained(self.tmpdirname,      **a_		)


 def  UpperCamelCase_   (				self:		Dict,      a_:		Any,      a_:		Tuple=False,      a_:		List[str]=20,      a_:		Tuple=5		):




   '''simple docstring'''



   _snake_case							:		Any								=  []
   for i in range(len(a_		)		):
     try:
       _snake_case							:		Any								=  tokenizer.decode([i],      clean_up_tokenization_spaces=a_		)
     except UnicodeDecodeError:
       pass
     toks.append((i, tok)		)

   _snake_case							:		int								=  list(filter(lambda a_		: re.match(r"""^[ a-zA-Z]+$""",      t[1]		),      a_		)		)
   _snake_case							:		Optional[Any]								=  list(filter(lambda a_		: [t[0]] == tokenizer.encode(t[1],      add_special_tokens=a_		),      a_		)		)
   if max_length is not None and len(a_		) > max_length:
     _snake_case							:		List[str]								=  toks[:max_length]
   if min_length is not None and len(a_		) < min_length and len(a_		) > 0:
     while len(a_		) < min_length:
       _snake_case							:		Optional[Any]								=  toks + toks
        # toks_str = [t[1] for t in toks]
   _snake_case							:		List[Any]								=  [t[0] for t in toks]

   # Ensure consistency
   _snake_case							:		Tuple								=  tokenizer.decode(a_,      clean_up_tokenization_spaces=a_		)
   if " " not in output_txt and len(a_		) > 1:
     _snake_case							:		Any								=  (
         tokenizer.decode([toks_ids[0]],      clean_up_tokenization_spaces=a_		)
         + """ """
         + tokenizer.decode(toks_ids[1:],      clean_up_tokenization_spaces=a_		)
     )
   if with_prefix_space:
     _snake_case							:		str								=  """ """ + output_txt
   _snake_case							:		Dict								=  tokenizer.encode(a_,      add_special_tokens=a_		)
   return output_txt, output_ids


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Tuple								=  self.perceiver_tokenizer
   _snake_case							:		Union[str, Any]								=  """Unicode €."""
   _snake_case							:		Dict								=  tokenizer(a_		)
   _snake_case							:		List[str]								=  [4, 91, 116, 111, 105, 117, 106, 107, 38, 232, 136, 178, 52, 5]
   self.assertEqual(encoded["""input_ids"""],      a_		)

   # decoding
   _snake_case							:		List[str]								=  tokenizer.decode(a_		)
   self.assertEqual(a_,      """[CLS]Unicode €.[SEP]"""		)

   _snake_case							:		Any								=  tokenizer("""e è é ê ë"""		)
   _snake_case							:		Union[str, Any]								=  [4, 107, 38, 201, 174, 38, 201, 175, 38, 201, 176, 38, 201, 177, 5]
   self.assertEqual(encoded["""input_ids"""],      a_		)
   # decoding
   _snake_case							:		Tuple								=  tokenizer.decode(a_		)
   self.assertEqual(a_,      """[CLS]e è é ê ë[SEP]"""		)

   # encode/decode, but with `encode` instead of `__call__`
   self.assertEqual(tokenizer.decode(tokenizer.encode("""e è é ê ë"""		)		),      """[CLS]e è é ê ë[SEP]"""		)


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Tuple								=  self.perceiver_tokenizer
   _snake_case							:		Any								=  ["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
   # fmt: off
   _snake_case							:		Any								=  [4, 71, 38, 114, 117, 116, 109, 38, 118, 103, 120, 103, 109, 120, 103, 118, 110, 38, 108, 117, 120, 38, 121, 123, 115, 115, 103, 120, 111, 128, 103, 122, 111, 117, 116, 52, 5, 0]
   # fmt: on
   _snake_case							:		Tuple								=  tokenizer(a_,      padding=a_,      return_tensors=a_		)
   self.assertIsInstance(a_,      a_		)

   if FRAMEWORK != "jax":
     _snake_case							:		str								=  list(batch.input_ids.numpy()[0]		)
   else:
     _snake_case							:		Any								=  list(batch.input_ids.tolist()[0]		)

   self.assertListEqual(a_,      a_		)

   self.assertEqual((2, 38),      batch.input_ids.shape		)
   self.assertEqual((2, 38),      batch.attention_mask.shape		)


 def  UpperCamelCase_   (				self:		List[str]		):




   '''simple docstring'''



   _snake_case							:		int								=  self.perceiver_tokenizer
   _snake_case							:		str								=  ["""A long paragraph for summarization.""", """Another paragraph for summarization."""]
   _snake_case							:		Union[str, Any]								=  tokenizer(a_,      padding=a_,      return_tensors=a_		)
   # check if input_ids are returned and no decoder_input_ids
   self.assertIn("""input_ids""",      a_		)
   self.assertIn("""attention_mask""",      a_		)
   self.assertNotIn("""decoder_input_ids""",      a_		)
   self.assertNotIn("""decoder_attention_mask""",      a_		)


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.perceiver_tokenizer
   _snake_case							:		int								=  [
       """Summary of the text.""",
       """Another summary.""",
   ]
   _snake_case							:		List[Any]								=  tokenizer(
       text_target=a_,      max_length=32,      padding="""max_length""",      truncation=a_,      return_tensors=a_		)
   self.assertEqual(32,      targets["""input_ids"""].shape[1]		)


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.get_tokenizers()
   for tokenizer in tokenizers:
     with self.subTest(f"{tokenizer.__class__.__name__}"		):
       self.assertNotEqual(tokenizer.model_max_length,      42		)

        # Now let's start the test
   _snake_case							:		int								=  self.get_tokenizers()
   for tokenizer in tokenizers:
     with self.subTest(f"{tokenizer.__class__.__name__}"		):
       # Isolate this from the other tests because we save additional tokens/etc
       _snake_case							:		str								=  tempfile.mkdtemp()

       _snake_case							:		Tuple								=  """ He is very happy, UNwant\u00E9d,running"""
       _snake_case							:		Any								=  tokenizer.encode(a_,      add_special_tokens=a_		)
       tokenizer.save_pretrained(a_		)

       _snake_case							:		List[str]								=  tokenizer.__class__.from_pretrained(a_		)
       _snake_case							:		Tuple								=  after_tokenizer.encode(a_,      add_special_tokens=a_		)
       self.assertListEqual(a_,      a_		)

       shutil.rmtree(a_		)

   _snake_case							:		Optional[int]								=  self.get_tokenizers(model_max_length=42		)
   for tokenizer in tokenizers:
     with self.subTest(f"{tokenizer.__class__.__name__}"		):
       # Isolate this from the other tests because we save additional tokens/etc
       _snake_case							:		Tuple								=  tempfile.mkdtemp()

       _snake_case							:		Optional[int]								=  """ He is very happy, UNwant\u00E9d,running"""
       tokenizer.add_tokens(["""bim""", """bambam"""]		)
       _snake_case							:		Optional[int]								=  tokenizer.additional_special_tokens
       additional_special_tokens.append("""new_additional_special_token"""		)
       tokenizer.add_special_tokens({"""additional_special_tokens""": additional_special_tokens}		)
       _snake_case							:		Tuple								=  tokenizer.encode(a_,      add_special_tokens=a_		)
       tokenizer.save_pretrained(a_		)

       _snake_case							:		int								=  tokenizer.__class__.from_pretrained(a_		)
       _snake_case							:		int								=  after_tokenizer.encode(a_,      add_special_tokens=a_		)
       self.assertListEqual(a_,      a_		)
       self.assertIn("""new_additional_special_token""",      after_tokenizer.additional_special_tokens		)
       self.assertEqual(after_tokenizer.model_max_length,      42		)

       _snake_case							:		int								=  tokenizer.__class__.from_pretrained(a_,      model_max_length=43		)
       self.assertEqual(tokenizer.model_max_length,      43		)

       shutil.rmtree(a_		)


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		int								=  []
   if self.test_slow_tokenizer:
     tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())		)

   if self.test_rust_tokenizer:
     tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())		)

   for tokenizer_class, tokenizer_utils in tokenizer_list:
     with tempfile.TemporaryDirectory() as tmp_dir:
       tokenizer_utils.save_pretrained(a_		)

       with open(os.path.join(a_,      """special_tokens_map.json"""		),      encoding="""utf-8"""		) as json_file:
         _snake_case							:		str								=  json.load(a_		)

       with open(os.path.join(a_,      """tokenizer_config.json"""		),      encoding="""utf-8"""		) as json_file:
         _snake_case							:		Optional[Any]								=  json.load(a_		)

       _snake_case							:		List[Any]								=  [f"<extra_id_{i}>" for i in range(125		)]

       _snake_case							:		int								=  added_tokens_extra_ids + [
           """an_additional_special_token"""
       ]
       _snake_case							:		Optional[int]								=  added_tokens_extra_ids + [
           """an_additional_special_token"""
       ]

       with open(os.path.join(a_,      """special_tokens_map.json"""		),      """w""",      encoding="""utf-8"""		) as outfile:
         json.dump(a_,      a_		)
       with open(os.path.join(a_,      """tokenizer_config.json"""		),      """w""",      encoding="""utf-8"""		) as outfile:
         json.dump(a_,      a_		)

       # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes
       # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and
       # "special_tokens_map.json" files
       _snake_case							:		Tuple								=  tokenizer_class.from_pretrained(
           a_,      )
       self.assertIn(
           """an_additional_special_token""",      tokenizer_without_change_in_init.additional_special_tokens		)
       self.assertEqual(
           ["""an_additional_special_token"""],      tokenizer_without_change_in_init.convert_ids_to_tokens(
               tokenizer_without_change_in_init.convert_tokens_to_ids(["""an_additional_special_token"""]		)		),      )

       # Now we test that we can change the value of additional_special_tokens in the from_pretrained
       _snake_case							:		str								=  added_tokens_extra_ids + [AddedToken("""a_new_additional_special_token""",      lstrip=a_		)]
       _snake_case							:		Any								=  tokenizer_class.from_pretrained(
           a_,      additional_special_tokens=a_,      )

       self.assertIn("""a_new_additional_special_token""",      tokenizer.additional_special_tokens		)
       self.assertEqual(
           ["""a_new_additional_special_token"""],      tokenizer.convert_ids_to_tokens(
               tokenizer.convert_tokens_to_ids(["""a_new_additional_special_token"""]		)		),      )


 def  UpperCamelCase_   (				self:		Any		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  self.perceiver_tokenizer
   self.assertEqual(tokenizer.decode([178]		),      """�"""		)


 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



   pass


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   pass


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   pass


 def  UpperCamelCase_   (				self:		List[Any]		):




   '''simple docstring'''



   pass


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		Dict								=  self.get_tokenizers(fast=a_,      do_lower_case=a_		)
   for tokenizer in tokenizers:
     with self.subTest(f"{tokenizer.__class__.__name__}"		):
       _snake_case							:		Tuple								=  ["""[CLS]""", """t""", """h""", """i""", """s""", """ """, """i""", """s""", """ """, """a""", """ """, """t""", """e""", """s""", """t""", """[SEP]"""]
       _snake_case							:		List[str]								=  tokenizer.convert_tokens_to_string(a_		)

       self.assertIsInstance(a_,      a_		)








from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
					from transformers import (
					    TFBertModel,
					    TFCLIPVisionModel,
					    TFDeiTModel,
					    TFRobertaModel,
					    TFVisionTextDualEncoderModel,
					    TFViTModel,
					    VisionTextDualEncoderConfig,
					)

if is_vision_available():
					from PIL import Image

					from transformers import VisionTextDualEncoderProcessor







def 	_a      (						SCREAMING_SNAKE_CASE_							:				Union[str, Any]     ):
					if isinstance(SCREAMING_SNAKE_CASE_      ,      collections.abc.Iterable     ):
										return x
					return (x, x)
@require_tf
class 			a__    :






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A			):




											"""simple docstring"""



											pass






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											pass






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											pass






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       VisionTextDualEncoderConfig.from_vision_text_configs(_A		,			_A			)

											__lowerCAmelCase				   =       TFVisionTextDualEncoderModel(_A			)

											__lowerCAmelCase				   =       model(input_ids=_A		,			pixel_values=_A		,			attention_mask=_A			)

											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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.get_vision_text_model(_A		,			_A			)
											__lowerCAmelCase				   =       TFVisionTextDualEncoderModel(vision_model=_A		,			text_model=_A			)

											__lowerCAmelCase				   =       model(input_ids=_A		,			pixel_values=_A		,			attention_mask=_A			)

											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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



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

											__lowerCAmelCase				   =       model(input_ids=_A		,			pixel_values=_A		,			attention_mask=_A			)

											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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.get_vision_text_model(_A		,			_A			)
											__lowerCAmelCase				   =       TFVisionTextDualEncoderModel(vision_model=_A		,			text_model=_A			)

											__lowerCAmelCase				   =       model(input_ids=_A		,			pixel_values=_A		,			attention_mask=_A			)
											__lowerCAmelCase				   =       output[0].numpy()

											with tempfile.TemporaryDirectory() as tmpdirname:
																model.save_pretrained(_A			)
																__lowerCAmelCase				   =       TFVisionTextDualEncoderModel.from_pretrained(_A			)

																__lowerCAmelCase				   =       model(input_ids=_A		,			pixel_values=_A		,			attention_mask=_A			)
																__lowerCAmelCase				   =       after_output[0].numpy()
																__lowerCAmelCase				   =       np.amax(np.abs(out_a - out_a			)			)
																self.assertLessEqual(_A		,			1E-5			)






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.get_vision_text_model(_A		,			_A			)
											__lowerCAmelCase				   =       TFVisionTextDualEncoderModel(vision_model=_A		,			text_model=_A			)

											__lowerCAmelCase				   =       model(
											    input_ids=_A		,			pixel_values=_A		,			attention_mask=_A		,			output_attentions=_A			)

											__lowerCAmelCase				   =       output.vision_model_output.attentions
											self.assertEqual(len(_A			)		,			vision_config.num_hidden_layers			)

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

											__lowerCAmelCase				   =       output.text_model_output.attentions
											self.assertEqual(len(_A			)		,			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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       np.abs((a - b)			).max()
											self.assertLessEqual(_A		,			_A		,			f"""Difference between torch and flax is {diff} (>= {tol})."""			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.prepare_config_and_inputs()
											self.check_vision_text_dual_encoder_model(**_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.prepare_config_and_inputs()
											self.check_model_from_pretrained_configs(**_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.prepare_config_and_inputs()
											self.check_vision_text_dual_encoder_from_pretrained(**_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.prepare_config_and_inputs()
											self.check_save_load(**_A			)






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase				   =       self.prepare_config_and_inputs()
											self.check_vision_text_output_attention(**_A			)






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.get_pretrained_model_and_inputs()

											__lowerCAmelCase				   =       model_a(**_A			)
											__lowerCAmelCase				   =       outputs[0].numpy()

											with tempfile.TemporaryDirectory() as tmp_dirname:
																model_a.save_pretrained(_A			)
																__lowerCAmelCase				   =       TFVisionTextDualEncoderModel.from_pretrained(_A			)

																__lowerCAmelCase				   =       model_a(**_A			)
																__lowerCAmelCase				   =       after_outputs[0].numpy()
																__lowerCAmelCase				   =       np.amax(np.abs(out_a - out_a			)			)
																self.assertLessEqual(_A		,			1E-5			)






@require_tf
class 			a__    (					snake_case__ ,				unittest.TestCase      ):






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											return model, inputs






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       TFViTModel(_A		,			name="vision_model"			)
											__lowerCAmelCase				   =       TFBertModel(_A		,			name="text_model"			)
											return vision_model, text_model






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase   ,							__lowerCAmelCase   ,							__lowerCAmelCase				   =       vision_config_and_inputs

											(
											    (
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							
											)				   =       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 			a__    (					snake_case__ ,				unittest.TestCase      ):






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											return model, inputs






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A		,			_A		,			_A		,			_A=None		,			**_A			):




											"""simple docstring"""



											__lowerCAmelCase   ,							__lowerCAmelCase				   =       self.get_vision_text_model(_A		,			_A			)
											__lowerCAmelCase				   =       TFVisionTextDualEncoderModel(vision_model=_A		,			text_model=_A			)

											__lowerCAmelCase				   =       model(
											    input_ids=_A		,			pixel_values=_A		,			attention_mask=_A		,			output_attentions=_A			)

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

											__lowerCAmelCase				   =       output.text_model_output.attentions
											self.assertEqual(len(_A			)		,			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 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       TFDeiTModel(_A		,			name="vision_model"			)
											__lowerCAmelCase				   =       TFRobertaModel(_A		,			name="text_model"			)
											return vision_model, text_model






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase   ,							__lowerCAmelCase   ,							__lowerCAmelCase				   =       vision_config_and_inputs

											(
											    (
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							(
											    __lowerCAmelCase
											)   ,							
											)				   =       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 			a__    (					snake_case__ ,				unittest.TestCase      ):






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											return model, inputs






						def 				__SCREAMING_SNAKE_CASE(   self		,			_A		,			_A			):




											"""simple docstring"""



											__lowerCAmelCase				   =       TFCLIPVisionModel(_A		,			name="vision_model"			)
											__lowerCAmelCase				   =       TFBertModel(_A		,			name="text_model"			)
											return vision_model, text_model






						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

											__lowerCAmelCase   ,							__lowerCAmelCase				   =       vision_config_and_inputs

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






						@slow
						def 				__SCREAMING_SNAKE_CASE(   self			):




											"""simple docstring"""



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

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

											__lowerCAmelCase				   =       model(**_A			)

											# 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				   =       np.array([[1.2_28_47_27, 0.3_10_41_22]]			)

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




