

import shutil
import tempfile
import unittest

from transformers import (
    SPIECE_UNDERLINE,
    AddedToken,
    BatchEncoding,
    NllbTokenizer,
    NllbTokenizerFast,
    is_torch_available,
)
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


lowercase_             =     get_tests_dir('fixtures/test_sentencepiece.model')


if is_torch_available():
  from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right

lowercase_             =     2_5_6_0_4_7
lowercase_             =     2_5_6_1_4_5





@require_sentencepiece
@require_tokenizers
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):
  _a		    =						NllbTokenizer
  _a		    =						NllbTokenizerFast
  _a		    =						True
  _a		    =						True
  _a		    =						{}




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        super().setUp()

        # We have a SentencePiece fixture for testing
        _lowercase			 =NllbTokenizer(lowerCAmelCase							,     keep_accents=lowerCAmelCase      )
        tokenizer.save_pretrained(self.tmpdirname      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        _lowercase			 =NllbTokenizer(lowerCAmelCase							,     keep_accents=lowerCAmelCase      )

        _lowercase			 =tokenizer.tokenize('This is a test'      )
        self.assertListEqual(lowerCAmelCase							,     ['▁This', '▁is', '▁a', '▁t', 'est']      )

        self.assertListEqual(
            tokenizer.convert_tokens_to_ids(lowerCAmelCase      )							,     [value + tokenizer.fairseq_offset for value in [285, 46, 10, 170, 382]]							,     )

        _lowercase			 =tokenizer.tokenize('I was born in 92000, and this is falsé.'      )
        self.assertListEqual(
            lowerCAmelCase							,     [
                SPIECE_UNDERLINE + 'I',
                SPIECE_UNDERLINE + 'was',
                SPIECE_UNDERLINE + 'b',
                'or',
                'n',
                SPIECE_UNDERLINE + 'in',
                SPIECE_UNDERLINE + '',
                '9',
                '2',
                '0',
                '0',
                '0',
                ',',
                SPIECE_UNDERLINE + 'and',
                SPIECE_UNDERLINE + 'this',
                SPIECE_UNDERLINE + 'is',
                SPIECE_UNDERLINE + 'f',
                'al',
                's',
                'é',
                '.',
            ]							,     )
        _lowercase			 =tokenizer.convert_tokens_to_ids(lowerCAmelCase      )
        self.assertListEqual(
            lowerCAmelCase							,     [
                value + tokenizer.fairseq_offset
                for value in [8, 21, 84, 55, 24, 19, 7, 2, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 2, 4]
            ]							,     )

        _lowercase			 =tokenizer.convert_ids_to_tokens(lowerCAmelCase      )
        self.assertListEqual(
            lowerCAmelCase							,     [
                SPIECE_UNDERLINE + 'I',
                SPIECE_UNDERLINE + 'was',
                SPIECE_UNDERLINE + 'b',
                'or',
                'n',
                SPIECE_UNDERLINE + 'in',
                SPIECE_UNDERLINE + '',
                '<unk>',
                '2',
                '0',
                '0',
                '0',
                ',',
                SPIECE_UNDERLINE + 'and',
                SPIECE_UNDERLINE + 'this',
                SPIECE_UNDERLINE + 'is',
                SPIECE_UNDERLINE + 'f',
                'al',
                's',
                '<unk>',
                '.',
            ]							,     )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =(self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-nllb', {})
        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
              with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''      ):
                    _lowercase			 =self.rust_tokenizer_class.from_pretrained(lowerCAmelCase							,     **lowerCAmelCase      )
                    _lowercase			 =self.tokenizer_class.from_pretrained(lowerCAmelCase							,     **lowerCAmelCase      )

                    _lowercase			 =tempfile.mkdtemp()

                    _lowercase			 =tokenizer_r.save_pretrained(lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.save_pretrained(lowerCAmelCase      )

                    # Checks it save with the same files + the tokenizer.json file for the fast one
                    self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files      )      )
                    _lowercase			 =tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f      )
                    self.assertSequenceEqual(lowerCAmelCase							,     lowerCAmelCase      )

                    # Checks everything loads correctly in the same way
                    _lowercase			 =tokenizer_r.from_pretrained(lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.from_pretrained(lowerCAmelCase      )

                    # Check special tokens are set accordingly on Rust and Python
                    for key in tokenizer_pp.special_tokens_map:
                          self.assertTrue(hasattr(lowerCAmelCase							,     lowerCAmelCase      )      )

                    shutil.rmtree(lowerCAmelCase      )

                    # Save tokenizer rust, legacy_format=True
                    _lowercase			 =tempfile.mkdtemp()

                    _lowercase			 =tokenizer_r.save_pretrained(lowerCAmelCase							,     legacy_format=lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.save_pretrained(lowerCAmelCase      )

                    # Checks it save with the same files
                    self.assertSequenceEqual(lowerCAmelCase							,     lowerCAmelCase      )

                    # Checks everything loads correctly in the same way
                    _lowercase			 =tokenizer_r.from_pretrained(lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.from_pretrained(lowerCAmelCase      )

                    # Check special tokens are set accordingly on Rust and Python
                    for key in tokenizer_pp.special_tokens_map:
                          self.assertTrue(hasattr(lowerCAmelCase							,     lowerCAmelCase      )      )

                    shutil.rmtree(lowerCAmelCase      )

                    # Save tokenizer rust, legacy_format=False
                    _lowercase			 =tempfile.mkdtemp()

                    _lowercase			 =tokenizer_r.save_pretrained(lowerCAmelCase							,     legacy_format=lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.save_pretrained(lowerCAmelCase      )

                    # Checks it saved the tokenizer.json file
                    self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files      )      )

                    # Checks everything loads correctly in the same way
                    _lowercase			 =tokenizer_r.from_pretrained(lowerCAmelCase      )
                    _lowercase			 =tokenizer_p.from_pretrained(lowerCAmelCase      )

                    # Check special tokens are set accordingly on Rust and Python
                    for key in tokenizer_pp.special_tokens_map:
                          self.assertTrue(hasattr(lowerCAmelCase							,     lowerCAmelCase      )      )

                    shutil.rmtree(lowerCAmelCase      )




  @require_torch
  def 						A__  (       self      )						->				Dict:
        '''simple docstring'''






        if not self.test_seqaseq:
              return

        _lowercase			 =self.get_tokenizers()
        for tokenizer in tokenizers:
              with self.subTest(F'''{tokenizer.__class__.__name__}'''      ):
                    # Longer text that will definitely require truncation.
                    _lowercase			 =[
                        ' UN Chief Says There Is No Military Solution in Syria',
                        ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for'
                        ' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons'
                        ' will only worsen the violence and misery for millions of people.',
                    ]
                    _lowercase			 =[
                        'Şeful ONU declară că nu există o soluţie militară în Siria',
                        'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al'
                        ' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi'
                        ' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
                    ]
                    try:
                          _lowercase			 =tokenizer.prepare_seqaseq_batch(
                              src_texts=lowerCAmelCase							,     tgt_texts=lowerCAmelCase							,     max_length=3							,     max_target_length=10							,     return_tensors='pt'							,     src_lang='eng_Latn'							,     tgt_lang='ron_Latn'							,     )
                    except NotImplementedError:
                          return
                    self.assertEqual(batch.input_ids.shape[1]							,     3      )
                    self.assertEqual(batch.labels.shape[1]							,     10      )
                    # max_target_length will default to max_length if not specified
                    _lowercase			 =tokenizer.prepare_seqaseq_batch(
                        lowerCAmelCase							,     tgt_texts=lowerCAmelCase							,     max_length=3							,     return_tensors='pt'      )
                    self.assertEqual(batch.input_ids.shape[1]							,     3      )
                    self.assertEqual(batch.labels.shape[1]							,     3      )

                    _lowercase			 =tokenizer.prepare_seqaseq_batch(
                        src_texts=lowerCAmelCase							,     max_length=3							,     max_target_length=10							,     return_tensors='pt'      )
                    self.assertEqual(batch_encoder_only.input_ids.shape[1]							,     3      )
                    self.assertEqual(batch_encoder_only.attention_mask.shape[1]							,     3      )
                    self.assertNotIn('decoder_input_ids'							,     lowerCAmelCase      )




  @unittest.skip('Unfortunately way too slow to build a BPE with SentencePiece.'      )
  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
              with self.subTest(F'''{tokenizer.__class__.__name__} ({pretrained_name})'''      ):
                    _lowercase			 =[AddedToken('<special>'							,     lstrip=lowerCAmelCase      )]

                    _lowercase			 =self.rust_tokenizer_class.from_pretrained(
                        lowerCAmelCase							,     additional_special_tokens=lowerCAmelCase							,     **lowerCAmelCase      )
                    _lowercase			 =tokenizer_r.encode('Hey this is a <special> token'      )

                    _lowercase			 =tokenizer_r.encode('<special>'							,     add_special_tokens=lowerCAmelCase      )[0]

                    self.assertTrue(special_token_id in r_output      )

                    if self.test_slow_tokenizer:
                          _lowercase			 =self.rust_tokenizer_class.from_pretrained(
                              lowerCAmelCase							,     additional_special_tokens=lowerCAmelCase							,     **lowerCAmelCase							,     )
                          _lowercase			 =self.tokenizer_class.from_pretrained(
                              lowerCAmelCase							,     additional_special_tokens=lowerCAmelCase							,     **lowerCAmelCase      )

                          _lowercase			 =tokenizer_p.encode('Hey this is a <special> token'      )

                          _lowercase			 =tokenizer_cr.encode('Hey this is a <special> token'      )

                          self.assertEqual(lowerCAmelCase							,     lowerCAmelCase      )
                          self.assertEqual(lowerCAmelCase							,     lowerCAmelCase      )
                          self.assertTrue(special_token_id in p_output      )
                          self.assertTrue(special_token_id in cr_output      )





@require_torch
@require_sentencepiece
@require_tokenizers
class 				__lowerCAmelCase  (      unittest.TestCase							):
  _a		    =						"""facebook/nllb-200-distilled-600M"""
  _a		    =						[
      """ UN Chief Says There Is No Military Solution in Syria""",
      """ Secretary-General Ban Ki-moon says his response to Russia's stepped up military support for Syria is that \"there is no military solution\" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.""",
  ]
  _a		    =						[
      """Şeful ONU declară că nu există o soluţie militară în Siria""",
      """Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei"""
      """ pentru Siria este că \"nu există o soluţie militară\" la conflictul de aproape cinci ani şi că noi arme nu vor"""
      """ face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.""",
  ]
  _a		    =						[
      256_047,
      16_297,
      134_408,
      8_165,
      248_066,
      14_734,
      950,
      1_135,
      105_721,
      3_573,
      83,
      27_352,
      108,
      49_486,
      2,
  ]




  @classmethod
  def 						A__  (       cls      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =NllbTokenizer.from_pretrained(
            cls.checkpoint_name							,     src_lang='eng_Latn'							,     tgt_lang='ron_Latn'      )
        _lowercase			 =1
        return cls




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Arab']							,     256_001      )
        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Latn']							,     256_002      )
        self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['fra_Latn']							,     256_057      )




  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =self.tokenizer.batch_encode_plus(self.src_text      ).input_ids[0]
        self.assertListEqual(self.expected_src_tokens							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        self.assertIn(lowerCAmelCase							,     self.tokenizer.all_special_ids      )
        # fmt: off
        _lowercase			 =[RO_CODE, 4_254, 98_068, 112_923, 39_072, 3_909, 713, 102_767, 26, 17_314, 35_642, 14_683, 33_118, 2_022, 66_987, 2, 256_047]
        # fmt: on

        _lowercase			 =self.tokenizer.decode(lowerCAmelCase							,     skip_special_tokens=lowerCAmelCase      )
        _lowercase			 =self.tokenizer.decode(generated_ids[1:]							,     skip_special_tokens=lowerCAmelCase      )
        self.assertEqual(lowerCAmelCase							,     lowerCAmelCase      )
        self.assertNotIn(self.tokenizer.eos_token							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =['this is gunna be a long sentence ' * 20]
        assert isinstance(src_text[0]							,     lowerCAmelCase      )
        _lowercase			 =10
        _lowercase			 =self.tokenizer(lowerCAmelCase							,     max_length=lowerCAmelCase							,     truncation=lowerCAmelCase      ).input_ids[0]
        self.assertEqual(ids[-1]							,     2      )
        self.assertEqual(ids[0]							,     lowerCAmelCase      )
        self.assertEqual(len(lowerCAmelCase      )							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR']      )							,     [256_203, 3]      )




  def 						A__  (       self      )						->				Dict:
        '''simple docstring'''






        _lowercase			 =tempfile.mkdtemp()
        _lowercase			 =self.tokenizer.fairseq_tokens_to_ids
        self.tokenizer.save_pretrained(lowerCAmelCase      )
        _lowercase			 =NllbTokenizer.from_pretrained(lowerCAmelCase      )
        self.assertDictEqual(new_tok.fairseq_tokens_to_ids							,     lowerCAmelCase      )




  @require_torch
  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        _lowercase			 =self.tokenizer(
            self.src_text							,     text_target=self.tgt_text							,     padding=lowerCAmelCase							,     truncation=lowerCAmelCase							,     max_length=len(self.expected_src_tokens      )							,     return_tensors='pt'							,     )
        _lowercase			 =shift_tokens_right(
            batch['labels']							,     self.tokenizer.pad_token_id							,     self.tokenizer.lang_code_to_id['ron_Latn']      )

        self.assertIsInstance(lowerCAmelCase							,     lowerCAmelCase      )

        self.assertEqual((2, 15)							,     batch.input_ids.shape      )
        self.assertEqual((2, 15)							,     batch.attention_mask.shape      )
        _lowercase			 =batch.input_ids.tolist()[0]
        self.assertListEqual(self.expected_src_tokens							,     lowerCAmelCase      )
        self.assertEqual(lowerCAmelCase							,     batch.decoder_input_ids[0, 0]      )  # EOS
        # Test that special tokens are reset
        self.assertEqual(self.tokenizer.prefix_tokens							,     [EN_CODE]      )
        self.assertEqual(self.tokenizer.suffix_tokens							,     [self.tokenizer.eos_token_id]      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.tokenizer(self.src_text							,     padding=lowerCAmelCase							,     truncation=lowerCAmelCase							,     max_length=3							,     return_tensors='pt'      )
        _lowercase			 =self.tokenizer(
            text_target=self.tgt_text							,     padding=lowerCAmelCase							,     truncation=lowerCAmelCase							,     max_length=10							,     return_tensors='pt'      )
        _lowercase			 =targets['input_ids']
        _lowercase			 =shift_tokens_right(
            lowerCAmelCase							,     self.tokenizer.pad_token_id							,     decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang]							,     )

        self.assertEqual(batch.input_ids.shape[1]							,     3      )
        self.assertEqual(batch.decoder_input_ids.shape[1]							,     10      )




  @require_torch
  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =self.tokenizer._build_translation_inputs(
            'A test'							,     return_tensors='pt'							,     src_lang='eng_Latn'							,     tgt_lang='fra_Latn'      )

        self.assertEqual(
            nested_simplify(lowerCAmelCase      )							,     {
                # A, test, EOS, en_XX
                'input_ids': [[256_047, 70, 7_356, 2]],
                'attention_mask': [[1, 1, 1, 1]],
                # ar_AR
                'forced_bos_token_id': 256_057,
            }							,     )




  @require_torch
  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =True
        _lowercase			 =self.tokenizer(
            'UN Chief says there is no military solution in Syria'							,     src_lang='eng_Latn'							,     tgt_lang='fra_Latn'      )
        self.assertEqual(
            inputs.input_ids							,     [16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2, 256_047]      )

        _lowercase			 =False
        _lowercase			 =self.tokenizer(
            'UN Chief says there is no military solution in Syria'							,     src_lang='eng_Latn'							,     tgt_lang='fra_Latn'      )
        self.assertEqual(
            inputs.input_ids							,     [256_047, 16_297, 134_408, 25_653, 6_370, 248, 254, 103_929, 94_995, 108, 49_486, 2]      )





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   (    A__			:     List[Any]  )					->   Union[str, Any]:





      """simple docstring"""



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





@require_tf
class 				__lowerCAmelCase  :




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        pass




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase							,     lowerCAmelCase      )

        _lowercase			 =TFVisionTextDualEncoderModel(lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 ={'vision_model': vision_model, 'text_model': text_model}
        _lowercase			 =TFVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				int:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase      )
        _lowercase			 =output[0].numpy()

        with tempfile.TemporaryDirectory() as tmpdirname:
              model.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

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

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






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




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_model(**lowerCAmelCase      )




  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_model_from_pretrained_configs(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_save_load(**lowerCAmelCase      )




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        self.check_vision_text_output_attention(**lowerCAmelCase      )




  @slow
  def 						A__  (       self      )						->				Union[str, Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_pretrained_model_and_inputs()

        _lowercase			 =model_a(**lowerCAmelCase      )
        _lowercase			 =outputs[0].numpy()

        with tempfile.TemporaryDirectory() as tmp_dirname:
              model_a.save_pretrained(lowerCAmelCase      )
              _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase      )

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





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






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

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =TFViTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






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

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

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





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






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

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase=None							,     **lowerCAmelCase      )						->				List[str]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.get_vision_text_model(lowerCAmelCase							,     lowerCAmelCase      )
        _lowercase			 =TFVisionTextDualEncoderModel(vision_model=lowerCAmelCase							,     text_model=lowerCAmelCase      )

        _lowercase			 =model(
            input_ids=lowerCAmelCase							,     pixel_values=lowerCAmelCase							,     attention_mask=lowerCAmelCase							,     output_attentions=lowerCAmelCase      )

        _lowercase			 =output.vision_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     vision_config.num_hidden_layers      )

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

        _lowercase			 =output.text_model_output.attentions
        self.assertEqual(len(lowerCAmelCase      )							,     text_config.num_hidden_layers      )

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




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFDeiTModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFRobertaModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






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

        _lowercase , _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

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





@require_tf
class 				__lowerCAmelCase  (      SCREAMING_SNAKE_CASE    ,    unittest.TestCase							):




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






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

        return model, inputs




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Tuple:
        '''simple docstring'''






        _lowercase			 =TFCLIPVisionModel(lowerCAmelCase							,     name='vision_model'      )
        _lowercase			 =TFBertModel(lowerCAmelCase							,     name='text_model'      )
        return vision_model, text_model




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






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

        _lowercase , _lowercase			 =vision_config_and_inputs

        (
            (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , (
            _lowercase
        ) , 
        )			 =text_config_and_inputs

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





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




  @slow
  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        _lowercase			 =TFVisionTextDualEncoderModel.from_pretrained(
            'clip-italian/clip-italian'							,     logit_scale_init_value=1.0							,     from_pt=lowerCAmelCase      )
        _lowercase			 =VisionTextDualEncoderProcessor.from_pretrained('clip-italian/clip-italian'      )

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

        _lowercase			 =model(**lowerCAmelCase      )

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

        _lowercase			 =np.array([[1.2284727, 0.3104122]]      )

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


