


"""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):

	"""simple docstring"""





	def 						UpperCAmelCase_   (				self    :       Tuple    ,    UpperCAmelCase__    :       str     ) ->			Optional[int]:




		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 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			Union[str, Any]:
		__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 						UpperCAmelCase_   (				self    :       int     ) ->			List[str]:
		__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 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			List[Any]:
		__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 						UpperCAmelCase_   (				self    :       List[str]     ) ->			Union[str, Any]:
		__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 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			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 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Optional[Any]:
		__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 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			List[str]:
		__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 						UpperCAmelCase_   (				self    :       Dict     ) ->			str:
		__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 						UpperCAmelCase_   (				self    :       Optional[Any]     ) ->			Union[str, 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 						UpperCAmelCase_   (				self    :       Union[str, Any]     ) ->			List[str]:
		__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 						UpperCAmelCase_   (				self    :       int     ) ->			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 						UpperCAmelCase_   (				self    :       str     ) ->			Optional[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 						UpperCAmelCase_   (				self    :       Dict     ) ->			Tuple:
		__SCREAMING_SNAKE_CASE		     =       "sshleifer/tiny-gpt2"

		def _check_summary_is_not_empty(UpperCAmelCase__    :       Optional[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 platform
from argparse import ArgumentParser

import huggingface_hub

from .. import __version__ as version
from ..utils import is_accelerate_available, is_torch_available, is_transformers_available, is_xformers_available
from . import BaseDiffusersCLICommand




def      UpperCAmelCase__ (lowerCAmelCase_     ):




					'''simple docstring'''


					return EnvironmentCommand()




class 				UpperCamelCase_			(				UpperCamelCase):

	"""simple docstring"""





	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       ArgumentParser     ) ->			Optional[Any]:
						__SCREAMING_SNAKE_CASE		     =       parser.add_parser("env"     )
						download_parser.set_defaults(func=UpperCAmelCase__     )




	def 						UpperCAmelCase_   (				self    :       List[Any]     ) ->			List[str]:
						__SCREAMING_SNAKE_CASE		     =       huggingface_hub.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						__SCREAMING_SNAKE_CASE		     =       "NA"
						if is_torch_available():
											import torch

											__SCREAMING_SNAKE_CASE		     =       torch.__version__
											__SCREAMING_SNAKE_CASE		     =       torch.cuda.is_available()

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_transformers_available():
											import transformers

											__SCREAMING_SNAKE_CASE		     =       transformers.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_accelerate_available():
											import accelerate

											__SCREAMING_SNAKE_CASE		     =       accelerate.__version__

						__SCREAMING_SNAKE_CASE		     =       "not installed"
						if is_xformers_available():
											import xformers

											__SCREAMING_SNAKE_CASE		     =       xformers.__version__

						__SCREAMING_SNAKE_CASE		     =       {
						    "`diffusers` version": version,
						    "Platform": platform.platform(),
						    "Python version": platform.python_version(),
						    "PyTorch version (GPU?)": F"""{pt_version} ({pt_cuda_available})""",
						    "Huggingface_hub version": hub_version,
						    "Transformers version": transformers_version,
						    "Accelerate version": accelerate_version,
						    "xFormers version": xformers_version,
						    "Using GPU in script?": "<fill in>",
						    "Using distributed or parallel set-up in script?": "<fill in>",
						}

						print("\nCopy-and-paste the text below in your GitHub issue and FILL OUT the two last points.\n"     )
						print(self.format_dict(UpperCAmelCase__     )     )

						return info




	@staticmethod
	def 						UpperCAmelCase_   (				UpperCAmelCase__    :       Optional[Any]     ) ->			str:
						return "\n".join([F"""- {prop}: {val}""" for prop, val in d.items()]     ) + "\n"
