

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__		:






  """simple docstring"""


  def __init__(							self						:		str     ,       __lowerCamelCase						:		str     ,       __lowerCamelCase						:		Optional[Any]=13     ,       __lowerCamelCase						:		Optional[Any]=7     ,       __lowerCamelCase						:		Union[str, Any]=True     ,       __lowerCamelCase						:		Tuple=True     ,       __lowerCamelCase						:		List[Any]=False     ,       __lowerCamelCase						:		Any=True     ,       __lowerCamelCase						:		Tuple=99     ,       __lowerCamelCase						:		Union[str, Any]=32     ,       __lowerCamelCase						:		Any=5     ,       __lowerCamelCase						:		Dict=4     ,       __lowerCamelCase						:		Optional[Any]=37     ,       __lowerCamelCase						:		Any="gelu"     ,       __lowerCamelCase						:		Tuple=0.1     ,       __lowerCamelCase						:		Union[str, Any]=0.1     ,       __lowerCamelCase						:		Any=512     ,       __lowerCamelCase						:		Any=16     ,       __lowerCamelCase						:		Optional[int]=2     ,       __lowerCamelCase						:		Any=0.02     ,       __lowerCamelCase						:		Any=3     ,       __lowerCamelCase						:		Optional[int]=4     ,       __lowerCamelCase						:		Any=None     ,       )		->							str:
       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 				lowercase_		(							self						:		Optional[int] )		->							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
       if self.use_token_type_ids:
            SCREAMING_SNAKE_CASE__   =   ids_tensor([self.batch_size, self.seq_length]     ,       self.type_vocab_size )

       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 				lowercase_		(							self						:		int )		->							Any:
       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=__lowerCamelCase     ,       initializer_range=self.initializer_range     ,       use_stable_embedding=__lowerCamelCase     ,       )






  def 				lowercase_		(							self						:		Union[str, Any]     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Union[str, Any]     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		Optional[int]     ,       __lowerCamelCase						:		List[Any]     ,       __lowerCamelCase						:		str )		->							Optional[int]:
       SCREAMING_SNAKE_CASE__   =   OpenLlamaModel(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 				lowercase_		(							self						:		Any     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		str     ,       __lowerCamelCase						:		Optional[int]     ,       )		->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__   =   True
       SCREAMING_SNAKE_CASE__   =   OpenLlamaModel(__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 				lowercase_		(							self						:		Union[str, Any]     ,       __lowerCamelCase						:		Tuple     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		Tuple     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		List[Any]     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Tuple     ,       __lowerCamelCase						:		Any     ,       )		->							Any:
       SCREAMING_SNAKE_CASE__   =   OpenLlamaForCausalLM(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 				lowercase_		(							self						:		List[str]     ,       __lowerCamelCase						:		Dict     ,       __lowerCamelCase						:		Optional[Any]     ,       __lowerCamelCase						:		Dict     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		List[Any]     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		Any     ,       )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   True
       SCREAMING_SNAKE_CASE__   =   True
       SCREAMING_SNAKE_CASE__   =   OpenLlamaForCausalLM(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 				lowercase_		(							self						:		Union[str, Any] )		->							List[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 	UpperCAmelCase__		(	A__						,					A__						,					A__						,					unittest.TestCase	):






  """simple docstring"""


  a									= (
      (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
  )
  a									= (OpenLlamaForCausalLM,) if is_torch_available() else ()
  a									= (
      {
          "feature-extraction": OpenLlamaModel,
          "text-classification": OpenLlamaForSequenceClassification,
          "text-generation": OpenLlamaForCausalLM,
          "zero-shot": OpenLlamaForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  a									= False
  a									= False






  def 				lowercase_		(							self						:		Optional[Any] )		->							Dict:
       SCREAMING_SNAKE_CASE__   =   OpenLlamaModelTester(self )
       SCREAMING_SNAKE_CASE__   =   ConfigTester(self     ,       config_class=__lowerCamelCase     ,       hidden_size=37 )






  def 				lowercase_		(							self						:		Dict )		->							List[Any]:
       self.config_tester.run_common_tests()






  def 				lowercase_		(							self						:		Tuple )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__lowerCamelCase )






  def 				lowercase_		(							self						:		Tuple )		->							List[Any]:
       SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            SCREAMING_SNAKE_CASE__   =   type
            self.model_tester.create_and_check_model(*__lowerCamelCase )






  def 				lowercase_		(							self						:		Tuple )		->							Optional[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__   =   OpenLlamaForSequenceClassification(__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 				lowercase_		(							self						:		Union[str, Any] )		->							int:
       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__   =   OpenLlamaForSequenceClassification(__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 				lowercase_		(							self						:		int )		->							int:
       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__   =   OpenLlamaForSequenceClassification(__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) )






  @unittest.skip('''Open-Llama buffers include complex numbers, which breaks this test''' )
  def 				lowercase_		(							self						:		List[Any] )		->							Optional[int]:
       pass







  @parameterized.expand([('''linear''',), ('''dynamic''',)] )
  def 				lowercase_		(							self						:		List[Any]     ,       __lowerCamelCase						:		Any )		->							Union[str, Any]:
       SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   self.model_tester.prepare_config_and_inputs_for_common()
       SCREAMING_SNAKE_CASE__   =   ids_tensor([1, 10]     ,       config.vocab_size )
       SCREAMING_SNAKE_CASE__   =   ids_tensor([1, int(config.max_position_embeddings * 1.5 )]     ,       config.vocab_size )

       set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
       SCREAMING_SNAKE_CASE__   =   OpenLlamaModel(__lowerCamelCase )
       original_model.to(__lowerCamelCase )
       original_model.eval()
       SCREAMING_SNAKE_CASE__   =   original_model(__lowerCamelCase ).last_hidden_state
       SCREAMING_SNAKE_CASE__   =   original_model(__lowerCamelCase ).last_hidden_state

       set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
       SCREAMING_SNAKE_CASE__   =   {'''type''': scaling_type, '''factor''': 10.0}
       SCREAMING_SNAKE_CASE__   =   OpenLlamaModel(__lowerCamelCase )
       scaled_model.to(__lowerCamelCase )
       scaled_model.eval()
       SCREAMING_SNAKE_CASE__   =   scaled_model(__lowerCamelCase ).last_hidden_state
       SCREAMING_SNAKE_CASE__   =   scaled_model(__lowerCamelCase ).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(__lowerCamelCase     ,       __lowerCamelCase     ,       atol=1e-5 ) )
       else:
            self.assertFalse(torch.allclose(__lowerCamelCase     ,       __lowerCamelCase     ,       atol=1e-5 ) )

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





from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt
def     UpperCAmelCase_       (						_A				,		_A				,		_A    ):





     '''simple docstring'''




     if not arr:
          return None, None, 0
     if low == high:
          return low, high, arr[low]

     SCREAMING_SNAKE_CASE__   =   (low + high) // 2
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_subarray(_A				,		_A				,		_A    )
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_subarray(_A				,		mid + 1				,		_A    )
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   max_cross_sum(_A				,		_A				,		_A				,		_A    )
     if left_sum >= right_sum and left_sum >= cross_sum:
          return left_low, left_high, left_sum
     elif right_sum >= left_sum and right_sum >= cross_sum:
          return right_low, right_high, right_sum
     return cross_left, cross_right, cross_sum
def     UpperCAmelCase_       (						_A				,		_A				,		_A				,		_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   float('''-inf'''    ), -1
     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   float('''-inf'''    ), -1

     SCREAMING_SNAKE_CASE__   =   0
     for i in range(_A				,		low - 1				,		-1    ):
          summ += arr[i]
          if summ > left_sum:
               SCREAMING_SNAKE_CASE__   =   summ
               SCREAMING_SNAKE_CASE__   =   i

     SCREAMING_SNAKE_CASE__   =   0
     for i in range(mid + 1				,		high + 1    ):
          summ += arr[i]
          if summ > right_sum:
               SCREAMING_SNAKE_CASE__   =   summ
               SCREAMING_SNAKE_CASE__   =   i

     return max_left, max_right, (left_sum + right_sum)
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   [randint(1				,		_A    ) for _ in range(_A    )]
     SCREAMING_SNAKE_CASE__   =   time.time()
     max_subarray(_A				,		0				,		input_size - 1    )
     SCREAMING_SNAKE_CASE__   =   time.time()
     return end - start
def     UpperCAmelCase_       (						):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   [10, 1_00, 10_00, 1_00_00, 5_00_00, 10_00_00, 20_00_00, 30_00_00, 40_00_00, 50_00_00]
     SCREAMING_SNAKE_CASE__   =   [time_max_subarray(_A    ) for input_size in input_sizes]
     print('''No of Inputs\t\tTime Taken'''    )
     for input_size, runtime in zip(_A				,		_A    ):
          print(_A				,		'''\t\t'''				,		_A    )
     plt.plot(_A				,		_A    )
     plt.xlabel('''Number of Inputs'''    )
     plt.ylabel('''Time taken in seconds'''    )
     plt.show()


if __name__ == "__main__":
       from doctest import testmod

       testmod()


