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




  '''simple docstring'''



  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->List[Any]:
        UpperCAmelCase_            =  AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  )
        UpperCAmelCase_            =  AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  ).to(UpperCAmelCase__  )
        UpperCAmelCase_            =  -1

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

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

        self.assertEqual(UpperCAmelCase__  ,							UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Tuple  )				->int:
        UpperCAmelCase_            =  AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  )
        UpperCAmelCase_            =  AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  ).to(UpperCAmelCase__  )
        UpperCAmelCase_            =  -1

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

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

        self.assertEqual(UpperCAmelCase__  ,							UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       int  )				->Any:
        UpperCAmelCase_            =  AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  )
        UpperCAmelCase_            =  AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  ).to(UpperCAmelCase__  )
        UpperCAmelCase_            =  -1

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

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

        self.assertEqual(UpperCAmelCase__  ,							UpperCAmelCase__  )






  def  lowerCAmelCase__ (      self			:       Union[str, Any]  )				->List[Any]:
        # Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested
        # with actual models -- the dummy models' tokenizers are not aligned with their models, and
        # `skip_special_tokens=True` has no effect on them
        UpperCAmelCase_            =  AutoTokenizer.from_pretrained('''distilgpt2'''  )
        UpperCAmelCase_            =  AutoModelForCausalLM.from_pretrained('''distilgpt2'''  ).to(UpperCAmelCase__  )
        UpperCAmelCase_            =  -1

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

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






  def  lowerCAmelCase__ (      self			:       List[str]  )				->str:
        UpperCAmelCase_            =  AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  )
        UpperCAmelCase_            =  AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''  ).to(UpperCAmelCase__  )
        UpperCAmelCase_            =  -1

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

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






'''simple docstring'''
from collections.abc import Callable



def  __lowerCamelCase       (      _UpperCamelCase  :  Callable[[float], float]							,    _UpperCamelCase  :  float							,    _UpperCamelCase  :  float					):


      '''simple docstring'''


      UpperCAmelCase_            =  a
      UpperCAmelCase_            =  b
      if function(_UpperCamelCase					) == 0:  # one of the a or b is a root for the function
            return a
      elif function(_UpperCamelCase					) == 0:
            return b
      elif (
          function(_UpperCamelCase					) * function(_UpperCamelCase					) > 0
      ):  # if none of these are root and they are both positive or negative,
            # then this algorithm can't find the root
            raise ValueError('''could not find root in given interval.'''					)
      else:
            UpperCAmelCase_            =  start + (end - start) / 2.0
            while abs(start - mid					) > 10**-7:  # until precisely equals to 10^-7
                  if function(_UpperCamelCase					) == 0:
                        return mid
                  elif function(_UpperCamelCase					) * function(_UpperCamelCase					) < 0:
                        UpperCAmelCase_            =  mid
                  else:
                        UpperCAmelCase_            =  mid
                  UpperCAmelCase_            =  start + (end - start) / 2.0
            return mid



def  __lowerCamelCase       (      _UpperCamelCase  :  float					):


      '''simple docstring'''


      return x**3 - 2 * x - 5


if __name__ == "__main__":
      print(bisection(f, 1, 1000))

      import doctest

      doctest.testmod()





