





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







	def 						__a   (				self			:   Dict , lowerCamelCase			:   str		):




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




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




				'''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 						__a   (				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_train_result		)
				self.check_results_dict_not_empty(results.memory_train_result		)







	@unittest.skipIf(torch_device == "cpu" , "Can't do half precision"		)
	def 						__a   (				self			:   List[str]		):




				'''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 						__a   (				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 						__a   (				self			:   int		):




				'''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 						__a   (				self			:   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 						__a   (				self			:   Union[str, 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_train_result		)
				self.check_results_dict_not_empty(results.memory_train_result		)







	def 						__a   (				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 						__a   (				self			:   str		):




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

				def _check_summary_is_not_empty(lowerCamelCase			:   List[str]		):
							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'''


# Copyright 2021 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse

from ...utils.dataclasses import (
    ComputeEnvironment,
    DistributedType,
    DynamoBackend,
    PrecisionType,
    SageMakerDistributedType,
)
from ..menu import BulletMenu


lowerCAmelCase_   :   List[Any]       =					[
    "EAGER",
    "AOT_EAGER",
    "INDUCTOR",
    "NVFUSER",
    "AOT_NVFUSER",
    "AOT_CUDAGRAPHS",
    "OFI",
    "FX2TRT",
    "ONNXRT",
    "IPEX",
]







def    _lowerCamelCase  (__lowerCamelCase  :     Tuple					,  __lowerCamelCase  :     Tuple=None					,  __lowerCamelCase  :     Tuple=None					,  __lowerCamelCase  :     List[Any]=None     )   ->    Any:
			a__     = True
			while ask_again:
						a__     = input(__lowerCamelCase     )
						try:
									if default is not None and len(__lowerCamelCase     ) == 0:
												return default
									return convert_value(__lowerCamelCase     ) if convert_value is not None else result
						except Exception:
									if error_message is not None:
												print(__lowerCamelCase     )







def    _lowerCamelCase  (__lowerCamelCase  :     Dict					,  __lowerCamelCase  :     List[Any]=[]					,  __lowerCamelCase  :     str=None					,  __lowerCamelCase  :     List[Any]=0     )   ->    List[Any]:
			a__     = BulletMenu(__lowerCamelCase					,  __lowerCamelCase     )
			a__     = menu.run(default_choice=__lowerCamelCase     )
			return convert_value(__lowerCamelCase     ) if convert_value is not None else result







def    _lowerCamelCase  (__lowerCamelCase  :     Tuple     )   ->    Optional[Any]:
			a__     = int(__lowerCamelCase     )
			return ComputeEnvironment(["LOCAL_MACHINE", "AMAZON_SAGEMAKER"][value]     )







def    _lowerCamelCase  (__lowerCamelCase  :     List[str]     )   ->    Optional[int]:
			a__     = int(__lowerCamelCase     )
			return DistributedType(["NO", "MULTI_CPU", "MULTI_XPU", "MULTI_GPU", "MULTI_NPU", "TPU"][value]     )







def    _lowerCamelCase  (__lowerCamelCase  :     List[Any]     )   ->    Optional[Any]:
			a__     = int(__lowerCamelCase     )
			return DynamoBackend(DYNAMO_BACKENDS[value]     ).value







def    _lowerCamelCase  (__lowerCamelCase  :     str     )   ->    str:
			a__     = int(__lowerCamelCase     )
			return PrecisionType(["no", "fp16", "bf16", "fp8"][value]     )







def    _lowerCamelCase  (__lowerCamelCase  :     int     )   ->    Optional[Any]:
			a__     = int(__lowerCamelCase     )
			return SageMakerDistributedType(["NO", "DATA_PARALLEL", "MODEL_PARALLEL"][value]     )







def    _lowerCamelCase  (__lowerCamelCase  :     Any     )   ->    List[Any]:
			return {"yes": True, "no": False}[value.lower()]






class       UpperCamelCase__  (		argparse.RawDescriptionHelpFormatter     ):







	def 						__a   (				self			:   Dict , lowerCamelCase			:   int , lowerCamelCase			:   Optional[Any] , lowerCamelCase			:   List[Any] , lowerCamelCase			:   Dict		):




				'''simple docstring'''
				a__     = super()._format_usage(lowerCamelCase , lowerCamelCase , lowerCamelCase , lowerCamelCase		)
				a__     = usage.replace("<command> [<args>] " , ""		)
				return usage



