





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  __lowerCAmelCase	:






			def __init__(self ,							__magic_name__ ,							__magic_name__=13 ,							__magic_name__=7 ,							__magic_name__=True ,							__magic_name__=True ,							__magic_name__=False ,							__magic_name__=True ,							__magic_name__=99 ,							__magic_name__=32 ,							__magic_name__=5 ,							__magic_name__=4 ,							__magic_name__=37 ,							__magic_name__="gelu" ,							__magic_name__=0.1 ,							__magic_name__=0.1 ,							__magic_name__=512 ,							__magic_name__=16 ,							__magic_name__=2 ,							__magic_name__=0.02 ,							__magic_name__=3 ,							__magic_name__=4 ,							__magic_name__=None ,							)					->       Any:


							'''simple docstring'''



							snake_case_						:   Optional[Any]           =  parent
							snake_case_						:   Optional[Any]           =  batch_size
							snake_case_						:   List[Any]           =  seq_length
							snake_case_						:   List[str]           =  is_training
							snake_case_						:   Union[str, Any]           =  use_input_mask
							snake_case_						:   Dict           =  use_token_type_ids
							snake_case_						:   int           =  use_labels
							snake_case_						:   List[Any]           =  vocab_size
							snake_case_						:   Optional[int]           =  hidden_size
							snake_case_						:   Dict           =  num_hidden_layers
							snake_case_						:   Optional[int]           =  num_attention_heads
							snake_case_						:   Union[str, Any]           =  intermediate_size
							snake_case_						:   Tuple           =  hidden_act
							snake_case_						:   int           =  hidden_dropout_prob
							snake_case_						:   Tuple           =  attention_probs_dropout_prob
							snake_case_						:   List[str]           =  max_position_embeddings
							snake_case_						:   Optional[int]           =  type_vocab_size
							snake_case_						:   List[str]           =  type_sequence_label_size
							snake_case_						:   Optional[int]           =  initializer_range
							snake_case_						:   Any           =  num_labels
							snake_case_						:   Any           =  num_choices
							snake_case_						:   Tuple           =  scope






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


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  ids_tensor([self.batch_size, self.seq_length] ,							self.vocab_size   )

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

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

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

							snake_case_						:   Dict           =  self.get_config()

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






			def       lowerCamelCase  (self   )					->       List[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=__magic_name__ ,							initializer_range=self.initializer_range ,							use_stable_embedding=__magic_name__ ,							)






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__   )					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  OpenLlamaModel(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Union[str, Any]           =  model(__magic_name__ ,							attention_mask=__magic_name__   )
							snake_case_						:   Optional[Any]           =  model(__magic_name__   )
							self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							)					->       str:


							'''simple docstring'''



							snake_case_						:   Tuple           =  True
							snake_case_						:   Optional[int]           =  OpenLlamaModel(__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   List[Any]           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							encoder_hidden_states=__magic_name__ ,							encoder_attention_mask=__magic_name__ ,							)
							snake_case_						:   Tuple           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							encoder_hidden_states=__magic_name__ ,							)
							snake_case_						:   Optional[int]           =  model(__magic_name__ ,							attention_mask=__magic_name__   )
							self.parent.assertEqual(result.last_hidden_state.shape ,							(self.batch_size, self.seq_length, self.hidden_size)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							)					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Any           =  OpenLlamaForCausalLM(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Tuple           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							labels=__magic_name__   )
							self.parent.assertEqual(result.logits.shape ,							(self.batch_size, self.seq_length, self.vocab_size)   )






			def       lowerCamelCase  (self ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							__magic_name__ ,							)					->       List[str]:


							'''simple docstring'''



							snake_case_						:   int           =  True
							snake_case_						:   Optional[Any]           =  True
							snake_case_						:   Any           =  OpenLlamaForCausalLM(config=__magic_name__   )
							model.to(__magic_name__   )
							model.eval()

							# first forward pass
							snake_case_						:   List[Any]           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							encoder_hidden_states=__magic_name__ ,							encoder_attention_mask=__magic_name__ ,							use_cache=__magic_name__ ,							)
							snake_case_						:   int           =  outputs.past_key_values

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

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

							snake_case_						:   Tuple           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							encoder_hidden_states=__magic_name__ ,							encoder_attention_mask=__magic_name__ ,							output_hidden_states=__magic_name__ ,							)['''hidden_states'''][0]
							snake_case_						:   Union[str, Any]           =  model(
							    __magic_name__ ,							attention_mask=__magic_name__ ,							encoder_hidden_states=__magic_name__ ,							encoder_attention_mask=__magic_name__ ,							past_key_values=__magic_name__ ,							output_hidden_states=__magic_name__ ,							)['''hidden_states'''][0]

							# select random slice
							snake_case_						:   Optional[int]           =  ids_tensor((1,) ,							output_from_past.shape[-1]   ).item()
							snake_case_						:   Any           =  output_from_no_past[:, -3:, random_slice_idx].detach()
							snake_case_						:   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(__magic_name__ ,							__magic_name__ ,							atol=1e-3   )   )






			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



							snake_case_						:   Optional[Any]           =  self.prepare_config_and_inputs()
							(
							    snake_case_							
							)						:   Dict           =  config_and_inputs
							snake_case_						:   Optional[int]           =  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict




@require_torch
class  __lowerCAmelCase	(      _a,					_a,					_a,					unittest.TestCase					):
			lowerCamelCase_					:					List[str]	      =				(
			    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
			)
			lowerCamelCase_					:					List[Any]	      =				(OpenLlamaForCausalLM,) if is_torch_available() else ()
			lowerCamelCase_					:					List[Any]	      =				(
			    {
			        '''feature-extraction''': OpenLlamaModel,
			        '''text-classification''': OpenLlamaForSequenceClassification,
			        '''text-generation''': OpenLlamaForCausalLM,
			        '''zero-shot''': OpenLlamaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase_					:					List[str]	      =				False
			lowerCamelCase_					:					Any	      =				False






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


							'''simple docstring'''



							snake_case_						:   str           =  OpenLlamaModelTester(self   )
							snake_case_						:   str           =  ConfigTester(self ,							config_class=__magic_name__ ,							hidden_size=37   )






			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



							self.config_tester.run_common_tests()






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   List[Any]           =  self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											snake_case_						:   Optional[int]           =  type
											self.model_tester.create_and_check_model(*__magic_name__   )






			def       lowerCamelCase  (self   )					->       Any:


							'''simple docstring'''



							snake_case_						:   Any           =  self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:   Union[str, Any]           =  3
							snake_case_						:   str           =  input_dict['''input_ids''']
							snake_case_						:   str           =  input_ids.ne(1   ).to(__magic_name__   )
							snake_case_						:   int           =  ids_tensor([self.model_tester.batch_size] ,							self.model_tester.type_sequence_label_size   )
							snake_case_						:   str           =  OpenLlamaForSequenceClassification(__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Tuple           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							labels=__magic_name__   )
							self.assertEqual(result.logits.shape ,							(self.model_tester.batch_size, self.model_tester.num_labels)   )






			def       lowerCamelCase  (self   )					->       Dict:


							'''simple docstring'''



							snake_case_						:   str           =  self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:   Dict           =  3
							snake_case_						:   List[Any]           =  '''single_label_classification'''
							snake_case_						:   str           =  input_dict['''input_ids''']
							snake_case_						:   List[Any]           =  input_ids.ne(1   ).to(__magic_name__   )
							snake_case_						:   str           =  ids_tensor([self.model_tester.batch_size] ,							self.model_tester.type_sequence_label_size   )
							snake_case_						:   Dict           =  OpenLlamaForSequenceClassification(__magic_name__   )
							model.to(__magic_name__   )
							model.eval()
							snake_case_						:   Optional[Any]           =  model(__magic_name__ ,							attention_mask=__magic_name__ ,							labels=__magic_name__   )
							self.assertEqual(result.logits.shape ,							(self.model_tester.batch_size, self.model_tester.num_labels)   )






			def       lowerCamelCase  (self   )					->       List[str]:


							'''simple docstring'''



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


							'''simple docstring'''



							pass






			@parameterized.expand([('''linear''',), ('''dynamic''',)]   )
			def       lowerCamelCase  (self ,							__magic_name__   )					->       Optional[int]:


							'''simple docstring'''



							snake_case_						:   Optional[int]           =  self.model_tester.prepare_config_and_inputs_for_common()
							snake_case_						:   Any           =  ids_tensor([1, 10] ,							config.vocab_size   )
							snake_case_						:   List[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
							snake_case_						:   str           =  OpenLlamaModel(__magic_name__   )
							original_model.to(__magic_name__   )
							original_model.eval()
							snake_case_						:   Any           =  original_model(__magic_name__   ).last_hidden_state
							snake_case_						:   List[Any]           =  original_model(__magic_name__   ).last_hidden_state

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

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







def 					lowerCamelCase_						(    _UpperCamelCase			)		->						str:






				"""simple docstring"""


				if number > 0:
								raise ValueError('''input must be a negative integer'''			)
				snake_case_						:   List[str]           =  len(bin(_UpperCamelCase			)[3:]			)
				snake_case_						:   str           =  bin(abs(_UpperCamelCase			) - (1 << binary_number_length)			)[3:]
				snake_case_						:   Dict           =  (
				    (
				        '''1'''
				        + '''0''' * (binary_number_length - len(_UpperCamelCase			))
				        + twos_complement_number
				    )
				    if number < 0
				    else '''0'''
				)
				return "0b" + twos_complement_number


if __name__ == "__main__":
	import doctest

	doctest.testmod()
