

'''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 						__magic_name__   :







 def __init__(       self  , snake_case_  , snake_case_=13  , snake_case_=7  , snake_case_=True  , snake_case_=True  , snake_case_=False  , snake_case_=True  , snake_case_=99  , snake_case_=32  , snake_case_=5  , snake_case_=4  , snake_case_=37  , snake_case_="gelu"  , snake_case_=0.1  , snake_case_=0.1  , snake_case_=5_12  , snake_case_=16  , snake_case_=2  , snake_case_=0.02  , snake_case_=3  , snake_case_=4  , snake_case_=None  , ):
   lowercase									=parent
   lowercase									=batch_size
   lowercase									=seq_length
   lowercase									=is_training
   lowercase									=use_input_mask
   lowercase									=use_token_type_ids
   lowercase									=use_labels
   lowercase									=vocab_size
   lowercase									=hidden_size
   lowercase									=num_hidden_layers
   lowercase									=num_attention_heads
   lowercase									=intermediate_size
   lowercase									=hidden_act
   lowercase									=hidden_dropout_prob
   lowercase									=attention_probs_dropout_prob
   lowercase									=max_position_embeddings
   lowercase									=type_vocab_size
   lowercase									=type_sequence_label_size
   lowercase									=initializer_range
   lowercase									=num_labels
   lowercase									=num_choices
   lowercase									=scope







 def 						_A(       self   ):
   lowercase									=ids_tensor([self.batch_size, self.seq_length]  , self.vocab_size   )

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

   lowercase									=None
   if self.use_token_type_ids:
     lowercase									=ids_tensor([self.batch_size, self.seq_length]  , self.type_vocab_size   )

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

   lowercase									=self.get_config()

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







 def 						_A(       self   ):
   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=snake_case_  , initializer_range=self.initializer_range  , use_stable_embedding=snake_case_  , )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_   ):
   lowercase									=OpenLlamaModel(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_   )
   lowercase									=model(snake_case_   )
   self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , ):
   lowercase									=True
   lowercase									=OpenLlamaModel(snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(
       snake_case_  , attention_mask=snake_case_  , encoder_hidden_states=snake_case_  , encoder_attention_mask=snake_case_  , )
   lowercase									=model(
       snake_case_  , attention_mask=snake_case_  , encoder_hidden_states=snake_case_  , )
   lowercase									=model(snake_case_  , attention_mask=snake_case_   )
   self.parent.assertEqual(result.last_hidden_state.shape  , (self.batch_size, self.seq_length, self.hidden_size)   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , ):
   lowercase									=OpenLlamaForCausalLM(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , labels=snake_case_   )
   self.parent.assertEqual(result.logits.shape  , (self.batch_size, self.seq_length, self.vocab_size)   )







 def 						_A(       self  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , snake_case_  , ):
   lowercase									=True
   lowercase									=True
   lowercase									=OpenLlamaForCausalLM(config=snake_case_   )
   model.to(snake_case_   )
   model.eval()

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

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

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

   lowercase									=model(
       snake_case_  , attention_mask=snake_case_  , encoder_hidden_states=snake_case_  , encoder_attention_mask=snake_case_  , output_hidden_states=snake_case_  , )['''hidden_states'''][0]
   lowercase									=model(
       snake_case_  , attention_mask=snake_case_  , encoder_hidden_states=snake_case_  , encoder_attention_mask=snake_case_  , past_key_values=snake_case_  , output_hidden_states=snake_case_  , )['''hidden_states'''][0]

   # select random slice
   lowercase									=ids_tensor((1,)  , output_from_past.shape[-1]   ).item()
   lowercase									=output_from_no_past[:, -3:, random_slice_idx].detach()
   lowercase									=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(snake_case_  , snake_case_  , atol=1E-3   )   )







 def 						_A(       self   ):
   lowercase									=self.prepare_config_and_inputs()
   (
       (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, (
       lowercase
   )							, 
   )									=config_and_inputs
   lowercase									={'''input_ids''': input_ids, '''attention_mask''': input_mask}
   return config, inputs_dict

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







 def 						_A(       self   ):
   lowercase									=OpenLlamaModelTester(self   )
   lowercase									=ConfigTester(self  , config_class=snake_case_  , hidden_size=37   )







 def 						_A(       self   ):
   self.config_tester.run_common_tests()







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*snake_case_   )







 def 						_A(       self   ):
   lowercase									=self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
     lowercase									=type
     self.model_tester.create_and_check_model(*snake_case_   )







 def 						_A(       self   ):
   lowercase							, lowercase									=self.model_tester.prepare_config_and_inputs_for_common()
   lowercase									=3
   lowercase									=input_dict['''input_ids''']
   lowercase									=input_ids.ne(1   ).to(snake_case_   )
   lowercase									=ids_tensor([self.model_tester.batch_size]  , self.model_tester.type_sequence_label_size   )
   lowercase									=OpenLlamaForSequenceClassification(snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , labels=snake_case_   )
   self.assertEqual(result.logits.shape  , (self.model_tester.batch_size, self.model_tester.num_labels)   )







 def 						_A(       self   ):
   lowercase							, lowercase									=self.model_tester.prepare_config_and_inputs_for_common()
   lowercase									=3
   lowercase									='''single_label_classification'''
   lowercase									=input_dict['''input_ids''']
   lowercase									=input_ids.ne(1   ).to(snake_case_   )
   lowercase									=ids_tensor([self.model_tester.batch_size]  , self.model_tester.type_sequence_label_size   )
   lowercase									=OpenLlamaForSequenceClassification(snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , labels=snake_case_   )
   self.assertEqual(result.logits.shape  , (self.model_tester.batch_size, self.model_tester.num_labels)   )







 def 						_A(       self   ):
   lowercase							, lowercase									=self.model_tester.prepare_config_and_inputs_for_common()
   lowercase									=3
   lowercase									='''multi_label_classification'''
   lowercase									=input_dict['''input_ids''']
   lowercase									=input_ids.ne(1   ).to(snake_case_   )
   lowercase									=ids_tensor(
       [self.model_tester.batch_size, config.num_labels]  , self.model_tester.type_sequence_label_size   ).to(torch.float   )
   lowercase									=OpenLlamaForSequenceClassification(snake_case_   )
   model.to(snake_case_   )
   model.eval()
   lowercase									=model(snake_case_  , attention_mask=snake_case_  , labels=snake_case_   )
   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 						_A(       self   ):
   pass







 @parameterized.expand([('''linear''',), ('''dynamic''',)]   )
 def 						_A(       self  , snake_case_   ):
   lowercase							, lowercase									=self.model_tester.prepare_config_and_inputs_for_common()
   lowercase									=ids_tensor([1, 10]  , config.vocab_size   )
   lowercase									=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
   lowercase									=OpenLlamaModel(snake_case_   )
   original_model.to(snake_case_   )
   original_model.eval()
   lowercase									=original_model(snake_case_   ).last_hidden_state
   lowercase									=original_model(snake_case_   ).last_hidden_state

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

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








"""simple docstring"""







def      UpperCamelCase       ( _A )     ->       int:
				lowercase    :      Dict       = 0
				while num > 0:
								digit_sum += num % 10
								num //= 10
				return digit_sum


def      UpperCamelCase       ( _A = 100 )     ->       int:
				lowercase    :      Union[str, Any]       = 1
				lowercase    :      Tuple       = 2

				for i in range(2  ,				max_n + 1 ):
								lowercase    :      Any       = pre_numerator
								lowercase    :      Dict       = 2 * i // 3 if i % 3 == 0 else 1
								lowercase    :      Optional[Any]       = cur_numerator
								lowercase    :      str       = e_cont * pre_numerator + temp

				return sum_digits(_A )


if __name__ == "__main__":
				print(F'{solution() = }')
