



"""simple docstring"""





import unittest
from dataclasses import dataclass

import pytest

from accelerate.commands.config.config_args import SageMakerConfig
from accelerate.utils import ComputeEnvironment
from accelerate.utils.launch import _convert_nargs_to_dict




@dataclass
class       lowercase__      (   A     ):
							'''simple docstring'''







							_UpperCAmelCase   					=       ComputeEnvironment.AMAZON_SAGEMAKER
							_UpperCAmelCase   					=       True
							_UpperCAmelCase   					=       'ml.p3.2xlarge'
							_UpperCAmelCase   					=       'accelerate_sagemaker_execution_role'
							_UpperCAmelCase   					=       'hf-sm'
							_UpperCAmelCase   					=       'us-east-1'
							_UpperCAmelCase   					=       1
							_UpperCAmelCase   					=       'accelerate-sagemaker-1'
							_UpperCAmelCase   					=       '1.6'
							_UpperCAmelCase   					=       '4.4'
							_UpperCAmelCase   					=       'train.py'
							_UpperCAmelCase   					=       [
							    '--model_name_or_path',
							    'bert',
							    '--do_train',
							    'False',
							    '--epochs',
							    '3',
							    '--learning_rate',
							    '5e-5',
							    '--max_steps',
							    '50.5',
							]
							_UpperCAmelCase   					=       [
							    '--model_name_or_path',
							    'bert',
							    '--do_train',
							    '--do_test',
							    'False',
							    '--do_predict',
							    '--epochs',
							    '3',
							    '--learning_rate',
							    '5e-5',
							    '--max_steps',
							    '50.5',
							]







class       lowercase__      (   unittest.TestCase     ):
							'''simple docstring'''







							def 							lowerCamelCase_ (	self    )      ->				List[Any]:
									_UpperCAmelCase							=		_convert_nargs_to_dict(MockLaunchConfig.success_training_script_args    )
									assert isinstance(converted_args['model_name_or_path']						,							_SCREAMING_SNAKE_CASE    )
									assert isinstance(converted_args['do_train']						,							_SCREAMING_SNAKE_CASE    )
									assert isinstance(converted_args['epochs']						,							_SCREAMING_SNAKE_CASE    )
									assert isinstance(converted_args['learning_rate']						,							_SCREAMING_SNAKE_CASE    )
									assert isinstance(converted_args['max_steps']						,							_SCREAMING_SNAKE_CASE    )

									with pytest.raises(_SCREAMING_SNAKE_CASE    ):
											_convert_nargs_to_dict(MockLaunchConfig.fail_training_script_args    )






import os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
		import tensorflow as tf

		from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments

@require_tf
class 						__a (							unittest.TestCase     ):
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									for model_result in results.values():
																for batch_size, sequence_length in zip(model_result['bs']     ,       model_result['ss']		):
																							_UpperCAmelCase											=     model_result['result'][batch_size][sequence_length]
																							self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sgugger/tiny-distilbert-classification'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       only_pretrain_model=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_train_result		)
									self.check_results_dict_not_empty(results.memory_train_result		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       [config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_train_result		)
									self.check_results_dict_not_empty(results.memory_train_result		)
		def       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									_UpperCAmelCase											=     'patrickvonplaten/t5-tiny-random'
									_UpperCAmelCase											=     AutoConfig.from_pretrained(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE     ,       configs=[config]		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices('GPU'		)		) == 0     ,       'Cannot do xla on CPU.'		)
		def       UpperCAmelCase__							(     self		)					->			int:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
									    models=[MODEL_ID]     ,       training=_SCREAMING_SNAKE_CASE     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       use_xla=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
									_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     benchmark.run()
									self.check_results_dict_not_empty(results.time_inference_result		)
									self.check_results_dict_not_empty(results.memory_inference_result		)
		def       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'
									with tempfile.TemporaryDirectory() as tmp_dir:
																_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
																    models=[MODEL_ID]     ,       inference=_SCREAMING_SNAKE_CASE     ,       save_to_csv=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       inference_time_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)     ,       inference_memory_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)     ,       env_info_csv_file=os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
																_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
																benchmark.run()
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_time.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'inf_mem.csv'		)		).exists()		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'env.csv'		)		).exists()		)






		def       UpperCAmelCase__							(     self		)					->			Tuple:




									"""simple docstring"""





									_UpperCAmelCase											=     'sshleifer/tiny-gpt2'

									def _check_summary_is_not_empty(_SCREAMING_SNAKE_CASE		):
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'sequential'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'cumulative'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'current'		)		)
																self.assertTrue(hasattr(_SCREAMING_SNAKE_CASE     ,       'total'		)		)

									with tempfile.TemporaryDirectory() as tmp_dir:
																_UpperCAmelCase											=     TensorFlowBenchmarkArguments(
																    models=[MODEL_ID]     ,       inference=_SCREAMING_SNAKE_CASE     ,       sequence_lengths=[8]     ,       batch_sizes=[1]     ,       log_filename=os.path.join(_SCREAMING_SNAKE_CASE     ,       'log.txt'		)     ,       log_print=_SCREAMING_SNAKE_CASE     ,       trace_memory_line_by_line=_SCREAMING_SNAKE_CASE     ,       eager_mode=_SCREAMING_SNAKE_CASE     ,       multi_process=_SCREAMING_SNAKE_CASE     ,       )
																_UpperCAmelCase											=     TensorFlowBenchmark(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     benchmark.run()
																_check_summary_is_not_empty(result.inference_summary		)
																self.assertTrue(Path(os.path.join(_SCREAMING_SNAKE_CASE     ,       'log.txt'		)		).exists()		)
