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







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							Optional[int]      ,    _A							:							List[Any]):



											"""simple docstring"""







											for model_result in results.values():
																		for batch_size, sequence_length in zip(model_result["""bs"""]      ,    model_result["""ss"""]):
																									_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    model_result["""result"""][batch_size][sequence_length]
																									self.assertIsNotNone(_A)





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    eager_mode=_A      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    TensorFlowBenchmark(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    """sgugger/tiny-distilbert-classification"""
											_SCREAMING_SNAKE_CASE		:						Dict			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    only_pretrain_model=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Dict			  =    TensorFlowBenchmark(_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TensorFlowBenchmark(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						Dict			  =    AutoConfig.from_pretrained(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    eager_mode=_A      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    TensorFlowBenchmark(_A      ,    [config])
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    AutoConfig.from_pretrained(_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TensorFlowBenchmark(_A      ,    [config])
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TensorFlowBenchmark(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_train_result)
											self.check_results_dict_not_empty(results.memory_train_result)





				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    AutoConfig.from_pretrained(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						str			  =    TensorFlowBenchmark(_A      ,    [config])
											_SCREAMING_SNAKE_CASE		:						str			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_train_result)
											self.check_results_dict_not_empty(results.memory_train_result)





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    """patrickvonplaten/t5-tiny-random"""
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoConfig.from_pretrained(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    TensorFlowBenchmark(_A      ,    configs=[config])
											_SCREAMING_SNAKE_CASE		:						str			  =    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 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    """sshleifer/tiny-gpt2"""
											_SCREAMING_SNAKE_CASE		:						Dict			  =    TensorFlowBenchmarkArguments(
											    models=[MODEL_ID]      ,    training=_A      ,    inference=_A      ,    sequence_lengths=[8]      ,    batch_sizes=[1]      ,    use_xla=_A      ,    multi_process=_A      ,    )
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    TensorFlowBenchmark(_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    benchmark.run()
											self.check_results_dict_not_empty(results.time_inference_result)
											self.check_results_dict_not_empty(results.memory_inference_result)





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    """sshleifer/tiny-gpt2"""
											with tempfile.TemporaryDirectory() as tmp_dir:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    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      ,    )
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    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 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    """sshleifer/tiny-gpt2"""

											def _check_summary_is_not_empty(_A							:							Any):
																		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:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    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      ,    )
																		_SCREAMING_SNAKE_CASE		:						Any			  =    TensorFlowBenchmark(_A)
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    benchmark.run()
																		_check_summary_is_not_empty(result.inference_summary)
																		self.assertTrue(Path(os.path.join(_A      ,    """log.txt""")).exists())



"""simple docstring"""





import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
			import torch

			from transformers import AutoModelForCausalLM




@require_torch
class  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    -1

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    tokenizer.decode(greedy_ids[0])

											with CaptureStdout() as cs:
																		_SCREAMING_SNAKE_CASE		:						Any			  =    TextStreamer(_A)
																		model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A      ,    streamer=_A)
											# The greedy text should be printed to stdout, except for the final "\n" in the streamer
											_SCREAMING_SNAKE_CASE		:						str			  =    cs.out[:-1]

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Any			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    -1

											_SCREAMING_SNAKE_CASE		:						str			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    tokenizer.decode(greedy_ids[0])

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    TextIteratorStreamer(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    {"""input_ids""": input_ids, """max_new_tokens""": 1_0, """do_sample""": False, """streamer""": streamer}
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    Thread(target=model.generate      ,    kwargs=_A)
											thread.start()
											_SCREAMING_SNAKE_CASE		:						Any			  =    """"""
											for new_text in streamer:
																		streamer_text += new_text

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Dict			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    -1

											_SCREAMING_SNAKE_CASE		:						Any			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    greedy_ids[:, input_ids.shape[1] :]
											_SCREAMING_SNAKE_CASE		:						Dict			  =    tokenizer.decode(new_greedy_ids[0])

											with CaptureStdout() as cs:
																		_SCREAMING_SNAKE_CASE		:						Any			  =    TextStreamer(_A      ,    skip_prompt=_A)
																		model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A      ,    streamer=_A)
											# The greedy text should be printed to stdout, except for the final "\n" in the streamer
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    cs.out[:-1]

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    AutoTokenizer.from_pretrained("""distilgpt2""")
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoModelForCausalLM.from_pretrained("""distilgpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    -1

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.ones((1, 5)      ,    device=_A).long() * model.config.bos_token_id
											with CaptureStdout() as cs:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    TextStreamer(_A      ,    skip_special_tokens=_A)
																		model.generate(_A      ,    max_new_tokens=1      ,    do_sample=_A      ,    streamer=_A)

											# The prompt contains a special token, so the streamer should not print it. As such, the output text, when
											# re-tokenized, must only contain one token
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    cs.out[:-1]  # Remove the final "\n"
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    tokenizer(_A      ,    return_tensors="""pt""")
											self.assertEqual(streamer_text_tokenized.input_ids.shape      ,    (1, 1))





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    -1

											_SCREAMING_SNAKE_CASE		:						Dict			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    TextIteratorStreamer(_A      ,    timeout=0.001)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    {"""input_ids""": input_ids, """max_new_tokens""": 1_0, """do_sample""": False, """streamer""": streamer}
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    Thread(target=model.generate      ,    kwargs=_A)
											thread.start()

											# The streamer will timeout after 0.001 seconds, so an exception will be raised
											with self.assertRaises(_A):
																		_SCREAMING_SNAKE_CASE		:						str			  =    """"""
																		for new_text in streamer:
																									streamer_text += new_text


