

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

			def        SCREAMING_SNAKE_CASE					(       self					:    str     ,					__UpperCAmelCase					:    List[Any]							) ->     Dict:
						for model_result in results.values():
									for batch_size, sequence_length in zip(model_result["""bs"""]     ,					model_result["""ss"""]							):
												SCREAMING_SNAKE_CASE__										=   model_result["""result"""][batch_size][sequence_length]
												self.assertIsNotNone(__UpperCAmelCase							)

			def        SCREAMING_SNAKE_CASE					(       self					:    str							) ->     int:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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[str]							) ->     Dict:
						SCREAMING_SNAKE_CASE__										=   """sgugger/tiny-distilbert-classification"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					only_pretrain_model=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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							) ->     Optional[int]:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					torchscript=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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					:    Any							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					fpaa=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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[str]							) ->     int:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   AutoConfig.from_pretrained(__UpperCAmelCase							)
						# set architectures equal to `None`
						SCREAMING_SNAKE_CASE__										=   None
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase     ,					configs=[config]							)
						SCREAMING_SNAKE_CASE__										=   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					:    Tuple							) ->     int:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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					:    List[str]							) ->     List[Any]:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					fpaa=__UpperCAmelCase     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   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					:    Tuple							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   AutoConfig.from_pretrained(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase     ,					configs=[config]							)
						SCREAMING_SNAKE_CASE__										=   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							) ->     Optional[Any]:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tinier_bart"""
						SCREAMING_SNAKE_CASE__										=   AutoConfig.from_pretrained(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase     ,					configs=[config]							)
						SCREAMING_SNAKE_CASE__										=   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[str]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						SCREAMING_SNAKE_CASE__										=   AutoConfig.from_pretrained(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase     ,					configs=[config]							)
						SCREAMING_SNAKE_CASE__										=   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]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tinier_bart"""
						SCREAMING_SNAKE_CASE__										=   AutoConfig.from_pretrained(__UpperCAmelCase							)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
						    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					multi_process=__UpperCAmelCase     ,					)
						SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase     ,					configs=[config]							)
						SCREAMING_SNAKE_CASE__										=   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					:    Union[str, Any]							) ->     Any:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""
						with tempfile.TemporaryDirectory() as tmp_dir:
									SCREAMING_SNAKE_CASE__										=   PyTorchBenchmarkArguments(
									    models=[MODEL_ID]     ,					training=__UpperCAmelCase     ,					inference=__UpperCAmelCase     ,					save_to_csv=__UpperCAmelCase     ,					sequence_lengths=[8]     ,					batch_sizes=[1]     ,					inference_time_csv_file=os.path.join(__UpperCAmelCase     ,					"""inf_time.csv"""							)     ,					train_memory_csv_file=os.path.join(__UpperCAmelCase     ,					"""train_mem.csv"""							)     ,					inference_memory_csv_file=os.path.join(__UpperCAmelCase     ,					"""inf_mem.csv"""							)     ,					train_time_csv_file=os.path.join(__UpperCAmelCase     ,					"""train_time.csv"""							)     ,					env_info_csv_file=os.path.join(__UpperCAmelCase     ,					"""env.csv"""							)     ,					multi_process=__UpperCAmelCase     ,					)
									SCREAMING_SNAKE_CASE__										=   PyTorchBenchmark(__UpperCAmelCase							)
									benchmark.run()
									self.assertTrue(Path(os.path.join(__UpperCAmelCase     ,					"""inf_time.csv"""							)							).exists()							)
									self.assertTrue(Path(os.path.join(__UpperCAmelCase     ,					"""train_time.csv"""							)							).exists()							)
									self.assertTrue(Path(os.path.join(__UpperCAmelCase     ,					"""inf_mem.csv"""							)							).exists()							)
									self.assertTrue(Path(os.path.join(__UpperCAmelCase     ,					"""train_mem.csv"""							)							).exists()							)
									self.assertTrue(Path(os.path.join(__UpperCAmelCase     ,					"""env.csv"""							)							).exists()							)





			def        SCREAMING_SNAKE_CASE					(       self					:    Optional[Any]							) ->     List[str]:
						SCREAMING_SNAKE_CASE__										=   """sshleifer/tiny-gpt2"""

						def _check_summary_is_not_empty(__UpperCAmelCase					:    Union[str, Any]							):
									self.assertTrue(hasattr(__UpperCAmelCase     ,					"""sequential"""							)							)
									self.assertTrue(hasattr(__UpperCAmelCase     ,					"""cumulative"""							)							)
									self.assertTrue(hasattr(__UpperCAmelCase     ,					"""current"""							)							)
									self.assertTrue(hasattr(__UpperCAmelCase     ,					"""total"""							)							)

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





"""simple docstring"""




import pickle
import unittest

import torch

from accelerate import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils import require_cpu





@require_cpu
class  lowerCamelCase    (unittest.TestCase      ):

			def        SCREAMING_SNAKE_CASE					(       self					:    Dict							) ->     str:
						SCREAMING_SNAKE_CASE__										=   torch.nn.Linear(1_0     ,					1_0							)
						SCREAMING_SNAKE_CASE__										=   torch.optim.SGD(model.parameters()     ,					0.1							)
						SCREAMING_SNAKE_CASE__										=   Accelerator()
						SCREAMING_SNAKE_CASE__										=   accelerator.prepare(__UpperCAmelCase							)
						try:
									pickle.loads(pickle.dumps(__UpperCAmelCase							)							)
						except Exception as e:
									self.fail(F"""Accelerated optimizer pickling failed with {e}"""							)
						AcceleratorState._reset_state()


