




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


A_      			=       '''▁'''

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






@require_sentencepiece
@require_tokenizers
class       lowercase(  __a ,    unittest.TestCase				):







 '''simple docstring'''





 lowercase__				      =      BigBirdTokenizer
 lowercase__				      =      BigBirdTokenizerFast
 lowercase__				      =      True
 lowercase__				      =      True


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   super().setUp()

   _snake_case							:	Union[str, Any]			=						self.tokenizer_class(a_,   keep_accents=a_	)
   tokenizer.save_pretrained(self.tmpdirname	)


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	Any			=						"""<s>"""
   _snake_case							:	List[Any]			=						1

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


 def 				UpperCamelCase_     (       self:	List[str]	):




   '''simple docstring'''



   _snake_case							:	Tuple			=						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(a_	),   1_004	)


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   self.assertEqual(self.get_tokenizer().vocab_size,   1_000	)


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   if not self.test_rust_tokenizer:
     return

   _snake_case							:	Optional[int]			=						self.get_tokenizer()
   _snake_case							:	Any			=						self.get_rust_tokenizer()

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

   _snake_case							:	Tuple			=						tokenizer.tokenize(a_	)
   _snake_case							:	Tuple			=						rust_tokenizer.tokenize(a_	)
   self.assertListEqual(a_,   a_	)

   _snake_case							:	List[str]			=						tokenizer.encode(a_,   add_special_tokens=a_	)
   _snake_case							:	Optional[Any]			=						rust_tokenizer.encode(a_,   add_special_tokens=a_	)
   self.assertListEqual(a_,   a_	)

   _snake_case							:	Tuple			=						self.get_rust_tokenizer()
   _snake_case							:	Union[str, Any]			=						tokenizer.encode(a_	)
   _snake_case							:	Optional[Any]			=						rust_tokenizer.encode(a_	)
   self.assertListEqual(a_,   a_	)


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   _snake_case							:	int			=						BigBirdTokenizer(a_,   keep_accents=a_	)

   _snake_case							:	int			=						tokenizer.tokenize("""This is a test"""	)
   self.assertListEqual(a_,   ["""▁This""", """▁is""", """▁a""", """▁t""", """est"""]	)

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

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

   _snake_case							:	Optional[Any]			=						tokenizer.convert_ids_to_tokens(a_	)
   self.assertListEqual(
       a_,   [
           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 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   return BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base"""	)


 @slow
 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						"""Hello World!"""
   _snake_case							:	List[str]			=						[65, 18_536, 2_260, 101, 66]

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


 @slow
 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	Any			=						(
       """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
   _snake_case							:	Union[str, Any]			=						[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(a_,   self.big_tokenizer.encode(a_	)	)


 @require_torch
 @slow
 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   import torch

   from transformers import BigBirdConfig, BigBirdModel

   # Build sequence
   _snake_case							:	int			=						list(self.big_tokenizer.get_vocab().keys()	)[:10]
   _snake_case							:	List[Any]			=						""" """.join(a_	)
   _snake_case							:	Dict			=						self.big_tokenizer.encode_plus(a_,   return_tensors="""pt""",   return_token_type_ids=a_	)
   _snake_case							:	List[str]			=						self.big_tokenizer.batch_encode_plus(
       [sequence + """ """ + sequence],   return_tensors="""pt""",   return_token_type_ids=a_	)

   _snake_case							:	Any			=						BigBirdConfig(attention_type="""original_full"""	)
   _snake_case							:	str			=						BigBirdModel(a_	)

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

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


 @slow
 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						BigBirdTokenizer.from_pretrained("""google/bigbird-roberta-base"""	)
   _snake_case							:	Optional[int]			=						tokenizer.decode(tokenizer("""Paris is the [MASK]."""	).input_ids	)

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


 @slow
 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	Any			=						{"""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, 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, 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=a_,   model_name="""google/bigbird-roberta-base""",   revision="""215c99f1600e06f83acce68422f2035b2b5c3510""",   )








"""simple docstring"""


import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases


def       UpperCAmelCase__ (snake_case__							:      Optional[Any]	,     snake_case__							:      Dict					):
  """simple docstring"""

  assert isinstance(snake_case__	,     snake_case__					)
  assert dataset.num_rows == 4
  assert dataset.num_columns == 3
  assert dataset.column_names == ["col_1", "col_2", "col_3"]
  for feature, expected_dtype in expected_features.items():
    assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize("""keep_in_memory"""	,     [False, True]					)
def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      Optional[int]	,     snake_case__							:      Dict					):
  """simple docstring"""

  _snake_case							:	str			=						tmp_path / """cache"""
  _snake_case							:	int			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    _snake_case							:	str			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__	,     keep_in_memory=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


@pytest.mark.parametrize(
    """features"""	,     [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      Dict	,     snake_case__							:      int	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	str			=						tmp_path / """cache"""
  _snake_case							:	List[Any]			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	List[Any]			=						features.copy() if features else default_expected_features
  _snake_case							:	List[Any]			=						(
      Features({feature: Value(snake_case__					) for feature, dtype in features.items()}					) if features is not None else None
  )
  _snake_case							:	Optional[Any]			=						ParquetDatasetReader(snake_case__	,     features=snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


@pytest.mark.parametrize("""split"""	,     [None, NamedSplit("""train"""					), """train""", """test"""]					)
def       UpperCAmelCase__ (snake_case__							:      Optional[int]	,     snake_case__							:      int	,     snake_case__							:      int					):
  """simple docstring"""

  _snake_case							:	List[str]			=						tmp_path / """cache"""
  _snake_case							:	Any			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	int			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__	,     split=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)
  assert dataset.split == split if split else "train"


@pytest.mark.parametrize("""path_type"""	,     [str, list]					)
def       UpperCAmelCase__ (snake_case__							:      Dict	,     snake_case__							:      str	,     snake_case__							:      str					):
  """simple docstring"""

  if issubclass(snake_case__	,     snake_case__					):
    _snake_case							:	Optional[Any]			=						parquet_path
  elif issubclass(snake_case__	,     snake_case__					):
    _snake_case							:	int			=						[parquet_path]
  _snake_case							:	Union[str, Any]			=						tmp_path / """cache"""
  _snake_case							:	Tuple			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	List[str]			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_dataset(snake_case__	,     snake_case__					)


def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      Optional[int]	,     snake_case__							:      str=("train",)					):
  """simple docstring"""

  assert isinstance(snake_case__	,     snake_case__					)
  for split in splits:
    _snake_case							:	int			=						dataset_dict[split]
    assert dataset.num_rows == 4
    assert dataset.num_columns == 3
    assert dataset.column_names == ["col_1", "col_2", "col_3"]
    for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype


@pytest.mark.parametrize("""keep_in_memory"""	,     [False, True]					)
def       UpperCAmelCase__ (snake_case__							:      str	,     snake_case__							:      str	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	Tuple			=						tmp_path / """cache"""
  _snake_case							:	Optional[int]			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    _snake_case							:	Tuple			=						ParquetDatasetReader(
        {"""train""": parquet_path}	,     cache_dir=snake_case__	,     keep_in_memory=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__					)


@pytest.mark.parametrize(
    """features"""	,     [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      List[str]	,     snake_case__							:      Tuple	,     snake_case__							:      List[Any]					):
  """simple docstring"""

  _snake_case							:	Optional[int]			=						tmp_path / """cache"""
  _snake_case							:	Dict			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	Optional[Any]			=						features.copy() if features else default_expected_features
  _snake_case							:	Dict			=						(
      Features({feature: Value(snake_case__					) for feature, dtype in features.items()}					) if features is not None else None
  )
  _snake_case							:	Optional[int]			=						ParquetDatasetReader({"""train""": parquet_path}	,     features=snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__					)


@pytest.mark.parametrize("""split"""	,     [None, NamedSplit("""train"""					), """train""", """test"""]					)
def       UpperCAmelCase__ (snake_case__							:      List[str]	,     snake_case__							:      Optional[Any]	,     snake_case__							:      Tuple					):
  """simple docstring"""

  if split:
    _snake_case							:	int			=						{split: parquet_path}
  else:
    _snake_case							:	Optional[Any]			=						"""train"""
    _snake_case							:	int			=						{"""train""": parquet_path, """test""": parquet_path}
  _snake_case							:	Dict			=						tmp_path / """cache"""
  _snake_case							:	Any			=						{"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
  _snake_case							:	Union[str, Any]			=						ParquetDatasetReader(snake_case__	,     cache_dir=snake_case__					).read()
  _check_parquet_datasetdict(snake_case__	,     snake_case__	,     splits=list(path.keys()					)					)
  assert all(dataset[split].split == split for split in path.keys()					)


def       UpperCAmelCase__ (snake_case__							:      Tuple	,     snake_case__							:      Tuple					):
  """simple docstring"""

  _snake_case							:	List[Any]			=						ParquetDatasetWriter(snake_case__	,     tmp_path / """foo.parquet"""					)
  assert writer.write() > 0
  _snake_case							:	str			=						pq.ParquetFile(tmp_path / """foo.parquet"""					)
  _snake_case							:	int			=						pf.read()
  assert dataset.data.table == output_table


def       UpperCAmelCase__ (snake_case__							:      int	,     snake_case__							:      int					):
  """simple docstring"""

  _snake_case							:	Optional[Any]			=						str(shared_datadir / """test_image_rgb.jpg"""					)
  _snake_case							:	Tuple			=						{"""image""": [image_path]}
  _snake_case							:	Optional[int]			=						Features({"""image""": Image()}					)
  _snake_case							:	int			=						Dataset.from_dict(snake_case__	,     features=snake_case__					)
  _snake_case							:	Optional[Any]			=						ParquetDatasetWriter(snake_case__	,     tmp_path / """foo.parquet"""					)
  assert writer.write() > 0

  _snake_case							:	List[str]			=						Dataset.from_parquet(str(tmp_path / """foo.parquet"""					)					)
  assert dataset.features == reloaded_dataset.features

  _snake_case							:	Optional[Any]			=						ParquetDatasetReader(str(tmp_path / """foo.parquet"""					)	,     streaming=snake_case__					).read()
  assert dataset.features == reloaded_iterable_dataset.features






@pytest.mark.parametrize(
    """feature, expected"""	,     [
        (Features({"""foo""": Value("""int32"""					)}					), None),
        (Features({"""image""": Image(), """foo""": Value("""int32"""					)}					), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({"""nested""": Sequence(Audio()					)}					), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]	,     )
def       UpperCAmelCase__ (snake_case__							:      List[Any]	,     snake_case__							:      str					):
  """simple docstring"""

  assert get_writer_batch_size(snake_case__					) == expected


