



"""simple docstring"""





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

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

from ...test_tokenization_common import TokenizerTesterMixin


if is_torch_available():
							_UpperCamelCase							=      'pt'
elif is_tf_available():
							_UpperCamelCase							=      'tf'
else:
							_UpperCamelCase							=      'jax'
class 		SCREAMING_SNAKE_CASE_							(    snake_case__       ,  unittest.TestCase   ):
				"""simple docstring"""
				__snake_case   :       Optional[int]	 =		ByTaTokenizer
				__snake_case   :       Union[str, Any]	 =		False




				def 					__lowercase  (     self  :List[str]			):
											super().setUp()
											__lowerCamelCase      :      str         =ByTaTokenizer()
											tokenizer.save_pretrained(self.tmpdirname			)




				@cached_property
				def 					__lowercase  (     self  :Optional[Any]			):
											return ByTaTokenizer.from_pretrained('''google/byt5-small'''			)




				def 					__lowercase  (     self  :Optional[int]						,    **__lowercase  :Union[str, Any]			):
											return self.tokenizer_class.from_pretrained(self.tmpdirname						,    **__lowercase			)




				def 					__lowercase  (     self  :Any						,    __lowercase  :int						,    __lowercase  :List[Any]=False						,    __lowercase  :List[str]=20						,    __lowercase  :Any=5			):
											# XXX The default common tokenizer tests assume that every ID is decodable on its own.
											# This assumption is invalid for ByT5 because single bytes might not be
											# valid utf-8 (byte 128 for instance).
											# Here we're overriding the smallest possible method to provide
											# a clean sequence without making the same assumption.

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

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

											# Ensure consistency
											__lowerCamelCase      :      List[Any]         =tokenizer.decode(__lowercase						,    clean_up_tokenization_spaces=__lowercase			)
											if " " not in output_txt and len(__lowercase			) > 1:
																		__lowerCamelCase      :      Optional[int]         =(
																		    tokenizer.decode([toks_ids[0]]						,    clean_up_tokenization_spaces=__lowercase			)
																		    + ''' '''
																		    + tokenizer.decode(toks_ids[1:]						,    clean_up_tokenization_spaces=__lowercase			)
																		)
											if with_prefix_space:
																		__lowerCamelCase      :      Any         =''' ''' + output_txt
											__lowerCamelCase      :      Any         =tokenizer.encode(__lowercase						,    add_special_tokens=__lowercase			)
											return output_txt, output_ids




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Optional[Any]         =self.ta_base_tokenizer
											__lowerCamelCase      :      Optional[Any]         =tokenizer(['''hi</s>''', '''I went to the gym</s>''', '''</s>''']			)
											__lowerCamelCase      :      Tuple         =tokenizer(['''hi''', '''I went to the gym''', '''''']			)
											self.assertListEqual(batch_with_eos_added['''input_ids''']						,    batch_without_eos_added['''input_ids''']			)




				def 					__lowercase  (     self  :List[str]			):
											__lowerCamelCase      :      Optional[int]         =self.ta_base_tokenizer
											__lowerCamelCase      :      List[str]         ='''Unicode €.'''
											__lowerCamelCase      :      str         =tokenizer(__lowercase			)
											__lowerCamelCase      :      Union[str, Any]         =[88, 113, 108, 102, 114, 103, 104, 35, 229, 133, 175, 49, 1]
											self.assertEqual(encoded['''input_ids''']						,    __lowercase			)

											# decoding
											__lowerCamelCase      :      Dict         =tokenizer.decode(__lowercase			)
											self.assertEqual(__lowercase						,    '''Unicode €.</s>'''			)

											__lowerCamelCase      :      Union[str, Any]         =tokenizer('''e è é ê ë'''			)
											__lowerCamelCase      :      Optional[Any]         =[104, 35, 198, 171, 35, 198, 172, 35, 198, 173, 35, 198, 174, 1]
											self.assertEqual(encoded['''input_ids''']						,    __lowercase			)
											# decoding
											__lowerCamelCase      :      Any         =tokenizer.decode(__lowercase			)
											self.assertEqual(__lowercase						,    '''e è é ê ë</s>'''			)

											# encode/decode, but with `encode` instead of `__call__`
											self.assertEqual(tokenizer.decode(tokenizer.encode('''e è é ê ë'''			)			)						,    '''e è é ê ë</s>'''			)




				def 					__lowercase  (     self  :Tuple			):
											__lowerCamelCase      :      Any         =self.ta_base_tokenizer
											__lowerCamelCase      :      Optional[Any]         =['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
											# fmt: off
											__lowerCamelCase      :      str         =[68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 1, 0]
											# fmt: on
											__lowerCamelCase      :      Tuple         =tokenizer(__lowercase						,    padding=__lowercase						,    return_tensors=__lowercase			)
											self.assertIsInstance(__lowercase						,    __lowercase			)

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

											self.assertListEqual(__lowercase						,    __lowercase			)

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




				def 					__lowercase  (     self  :Dict			):
											__lowerCamelCase      :      int         =self.ta_base_tokenizer
											__lowerCamelCase      :      Union[str, Any]         =['''A long paragraph for summarization.''', '''Another paragraph for summarization.''']
											__lowerCamelCase      :      Optional[Any]         =tokenizer(__lowercase						,    padding=__lowercase						,    return_tensors=__lowercase			)
											# check if input_ids are returned and no decoder_input_ids
											self.assertIn('''input_ids'''						,    __lowercase			)
											self.assertIn('''attention_mask'''						,    __lowercase			)
											self.assertNotIn('''decoder_input_ids'''						,    __lowercase			)
											self.assertNotIn('''decoder_attention_mask'''						,    __lowercase			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      Optional[Any]         =self.ta_base_tokenizer
											__lowerCamelCase      :      int         =[
											    '''Summary of the text.''',
											    '''Another summary.''',
											]
											__lowerCamelCase      :      int         =tokenizer(
											    text_target=__lowercase						,    max_length=32						,    padding='''max_length'''						,    truncation=__lowercase						,    return_tensors=__lowercase			)
											self.assertEqual(32						,    targets['''input_ids'''].shape[1]			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      int         =self.ta_base_tokenizer
											__lowerCamelCase      :      Tuple         =['''A long paragraph for summarization. </s>''']
											__lowerCamelCase      :      Union[str, Any]         =['''Summary of the text. </s>''']
											# fmt: off
											__lowerCamelCase      :      List[Any]         =[68, 35, 111, 114, 113, 106, 35, 115, 100, 117, 100, 106, 117, 100, 115, 107, 35, 105, 114, 117, 35, 118, 120, 112, 112, 100, 117, 108, 125, 100, 119, 108, 114, 113, 49, 35, 1]
											__lowerCamelCase      :      Dict         =[86, 120, 112, 112, 100, 117, 124, 35, 114, 105, 35, 119, 107, 104, 35, 119, 104, 123, 119, 49, 35, 1]
											# fmt: on

											__lowerCamelCase      :      Optional[int]         =tokenizer(__lowercase						,    text_target=__lowercase			)

											self.assertEqual(__lowercase						,    batch['''input_ids'''][0]			)
											self.assertEqual(__lowercase						,    batch['''labels'''][0]			)




				def 					__lowercase  (     self  :Optional[int]			):
											# safety check on max_len default value so we are sure the test works
											__lowerCamelCase      :      List[Any]         =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
											__lowerCamelCase      :      Tuple         =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
																									__lowerCamelCase      :      Any         =tempfile.mkdtemp()

																									__lowerCamelCase      :      Dict         =''' He is very happy, UNwant\u00E9d,running'''
																									__lowerCamelCase      :      List[Any]         =tokenizer.encode(__lowercase						,    add_special_tokens=__lowercase			)
																									tokenizer.save_pretrained(__lowercase			)

																									__lowerCamelCase      :      Optional[int]         =tokenizer.__class__.from_pretrained(__lowercase			)
																									__lowerCamelCase      :      Union[str, Any]         =after_tokenizer.encode(__lowercase						,    add_special_tokens=__lowercase			)
																									self.assertListEqual(__lowercase						,    __lowercase			)

																									shutil.rmtree(__lowercase			)

											__lowerCamelCase      :      Union[str, Any]         =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
																									__lowerCamelCase      :      int         =tempfile.mkdtemp()

																									__lowerCamelCase      :      int         =''' He is very happy, UNwant\u00E9d,running'''
																									tokenizer.add_tokens(['''bim''', '''bambam''']			)
																									__lowerCamelCase      :      Dict         =tokenizer.additional_special_tokens
																									additional_special_tokens.append('''new_additional_special_token'''			)
																									tokenizer.add_special_tokens({'''additional_special_tokens''': additional_special_tokens}			)
																									__lowerCamelCase      :      int         =tokenizer.encode(__lowercase						,    add_special_tokens=__lowercase			)
																									tokenizer.save_pretrained(__lowercase			)

																									__lowerCamelCase      :      List[Any]         =tokenizer.__class__.from_pretrained(__lowercase			)
																									__lowerCamelCase      :      List[Any]         =after_tokenizer.encode(__lowercase						,    add_special_tokens=__lowercase			)
																									self.assertListEqual(__lowercase						,    __lowercase			)
																									self.assertIn('''new_additional_special_token'''						,    after_tokenizer.additional_special_tokens			)
																									self.assertEqual(after_tokenizer.model_max_length						,    42			)

																									__lowerCamelCase      :      Tuple         =tokenizer.__class__.from_pretrained(__lowercase						,    model_max_length=43			)
																									self.assertEqual(tokenizer.model_max_length						,    43			)

																									shutil.rmtree(__lowercase			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      List[str]         =[]
											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(__lowercase			)

																									with open(os.path.join(__lowercase						,    '''special_tokens_map.json'''			)						,    encoding='''utf-8'''			) as json_file:
																																__lowerCamelCase      :      str         =json.load(__lowercase			)

																									with open(os.path.join(__lowercase						,    '''tokenizer_config.json'''			)						,    encoding='''utf-8'''			) as json_file:
																																__lowerCamelCase      :      List[str]         =json.load(__lowercase			)

																									__lowerCamelCase      :      Any         =[f'<extra_id_{i}>' for i in range(125			)]

																									__lowerCamelCase      :      Optional[int]         =added_tokens_extra_ids + [
																									    '''an_additional_special_token'''
																									]
																									__lowerCamelCase      :      List[str]         =added_tokens_extra_ids + [
																									    '''an_additional_special_token'''
																									]

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

																									# 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
																									__lowerCamelCase      :      int         =tokenizer_class.from_pretrained(
																									    __lowercase						,    )
																									self.assertIn(
																									    '''an_additional_special_token'''						,    tokenizer_without_change_in_init.additional_special_tokens			)
																									# self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab
																									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
																									__lowerCamelCase      :      int         =added_tokens_extra_ids + [AddedToken('''a_new_additional_special_token'''						,    lstrip=__lowercase			)]
																									__lowerCamelCase      :      List[str]         =tokenizer_class.from_pretrained(
																									    __lowercase						,    additional_special_tokens=__lowercase						,    )

																									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 					__lowercase  (     self  :int			):
											__lowerCamelCase      :      Dict         =[]
											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(__lowercase			)

																									__lowerCamelCase      :      Optional[int]         =tokenizer_class.from_pretrained(__lowercase			)

																									self.assertTrue(tokenizer.decode([255]			) == ''''''			)




				def 					__lowercase  (     self  :Dict			):
											pass




				def 					__lowercase  (     self  :Optional[int]			):
											pass




				def 					__lowercase  (     self  :Dict			):
											pass




				def 					__lowercase  (     self  :Dict			):
											pass




				def 					__lowercase  (     self  :Union[str, Any]			):
											# The default common tokenizer tests uses invalid tokens for ByT5 that can only accept one-character strings
											# and special added tokens as tokens
											__lowerCamelCase      :      Optional[int]         =self.get_tokenizers(fast=__lowercase						,    do_lower_case=__lowercase			)
											for tokenizer in tokenizers:
																		with self.subTest(f'{tokenizer.__class__.__name__}'			):
																									__lowerCamelCase      :      Any         =['''t''', '''h''', '''i''', '''s''', ''' ''', '''i''', '''s''', ''' ''', '''a''', ''' ''', '''t''', '''e''', '''x''', '''t''', '''</s>''']
																									__lowerCamelCase      :      Any         =tokenizer.convert_tokens_to_string(__lowercase			)

																									self.assertIsInstance(__lowercase						,    __lowercase			)




				def 					__lowercase  (     self  :str			):
											__lowerCamelCase      :      int         =self.get_tokenizers()
											for tokenizer in tokenizers:
																		with self.subTest(f'{tokenizer.__class__.__name__}'			):
																									__lowerCamelCase      :      List[Any]         =[
																									    '''bos_token''',
																									    '''eos_token''',
																									    '''unk_token''',
																									    '''sep_token''',
																									    '''pad_token''',
																									    '''cls_token''',
																									    '''mask_token''',
																									]

																									__lowerCamelCase      :      Tuple         =0
																									__lowerCamelCase      :      List[str]         =tokenizer.convert_ids_to_tokens(
																									    __lowercase						,    skip_special_tokens=__lowercase			)

																									for attr in attributes_list:
																																setattr(__lowercase						,    attr + '''_id'''						,    __lowercase			)
																																self.assertEqual(getattr(__lowercase						,    __lowercase			)						,    __lowercase			)
																																self.assertEqual(getattr(__lowercase						,    attr + '''_id'''			)						,    __lowercase			)

																																setattr(__lowercase						,    attr + '''_id'''						,    __lowercase			)
																																self.assertEqual(getattr(__lowercase						,    __lowercase			)						,    __lowercase			)
																																self.assertEqual(getattr(__lowercase						,    attr + '''_id'''			)						,    __lowercase			)

																									setattr(__lowercase						,    '''additional_special_tokens_ids'''						,    []			)
																									self.assertListEqual(getattr(__lowercase						,    '''additional_special_tokens'''			)						,    []			)
																									self.assertListEqual(getattr(__lowercase						,    '''additional_special_tokens_ids'''			)						,    []			)

																									setattr(__lowercase						,    '''additional_special_tokens_ids'''						,    [token_id_to_test_setters]			)
																									self.assertListEqual(getattr(__lowercase						,    '''additional_special_tokens'''			)						,    [token_to_test_setters]			)
																									self.assertListEqual(getattr(__lowercase						,    '''additional_special_tokens_ids'''			)						,    [token_id_to_test_setters]			)











"""simple docstring"""





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

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

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


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

if is_vision_available():
							from PIL import Image

							from transformers import VisionTextDualEncoderProcessor



def 				lowerCAmelCase_			(      SCREAMING_SNAKE_CASE       :							Optional[int]   ):


							'''simple docstring'''







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




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




				def 					__lowercase  (     self  :Any			):
											pass




				def 					__lowercase  (     self  :str			):
											pass




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

											__lowerCamelCase      :      Dict         =TFVisionTextDualEncoderModel(__lowercase			)

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

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




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

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

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




				def 					__lowercase  (     self  :int						,    __lowercase  :Dict						,    __lowercase  :List[Any]						,    __lowercase  :Any						,    __lowercase  :Optional[Any]						,    __lowercase  :Tuple=None						,    **__lowercase  :List[Any]			):
											__lowerCamelCase    ,						__lowerCamelCase      :      int         =self.get_vision_text_model(__lowercase						,    __lowercase			)
											__lowerCamelCase      :      Dict         ={'''vision_model''': vision_model, '''text_model''': text_model}
											__lowerCamelCase      :      List[Any]         =TFVisionTextDualEncoderModel.from_vision_text_pretrained(**__lowercase			)

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

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




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

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

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

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




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

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

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

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

											__lowerCamelCase      :      List[Any]         =output.text_model_output.attentions
											self.assertEqual(len(__lowercase			)						,    text_config.num_hidden_layers			)

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




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




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




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




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




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




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




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

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

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

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

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




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

											return model, inputs




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




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

											__lowerCamelCase    ,						__lowerCamelCase    ,						__lowerCamelCase      :      Dict         =vision_config_and_inputs

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      int         =text_config_and_inputs

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

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




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

											return model, inputs




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

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

											__lowerCamelCase      :      str         =output.vision_model_output.attentions
											self.assertEqual(len(__lowercase			)						,    vision_config.num_hidden_layers			)

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

											__lowerCamelCase      :      Any         =output.text_model_output.attentions
											self.assertEqual(len(__lowercase			)						,    text_config.num_hidden_layers			)

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




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




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

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

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      List[Any]         =text_config_and_inputs

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

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




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

											return model, inputs




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




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

											__lowerCamelCase    ,						__lowerCamelCase      :      Any         =vision_config_and_inputs

											(
											    (
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						(
											    __lowerCamelCase
											)    ,						
											)      :      str         =text_config_and_inputs

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

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




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

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

											__lowerCamelCase      :      str         =model(**__lowercase			)

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

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

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




