import unittest

from transformers import AutoTokenizer, FalconConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          FalconForCausalLM,
          FalconForQuestionAnswering,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconModel,
      )


class   lowerCamelCase					:
  """simple docstring"""







  def __init__(							self :       str  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Union[str, Any]=3  ,     __magic_name__ :       int=7  ,     __magic_name__ :       Union[str, Any]=True  ,     __magic_name__ :       int=True  ,     __magic_name__ :       Optional[int]=False  ,     __magic_name__ :       Optional[Any]=True  ,     __magic_name__ :       Tuple=99  ,     __magic_name__ :       List[str]=32  ,     __magic_name__ :       Optional[int]=5  ,     __magic_name__ :       Optional[Any]=4  ,     __magic_name__ :       Any=37  ,     __magic_name__ :       List[Any]="gelu"  ,     __magic_name__ :       List[Any]=0.1  ,     __magic_name__ :       str=0.1  ,     __magic_name__ :       List[str]=512  ,     __magic_name__ :       Union[str, Any]=16  ,     __magic_name__ :       Optional[Any]=2  ,     __magic_name__ :       Optional[int]=0.02  ,     __magic_name__ :       List[Any]=3  ,     __magic_name__ :       Dict=4  ,     __magic_name__ :       Optional[Any]=None  ,     )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  parent
        SCREAMING_SNAKE_CASE_          =  batch_size
        SCREAMING_SNAKE_CASE_          =  seq_length
        SCREAMING_SNAKE_CASE_          =  is_training
        SCREAMING_SNAKE_CASE_          =  use_input_mask
        SCREAMING_SNAKE_CASE_          =  use_token_type_ids
        SCREAMING_SNAKE_CASE_          =  use_labels
        SCREAMING_SNAKE_CASE_          =  vocab_size
        SCREAMING_SNAKE_CASE_          =  hidden_size
        SCREAMING_SNAKE_CASE_          =  num_hidden_layers
        SCREAMING_SNAKE_CASE_          =  num_attention_heads
        SCREAMING_SNAKE_CASE_          =  intermediate_size
        SCREAMING_SNAKE_CASE_          =  hidden_act
        SCREAMING_SNAKE_CASE_          =  hidden_dropout_prob
        SCREAMING_SNAKE_CASE_          =  attention_probs_dropout_prob
        SCREAMING_SNAKE_CASE_          =  max_position_embeddings
        SCREAMING_SNAKE_CASE_          =  type_vocab_size
        SCREAMING_SNAKE_CASE_          =  type_sequence_label_size
        SCREAMING_SNAKE_CASE_          =  initializer_range
        SCREAMING_SNAKE_CASE_          =  num_labels
        SCREAMING_SNAKE_CASE_          =  num_choices
        SCREAMING_SNAKE_CASE_          =  scope







  def    __A     (							self :       List[str]   )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size   )

        SCREAMING_SNAKE_CASE_          =  None
        if self.use_input_mask:
              SCREAMING_SNAKE_CASE_          =  random_attention_mask([self.batch_size, self.seq_length]   )

        SCREAMING_SNAKE_CASE_          =  None

        SCREAMING_SNAKE_CASE_          =  None
        SCREAMING_SNAKE_CASE_          =  None
        SCREAMING_SNAKE_CASE_          =  None
        if self.use_labels:
              SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size]  ,     self.type_sequence_label_size   )
              SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels   )
              SCREAMING_SNAKE_CASE_          =  ids_tensor([self.batch_size]  ,     self.num_choices   )

        SCREAMING_SNAKE_CASE_          =  self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels







  def    __A     (							self :       str   )  ->	str:
        return FalconConfig(
            vocab_size=self.vocab_size  ,     hidden_size=self.hidden_size  ,     num_hidden_layers=self.num_hidden_layers  ,     num_attention_heads=self.num_attention_heads  ,     intermediate_size=self.intermediate_size  ,     hidden_act=self.hidden_act  ,     hidden_dropout_prob=self.hidden_dropout_prob  ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,     max_position_embeddings=self.max_position_embeddings  ,     type_vocab_size=self.type_vocab_size  ,     is_decoder=_lowerCamelCase  ,     initializer_range=self.initializer_range  ,     pad_token_id=1  ,     new_decoder_architecture=_lowerCamelCase  ,     )







  def    __A     (							self :       Any  ,     __magic_name__ :       str  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Any  ,     __magic_name__ :       int  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  FalconModel(config=_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase   )
        self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)   )







  def    __A     (							self :       Union[str, Any]  ,     __magic_name__ :       int  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       int  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Optional[int]  ,     )  ->	int:
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  FalconModel(_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(
            _lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     encoder_hidden_states=_lowerCamelCase  ,     encoder_attention_mask=_lowerCamelCase  ,     )
        SCREAMING_SNAKE_CASE_          =  model(
            _lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     encoder_hidden_states=_lowerCamelCase  ,     )
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase   )
        self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)   )







  def    __A     (							self :       List[str]  ,     __magic_name__ :       str  ,     __magic_name__ :       List[str]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Union[str, Any]  ,     __magic_name__ :       Optional[int]  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       Dict  ,     __magic_name__ :       Dict  ,     )  ->	Optional[int]:
        SCREAMING_SNAKE_CASE_          =  FalconForCausalLM(config=_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     labels=_lowerCamelCase   )
        self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)   )







  def    __A     (							self :       List[str]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       Optional[Any]  ,     __magic_name__ :       Tuple  ,     __magic_name__ :       int  ,     __magic_name__ :       List[Any]  ,     __magic_name__ :       int  ,     __magic_name__ :       Dict  ,     __magic_name__ :       int  ,     __magic_name__ :       Union[str, Any]  ,     )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  True
        SCREAMING_SNAKE_CASE_          =  FalconForCausalLM(config=_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()

        # first forward pass
        SCREAMING_SNAKE_CASE_          =  model(
            _lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     encoder_hidden_states=_lowerCamelCase  ,     encoder_attention_mask=_lowerCamelCase  ,     use_cache=_lowerCamelCase  ,     )
        SCREAMING_SNAKE_CASE_          =  outputs.past_key_values

        # create hypothetical multiple next token and extent to next_input_ids
        SCREAMING_SNAKE_CASE_          =  ids_tensor((self.batch_size, 3)  ,     config.vocab_size   )
        SCREAMING_SNAKE_CASE_          =  ids_tensor((self.batch_size, 3)  ,     vocab_size=2   )

        # append to next input_ids and
        SCREAMING_SNAKE_CASE_          =  torch.cat([input_ids, next_tokens]  ,     dim=-1   )
        SCREAMING_SNAKE_CASE_          =  torch.cat([input_mask, next_mask]  ,     dim=-1   )

        SCREAMING_SNAKE_CASE_          =  model(
            _lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     encoder_hidden_states=_lowerCamelCase  ,     encoder_attention_mask=_lowerCamelCase  ,     output_hidden_states=_lowerCamelCase  ,     )["hidden_states"][0]
        SCREAMING_SNAKE_CASE_          =  model(
            _lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     encoder_hidden_states=_lowerCamelCase  ,     encoder_attention_mask=_lowerCamelCase  ,     past_key_values=_lowerCamelCase  ,     output_hidden_states=_lowerCamelCase  ,     )["hidden_states"][0]

        # select random slice
        SCREAMING_SNAKE_CASE_          =  ids_tensor((1,)  ,     output_from_past.shape[-1]   ).item()
        SCREAMING_SNAKE_CASE_          =  output_from_no_past[:, -3:, random_slice_idx].detach()
        SCREAMING_SNAKE_CASE_          =  output_from_past[:, :, random_slice_idx].detach()

        self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

        # test that outputs are equal for slice
        self.parent.assertTrue(torch.allclose(_lowerCamelCase  ,     _lowerCamelCase  ,     atol=1e-3   )   )







  def    __A     (							self :       Optional[Any]   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  self.prepare_config_and_inputs()
        (
            (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , (
            SCREAMING_SNAKE_CASE_	
        )     , 	
        )          =  config_and_inputs
        SCREAMING_SNAKE_CASE_          =  {"input_ids": input_ids, "attention_mask": input_mask}
        return config, inputs_dict



@require_torch
class   lowerCamelCase					(__UpperCAmelCase  ,      __UpperCAmelCase  ,      __UpperCAmelCase  ,      unittest.TestCase					):
  """simple docstring"""







  lowerCamelCase__     =						(
      (
          FalconModel,
          FalconForCausalLM,
          FalconForSequenceClassification,
          FalconForTokenClassification,
          FalconForQuestionAnswering,
      )
      if is_torch_available()
      else ()
  )
  lowerCamelCase__     =						(FalconForCausalLM,) if is_torch_available() else ()
  lowerCamelCase__     =						(
      {
          "feature-extraction": FalconModel,
          "text-classification": FalconForSequenceClassification,
          "text-generation": FalconForCausalLM,
          "question-answering": FalconForQuestionAnswering,
          "token-classification": FalconForTokenClassification,
          "zero-shot": FalconForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  lowerCamelCase__     =						False
  lowerCamelCase__     =						False







  def    __A     (							self :       Optional[int]   )  ->	Any:
        SCREAMING_SNAKE_CASE_          =  FalconModelTester(self   )
        SCREAMING_SNAKE_CASE_          =  ConfigTester(self  ,     config_class=_lowerCamelCase  ,     hidden_size=37   )







  def    __A     (							self :       int   )  ->	List[Any]:
        self.config_tester.run_common_tests()







  def    __A     (							self :       Optional[Any]   )  ->	Tuple:
        SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_lowerCamelCase   )







  def    __A     (							self :       str   )  ->	int:
        SCREAMING_SNAKE_CASE_     , *SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs()
        for alibi in [True, False]:
              SCREAMING_SNAKE_CASE_          =  alibi
              self.model_tester.create_and_check_model(_lowerCamelCase  ,     *_lowerCamelCase   )







  def    __A     (							self :       List[str]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  3
        SCREAMING_SNAKE_CASE_          =  input_dict["input_ids"]
        SCREAMING_SNAKE_CASE_          =  input_ids.ne(1   ).to(_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.model_tester.batch_size]  ,     self.model_tester.type_sequence_label_size   )
        SCREAMING_SNAKE_CASE_          =  FalconForSequenceClassification(_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     labels=_lowerCamelCase   )
        self.assertEqual(result.logits.shape  ,     (self.model_tester.batch_size, self.model_tester.num_labels)   )







  def    __A     (							self :       Optional[Any]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  3
        SCREAMING_SNAKE_CASE_          =  "single_label_classification"
        SCREAMING_SNAKE_CASE_          =  input_dict["input_ids"]
        SCREAMING_SNAKE_CASE_          =  input_ids.ne(1   ).to(_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  ids_tensor([self.model_tester.batch_size]  ,     self.model_tester.type_sequence_label_size   )
        SCREAMING_SNAKE_CASE_          =  FalconForSequenceClassification(_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     labels=_lowerCamelCase   )
        self.assertEqual(result.logits.shape  ,     (self.model_tester.batch_size, self.model_tester.num_labels)   )







  def    __A     (							self :       Any   )  ->	int:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  input_dict["input_ids"]
        SCREAMING_SNAKE_CASE_          =  FalconForCausalLM(_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     use_cache=_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  input_ids.shape[0]
        SCREAMING_SNAKE_CASE_          =  model._convert_to_rw_cache(result.past_key_values   )
        SCREAMING_SNAKE_CASE_          =  model._convert_cache_to_standard_format(_lowerCamelCase  ,     _lowerCamelCase   )
        for layer in range(len(_lowerCamelCase   )   ):
              for tensor_idx in range(2   ):
                    self.assertTrue(rw_cache[layer][tensor_idx].ndim == 3   )
                    self.assertTrue(result.past_key_values[layer][tensor_idx].ndim == 4   )
                    self.assertTrue(
                        torch.all(result.past_key_values[layer][tensor_idx] == standard_cache[layer][tensor_idx]   )   )







  def    __A     (							self :       Any   )  ->	Any:
        SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()
        SCREAMING_SNAKE_CASE_          =  3
        SCREAMING_SNAKE_CASE_          =  "multi_label_classification"
        SCREAMING_SNAKE_CASE_          =  input_dict["input_ids"]
        SCREAMING_SNAKE_CASE_          =  input_ids.ne(1   ).to(_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  ids_tensor(
            [self.model_tester.batch_size, config.num_labels]  ,     self.model_tester.type_sequence_label_size   ).to(torch.float   )
        SCREAMING_SNAKE_CASE_          =  FalconForSequenceClassification(_lowerCamelCase   )
        model.to(_lowerCamelCase   )
        model.eval()
        SCREAMING_SNAKE_CASE_          =  model(_lowerCamelCase  ,     attention_mask=_lowerCamelCase  ,     labels=_lowerCamelCase   )
        self.assertEqual(result.logits.shape  ,     (self.model_tester.batch_size, self.model_tester.num_labels)   )







  def    __A     (							self :       int   )  ->	Optional[Any]:
        # Falcon can have different numbers of KV-heads than the number of query heads, so we need
        # to override this test to use the right head counts.
        for model_class in self.all_generative_model_classes:
              SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  self.model_tester.prepare_config_and_inputs_for_common()

              # If it doesn't support cache, pass the test
              if not hasattr(_lowerCamelCase  ,     "use_cache"   ):
                    return

              SCREAMING_SNAKE_CASE_          =  model_class(_lowerCamelCase   ).to(_lowerCamelCase   )
              if "use_cache" not in inputs:
                    SCREAMING_SNAKE_CASE_          =  True
              SCREAMING_SNAKE_CASE_          =  model(**_lowerCamelCase   )

              # If "past_key_values" is not returned, pass the test (e.g. RWKV uses a different cache name and format)
              if "past_key_values" not in outputs:
                    return

              SCREAMING_SNAKE_CASE_          =  (
                  getattr(_lowerCamelCase  ,     "decoder_layers"  ,     _lowerCamelCase   )
                  or getattr(_lowerCamelCase  ,     "num_decoder_layers"  ,     _lowerCamelCase   )
                  or config.num_hidden_layers
              )
              SCREAMING_SNAKE_CASE_          =  getattr(_lowerCamelCase  ,     "num_kv_heads"  ,     config.num_attention_heads   )
              SCREAMING_SNAKE_CASE_          =  getattr(_lowerCamelCase  ,     "d_model"  ,     config.hidden_size   )
              SCREAMING_SNAKE_CASE_          =  embed_dim // num_attention_heads

              SCREAMING_SNAKE_CASE_          =  outputs["past_key_values"]
              self.assertEqual(len(_lowerCamelCase   )  ,     _lowerCamelCase   )

              SCREAMING_SNAKE_CASE_     , SCREAMING_SNAKE_CASE_          =  inputs["input_ids"].shape
              for i in range(_lowerCamelCase   ):
                    if config.new_decoder_architecture:
                          SCREAMING_SNAKE_CASE_          =  config.num_attention_heads
                    elif config.multi_query:
                          SCREAMING_SNAKE_CASE_          =  1
                    self.assertEqual(len(past_kv[0]   )  ,     2   )  # K V for the decoder = 2
                    self.assertEqual(
                        past_kv[i][0].shape  ,     (batch_size, num_attention_heads, seq_length, per_head_embed_dim)   )
                    self.assertEqual(
                        past_kv[i][1].shape  ,     (batch_size, num_attention_heads, seq_length, per_head_embed_dim)   )



@require_torch
class   lowerCamelCase					(unittest.TestCase					):
  """simple docstring"""







  @slow
  def    __A     (							self :       List[str]   )  ->	List[Any]:
        SCREAMING_SNAKE_CASE_          =  AutoTokenizer.from_pretrained("Rocketknight1/falcon-rw-1b"   )
        SCREAMING_SNAKE_CASE_          =  FalconForCausalLM.from_pretrained("Rocketknight1/falcon-rw-1b"   )
        model.eval()
        model.to(_lowerCamelCase   )
        SCREAMING_SNAKE_CASE_          =  tokenizer("My favorite food is"  ,     return_tensors="pt"   ).to(_lowerCamelCase   )

        SCREAMING_SNAKE_CASE_          =  (
            "My favorite food is pizza. I love it so much that I have a pizza party every year for my birthday."
        )

        SCREAMING_SNAKE_CASE_          =  model.generate(**_lowerCamelCase  ,     do_sample=_lowerCamelCase  ,     max_new_tokens=19   )
        SCREAMING_SNAKE_CASE_          =  tokenizer.batch_decode(_lowerCamelCase   )[0]

        self.assertEqual(_lowerCamelCase  ,     _lowerCamelCase   )







  @slow
  def    __A     (							self :       Any   )  ->	Optional[Any]:
        # The big models are way too big for the CI, so we use tiny random models that resemble their
        # architectures but with much smaller and fewer layers
        for repo in ["Rocketknight1/tiny-random-falcon-7b", "Rocketknight1/tiny-random-falcon-40b"]:
              SCREAMING_SNAKE_CASE_          =  AutoTokenizer.from_pretrained(_lowerCamelCase   )
              SCREAMING_SNAKE_CASE_          =  FalconForCausalLM.from_pretrained(_lowerCamelCase   )
              model.eval()
              model.to(_lowerCamelCase   )
              SCREAMING_SNAKE_CASE_          =  tokenizer("My favorite food is"  ,     return_tensors="pt"   ).to(_lowerCamelCase   )

              # We just test that these run without errors - the models are randomly initialized
              # and so the actual text outputs will be garbage
              model.generate(**_lowerCamelCase  ,     do_sample=_lowerCamelCase  ,     max_new_tokens=4   )
              model.generate(**_lowerCamelCase  ,     do_sample=_lowerCamelCase  ,     max_new_tokens=4   )
              model.generate(**_lowerCamelCase  ,     num_beams=2  ,     max_new_tokens=4   )







  @slow
  def    __A     (							self :       Dict   )  ->	Tuple:
        # The big models are way too big for the CI, so we use tiny random models that resemble their
        # architectures but with much smaller and fewer layers
        with torch.no_grad():
              for repo in [
                  "Rocketknight1/falcon-rw-1b",
                  "Rocketknight1/tiny-random-falcon-7b",
                  "Rocketknight1/tiny-random-falcon-40b",
              ]:
                    SCREAMING_SNAKE_CASE_          =  AutoTokenizer.from_pretrained(_lowerCamelCase   )
                    SCREAMING_SNAKE_CASE_          =  FalconForCausalLM.from_pretrained(_lowerCamelCase   )
                    model.eval()
                    model.to(device=_lowerCamelCase   )
                    SCREAMING_SNAKE_CASE_          =  tokenizer("My favorite food is"  ,     return_tensors="pt"   ).to(_lowerCamelCase   )

                    # Test results are the same with and without cache
                    SCREAMING_SNAKE_CASE_          =  model.generate(**_lowerCamelCase  ,     do_sample=_lowerCamelCase  ,     max_new_tokens=20  ,     use_cache=_lowerCamelCase   )
                    SCREAMING_SNAKE_CASE_          =  model.generate(**_lowerCamelCase  ,     do_sample=_lowerCamelCase  ,     max_new_tokens=20  ,     use_cache=_lowerCamelCase   )
                    self.assertTrue((outputs_cache - outputs_no_cache).sum().item() == 0   )



from collections.abc import Callable


def      a__ (		__UpperCamelCase     ,      __UpperCamelCase     ,      __UpperCamelCase				):
      SCREAMING_SNAKE_CASE_          =  a
      SCREAMING_SNAKE_CASE_          =  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:
            SCREAMING_SNAKE_CASE_          =  start + (end - start) / 2.0
            while abs(start - mid				) > 1_0**-7:  # until precisely equals to 10^-7
                  if function(__UpperCamelCase				) == 0:
                        return mid
                  elif function(__UpperCamelCase				) * function(__UpperCamelCase				) < 0:
                        SCREAMING_SNAKE_CASE_          =  mid
                  else:
                        SCREAMING_SNAKE_CASE_          =  mid
                  SCREAMING_SNAKE_CASE_          =  start + (end - start) / 2.0
            return mid


def      a__ (		__UpperCamelCase				):
      return x**3 - 2 * x - 5


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

      import doctest

      doctest.testmod()


