



import dataclasses
import json
import warnings
from dataclasses import dataclass, field
from time import time
from typing import List

from ..utils import logging


SCREAMING_SNAKE_CASE_:str             =						logging.get_logger(__name__)




def 	__UpperCamelCase	(  _lowerCAmelCase=None      ,     _lowerCAmelCase=None		)       ->  int:



    """simple docstring"""






    return field(default_factory=lambda: default      ,     metadata=__a		)






@dataclass
class SCREAMING_SNAKE_CASE__     :




    '''simple docstring'''






    __lowerCamelCase			: Optional[Any]					=      list_field(
        default=[] ,						metadata={
            "help": (
                "Model checkpoints to be provided to the AutoModel classes. Leave blank to benchmark the base version"
                " of all available models"
            )
        } ,						)

    __lowerCamelCase			: Optional[Any]					=      list_field(
        default=[8] ,						metadata={"help": "List of batch sizes for which memory and time performance will be evaluated"}    )

    __lowerCamelCase			: List[Any]					=      list_field(
        default=[8, 32, 128, 512] ,						metadata={"help": "List of sequence lengths for which memory and time performance will be evaluated"} ,						)

    __lowerCamelCase			: List[str]					=      field(
        default=UpperCAmelCase__ ,						metadata={"help": "Whether to benchmark inference of model. Inference can be disabled via --no-inference."} ,						)
    __lowerCamelCase			: List[str]					=      field(
        default=UpperCAmelCase__ ,						metadata={"help": "Whether to run on available cuda devices. Cuda can be disabled via --no-cuda."} ,						)
    __lowerCamelCase			: Any					=      field(
        default=UpperCAmelCase__ ,						metadata={"help": "Whether to run on available tpu devices. TPU can be disabled via --no-tpu."}    )
    __lowerCamelCase			: Dict					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Use FP16 to accelerate inference."}    )
    __lowerCamelCase			: Optional[Any]					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Benchmark training of model"}    )
    __lowerCamelCase			: Tuple					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Verbose memory tracing"}    )
    __lowerCamelCase			: Optional[int]					=      field(
        default=UpperCAmelCase__ ,						metadata={"help": "Whether to perform speed measurements. Speed measurements can be disabled via --no-speed."} ,						)
    __lowerCamelCase			: Optional[Any]					=      field(
        default=UpperCAmelCase__ ,						metadata={
            "help": "Whether to perform memory measurements. Memory measurements can be disabled via --no-memory"
        } ,						)
    __lowerCamelCase			: str					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Trace memory line by line"}    )
    __lowerCamelCase			: Dict					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Save result to a CSV file"}    )
    __lowerCamelCase			: Optional[Any]					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Save all print statements in a log file"}    )
    __lowerCamelCase			: List[str]					=      field(default=UpperCAmelCase__ ,						metadata={"help": "Whether to print environment information"}    )
    __lowerCamelCase			: List[Any]					=      field(
        default=UpperCAmelCase__ ,						metadata={
            "help": (
                "Whether to use multiprocessing for memory and speed measurement. It is highly recommended to use"
                " multiprocessing for accurate CPU and GPU memory measurements. This option should only be disabled"
                " for debugging / testing and on TPU."
            )
        } ,						)
    __lowerCamelCase			: Optional[int]					=      field(
        default=F'''inference_time_{round(time()    )}.csv''' ,						metadata={"help": "CSV filename used if saving time results to csv."} ,						)
    __lowerCamelCase			: Optional[Any]					=      field(
        default=F'''inference_memory_{round(time()    )}.csv''' ,						metadata={"help": "CSV filename used if saving memory results to csv."} ,						)
    __lowerCamelCase			: str					=      field(
        default=F'''train_time_{round(time()    )}.csv''' ,						metadata={"help": "CSV filename used if saving time results to csv for training."} ,						)
    __lowerCamelCase			: Dict					=      field(
        default=F'''train_memory_{round(time()    )}.csv''' ,						metadata={"help": "CSV filename used if saving memory results to csv for training."} ,						)
    __lowerCamelCase			: List[str]					=      field(
        default=F'''env_info_{round(time()    )}.csv''' ,						metadata={"help": "CSV filename used if saving environment information."} ,						)
    __lowerCamelCase			: List[Any]					=      field(
        default=F'''log_{round(time()    )}.csv''' ,						metadata={"help": "Log filename used if print statements are saved in log."} ,						)
    __lowerCamelCase			: Tuple					=      field(default=3 ,						metadata={"help": "Times an experiment will be run."}    )
    __lowerCamelCase			: Optional[Any]					=      field(
        default=UpperCAmelCase__ ,						metadata={
            "help": (
                "Instead of loading the model as defined in `config.architectures` if exists, just load the pretrain"
                " model weights."
            )
        } ,						)






    def 			_lowerCAmelCase					(       self		):
        warnings.warn(
            f'''The class {self.__class__} is deprecated. Hugging Face Benchmarking utils'''
            """ are deprecated in general and it is advised to use external Benchmarking libraries """
            """ to benchmark Transformer models.""",		lowercase_,		)






    def 			_lowerCAmelCase					(       self		):
        return json.dumps(dataclasses.asdict(self		),		indent=2		)






    @property
    def 			_lowerCAmelCase					(       self		):


        if len(self.models		) <= 0:
            raise ValueError(
                """Please make sure you provide at least one model name / model identifier, *e.g.* `--models"""
                """ bert-base-cased` or `args.models = [\'bert-base-cased\']."""		)
        return self.models






    @property
    def 			_lowerCAmelCase					(       self		):


        if not self.multi_process:
            return False
        elif self.is_tpu:
            logger.info("""Multiprocessing is currently not possible on TPU."""		)
            return False
        else:
            return True










"""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 							lowerCAmelCase__				(			unittest.TestCase ):






      '''simple docstring'''

      @parameterized.expand([(None,), ('''foo.json''',)])
      def _SCREAMING_SNAKE_CASE (						self     :  Dict						,	lowercase_     :  Union[str, Any]):




          '''simple docstring'''


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

          # Checks parameters that were specified
          self.assertEqual(loaded_config.do_sample						,	lowercase_)
          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						,	lowercase_)
      def _SCREAMING_SNAKE_CASE (						self     :  Optional[Any]):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		AutoConfig.from_pretrained('''gpt2''')
          SCREAMING_SNAKE_CASE_      :  int    						=		GenerationConfig.from_model_config(lowercase_)
          SCREAMING_SNAKE_CASE_      :  int    						=		GenerationConfig()

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

          # 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 _SCREAMING_SNAKE_CASE (						self     :  Dict):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  str    						=		GenerationConfig()
          SCREAMING_SNAKE_CASE_      :  Any    						=		{
              '''max_new_tokens''': 1024,
              '''foo''': '''bar''',
          }
          SCREAMING_SNAKE_CASE_      :  str    						=		copy.deepcopy(lowercase_)
          SCREAMING_SNAKE_CASE_      :  Tuple    						=		generation_config.update(**lowercase_)

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

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

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




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		GenerationConfig()
          SCREAMING_SNAKE_CASE_      :  List[str]    						=		'''bar'''

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

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

          SCREAMING_SNAKE_CASE_      :  Optional[Any]    						=		GenerationConfig.from_model_config(lowercase_)
          assert not hasattr(lowercase_						,	'''foo''')  # no new kwargs should be initialized if from config



      def _SCREAMING_SNAKE_CASE (						self     :  Tuple):




          '''simple docstring'''


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

          SCREAMING_SNAKE_CASE_      :  Dict    						=		GenerationConfig(
              do_sample=lowercase_						,	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						,	lowercase_)
          self.assertEqual(config.num_beams						,	1)

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

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




@is_staging_test
class 							lowerCAmelCase__				(			unittest.TestCase ):






      '''simple docstring'''

      @classmethod
      def _SCREAMING_SNAKE_CASE (						cls     :  Any):




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  str    						=		TOKEN
          HfFolder.save_token(lowercase_)
      @classmethod
      def _SCREAMING_SNAKE_CASE (						cls     :  List[str]):




          '''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 _SCREAMING_SNAKE_CASE (						self     :  Dict):




          '''simple docstring'''


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

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

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

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



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




          '''simple docstring'''


          SCREAMING_SNAKE_CASE_      :  Dict    						=		GenerationConfig(
              do_sample=lowercase_						,	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_      :  Any    						=		GenerationConfig.from_pretrained('''valid_org/test-generation-config-org''')
          for k, v in config.to_dict().items():
              if k != "transformers_version":
                  self.assertEqual(lowercase_						,	getattr(lowercase_						,	lowercase_))

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

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


