




'''simple docstring'''




import unittest

from transformers import CamembertTokenizer, CamembertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow
from transformers.utils import is_torch_available

from ...test_tokenization_common import TokenizerTesterMixin


UpperCAmelCase_												=   get_tests_dir('fixtures/test_sentencepiece.model')
UpperCAmelCase_												=   get_tests_dir('fixtures/test_sentencepiece_bpe.model')

UpperCAmelCase_												=   "pt" if is_torch_available() else "tf"

@require_sentencepiece
@require_tokenizers
class     lowerCAmelCase_ (			lowercase_      ,	unittest.TestCase ):







  '''simple docstring'''







  lowerCAmelCase_					:       List[str]					  =    CamembertTokenizer
  lowerCAmelCase_					:       str					  =    CamembertTokenizerFast
  lowerCAmelCase_					:       str					  =    True
  lowerCAmelCase_					:       Union[str, Any]					  =    True


  def 				SCREAMING_SNAKE_CASE__      (    self     :						List[str]			):






   """simple docstring"""






   super().setUp()

   # We have a SentencePiece fixture for testing
   UpperCAmelCase__      				=     CamembertTokenizer(UpperCamelCase_			)
   tokenizer.save_pretrained(self.tmpdirname			)


  def 				SCREAMING_SNAKE_CASE__      (    self     :						Optional[int]			):






   """simple docstring"""






   UpperCAmelCase__      				=     """<pad>"""
   UpperCAmelCase__      				=     1

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


  def 				SCREAMING_SNAKE_CASE__      (    self     :						List[str]			):






   """simple docstring"""






   UpperCAmelCase__      				=     list(self.get_tokenizer().get_vocab().keys()			)

   self.assertEqual(vocab_keys[0]    ,		"""<s>NOTUSED"""			)
   self.assertEqual(vocab_keys[1]    ,		"""<pad>"""			)
   self.assertEqual(vocab_keys[-1]    ,		"""<mask>"""			)
   self.assertEqual(len(UpperCamelCase_			)    ,		10_04			)


  def 				SCREAMING_SNAKE_CASE__      (    self     :						int			):






   """simple docstring"""






   self.assertEqual(self.get_tokenizer().vocab_size    ,		10_05			)


  def 				SCREAMING_SNAKE_CASE__      (    self     :						Tuple			):






   """simple docstring"""






   UpperCAmelCase__      				=     CamembertTokenizer(UpperCamelCase_			)
   tokenizer.save_pretrained(self.tmpdirname			)
   UpperCAmelCase__      				=     CamembertTokenizerFast.from_pretrained(self.tmpdirname			)

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

   UpperCAmelCase__      				=     tokenizer.encode(UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.encode(UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)

   UpperCAmelCase__      				=     tokenizer.encode(UpperCamelCase_    ,		add_special_tokens=UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.encode(UpperCamelCase_    ,		add_special_tokens=UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)

   # <unk> tokens are not the same for `rust` than for `slow`.
   # Because spm gives back raw token instead of `unk` in EncodeAsPieces
   # tokens = tokenizer.tokenize(sequence)
   UpperCAmelCase__      				=     tokenizer.convert_ids_to_tokens(UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.tokenize(UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)


  def 				SCREAMING_SNAKE_CASE__      (    self     :						Union[str, Any]			):






   """simple docstring"""






   if not self.test_rust_tokenizer:
    return

   UpperCAmelCase__      				=     self.get_tokenizer()
   UpperCAmelCase__      				=     self.get_rust_tokenizer()

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

   UpperCAmelCase__      				=     tokenizer.tokenize(UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.tokenize(UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)

   UpperCAmelCase__      				=     tokenizer.encode(UpperCamelCase_    ,		add_special_tokens=UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.encode(UpperCamelCase_    ,		add_special_tokens=UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)

   UpperCAmelCase__      				=     self.get_rust_tokenizer()
   UpperCAmelCase__      				=     tokenizer.encode(UpperCamelCase_			)
   UpperCAmelCase__      				=     rust_tokenizer.encode(UpperCamelCase_			)
   self.assertListEqual(UpperCamelCase_    ,		UpperCamelCase_			)



  @slow
  def 				SCREAMING_SNAKE_CASE__      (    self     :						List[str]			):






   """simple docstring"""






   UpperCAmelCase__      				=     {"""input_ids""": [[5, 54, 71_96, 2_97, 30, 23, 7_76, 18, 11, 32_15, 37_05, 82_52, 22, 31_64, 11_81, 21_16, 29, 16, 8_13, 25, 7_91, 33_14, 20, 34_46, 38, 2_75_75, 1_20, 6, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [5, 4_68, 17, 11, 90_88, 20, 15_17, 8, 2_28_04, 1_88_18, 10, 38, 6_29, 6_07, 6_07, 1_42, 19, 71_96, 8_67, 56, 1_03_26, 24, 22_67, 20, 4_16, 50_72, 1_56_12, 2_33, 7_34, 7, 23_99, 27, 16, 30_15, 16_49, 7, 24, 20, 43_38, 23_99, 27, 13, 34_00, 14, 13, 61_89, 8, 9_30, 9, 6]], """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, 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, 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]]}  # noqa: E501
   # fmt: on

   # camembert is a french model. So we also use french texts.
   UpperCAmelCase__      				=     [
       """Le transformeur est un modèle d'apprentissage profond introduit en 2017, """
       """utilisé principalement dans le domaine du traitement automatique des langues (TAL).""",
       """À l'instar des réseaux de neurones récurrents (RNN), les transformeurs sont conçus """
       """pour gérer des données séquentielles, telles que le langage naturel, pour des tâches """
       """telles que la traduction et la synthèse de texte.""",
   ]

   self.tokenizer_integration_test_util(
       expected_encoding=UpperCamelCase_    ,		model_name="""camembert-base"""    ,		revision="""3a0641d9a1aeb7e848a74299e7e4c4bca216b4cf"""    ,		sequences=UpperCamelCase_    ,		)








from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a   :							Optional[Any]						= logging.get_logger(__name__)

__a   :							Dict						= {
    "google/realm-cc-news-pretrained-embedder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-encoder": (
        "https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-scorer": (
        "https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"
    ),
    "google/realm-cc-news-pretrained-openqa": (
        "https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"
    ),
    "google/realm-orqa-nq-openqa": "https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json",
    "google/realm-orqa-nq-reader": "https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json",
    "google/realm-orqa-wq-openqa": "https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json",
    "google/realm-orqa-wq-reader": "https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json",
    # See all REALM models at https://huggingface.co/models?filter=realm
}


class    __lowercase   (						lowercase_	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					"realm"




     def __init__(					self							:      str			,						UpperCamelCase_							:      List[Any]=30_522			,						UpperCamelCase_							:      Dict=768			,						UpperCamelCase_							:      Union[str, Any]=128			,						UpperCamelCase_							:      Tuple=12			,						UpperCamelCase_							:      Optional[Any]=12			,						UpperCamelCase_							:      Optional[int]=8			,						UpperCamelCase_							:      str=3_072			,						UpperCamelCase_							:      List[str]="gelu_new"			,						UpperCamelCase_							:      Optional[int]=0.1			,						UpperCamelCase_							:      Optional[int]=0.1			,						UpperCamelCase_							:      str=512			,						UpperCamelCase_							:      int=2			,						UpperCamelCase_							:      str=0.02			,						UpperCamelCase_							:      List[Any]=1e-12			,						UpperCamelCase_							:      str=256			,						UpperCamelCase_							:      List[str]=10			,						UpperCamelCase_							:      int=1e-3			,						UpperCamelCase_							:      List[str]=5			,						UpperCamelCase_							:      Tuple=320			,						UpperCamelCase_							:      List[str]=13_353_718			,						UpperCamelCase_							:      Tuple=5_000			,						UpperCamelCase_							:      Optional[int]=1			,						UpperCamelCase_							:      List[str]=0			,						UpperCamelCase_							:      Union[str, Any]=2			,						**UpperCamelCase_							:      List[Any]			,						):


         """simple docstring"""



         super().__init__(pad_token_id=UpperCamelCase_			,						bos_token_id=UpperCamelCase_			,						eos_token_id=UpperCamelCase_			,						**UpperCamelCase_					)

         # Common config
         __A       =						vocab_size
         __A       =						max_position_embeddings
         __A       =						hidden_size
         __A       =						retriever_proj_size
         __A       =						num_hidden_layers
         __A       =						num_attention_heads
         __A       =						num_candidates
         __A       =						intermediate_size
         __A       =						hidden_act
         __A       =						hidden_dropout_prob
         __A       =						attention_probs_dropout_prob
         __A       =						initializer_range
         __A       =						type_vocab_size
         __A       =						layer_norm_eps

         # Reader config
         __A       =						span_hidden_size
         __A       =						max_span_width
         __A       =						reader_layer_norm_eps
         __A       =						reader_beam_size
         __A       =						reader_seq_len

         # Retrieval config
         __A       =						num_block_records
         __A       =						searcher_beam_size




