
'''simple docstring'''






import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, 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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel

class  UpperCAmelCase__						:



    def __init__(	self      :       Dict,__A      :       Any,__A      :       str=1_3,__A      :       Optional[int]=7,__A      :       Tuple=True,__A      :       Optional[Any]=True,__A      :       Any=False,__A      :       List[Any]=True,__A      :       int=9_9,__A      :       List[str]=3_2,__A      :       Tuple=5,__A      :       Optional[Any]=4,__A      :       str=3_7,__A      :       Optional[Any]="gelu",__A      :       List[str]=0.1,__A      :       List[Any]=0.1,__A      :       str=5_1_2,__A      :       Optional[int]=1_6,__A      :       Dict=2,__A      :       Union[str, Any]=0.02,__A      :       Any=3,__A      :       Dict=4,__A      :       Any=None,):
          _lowerCamelCase				:   Dict								=   parent
          _lowerCamelCase				:   int								=   batch_size
          _lowerCamelCase				:   List[Any]								=   seq_length
          _lowerCamelCase				:   Optional[int]								=   is_training
          _lowerCamelCase				:   List[str]								=   use_input_mask
          _lowerCamelCase				:   Dict								=   use_token_type_ids
          _lowerCamelCase				:   int								=   use_labels
          _lowerCamelCase				:   Optional[int]								=   vocab_size
          _lowerCamelCase				:   str								=   hidden_size
          _lowerCamelCase				:   List[str]								=   num_hidden_layers
          _lowerCamelCase				:   Dict								=   num_attention_heads
          _lowerCamelCase				:   List[str]								=   intermediate_size
          _lowerCamelCase				:   str								=   hidden_act
          _lowerCamelCase				:   Optional[int]								=   hidden_dropout_prob
          _lowerCamelCase				:   str								=   attention_probs_dropout_prob
          _lowerCamelCase				:   str								=   max_position_embeddings
          _lowerCamelCase				:   List[str]								=   type_vocab_size
          _lowerCamelCase				:   List[Any]								=   type_sequence_label_size
          _lowerCamelCase				:   int								=   initializer_range
          _lowerCamelCase				:   Dict								=   num_labels
          _lowerCamelCase				:   str								=   num_choices
          _lowerCamelCase				:   Union[str, Any]								=   scope



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.batch_size, self.seq_length],self.vocab_size     )

          _lowerCamelCase				:   List[Any]								=   None
          if self.use_input_mask:
                _lowerCamelCase				:   int								=   random_attention_mask([self.batch_size, self.seq_length]     )

          _lowerCamelCase				:   Union[str, Any]								=   None
          if self.use_token_type_ids:
                _lowerCamelCase				:   Optional[Any]								=   ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size     )

          _lowerCamelCase				:   str								=   None
          _lowerCamelCase				:   Optional[int]								=   None
          _lowerCamelCase				:   Optional[int]								=   None
          if self.use_labels:
                _lowerCamelCase				:   Dict								=   ids_tensor([self.batch_size],self.type_sequence_label_size     )
                _lowerCamelCase				:   List[Any]								=   ids_tensor([self.batch_size, self.seq_length],self.num_labels     )
                _lowerCamelCase				:   Any								=   ids_tensor([self.batch_size],self.num_choices     )

          _lowerCamelCase				:   Optional[int]								=   self.get_config()

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



    def   lowerCamelCase_  (	self      :       int     ):
          return OpenLlamaConfig(
              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=__A,initializer_range=self.initializer_range,use_stable_embedding=__A,)



    def   lowerCamelCase_  (	self      :       str,__A      :       Tuple,__A      :       str,__A      :       Union[str, Any],__A      :       List[Any],__A      :       List[str],__A      :       Optional[int],__A      :       str     ):
          _lowerCamelCase				:   Union[str, Any]								=   OpenLlamaModel(config=__A     )
          model.to(__A     )
          model.eval()
          _lowerCamelCase				:   Any								=   model(__A,attention_mask=__A     )
          _lowerCamelCase				:   List[str]								=   model(__A     )
          self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size)     )



    def   lowerCamelCase_  (	self      :       List[Any],__A      :       Dict,__A      :       int,__A      :       Union[str, Any],__A      :       Any,__A      :       List[Any],__A      :       str,__A      :       Union[str, Any],__A      :       Tuple,__A      :       List[str],):
          _lowerCamelCase				:   Union[str, Any]								=   True
          _lowerCamelCase				:   str								=   OpenLlamaModel(__A     )
          model.to(__A     )
          model.eval()
          _lowerCamelCase				:   str								=   model(
              __A,attention_mask=__A,encoder_hidden_states=__A,encoder_attention_mask=__A,)
          _lowerCamelCase				:   str								=   model(
              __A,attention_mask=__A,encoder_hidden_states=__A,)
          _lowerCamelCase				:   Dict								=   model(__A,attention_mask=__A     )
          self.parent.assertEqual(result.last_hidden_state.shape,(self.batch_size, self.seq_length, self.hidden_size)     )



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



    def   lowerCamelCase_  (	self      :       Any,__A      :       List[str],__A      :       int,__A      :       int,__A      :       int,__A      :       Dict,__A      :       Optional[Any],__A      :       Optional[Any],__A      :       Union[str, Any],__A      :       List[str],):
          _lowerCamelCase				:   int								=   True
          _lowerCamelCase				:   Tuple								=   True
          _lowerCamelCase				:   List[str]								=   OpenLlamaForCausalLM(config=__A     )
          model.to(__A     )
          model.eval()

          # first forward pass
          _lowerCamelCase				:   Tuple								=   model(
              __A,attention_mask=__A,encoder_hidden_states=__A,encoder_attention_mask=__A,use_cache=__A,)
          _lowerCamelCase				:   List[Any]								=   outputs.past_key_values

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

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

          _lowerCamelCase				:   Any								=   model(
              __A,attention_mask=__A,encoder_hidden_states=__A,encoder_attention_mask=__A,output_hidden_states=__A,)["hidden_states"][0]
          _lowerCamelCase				:   Optional[int]								=   model(
              __A,attention_mask=__A,encoder_hidden_states=__A,encoder_attention_mask=__A,past_key_values=__A,output_hidden_states=__A,)["hidden_states"][0]

          # select random slice
          _lowerCamelCase				:   Dict								=   ids_tensor((1,),output_from_past.shape[-1]     ).item()
          _lowerCamelCase				:   Any								=   output_from_no_past[:, -3:, random_slice_idx].detach()
          _lowerCamelCase				:   str								=   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(__A,__A,atol=1e-3     )     )



    def   lowerCamelCase_  (	self      :       int     ):
          _lowerCamelCase				:   int								=   self.prepare_config_and_inputs()
          (
              (
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		(
              _lowerCamelCase
          )						,		
          )				:   Any								=   config_and_inputs
          _lowerCamelCase				:   Optional[Any]								=   {"input_ids": input_ids, "attention_mask": input_mask}
          return config, inputs_dict


@require_torch
class  UpperCAmelCase__						(       A						,	A						,	A						,	unittest.TestCase ):
    lowerCAmelCase_				    =		(
        (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
    )
    lowerCAmelCase_				    =		(OpenLlamaForCausalLM,) if is_torch_available() else ()
    lowerCAmelCase_				    =		(
        {
            'feature-extraction': OpenLlamaModel,
            'text-classification': OpenLlamaForSequenceClassification,
            'text-generation': OpenLlamaForCausalLM,
            'zero-shot': OpenLlamaForSequenceClassification,
        }
        if is_torch_available()
        else {}
    )
    lowerCAmelCase_				    =		False
    lowerCAmelCase_				    =		False



    def   lowerCamelCase_  (	self      :       int     ):
          _lowerCamelCase				:   List[str]								=   OpenLlamaModelTester(self     )
          _lowerCamelCase				:   Tuple								=   ConfigTester(self,config_class=__A,hidden_size=3_7     )



    def   lowerCamelCase_  (	self      :       Union[str, Any]     ):
          self.config_tester.run_common_tests()



    def   lowerCamelCase_  (	self      :       str     ):
          _lowerCamelCase				:   List[str]								=   self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*__A     )



    def   lowerCamelCase_  (	self      :       str     ):
          _lowerCamelCase				:   Any								=   self.model_tester.prepare_config_and_inputs()
          for type in ["absolute", "relative_key", "relative_key_query"]:
                _lowerCamelCase				:   str								=   type
                self.model_tester.create_and_check_model(*__A     )



    def   lowerCamelCase_  (	self      :       List[Any]     ):
          _lowerCamelCase						,		_lowerCamelCase				:   Any								=   self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase				:   Tuple								=   3
          _lowerCamelCase				:   Dict								=   input_dict["input_ids"]
          _lowerCamelCase				:   List[str]								=   input_ids.ne(1     ).to(__A     )
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.model_tester.batch_size],self.model_tester.type_sequence_label_size     )
          _lowerCamelCase				:   Union[str, Any]								=   OpenLlamaForSequenceClassification(__A     )
          model.to(__A     )
          model.eval()
          _lowerCamelCase				:   List[str]								=   model(__A,attention_mask=__A,labels=__A     )
          self.assertEqual(result.logits.shape,(self.model_tester.batch_size, self.model_tester.num_labels)     )



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



    def   lowerCamelCase_  (	self      :       Optional[int]     ):
          _lowerCamelCase						,		_lowerCamelCase				:   Union[str, Any]								=   self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase				:   Dict								=   3
          _lowerCamelCase				:   List[Any]								=   "multi_label_classification"
          _lowerCamelCase				:   Optional[Any]								=   input_dict["input_ids"]
          _lowerCamelCase				:   Tuple								=   input_ids.ne(1     ).to(__A     )
          _lowerCamelCase				:   str								=   ids_tensor(
              [self.model_tester.batch_size, config.num_labels],self.model_tester.type_sequence_label_size     ).to(torch.float     )
          _lowerCamelCase				:   Optional[int]								=   OpenLlamaForSequenceClassification(__A     )
          model.to(__A     )
          model.eval()
          _lowerCamelCase				:   Dict								=   model(__A,attention_mask=__A,labels=__A     )
          self.assertEqual(result.logits.shape,(self.model_tester.batch_size, self.model_tester.num_labels)     )



    @unittest.skip("Open-Llama buffers include complex numbers, which breaks this test"     )
    def   lowerCamelCase_  (	self      :       List[Any]     ):
          pass



    @parameterized.expand([("linear",), ("dynamic",)]     )
    def   lowerCamelCase_  (	self      :       Any,__A      :       Optional[int]     ):
          _lowerCamelCase						,		_lowerCamelCase				:   str								=   self.model_tester.prepare_config_and_inputs_for_common()
          _lowerCamelCase				:   Tuple								=   ids_tensor([1, 1_0],config.vocab_size     )
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([1, int(config.max_position_embeddings * 1.5     )],config.vocab_size     )

          set_seed(4_2     )  # Fixed seed at init time so the two models get the same random weights
          _lowerCamelCase				:   Any								=   OpenLlamaModel(__A     )
          original_model.to(__A     )
          original_model.eval()
          _lowerCamelCase				:   Optional[Any]								=   original_model(__A     ).last_hidden_state
          _lowerCamelCase				:   Dict								=   original_model(__A     ).last_hidden_state

          set_seed(4_2     )  # Fixed seed at init time so the two models get the same random weights
          _lowerCamelCase				:   Tuple								=   {"type": scaling_type, "factor": 10.0}
          _lowerCamelCase				:   Optional[Any]								=   OpenLlamaModel(__A     )
          scaled_model.to(__A     )
          scaled_model.eval()
          _lowerCamelCase				:   List[str]								=   scaled_model(__A     ).last_hidden_state
          _lowerCamelCase				:   Optional[int]								=   scaled_model(__A     ).last_hidden_state

          # Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
          # maximum sequence length, so the outputs for the short input should match.
          if scaling_type == "dynamic":
                self.assertTrue(torch.allclose(__A,__A,atol=1e-5     )     )
          else:
                self.assertFalse(torch.allclose(__A,__A,atol=1e-5     )     )

          # The output should be different for long inputs
          self.assertFalse(torch.allclose(__A,__A,atol=1e-5     )     )

'''simple docstring'''






def 		A_       (	_lowerCAmelCase   :    float							):



      """simple docstring"""




      return 10 - x * x







def 		A_       (	_lowerCAmelCase   :    float				,	_lowerCAmelCase   :    float							):



      """simple docstring"""




      if equation(_lowerCAmelCase							) * equation(_lowerCAmelCase							) >= 0:
            raise ValueError("Wrong space!"							)

      _lowerCamelCase				:   List[str]								=   a
      while (b - a) >= 0.0_1:
            # Find middle point
            _lowerCamelCase				:   Union[str, Any]								=   (a + b) / 2
            # Check if middle point is root
            if equation(_lowerCAmelCase							) == 0.0:
                  break
            # Decide the side to repeat the steps
            if equation(_lowerCAmelCase							) * equation(_lowerCAmelCase							) < 0:
                  _lowerCamelCase				:   Union[str, Any]								=   c
            else:
                  _lowerCamelCase				:   Any								=   c
      return c


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 print(bisection(-2, 5))
 print(bisection(0, 6))