



"""simple docstring"""




import unittest

from parameterized import parameterized

from transformers import LlamaConfig, is_torch_available, set_seed
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 LlamaForCausalLM, LlamaForSequenceClassification, LlamaModel, LlamaTokenizer






class      UpperCAmelCase_  :



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



      def 						snake_case_ (					self					:     str						):
           _UpperCAmelCase      :     Tuple        =   ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size						)

           _UpperCAmelCase      :     Optional[Any]        =   None
           if self.use_input_mask:
                _UpperCAmelCase      :     List[str]        =   random_attention_mask([self.batch_size, self.seq_length]						)

           _UpperCAmelCase      :     Optional[int]        =   None
           if self.use_token_type_ids:
                _UpperCAmelCase      :     str        =   ids_tensor([self.batch_size, self.seq_length]					,     self.type_vocab_size						)

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

           _UpperCAmelCase      :     Tuple        =   self.get_config()

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



      def 						snake_case_ (					self					:     Optional[int]						):
           return LlamaConfig(
               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					,     )



      def 						snake_case_ (					self					:     Optional[Any]					,     A					:     int					,     A					:     Tuple					,     A					:     Union[str, Any]					,     A					:     List[str]					,     A					:     int					,     A					:     str					,     A					:     Optional[Any]						):
           _UpperCAmelCase      :     Tuple        =   LlamaModel(config=A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     Any        =   model(A					,     attention_mask=A						)
           _UpperCAmelCase      :     int        =   model(A						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)



      def 						snake_case_ (					self					:     Tuple					,     A					:     Optional[Any]					,     A					:     Tuple					,     A					:     Optional[Any]					,     A					:     List[str]					,     A					:     Union[str, Any]					,     A					:     Optional[Any]					,     A					:     Union[str, Any]					,     A					:     Union[str, Any]					,     A					:     Any					,     ):
           _UpperCAmelCase      :     int        =   True
           _UpperCAmelCase      :     int        =   LlamaModel(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     Tuple        =   model(
               A					,     attention_mask=A					,     encoder_hidden_states=A					,     encoder_attention_mask=A					,     )
           _UpperCAmelCase      :     int        =   model(
               A					,     attention_mask=A					,     encoder_hidden_states=A					,     )
           _UpperCAmelCase      :     Optional[Any]        =   model(A					,     attention_mask=A						)
           self.parent.assertEqual(result.last_hidden_state.shape					,     (self.batch_size, self.seq_length, self.hidden_size)						)



      def 						snake_case_ (					self					:     List[str]					,     A					:     Tuple					,     A					:     Any					,     A					:     int					,     A					:     Optional[Any]					,     A					:     Tuple					,     A					:     Any					,     A					:     List[str]					,     A					:     Any					,     A					:     Dict					,     ):
           _UpperCAmelCase      :     Any        =   LlamaForCausalLM(config=A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     str        =   model(A					,     attention_mask=A					,     labels=A						)
           self.parent.assertEqual(result.logits.shape					,     (self.batch_size, self.seq_length, self.vocab_size)						)



      def 						snake_case_ (					self					:     Any					,     A					:     Tuple					,     A					:     str					,     A					:     List[str]					,     A					:     Optional[Any]					,     A					:     Any					,     A					:     Dict					,     A					:     Dict					,     A					:     List[str]					,     A					:     Optional[Any]					,     ):
           _UpperCAmelCase      :     Optional[int]        =   True
           _UpperCAmelCase      :     Tuple        =   True
           _UpperCAmelCase      :     Optional[Any]        =   LlamaForCausalLM(config=A						)
           model.to(A						)
           model.eval()

           # first forward pass
           _UpperCAmelCase      :     str        =   model(
               A					,     attention_mask=A					,     encoder_hidden_states=A					,     encoder_attention_mask=A					,     use_cache=A					,     )
           _UpperCAmelCase      :     Union[str, Any]        =   outputs.past_key_values

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

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

           _UpperCAmelCase      :     Dict        =   model(
               A					,     attention_mask=A					,     encoder_hidden_states=A					,     encoder_attention_mask=A					,     output_hidden_states=A					,     )["hidden_states"][0]
           _UpperCAmelCase      :     str        =   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
           _UpperCAmelCase      :     Union[str, Any]        =   ids_tensor((1,)					,     output_from_past.shape[-1]						).item()
           _UpperCAmelCase      :     Dict        =   output_from_no_past[:, -3:, random_slice_idx].detach()
           _UpperCAmelCase      :     Union[str, Any]        =   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 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Any        =   self.prepare_config_and_inputs()
           (
               (
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	(
               _UpperCAmelCase
           )    ,	
           )      :     List[str]        =   config_and_inputs
           _UpperCAmelCase      :     Optional[int]        =   {"input_ids": input_ids, "attention_mask": input_mask}
           return config, inputs_dict







@require_torch
class      UpperCAmelCase_  (    _UpperCamelCase	,      _UpperCamelCase	,      _UpperCamelCase	,      unittest.TestCase						):
      __SCREAMING_SNAKE_CASE						:       str								=			(LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else ()
      __SCREAMING_SNAKE_CASE						:       Optional[int]								=			(LlamaForCausalLM,) if is_torch_available() else ()
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			(
          {
              'feature-extraction': LlamaModel,
              'text-classification': LlamaForSequenceClassification,
              'text-generation': LlamaForCausalLM,
              'zero-shot': LlamaForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )
      __SCREAMING_SNAKE_CASE						:       Dict								=			False
      __SCREAMING_SNAKE_CASE						:       int								=			False



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     str        =   LlamaModelTester(self						)
           _UpperCAmelCase      :     Optional[Any]        =   ConfigTester(self					,     config_class=A					,     hidden_size=3_7						)



      def 						snake_case_ (					self					:     Optional[int]						):
           self.config_tester.run_common_tests()



      def 						snake_case_ (					self					:     Tuple						):
           _UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*A						)



      def 						snake_case_ (					self					:     Any						):
           _UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                _UpperCAmelCase      :     List[Any]        =   type
                self.model_tester.create_and_check_model(*A						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     Dict        =   self.model_tester.prepare_config_and_inputs_for_common()
           _UpperCAmelCase      :     str        =   3
           _UpperCAmelCase      :     Any        =   input_dict["input_ids"]
           _UpperCAmelCase      :     List[Any]        =   input_ids.ne(1						).to(A						)
           _UpperCAmelCase      :     Optional[int]        =   ids_tensor([self.model_tester.batch_size]					,     self.model_tester.type_sequence_label_size						)
           _UpperCAmelCase      :     Any        =   LlamaForSequenceClassification(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     List[Any]        =   model(A					,     attention_mask=A					,     labels=A						)
           self.assertEqual(result.logits.shape					,     (self.model_tester.batch_size, self.model_tester.num_labels)						)



      def 						snake_case_ (					self					:     Tuple						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     Optional[Any]        =   self.model_tester.prepare_config_and_inputs_for_common()
           _UpperCAmelCase      :     List[Any]        =   3
           _UpperCAmelCase      :     List[str]        =   "single_label_classification"
           _UpperCAmelCase      :     Optional[Any]        =   input_dict["input_ids"]
           _UpperCAmelCase      :     Any        =   input_ids.ne(1						).to(A						)
           _UpperCAmelCase      :     Union[str, Any]        =   ids_tensor([self.model_tester.batch_size]					,     self.model_tester.type_sequence_label_size						)
           _UpperCAmelCase      :     Tuple        =   LlamaForSequenceClassification(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     int        =   model(A					,     attention_mask=A					,     labels=A						)
           self.assertEqual(result.logits.shape					,     (self.model_tester.batch_size, self.model_tester.num_labels)						)



      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     Any        =   self.model_tester.prepare_config_and_inputs_for_common()
           _UpperCAmelCase      :     List[str]        =   3
           _UpperCAmelCase      :     List[Any]        =   "multi_label_classification"
           _UpperCAmelCase      :     Union[str, Any]        =   input_dict["input_ids"]
           _UpperCAmelCase      :     Dict        =   input_ids.ne(1						).to(A						)
           _UpperCAmelCase      :     List[Any]        =   ids_tensor(
               [self.model_tester.batch_size, config.num_labels]					,     self.model_tester.type_sequence_label_size						).to(torch.float						)
           _UpperCAmelCase      :     str        =   LlamaForSequenceClassification(A						)
           model.to(A						)
           model.eval()
           _UpperCAmelCase      :     List[Any]        =   model(A					,     attention_mask=A					,     labels=A						)
           self.assertEqual(result.logits.shape					,     (self.model_tester.batch_size, self.model_tester.num_labels)						)



      @unittest.skip("LLaMA buffers include complex numbers, which breaks this test"						)
      def 						snake_case_ (					self					:     str						):
           pass



      @parameterized.expand([("linear",), ("dynamic",)]						)
      def 						snake_case_ (					self					:     Tuple					,     A					:     Tuple						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     List[str]        =   self.model_tester.prepare_config_and_inputs_for_common()
           _UpperCAmelCase      :     str        =   ids_tensor([1, 1_0]					,     config.vocab_size						)
           _UpperCAmelCase      :     Optional[Any]        =   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
           _UpperCAmelCase      :     Union[str, Any]        =   LlamaModel(A						)
           original_model.to(A						)
           original_model.eval()
           _UpperCAmelCase      :     Dict        =   original_model(A						).last_hidden_state
           _UpperCAmelCase      :     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
           _UpperCAmelCase      :     Any        =   {"type": scaling_type, "factor": 10.0}
           _UpperCAmelCase      :     List[Any]        =   LlamaModel(A						)
           scaled_model.to(A						)
           scaled_model.eval()
           _UpperCAmelCase      :     List[str]        =   scaled_model(A						).last_hidden_state
           _UpperCAmelCase      :     str        =   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						)						)







@require_torch
class      UpperCAmelCase_  (    unittest.TestCase						):



      @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"						)
      @slow
      def 						snake_case_ (					self					:     Optional[Any]						):
           _UpperCAmelCase      :     int        =   [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
           _UpperCAmelCase      :     Any        =   LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf"					,     device_map="auto"						)
           _UpperCAmelCase      :     int        =   model(torch.tensor([input_ids]						)						)
           # Expected mean on dim = -1
           _UpperCAmelCase      :     Optional[Any]        =   torch.tensor([[-6.6_550, -4.1_227, -4.9_859, -3.2_406, 0.8_262, -3.0_033, 1.2_964, -3.3_699]]						)
           torch.testing.assert_close(out.mean(-1						)					,     A					,     atol=1e-2					,     rtol=1e-2						)
           # slicing logits[0, 0, 0:30]
           # fmt: off
           _UpperCAmelCase      :     Optional[int]        =   torch.tensor([-12.8_281, -7.4_453, -0.4_639, -8.0_625, -7.2_500, -8.0_000, -6.4_883, -7.7_695, -7.8_438, -7.0_312, -6.2_188, -7.1_328, -1.8_496, 1.9_961, -8.6_250, -6.7_227, -12.8_281, -6.9_492, -7.0_742, -7.7_852, -7.5_820, -7.9_062, -6.9_375, -7.9_805, -8.3_438, -8.1_562, -8.0_469, -7.6_250, -7.7_422, -7.3_398,]						)
           # fmt: on
           torch.testing.assert_close(out[0, 0, :3_0]					,     A					,     atol=1e-5					,     rtol=1e-5						)



      @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"						)
      @slow
      def 						snake_case_ (					self					:     Any						):
           _UpperCAmelCase      :     List[str]        =   [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
           _UpperCAmelCase      :     Optional[Any]        =   LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-hf"					,     device_map="auto"						)
           _UpperCAmelCase      :     Tuple        =   model(torch.tensor(A						)						)
           # Expected mean on dim = -1
           _UpperCAmelCase      :     Dict        =   torch.tensor([[-2.0_622, -1.2_794, -1.1_638, -0.9_788, -1.4_603, -1.0_238, -1.7_893, -1.4_411]]						)
           torch.testing.assert_close(out.mean(-1						)					,     A					,     atol=1e-2					,     rtol=1e-2						)
           # slicing logits[0, 0, 0:30]
           # fmt: off
           _UpperCAmelCase      :     Dict        =   torch.tensor([-8.1_406, -8.0_547, 2.7_461, -1.2_344, -0.1_448, -1.8_262, -1.0_020, -1.8_154, -1.6_895, -1.8_516, -2.3_574, -0.9_277, 3.7_598, 6.5_742, -1.2_998, -0.1_177, -8.1_406, -2.9_688, -2.9_199, -3.1_699, -3.5_254, -2.3_555, -2.7_988, -3.4_141, -2.8_262, -4.5_195, -3.3_379, -3.3_164, -2.7_832, -3.0_273]						)
           # fmt: on
           torch.testing.assert_close(out[0, 0, :3_0]					,     A					,     atol=1e-5					,     rtol=1e-5						)



      @unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"						)
      @slow
      def 						snake_case_ (					self					:     Dict						):
           _UpperCAmelCase      :     Tuple        =   [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
           _UpperCAmelCase      :     Optional[int]        =   LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf"					,     device_map="auto"						)
           _UpperCAmelCase      :     Optional[Any]        =   model(torch.tensor(A						)						)
           # Expected mean on dim = -1
           _UpperCAmelCase      :     str        =   torch.tensor([[-0.8_562, -1.8_520, -0.7_551, -0.4_162, -1.5_161, -1.2_038, -2.4_823, -2.3_254]]						)
           torch.testing.assert_close(out.mean(-1						)					,     A					,     atol=1e-2					,     rtol=1e-2						)
           # slicing logits[0, 0, 0:30]
           # fmt: off
           _UpperCAmelCase      :     Any        =   torch.tensor([-2.2_227, 4.8_828, 0.9_023, -0.4_578, -0.7_871, -0.1_033, -0.6_221, -0.5_786, -0.7_803, -1.0_674, -1.2_920, -0.1_570, 0.8_008, 2.0_723, -0.9_497, 0.2_771, -2.2_227, -0.7_612, -1.4_346, -1.2_061, -1.6_426, -0.3_000, -0.7_139, -1.1_934, -1.8_691, -1.6_973, -1.5_947, -1.2_705, -0.3_523, -0.5_513]						)
           # fmt: on
           torch.testing.assert_close(out.mean(-1						)					,     A					,     atol=1e-2					,     rtol=1e-2						)



      @unittest.skip(
          "Logits are not exactly the same, once we fix the instabalities somehow, will update! Also it is gonna be a `too_slow` test"						)
      @slow
      def 						snake_case_ (					self					:     Optional[Any]						):
           _UpperCAmelCase      :     List[str]        =   [1, 3_0_6, 4_6_5_8, 2_7_8, 6_5_9_3, 3_1_0, 2_8_3_4, 3_3_8]
           _UpperCAmelCase      :     Union[str, Any]        =   LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-70b-hf"					,     device_map="auto"						)
           _UpperCAmelCase      :     str        =   model(torch.tensor(A						)						)

           _UpperCAmelCase      :     Tuple        =   torch.tensor(
               [[-4.2_327, -3.3_360, -4.6_665, -4.7_631, -1.8_180, -3.4_170, -1.4_211, -3.1_810]]					,     dtype=torch.floataa						)
           torch.testing.assert_close(out.mean(-1						)					,     A					,     atol=1e-2					,     rtol=1e-2						)
           # fmt: off
           _UpperCAmelCase      :     List[str]        =   torch.tensor([-9.4_922, -3.9_551, 1.7_998, -5.6_758, -5.1_055, -5.8_984, -4.8_320, -6.8_086, -6.5_391, -5.6_172, -5.5_820, -5.5_352, 1.7_881, 3.6_289, -6.5_117, -3.4_785, -9.5_000, -6.0_352, -6.8_125, -6.0_195, -6.6_836, -5.4_727, -6.2_812, -6.0_391, -7.3_398, -7.4_297, -7.4_844, -6.5_820, -5.8_789, -5.5_312]						)
           # fmt: on
           torch.testing.assert_close(out[0, 0, :3_0]					,     A					,     atol=1e-5					,     rtol=1e-5						)



      @unittest.skip("Model is curently gated"						)
      @slow
      def 						snake_case_ (					self					:     Union[str, Any]						):
           _UpperCAmelCase      :     Optional[int]        =   "Simply put, the theory of relativity states that 1) the laws of physics are the same everywhere in the universe and 2) the passage of time and the length of objects can vary depending on the observer\'s frame of reference.\n\nThe first part of the theory, that the laws of physics are the same everywhere, is known as the \"princi"
           _UpperCAmelCase      :     str        =   "Simply put, the theory of relativity states that "
           _UpperCAmelCase      :     str        =   LlamaTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf"						)
           _UpperCAmelCase      :     Any        =   tokenizer.encode(A					,     return_tensors="pt"						)
           _UpperCAmelCase      :     List[str]        =   LlamaForCausalLM.from_pretrained(
               "meta-llama/Llama-2-13b-chat-hf"					,     device_map="sequential"					,     use_safetensors=A						)

           # greedy generation outputs
           _UpperCAmelCase      :     str        =   model.generate(A					,     max_new_tokens=6_4					,     top_p=A					,     temperature=1					,     do_sample=A						)
           _UpperCAmelCase      :     Any        =   tokenizer.decode(generated_ids[0]					,     skip_special_tokens=A						)
           self.assertEqual(A					,     A						)





"""simple docstring"""




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from .tokenization_electra import ElectraTokenizer


_lowerCAmelCase    : List[str]							=							{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_lowerCAmelCase    : int							=							{
    "vocab_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/vocab.txt"
        ),
        "google/electra-base-generator": "https://huggingface.co/google/electra-base-generator/resolve/main/vocab.txt",
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/vocab.txt"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/vocab.txt"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "google/electra-small-generator": (
            "https://huggingface.co/google/electra-small-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-generator": (
            "https://huggingface.co/google/electra-base-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-generator": (
            "https://huggingface.co/google/electra-large-generator/resolve/main/tokenizer.json"
        ),
        "google/electra-small-discriminator": (
            "https://huggingface.co/google/electra-small-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-base-discriminator": (
            "https://huggingface.co/google/electra-base-discriminator/resolve/main/tokenizer.json"
        ),
        "google/electra-large-discriminator": (
            "https://huggingface.co/google/electra-large-discriminator/resolve/main/tokenizer.json"
        ),
    },
}

_lowerCAmelCase    : List[Any]							=							{
    "google/electra-small-generator": 5_12,
    "google/electra-base-generator": 5_12,
    "google/electra-large-generator": 5_12,
    "google/electra-small-discriminator": 5_12,
    "google/electra-base-discriminator": 5_12,
    "google/electra-large-discriminator": 5_12,
}

_lowerCAmelCase    : Optional[Any]							=							{
    "google/electra-small-generator": {"do_lower_case": True},
    "google/electra-base-generator": {"do_lower_case": True},
    "google/electra-large-generator": {"do_lower_case": True},
    "google/electra-small-discriminator": {"do_lower_case": True},
    "google/electra-base-discriminator": {"do_lower_case": True},
    "google/electra-large-discriminator": {"do_lower_case": True},
}






class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			VOCAB_FILES_NAMES
      __SCREAMING_SNAKE_CASE						:       Any								=			PRETRAINED_VOCAB_FILES_MAP
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_INIT_CONFIGURATION
      __SCREAMING_SNAKE_CASE						:       List[str]								=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __SCREAMING_SNAKE_CASE						:       Tuple								=			ElectraTokenizer



      def __init__(					self					:     Dict					,     A					:     Dict=None					,     A					:     Optional[int]=None					,     A					:     Dict=True					,     A					:     Optional[Any]="[UNK]"					,     A					:     Any="[SEP]"					,     A					:     str="[PAD]"					,     A					:     Tuple="[CLS]"					,     A					:     Optional[Any]="[MASK]"					,     A					:     Any=True					,     A					:     Tuple=None					,     **A					:     Any					,     ):
           super().__init__(
               A					,     tokenizer_file=A					,     do_lower_case=A					,     unk_token=A					,     sep_token=A					,     pad_token=A					,     cls_token=A					,     mask_token=A					,     tokenize_chinese_chars=A					,     strip_accents=A					,     **A					,     )

           _UpperCAmelCase      :     List[str]        =   json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
           if (
               normalizer_state.get("lowercase"					,     A						) != do_lower_case
               or normalizer_state.get("strip_accents"					,     A						) != strip_accents
               or normalizer_state.get("handle_chinese_chars"					,     A						) != tokenize_chinese_chars
           ):
                _UpperCAmelCase      :     Union[str, Any]        =   getattr(A					,     normalizer_state.pop("type"						)						)
                _UpperCAmelCase      :     Dict        =   do_lower_case
                _UpperCAmelCase      :     Optional[int]        =   strip_accents
                _UpperCAmelCase      :     Any        =   tokenize_chinese_chars
                _UpperCAmelCase      :     Optional[Any]        =   normalizer_class(**A						)

           _UpperCAmelCase      :     int        =   do_lower_case



      def 						snake_case_ (					self					:     Tuple					,     A					:     str					,     A					:     int=None						):
           _UpperCAmelCase      :     Dict        =   [self.cls_token_id] + token_ids_a + [self.sep_token_id]

           if token_ids_a:
                output += token_ids_a + [self.sep_token_id]

           return output



      def 						snake_case_ (					self					:     Any					,     A					:     List[int]					,     A					:     Optional[List[int]] = None						):
           _UpperCAmelCase      :     Any        =   [self.sep_token_id]
           _UpperCAmelCase      :     Dict        =   [self.cls_token_id]
           if token_ids_a is None:
                return len(cls + token_ids_a + sep						) * [0]
           return len(cls + token_ids_a + sep						) * [0] + len(token_ids_a + sep						) * [1]



      def 						snake_case_ (					self					:     Any					,     A					:     str					,     A					:     Optional[str] = None						):
           _UpperCAmelCase      :     List[Any]        =   self._tokenizer.model.save(A					,     name=A						)
           return tuple(A						)
