
import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
   from transformers import (
       FlaxBertModel,
       FlaxCLIPVisionModel,
       FlaxVisionTextDualEncoderModel,
       FlaxViTModel,
       VisionTextDualEncoderConfig,
       VisionTextDualEncoderProcessor,
   )
   from transformers.modeling_flax_pytorch_utils import (
       convert_pytorch_state_dict_to_flax,
       load_flax_weights_in_pytorch_model,
   )


if is_torch_available():
   import torch

   from transformers import VisionTextDualEncoderModel

if is_vision_available():
   from PIL import Image


def 			UpperCamelCase					(    __lowerCamelCase     : List[str]    ):




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






@require_flax
class   UpperCAmelCase		:



   def 				_SCREAMING_SNAKE_CASE   (self		:					Any    ,						snake_case__		:					List[Any]    ,						snake_case__		:					List[str] )		->	List[str]:


          '''simple docstring'''


          pass



   def 				_SCREAMING_SNAKE_CASE   (self		:					int )		->	Any:


          '''simple docstring'''


          pass



   def 				_SCREAMING_SNAKE_CASE   (self		:					int )		->	Union[str, Any]:


          '''simple docstring'''


          pass



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[int]    ,						snake_case__		:					Any    ,						snake_case__		:					Dict    ,						snake_case__		:					Dict )		->	Union[str, Any]:


          '''simple docstring'''


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



   def 				_SCREAMING_SNAKE_CASE   (self		:					Union[str, Any]    ,						snake_case__		:					str    ,						snake_case__		:					Tuple    ,						snake_case__		:					int    ,						snake_case__		:					List[Any]    ,						snake_case__		:					Any=None    ,						**snake_case__		:					Optional[int] )		->	Optional[int]:


          '''simple docstring'''


          snake_case       :   List[str]							=      VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__    ,						snake_case__ )

          snake_case       :   List[str]							=      FlaxVisionTextDualEncoderModel(snake_case__ )

          snake_case       :   Dict							=      model(input_ids=snake_case__    ,						pixel_values=snake_case__    ,						attention_mask=snake_case__ )

          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		:					Tuple    ,						snake_case__		:					List[Any]    ,						snake_case__		:					Any    ,						snake_case__		:					Union[str, Any]    ,						snake_case__		:					List[str]    ,						snake_case__		:					str=None    ,						**snake_case__		:					Any )		->	Optional[Any]:


          '''simple docstring'''


          snake_case				,					snake_case       :   Dict							=      self.get_vision_text_model(snake_case__    ,						snake_case__ )
          snake_case       :   Optional[int]							=      {"vision_model": vision_model, "text_model": text_model}
          snake_case       :   str							=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__ )

          snake_case       :   Union[str, Any]							=      model(input_ids=snake_case__    ,						pixel_values=snake_case__    ,						attention_mask=snake_case__ )

          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		:					Any    ,						snake_case__		:					Union[str, Any]    ,						snake_case__		:					List[str]    ,						snake_case__		:					Any    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					Optional[int]=None    ,						**snake_case__		:					int )		->	Union[str, Any]:


          '''simple docstring'''


          snake_case				,					snake_case       :   Optional[int]							=      self.get_vision_text_model(snake_case__    ,						snake_case__ )
          snake_case       :   Any							=      {"vision_model": vision_model, "text_model": text_model}
          snake_case       :   Dict							=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__ )

          snake_case       :   List[str]							=      model(input_ids=snake_case__    ,						pixel_values=snake_case__    ,						attention_mask=snake_case__ )
          snake_case       :   int							=      output[0]

          with tempfile.TemporaryDirectory() as tmpdirname:
                 model.save_pretrained(snake_case__ )
                 snake_case       :   Optional[Any]							=      FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__ )

                 snake_case       :   str							=      model(input_ids=snake_case__    ,						pixel_values=snake_case__    ,						attention_mask=snake_case__ )
                 snake_case       :   List[Any]							=      after_output[0]
                 snake_case       :   Union[str, Any]							=      np.amax(np.abs(out_a - out_a ) )
                 self.assertLessEqual(snake_case__    ,						1e-3 )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Union[str, Any]    ,						snake_case__		:					Dict    ,						snake_case__		:					Tuple    ,						snake_case__		:					Optional[Any]    ,						snake_case__		:					List[Any]    ,						snake_case__		:					Union[str, Any]=None    ,						**snake_case__		:					str )		->	Optional[int]:


          '''simple docstring'''


          snake_case				,					snake_case       :   Any							=      self.get_vision_text_model(snake_case__    ,						snake_case__ )
          snake_case       :   Dict							=      {"vision_model": vision_model, "text_model": text_model}
          snake_case       :   List[str]							=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case__ )

          snake_case       :   List[Any]							=      model(
              input_ids=snake_case__    ,						pixel_values=snake_case__    ,						attention_mask=snake_case__    ,						output_attentions=snake_case__ )

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

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

          snake_case       :   int							=      output.text_model_output.attentions
          self.assertEqual(len(snake_case__ )    ,						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		:					str    ,						snake_case__		:					Tuple    ,						snake_case__		:					Dict    ,						snake_case__		:					int )		->	Dict:


          '''simple docstring'''


          pt_model.to(snake_case__ )
          pt_model.eval()

          # prepare inputs
          snake_case       :   Dict							=      inputs_dict
          snake_case       :   Dict							=      {k: torch.tensor(v.tolist() ) for k, v in flax_inputs.items()}

          with torch.no_grad():
                 snake_case       :   int							=      pt_model(**snake_case__ ).to_tuple()

          snake_case       :   Tuple							=      fx_model(**snake_case__ ).to_tuple()
          self.assertEqual(len(snake_case__ )    ,						len(snake_case__ )    ,						"Output lengths differ between Flax and PyTorch" )
          for fx_output, pt_output in zip(fx_outputs[:4]    ,						pt_outputs[:4] ):
                 self.assert_almost_equals(snake_case__    ,						pt_output.numpy()    ,						4e-2 )

          # PT -> Flax
          with tempfile.TemporaryDirectory() as tmpdirname:
                 pt_model.save_pretrained(snake_case__ )
                 snake_case       :   Union[str, Any]							=      FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__    ,						from_pt=snake_case__ )

          snake_case       :   Union[str, Any]							=      fx_model_loaded(**snake_case__ ).to_tuple()
          self.assertEqual(len(snake_case__ )    ,						len(snake_case__ )    ,						"Output lengths differ between Flax and PyTorch" )
          for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4]    ,						pt_outputs[:4] ):
                 self.assert_almost_equals(snake_case__    ,						pt_output.numpy()    ,						4e-2 )

          # Flax -> PT
          with tempfile.TemporaryDirectory() as tmpdirname:
                 fx_model.save_pretrained(snake_case__ )
                 snake_case       :   Dict							=      VisionTextDualEncoderModel.from_pretrained(snake_case__    ,						from_flax=snake_case__ )

          pt_model_loaded.to(snake_case__ )
          pt_model_loaded.eval()

          with torch.no_grad():
                 snake_case       :   Optional[Any]							=      pt_model_loaded(**snake_case__ ).to_tuple()

          self.assertEqual(len(snake_case__ )    ,						len(snake_case__ )    ,						"Output lengths differ between Flax and PyTorch" )
          for fx_output, pt_output_loaded in zip(fx_outputs[:4]    ,						pt_outputs_loaded[:4] ):
                 self.assert_almost_equals(snake_case__    ,						pt_output_loaded.numpy()    ,						4e-2 )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[Any]    ,						snake_case__		:					List[str]    ,						snake_case__		:					Union[str, Any]    ,						snake_case__		:					List[Any] )		->	Dict:


          '''simple docstring'''


          snake_case       :   Optional[int]							=      VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__    ,						snake_case__ )

          snake_case       :   List[str]							=      VisionTextDualEncoderModel(snake_case__ )
          snake_case       :   Tuple							=      FlaxVisionTextDualEncoderModel(snake_case__ )

          snake_case       :   int							=      convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,						snake_case__ )
          snake_case       :   str							=      fx_state

          self.check_pt_flax_equivalence(snake_case__    ,						snake_case__    ,						snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Union[str, Any]    ,						snake_case__		:					Optional[int]    ,						snake_case__		:					List[Any]    ,						snake_case__		:					Union[str, Any] )		->	Any:


          '''simple docstring'''


          snake_case       :   List[Any]							=      VisionTextDualEncoderConfig.from_vision_text_configs(snake_case__    ,						snake_case__ )

          snake_case       :   Union[str, Any]							=      VisionTextDualEncoderModel(snake_case__ )
          snake_case       :   List[Any]							=      FlaxVisionTextDualEncoderModel(snake_case__ )

          snake_case       :   Dict							=      load_flax_weights_in_pytorch_model(snake_case__    ,						fx_model.params )

          self.check_pt_flax_equivalence(snake_case__    ,						snake_case__    ,						snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					List[str] )		->	Any:


          '''simple docstring'''


          snake_case       :   Dict							=      self.prepare_config_and_inputs()
          self.check_model_from_pretrained_configs(**snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[Any] )		->	List[Any]:


          '''simple docstring'''


          snake_case       :   Union[str, Any]							=      self.prepare_config_and_inputs()
          self.check_vision_text_dual_encoder_from_pretrained(**snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[Any] )		->	Dict:


          '''simple docstring'''


          snake_case       :   List[str]							=      self.prepare_config_and_inputs()
          self.check_save_load(**snake_case__ )



   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	Dict:


          '''simple docstring'''


          snake_case       :   Optional[Any]							=      self.prepare_config_and_inputs()
          self.check_vision_text_output_attention(**snake_case__ )



   @is_pt_flax_cross_test
   def 				_SCREAMING_SNAKE_CASE   (self		:					Any )		->	str:


          '''simple docstring'''


          snake_case       :   Optional[int]							=      self.prepare_config_and_inputs()
          snake_case       :   List[Any]							=      config_inputs_dict.pop("vision_config" )
          snake_case       :   int							=      config_inputs_dict.pop("text_config" )

          snake_case       :   List[str]							=      config_inputs_dict

          self.check_equivalence_pt_to_flax(snake_case__    ,						snake_case__    ,						snake_case__ )
          self.check_equivalence_flax_to_pt(snake_case__    ,						snake_case__    ,						snake_case__ )



   @slow
   def 				_SCREAMING_SNAKE_CASE   (self		:					Any )		->	Optional[Any]:


          '''simple docstring'''


          snake_case				,					snake_case       :   Tuple							=      self.get_pretrained_model_and_inputs()

          snake_case       :   Tuple							=      model_a(**snake_case__ )
          snake_case       :   List[Any]							=      outputs[0]

          with tempfile.TemporaryDirectory() as tmp_dirname:
                 model_a.save_pretrained(snake_case__ )
                 snake_case       :   Union[str, Any]							=      FlaxVisionTextDualEncoderModel.from_pretrained(snake_case__ )

                 snake_case       :   str							=      model_a(**snake_case__ )
                 snake_case       :   str							=      after_outputs[0]
                 snake_case       :   Dict							=      np.amax(np.abs(out_a - out_a ) )
                 self.assertLessEqual(snake_case__    ,						1e-5 )






@require_flax
class   UpperCAmelCase		(						_UpperCAmelCase       ,unittest.TestCase ):



   def 				_SCREAMING_SNAKE_CASE   (self		:					str )		->	Optional[int]:


          '''simple docstring'''


          snake_case       :   List[Any]							=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
              "hf-internal-testing/tiny-random-vit"    ,						"hf-internal-testing/tiny-bert"    ,						vision_from_pt=snake_case__    ,						text_from_pt=snake_case__    ,						)
          snake_case       :   Optional[Any]							=      13
          snake_case       :   List[Any]							=      floats_tensor(
              [
                  batch_size,
                  model.config.vision_config.num_channels,
                  model.config.vision_config.image_size,
                  model.config.vision_config.image_size,
              ] )
          snake_case       :   Dict							=      ids_tensor([batch_size, 4]    ,						model.config.text_config.vocab_size )
          snake_case       :   List[Any]							=      random_attention_mask([batch_size, 4] )
          snake_case       :   int							=      {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs



   def 				_SCREAMING_SNAKE_CASE   (self		:					Optional[Any]    ,						snake_case__		:					Any    ,						snake_case__		:					Union[str, Any] )		->	Dict:


          '''simple docstring'''


          snake_case       :   List[Any]							=      FlaxViTModel(snake_case__ )
          snake_case       :   Union[str, Any]							=      FlaxBertModel(snake_case__ )
          return vision_model, text_model



   def 				_SCREAMING_SNAKE_CASE   (self		:					Union[str, Any] )		->	Tuple:


          '''simple docstring'''


          snake_case       :   Any							=      FlaxViTModelTester(self )
          snake_case       :   Optional[Any]							=      FlaxBertModelTester(self )
          snake_case       :   Dict							=      vit_model_tester.prepare_config_and_inputs()
          snake_case       :   List[str]							=      bert_model_tester.prepare_config_and_inputs()

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

          snake_case				,					snake_case				,					snake_case				,					snake_case       :   int							=      text_config_and_inputs

          # make sure that cross attention layers are added
          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": attention_mask,
              "input_ids": input_ids,
              "token_type_ids": token_type_ids,
          }






@require_torch
class   UpperCAmelCase		(						_UpperCAmelCase       ,unittest.TestCase ):



   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple )		->	str:


          '''simple docstring'''


          snake_case       :   int							=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
              "hf-internal-testing/tiny-random-clip"    ,						"hf-internal-testing/tiny-bert"    ,						vision_from_pt=snake_case__    ,						text_from_pt=snake_case__    ,						)
          snake_case       :   List[Any]							=      13
          snake_case       :   List[Any]							=      floats_tensor(
              [
                  batch_size,
                  model.config.vision_config.num_channels,
                  model.config.vision_config.image_size,
                  model.config.vision_config.image_size,
              ] )
          snake_case       :   int							=      ids_tensor([batch_size, 4]    ,						model.config.text_config.vocab_size )
          snake_case       :   int							=      random_attention_mask([batch_size, 4] )
          snake_case       :   int							=      {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

          return model, inputs



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict    ,						snake_case__		:					str    ,						snake_case__		:					Optional[int] )		->	Optional[Any]:


          '''simple docstring'''


          snake_case       :   Union[str, Any]							=      FlaxCLIPVisionModel(snake_case__ )
          snake_case       :   str							=      FlaxBertModel(snake_case__ )
          return vision_model, text_model



   def 				_SCREAMING_SNAKE_CASE   (self		:					Dict )		->	Optional[int]:


          '''simple docstring'''


          snake_case       :   int							=      FlaxCLIPVisionModelTester(self )
          snake_case       :   List[Any]							=      FlaxBertModelTester(self )
          snake_case       :   Union[str, Any]							=      clip_model_tester.prepare_config_and_inputs()
          snake_case       :   str							=      bert_model_tester.prepare_config_and_inputs()

          snake_case				,					snake_case       :   str							=      vision_config_and_inputs

          snake_case				,					snake_case				,					snake_case				,					snake_case       :   List[str]							=      text_config_and_inputs

          # make sure that cross attention layers are added
          return {
              "text_config": text_config,
              "vision_config": vision_config,
              "pixel_values": pixel_values,
              "attention_mask": attention_mask,
              "input_ids": input_ids,
              "token_type_ids": token_type_ids,
          }






@require_flax
@require_vision
class   UpperCAmelCase		(						unittest.TestCase ):



   @slow
   def 				_SCREAMING_SNAKE_CASE   (self		:					Tuple )		->	int:


          '''simple docstring'''


          snake_case       :   List[str]							=      FlaxVisionTextDualEncoderModel.from_pretrained("clip-italian/clip-italian"    ,						logit_scale_init_value=1.0 )
          snake_case       :   int							=      VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian" )

          snake_case       :   Tuple							=      Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png" )
          snake_case       :   Optional[Any]							=      processor(
              text=["una foto di un gatto", "una foto di un cane"]    ,						images=snake_case__    ,						padding=snake_case__    ,						return_tensors="np" )

          snake_case       :   int							=      model(**snake_case__ )

          # 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])    ,						)

          snake_case       :   str							=      np.array([[1.2284727, 0.3104122]] )

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












"""simple docstring"""





import unittest

from transformers import BigBirdTokenizer, BigBirdTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, require_torch, slow
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


SCREAMING_SNAKE_CASE__					=   "▁"

SCREAMING_SNAKE_CASE__					=   get_tests_dir("fixtures/test_sentencepiece.model")

@require_sentencepiece
@require_tokenizers
class 		lowercase					(					_UpperCAmelCase     ,  unittest.TestCase					):
      _SCREAMING_SNAKE_CASE					=      BigBirdTokenizer
      _SCREAMING_SNAKE_CASE					=      BigBirdTokenizerFast
      _SCREAMING_SNAKE_CASE					=      True
      _SCREAMING_SNAKE_CASE					=      True





      def     _snake_case			(       self )		->     List[str]:
            super().setUp()

            lowerCAmelCase  						=       self.tokenizer_class(lowercase      ,						keep_accents=lowercase )
            tokenizer.save_pretrained(self.tmpdirname )





      def     _snake_case			(       self )		->     List[Any]:
            lowerCAmelCase  						=       """<s>"""
            lowerCAmelCase  						=       1

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





      def     _snake_case			(       self )		->     List[str]:
            lowerCAmelCase  						=       list(self.get_tokenizer().get_vocab().keys() )

            self.assertEqual(vocab_keys[0]      ,						"""<unk>""" )
            self.assertEqual(vocab_keys[1]      ,						"""<s>""" )
            self.assertEqual(vocab_keys[-1]      ,						"""[MASK]""" )
            self.assertEqual(len(lowercase )      ,						1_004 )





      def     _snake_case			(       self )		->     Union[str, Any]:
            self.assertEqual(self.get_tokenizer().vocab_size      ,						1_000 )





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

            lowerCAmelCase  						=       self.get_tokenizer()
            lowerCAmelCase  						=       self.get_rust_tokenizer()

            lowerCAmelCase  						=       """I was born in 92000, and this is falsé."""

            lowerCAmelCase  						=       tokenizer.tokenize(lowercase )
            lowerCAmelCase  						=       rust_tokenizer.tokenize(lowercase )
            self.assertListEqual(lowercase      ,						lowercase )

            lowerCAmelCase  						=       tokenizer.encode(lowercase      ,						add_special_tokens=lowercase )
            lowerCAmelCase  						=       rust_tokenizer.encode(lowercase      ,						add_special_tokens=lowercase )
            self.assertListEqual(lowercase      ,						lowercase )

            lowerCAmelCase  						=       self.get_rust_tokenizer()
            lowerCAmelCase  						=       tokenizer.encode(lowercase )
            lowerCAmelCase  						=       rust_tokenizer.encode(lowercase )
            self.assertListEqual(lowercase      ,						lowercase )





      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       BigBirdTokenizer(lowercase      ,						keep_accents=lowercase )

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

            self.assertListEqual(
                tokenizer.convert_tokens_to_ids(lowercase )      ,						[285, 46, 10, 170, 382]      ,						)

            lowerCAmelCase  						=       tokenizer.tokenize("""I was born in 92000, and this is falsé.""" )
            self.assertListEqual(
                lowercase      ,						[
                    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""",
                    """é""",
                    """.""",
                ]      ,						)
            lowerCAmelCase  						=       tokenizer.convert_tokens_to_ids(lowercase )
            self.assertListEqual(
                lowercase      ,						[8, 21, 84, 55, 24, 19, 7, 0, 602, 347, 347, 347, 3, 12, 66, 46, 72, 80, 6, 0, 4]      ,						)

            lowerCAmelCase  						=       tokenizer.convert_ids_to_tokens(lowercase )
            self.assertListEqual(
                lowercase      ,						[
                    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>""",
                    """.""",
                ]      ,						)





      @cached_property
      def     _snake_case			(       self )		->     Tuple:
            return BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base""" )





      @slow
      def     _snake_case			(       self )		->     Tuple:
            lowerCAmelCase  						=       """Hello World!"""
            lowerCAmelCase  						=       [65, 18_536, 2_260, 101, 66]

            self.assertListEqual(lowercase      ,						self.big_tokenizer.encode(lowercase ) )





      @slow
      def     _snake_case			(       self )		->     int:
            lowerCAmelCase  						=       (
                """This is a very long text with a lot of weird characters, such as: . , ~ ? ( ) \" [ ] ! : - . Also we will"""
                """ add words that should not exsist and be tokenized to <unk>, such as saoneuhaoesuth"""
            )
            # fmt: off
            lowerCAmelCase  						=       [65, 871, 419, 358, 946, 991, 2_521, 452, 358, 1_357, 387, 7_751, 3_536, 112, 985, 456, 126, 865, 938, 5_400, 5_734, 458, 1_368, 467, 786, 2_462, 5_246, 1_159, 633, 865, 4_519, 457, 582, 852, 2_557, 427, 916, 508, 405, 34_324, 497, 391, 408, 11_342, 1_244, 385, 100, 938, 985, 456, 574, 362, 12_597, 3_200, 3_129, 1_172, 66]  # noqa: E231
            # fmt: on
            self.assertListEqual(lowercase      ,						self.big_tokenizer.encode(lowercase ) )





      @require_torch
      @slow
      def     _snake_case			(       self )		->     Tuple:
            import torch

            from transformers import BigBirdConfig, BigBirdModel

            # Build sequence
            lowerCAmelCase  						=       list(self.big_tokenizer.get_vocab().keys() )[:10]
            lowerCAmelCase  						=       """ """.join(lowercase )
            lowerCAmelCase  						=       self.big_tokenizer.encode_plus(lowercase      ,						return_tensors="""pt"""      ,						return_token_type_ids=lowercase )
            lowerCAmelCase  						=       self.big_tokenizer.batch_encode_plus(
                [sequence + """ """ + sequence]      ,						return_tensors="""pt"""      ,						return_token_type_ids=lowercase )

            lowerCAmelCase  						=       BigBirdConfig(attention_type="""original_full""" )
            lowerCAmelCase  						=       BigBirdModel(lowercase )

            assert model.get_input_embeddings().weight.shape[0] >= self.big_tokenizer.vocab_size

            with torch.no_grad():
                  model(**lowercase )
                  model(**lowercase )





      @slow
      def     _snake_case			(       self )		->     List[str]:
            lowerCAmelCase  						=       BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base""" )
            lowerCAmelCase  						=       tokenizer.decode(tokenizer("""Paris is the [MASK].""" ).input_ids )

            self.assertTrue(decoded_text == """[CLS] Paris is the[MASK].[SEP]""" )





      @slow
      def     _snake_case			(       self )		->     Optional[int]:
            # fmt: off
            lowerCAmelCase  						=       {"""input_ids""": [[65, 39_286, 458, 36_335, 2_001, 456, 13_073, 13_266, 455, 113, 7_746, 1_741, 11_157, 391, 13_073, 13_266, 455, 113, 3_967, 35_412, 113, 4_936, 109, 3_870, 2_377, 113, 30_084, 45_720, 458, 134, 17_496, 112, 503, 11_672, 113, 118, 112, 5_665, 13_347, 38_687, 112, 1_496, 31_389, 112, 3_268, 47_264, 134, 962, 112, 16_377, 8_035, 23_130, 430, 12_169, 15_518, 28_592, 458, 146, 41_697, 109, 391, 12_169, 15_518, 16_689, 458, 146, 41_358, 109, 452, 726, 4_034, 111, 763, 35_412, 5_082, 388, 1_903, 111, 9_051, 391, 2_870, 48_918, 1_900, 1_123, 550, 998, 112, 9_586, 15_985, 455, 391, 410, 22_955, 37_636, 114, 66], [65, 448, 17_496, 419, 3_663, 385, 763, 113, 27_533, 2_870, 3_283, 13_043, 1_639, 24_713, 523, 656, 24_013, 18_550, 2_521, 517, 27_014, 21_244, 420, 1_212, 1_465, 391, 927, 4_833, 388, 578, 11_786, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [65, 484, 2_169, 7_687, 21_932, 18_146, 726, 363, 17_032, 3_391, 114, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], """attention_mask""": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
            # fmt: on

            self.tokenizer_integration_test_util(
                expected_encoding=lowercase      ,						model_name="""google/bigbird-roberta-base"""      ,						revision="""215c99f1600e06f83acce68422f2035b2b5c3510"""      ,						)






