


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






						'''simple docstring'''







						def __init__(	self:						Any   ,				__UpperCamelCase:						Dict   ,				__UpperCamelCase:						Optional[int]=13   ,				__UpperCamelCase:						Optional[Any]=7   ,				__UpperCamelCase:						Tuple=True   ,				__UpperCamelCase:						str=True   ,				__UpperCamelCase:						List[Any]=False   ,				__UpperCamelCase:						Union[str, Any]=True   ,				__UpperCamelCase:						Tuple=99   ,				__UpperCamelCase:						Optional[Any]=32   ,				__UpperCamelCase:						List[Any]=5   ,				__UpperCamelCase:						Dict=4   ,				__UpperCamelCase:						Union[str, Any]=37   ,				__UpperCamelCase:						List[str]="gelu"   ,				__UpperCamelCase:						Optional[int]=0.1   ,				__UpperCamelCase:						Optional[int]=0.1   ,				__UpperCamelCase:						List[Any]=512   ,				__UpperCamelCase:						List[Any]=16   ,				__UpperCamelCase:						List[Any]=2   ,				__UpperCamelCase:						Optional[int]=0.0_2   ,				__UpperCamelCase:						Dict=3   ,				__UpperCamelCase:						List[Any]=4   ,				__UpperCamelCase:						Any=None   ,				)				->						Dict:
										__magic_name__					:       Optional[int]    							=							parent
										__magic_name__					:       Any    							=							batch_size
										__magic_name__					:       int    							=							seq_length
										__magic_name__					:       int    							=							is_training
										__magic_name__					:       Optional[int]    							=							use_input_mask
										__magic_name__					:       List[Any]    							=							use_token_type_ids
										__magic_name__					:       List[Any]    							=							use_labels
										__magic_name__					:       int    							=							vocab_size
										__magic_name__					:       Union[str, Any]    							=							hidden_size
										__magic_name__					:       str    							=							num_hidden_layers
										__magic_name__					:       Tuple    							=							num_attention_heads
										__magic_name__					:       int    							=							intermediate_size
										__magic_name__					:       Optional[int]    							=							hidden_act
										__magic_name__					:       Tuple    							=							hidden_dropout_prob
										__magic_name__					:       Dict    							=							attention_probs_dropout_prob
										__magic_name__					:       List[Any]    							=							max_position_embeddings
										__magic_name__					:       str    							=							type_vocab_size
										__magic_name__					:       Tuple    							=							type_sequence_label_size
										__magic_name__					:       Optional[int]    							=							initializer_range
										__magic_name__					:       List[Any]    							=							num_labels
										__magic_name__					:       int    							=							num_choices
										__magic_name__					:       List[str]    							=							scope







						def lowerCAmelCase__			(	self:						Optional[int]  )				->						Dict:
										__magic_name__					:       Optional[Any]    							=							ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size  )

										__magic_name__					:       Dict    							=							None
										if self.use_input_mask:
														__magic_name__					:       Tuple    							=							random_attention_mask([self.batch_size, self.seq_length]  )

										__magic_name__					:       List[Any]    							=							None
										if self.use_token_type_ids:
														__magic_name__					:       List[str]    							=							ids_tensor([self.batch_size, self.seq_length]   ,				self.type_vocab_size  )

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

										__magic_name__					:       Tuple    							=							self.get_config()

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







						def lowerCAmelCase__			(	self:						Any  )				->						Dict:
										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 lowerCAmelCase__			(	self:						List[str]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						Optional[int]   ,				__UpperCamelCase:						Any   ,				__UpperCamelCase:						List[str]   ,				__UpperCamelCase:						List[Any]  )				->						Any:
										__magic_name__					:       Union[str, Any]    							=							LlamaModel(config=_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       Tuple    							=							model(_SCREAMING_SNAKE_CASE   ,				attention_mask=_SCREAMING_SNAKE_CASE  )
										__magic_name__					:       str    							=							model(_SCREAMING_SNAKE_CASE  )
										self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)  )







						def lowerCAmelCase__			(	self:						Tuple   ,				__UpperCamelCase:						List[Any]   ,				__UpperCamelCase:						str   ,				__UpperCamelCase:						str   ,				__UpperCamelCase:						Optional[Any]   ,				__UpperCamelCase:						List[Any]   ,				__UpperCamelCase:						List[Any]   ,				__UpperCamelCase:						int   ,				__UpperCamelCase:						Any   ,				__UpperCamelCase:						Dict   ,				)				->						List[str]:
										__magic_name__					:       Tuple    							=							True
										__magic_name__					:       List[str]    							=							LlamaModel(_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       Optional[int]    							=							model(
										    _SCREAMING_SNAKE_CASE   ,				attention_mask=_SCREAMING_SNAKE_CASE   ,				encoder_hidden_states=_SCREAMING_SNAKE_CASE   ,				encoder_attention_mask=_SCREAMING_SNAKE_CASE   ,				)
										__magic_name__					:       Dict    							=							model(
										    _SCREAMING_SNAKE_CASE   ,				attention_mask=_SCREAMING_SNAKE_CASE   ,				encoder_hidden_states=_SCREAMING_SNAKE_CASE   ,				)
										__magic_name__					:       Any    							=							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 lowerCAmelCase__			(	self:						Union[str, Any]   ,				__UpperCamelCase:						Optional[int]   ,				__UpperCamelCase:						Any   ,				__UpperCamelCase:						int   ,				__UpperCamelCase:						Optional[Any]   ,				__UpperCamelCase:						Optional[int]   ,				__UpperCamelCase:						int   ,				__UpperCamelCase:						str   ,				__UpperCamelCase:						Optional[int]   ,				__UpperCamelCase:						List[str]   ,				)				->						Union[str, Any]:
										__magic_name__					:       Tuple    							=							LlamaForCausalLM(config=_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       List[str]    							=							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 lowerCAmelCase__			(	self:						Optional[int]   ,				__UpperCamelCase:						Optional[Any]   ,				__UpperCamelCase:						int   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						List[Any]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						Union[str, Any]   ,				__UpperCamelCase:						int   ,				__UpperCamelCase:						Dict   ,				)				->						int:
										__magic_name__					:       List[str]    							=							True
										__magic_name__					:       Tuple    							=							True
										__magic_name__					:       str    							=							LlamaForCausalLM(config=_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()

										# first forward pass
										__magic_name__					:       List[str]    							=							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   ,				)
										__magic_name__					:       List[str]    							=							outputs.past_key_values

										# create hypothetical multiple next token and extent to next_input_ids
										__magic_name__					:       Dict    							=							ids_tensor((self.batch_size, 3)   ,				config.vocab_size  )
										__magic_name__					:       List[str]    							=							ids_tensor((self.batch_size, 3)   ,				vocab_size=2  )

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

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






@require_torch
class 		_snake_case     (					__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE	,	__SCREAMING_SNAKE_CASE	,	unittest.TestCase		):






						'''simple docstring'''







						__snake_case				=   (LlamaModel, LlamaForCausalLM, LlamaForSequenceClassification) if is_torch_available() else ()
						__snake_case				=   (LlamaForCausalLM,) if is_torch_available() else ()
						__snake_case				=   (
						    {
						        "feature-extraction": LlamaModel,
						        "text-classification": LlamaForSequenceClassification,
						        "text-generation": LlamaForCausalLM,
						        "zero-shot": LlamaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						__snake_case				=   False
						__snake_case				=   False







						def lowerCAmelCase__			(	self:						Union[str, Any]  )				->						Tuple:
										__magic_name__					:       int    							=							LlamaModelTester(self  )
										__magic_name__					:       List[Any]    							=							ConfigTester(self   ,				config_class=_SCREAMING_SNAKE_CASE   ,				hidden_size=37  )







						def lowerCAmelCase__			(	self:						Any  )				->						List[str]:
										self.config_tester.run_common_tests()







						def lowerCAmelCase__			(	self:						Tuple  )				->						str:
										__magic_name__					:       Tuple    							=							self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE  )







						def lowerCAmelCase__			(	self:						Tuple  )				->						str:
										__magic_name__					:       Optional[Any]    							=							self.model_tester.prepare_config_and_inputs()
										for type in ["absolute", "relative_key", "relative_key_query"]:
														__magic_name__					:       Union[str, Any]    							=							type
														self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE  )







						def lowerCAmelCase__			(	self:						List[Any]  )				->						Optional[int]:
										__magic_name__					:       Optional[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()
										__magic_name__					:       Optional[int]    							=							3
										__magic_name__					:       Optional[Any]    							=							input_dict["input_ids"]
										__magic_name__					:       List[Any]    							=							input_ids.ne(1  ).to(_SCREAMING_SNAKE_CASE  )
										__magic_name__					:       Tuple    							=							ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size  )
										__magic_name__					:       Union[str, Any]    							=							LlamaForSequenceClassification(_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       Dict    							=							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 lowerCAmelCase__			(	self:						List[str]  )				->						Tuple:
										__magic_name__					:       Optional[int]    							=							self.model_tester.prepare_config_and_inputs_for_common()
										__magic_name__					:       Any    							=							3
										__magic_name__					:       Optional[int]    							=							"single_label_classification"
										__magic_name__					:       Tuple    							=							input_dict["input_ids"]
										__magic_name__					:       Any    							=							input_ids.ne(1  ).to(_SCREAMING_SNAKE_CASE  )
										__magic_name__					:       Optional[Any]    							=							ids_tensor([self.model_tester.batch_size]   ,				self.model_tester.type_sequence_label_size  )
										__magic_name__					:       Tuple    							=							LlamaForSequenceClassification(_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       str    							=							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 lowerCAmelCase__			(	self:						Optional[int]  )				->						str:
										__magic_name__					:       List[str]    							=							self.model_tester.prepare_config_and_inputs_for_common()
										__magic_name__					:       str    							=							3
										__magic_name__					:       int    							=							"multi_label_classification"
										__magic_name__					:       Union[str, Any]    							=							input_dict["input_ids"]
										__magic_name__					:       str    							=							input_ids.ne(1  ).to(_SCREAMING_SNAKE_CASE  )
										__magic_name__					:       int    							=							ids_tensor(
										    [self.model_tester.batch_size, config.num_labels]   ,				self.model_tester.type_sequence_label_size  ).to(torch.float  )
										__magic_name__					:       Dict    							=							LlamaForSequenceClassification(_SCREAMING_SNAKE_CASE  )
										model.to(_SCREAMING_SNAKE_CASE  )
										model.eval()
										__magic_name__					:       Dict    							=							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 lowerCAmelCase__			(	self:						int  )				->						List[str]:
										pass







						@parameterized.expand([("linear",), ("dynamic",)]  )
						def lowerCAmelCase__			(	self:						Any   ,				__UpperCamelCase:						Optional[Any]  )				->						List[Any]:
										__magic_name__					:       Optional[Any]    							=							self.model_tester.prepare_config_and_inputs_for_common()
										__magic_name__					:       Optional[Any]    							=							ids_tensor([1, 10]   ,				config.vocab_size  )
										__magic_name__					:       Union[str, Any]    							=							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
										__magic_name__					:       List[str]    							=							LlamaModel(_SCREAMING_SNAKE_CASE  )
										original_model.to(_SCREAMING_SNAKE_CASE  )
										original_model.eval()
										__magic_name__					:       Dict    							=							original_model(_SCREAMING_SNAKE_CASE  ).last_hidden_state
										__magic_name__					:       Optional[int]    							=							original_model(_SCREAMING_SNAKE_CASE  ).last_hidden_state

										set_seed(42  )  # Fixed seed at init time so the two models get the same random weights
										__magic_name__					:       Dict    							=							{"type": scaling_type, "factor": 1_0.0}
										__magic_name__					:       Optional[Any]    							=							LlamaModel(_SCREAMING_SNAKE_CASE  )
										scaled_model.to(_SCREAMING_SNAKE_CASE  )
										scaled_model.eval()
										__magic_name__					:       List[Any]    							=							scaled_model(_SCREAMING_SNAKE_CASE  ).last_hidden_state
										__magic_name__					:       Union[str, Any]    							=							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 		_snake_case     (					unittest.TestCase		):






						'''simple docstring'''







						@unittest.skip("Logits are not exactly the same, once we fix the instabalities somehow, will update!"  )
						@slow
						def lowerCAmelCase__			(	self:						int  )				->						Dict:
										__magic_name__					:       List[str]    							=							[1, 306, 4658, 278, 6593, 310, 2834, 338]
										__magic_name__					:       str    							=							LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf"   ,				device_map="auto"  )
										__magic_name__					:       Dict    							=							model(torch.tensor([input_ids]  )  )
										# Expected mean on dim = -1
										__magic_name__					:       List[Any]    							=							torch.tensor([[-6.6_5_5_0, -4.1_2_2_7, -4.9_8_5_9, -3.2_4_0_6, 0.8_2_6_2, -3.0_0_3_3, 1.2_9_6_4, -3.3_6_9_9]]  )
										torch.testing.assert_close(out.mean(-1  )   ,				_SCREAMING_SNAKE_CASE   ,				atol=1E-2   ,				rtol=1E-2  )
										# slicing logits[0, 0, 0:30]
										# fmt: off
										__magic_name__					:       Dict    							=							torch.tensor([-1_2.8_2_8_1, -7.4_4_5_3, -0.4_6_3_9, -8.0_6_2_5, -7.2_5_0_0, -8.0_0_0_0, -6.4_8_8_3, -7.7_6_9_5, -7.8_4_3_8, -7.0_3_1_2, -6.2_1_8_8, -7.1_3_2_8, -1.8_4_9_6, 1.9_9_6_1, -8.6_2_5_0, -6.7_2_2_7, -1_2.8_2_8_1, -6.9_4_9_2, -7.0_7_4_2, -7.7_8_5_2, -7.5_8_2_0, -7.9_0_6_2, -6.9_3_7_5, -7.9_8_0_5, -8.3_4_3_8, -8.1_5_6_2, -8.0_4_6_9, -7.6_2_5_0, -7.7_4_2_2, -7.3_3_9_8,]  )
										# fmt: on
										torch.testing.assert_close(out[0, 0, :30]   ,				_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 lowerCAmelCase__			(	self:						str  )				->						Tuple:
										__magic_name__					:       List[str]    							=							[1, 306, 4658, 278, 6593, 310, 2834, 338]
										__magic_name__					:       Union[str, Any]    							=							LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-hf"   ,				device_map="auto"  )
										__magic_name__					:       Optional[int]    							=							model(torch.tensor(_SCREAMING_SNAKE_CASE  )  )
										# Expected mean on dim = -1
										__magic_name__					:       Tuple    							=							torch.tensor([[-2.0_6_2_2, -1.2_7_9_4, -1.1_6_3_8, -0.9_7_8_8, -1.4_6_0_3, -1.0_2_3_8, -1.7_8_9_3, -1.4_4_1_1]]  )
										torch.testing.assert_close(out.mean(-1  )   ,				_SCREAMING_SNAKE_CASE   ,				atol=1E-2   ,				rtol=1E-2  )
										# slicing logits[0, 0, 0:30]
										# fmt: off
										__magic_name__					:       List[str]    							=							torch.tensor([-8.1_4_0_6, -8.0_5_4_7, 2.7_4_6_1, -1.2_3_4_4, -0.1_4_4_8, -1.8_2_6_2, -1.0_0_2_0, -1.8_1_5_4, -1.6_8_9_5, -1.8_5_1_6, -2.3_5_7_4, -0.9_2_7_7, 3.7_5_9_8, 6.5_7_4_2, -1.2_9_9_8, -0.1_1_7_7, -8.1_4_0_6, -2.9_6_8_8, -2.9_1_9_9, -3.1_6_9_9, -3.5_2_5_4, -2.3_5_5_5, -2.7_9_8_8, -3.4_1_4_1, -2.8_2_6_2, -4.5_1_9_5, -3.3_3_7_9, -3.3_1_6_4, -2.7_8_3_2, -3.0_2_7_3]  )
										# fmt: on
										torch.testing.assert_close(out[0, 0, :30]   ,				_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 lowerCAmelCase__			(	self:						Optional[Any]  )				->						List[str]:
										__magic_name__					:       str    							=							[1, 306, 4658, 278, 6593, 310, 2834, 338]
										__magic_name__					:       Union[str, Any]    							=							LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf"   ,				device_map="auto"  )
										__magic_name__					:       List[str]    							=							model(torch.tensor(_SCREAMING_SNAKE_CASE  )  )
										# Expected mean on dim = -1
										__magic_name__					:       Union[str, Any]    							=							torch.tensor([[-0.8_5_6_2, -1.8_5_2_0, -0.7_5_5_1, -0.4_1_6_2, -1.5_1_6_1, -1.2_0_3_8, -2.4_8_2_3, -2.3_2_5_4]]  )
										torch.testing.assert_close(out.mean(-1  )   ,				_SCREAMING_SNAKE_CASE   ,				atol=1E-2   ,				rtol=1E-2  )
										# slicing logits[0, 0, 0:30]
										# fmt: off
										__magic_name__					:       List[Any]    							=							torch.tensor([-2.2_2_2_7, 4.8_8_2_8, 0.9_0_2_3, -0.4_5_7_8, -0.7_8_7_1, -0.1_0_3_3, -0.6_2_2_1, -0.5_7_8_6, -0.7_8_0_3, -1.0_6_7_4, -1.2_9_2_0, -0.1_5_7_0, 0.8_0_0_8, 2.0_7_2_3, -0.9_4_9_7, 0.2_7_7_1, -2.2_2_2_7, -0.7_6_1_2, -1.4_3_4_6, -1.2_0_6_1, -1.6_4_2_6, -0.3_0_0_0, -0.7_1_3_9, -1.1_9_3_4, -1.8_6_9_1, -1.6_9_7_3, -1.5_9_4_7, -1.2_7_0_5, -0.3_5_2_3, -0.5_5_1_3]  )
										# 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 lowerCAmelCase__			(	self:						Dict  )				->						str:
										__magic_name__					:       str    							=							[1, 306, 4658, 278, 6593, 310, 2834, 338]
										__magic_name__					:       Any    							=							LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-70b-hf"   ,				device_map="auto"  )
										__magic_name__					:       Any    							=							model(torch.tensor(_SCREAMING_SNAKE_CASE  )  )

										__magic_name__					:       int    							=							torch.tensor(
										    [[-4.2_3_2_7, -3.3_3_6_0, -4.6_6_6_5, -4.7_6_3_1, -1.8_1_8_0, -3.4_1_7_0, -1.4_2_1_1, -3.1_8_1_0]]   ,				dtype=torch.floataa  )
										torch.testing.assert_close(out.mean(-1  )   ,				_SCREAMING_SNAKE_CASE   ,				atol=1E-2   ,				rtol=1E-2  )
										# fmt: off
										__magic_name__					:       Any    							=							torch.tensor([-9.4_9_2_2, -3.9_5_5_1, 1.7_9_9_8, -5.6_7_5_8, -5.1_0_5_5, -5.8_9_8_4, -4.8_3_2_0, -6.8_0_8_6, -6.5_3_9_1, -5.6_1_7_2, -5.5_8_2_0, -5.5_3_5_2, 1.7_8_8_1, 3.6_2_8_9, -6.5_1_1_7, -3.4_7_8_5, -9.5_0_0_0, -6.0_3_5_2, -6.8_1_2_5, -6.0_1_9_5, -6.6_8_3_6, -5.4_7_2_7, -6.2_8_1_2, -6.0_3_9_1, -7.3_3_9_8, -7.4_2_9_7, -7.4_8_4_4, -6.5_8_2_0, -5.8_7_8_9, -5.5_3_1_2]  )
										# fmt: on
										torch.testing.assert_close(out[0, 0, :30]   ,				_SCREAMING_SNAKE_CASE   ,				atol=1E-5   ,				rtol=1E-5  )







						@unittest.skip("Model is curently gated"  )
						@slow
						def lowerCAmelCase__			(	self:						int  )				->						Union[str, Any]:
										__magic_name__					:       Tuple    							=							"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"
										__magic_name__					:       List[str]    							=							"Simply put, the theory of relativity states that "
										__magic_name__					:       Tuple    							=							LlamaTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf"  )
										__magic_name__					:       List[str]    							=							tokenizer.encode(_SCREAMING_SNAKE_CASE   ,				return_tensors="pt"  )
										__magic_name__					:       Any    							=							LlamaForCausalLM.from_pretrained(
										    "meta-llama/Llama-2-13b-chat-hf"   ,				device_map="sequential"   ,				use_safetensors=_SCREAMING_SNAKE_CASE  )

										# greedy generation outputs
										__magic_name__					:       List[Any]    							=							model.generate(_SCREAMING_SNAKE_CASE   ,				max_new_tokens=64   ,				top_p=_SCREAMING_SNAKE_CASE   ,				temperature=1   ,				do_sample=_SCREAMING_SNAKE_CASE  )
										__magic_name__					:       Any    							=							tokenizer.decode(generated_ids[0]   ,				skip_special_tokens=_SCREAMING_SNAKE_CASE  )
										self.assertEqual(_SCREAMING_SNAKE_CASE   ,				_SCREAMING_SNAKE_CASE  )




"""simple docstring"""






import tempfile
import unittest
from pathlib import Path
from shutil import copyfile

from transformers import BatchEncoding, MarianTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, slow
from transformers.utils import is_sentencepiece_available, is_tf_available, is_torch_available


if is_sentencepiece_available():
	from transformers.models.marian.tokenization_marian import VOCAB_FILES_NAMES, save_json

from ...test_tokenization_common import TokenizerTesterMixin


__snake_case				:  List[Any] =       get_tests_dir('fixtures/test_sentencepiece.model')

__snake_case				:  Union[str, Any] =       {'target_lang': 'fi', 'source_lang': 'en'}
__snake_case				:  Union[str, Any] =       '>>zh<<'
__snake_case				:  List[str] =       'Helsinki-NLP/'

if is_torch_available():
	__snake_case				:  Optional[int] =       'pt'
elif is_tf_available():
	__snake_case				:  List[Any] =       'tf'
else:
	__snake_case				:  Union[str, Any] =       'jax'



@require_sentencepiece
class      A__  (    __SCREAMING_SNAKE_CASE    ,						unittest.TestCase				):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE        =      MarianTokenizer
	SCREAMING_SNAKE_CASE        =      False
	SCREAMING_SNAKE_CASE        =      True

	def 				_SCREAMING_SNAKE_CASE  (	self:						int)							->			Tuple:

				"""simple docstring"""

				super().setUp()
				__lowerCAmelCase :    Dict				=			["</s>", "<unk>", "▁This", "▁is", "▁a", "▁t", "est", "\u0120", "<pad>"]
				__lowerCAmelCase :    Union[str, Any]				=			dict(zip(_SCREAMING_SNAKE_CASE			,     range(len(_SCREAMING_SNAKE_CASE))))
				__lowerCAmelCase :    int				=			Path(self.tmpdirname)
				save_json(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["vocab"])
				save_json(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["tokenizer_config_file"])
				if not (save_dir / VOCAB_FILES_NAMES["source_spm"]).exists():
							copyfile(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["source_spm"])
							copyfile(_SCREAMING_SNAKE_CASE			,     save_dir / VOCAB_FILES_NAMES["target_spm"])

				__lowerCAmelCase :    Tuple				=			MarianTokenizer.from_pretrained(self.tmpdirname)
				tokenizer.save_pretrained(self.tmpdirname)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Dict			,     **_SCREAMING_SNAKE_CASE:						List[str])							->			MarianTokenizer:

				"""simple docstring"""

				return MarianTokenizer.from_pretrained(self.tmpdirname			,     **_SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						List[Any]			,     _SCREAMING_SNAKE_CASE:						List[str])							->			Dict:

				"""simple docstring"""

				return (
				    "This is a test",
				    "This is a test",
				)

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

				"""simple docstring"""

				__lowerCAmelCase :    Union[str, Any]				=			"</s>"
				__lowerCAmelCase :    int				=			0

				self.assertEqual(self.get_tokenizer()._convert_token_to_id(_SCREAMING_SNAKE_CASE)			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(self.get_tokenizer()._convert_id_to_token(_SCREAMING_SNAKE_CASE)			,     _SCREAMING_SNAKE_CASE)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			List[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			list(self.get_tokenizer().get_vocab().keys())

				self.assertEqual(vocab_keys[0]			,     "</s>")
				self.assertEqual(vocab_keys[1]			,     "<unk>")
				self.assertEqual(vocab_keys[-1]			,     "<pad>")
				self.assertEqual(len(_SCREAMING_SNAKE_CASE)			,     9)

	def 				_SCREAMING_SNAKE_CASE  (	self:						Tuple)							->			Dict:

				"""simple docstring"""

				self.assertEqual(self.get_tokenizer().vocab_size			,     9)

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

				"""simple docstring"""

				__lowerCAmelCase :    Optional[Any]				=			MarianTokenizer.from_pretrained(F"""{ORG_NAME}opus-mt-en-de""")
				__lowerCAmelCase :    int				=			en_de_tokenizer(["I am a small frog"]			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    Optional[int]				=			[38, 121, 14, 697, 3_8848, 0]
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     batch.input_ids[0])

				__lowerCAmelCase :    Tuple				=			tempfile.mkdtemp()
				en_de_tokenizer.save_pretrained(_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase :    List[Any]				=			[x.name for x in Path(_SCREAMING_SNAKE_CASE).glob("*")]
				self.assertIn("source.spm"			,     _SCREAMING_SNAKE_CASE)
				MarianTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE)

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

				"""simple docstring"""

				__lowerCAmelCase :    List[str]				=			self.get_tokenizer()

				__lowerCAmelCase :    int				=			tok(
				    ["I am a small frog" * 1000, "I am a small frog"]			,     padding=_SCREAMING_SNAKE_CASE			,     truncation=_SCREAMING_SNAKE_CASE			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(batch.input_ids.shape			,     (2, 512))

	def 				_SCREAMING_SNAKE_CASE  (	self:						List[Any])							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Any				=			self.get_tokenizer()
				__lowerCAmelCase :    str				=			tok(["I am a tiny frog", "I am a small frog"]			,     padding=_SCREAMING_SNAKE_CASE			,     return_tensors=_SCREAMING_SNAKE_CASE)
				self.assertIsInstance(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)
				self.assertEqual(batch_smaller.input_ids.shape			,     (2, 10))

	@slow
	def 				_SCREAMING_SNAKE_CASE  (	self:						Optional[Any])							->			Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase :    Optional[int]				=			{"input_ids": [[4_3495, 462, 20, 4_2164, 1369, 52, 464, 132, 1703, 492, 13, 7491, 3_8999, 6, 8, 464, 132, 1703, 492, 13, 4669, 3_7867, 13, 7525, 27, 1593, 988, 13, 3_3972, 7029, 6, 20, 8251, 383, 2, 270, 5866, 3788, 2, 2353, 8251, 1_2338, 2, 1_3958, 387, 2, 3629, 6953, 188, 2900, 2, 1_3958, 8011, 1_1501, 23, 8460, 4073, 3_4009, 20, 435, 1_1439, 27, 8, 8460, 4073, 6004, 20, 9988, 375, 27, 33, 266, 1945, 1076, 1350, 3_7867, 3288, 5, 577, 1076, 4374, 8, 5082, 5, 2_6453, 257, 556, 403, 2, 242, 132, 383, 316, 492, 8, 1_0767, 6, 316, 304, 4239, 3, 0], [148, 1_5722, 19, 1839, 12, 1350, 13, 2_2327, 5082, 5418, 4_7567, 3_5938, 59, 318, 1_9552, 108, 2183, 54, 1_4976, 4835, 32, 547, 1114, 8, 315, 2417, 5, 92, 1_9088, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100], [36, 6395, 1_2570, 3_9147, 1_1597, 6, 266, 4, 4_5405, 7296, 3, 0, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100, 5_8100]], "attention_mask": [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
				# fmt: on

				self.tokenizer_integration_test_util(
				    expected_encoding=_SCREAMING_SNAKE_CASE			,     model_name="Helsinki-NLP/opus-mt-en-de"			,     revision="1a8c2263da11e68e50938f97e10cd57820bd504c"			,     decode_kwargs={"use_source_tokenizer": True}			,     )







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

				"""simple docstring"""

				__lowerCAmelCase :    List[Any]				=			MarianTokenizer.from_pretrained("hf-internal-testing/test-marian-two-vocabs")

				__lowerCAmelCase :    List[str]				=			"Tämä on testi"
				__lowerCAmelCase :    int				=			"This is a test"

				__lowerCAmelCase :    Union[str, Any]				=			[76, 7, 2047, 2]
				__lowerCAmelCase :    Dict				=			[69, 12, 11, 940, 2]

				__lowerCAmelCase :    List[str]				=			tokenizer(_SCREAMING_SNAKE_CASE).input_ids
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    str				=			tokenizer(text_target=_SCREAMING_SNAKE_CASE).input_ids
				self.assertListEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase :    Dict				=			tokenizer.decode(_SCREAMING_SNAKE_CASE			,     skip_special_tokens=_SCREAMING_SNAKE_CASE)
				self.assertEqual(_SCREAMING_SNAKE_CASE			,     _SCREAMING_SNAKE_CASE)