



"""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 :					List[str]		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""		)
     SCREAMING_SNAKE_CASE_: List[str]     			=AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=-1

     SCREAMING_SNAKE_CASE_: List[str]     			=ids_tensor((1, 5)       ,		vocab_size=model.config.vocab_size		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[str]     			=model.generate(lowerCAmelCase       ,		max_new_tokens=10       ,		do_sample=lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[str]     			=tokenizer.decode(greedy_ids[0]		)

     with CaptureStdout() as cs:
        SCREAMING_SNAKE_CASE_: Optional[Any]     			=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
     SCREAMING_SNAKE_CASE_: Optional[int]     			=cs.out[:-1]

     self.assertEqual(lowerCAmelCase       ,		lowerCAmelCase		)



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: str     			=AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""		)
     SCREAMING_SNAKE_CASE_: Dict     			=AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2"""		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Dict     			=-1

     SCREAMING_SNAKE_CASE_: str     			=ids_tensor((1, 5)       ,		vocab_size=model.config.vocab_size		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[Any]     			=model.generate(lowerCAmelCase       ,		max_new_tokens=10       ,		do_sample=lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[Any]     			=tokenizer.decode(greedy_ids[0]		)

     SCREAMING_SNAKE_CASE_: Optional[Any]     			=TextIteratorStreamer(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[Any]     			={"""input_ids""": input_ids, """max_new_tokens""": 10, """do_sample""": False, """streamer""": streamer}
     SCREAMING_SNAKE_CASE_: Dict     			=Thread(target=model.generate       ,		kwargs=lowerCAmelCase		)
     thread.start()
     SCREAMING_SNAKE_CASE_: Dict     			=""""""
     for new_text in streamer:
        streamer_text += new_text

     self.assertEqual(lowerCAmelCase       ,		lowerCAmelCase		)



  def 	lowerCamelCase__       (						self :					str		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[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(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Any     			=-1

     SCREAMING_SNAKE_CASE_: str     			=ids_tensor((1, 5)       ,		vocab_size=model.config.vocab_size		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=model.generate(lowerCAmelCase       ,		max_new_tokens=10       ,		do_sample=lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=greedy_ids[:, input_ids.shape[1] :]
     SCREAMING_SNAKE_CASE_: Any     			=tokenizer.decode(new_greedy_ids[0]		)

     with CaptureStdout() as cs:
        SCREAMING_SNAKE_CASE_: Tuple     			=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
     SCREAMING_SNAKE_CASE_: Any     			=cs.out[:-1]

     self.assertEqual(lowerCAmelCase       ,		lowerCAmelCase		)



  def 	lowerCamelCase__       (						self :					Any		)     ->					Dict:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Dict     			=AutoTokenizer.from_pretrained("""distilgpt2"""		)
     SCREAMING_SNAKE_CASE_: int     			=AutoModelForCausalLM.from_pretrained("""distilgpt2"""		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: List[str]     			=-1

     SCREAMING_SNAKE_CASE_: Optional[int]     			=torch.ones((1, 5)       ,		device=lowerCAmelCase		).long() * model.config.bos_token_id
     with CaptureStdout() as cs:
        SCREAMING_SNAKE_CASE_: Optional[int]     			=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
     SCREAMING_SNAKE_CASE_: int     			=cs.out[:-1]  # Remove the final "\n"
     SCREAMING_SNAKE_CASE_: List[str]     			=tokenizer(lowerCAmelCase       ,		return_tensors="""pt"""		)
     self.assertEqual(streamer_text_tokenized.input_ids.shape       ,		(1, 1)		)





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


     '''simple docstring'''




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

     SCREAMING_SNAKE_CASE_: List[Any]     			=ids_tensor((1, 5)       ,		vocab_size=model.config.vocab_size		).to(lowerCAmelCase		)
     SCREAMING_SNAKE_CASE_: Any     			=TextIteratorStreamer(lowerCAmelCase       ,		timeout=0.0_0_1		)
     SCREAMING_SNAKE_CASE_: Optional[Any]     			={"""input_ids""": input_ids, """max_new_tokens""": 10, """do_sample""": False, """streamer""": streamer}
     SCREAMING_SNAKE_CASE_: List[str]     			=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		):
        SCREAMING_SNAKE_CASE_: Tuple     			=""""""
        for new_text in streamer:
           streamer_text += new_text








"""simple docstring"""
def      __magic_name__				(							lowercase							,			lowercase ):
   return int((input_a, input_a).count(0 ) == 0 )





def      __magic_name__				(							):
   assert and_gate(0							,			0 ) == 0
   assert and_gate(0							,			1 ) == 0
   assert and_gate(1							,			0 ) == 0
   assert and_gate(1							,			1 ) == 1


if __name__ == "__main__":
   test_and_gate()
   print(and_gate(1, 0))
   print(and_gate(0, 0))
   print(and_gate(0, 1))
   print(and_gate(1, 1))



