
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							):


 def      lowercase__     (					self					,  SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   for model_result in results.values():
     for batch_size, sequence_length in zip(model_result["""bs"""]					,  model_result["""ss"""]):
       lowercase__  :   str    		=      model_result["""result"""][batch_size][sequence_length]
       self.assertIsNotNone(A__)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Dict    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Tuple    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   int    		=      PyTorchBenchmark(A__)
   lowercase__  :   List[Any]    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Dict    		=      """sgugger/tiny-distilbert-classification"""
   lowercase__  :   Dict    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  only_pretrain_model=A__					,  )
   lowercase__  :   str    		=      PyTorchBenchmark(A__)
   lowercase__  :   List[str]    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Any    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   List[Any]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  torchscript=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   Tuple    		=      PyTorchBenchmark(A__)
   lowercase__  :   str    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[Any]    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Optional[Any]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  fpaa=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   str    		=      PyTorchBenchmark(A__)
   lowercase__  :   Any    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[Any]    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Tuple    		=      AutoConfig.from_pretrained(A__)
   # set architectures equal to `None`
   lowercase__  :   List[Any]    		=      None
   lowercase__  :   str    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   List[str]    		=      PyTorchBenchmark(A__					,  configs=[config])
   lowercase__  :   Any    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Optional[int]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   Any    		=      PyTorchBenchmark(A__)
   lowercase__  :   Dict    		=      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      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   List[str]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  fpaa=A__					,  multi_process=A__					,  )
   lowercase__  :   Dict    		=      PyTorchBenchmark(A__)
   lowercase__  :   Optional[int]    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result)
   self.check_results_dict_not_empty(results.memory_train_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   int    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Optional[int]    		=      AutoConfig.from_pretrained(A__)
   lowercase__  :   str    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   int    		=      PyTorchBenchmark(A__					,  configs=[config])
   lowercase__  :   Tuple    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   List[str]    		=      """sshleifer/tinier_bart"""
   lowercase__  :   List[str]    		=      AutoConfig.from_pretrained(A__)
   lowercase__  :   List[str]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   Union[str, Any]    		=      PyTorchBenchmark(A__					,  configs=[config])
   lowercase__  :   str    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_inference_result)
   self.check_results_dict_not_empty(results.memory_inference_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      """sshleifer/tiny-gpt2"""
   lowercase__  :   Union[str, Any]    		=      AutoConfig.from_pretrained(A__)
   lowercase__  :   Tuple    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   int    		=      PyTorchBenchmark(A__					,  configs=[config])
   lowercase__  :   Tuple    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result)
   self.check_results_dict_not_empty(results.memory_train_result)


 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Dict    		=      """sshleifer/tinier_bart"""
   lowercase__  :   int    		=      AutoConfig.from_pretrained(A__)
   lowercase__  :   Union[str, Any]    		=      PyTorchBenchmarkArguments(
       models=[MODEL_ID]					,  training=A__					,  inference=A__					,  sequence_lengths=[8]					,  batch_sizes=[1]					,  multi_process=A__					,  )
   lowercase__  :   List[Any]    		=      PyTorchBenchmark(A__					,  configs=[config])
   lowercase__  :   Optional[Any]    		=      benchmark.run()
   self.check_results_dict_not_empty(results.time_train_result)
   self.check_results_dict_not_empty(results.memory_train_result)


 def      lowercase__     (					self):


   '''simple docstring'''




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



 def      lowercase__     (					self):


   '''simple docstring'''




   lowercase__  :   Optional[int]    		=      """sshleifer/tiny-gpt2"""

   def _check_summary_is_not_empty(SCREAMING_SNAKE_CASE_):
     self.assertTrue(hasattr(A__					,  """sequential"""))
     self.assertTrue(hasattr(A__					,  """cumulative"""))
     self.assertTrue(hasattr(A__					,  """current"""))
     self.assertTrue(hasattr(A__					,  """total"""))

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








from ..utils import DummyObject, requires_backends



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Any        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Any        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Union[str, Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     int        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Optional[Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Tuple        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     List[str]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Union[str, Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Optional[Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Dict        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Optional[Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     Optional[Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



class       _snake_case   (	metaclass=UpperCAmelCase_							):
 __lowerCAmelCase  :     List[Any]        =			['flax']


 def __init__(					self					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(self					,  ["""flax"""])


 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])



 @classmethod
 def      lowercase__     (					cls					,  *SCREAMING_SNAKE_CASE_					,  **SCREAMING_SNAKE_CASE_):


   '''simple docstring'''




   requires_backends(cls					,  ["""flax"""])






