




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







							def      __lowerCAmelCase    (						self :						Union[str, Any]      )    ->							Union[str, Any]:


														"""simple docstring"""


														_a			  =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      )
														_a			  =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      ).to(lowerCAmelCase_      )
														_a			  =       -1

														_a			  =       ids_tensor((1, 5)							,    vocab_size=model.config.vocab_size      ).to(lowerCAmelCase_      )
														_a			  =       model.generate(lowerCAmelCase_							,    max_new_tokens=10							,    do_sample=lowerCAmelCase_      )
														_a			  =       tokenizer.decode(greedy_ids[0]      )

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

														self.assertEqual(lowerCAmelCase_							,    lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						Optional[Any]      )    ->							int:


														"""simple docstring"""


														_a			  =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      )
														_a			  =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      ).to(lowerCAmelCase_      )
														_a			  =       -1

														_a			  =       ids_tensor((1, 5)							,    vocab_size=model.config.vocab_size      ).to(lowerCAmelCase_      )
														_a			  =       model.generate(lowerCAmelCase_							,    max_new_tokens=10							,    do_sample=lowerCAmelCase_      )
														_a			  =       tokenizer.decode(greedy_ids[0]      )

														_a			  =       TextIteratorStreamer(lowerCAmelCase_      )
														_a			  =       {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer}
														_a			  =       Thread(target=model.generate							,    kwargs=lowerCAmelCase_      )
														thread.start()
														_a			  =       ''''''
														for new_text in streamer:
																					streamer_text += new_text

														self.assertEqual(lowerCAmelCase_							,    lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						int      )    ->							Optional[int]:


														"""simple docstring"""


														_a			  =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      )
														_a			  =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      ).to(lowerCAmelCase_      )
														_a			  =       -1

														_a			  =       ids_tensor((1, 5)							,    vocab_size=model.config.vocab_size      ).to(lowerCAmelCase_      )
														_a			  =       model.generate(lowerCAmelCase_							,    max_new_tokens=10							,    do_sample=lowerCAmelCase_      )
														_a			  =       greedy_ids[:, input_ids.shape[1] :]
														_a			  =       tokenizer.decode(new_greedy_ids[0]      )

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

														self.assertEqual(lowerCAmelCase_							,    lowerCAmelCase_      )







							def      __lowerCAmelCase    (						self :						Optional[Any]      )    ->							int:


														"""simple docstring"""


														_a			  =       AutoTokenizer.from_pretrained('''distilgpt2'''      )
														_a			  =       AutoModelForCausalLM.from_pretrained('''distilgpt2'''      ).to(lowerCAmelCase_      )
														_a			  =       -1

														_a			  =       torch.ones((1, 5)							,    device=lowerCAmelCase_      ).long() * model.config.bos_token_id
														with CaptureStdout() as cs:
																					_a			  =       TextStreamer(lowerCAmelCase_							,    skip_special_tokens=lowerCAmelCase_      )
																					model.generate(lowerCAmelCase_							,    max_new_tokens=1							,    do_sample=lowerCAmelCase_							,    streamer=lowerCAmelCase_      )

														# 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
														_a			  =       cs.out[:-1]  # Remove the final "\n"
														_a			  =       tokenizer(lowerCAmelCase_							,    return_tensors='''pt'''      )
														self.assertEqual(streamer_text_tokenized.input_ids.shape							,    (1, 1)      )







							def      __lowerCAmelCase    (						self :						str      )    ->							Optional[int]:


														"""simple docstring"""


														_a			  =       AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      )
														_a			  =       AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''      ).to(lowerCAmelCase_      )
														_a			  =       -1

														_a			  =       ids_tensor((1, 5)							,    vocab_size=model.config.vocab_size      ).to(lowerCAmelCase_      )
														_a			  =       TextIteratorStreamer(lowerCAmelCase_							,    timeout=0.0_0_1      )
														_a			  =       {'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer}
														_a			  =       Thread(target=model.generate							,    kwargs=lowerCAmelCase_      )
														thread.start()

														# The streamer will timeout after 0.001 seconds, so an exception will be raised
														with self.assertRaises(lowerCAmelCase_      ):
																					_a			  =       ''''''
																					for new_text in streamer:
																												streamer_text += new_text












'''simple docstring'''


import os
import sys
from contextlib import contextmanager


# Windows only
if os.name == "nt":
				import ctypes
				import msvcrt  # noqa

				class     A					(	ctypes.Structure					):
											# _fields is a specific attr expected by ctypes
											lowercase_           =       [('size', ctypes.c_int), ('visible', ctypes.c_byte)]
def 	snake_case_					():





							'''simple docstring'''


							if os.name == "nt":
														_a			  =       CursorInfo()
														_a			  =       ctypes.windll.kernelaa.GetStdHandle(-11   )
														ctypes.windll.kernelaa.GetConsoleCursorInfo(UpperCamelCase				,		ctypes.byref(UpperCamelCase   )   )
														_a			  =       False
														ctypes.windll.kernelaa.SetConsoleCursorInfo(UpperCamelCase				,		ctypes.byref(UpperCamelCase   )   )
							elif os.name == "posix":
														sys.stdout.write('''\033[?25l'''   )
														sys.stdout.flush()
def 	snake_case_					():





							'''simple docstring'''


							if os.name == "nt":
														_a			  =       CursorInfo()
														_a			  =       ctypes.windll.kernelaa.GetStdHandle(-11   )
														ctypes.windll.kernelaa.GetConsoleCursorInfo(UpperCamelCase				,		ctypes.byref(UpperCamelCase   )   )
														_a			  =       True
														ctypes.windll.kernelaa.SetConsoleCursorInfo(UpperCamelCase				,		ctypes.byref(UpperCamelCase   )   )
							elif os.name == "posix":
														sys.stdout.write('''\033[?25h'''   )
														sys.stdout.flush()

@contextmanager
def 	snake_case_					():





							'''simple docstring'''


							try:
														hide_cursor()
														yield
							finally:
														show_cursor()

