
"""simple docstring"""




import copy
import tempfile
import unittest

from huggingface_hub import HfFolder, delete_repo
from parameterized import parameterized
from requests.exceptions import HTTPError

from transformers import AutoConfig, GenerationConfig
from transformers.testing_utils import TOKEN, USER, is_staging_test





class  snake_case     (							unittest.TestCase   ):

							@parameterized.expand([(None,), ('''foo.json''',)]    )
							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig(
									    do_sample=SCREAMING_SNAKE_CASE_       ,       temperature=0.7       ,       length_penalty=1.0       ,       bad_words_ids=[[1, 2, 3], [4, 5]]       ,       )
									with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(SCREAMING_SNAKE_CASE_       ,       config_name=SCREAMING_SNAKE_CASE_    )
											SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained(SCREAMING_SNAKE_CASE_       ,       config_name=SCREAMING_SNAKE_CASE_    )

									# Checks parameters that were specified
									self.assertEqual(loaded_config.do_sample       ,       SCREAMING_SNAKE_CASE_    )
									self.assertEqual(loaded_config.temperature       ,       0.7    )
									self.assertEqual(loaded_config.length_penalty       ,       1.0    )
									self.assertEqual(loaded_config.bad_words_ids       ,       [[1, 2, 3], [4, 5]]    )

									# Checks parameters that were not specified (defaults)
									self.assertEqual(loaded_config.top_k       ,       50    )
									self.assertEqual(loaded_config.max_length       ,       20    )
									self.assertEqual(loaded_config.max_time       ,       SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = AutoConfig.from_pretrained('''gpt2'''    )
									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_model_config(SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = GenerationConfig()

									# The generation config has loaded a few non-default parameters from the model config
									self.assertNotEqual(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

									# One of those parameters is eos_token_id -- check if it matches
									self.assertNotEqual(generation_config_from_model.eos_token_id       ,       default_generation_config.eos_token_id    )
									self.assertEqual(generation_config_from_model.eos_token_id       ,       model_config.eos_token_id    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig()
									SCREAMING_SNAKE_CASE_						 = {
									    '''max_new_tokens''': 10_24,
									    '''foo''': '''bar''',
									}
									SCREAMING_SNAKE_CASE_						 = copy.deepcopy(SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = generation_config.update(**SCREAMING_SNAKE_CASE_    )

									# update_kwargs was not modified (no side effects)
									self.assertEqual(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )

									# update_kwargs was used to update the config on valid attributes
									self.assertEqual(generation_config.max_new_tokens       ,       10_24    )

									# `.update()` returns a dictionary of unused kwargs
									self.assertEqual(SCREAMING_SNAKE_CASE_       ,       {'''foo''': '''bar'''}    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig()
									SCREAMING_SNAKE_CASE_						 = '''bar'''

									with tempfile.TemporaryDirectory('''test-generation-config'''    ) as tmp_dir:
											generation_config.save_pretrained(SCREAMING_SNAKE_CASE_    )

											SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained(SCREAMING_SNAKE_CASE_    )
									# update_kwargs was used to update the config on valid attributes
									self.assertEqual(new_config.foo       ,       '''bar'''    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_model_config(SCREAMING_SNAKE_CASE_    )
									assert not hasattr(SCREAMING_SNAKE_CASE_       ,       '''foo'''    )  # no new kwargs should be initialized if from config







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig()
									self.assertEqual(default_config.temperature       ,       1.0    )
									self.assertEqual(default_config.do_sample       ,       SCREAMING_SNAKE_CASE_    )
									self.assertEqual(default_config.num_beams       ,       1    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig(
									    do_sample=SCREAMING_SNAKE_CASE_       ,       temperature=0.7       ,       length_penalty=1.0       ,       bad_words_ids=[[1, 2, 3], [4, 5]]       ,       )
									self.assertEqual(config.temperature       ,       0.7    )
									self.assertEqual(config.do_sample       ,       SCREAMING_SNAKE_CASE_    )
									self.assertEqual(config.num_beams       ,       1    )

									with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(SCREAMING_SNAKE_CASE_    )
											SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained(SCREAMING_SNAKE_CASE_       ,       temperature=1.0    )

									self.assertEqual(loaded_config.temperature       ,       1.0    )
									self.assertEqual(loaded_config.do_sample       ,       SCREAMING_SNAKE_CASE_    )
									self.assertEqual(loaded_config.num_beams       ,       1    )  # default value







@is_staging_test
class  snake_case     (							unittest.TestCase   ):

							@classmethod
							def _lowercase					(cls    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = TOKEN
									HfFolder.save_token(SCREAMING_SNAKE_CASE_    )

							@classmethod
							def _lowercase					(cls    ):

									"""simple docstring"""







									try:
											delete_repo(token=cls._token       ,       repo_id='''test-generation-config'''    )
									except HTTPError:
											pass

									try:
											delete_repo(token=cls._token       ,       repo_id='''valid_org/test-generation-config-org'''    )
									except HTTPError:
											pass

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig(
									    do_sample=SCREAMING_SNAKE_CASE_       ,       temperature=0.7       ,       length_penalty=1.0       ,       )
									config.push_to_hub('''test-generation-config'''       ,       use_auth_token=self._token    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained(f'{USER}/test-generation-config'    )
									for k, v in config.to_dict().items():
											if k != "transformers_version":
													self.assertEqual(SCREAMING_SNAKE_CASE_       ,       getattr(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )    )

        # Reset repo
									delete_repo(token=self._token       ,       repo_id='''test-generation-config'''    )

									# Push to hub via save_pretrained
									with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(
											    SCREAMING_SNAKE_CASE_       ,       repo_id='''test-generation-config'''       ,       push_to_hub=SCREAMING_SNAKE_CASE_       ,       use_auth_token=self._token    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained(f'{USER}/test-generation-config'    )
									for k, v in config.to_dict().items():
											if k != "transformers_version":
													self.assertEqual(SCREAMING_SNAKE_CASE_       ,       getattr(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )    )







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = GenerationConfig(
									    do_sample=SCREAMING_SNAKE_CASE_       ,       temperature=0.7       ,       length_penalty=1.0       ,       )
									config.push_to_hub('''valid_org/test-generation-config-org'''       ,       use_auth_token=self._token    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''    )
									for k, v in config.to_dict().items():
											if k != "transformers_version":
													self.assertEqual(SCREAMING_SNAKE_CASE_       ,       getattr(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )    )

        # Reset repo
									delete_repo(token=self._token       ,       repo_id='''valid_org/test-generation-config-org'''    )

									# Push to hub via save_pretrained
									with tempfile.TemporaryDirectory() as tmp_dir:
											config.save_pretrained(
											    SCREAMING_SNAKE_CASE_       ,       repo_id='''valid_org/test-generation-config-org'''       ,       push_to_hub=SCREAMING_SNAKE_CASE_       ,       use_auth_token=self._token    )

									SCREAMING_SNAKE_CASE_						 = GenerationConfig.from_pretrained('''valid_org/test-generation-config-org'''    )
									for k, v in config.to_dict().items():
											if k != "transformers_version":
													self.assertEqual(SCREAMING_SNAKE_CASE_       ,       getattr(SCREAMING_SNAKE_CASE_       ,       SCREAMING_SNAKE_CASE_    )    )


"""simple docstring"""




import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin





class  snake_case     (							__lowercase       ,			unittest.TestCase   ):
							UpperCAmelCase__						     =      TransfoXLTokenizer
							UpperCAmelCase__						     =      False
							UpperCAmelCase__						     =      False

							def _lowercase					(self    ):

									"""simple docstring"""







									super().setUp()

									SCREAMING_SNAKE_CASE_						 = [
									    '''<unk>''',
									    '''[CLS]''',
									    '''[SEP]''',
									    '''want''',
									    '''unwanted''',
									    '''wa''',
									    '''un''',
									    '''running''',
									    ''',''',
									    '''low''',
									    '''l''',
									]
									SCREAMING_SNAKE_CASE_						 = os.path.join(self.tmpdirname       ,       VOCAB_FILES_NAMES['''vocab_file''']    )
									with open(self.vocab_file       ,       '''w'''       ,       encoding='''utf-8'''    ) as vocab_writer:
											vocab_writer.write(''''''.join([x + '''\n''' for x in vocab_tokens]    )    )

							def _lowercase					(self       ,       **SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = True
									return TransfoXLTokenizer.from_pretrained(self.tmpdirname       ,       **SCREAMING_SNAKE_CASE_    )

							def _lowercase					(self       ,       SCREAMING_SNAKE_CASE_    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = '''<unk> UNwanted , running'''
									SCREAMING_SNAKE_CASE_						 = '''<unk> unwanted, running'''
									return input_text, output_text

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = TransfoXLTokenizer(vocab_file=self.vocab_file       ,       lower_case=SCREAMING_SNAKE_CASE_    )

									SCREAMING_SNAKE_CASE_						 = tokenizer.tokenize('''<unk> UNwanted , running'''    )
									self.assertListEqual(SCREAMING_SNAKE_CASE_       ,       ['''<unk>''', '''unwanted''', ''',''', '''running''']    )

									self.assertListEqual(tokenizer.convert_tokens_to_ids(SCREAMING_SNAKE_CASE_    )       ,       [0, 4, 8, 7]    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = TransfoXLTokenizer(lower_case=SCREAMING_SNAKE_CASE_    )

									self.assertListEqual(
									    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''    )       ,       ['''hello''', '''!''', '''how''', '''are''', '''you''', '''?''']    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = TransfoXLTokenizer(lower_case=SCREAMING_SNAKE_CASE_    )

									self.assertListEqual(
									    tokenizer.tokenize(''' \tHeLLo ! how  \n Are yoU ?  '''    )       ,       ['''HeLLo''', '''!''', '''how''', '''Are''', '''yoU''', '''?''']    )

							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = TransfoXLTokenizer(lower_case=SCREAMING_SNAKE_CASE_    )
									SCREAMING_SNAKE_CASE_						 = '''Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'''
									SCREAMING_SNAKE_CASE_						 = [
									    '''Hello''',
									    '''(''',
									    '''bracket''',
									    ''')''',
									    '''and''',
									    '''side''',
									    '''@-@''',
									    '''scrolled''',
									    '''[''',
									    '''and''',
									    ''']''',
									    '''Henry''',
									    '''\'s''',
									    '''$''',
									    '''5''',
									    '''@,@''',
									    '''000''',
									    '''with''',
									    '''3''',
									    '''@.@''',
									    '''34''',
									    '''m''',
									    '''.''',
									    '''What''',
									    '''\'s''',
									    '''up''',
									    '''!''',
									    '''?''',
									]

									self.assertListEqual(tokenizer.tokenize(SCREAMING_SNAKE_CASE_    )       ,       SCREAMING_SNAKE_CASE_    )

									self.assertEqual(tokenizer.convert_tokens_to_string(SCREAMING_SNAKE_CASE_    )       ,       SCREAMING_SNAKE_CASE_    )







							def _lowercase					(self    ):

									"""simple docstring"""







									SCREAMING_SNAKE_CASE_						 = self.get_tokenizer()
									SCREAMING_SNAKE_CASE_						 = len(SCREAMING_SNAKE_CASE_    )

									tokenizer.add_tokens(['''new1''', '''new2''']    )
									tokenizer.move_added_token('''new1'''       ,       1    )

									# Check that moved token is not copied (duplicate)
									self.assertEqual(len(SCREAMING_SNAKE_CASE_    )       ,       original_len + 2    )
									# Check that token is moved to specified id
									self.assertEqual(tokenizer.encode('''new1'''    )       ,       [1]    )
									self.assertEqual(tokenizer.decode([1]    )       ,       '''new1'''    )