
'''simple docstring'''


import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
					import torch

					from transformers import OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel

class 	__lowercase				:

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

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Any     =		ids_tensor([self.batch_size, self.seq_length]			,   self.vocab_size      )

								lowerCamelCase_					:      List[str]     =		None
								if self.use_input_mask:
													lowerCamelCase_					:      int     =		random_attention_mask([self.batch_size, self.seq_length]      )

								lowerCamelCase_					:      Dict     =		None
								if self.use_token_type_ids:
													lowerCamelCase_					:      Optional[Any]     =		ids_tensor([self.batch_size, self.seq_length]			,   self.type_vocab_size      )

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

								lowerCamelCase_					:      List[str]     =		self.get_config()

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

			def        UpperCAmelCase__ (self      ):
								return OpenLlamaConfig(
								    vocab_size=self.vocab_size			,   hidden_size=self.hidden_size			,   num_hidden_layers=self.num_hidden_layers			,   num_attention_heads=self.num_attention_heads			,   intermediate_size=self.intermediate_size			,   hidden_act=self.hidden_act			,   hidden_dropout_prob=self.hidden_dropout_prob			,   attention_probs_dropout_prob=self.attention_probs_dropout_prob			,   max_position_embeddings=self.max_position_embeddings			,   type_vocab_size=self.type_vocab_size			,   is_decoder=A			,   initializer_range=self.initializer_range			,   use_stable_embedding=A			,   )

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A			,   A      ):
								lowerCamelCase_					:      List[Any]     =		OpenLlamaModel(config=A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      Tuple     =		model(A			,   attention_mask=A      )
								lowerCamelCase_					:      Optional[Any]     =		model(A      )
								self.parent.assertEqual(result.last_hidden_state.shape			,   (self.batch_size, self.seq_length, self.hidden_size)      )

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   ):
								lowerCamelCase_					:      int     =		True
								lowerCamelCase_					:      Dict     =		OpenLlamaModel(A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      Dict     =		model(
								    A			,   attention_mask=A			,   encoder_hidden_states=A			,   encoder_attention_mask=A			,   )
								lowerCamelCase_					:      Optional[Any]     =		model(
								    A			,   attention_mask=A			,   encoder_hidden_states=A			,   )
								lowerCamelCase_					:      Optional[int]     =		model(A			,   attention_mask=A      )
								self.parent.assertEqual(result.last_hidden_state.shape			,   (self.batch_size, self.seq_length, self.hidden_size)      )

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   ):
								lowerCamelCase_					:      int     =		OpenLlamaForCausalLM(config=A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      Dict     =		model(A			,   attention_mask=A			,   labels=A      )
								self.parent.assertEqual(result.logits.shape			,   (self.batch_size, self.seq_length, self.vocab_size)      )

			def        UpperCAmelCase__ (self			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   A			,   ):
								lowerCamelCase_					:      Dict     =		True
								lowerCamelCase_					:      List[Any]     =		True
								lowerCamelCase_					:      str     =		OpenLlamaForCausalLM(config=A      )
								model.to(A      )
								model.eval()

								# first forward pass
								lowerCamelCase_					:      int     =		model(
								    A			,   attention_mask=A			,   encoder_hidden_states=A			,   encoder_attention_mask=A			,   use_cache=A			,   )
								lowerCamelCase_					:      Tuple     =		outputs.past_key_values

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

								# append to next input_ids and
								lowerCamelCase_					:      Dict     =		torch.cat([input_ids, next_tokens]			,   dim=-1      )
								lowerCamelCase_					:      Optional[int]     =		torch.cat([input_mask, next_mask]			,   dim=-1      )

								lowerCamelCase_					:      int     =		model(
								    A			,   attention_mask=A			,   encoder_hidden_states=A			,   encoder_attention_mask=A			,   output_hidden_states=A			,   )['''hidden_states'''][0]
								lowerCamelCase_					:      Tuple     =		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
								lowerCamelCase_					:      Dict     =		ids_tensor((1,)			,   output_from_past.shape[-1]      ).item()
								lowerCamelCase_					:      Union[str, Any]     =		output_from_no_past[:, -3:, random_slice_idx].detach()
								lowerCamelCase_					:      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(A			,   A			,   atol=1E-3      )      )






			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Dict     =		self.prepare_config_and_inputs()
								(
								    lowerCamelCase_      
								)					:      Any     =		config_and_inputs
								lowerCamelCase_					:      Any     =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict

@require_torch
class 	__lowercase				(  _lowercase      ,     _lowercase      ,     _lowercase      ,     unittest.TestCase    ):
			lowerCamelCase			:		List[Any]	      =			(
			    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
			)
			lowerCamelCase			:		List[Any]	      =			(OpenLlamaForCausalLM,) if is_torch_available() else ()
			lowerCamelCase			:		str	      =			(
			    {
			        "feature-extraction": OpenLlamaModel,
			        "text-classification": OpenLlamaForSequenceClassification,
			        "text-generation": OpenLlamaForCausalLM,
			        "zero-shot": OpenLlamaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase			:		List[str]	      =			False
			lowerCamelCase			:		int	      =			False

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      List[str]     =		OpenLlamaModelTester(self      )
								lowerCamelCase_					:      List[str]     =		ConfigTester(self			,   config_class=A			,   hidden_size=3_7      )

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

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Dict     =		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*A      )

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

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase_					:      str     =		3
								lowerCamelCase_					:      Optional[int]     =		input_dict['''input_ids''']
								lowerCamelCase_					:      int     =		input_ids.ne(1      ).to(A      )
								lowerCamelCase_					:      Union[str, Any]     =		ids_tensor([self.model_tester.batch_size]			,   self.model_tester.type_sequence_label_size      )
								lowerCamelCase_					:      Optional[Any]     =		OpenLlamaForSequenceClassification(A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      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        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase_					:      Union[str, Any]     =		3
								lowerCamelCase_					:      Union[str, Any]     =		'''single_label_classification'''
								lowerCamelCase_					:      List[Any]     =		input_dict['''input_ids''']
								lowerCamelCase_					:      str     =		input_ids.ne(1      ).to(A      )
								lowerCamelCase_					:      str     =		ids_tensor([self.model_tester.batch_size]			,   self.model_tester.type_sequence_label_size      )
								lowerCamelCase_					:      Optional[int]     =		OpenLlamaForSequenceClassification(A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      int     =		model(A			,   attention_mask=A			,   labels=A      )
								self.assertEqual(result.logits.shape			,   (self.model_tester.batch_size, self.model_tester.num_labels)      )

			def        UpperCAmelCase__ (self      ):
								lowerCamelCase_					:      Optional[Any]     =		self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase_					:      Optional[Any]     =		3
								lowerCamelCase_					:      str     =		'''multi_label_classification'''
								lowerCamelCase_					:      Union[str, Any]     =		input_dict['''input_ids''']
								lowerCamelCase_					:      Any     =		input_ids.ne(1      ).to(A      )
								lowerCamelCase_					:      int     =		ids_tensor(
								    [self.model_tester.batch_size, config.num_labels]			,   self.model_tester.type_sequence_label_size      ).to(torch.float      )
								lowerCamelCase_					:      Tuple     =		OpenLlamaForSequenceClassification(A      )
								model.to(A      )
								model.eval()
								lowerCamelCase_					:      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('''Open-Llama buffers include complex numbers, which breaks this test'''      )
			def        UpperCAmelCase__ (self      ):
								pass






			@parameterized.expand([('''linear''',), ('''dynamic''',)]      )
			def        UpperCAmelCase__ (self			,   A      ):
								lowerCamelCase_					:      Any     =		self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase_					:      Any     =		ids_tensor([1, 1_0]			,   config.vocab_size      )
								lowerCamelCase_					:      List[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
								lowerCamelCase_					:      Optional[Any]     =		OpenLlamaModel(A      )
								original_model.to(A      )
								original_model.eval()
								lowerCamelCase_					:      str     =		original_model(A      ).last_hidden_state
								lowerCamelCase_					:      List[Any]     =		original_model(A      ).last_hidden_state

								set_seed(4_2      )  # Fixed seed at init time so the two models get the same random weights
								lowerCamelCase_					:      int     =		{'''type''': scaling_type, '''factor''': 10.0}
								lowerCamelCase_					:      Optional[Any]     =		OpenLlamaModel(A      )
								scaled_model.to(A      )
								scaled_model.eval()
								lowerCamelCase_					:      Optional[Any]     =		scaled_model(A      ).last_hidden_state
								lowerCamelCase_					:      Any     =		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      )      )





'''simple docstring'''


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_sentencepiece_available


__lowercase							:   List[Any]					=     {}


try:
					if not is_sentencepiece_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					__lowercase							:   List[Any]					=     ['''MLukeTokenizer''']

if TYPE_CHECKING:
					try:
										if not is_sentencepiece_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .tokenization_mluke import MLukeTokenizer


else:
					import sys

					__lowercase							:   List[str]					=     _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


