
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   __a		(      self       ,       _UpperCamelCase  )       ->   Tuple:
							for model_result in results.values():
										for batch_size, sequence_length in zip(model_result["bs"]       ,       model_result["ss"]  ):
													lowerCAmelCase_      			=			model_result["result"][batch_size][sequence_length]
													self.assertIsNotNone(__A  )




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




				def   __a		(      self  )       ->   Dict:
							lowerCAmelCase_      			=			"sshleifer/tiny-gpt2"

							def _check_summary_is_not_empty(_UpperCamelCase  ):
										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:
										lowerCAmelCase_      			=			PyTorchBenchmarkArguments(
										    models=[MODEL_ID]       ,       training=__A       ,       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       ,       multi_process=__A       ,       )
										lowerCAmelCase_      			=			PyTorchBenchmark(__A  )
										lowerCAmelCase_      			=			benchmark.run()
										_check_summary_is_not_empty(result.inference_summary  )
										_check_summary_is_not_empty(result.train_summary  )
										self.assertTrue(Path(os.path.join(__A       ,       "log.txt"  )  ).exists()  )





def      lowerCamelCase__       (       __lowerCAmelCase						:						int       ):




			"""simple docstring"""


			if upper_limit < 0:
						raise ValueError("Limit for the Catalan sequence must be ≥ 0"       )

			lowerCAmelCase_      			=			[0] * (upper_limit + 1)

			# Base case: C(0) = C(1) = 1
			lowerCAmelCase_      			=			1
			if upper_limit > 0:
						lowerCAmelCase_      			=			1

			# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
			for i in range(2					,							upper_limit + 1       ):
						for j in range(__lowerCAmelCase       ):
									catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

			return catalan_list


if __name__ == "__main__":
			print("\n********* Catalan Numbers Using Dynamic Programming ************\n")
			print("\n*** Enter -1 at any time to quit ***")
			print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="")
			try:
						while True:
									_A     =  int(input().strip())
									if N < 0:
												print("\n********* Goodbye!! ************")
												break
									else:
												print(f"""The Catalan numbers from 0 through {N} are:""")
												print(catalan_numbers(N))
												print("Try another upper limit for the sequence: ", end="")
			except (NameError, ValueError):
						print("\n********* Invalid input, goodbye! ************\n")

			import doctest

			doctest.testmod()



