


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






			"""simple docstring"""






			def 					snake_case__					(				self   ,    snake_case  ):

						'''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(snake_case  )




			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    eager_mode=snake_case   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sgugger/tiny-distilbert-classification"
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    only_pretrain_model=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							AutoConfig.from_pretrained(snake_case  )
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    eager_mode=snake_case   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							AutoConfig.from_pretrained(snake_case  )
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							AutoConfig.from_pretrained(snake_case  )
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"patrickvonplaten/t5-tiny-random"
						UpperCamelCase__       =							AutoConfig.from_pretrained(snake_case  )
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"
						UpperCamelCase__       =							TensorFlowBenchmarkArguments(
						    models=[MODEL_ID]   ,    training=snake_case   ,    inference=snake_case   ,    sequence_lengths=[8]   ,    batch_sizes=[1]   ,    use_xla=snake_case   ,    multi_process=snake_case   ,    )
						UpperCamelCase__       =							TensorFlowBenchmark(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 					snake_case__					(				self  ):

						'''simple docstring'''


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







			def 					snake_case__					(				self  ):

						'''simple docstring'''


						UpperCamelCase__       =							"sshleifer/tiny-gpt2"

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

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






__UpperCamelCase     		=							2_5_6
# Modulus to hash a string
__UpperCamelCase     		=							1_0_0_0_0_0_3
def 							UpperCamelCase_(					_A :str					,  _A :str	)->		bool:
			UpperCamelCase__       =							len(_A	)
			UpperCamelCase__       =							len(_A	)
			if p_len > t_len:
						return False

			UpperCamelCase__       =							0
			UpperCamelCase__       =							0
			UpperCamelCase__       =							1

			# Calculating the hash of pattern and substring of text
			for i in range(_A	):
						UpperCamelCase__       =							(ord(pattern[i]	) + p_hash * alphabet_size) % modulus
						UpperCamelCase__       =							(ord(text[i]	) + text_hash * alphabet_size) % modulus
						if i == p_len - 1:
									continue
						UpperCamelCase__       =							(modulus_power * alphabet_size) % modulus

			for i in range(0					,  t_len - p_len + 1	):
						if text_hash == p_hash and text[i : i + p_len] == pattern:
									return True
						if i == t_len - p_len:
									continue
						# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
						UpperCamelCase__       =							(
						    (text_hash - ord(text[i]	) * modulus_power) * alphabet_size
						    + ord(text[i + p_len]	)
						) % modulus
			return False
def 							UpperCamelCase_(					)->		None:
			UpperCamelCase__       =							"abc1abc12"
			UpperCamelCase__       =							"alskfjaldsabc1abc1abc12k23adsfabcabc"
			UpperCamelCase__       =							"alskfjaldsk23adsfabcabc"
			assert rabin_karp(_A					,  _A	) and not rabin_karp(_A					,  _A	)

			# Test 2)
			UpperCamelCase__       =							"ABABX"
			UpperCamelCase__       =							"ABABZABABYABABX"
			assert rabin_karp(_A					,  _A	)

			# Test 3)
			UpperCamelCase__       =							"AAAB"
			UpperCamelCase__       =							"ABAAAAAB"
			assert rabin_karp(_A					,  _A	)

			# Test 4)
			UpperCamelCase__       =							"abcdabcy"
			UpperCamelCase__       =							"abcxabcdabxabcdabcdabcy"
			assert rabin_karp(_A					,  _A	)

			# Test 5)
			UpperCamelCase__       =							"Lü"
			UpperCamelCase__       =							"Lüsai"
			assert rabin_karp(_A					,  _A	)
			UpperCamelCase__       =							"Lue"
			assert not rabin_karp(_A					,  _A	)
			print("Success."	)


if __name__ == "__main__":
						test_rabin_karp()


