



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


					'''simple docstring'''





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



					def 	snake_case ( self							:						List[str]  )->      List[Any]:
								lowerCamelCase__	:	Union[str, Any]				=ids_tensor([self.batch_size, self.seq_length],						self.vocab_size  )

								lowerCamelCase__	:	Union[str, Any]				=None
								if self.use_input_mask:
											lowerCamelCase__	:	List[str]				=random_attention_mask([self.batch_size, self.seq_length]  )

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

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

								lowerCamelCase__	:	Union[str, Any]				=self.get_config()

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



					def 	snake_case ( self							:						str  )->      int:
								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=lowerCamelCase,						initializer_range=self.initializer_range,						use_stable_embedding=lowerCamelCase,						)



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



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



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



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

								# first forward pass
								lowerCamelCase__	:	Optional[int]				=model(
								    lowerCamelCase,						attention_mask=lowerCamelCase,						encoder_hidden_states=lowerCamelCase,						encoder_attention_mask=lowerCamelCase,						use_cache=lowerCamelCase,						)
								lowerCamelCase__	:	Any				=outputs.past_key_values

								# create hypothetical multiple next token and extent to next_input_ids
								lowerCamelCase__	:	List[Any]				=ids_tensor((self.batch_size, 3),						config.vocab_size  )
								lowerCamelCase__	:	Any				=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__	:	str				=torch.cat([input_mask, next_mask],						dim=-1  )

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

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




					def 	snake_case ( self							:						List[Any]  )->      List[Any]:
								lowerCamelCase__	:	str				=self.prepare_config_and_inputs()
								(
								    (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     (
								    lowerCamelCase__
								)			,     
								)	:	Any				=config_and_inputs
								lowerCamelCase__	:	Optional[Any]				={'''input_ids''': input_ids, '''attention_mask''': input_mask}
								return config, inputs_dict





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


					'''simple docstring'''





					_a         =							(
					    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
					)
					_a         =							(OpenLlamaForCausalLM,) if is_torch_available() else ()
					_a         =							(
					    {
					        'feature-extraction': OpenLlamaModel,
					        'text-classification': OpenLlamaForSequenceClassification,
					        'text-generation': OpenLlamaForCausalLM,
					        'zero-shot': OpenLlamaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					_a         =							False
					_a         =							False



					def 	snake_case ( self							:						Any  )->      Optional[Any]:
								lowerCamelCase__	:	Any				=OpenLlamaModelTester(self  )
								lowerCamelCase__	:	int				=ConfigTester(self,						config_class=lowerCamelCase,						hidden_size=37  )



					def 	snake_case ( self							:						Tuple  )->      Dict:
								self.config_tester.run_common_tests()



					def 	snake_case ( self							:						str  )->      Optional[Any]:
								lowerCamelCase__	:	Optional[int]				=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*lowerCamelCase  )



					def 	snake_case ( self							:						Dict  )->      int:
								lowerCamelCase__	:	Tuple				=self.model_tester.prepare_config_and_inputs()
								for type in ["absolute", "relative_key", "relative_key_query"]:
											lowerCamelCase__	:	List[Any]				=type
											self.model_tester.create_and_check_model(*lowerCamelCase  )



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



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



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




					@parameterized.expand([('''linear''',), ('''dynamic''',)]  )
					def 	snake_case ( self							:						str,						lowerCamelCase							:						Tuple  )->      Dict:
								lowerCamelCase__			,     lowerCamelCase__	:	int				=self.model_tester.prepare_config_and_inputs_for_common()
								lowerCamelCase__	:	List[str]				=ids_tensor([1, 10],						config.vocab_size  )
								lowerCamelCase__	:	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
								lowerCamelCase__	:	str				=OpenLlamaModel(lowerCamelCase  )
								original_model.to(lowerCamelCase  )
								original_model.eval()
								lowerCamelCase__	:	Tuple				=original_model(lowerCamelCase  ).last_hidden_state
								lowerCamelCase__	:	List[str]				=original_model(lowerCamelCase  ).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__	:	Tuple				=OpenLlamaModel(lowerCamelCase  )
								scaled_model.to(lowerCamelCase  )
								scaled_model.eval()
								lowerCamelCase__	:	Any				=scaled_model(lowerCamelCase  ).last_hidden_state
								lowerCamelCase__	:	Optional[Any]				=scaled_model(lowerCamelCase  ).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(lowerCamelCase,						lowerCamelCase,						atol=1E-5  )  )
								else:
											self.assertFalse(torch.allclose(lowerCamelCase,						lowerCamelCase,						atol=1E-5  )  )

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









"""simple docstring"""






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					int							,	__lowerCamelCase       :					int							,	__lowerCamelCase       :					list[int]			):
			"""simple docstring"""

			# 1. Validate that path exists between current and next vertices
			if graph[path[curr_ind - 1]][next_ver] == 0:
						return False

			# 2. Validate that next vertex is not already in path
			return not any(vertex == next_ver for vertex in path			)






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					list[int]							,	__lowerCamelCase       :					int			):
			"""simple docstring"""

			# Base Case
			if curr_ind == len(__lowerCamelCase			):
						# return whether path exists between current and starting vertices
						return graph[path[curr_ind - 1]][path[0]] == 1

			# Recursive Step
			for next_ver in range(0							,	len(__lowerCamelCase			)			):
						if valid_connection(__lowerCamelCase							,	__lowerCamelCase							,	__lowerCamelCase							,	__lowerCamelCase			):
									# Insert current vertex  into path as next transition
									lowerCamelCase__	:	Tuple				=next_ver
									# Validate created path
									if util_hamilton_cycle(__lowerCamelCase							,	__lowerCamelCase							,	curr_ind + 1			):
												return True
									# Backtrack
									lowerCamelCase__	:	int				=-1
			return False






def   snake_case__     (	__lowerCamelCase       :					list[list[int]]							,	__lowerCamelCase       :					int = 0			):
			"""simple docstring"""
			lowerCamelCase__	:	Tuple				=[-1] * (len(__lowerCamelCase			) + 1)
			# initialize start and end of path with starting index
			lowerCamelCase__	:	Union[str, Any]				=start_index
			# evaluate and if we find answer return path either return empty array
			return path if util_hamilton_cycle(__lowerCamelCase							,	__lowerCamelCase							,	1			) else []




