






'''simple docstring'''







import os
import string
import sys


UpperCamelCase    :      Optional[int]  			=		1 << 8

UpperCamelCase    :      Any  			=		{
    'tab': ord('\t'),
    'newline': ord('\r'),
    'esc': 27,
    'up': 65 + ARROW_KEY_FLAG,
    'down': 66 + ARROW_KEY_FLAG,
    'right': 67 + ARROW_KEY_FLAG,
    'left': 68 + ARROW_KEY_FLAG,
    'mod_int': 91,
    'undefined': sys.maxsize,
    'interrupt': 3,
    'insert': 50,
    'delete': 51,
    'pg_up': 53,
    'pg_down': 54,
}

UpperCamelCase    :      Any  			=		KEYMAP['up']
UpperCamelCase    :      int  			=		KEYMAP['left']

if sys.platform == "win32":
							UpperCamelCase    :      Union[str, Any]  			=		[]
							UpperCamelCase    :      Any  			=		{
							    B'\xe0H': KEYMAP['up'] - ARROW_KEY_FLAG,
							    B'\x00H': KEYMAP['up'] - ARROW_KEY_FLAG,
							    B'\xe0P': KEYMAP['down'] - ARROW_KEY_FLAG,
							    B'\x00P': KEYMAP['down'] - ARROW_KEY_FLAG,
							    B'\xe0M': KEYMAP['right'] - ARROW_KEY_FLAG,
							    B'\x00M': KEYMAP['right'] - ARROW_KEY_FLAG,
							    B'\xe0K': KEYMAP['left'] - ARROW_KEY_FLAG,
							    B'\x00K': KEYMAP['left'] - ARROW_KEY_FLAG,
							}

for i in range(10):
							UpperCamelCase    :      Tuple  			=		ord(str(i))
def 						A__			(				):
						if os.name == "nt":
												import msvcrt

												lowerCamelCase__         =  """mbcs"""
												# Flush the keyboard buffer
												while msvcrt.kbhit():
																		msvcrt.getch()
												if len(__lowerCAmelCase						) == 0:
																		# Read the keystroke
																		lowerCamelCase__         =  msvcrt.getch()

																		# If it is a prefix char, get second part
																		if ch in (b"\x00", b"\xe0"):
																								lowerCamelCase__         =  ch + msvcrt.getch()
																								# Translate actual Win chars to bullet char types
																								try:
																														lowerCamelCase__         =  chr(WIN_KEYMAP[cha]						)
																														WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""]						)						)
																														WIN_CH_BUFFER.append(__lowerCAmelCase						)
																														if ord(__lowerCAmelCase						) in (
																														    KEYMAP["insert"] - 1 << 9,
																														    KEYMAP["delete"] - 1 << 9,
																														    KEYMAP["pg_up"] - 1 << 9,
																														    KEYMAP["pg_down"] - 1 << 9,
																														):
																																				WIN_CH_BUFFER.append(chr(126						)						)
																														lowerCamelCase__         =  chr(KEYMAP["""esc"""]						)
																								except KeyError:
																														lowerCamelCase__         =  cha[1]
																		else:
																								lowerCamelCase__         =  ch.decode(__lowerCAmelCase						)
												else:
																		lowerCamelCase__         =  WIN_CH_BUFFER.pop(0						)
						elif os.name == "posix":
												import termios
												import tty

												lowerCamelCase__         =  sys.stdin.fileno()
												lowerCamelCase__         =  termios.tcgetattr(__lowerCAmelCase						)
												try:
																		tty.setraw(__lowerCAmelCase						)
																		lowerCamelCase__         =  sys.stdin.read(1						)
												finally:
																		termios.tcsetattr(__lowerCAmelCase   ,    termios.TCSADRAIN   ,    __lowerCAmelCase						)
						return ch





def 						A__			(				):
						lowerCamelCase__         =  get_raw_chars()
						if ord(__lowerCAmelCase						) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
												return char

						elif ord(__lowerCAmelCase						) == KEYMAP["esc"]:
												lowerCamelCase__         =  get_raw_chars()
												if ord(__lowerCAmelCase						) == KEYMAP["mod_int"]:
																		lowerCamelCase__         =  get_raw_chars()
																		if ord(__lowerCAmelCase						) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(__lowerCAmelCase						) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																								return chr(ord(__lowerCAmelCase						) + ARROW_KEY_FLAG						)
																		else:
																								return KEYMAP["undefined"]
												else:
																		return get_raw_chars()

						else:
												if char in string.printable:
																		return char
												else:
																		return KEYMAP["undefined"]









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




		'''simple docstring'''





		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase       ):
								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(_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sgugger/tiny-distilbert-classification"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,only_pretrain_model=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,eager_mode=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,[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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """patrickvonplaten/t5-tiny-random"""
								lowerCamelCase__         =  AutoConfig.from_pretrained(_lowerCAmelCase       )
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase   ,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       )






		@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("""GPU"""       )       ) == 0   ,"""Cannot do xla on CPU."""       )
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								lowerCamelCase__         =  TensorFlowBenchmarkArguments(
								    models=[MODEL_ID]   ,training=_lowerCAmelCase   ,inference=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,use_xla=_lowerCAmelCase   ,multi_process=_lowerCAmelCase   ,)
								lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
								lowerCamelCase__         =  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       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""
								with tempfile.TemporaryDirectory() as tmp_dir:
														lowerCamelCase__         =  TensorFlowBenchmarkArguments(
														    models=[MODEL_ID]   ,inference=_lowerCAmelCase   ,save_to_csv=_lowerCAmelCase   ,sequence_lengths=[8]   ,batch_sizes=[1]   ,inference_time_csv_file=os.path.join(_lowerCAmelCase   ,"""inf_time.csv"""       )   ,inference_memory_csv_file=os.path.join(_lowerCAmelCase   ,"""inf_mem.csv"""       )   ,env_info_csv_file=os.path.join(_lowerCAmelCase   ,"""env.csv"""       )   ,multi_process=_lowerCAmelCase   ,)
														lowerCamelCase__         =  TensorFlowBenchmark(_lowerCAmelCase       )
														benchmark.run()
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""inf_time.csv"""       )       ).exists()       )
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""inf_mem.csv"""       )       ).exists()       )
														self.assertTrue(Path(os.path.join(_lowerCAmelCase   ,"""env.csv"""       )       ).exists()       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  """sshleifer/tiny-gpt2"""

								def _check_summary_is_not_empty(_lowerCAmelCase       ):
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""sequential"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""cumulative"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""current"""       )       )
														self.assertTrue(hasattr(_lowerCAmelCase   ,"""total"""       )       )

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

