
"""simple docstring"""




import shutil
import tempfile
import unittest

from transformers import (
    SPIECE_UNDERLINE,
    AddedToken,
    BatchEncoding,
    NllbTokenizer,
    NllbTokenizerFast,
    is_torch_available,
)
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


a		:       Union[str, Any]     						=       get_tests_dir('fixtures/test_sentencepiece.model')


if is_torch_available():
    from transformers.models.mam_aaa.modeling_mam_aaa import shift_tokens_right

a		:       Optional[Any]     						=       25_60_47
a		:       Tuple     						=       25_61_45







@require_sentencepiece
@require_tokenizers
class     lowercase(_lowercase    ,						unittest.TestCase       ):
     __snake_case:	Dict						=     NllbTokenizer
     __snake_case:	Dict						=     NllbTokenizerFast
     __snake_case:	List[Any]						=     True
     __snake_case:	List[str]						=     True
     __snake_case:	Union[str, Any]						=     {}



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         super().setUp()

         # We have a SentencePiece fixture for testing
         a__		=			NllbTokenizer(__SCREAMING_SNAKE_CASE     ,				keep_accents=__SCREAMING_SNAKE_CASE   )
         tokenizer.save_pretrained(self.tmpdirname   )



     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         a__		=			NllbTokenizer(__SCREAMING_SNAKE_CASE     ,				keep_accents=__SCREAMING_SNAKE_CASE   )

         a__		=			tokenizer.tokenize('This is a test'   )
         self.assertListEqual(__SCREAMING_SNAKE_CASE     ,				['▁This', '▁is', '▁a', '▁t', 'est']   )

         self.assertListEqual(
             tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE   )     ,				[value + tokenizer.fairseq_offset for value in [2_8_5, 4_6, 1_0, 1_7_0, 3_8_2]]     ,				)

         a__		=			tokenizer.tokenize('I was born in 92000, and this is falsé.'   )
         self.assertListEqual(
             __SCREAMING_SNAKE_CASE     ,				[
                 SPIECE_UNDERLINE + 'I',
                 SPIECE_UNDERLINE + 'was',
                 SPIECE_UNDERLINE + 'b',
                 'or',
                 'n',
                 SPIECE_UNDERLINE + 'in',
                 SPIECE_UNDERLINE + '',
                 '9',
                 '2',
                 '0',
                 '0',
                 '0',
                 ',',
                 SPIECE_UNDERLINE + 'and',
                 SPIECE_UNDERLINE + 'this',
                 SPIECE_UNDERLINE + 'is',
                 SPIECE_UNDERLINE + 'f',
                 'al',
                 's',
                 'é',
                 '.',
             ]     ,				)
         a__		=			tokenizer.convert_tokens_to_ids(__SCREAMING_SNAKE_CASE   )
         self.assertListEqual(
             __SCREAMING_SNAKE_CASE     ,				[
                 value + tokenizer.fairseq_offset
                 for value in [8, 2_1, 8_4, 5_5, 2_4, 1_9, 7, 2, 6_0_2, 3_4_7, 3_4_7, 3_4_7, 3, 1_2, 6_6, 4_6, 7_2, 8_0, 6, 2, 4]
             ]     ,				)

         a__		=			tokenizer.convert_ids_to_tokens(__SCREAMING_SNAKE_CASE   )
         self.assertListEqual(
             __SCREAMING_SNAKE_CASE     ,				[
                 SPIECE_UNDERLINE + 'I',
                 SPIECE_UNDERLINE + 'was',
                 SPIECE_UNDERLINE + 'b',
                 'or',
                 'n',
                 SPIECE_UNDERLINE + 'in',
                 SPIECE_UNDERLINE + '',
                 '<unk>',
                 '2',
                 '0',
                 '0',
                 '0',
                 ',',
                 SPIECE_UNDERLINE + 'and',
                 SPIECE_UNDERLINE + 'this',
                 SPIECE_UNDERLINE + 'is',
                 SPIECE_UNDERLINE + 'f',
                 'al',
                 's',
                 '<unk>',
                 '.',
             ]     ,				)



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			(self.rust_tokenizer_class, 'hf-internal-testing/tiny-random-nllb', {})
         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
             with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
                 a__		=			self.rust_tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE   )
                 a__		=			self.tokenizer_class.from_pretrained(__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE   )

                 a__		=			tempfile.mkdtemp()

                 a__		=			tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Checks it save with the same files + the tokenizer.json file for the fast one
                 self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files   )   )
                 a__		=			tuple(f for f in tokenizer_r_files if 'tokenizer.json' not in f   )
                 self.assertSequenceEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )

                 # Checks everything loads correctly in the same way
                 a__		=			tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Check special tokens are set accordingly on Rust and Python
                 for key in tokenizer_pp.special_tokens_map:
                     self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )   )

                 shutil.rmtree(__SCREAMING_SNAKE_CASE   )

                 # Save tokenizer rust, legacy_format=True
                 a__		=			tempfile.mkdtemp()

                 a__		=			tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE     ,				legacy_format=__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Checks it save with the same files
                 self.assertSequenceEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )

                 # Checks everything loads correctly in the same way
                 a__		=			tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Check special tokens are set accordingly on Rust and Python
                 for key in tokenizer_pp.special_tokens_map:
                     self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )   )

                 shutil.rmtree(__SCREAMING_SNAKE_CASE   )

                 # Save tokenizer rust, legacy_format=False
                 a__		=			tempfile.mkdtemp()

                 a__		=			tokenizer_r.save_pretrained(__SCREAMING_SNAKE_CASE     ,				legacy_format=__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.save_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Checks it saved the tokenizer.json file
                 self.assertTrue(any('tokenizer.json' in f for f in tokenizer_r_files   )   )

                 # Checks everything loads correctly in the same way
                 a__		=			tokenizer_r.from_pretrained(__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_p.from_pretrained(__SCREAMING_SNAKE_CASE   )

                 # Check special tokens are set accordingly on Rust and Python
                 for key in tokenizer_pp.special_tokens_map:
                     self.assertTrue(hasattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )   )

                 shutil.rmtree(__SCREAMING_SNAKE_CASE   )



     @require_torch
     def 			lowercase__  (							self   )		->     Tuple:







         """simple docstring"""



         if not self.test_seqaseq:
             return

         a__		=			self.get_tokenizers()
         for tokenizer in tokenizers:
             with self.subTest(f'{tokenizer.__class__.__name__}'   ):
                 # Longer text that will definitely require truncation.
                 a__		=			[
                     ' UN Chief Says There Is No Military Solution in Syria',
                     ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for'
                     ' Syria is that \'there is no military solution\' to the nearly five-year conflict and more weapons'
                     ' will only worsen the violence and misery for millions of people.',
                 ]
                 a__		=			[
                     'Şeful ONU declară că nu există o soluţie militară în Siria',
                     'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al'
                     ' Rusiei pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi'
                     ' că noi arme nu vor face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
                 ]
                 try:
                     a__		=			tokenizer.prepare_seqaseq_batch(
                         src_texts=__SCREAMING_SNAKE_CASE     ,				tgt_texts=__SCREAMING_SNAKE_CASE     ,				max_length=3     ,				max_target_length=1_0     ,				return_tensors='pt'     ,				src_lang='eng_Latn'     ,				tgt_lang='ron_Latn'     ,				)
                 except NotImplementedError:
                     return
                 self.assertEqual(batch.input_ids.shape[1]     ,				3   )
                 self.assertEqual(batch.labels.shape[1]     ,				1_0   )
                 # max_target_length will default to max_length if not specified
                 a__		=			tokenizer.prepare_seqaseq_batch(
                     __SCREAMING_SNAKE_CASE     ,				tgt_texts=__SCREAMING_SNAKE_CASE     ,				max_length=3     ,				return_tensors='pt'   )
                 self.assertEqual(batch.input_ids.shape[1]     ,				3   )
                 self.assertEqual(batch.labels.shape[1]     ,				3   )

                 a__		=			tokenizer.prepare_seqaseq_batch(
                     src_texts=__SCREAMING_SNAKE_CASE     ,				max_length=3     ,				max_target_length=1_0     ,				return_tensors='pt'   )
                 self.assertEqual(batch_encoder_only.input_ids.shape[1]     ,				3   )
                 self.assertEqual(batch_encoder_only.attention_mask.shape[1]     ,				3   )
                 self.assertNotIn('decoder_input_ids'     ,				__SCREAMING_SNAKE_CASE   )



     @unittest.skip('Unfortunately way too slow to build a BPE with SentencePiece.'   )
     def 			lowercase__  (							self   )		->     Union[str, Any]:







         """simple docstring"""



         pass




     def 			lowercase__  (							self   )		->     Optional[int]:







         """simple docstring"""



         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
             with self.subTest(f'{tokenizer.__class__.__name__} ({pretrained_name})'   ):
                 a__		=			[AddedToken('<special>'     ,				lstrip=__SCREAMING_SNAKE_CASE   )]

                 a__		=			self.rust_tokenizer_class.from_pretrained(
                     __SCREAMING_SNAKE_CASE     ,				additional_special_tokens=__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE   )
                 a__		=			tokenizer_r.encode('Hey this is a <special> token'   )

                 a__		=			tokenizer_r.encode('<special>'     ,				add_special_tokens=__SCREAMING_SNAKE_CASE   )[0]

                 self.assertTrue(special_token_id in r_output   )

                 if self.test_slow_tokenizer:
                     a__		=			self.rust_tokenizer_class.from_pretrained(
                         __SCREAMING_SNAKE_CASE     ,				additional_special_tokens=__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE     ,				)
                     a__		=			self.tokenizer_class.from_pretrained(
                         __SCREAMING_SNAKE_CASE     ,				additional_special_tokens=__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE   )

                     a__		=			tokenizer_p.encode('Hey this is a <special> token'   )

                     a__		=			tokenizer_cr.encode('Hey this is a <special> token'   )

                     self.assertEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
                     self.assertEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
                     self.assertTrue(special_token_id in p_output   )
                     self.assertTrue(special_token_id in cr_output   )







@require_torch
@require_sentencepiece
@require_tokenizers
class     lowercase(unittest.TestCase       ):
     __snake_case:	Any						=     'facebook/nllb-200-distilled-600M'
     __snake_case:	Dict						=     [
         ' UN Chief Says There Is No Military Solution in Syria',
         ' Secretary-General Ban Ki-moon says his response to Russia\'s stepped up military support for Syria is that "there is no military solution" to the nearly five-year conflict and more weapons will only worsen the violence and misery for millions of people.',
     ]
     __snake_case:	Dict						=     [
         'Şeful ONU declară că nu există o soluţie militară în Siria',
         'Secretarul General Ban Ki-moon declară că răspunsul său la intensificarea sprijinului militar al Rusiei'
         ' pentru Siria este că "nu există o soluţie militară" la conflictul de aproape cinci ani şi că noi arme nu vor'
         ' face decât să înrăutăţească violenţele şi mizeria pentru milioane de oameni.',
     ]
     __snake_case:	Any						=     [
         256047,
         16297,
         134408,
         8165,
         248066,
         14734,
         950,
         1135,
         105721,
         3573,
         83,
         27352,
         108,
         49486,
         2,
     ]



     @classmethod
     def 			lowercase__  (							cls   )		->     Optional[int]:







         """simple docstring"""



         a__		=			NllbTokenizer.from_pretrained(
             cls.checkpoint_name     ,				src_lang='eng_Latn'     ,				tgt_lang='ron_Latn'   )
         a__		=			1
         return cls



     def 			lowercase__  (							self   )		->     Dict:







         """simple docstring"""



         self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Arab']     ,				2_5_6_0_0_1   )
         self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['ace_Latn']     ,				2_5_6_0_0_2   )
         self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['fra_Latn']     ,				2_5_6_0_5_7   )



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			self.tokenizer.batch_encode_plus(self.src_text   ).input_ids[0]
         self.assertListEqual(self.expected_src_tokens     ,				__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     str:







         """simple docstring"""



         self.assertIn(__SCREAMING_SNAKE_CASE     ,				self.tokenizer.all_special_ids   )
         # fmt: off
         a__		=			[RO_CODE, 4_2_5_4, 9_8_0_6_8, 1_1_2_9_2_3, 3_9_0_7_2, 3_9_0_9, 7_1_3, 1_0_2_7_6_7, 2_6, 1_7_3_1_4, 3_5_6_4_2, 1_4_6_8_3, 3_3_1_1_8, 2_0_2_2, 6_6_9_8_7, 2, 2_5_6_0_4_7]
         # fmt: on

         a__		=			self.tokenizer.decode(__SCREAMING_SNAKE_CASE     ,				skip_special_tokens=__SCREAMING_SNAKE_CASE   )
         a__		=			self.tokenizer.decode(generated_ids[1:]     ,				skip_special_tokens=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
         self.assertNotIn(self.tokenizer.eos_token     ,				__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     Union[str, Any]:







         """simple docstring"""



         a__		=			['this is gunna be a long sentence ' * 2_0]
         assert isinstance(src_text[0]     ,				__SCREAMING_SNAKE_CASE   )
         a__		=			1_0
         a__		=			self.tokenizer(__SCREAMING_SNAKE_CASE     ,				max_length=__SCREAMING_SNAKE_CASE     ,				truncation=__SCREAMING_SNAKE_CASE   ).input_ids[0]
         self.assertEqual(ids[-1]     ,				2   )
         self.assertEqual(ids[0]     ,				__SCREAMING_SNAKE_CASE   )
         self.assertEqual(len(__SCREAMING_SNAKE_CASE   )     ,				__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     Dict:







         """simple docstring"""



         self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['<mask>', 'ar_AR']   )     ,				[2_5_6_2_0_3, 3]   )



     def 			lowercase__  (							self   )		->     Tuple:







         """simple docstring"""



         a__		=			tempfile.mkdtemp()
         a__		=			self.tokenizer.fairseq_tokens_to_ids
         self.tokenizer.save_pretrained(__SCREAMING_SNAKE_CASE   )
         a__		=			NllbTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE   )
         self.assertDictEqual(new_tok.fairseq_tokens_to_ids     ,				__SCREAMING_SNAKE_CASE   )



     @require_torch
     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         a__		=			self.tokenizer(
             self.src_text     ,				text_target=self.tgt_text     ,				padding=__SCREAMING_SNAKE_CASE     ,				truncation=__SCREAMING_SNAKE_CASE     ,				max_length=len(self.expected_src_tokens   )     ,				return_tensors='pt'     ,				)
         a__		=			shift_tokens_right(
             batch['labels']     ,				self.tokenizer.pad_token_id     ,				self.tokenizer.lang_code_to_id['ron_Latn']   )

         self.assertIsInstance(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )

         self.assertEqual((2, 1_5)     ,				batch.input_ids.shape   )
         self.assertEqual((2, 1_5)     ,				batch.attention_mask.shape   )
         a__		=			batch.input_ids.tolist()[0]
         self.assertListEqual(self.expected_src_tokens     ,				__SCREAMING_SNAKE_CASE   )
         self.assertEqual(__SCREAMING_SNAKE_CASE     ,				batch.decoder_input_ids[0, 0]   )  # EOS
         # Test that special tokens are reset
         self.assertEqual(self.tokenizer.prefix_tokens     ,				[EN_CODE]   )
         self.assertEqual(self.tokenizer.suffix_tokens     ,				[self.tokenizer.eos_token_id]   )



     def 			lowercase__  (							self   )		->     Union[str, Any]:







         """simple docstring"""



         a__		=			self.tokenizer(self.src_text     ,				padding=__SCREAMING_SNAKE_CASE     ,				truncation=__SCREAMING_SNAKE_CASE     ,				max_length=3     ,				return_tensors='pt'   )
         a__		=			self.tokenizer(
             text_target=self.tgt_text     ,				padding=__SCREAMING_SNAKE_CASE     ,				truncation=__SCREAMING_SNAKE_CASE     ,				max_length=1_0     ,				return_tensors='pt'   )
         a__		=			targets['input_ids']
         a__		=			shift_tokens_right(
             __SCREAMING_SNAKE_CASE     ,				self.tokenizer.pad_token_id     ,				decoder_start_token_id=self.tokenizer.lang_code_to_id[self.tokenizer.tgt_lang]     ,				)

         self.assertEqual(batch.input_ids.shape[1]     ,				3   )
         self.assertEqual(batch.decoder_input_ids.shape[1]     ,				1_0   )



     @require_torch
     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         a__		=			self.tokenizer._build_translation_inputs(
             'A test'     ,				return_tensors='pt'     ,				src_lang='eng_Latn'     ,				tgt_lang='fra_Latn'   )

         self.assertEqual(
             nested_simplify(__SCREAMING_SNAKE_CASE   )     ,				{
                 # A, test, EOS, en_XX
                 'input_ids': [[2_5_6_0_4_7, 7_0, 7_3_5_6, 2]],
                 'attention_mask': [[1, 1, 1, 1]],
                 # ar_AR
                 'forced_bos_token_id': 2_5_6_0_5_7,
             }     ,				)




     @require_torch
     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__		=			True
         a__		=			self.tokenizer(
             'UN Chief says there is no military solution in Syria'     ,				src_lang='eng_Latn'     ,				tgt_lang='fra_Latn'   )
         self.assertEqual(
             inputs.input_ids     ,				[1_6_2_9_7, 1_3_4_4_0_8, 2_5_6_5_3, 6_3_7_0, 2_4_8, 2_5_4, 1_0_3_9_2_9, 9_4_9_9_5, 1_0_8, 4_9_4_8_6, 2, 2_5_6_0_4_7]   )

         a__		=			False
         a__		=			self.tokenizer(
             'UN Chief says there is no military solution in Syria'     ,				src_lang='eng_Latn'     ,				tgt_lang='fra_Latn'   )
         self.assertEqual(
             inputs.input_ids     ,				[2_5_6_0_4_7, 1_6_2_9_7, 1_3_4_4_0_8, 2_5_6_5_3, 6_3_7_0, 2_4_8, 2_5_4, 1_0_3_9_2_9, 9_4_9_9_5, 1_0_8, 4_9_4_8_6, 2]   )




"""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     lowercase:



     def __init__(							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE=1_3     ,				__SCREAMING_SNAKE_CASE=7     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=True     ,				__SCREAMING_SNAKE_CASE=9_9     ,				__SCREAMING_SNAKE_CASE=3_2     ,				__SCREAMING_SNAKE_CASE=5     ,				__SCREAMING_SNAKE_CASE=4     ,				__SCREAMING_SNAKE_CASE=3_7     ,				__SCREAMING_SNAKE_CASE="gelu"     ,				__SCREAMING_SNAKE_CASE=0.1     ,				__SCREAMING_SNAKE_CASE=0.1     ,				__SCREAMING_SNAKE_CASE=5_1_2     ,				__SCREAMING_SNAKE_CASE=1_6     ,				__SCREAMING_SNAKE_CASE=2     ,				__SCREAMING_SNAKE_CASE=0.02     ,				__SCREAMING_SNAKE_CASE=3     ,				__SCREAMING_SNAKE_CASE=4     ,				__SCREAMING_SNAKE_CASE=None     ,				)		->     int:







         """simple docstring"""



         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 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         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 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         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=__SCREAMING_SNAKE_CASE     ,				initializer_range=self.initializer_range     ,				)



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     Dict:







         """simple docstring"""



         a__		=			LlamaModel(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE   )
         a__		=			model(__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     Any:







         """simple docstring"""



         a__		=			True
         a__		=			LlamaModel(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				)
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				)
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.last_hidden_state.shape     ,				(self.batch_size, self.seq_length, self.hidden_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     List[str]:







         """simple docstring"""



         a__		=			LlamaForCausalLM(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.parent.assertEqual(result.logits.shape     ,				(self.batch_size, self.seq_length, self.vocab_size)   )



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				)		->     Optional[int]:







         """simple docstring"""



         a__		=			True
         a__		=			True
         a__		=			LlamaForCausalLM(config=__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()

         # first forward pass
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				use_cache=__SCREAMING_SNAKE_CASE     ,				)
         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(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				output_hidden_states=__SCREAMING_SNAKE_CASE     ,				)['hidden_states'][0]
         a__		=			model(
             __SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				encoder_hidden_states=__SCREAMING_SNAKE_CASE     ,				encoder_attention_mask=__SCREAMING_SNAKE_CASE     ,				past_key_values=__SCREAMING_SNAKE_CASE     ,				output_hidden_states=__SCREAMING_SNAKE_CASE     ,				)['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(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-3   )   )




     def 			lowercase__  (							self   )		->     Dict:







         """simple docstring"""



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







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



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         a__		=			LlamaModelTester(self   )
         a__		=			ConfigTester(self     ,				config_class=__SCREAMING_SNAKE_CASE     ,				hidden_size=3_7   )



     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         self.config_tester.run_common_tests()



     def 			lowercase__  (							self   )		->     Optional[Any]:







         """simple docstring"""



         a__		=			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         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(*__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			3
         a__		=			input_dict['input_ids']
         a__		=			input_ids.ne(1   ).to(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor([self.model_tester.batch_size]     ,				self.model_tester.type_sequence_label_size   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(result.logits.shape     ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )



     def 			lowercase__  (							self   )		->     Union[str, Any]:







         """simple docstring"""



         a__	,     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(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor([self.model_tester.batch_size]     ,				self.model_tester.type_sequence_label_size   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(result.logits.shape     ,				(self.model_tester.batch_size, self.model_tester.num_labels)   )



     def 			lowercase__  (							self   )		->     int:







         """simple docstring"""



         a__	,     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(__SCREAMING_SNAKE_CASE   )
         a__		=			ids_tensor(
             [self.model_tester.batch_size, config.num_labels]     ,				self.model_tester.type_sequence_label_size   ).to(torch.float   )
         a__		=			LlamaForSequenceClassification(__SCREAMING_SNAKE_CASE   )
         model.to(__SCREAMING_SNAKE_CASE   )
         model.eval()
         a__		=			model(__SCREAMING_SNAKE_CASE     ,				attention_mask=__SCREAMING_SNAKE_CASE     ,				labels=__SCREAMING_SNAKE_CASE   )
         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 			lowercase__  (							self   )		->     List[str]:







         """simple docstring"""



         pass




     @parameterized.expand([('linear',), ('dynamic',)]   )
     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     Optional[int]:







         """simple docstring"""



         a__	,     a__		=			self.model_tester.prepare_config_and_inputs_for_common()
         a__		=			ids_tensor([1, 1_0]     ,				config.vocab_size   )
         a__		=			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
         a__		=			LlamaModel(__SCREAMING_SNAKE_CASE   )
         original_model.to(__SCREAMING_SNAKE_CASE   )
         original_model.eval()
         a__		=			original_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state
         a__		=			original_model(__SCREAMING_SNAKE_CASE   ).last_hidden_state

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

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







@require_torch
class     lowercase(unittest.TestCase       ):



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     Any:







         """simple docstring"""



         a__		=			[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]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-7b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor([input_ids]   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-6.65_50, -4.12_27, -4.98_59, -3.24_06, 0.82_62, -3.00_33, 1.29_64, -3.36_99]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-12.82_81, -7.44_53, -0.46_39, -8.06_25, -7.25_00, -8.00_00, -6.48_83, -7.76_95, -7.84_38, -7.03_12, -6.21_88, -7.13_28, -1.84_96, 1.99_61, -8.62_50, -6.72_27, -12.82_81, -6.94_92, -7.07_42, -7.78_52, -7.58_20, -7.90_62, -6.93_75, -7.98_05, -8.34_38, -8.15_62, -8.04_69, -7.62_50, -7.74_22, -7.33_98,]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     Optional[int]:







         """simple docstring"""



         a__		=			[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]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-2.06_22, -1.27_94, -1.16_38, -0.97_88, -1.46_03, -1.02_38, -1.78_93, -1.44_11]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-8.14_06, -8.05_47, 2.74_61, -1.23_44, -0.14_48, -1.82_62, -1.00_20, -1.81_54, -1.68_95, -1.85_16, -2.35_74, -0.92_77, 3.75_98, 6.57_42, -1.29_98, -0.11_77, -8.14_06, -2.96_88, -2.91_99, -3.16_99, -3.52_54, -2.35_55, -2.79_88, -3.41_41, -2.82_62, -4.51_95, -3.33_79, -3.31_64, -2.78_32, -3.02_73]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )



     @unittest.skip('Logits are not exactly the same, once we fix the instabalities somehow, will update!'   )
     @slow
     def 			lowercase__  (							self   )		->     str:







         """simple docstring"""



         a__		=			[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]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-13b-chat-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )
         # Expected mean on dim = -1
         a__		=			torch.tensor([[-0.85_62, -1.85_20, -0.75_51, -0.41_62, -1.51_61, -1.20_38, -2.48_23, -2.32_54]]   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # slicing logits[0, 0, 0:30]
         # fmt: off
         a__		=			torch.tensor([-2.22_27, 4.88_28, 0.90_23, -0.45_78, -0.78_71, -0.10_33, -0.62_21, -0.57_86, -0.78_03, -1.06_74, -1.29_20, -0.15_70, 0.80_08, 2.07_23, -0.94_97, 0.27_71, -2.22_27, -0.76_12, -1.43_46, -1.20_61, -1.64_26, -0.30_00, -0.71_39, -1.19_34, -1.86_91, -1.69_73, -1.59_47, -1.27_05, -0.35_23, -0.55_13]   )
         # fmt: on
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				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 			lowercase__  (							self   )		->     Tuple:







         """simple docstring"""



         a__		=			[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]
         a__		=			LlamaForCausalLM.from_pretrained('meta-llama/Llama-2-70b-hf'     ,				device_map='auto'   )
         a__		=			model(torch.tensor(__SCREAMING_SNAKE_CASE   )   )

         a__		=			torch.tensor(
             [[-4.23_27, -3.33_60, -4.66_65, -4.76_31, -1.81_80, -3.41_70, -1.42_11, -3.18_10]]     ,				dtype=torch.floataa   )
         torch.testing.assert_close(out.mean(-1   )     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-2     ,				rtol=1e-2   )
         # fmt: off
         a__		=			torch.tensor([-9.49_22, -3.95_51, 1.79_98, -5.67_58, -5.10_55, -5.89_84, -4.83_20, -6.80_86, -6.53_91, -5.61_72, -5.58_20, -5.53_52, 1.78_81, 3.62_89, -6.51_17, -3.47_85, -9.50_00, -6.03_52, -6.81_25, -6.01_95, -6.68_36, -5.47_27, -6.28_12, -6.03_91, -7.33_98, -7.42_97, -7.48_44, -6.58_20, -5.87_89, -5.53_12]   )
         # fmt: on
         torch.testing.assert_close(out[0, 0, :3_0]     ,				__SCREAMING_SNAKE_CASE     ,				atol=1e-5     ,				rtol=1e-5   )




     @unittest.skip('Model is curently gated'   )
     @slow
     def 			lowercase__  (							self   )		->     List[str]:







         """simple docstring"""



         a__		=			'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'
         a__		=			'Simply put, the theory of relativity states that '
         a__		=			LlamaTokenizer.from_pretrained('meta-llama/Llama-2-13b-chat-hf'   )
         a__		=			tokenizer.encode(__SCREAMING_SNAKE_CASE     ,				return_tensors='pt'   )
         a__		=			LlamaForCausalLM.from_pretrained(
             'meta-llama/Llama-2-13b-chat-hf'     ,				device_map='sequential'     ,				use_safetensors=__SCREAMING_SNAKE_CASE   )

         # greedy generation outputs
         a__		=			model.generate(__SCREAMING_SNAKE_CASE     ,				max_new_tokens=6_4     ,				top_p=__SCREAMING_SNAKE_CASE     ,				temperature=1     ,				do_sample=__SCREAMING_SNAKE_CASE   )
         a__		=			tokenizer.decode(generated_ids[0]     ,				skip_special_tokens=__SCREAMING_SNAKE_CASE   )
         self.assertEqual(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )


