





"""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 					__UpperCAmelCase							:
				def __init__(		self    :			Optional[Any]	, a_    :			Optional[int]	, a_    :			Optional[int]=13	, a_    :			Optional[Any]=7	, a_    :			str=True	, a_    :			Dict=True	, a_    :			int=False	, a_    :			Optional[Any]=True	, a_    :			Optional[Any]=99	, a_    :			Dict=32	, a_    :			List[Any]=5	, a_    :			int=4	, a_    :			List[str]=37	, a_    :			Optional[Any]="gelu"	, a_    :			str=0.1	, a_    :			Optional[int]=0.1	, a_    :			int=5_12	, a_    :			Union[str, Any]=16	, a_    :			Optional[Any]=2	, a_    :			Tuple=0.02	, a_    :			Dict=3	, a_    :			List[str]=4	, a_    :			List[str]=None	, )	->	List[str]:



							'''simple docstring'''



							a__  :	List[str]		 =			parent
							a__  :	Tuple		 =			batch_size
							a__  :	str		 =			seq_length
							a__  :	str		 =			is_training
							a__  :	Optional[Any]		 =			use_input_mask
							a__  :	Tuple		 =			use_token_type_ids
							a__  :	Tuple		 =			use_labels
							a__  :	Optional[int]		 =			vocab_size
							a__  :	List[Any]		 =			hidden_size
							a__  :	Optional[Any]		 =			num_hidden_layers
							a__  :	str		 =			num_attention_heads
							a__  :	Optional[int]		 =			intermediate_size
							a__  :	str		 =			hidden_act
							a__  :	Union[str, Any]		 =			hidden_dropout_prob
							a__  :	Dict		 =			attention_probs_dropout_prob
							a__  :	Optional[Any]		 =			max_position_embeddings
							a__  :	Union[str, Any]		 =			type_vocab_size
							a__  :	List[str]		 =			type_sequence_label_size
							a__  :	List[str]		 =			initializer_range
							a__  :	Tuple		 =			num_labels
							a__  :	Union[str, Any]		 =			num_choices
							a__  :	Tuple		 =			scope
				def       UpperCAmelCase      (		self    :			Dict    )	->	List[str]:



							'''simple docstring'''



							a__  :	Any		 =			ids_tensor([self.batch_size, self.seq_length]	, self.vocab_size    )

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

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

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

							a__  :	Optional[Any]		 =			self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
				def       UpperCAmelCase      (		self    :			Any    )	->	str:



							'''simple docstring'''



							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    :			List[str]	, a_    :			List[str]	, a_    :			List[Any]	, a_    :			List[str]	, a_    :			int	, a_    :			Tuple	, a_    :			List[str]	, a_    :			Optional[Any]    )	->	List[str]:



							'''simple docstring'''



							a__  :	int		 =			OpenLlamaModel(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	Dict		 =			model(a_	, attention_mask=a_    )
							a__  :	int		 =			model(a_    )
							self.parent.assertEqual(result.last_hidden_state.shape	, (self.batch_size, self.seq_length, self.hidden_size)    )
				def       UpperCAmelCase      (		self    :			Tuple	, a_    :			Dict	, a_    :			Optional[Any]	, a_    :			Optional[Any]	, a_    :			Union[str, Any]	, a_    :			Optional[int]	, a_    :			Tuple	, a_    :			Union[str, Any]	, a_    :			Union[str, Any]	, a_    :			Optional[int]	, )	->	Union[str, Any]:



							'''simple docstring'''



							a__  :	Optional[int]		 =			True
							a__  :	Tuple		 =			OpenLlamaModel(a_    )
							model.to(a_    )
							model.eval()
							a__  :	Optional[int]		 =			model(
							    a_	, attention_mask=a_	, encoder_hidden_states=a_	, encoder_attention_mask=a_	, )
							a__  :	List[Any]		 =			model(
							    a_	, attention_mask=a_	, encoder_hidden_states=a_	, )
							a__  :	Tuple		 =			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    :			Optional[int]	, a_    :			Optional[int]	, a_    :			Any	, a_    :			str	, a_    :			Tuple	, a_    :			List[str]	, a_    :			Optional[int]	, a_    :			str	, a_    :			str	, a_    :			Tuple	, )	->	str:



							'''simple docstring'''



							a__  :	str		 =			OpenLlamaForCausalLM(config=a_    )
							model.to(a_    )
							model.eval()
							a__  :	int		 =			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    :			Optional[Any]	, a_    :			Union[str, Any]	, a_    :			Dict	, a_    :			Dict	, a_    :			int	, a_    :			Any	, a_    :			Union[str, Any]	, a_    :			Union[str, Any]	, a_    :			Optional[Any]	, a_    :			Tuple	, )	->	List[str]:



							'''simple docstring'''



							a__  :	Any		 =			True
							a__  :	Optional[int]		 =			True
							a__  :	str		 =			OpenLlamaForCausalLM(config=a_    )
							model.to(a_    )
							model.eval()

							# first forward pass
							a__  :	Optional[int]		 =			model(
							    a_	, attention_mask=a_	, encoder_hidden_states=a_	, encoder_attention_mask=a_	, use_cache=a_	, )
							a__  :	Union[str, Any]		 =			outputs.past_key_values

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

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

							a__  :	Dict		 =			model(
							    a_	, attention_mask=a_	, encoder_hidden_states=a_	, encoder_attention_mask=a_	, output_hidden_states=a_	, )["hidden_states"][0]
							a__  :	List[str]		 =			model(
							    a_	, attention_mask=a_	, encoder_hidden_states=a_	, encoder_attention_mask=a_	, past_key_values=a_	, output_hidden_states=a_	, )["hidden_states"][0]

							# select random slice
							a__  :	Any		 =			ids_tensor((1,)	, output_from_past.shape[-1]    ).item()
							a__  :	Any		 =			output_from_no_past[:, -3:, random_slice_idx].detach()
							a__  :	str		 =			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    :			List[str]    )	->	Tuple:



							'''simple docstring'''



							a__  :	Any		 =			self.prepare_config_and_inputs()
							(
							    (
							    a__
							) ,	(
							    a__
							) ,	(
							    a__
							) ,	(
							    a__
							) ,	(
							    a__
							) ,	(
							    a__
							) ,	(
							    a__
							) ,	
							)  :	int		 =			config_and_inputs
							a__  :	List[str]		 =			{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict



@require_torch
class 					__UpperCAmelCase							(						_UpperCamelCase						,  _UpperCamelCase						,  _UpperCamelCase						,  unittest.TestCase  ):
				__lowerCamelCase				:	Any				     =				(
				    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
				)
				__lowerCamelCase				:	Any				     =				(OpenLlamaForCausalLM,) if is_torch_available() else ()
				__lowerCamelCase				:	Optional[int]				     =				(
				    {
				        "feature-extraction": OpenLlamaModel,
				        "text-classification": OpenLlamaForSequenceClassification,
				        "text-generation": OpenLlamaForCausalLM,
				        "zero-shot": OpenLlamaForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				__lowerCamelCase				:	Tuple				     =				False
				__lowerCamelCase				:	Optional[int]				     =				False
				def       UpperCAmelCase      (		self    :			Union[str, Any]    )	->	Dict:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			OpenLlamaModelTester(self    )
							a__  :	Dict		 =			ConfigTester(self	, config_class=a_	, hidden_size=37    )
				def       UpperCAmelCase      (		self    :			Optional[int]    )	->	Tuple:



							'''simple docstring'''



							self.config_tester.run_common_tests()
				def       UpperCAmelCase      (		self    :			Any    )	->	Any:



							'''simple docstring'''



							a__  :	Optional[Any]		 =			self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*a_    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	int:



							'''simple docstring'''



							a__  :	Union[str, Any]		 =			self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
										a__  :	int		 =			type
										self.model_tester.create_and_check_model(*a_    )
				def       UpperCAmelCase      (		self    :			Dict    )	->	Any:



							'''simple docstring'''



							a__ ,	a__  :	int		 =			self.model_tester.prepare_config_and_inputs_for_common()
							a__  :	Tuple		 =			3
							a__  :	Tuple		 =			input_dict["input_ids"]
							a__  :	Dict		 =			input_ids.ne(1    ).to(a_    )
							a__  :	Tuple		 =			ids_tensor([self.model_tester.batch_size]	, self.model_tester.type_sequence_label_size    )
							a__  :	Optional[Any]		 =			OpenLlamaForSequenceClassification(a_    )
							model.to(a_    )
							model.eval()
							a__  :	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    :			Dict    )	->	Union[str, Any]:



							'''simple docstring'''



							a__ ,	a__  :	int		 =			self.model_tester.prepare_config_and_inputs_for_common()
							a__  :	Tuple		 =			3
							a__  :	int		 =			"single_label_classification"
							a__  :	Optional[int]		 =			input_dict["input_ids"]
							a__  :	int		 =			input_ids.ne(1    ).to(a_    )
							a__  :	int		 =			ids_tensor([self.model_tester.batch_size]	, self.model_tester.type_sequence_label_size    )
							a__  :	str		 =			OpenLlamaForSequenceClassification(a_    )
							model.to(a_    )
							model.eval()
							a__  :	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    :			List[str]    )	->	Optional[int]:



							'''simple docstring'''



							a__ ,	a__  :	Any		 =			self.model_tester.prepare_config_and_inputs_for_common()
							a__  :	Union[str, Any]		 =			3
							a__  :	Dict		 =			"multi_label_classification"
							a__  :	Optional[int]		 =			input_dict["input_ids"]
							a__  :	int		 =			input_ids.ne(1    ).to(a_    )
							a__  :	Optional[Any]		 =			ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]	, self.model_tester.type_sequence_label_size    ).to(torch.float    )
							a__  :	List[Any]		 =			OpenLlamaForSequenceClassification(a_    )
							model.to(a_    )
							model.eval()
							a__  :	Dict		 =			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    :			Any    )	->	Any:



							'''simple docstring'''



							pass






				@parameterized.expand([("linear",), ("dynamic",)]    )
				def       UpperCAmelCase      (		self    :			Optional[int]	, a_    :			int    )	->	int:



							'''simple docstring'''



							a__ ,	a__  :	Optional[int]		 =			self.model_tester.prepare_config_and_inputs_for_common()
							a__  :	List[Any]		 =			ids_tensor([1, 10]	, config.vocab_size    )
							a__  :	List[str]		 =			ids_tensor([1, int(config.max_position_embeddings * 1.5    )]	, config.vocab_size    )

							set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
							a__  :	Tuple		 =			OpenLlamaModel(a_    )
							original_model.to(a_    )
							original_model.eval()
							a__  :	int		 =			original_model(a_    ).last_hidden_state
							a__  :	Tuple		 =			original_model(a_    ).last_hidden_state

							set_seed(42    )  # Fixed seed at init time so the two models get the same random weights
							a__  :	Any		 =			{"type": scaling_type, "factor": 10.0}
							a__  :	Any		 =			OpenLlamaModel(a_    )
							scaled_model.to(a_    )
							scaled_model.eval()
							a__  :	List[str]		 =			scaled_model(a_    ).last_hidden_state
							a__  :	Optional[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 maths.is_square_free import is_square_free
from maths.prime_factors import prime_factors






def      lowercase__ (					lowerCAmelCase__   :			int			)    ->			int:



			'''simple docstring'''
			a__  :	Tuple		 =			prime_factors(lowerCAmelCase__			)
			if is_square_free(lowerCAmelCase__			):
						return -1 if len(lowerCAmelCase__			) % 2 else 1
			return 0


if __name__ == "__main__":
		import doctest

		doctest.testmod()