


"""simple docstring"""







import concurrent.futures
import json
import os
import shutil
import tempfile
import unittest

from transformers import AutoTokenizer, PreTrainedTokenizerFast
from transformers.testing_utils import require_tokenizers

from ..test_tokenization_common import TokenizerTesterMixin
@require_tokenizers
class 							lowerCamelCase       (			__SCREAMING_SNAKE_CASE				,					unittest.TestCase ):


							"""simple docstring"""
							SCREAMING_SNAKE_CASE										=      PreTrainedTokenizerFast
							SCREAMING_SNAKE_CASE										=      False
							SCREAMING_SNAKE_CASE										=      True
							SCREAMING_SNAKE_CASE										=      """tokenizer_file"""






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    False  # because we don't have pretrained_vocab_files_map
													super().setUp()
													UpperCAmelCase__		=    True

													UpperCAmelCase__		=    ['''robot-test/dummy-tokenizer-fast''', '''robot-test/dummy-tokenizer-wordlevel''']
													UpperCAmelCase__		=    '''SaulLu/dummy-tokenizer-bytelevel-bpe'''

													# Inclusion of 2 tokenizers to test different types of models (Unigram and WordLevel for the moment)
													UpperCAmelCase__		=    [(PreTrainedTokenizerFast, model_path, {}) for model_path in model_paths]

													UpperCAmelCase__		=    PreTrainedTokenizerFast.from_pretrained(model_paths[0]				)
													tokenizer.save_pretrained(self.tmpdirname				)






							def 					lowerCAmelCase     (						self :     List[str]				):
													# We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
													# model
													pass






							def 					lowerCAmelCase     (						self :     List[str]				):
													# We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
													# model
													pass






							def 					lowerCAmelCase     (						self :     Dict				):
													# We disable this test for PreTrainedTokenizerFast because it is the only tokenizer that is not linked to any
													# model
													pass






							def 					lowerCAmelCase     (						self :     Union[str, Any]				):
													# PreTrainedTokenizerFast doesn't have tokenizer_file in its signature
													pass






							def 					lowerCAmelCase     (						self :     int				):
													UpperCAmelCase__		=    self.tmpdirname
													# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
													for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																			with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""				):
																									try:
																															UpperCAmelCase__		=    tempfile.mkdtemp()
																															UpperCAmelCase__		=    self.rust_tokenizer_class.from_pretrained(lowerCamelCase_		, **lowerCamelCase_				)

																															tokenizer.save_pretrained(self.tmpdirname				)
																															super().test_training_new_tokenizer()
																									finally:
																															# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
																															# is restored
																															shutil.rmtree(self.tmpdirname				)
																															UpperCAmelCase__		=    tmpdirname_orig






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    self.tmpdirname
													# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
													for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
																			with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""				):
																									try:
																															UpperCAmelCase__		=    tempfile.mkdtemp()
																															UpperCAmelCase__		=    self.rust_tokenizer_class.from_pretrained(lowerCamelCase_		, **lowerCamelCase_				)

																															tokenizer.save_pretrained(self.tmpdirname				)
																															super().test_training_new_tokenizer_with_special_tokens_change()
																									finally:
																															# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
																															# is restored
																															shutil.rmtree(self.tmpdirname				)
																															UpperCAmelCase__		=    tmpdirname_orig






							def 					lowerCAmelCase     (						self :     List[str]				):
													UpperCAmelCase__		=    self.rust_tokenizer_class.from_pretrained(self.bytelevel_bpe_model_name				)

													UpperCAmelCase__		=    ('''a''' for _ in range(1000				))
													UpperCAmelCase__		=    tokenizer.train_new_from_iterator(text_iterator=lowerCamelCase_		, length=1000		, vocab_size=50				)

													UpperCAmelCase__		=    new_tokenizer.encode('''a🤗'''				)
													self.assertEqual(lowerCamelCase_		, [64, 172, 253, 97, 245]				)




@require_tokenizers
class 							lowerCamelCase       (			unittest.TestCase ):


							"""simple docstring"""






							def 					lowerCAmelCase     (						self :     Tuple				):
													UpperCAmelCase__		=    AutoTokenizer.from_pretrained('''bert-base-cased'''				)
													UpperCAmelCase__		=    json.loads(tokenizer._tokenizer.to_str()				)
													UpperCAmelCase__		=    len(lowerCamelCase_				)

													with tempfile.TemporaryDirectory() as tmp_dir:
																			# Hack to save this in the tokenizer_config.json
																			UpperCAmelCase__		=    ['''tokenizer.4.0.0.json''']
																			tokenizer.save_pretrained(lowerCamelCase_				)
																			json.dump(lowerCamelCase_		, open(os.path.join(lowerCamelCase_		, '''tokenizer.4.0.0.json'''				)		, '''w'''				)				)

																			# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
																			UpperCAmelCase__		=    AutoTokenizer.from_pretrained(lowerCamelCase_				)
																			self.assertEqual(len(lowerCamelCase_				)		, len(lowerCamelCase_				) + 1				)
																			UpperCAmelCase__		=    json.loads(new_tokenizer._tokenizer.to_str()				)
																			self.assertIn('''huggingface'''		, json_tokenizer['''model''']['''vocab''']				)

																			# Will need to be adjusted if we reach v42 and this test is still here.
																			# Should pick the old tokenizer file as the version of Transformers is < 4.0.0
																			shutil.move(os.path.join(lowerCamelCase_		, '''tokenizer.4.0.0.json'''				)		, os.path.join(lowerCamelCase_		, '''tokenizer.42.0.0.json'''				)				)
																			UpperCAmelCase__		=    ['''tokenizer.42.0.0.json''']
																			tokenizer.save_pretrained(lowerCamelCase_				)
																			UpperCAmelCase__		=    AutoTokenizer.from_pretrained(lowerCamelCase_				)
																			self.assertEqual(len(lowerCamelCase_				)		, len(lowerCamelCase_				)				)
																			UpperCAmelCase__		=    json.loads(new_tokenizer._tokenizer.to_str()				)
																			self.assertNotIn('''huggingface'''		, json_tokenizer['''model''']['''vocab''']				)






							def 					lowerCAmelCase     (						self :     int				):
													# This repo has two tokenizer files, one for v4.0.0 and above with an added token, one for versions lower.
													UpperCAmelCase__		=    '''hf-internal-testing/test-two-tokenizers'''

													# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
													UpperCAmelCase__		=    AutoTokenizer.from_pretrained(lowerCamelCase_				)
													self.assertEqual(len(lowerCamelCase_				)		, 28997				)
													UpperCAmelCase__		=    json.loads(tokenizer._tokenizer.to_str()				)
													self.assertIn('''huggingface'''		, json_tokenizer['''model''']['''vocab''']				)

													# Testing an older version by monkey-patching the version in the module it's used.
													import transformers as old_transformers

													UpperCAmelCase__		=    '''3.0.0'''
													UpperCAmelCase__		=    old_transformers.models.auto.AutoTokenizer.from_pretrained(lowerCamelCase_				)
													self.assertEqual(len(lowerCamelCase_				)		, 28996				)
													UpperCAmelCase__		=    json.loads(old_tokenizer._tokenizer.to_str()				)
													self.assertNotIn('''huggingface'''		, json_tokenizer['''model''']['''vocab''']				)




@require_tokenizers
class 							lowerCamelCase       (			unittest.TestCase ):


							"""simple docstring"""






							def 					lowerCAmelCase     (						self :     List[str]				):
													# See https://github.com/huggingface/transformers/pull/12550
													# and https://github.com/huggingface/tokenizers/issues/537
													UpperCAmelCase__		=    PreTrainedTokenizerFast.from_pretrained('''robot-test/dummy-tokenizer-wordlevel'''				)
													UpperCAmelCase__		=    '''The Matrix is a 1999 science fiction action film.'''

													with concurrent.futures.ThreadPoolExecutor() as executor:
																			UpperCAmelCase__		=    [executor.submit(self.fetch		, lowerCamelCase_		, lowerCamelCase_				) for i in range(10				)]
																			UpperCAmelCase__		=    [future.result() for future in futures]
																			self.assertEqual(lowerCamelCase_		, [[1, 10, 0, 8, 0, 18, 0, 0, 0, 2] for i in range(10				)]				)






							def 					lowerCAmelCase     (						self :     Optional[Any]		, lowerCamelCase_ :     Dict		, lowerCamelCase_ :     int				):
													return tokenizer.encode(lowerCamelCase_		, truncation='''longest_first'''		, padding='''longest'''				)



"""simple docstring"""







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


__a			 =   logging.get_logger(__name__)

__a			 =   {
    "google/pegasus-x-base": "https://huggingface.co/google/pegasus-x-base/resolve/main/config.json",
    "google/pegasus-x-large": "https://huggingface.co/google/pegasus-x-large/resolve/main/config.json",
    # See all PEGASUS-X models at https://huggingface.co/models?filter=pegasus-x
}
class 							lowerCamelCase       (			__SCREAMING_SNAKE_CASE ):


							"""simple docstring"""
							SCREAMING_SNAKE_CASE										=      """pegasus_x"""
							SCREAMING_SNAKE_CASE										=      ["""past_key_values"""]
							SCREAMING_SNAKE_CASE										=      {"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}






							def __init__(						self :     int		, lowerCamelCase_ :     Any=96103		, lowerCamelCase_ :     Union[str, Any]=16384		, lowerCamelCase_ :     Any=16		, lowerCamelCase_ :     str=4096		, lowerCamelCase_ :     Tuple=16		, lowerCamelCase_ :     Optional[int]=16		, lowerCamelCase_ :     Tuple=4096		, lowerCamelCase_ :     Any=16		, lowerCamelCase_ :     List[str]=0.0		, lowerCamelCase_ :     int=0.0		, lowerCamelCase_ :     List[Any]=True		, lowerCamelCase_ :     str=True		, lowerCamelCase_ :     int="gelu"		, lowerCamelCase_ :     Optional[Any]=1024		, lowerCamelCase_ :     Optional[int]=0.1		, lowerCamelCase_ :     str=0.0		, lowerCamelCase_ :     Optional[Any]=0.0		, lowerCamelCase_ :     List[str]=0.02		, lowerCamelCase_ :     Optional[Any]=0		, lowerCamelCase_ :     List[str]=True		, lowerCamelCase_ :     List[Any]=0		, lowerCamelCase_ :     str=1		, lowerCamelCase_ :     Optional[Any]=1		, lowerCamelCase_ :     Tuple=32		, lowerCamelCase_ :     Dict=512		, lowerCamelCase_ :     List[Any]=True		, **lowerCamelCase_ :     int		, ):
													UpperCAmelCase__		=    vocab_size
													UpperCAmelCase__		=    max_position_embeddings
													UpperCAmelCase__		=    d_model
													UpperCAmelCase__		=    encoder_ffn_dim
													UpperCAmelCase__		=    encoder_layers
													UpperCAmelCase__		=    encoder_attention_heads
													UpperCAmelCase__		=    decoder_ffn_dim
													UpperCAmelCase__		=    decoder_layers
													UpperCAmelCase__		=    decoder_attention_heads
													UpperCAmelCase__		=    dropout
													UpperCAmelCase__		=    attention_dropout
													UpperCAmelCase__		=    activation_dropout
													UpperCAmelCase__		=    activation_function
													UpperCAmelCase__		=    init_std
													UpperCAmelCase__		=    encoder_layerdrop
													UpperCAmelCase__		=    decoder_layerdrop
													UpperCAmelCase__		=    use_cache
													UpperCAmelCase__		=    encoder_layers
													UpperCAmelCase__		=    scale_embedding  # scale factor will be sqrt(d_model) if True

													UpperCAmelCase__		=    num_global_tokens
													UpperCAmelCase__		=    block_size
													UpperCAmelCase__		=    stagger_local_blocks

													super().__init__(
													    pad_token_id=lowerCamelCase_		, eos_token_id=lowerCamelCase_		, is_encoder_decoder=lowerCamelCase_		, decoder_start_token_id=lowerCamelCase_		, forced_eos_token_id=lowerCamelCase_		, **lowerCamelCase_		, )






							@property
							def 					lowerCAmelCase     (						self :     Optional[int]				):
													return self.encoder_attention_heads






							@property
							def 					lowerCAmelCase     (						self :     Optional[int]				):
													return self.d_model