


'''simple docstring'''



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




	def       __UpperCAmelCase							(   self						,				__a							):

						'''simple docstring'''





						for model_result in results.values():
											for batch_size, sequence_length in zip(model_result['bs']						,				model_result['ss']							):
																__a			:       List[Any]					    =  model_result['result'][batch_size][sequence_length]
																self.assertIsNotNone(__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[str]					    =  'sshleifer/tiny-gpt2'
						__a			:       Optional[Any]					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				eager_mode=__a						,				multi_process=__a						,				)
						__a			:       List[Any]					    =  TensorFlowBenchmark(__a							)
						__a			:       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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  'sgugger/tiny-distilbert-classification'
						__a			:       Optional[Any]					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				only_pretrain_model=__a						,				)
						__a			:       Dict					    =  TensorFlowBenchmark(__a							)
						__a			:       Tuple					    =  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							):

						'''simple docstring'''





						__a			:       Tuple					    =  'sshleifer/tiny-gpt2'
						__a			:       Optional[Any]					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				)
						__a			:       str					    =  TensorFlowBenchmark(__a							)
						__a			:       str					    =  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							):

						'''simple docstring'''





						__a			:       str					    =  'sshleifer/tiny-gpt2'
						__a			:       Optional[Any]					    =  AutoConfig.from_pretrained(__a							)
						__a			:       Optional[Any]					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				eager_mode=__a						,				multi_process=__a						,				)
						__a			:       Tuple					    =  TensorFlowBenchmark(__a						,				[config]							)
						__a			:       Optional[int]					    =  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							):

						'''simple docstring'''





						__a			:       Tuple					    =  'sshleifer/tiny-gpt2'
						__a			:       Optional[int]					    =  AutoConfig.from_pretrained(__a							)
						__a			:       Dict					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				)
						__a			:       Tuple					    =  TensorFlowBenchmark(__a						,				[config]							)
						__a			:       Union[str, Any]					    =  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							):

						'''simple docstring'''





						__a			:       List[Any]					    =  'sshleifer/tiny-gpt2'
						__a			:       Dict					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				)
						__a			:       Optional[Any]					    =  TensorFlowBenchmark(__a							)
						__a			:       str					    =  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							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  'sshleifer/tiny-gpt2'
						__a			:       Tuple					    =  AutoConfig.from_pretrained(__a							)
						__a			:       Dict					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				)
						__a			:       Optional[Any]					    =  TensorFlowBenchmark(__a						,				[config]							)
						__a			:       Any					    =  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							):

						'''simple docstring'''





						__a			:       Dict					    =  'patrickvonplaten/t5-tiny-random'
						__a			:       Optional[int]					    =  AutoConfig.from_pretrained(__a							)
						__a			:       Optional[int]					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				multi_process=__a						,				)
						__a			:       str					    =  TensorFlowBenchmark(__a						,				configs=[config]							)
						__a			:       Union[str, Any]					    =  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							):

						'''simple docstring'''





						__a			:       Optional[Any]					    =  'sshleifer/tiny-gpt2'
						__a			:       str					    =  TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]						,				training=__a						,				inference=__a						,				sequence_lengths=[8]						,				batch_sizes=[1]						,				use_xla=__a						,				multi_process=__a						,				)
						__a			:       int					    =  TensorFlowBenchmark(__a							)
						__a			:       Tuple					    =  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							):

						'''simple docstring'''





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





	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Tuple					    =  'sshleifer/tiny-gpt2'

						def _check_summary_is_not_empty(__a							):
											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:
											__a			:       Optional[int]					    =  TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]						,				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						,				eager_mode=__a						,				multi_process=__a						,				)
											__a			:       Any					    =  TensorFlowBenchmark(__a							)
											__a			:       str					    =  benchmark.run()
											_check_summary_is_not_empty(result.inference_summary							)
											self.assertTrue(Path(os.path.join(__a						,				'log.txt'							)							).exists()							)







'''simple docstring'''



import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_huggingface_suite,
    require_multi_gpu,
    require_single_gpu,
)
from accelerate.utils import patch_environment





@require_huggingface_suite
class 							__UpperCamelCase				(		unittest.TestCase					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       List[Any]					    =  inspect.getfile(accelerate.test_utils							)
						__a			:       List[str]					    =  os.path.sep.join(
						    mod_file.split(os.path.sep							)[:-1] + ['scripts', 'external_deps', 'test_metrics.py']							)

						from accelerate.test_utils.scripts.external_deps import test_metrics  # noqa: F401

						__a			:       Union[str, Any]					    =  test_metrics




	@require_cpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						debug_launcher(self.test_metrics.main						,				num_processes=1							)




	@require_cpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						debug_launcher(self.test_metrics.main							)




	@require_single_gpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						self.test_metrics.main()





	@require_multi_gpu
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						print(f"""Found {torch.cuda.device_count()} devices."""							)
						__a			:       List[Any]					    =  ['torchrun', f"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
						with patch_environment(omp_num_threads=1							):
											execute_subprocess_async(__a						,				env=os.environ.copy()							)



