


'''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 		UpperCAmelCase_      (  unittest.TestCase		):





     @parameterized.expand([(None,), ('foo.json',)]    )
     def 							__UpperCAmelCase      ( self       :      Dict    ,      UpperCAmelCase__       :      Optional[Any]    )				->  Tuple:
        lowerCAmelCase     =			GenerationConfig(
            do_sample=UpperCAmelCase__    ,      temperature=0.7    ,      length_penalty=1.0    ,      bad_words_ids=[[1, 2, 3], [4, 5]]    ,      )
        with tempfile.TemporaryDirectory() as tmp_dir:
           config.save_pretrained(UpperCAmelCase__    ,      config_name=UpperCAmelCase__    )
           lowerCAmelCase     =			GenerationConfig.from_pretrained(UpperCAmelCase__    ,      config_name=UpperCAmelCase__    )

        # Checks parameters that were specified
        self.assertEqual(loaded_config.do_sample    ,      UpperCAmelCase__    )
        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    ,      5_0    )
        self.assertEqual(loaded_config.max_length    ,      2_0    )
        self.assertEqual(loaded_config.max_time    ,      UpperCAmelCase__    )





     def 							__UpperCAmelCase      ( self       :      Tuple    )				->  Union[str, Any]:
        lowerCAmelCase     =			AutoConfig.from_pretrained('gpt2'    )
        lowerCAmelCase     =			GenerationConfig.from_model_config(UpperCAmelCase__    )
        lowerCAmelCase     =			GenerationConfig()

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

        # 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 							__UpperCAmelCase      ( self       :      str    )				->  Dict:
        lowerCAmelCase     =			GenerationConfig()
        lowerCAmelCase     =			{
            'max_new_tokens': 1_0_2_4,
            'foo': 'bar',
        }
        lowerCAmelCase     =			copy.deepcopy(UpperCAmelCase__    )
        lowerCAmelCase     =			generation_config.update(**UpperCAmelCase__    )

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

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

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





     def 							__UpperCAmelCase      ( self       :      int    )				->  Any:
        lowerCAmelCase     =			GenerationConfig()
        lowerCAmelCase     =			'bar'

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

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

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





     def 							__UpperCAmelCase      ( self       :      int    )				->  int:
        lowerCAmelCase     =			GenerationConfig()
        self.assertEqual(default_config.temperature    ,      1.0    )
        self.assertEqual(default_config.do_sample    ,      UpperCAmelCase__    )
        self.assertEqual(default_config.num_beams    ,      1    )

        lowerCAmelCase     =			GenerationConfig(
            do_sample=UpperCAmelCase__    ,      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    ,      UpperCAmelCase__    )
        self.assertEqual(config.num_beams    ,      1    )

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

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




@is_staging_test
class 		UpperCAmelCase_      (  unittest.TestCase		):





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      Dict    )				->  List[str]:
        lowerCAmelCase     =			TOKEN
        HfFolder.save_token(UpperCAmelCase__    )





     @classmethod
     def 							__UpperCAmelCase      ( cls       :      str    )				->  str:
        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 							__UpperCAmelCase      ( self       :      int    )				->  List[Any]:
        lowerCAmelCase     =			GenerationConfig(
            do_sample=UpperCAmelCase__    ,      temperature=0.7    ,      length_penalty=1.0    ,      )
        config.push_to_hub('test-generation-config'    ,      use_auth_token=self._token    )

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

        # 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(
               UpperCAmelCase__    ,      repo_id='test-generation-config'    ,      push_to_hub=UpperCAmelCase__    ,      use_auth_token=self._token    )

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





     def 							__UpperCAmelCase      ( self       :      Dict    )				->  Optional[int]:
        lowerCAmelCase     =			GenerationConfig(
            do_sample=UpperCAmelCase__    ,      temperature=0.7    ,      length_penalty=1.0    ,      )
        config.push_to_hub('valid_org/test-generation-config-org'    ,      use_auth_token=self._token    )

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

        # 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(
               UpperCAmelCase__    ,      repo_id='valid_org/test-generation-config-org'    ,      push_to_hub=UpperCAmelCase__    ,      use_auth_token=self._token    )

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













"""simple docstring"""


import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_torch_available
from transformers.testing_utils import require_torch, torch_device


if is_torch_available():
       from transformers import PyTorchBenchmark, PyTorchBenchmarkArguments


@require_torch
class 					snake_case		(							unittest.TestCase						):


   '''simple docstring'''

   def 				_SCREAMING_SNAKE_CASE				(	self :     Any,       _lowerCamelCase :     Optional[int]  ):

     '''simple docstring'''
     for model_result in results.values():
       for batch_size, sequence_length in zip(model_result['''bs'''],       model_result['''ss''']  ):
         __A									= model_result['''result'''][batch_size][sequence_length]
         self.assertIsNotNone(_lowerCamelCase  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sgugger/tiny-distilbert-classification'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       only_pretrain_model=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Any  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       torchscript=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   @unittest.skipIf(torch_device == '''cpu''',       '''Cant do half precision'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       fpaa=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     # set architectures equal to `None`
     __A									= None
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     int  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   @unittest.skipIf(torch_device == '''cpu''',       '''Can\'t do half precision'''  )
   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[int]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       fpaa=_lowerCamelCase,       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     str  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     List[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tinier_bart'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_inference_result  )
     self.check_results_dict_not_empty(results.memory_inference_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Dict  ):

     '''simple docstring'''
     __A									= '''sshleifer/tinier_bart'''
     __A									= AutoConfig.from_pretrained(_lowerCamelCase  )
     __A									= PyTorchBenchmarkArguments(
         models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       multi_process=_lowerCamelCase,       )
     __A									= PyTorchBenchmark(_lowerCamelCase,       configs=[config]  )
     __A									= benchmark.run()
     self.check_results_dict_not_empty(results.time_train_result  )
     self.check_results_dict_not_empty(results.memory_train_result  )


   def 				_SCREAMING_SNAKE_CASE				(	self :     Optional[Any]  ):

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''
     with tempfile.TemporaryDirectory() as tmp_dir:
       __A									= PyTorchBenchmarkArguments(
           models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       save_to_csv=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       inference_time_csv_file=os.path.join(_lowerCamelCase,       '''inf_time.csv'''  ),       train_memory_csv_file=os.path.join(_lowerCamelCase,       '''train_mem.csv'''  ),       inference_memory_csv_file=os.path.join(_lowerCamelCase,       '''inf_mem.csv'''  ),       train_time_csv_file=os.path.join(_lowerCamelCase,       '''train_time.csv'''  ),       env_info_csv_file=os.path.join(_lowerCamelCase,       '''env.csv'''  ),       multi_process=_lowerCamelCase,       )
       __A									= PyTorchBenchmark(_lowerCamelCase  )
       benchmark.run()
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''inf_time.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''train_time.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''inf_mem.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''train_mem.csv'''  )  ).exists()  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''env.csv'''  )  ).exists()  )






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

     '''simple docstring'''
     __A									= '''sshleifer/tiny-gpt2'''

     def _check_summary_is_not_empty(_lowerCamelCase :     List[Any]  ):
       self.assertTrue(hasattr(_lowerCamelCase,       '''sequential'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''cumulative'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''current'''  )  )
       self.assertTrue(hasattr(_lowerCamelCase,       '''total'''  )  )

     with tempfile.TemporaryDirectory() as tmp_dir:
       __A									= PyTorchBenchmarkArguments(
           models=[MODEL_ID],       training=_lowerCamelCase,       inference=_lowerCamelCase,       sequence_lengths=[8],       batch_sizes=[1],       log_filename=os.path.join(_lowerCamelCase,       '''log.txt'''  ),       log_print=_lowerCamelCase,       trace_memory_line_by_line=_lowerCamelCase,       multi_process=_lowerCamelCase,       )
       __A									= PyTorchBenchmark(_lowerCamelCase  )
       __A									= benchmark.run()
       _check_summary_is_not_empty(result.inference_summary  )
       _check_summary_is_not_empty(result.train_summary  )
       self.assertTrue(Path(os.path.join(_lowerCamelCase,       '''log.txt'''  )  ).exists()  )




