

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



    def 					snake_case_			(					self		,				UpperCamelCase__       )						->      Dict:





           '''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 					snake_case_			(					self       )						->      Union[str, 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 					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 					snake_case_			(					self       )						->      str:





           '''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 					snake_case_			(					self       )						->      str:





           '''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 					snake_case_			(					self       )						->      Optional[Any]:





           '''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 					snake_case_			(					self       )						->      Tuple:





           '''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 					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]		,				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 					snake_case_			(					self       )						->      List[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_inference_result       )
           self.check_results_dict_not_empty(results.memory_inference_result       )



    def 					snake_case_			(					self       )						->      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 					snake_case_			(					self       )						->      Tuple:





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





           '''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 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           A_						=      "sshleifer/tiny-gpt2"

           def _check_summary_is_not_empty(UpperCamelCase__       ):
                  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()       )



'''simple docstring'''







def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__				)			->			float:
       _validate_point(UpperCAmelCase__				)
       _validate_point(UpperCAmelCase__				)
       if len(UpperCAmelCase__				) != len(UpperCAmelCase__				):
              raise ValueError("""Both points must be in the same n-dimensional space"""				)

       return float(sum(abs(a - b				) for a, b in zip(UpperCAmelCase__,       UpperCAmelCase__				)				)				)
def       UpperCAmelCase__    (      UpperCAmelCase__				)			->			None:
       if point:
              if isinstance(UpperCAmelCase__,       UpperCAmelCase__				):
                     for item in point:
                            if not isinstance(UpperCAmelCase__,       (int, float)				):
                                   A_						=      (
                                       """Expected a list of numbers as input, found """
                                       F'''{type(UpperCAmelCase__				).__name__}'''
                                   )
                                   raise TypeError(UpperCAmelCase__				)
              else:
                     A_						=      F'''Expected a list of numbers as input, found {type(UpperCAmelCase__				).__name__}'''
                     raise TypeError(UpperCAmelCase__				)
       else:
              raise ValueError("""Missing an input"""				)
def       UpperCAmelCase__    (      UpperCAmelCase__,       UpperCAmelCase__				)			->			float:
       _validate_point(UpperCAmelCase__				)
       _validate_point(UpperCAmelCase__				)
       if len(UpperCAmelCase__				) != len(UpperCAmelCase__				):
              raise ValueError("""Both points must be in the same n-dimensional space"""				)

       return float(sum(abs(x - y				) for x, y in zip(UpperCAmelCase__,       UpperCAmelCase__				)				)				)


if __name__ == "__main__":
      import doctest

      doctest.testmod()
