
"""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		,			UpperCAmelCase_		,			UpperCAmelCase_=13		,			UpperCAmelCase_=7		,			UpperCAmelCase_=True		,			UpperCAmelCase_=True		,			UpperCAmelCase_=False		,			UpperCAmelCase_=True		,			UpperCAmelCase_=99		,			UpperCAmelCase_=32		,			UpperCAmelCase_=5		,			UpperCAmelCase_=4		,			UpperCAmelCase_=37		,			UpperCAmelCase_="gelu"		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=0.1		,			UpperCAmelCase_=512		,			UpperCAmelCase_=16		,			UpperCAmelCase_=2		,			UpperCAmelCase_=0.02		,			UpperCAmelCase_=3		,			UpperCAmelCase_=4		,			UpperCAmelCase_=None		,			)		->							Tuple:
			lowerCamelCase					:      Optional[int]          =   parent
			lowerCamelCase					:      Dict          =   batch_size
			lowerCamelCase					:      Tuple          =   seq_length
			lowerCamelCase					:      str          =   is_training
			lowerCamelCase					:      int          =   use_input_mask
			lowerCamelCase					:      int          =   use_token_type_ids
			lowerCamelCase					:      Optional[Any]          =   use_labels
			lowerCamelCase					:      Union[str, Any]          =   vocab_size
			lowerCamelCase					:      str          =   hidden_size
			lowerCamelCase					:      Optional[int]          =   num_hidden_layers
			lowerCamelCase					:      List[Any]          =   num_attention_heads
			lowerCamelCase					:      int          =   intermediate_size
			lowerCamelCase					:      Union[str, Any]          =   hidden_act
			lowerCamelCase					:      Optional[int]          =   hidden_dropout_prob
			lowerCamelCase					:      int          =   attention_probs_dropout_prob
			lowerCamelCase					:      int          =   max_position_embeddings
			lowerCamelCase					:      str          =   type_vocab_size
			lowerCamelCase					:      Any          =   type_sequence_label_size
			lowerCamelCase					:      Optional[int]          =   initializer_range
			lowerCamelCase					:      Optional[int]          =   num_labels
			lowerCamelCase					:      int          =   num_choices
			lowerCamelCase					:      Tuple          =   scope




	def       _UpperCamelCase   (			self )		->							Union[str, Any]:
			lowerCamelCase					:      Any          =   ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size )

			lowerCamelCase					:      Optional[int]          =   None
			if self.use_input_mask:
					lowerCamelCase					:      Dict          =   random_attention_mask([self.batch_size, self.seq_length] )

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

			lowerCamelCase					:      Tuple          =   None
			lowerCamelCase					:      List[str]          =   None
			lowerCamelCase					:      List[Any]          =   None
			if self.use_labels:
					lowerCamelCase					:      Union[str, Any]          =   ids_tensor([self.batch_size]		,			self.type_sequence_label_size )
					lowerCamelCase					:      Any          =   ids_tensor([self.batch_size, self.seq_length]		,			self.num_labels )
					lowerCamelCase					:      Optional[Any]          =   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 )		->							Any:
			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=UpperCAmelCase_		,			initializer_range=self.initializer_range		,			use_stable_embedding=UpperCAmelCase_		,			)




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_ )		->							Any:
			lowerCamelCase					:      int          =   OpenLlamaModel(config=UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      Any          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_ )
			lowerCamelCase					:      str          =   model(UpperCAmelCase_ )
			self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size) )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			)		->							Dict:
			lowerCamelCase					:      Tuple          =   True
			lowerCamelCase					:      List[str]          =   OpenLlamaModel(UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      List[Any]          =   model(
			    UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			encoder_hidden_states=UpperCAmelCase_		,			encoder_attention_mask=UpperCAmelCase_		,			)
			lowerCamelCase					:      str          =   model(
			    UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			encoder_hidden_states=UpperCAmelCase_		,			)
			lowerCamelCase					:      List[str]          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_ )
			self.parent.assertEqual(result.last_hidden_state.shape		,			(self.batch_size, self.seq_length, self.hidden_size) )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			)		->							List[Any]:
			lowerCamelCase					:      str          =   OpenLlamaForCausalLM(config=UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      Optional[Any]          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			self.parent.assertEqual(result.logits.shape		,			(self.batch_size, self.seq_length, self.vocab_size) )




	def       _UpperCamelCase   (			self		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			UpperCAmelCase_		,			)		->							Tuple:
			lowerCamelCase					:      List[Any]          =   True
			lowerCamelCase					:      Optional[int]          =   True
			lowerCamelCase					:      str          =   OpenLlamaForCausalLM(config=UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()

			# first forward pass
			lowerCamelCase					:      str          =   model(
			    UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			encoder_hidden_states=UpperCAmelCase_		,			encoder_attention_mask=UpperCAmelCase_		,			use_cache=UpperCAmelCase_		,			)
			lowerCamelCase					:      Any          =   outputs.past_key_values

			# create hypothetical multiple next token and extent to next_input_ids
			lowerCamelCase					:      Any          =   ids_tensor((self.batch_size, 3)		,			config.vocab_size )
			lowerCamelCase					:      Optional[int]          =   ids_tensor((self.batch_size, 3)		,			vocab_size=2 )

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

			lowerCamelCase					:      Optional[Any]          =   model(
			    UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			encoder_hidden_states=UpperCAmelCase_		,			encoder_attention_mask=UpperCAmelCase_		,			output_hidden_states=UpperCAmelCase_		,			)['hidden_states'][0]
			lowerCamelCase					:      Any          =   model(
			    UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			encoder_hidden_states=UpperCAmelCase_		,			encoder_attention_mask=UpperCAmelCase_		,			past_key_values=UpperCAmelCase_		,			output_hidden_states=UpperCAmelCase_		,			)['hidden_states'][0]

			# select random slice
			lowerCamelCase					:      Optional[Any]          =   ids_tensor((1,)		,			output_from_past.shape[-1] ).item()
			lowerCamelCase					:      int          =   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(UpperCAmelCase_		,			UpperCAmelCase_		,			atol=1E-3 ) )




	def       _UpperCamelCase   (			self )		->							Optional[int]:
			lowerCamelCase					:      Tuple          =   self.prepare_config_and_inputs()
			(
			    (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   (
			    lowerCamelCase
			)     ,   
			)					:      Union[str, Any]          =   config_and_inputs
			lowerCamelCase					:      Union[str, Any]          =   {'input_ids': input_ids, 'attention_mask': input_mask}
			return config, inputs_dict






@require_torch
class 	_lowercase       (    __UpperCAmelCase    ,	__UpperCAmelCase    ,	__UpperCAmelCase    ,	unittest.TestCase		):
	lowercase_				=     (
	    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
	)
	lowercase_				=     (OpenLlamaForCausalLM,) if is_torch_available() else ()
	lowercase_				=     (
	    {
	        'feature-extraction': OpenLlamaModel,
	        'text-classification': OpenLlamaForSequenceClassification,
	        'text-generation': OpenLlamaForCausalLM,
	        'zero-shot': OpenLlamaForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowercase_				=     False
	lowercase_				=     False




	def       _UpperCamelCase   (			self )		->							Optional[Any]:
			lowerCamelCase					:      str          =   OpenLlamaModelTester(self )
			lowerCamelCase					:      Any          =   ConfigTester(self		,			config_class=UpperCAmelCase_		,			hidden_size=37 )




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




	def       _UpperCamelCase   (			self )		->							Union[str, Any]:
			lowerCamelCase					:      Dict          =   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*UpperCAmelCase_ )




	def       _UpperCamelCase   (			self )		->							List[Any]:
			lowerCamelCase					:      List[str]          =   self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					lowerCamelCase					:      str          =   type
					self.model_tester.create_and_check_model(*UpperCAmelCase_ )




	def       _UpperCamelCase   (			self )		->							int:
			lowerCamelCase     ,   lowerCamelCase					:      Union[str, Any]          =   self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase					:      str          =   3
			lowerCamelCase					:      Optional[int]          =   input_dict['input_ids']
			lowerCamelCase					:      Optional[int]          =   input_ids.ne(1 ).to(UpperCAmelCase_ )
			lowerCamelCase					:      List[str]          =   ids_tensor([self.model_tester.batch_size]		,			self.model_tester.type_sequence_label_size )
			lowerCamelCase					:      List[str]          =   OpenLlamaForSequenceClassification(UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      Union[str, Any]          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			self.assertEqual(result.logits.shape		,			(self.model_tester.batch_size, self.model_tester.num_labels) )




	def       _UpperCamelCase   (			self )		->							int:
			lowerCamelCase     ,   lowerCamelCase					:      Optional[Any]          =   self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase					:      Tuple          =   3
			lowerCamelCase					:      List[str]          =   'single_label_classification'
			lowerCamelCase					:      List[Any]          =   input_dict['input_ids']
			lowerCamelCase					:      Optional[int]          =   input_ids.ne(1 ).to(UpperCAmelCase_ )
			lowerCamelCase					:      Tuple          =   ids_tensor([self.model_tester.batch_size]		,			self.model_tester.type_sequence_label_size )
			lowerCamelCase					:      str          =   OpenLlamaForSequenceClassification(UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      Tuple          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			self.assertEqual(result.logits.shape		,			(self.model_tester.batch_size, self.model_tester.num_labels) )




	def       _UpperCamelCase   (			self )		->							Dict:
			lowerCamelCase     ,   lowerCamelCase					:      List[str]          =   self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase					:      int          =   3
			lowerCamelCase					:      Dict          =   'multi_label_classification'
			lowerCamelCase					:      Optional[Any]          =   input_dict['input_ids']
			lowerCamelCase					:      str          =   input_ids.ne(1 ).to(UpperCAmelCase_ )
			lowerCamelCase					:      Optional[Any]          =   ids_tensor(
			    [self.model_tester.batch_size, config.num_labels]		,			self.model_tester.type_sequence_label_size ).to(torch.float )
			lowerCamelCase					:      str          =   OpenLlamaForSequenceClassification(UpperCAmelCase_ )
			model.to(UpperCAmelCase_ )
			model.eval()
			lowerCamelCase					:      Union[str, Any]          =   model(UpperCAmelCase_		,			attention_mask=UpperCAmelCase_		,			labels=UpperCAmelCase_ )
			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 )		->							Dict:
			pass




	@parameterized.expand([('linear',), ('dynamic',)] )
	def       _UpperCamelCase   (			self		,			UpperCAmelCase_ )		->							Dict:
			lowerCamelCase     ,   lowerCamelCase					:      Tuple          =   self.model_tester.prepare_config_and_inputs_for_common()
			lowerCamelCase					:      List[str]          =   ids_tensor([1, 10]		,			config.vocab_size )
			lowerCamelCase					:      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
			lowerCamelCase					:      List[str]          =   OpenLlamaModel(UpperCAmelCase_ )
			original_model.to(UpperCAmelCase_ )
			original_model.eval()
			lowerCamelCase					:      Optional[int]          =   original_model(UpperCAmelCase_ ).last_hidden_state
			lowerCamelCase					:      Optional[Any]          =   original_model(UpperCAmelCase_ ).last_hidden_state

			set_seed(42 )  # Fixed seed at init time so the two models get the same random weights
			lowerCamelCase					:      List[Any]          =   {'type': scaling_type, 'factor': 10.0}
			lowerCamelCase					:      str          =   OpenLlamaModel(UpperCAmelCase_ )
			scaled_model.to(UpperCAmelCase_ )
			scaled_model.eval()
			lowerCamelCase					:      str          =   scaled_model(UpperCAmelCase_ ).last_hidden_state
			lowerCamelCase					:      Optional[int]          =   scaled_model(UpperCAmelCase_ ).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(UpperCAmelCase_		,			UpperCAmelCase_		,			atol=1E-5 ) )
			else:
					self.assertFalse(torch.allclose(UpperCAmelCase_		,			UpperCAmelCase_		,			atol=1E-5 ) )

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








"""simple docstring"""

def 		UpperCAmelCase (		a_,						a_      ):





		'''simple docstring'''


		while b:
				lowerCamelCase     ,   lowerCamelCase					:      Tuple          =   b, a % b
		return a
def 		UpperCAmelCase (		a_,						a_      ):





		'''simple docstring'''


		return a if b == 0 else euclidean_gcd_recursive(a_,						a % b      )
def 		UpperCAmelCase (		):





		'''simple docstring'''


		print(F"""euclidean_gcd(3, 5) = {euclidean_gcd(3,						5      )}"""      )
		print(F"""euclidean_gcd(5, 3) = {euclidean_gcd(5,						3      )}"""      )
		print(F"""euclidean_gcd(1, 3) = {euclidean_gcd(1,						3      )}"""      )
		print(F"""euclidean_gcd(3, 6) = {euclidean_gcd(3,						6      )}"""      )
		print(F"""euclidean_gcd(6, 3) = {euclidean_gcd(6,						3      )}"""      )

		print(F"""euclidean_gcd_recursive(3, 5) = {euclidean_gcd_recursive(3,						5      )}"""      )
		print(F"""euclidean_gcd_recursive(5, 3) = {euclidean_gcd_recursive(5,						3      )}"""      )
		print(F"""euclidean_gcd_recursive(1, 3) = {euclidean_gcd_recursive(1,						3      )}"""      )
		print(F"""euclidean_gcd_recursive(3, 6) = {euclidean_gcd_recursive(3,						6      )}"""      )
		print(F"""euclidean_gcd_recursive(6, 3) = {euclidean_gcd_recursive(6,						3      )}"""      )


if __name__ == "__main__":
					main()






