






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

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase       )->					Dict:







							'''simple docstring'''





							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 		UpperCAmelCase  (							self       )->					Any:







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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







							'''simple docstring'''





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

	@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("GPU"       )       ) == 0		,    "Cannot do xla on CPU."       )
	def 		UpperCAmelCase  (							self       )->					Tuple:







							'''simple docstring'''





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







							'''simple docstring'''





							lowerCAmelCase__       =						"sshleifer/tiny-gpt2"
							with tempfile.TemporaryDirectory() as tmp_dir:
													lowerCAmelCase__       =						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_		,    )
													lowerCAmelCase__       =						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       )->					Dict:







							'''simple docstring'''





							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__       =						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_		,    )
													lowerCAmelCase__       =						TensorFlowBenchmark(A_       )
													lowerCAmelCase__       =						benchmark.run()
													_check_summary_is_not_empty(result.inference_summary       )
													self.assertTrue(Path(os.path.join(A_		,    "log.txt"       )       ).exists()       )














import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class      lowercase__     (			_UpperCAmelCase,					unittest.TestCase	):
	a_         =None
	a_         =BloomTokenizerFast
	a_         =BloomTokenizerFast
	a_         =True
	a_         =False
	a_         ="""tokenizer_file"""
	a_         ={"""bos_token""": """<s>""", """eos_token""": """</s>""", """unk_token""": """<unk>""", """pad_token""": """<pad>"""}

	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							super().setUp()
							lowerCAmelCase__       =						BloomTokenizerFast.from_pretrained("bigscience/tokenizer"       )
							tokenizer.save_pretrained(self.tmpdirname       )

	def 		UpperCAmelCase  (							self		,    **__UpperCAmelCase       )->					int:







							'''simple docstring'''





							kwargs.update(self.special_tokens_map       )
							return BloomTokenizerFast.from_pretrained(self.tmpdirname		,    **__UpperCAmelCase       )

	def 		UpperCAmelCase  (							self       )->					Any:







							'''simple docstring'''





							lowerCAmelCase__       =						self.get_rust_tokenizer()

							lowerCAmelCase__       =						["The quick brown fox</s>", "jumps over the lazy dog</s>"]
							lowerCAmelCase__       =						[[2175, 23714, 73173, 144252, 2], [77, 132619, 3478, 368, 109586, 35433, 2]]

							lowerCAmelCase__       =						tokenizer.batch_encode_plus(__UpperCAmelCase       )["input_ids"]
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )

							lowerCAmelCase__       =						tokenizer.batch_decode(__UpperCAmelCase       )
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )

	def 		UpperCAmelCase  (							self		,    __UpperCAmelCase=6       )->					str:







							'''simple docstring'''





							for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
													with self.subTest(F"{tokenizer.__class__.__name__} ({pretrained_name})"       ):
																			lowerCAmelCase__       =						self.rust_tokenizer_class.from_pretrained(__UpperCAmelCase		,    **__UpperCAmelCase       )
																			# tokenizer_r.pad_token = None # Hotfixing padding = None
																			# Simple input
																			lowerCAmelCase__       =						"This is a simple input"
																			lowerCAmelCase__       =						["This is a simple input 1", "This is a simple input 2"]
																			lowerCAmelCase__       =						("This is a simple input", "This is a pair")
																			lowerCAmelCase__       =						[
																			    ("This is a simple input 1", "This is a simple input 2"),
																			    ("This is a simple pair 1", "This is a simple pair 2"),
																			]

																			# Simple input tests
																			try:
																									tokenizer_r.encode(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )

																									tokenizer_r.batch_encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.encode(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																									tokenizer_r.batch_encode_plus(__UpperCAmelCase		,    max_length=__UpperCAmelCase       )
																			except ValueError:
																									self.fail("Bloom Tokenizer should be able to deal with padding"       )

																			lowerCAmelCase__       =						None  # Hotfixing padding = None
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Simple input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Simple input
																			self.assertRaises(
																			    __UpperCAmelCase		,    tokenizer_r.batch_encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"		,    )

																			# Pair input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Pair input
																			self.assertRaises(__UpperCAmelCase		,    tokenizer_r.encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"       )

																			# Pair input
																			self.assertRaises(
																			    __UpperCAmelCase		,    tokenizer_r.batch_encode_plus		,    __UpperCAmelCase		,    max_length=__UpperCAmelCase		,    padding="max_length"		,    )

	def 		UpperCAmelCase  (							self       )->					int:







							'''simple docstring'''





							lowerCAmelCase__       =						self.get_rust_tokenizer()
							lowerCAmelCase__       =						load_dataset("xnli"		,    "all_languages"		,    split="test"		,    streaming=__UpperCAmelCase       )

							lowerCAmelCase__       =						next(iter(__UpperCAmelCase       )       )["premise"]  # pick up one data
							lowerCAmelCase__       =						list(sample_data.values()       )

							lowerCAmelCase__       =						list(map(tokenizer.encode		,    __UpperCAmelCase       )       )
							lowerCAmelCase__       =						[tokenizer.decode(__UpperCAmelCase		,    clean_up_tokenization_spaces=__UpperCAmelCase       ) for x in output_tokens]
							self.assertListEqual(__UpperCAmelCase		,    __UpperCAmelCase       )


	def 		UpperCAmelCase  (							self       )->					List[str]:







							'''simple docstring'''





							self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map       )		,    1       )
							self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()       )[0]       )		,    1       )






