


'''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        a   :
 def __init__(       self					, __magic_name__					, __magic_name__=13					, __magic_name__=7					, __magic_name__=True					, __magic_name__=True					, __magic_name__=False					, __magic_name__=True					, __magic_name__=99					, __magic_name__=32					, __magic_name__=5					, __magic_name__=4					, __magic_name__=37					, __magic_name__="gelu"					, __magic_name__=0.1					, __magic_name__=0.1					, __magic_name__=5_12					, __magic_name__=16					, __magic_name__=2					, __magic_name__=0.0_2					, __magic_name__=3					, __magic_name__=4					, __magic_name__=None					, )    ->							Optional[Any]:
      _a						=							parent
      _a						=							batch_size
      _a						=							seq_length
      _a						=							is_training
      _a						=							use_input_mask
      _a						=							use_token_type_ids
      _a						=							use_labels
      _a						=							vocab_size
      _a						=							hidden_size
      _a						=							num_hidden_layers
      _a						=							num_attention_heads
      _a						=							intermediate_size
      _a						=							hidden_act
      _a						=							hidden_dropout_prob
      _a						=							attention_probs_dropout_prob
      _a						=							max_position_embeddings
      _a						=							type_vocab_size
      _a						=							type_sequence_label_size
      _a						=							initializer_range
      _a						=							num_labels
      _a						=							num_choices
      _a						=							scope
 def 							__UpperCAmelCase						(       self  )    ->							Optional[Any]:
      _a						=							ids_tensor([self.batch_size, self.seq_length]					, self.vocab_size  )

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

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

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

      _a						=							self.get_config()

      return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 							__UpperCAmelCase						(       self  )    ->							Optional[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=__magic_name__					, initializer_range=self.initializer_range					, use_stable_embedding=__magic_name__					, )
 def 							__UpperCAmelCase						(       self					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__  )    ->							int:
      _a						=							OpenLlamaModel(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(__magic_name__					, attention_mask=__magic_name__  )
      _a						=							model(__magic_name__  )
      self.parent.assertEqual(result.last_hidden_state.shape					, (self.batch_size, self.seq_length, self.hidden_size)  )
 def 							__UpperCAmelCase						(       self					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, )    ->							str:
      _a						=							True
      _a						=							OpenLlamaModel(__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(
          __magic_name__					, attention_mask=__magic_name__					, encoder_hidden_states=__magic_name__					, encoder_attention_mask=__magic_name__					, )
      _a						=							model(
          __magic_name__					, attention_mask=__magic_name__					, encoder_hidden_states=__magic_name__					, )
      _a						=							model(__magic_name__					, attention_mask=__magic_name__  )
      self.parent.assertEqual(result.last_hidden_state.shape					, (self.batch_size, self.seq_length, self.hidden_size)  )
 def 							__UpperCAmelCase						(       self					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, )    ->							int:
      _a						=							OpenLlamaForCausalLM(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(__magic_name__					, attention_mask=__magic_name__					, labels=__magic_name__  )
      self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.seq_length, self.vocab_size)  )
 def 							__UpperCAmelCase						(       self					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, __magic_name__					, )    ->							List[str]:
      _a						=							True
      _a						=							True
      _a						=							OpenLlamaForCausalLM(config=__magic_name__  )
      model.to(__magic_name__  )
      model.eval()

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

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

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

      _a						=							model(
          __magic_name__					, attention_mask=__magic_name__					, encoder_hidden_states=__magic_name__					, encoder_attention_mask=__magic_name__					, output_hidden_states=__magic_name__					, )["hidden_states"][0]
      _a						=							model(
          __magic_name__					, attention_mask=__magic_name__					, encoder_hidden_states=__magic_name__					, encoder_attention_mask=__magic_name__					, past_key_values=__magic_name__					, output_hidden_states=__magic_name__					, )["hidden_states"][0]

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




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





@require_torch
class        a   (   _lowerCAmelCase     , _lowerCAmelCase     , _lowerCAmelCase     , 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 							__UpperCAmelCase						(       self  )    ->							Tuple:
      _a						=							OpenLlamaModelTester(self  )
      _a						=							ConfigTester(self					, config_class=__magic_name__					, hidden_size=37  )
 def 							__UpperCAmelCase						(       self  )    ->							Optional[Any]:
      self.config_tester.run_common_tests()
 def 							__UpperCAmelCase						(       self  )    ->							List[str]:
      _a						=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__magic_name__  )
 def 							__UpperCAmelCase						(       self  )    ->							List[Any]:
      _a						=							self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
           _a						=							type
           self.model_tester.create_and_check_model(*__magic_name__  )
 def 							__UpperCAmelCase						(       self  )    ->							Tuple:
      _a						=							self.model_tester.prepare_config_and_inputs_for_common()
      _a						=							3
      _a						=							input_dict["input_ids"]
      _a						=							input_ids.ne(1  ).to(__magic_name__  )
      _a						=							ids_tensor([self.model_tester.batch_size]					, self.model_tester.type_sequence_label_size  )
      _a						=							OpenLlamaForSequenceClassification(__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(__magic_name__					, attention_mask=__magic_name__					, labels=__magic_name__  )
      self.assertEqual(result.logits.shape					, (self.model_tester.batch_size, self.model_tester.num_labels)  )
 def 							__UpperCAmelCase						(       self  )    ->							Dict:
      _a						=							self.model_tester.prepare_config_and_inputs_for_common()
      _a						=							3
      _a						=							"single_label_classification"
      _a						=							input_dict["input_ids"]
      _a						=							input_ids.ne(1  ).to(__magic_name__  )
      _a						=							ids_tensor([self.model_tester.batch_size]					, self.model_tester.type_sequence_label_size  )
      _a						=							OpenLlamaForSequenceClassification(__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(__magic_name__					, attention_mask=__magic_name__					, labels=__magic_name__  )
      self.assertEqual(result.logits.shape					, (self.model_tester.batch_size, self.model_tester.num_labels)  )
 def 							__UpperCAmelCase						(       self  )    ->							Optional[int]:
      _a						=							self.model_tester.prepare_config_and_inputs_for_common()
      _a						=							3
      _a						=							"multi_label_classification"
      _a						=							input_dict["input_ids"]
      _a						=							input_ids.ne(1  ).to(__magic_name__  )
      _a						=							ids_tensor(
          [self.model_tester.batch_size, config.num_labels]					, self.model_tester.type_sequence_label_size  ).to(torch.float  )
      _a						=							OpenLlamaForSequenceClassification(__magic_name__  )
      model.to(__magic_name__  )
      model.eval()
      _a						=							model(__magic_name__					, attention_mask=__magic_name__					, labels=__magic_name__  )
      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 							__UpperCAmelCase						(       self  )    ->							Optional[Any]:
      pass




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

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

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








'''simple docstring'''


lowerCAmelCase					:     Optional[int]												=256
# Modulus to hash a string
lowerCAmelCase					:     Tuple												=1_000_003
def 							UpperCAmelCase_  (						__lowerCamelCase					:					str	,__lowerCamelCase					:					str ):
   lowercase_			:Dict    			= len(__lowerCamelCase )
   lowercase_			:int    			= len(__lowerCamelCase )
   if p_len > t_len:
      return False

   lowercase_			:Any    			= 0
   lowercase_			:Dict    			= 0
   lowercase_			:int    			= 1

   # Calculating the hash of pattern and substring of text
   for i in range(__lowerCamelCase ):
      lowercase_			:Union[str, Any]    			= (ord(pattern[i] ) + p_hash * alphabet_size) % modulus
      lowercase_			:Tuple    			= (ord(text[i] ) + text_hash * alphabet_size) % modulus
      if i == p_len - 1:
         continue
      lowercase_			:Tuple    			= (modulus_power * alphabet_size) % modulus

   for i in range(0	,t_len - p_len + 1 ):
      if text_hash == p_hash and text[i : i + p_len] == pattern:
         return True
      if i == t_len - p_len:
         continue
      # Calculate the https://en.wikipedia.org/wiki/Rolling_hash
      lowercase_			:Tuple    			= (
          (text_hash - ord(text[i] ) * modulus_power) * alphabet_size
          + ord(text[i + p_len] )
      ) % modulus
   return False
def 							UpperCAmelCase_  (						):
   lowercase_			:List[Any]    			= "abc1abc12"
   lowercase_			:List[str]    			= "alskfjaldsabc1abc1abc12k23adsfabcabc"
   lowercase_			:int    			= "alskfjaldsk23adsfabcabc"
   assert rabin_karp(__lowerCamelCase	,__lowerCamelCase ) and not rabin_karp(__lowerCamelCase	,__lowerCamelCase )

   # Test 2)
   lowercase_			:Union[str, Any]    			= "ABABX"
   lowercase_			:Optional[Any]    			= "ABABZABABYABABX"
   assert rabin_karp(__lowerCamelCase	,__lowerCamelCase )

   # Test 3)
   lowercase_			:Dict    			= "AAAB"
   lowercase_			:int    			= "ABAAAAAB"
   assert rabin_karp(__lowerCamelCase	,__lowerCamelCase )

   # Test 4)
   lowercase_			:Tuple    			= "abcdabcy"
   lowercase_			:Union[str, Any]    			= "abcxabcdabxabcdabcdabcy"
   assert rabin_karp(__lowerCamelCase	,__lowerCamelCase )

   # Test 5)
   lowercase_			:Tuple    			= "Lü"
   lowercase_			:List[Any]    			= "Lüsai"
   assert rabin_karp(__lowerCamelCase	,__lowerCamelCase )
   lowercase_			:str    			= "Lue"
   assert not rabin_karp(__lowerCamelCase	,__lowerCamelCase )
   print("Success." )


if __name__ == "__main__":
 test_rabin_karp()


