



"""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 			_a :
			"""simple docstring"""



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



			def  lowercase__						(		self     :    Tuple   )->Dict:
							_UpperCAmelCase					=    ids_tensor([self.batch_size, self.seq_length]				,   self.vocab_size   )

							_UpperCAmelCase					=    None
							if self.use_input_mask:
											_UpperCAmelCase					=    random_attention_mask([self.batch_size, self.seq_length]   )

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

							_UpperCAmelCase					=    None
							_UpperCAmelCase					=    None
							_UpperCAmelCase					=    None
							if self.use_labels:
											_UpperCAmelCase					=    ids_tensor([self.batch_size]				,   self.type_sequence_label_size   )
											_UpperCAmelCase					=    ids_tensor([self.batch_size, self.seq_length]				,   self.num_labels   )
											_UpperCAmelCase					=    ids_tensor([self.batch_size]				,   self.num_choices   )

							_UpperCAmelCase					=    self.get_config()

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



			def  lowercase__						(		self     :    List[str]   )->List[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  lowercase__						(		self     :    Tuple				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    int				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    List[Any]   )->List[Any]:
							_UpperCAmelCase					=    OpenLlamaModel(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    model(__UpperCamelCase				,   attention_mask=__UpperCamelCase   )
							_UpperCAmelCase					=    model(__UpperCamelCase   )
							self.parent.assertEqual(result.last_hidden_state.shape				,   (self.batch_size, self.seq_length, self.hidden_size)   )



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



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



			def  lowercase__						(		self     :    Optional[Any]				,   __UpperCamelCase     :    Any				,   __UpperCamelCase     :    str				,   __UpperCamelCase     :    Optional[int]				,   __UpperCamelCase     :    Union[str, Any]				,   __UpperCamelCase     :    Dict				,   __UpperCamelCase     :    List[str]				,   __UpperCamelCase     :    Tuple				,   __UpperCamelCase     :    List[Any]				,   __UpperCamelCase     :    Tuple				,   )->Dict:
							_UpperCAmelCase					=    True
							_UpperCAmelCase					=    True
							_UpperCAmelCase					=    OpenLlamaForCausalLM(config=__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()

							# first forward pass
							_UpperCAmelCase					=    model(
							    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   encoder_hidden_states=__UpperCamelCase				,   encoder_attention_mask=__UpperCamelCase				,   use_cache=__UpperCamelCase				,   )
							_UpperCAmelCase					=    outputs.past_key_values

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

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

							_UpperCAmelCase					=    model(
							    __UpperCamelCase				,   attention_mask=__UpperCamelCase				,   encoder_hidden_states=__UpperCamelCase				,   encoder_attention_mask=__UpperCamelCase				,   output_hidden_states=__UpperCamelCase				,   )['''hidden_states'''][0]
							_UpperCAmelCase					=    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
							_UpperCAmelCase					=    ids_tensor((1,)				,   output_from_past.shape[-1]   ).item()
							_UpperCAmelCase					=    output_from_no_past[:, -3:, random_slice_idx].detach()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    Optional[int]   )->Any:
							_UpperCAmelCase					=    self.prepare_config_and_inputs()
							(
							    (
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						(
							    _UpperCAmelCase
							)		,						
							)					=    config_and_inputs
							_UpperCAmelCase					=    {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class 			_a (					lowerCAmelCase			,							lowerCAmelCase			,							lowerCAmelCase			,							unittest.TestCase):
			"""simple docstring"""
			UpperCamelCase__			      =	(
			    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
			)
			UpperCamelCase__			      =	(OpenLlamaForCausalLM,) if is_torch_available() else ()
			UpperCamelCase__			      =	(
			    {
			        """feature-extraction""": OpenLlamaModel,
			        """text-classification""": OpenLlamaForSequenceClassification,
			        """text-generation""": OpenLlamaForCausalLM,
			        """zero-shot""": OpenLlamaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			UpperCamelCase__			      =	False
			UpperCamelCase__			      =	False



			def  lowercase__						(		self     :    Dict   )->Optional[int]:
							_UpperCAmelCase					=    OpenLlamaModelTester(self   )
							_UpperCAmelCase					=    ConfigTester(self				,   config_class=__UpperCamelCase				,   hidden_size=3_7   )



			def  lowercase__						(		self     :    Optional[int]   )->List[Any]:
							self.config_tester.run_common_tests()



			def  lowercase__						(		self     :    Tuple   )->Dict:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__UpperCamelCase   )



			def  lowercase__						(		self     :    str   )->List[str]:
							_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											_UpperCAmelCase					=    type
											self.model_tester.create_and_check_model(*__UpperCamelCase   )



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



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



			def  lowercase__						(		self     :    List[str]   )->Any:
							_UpperCAmelCase		,						_UpperCAmelCase					=    self.model_tester.prepare_config_and_inputs_for_common()
							_UpperCAmelCase					=    3
							_UpperCAmelCase					=    '''multi_label_classification'''
							_UpperCAmelCase					=    input_dict['''input_ids''']
							_UpperCAmelCase					=    input_ids.ne(1   ).to(__UpperCamelCase   )
							_UpperCAmelCase					=    ids_tensor(
							    [self.model_tester.batch_size, config.num_labels]				,   self.model_tester.type_sequence_label_size   ).to(torch.float   )
							_UpperCAmelCase					=    OpenLlamaForSequenceClassification(__UpperCamelCase   )
							model.to(__UpperCamelCase   )
							model.eval()
							_UpperCAmelCase					=    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  lowercase__						(		self     :    List[str]   )->Tuple:
							pass



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

							set_seed(4_2   )  # Fixed seed at init time so the two models get the same random weights
							_UpperCAmelCase					=    {'''type''': scaling_type, '''factor''': 1_0.0}
							_UpperCAmelCase					=    OpenLlamaModel(__UpperCamelCase   )
							scaled_model.to(__UpperCamelCase   )
							scaled_model.eval()
							_UpperCAmelCase					=    scaled_model(__UpperCamelCase   ).last_hidden_state
							_UpperCAmelCase					=    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"""


import functools







def  lowercase	(      _SCREAMING_SNAKE_CASE   :						list[int]					,     _SCREAMING_SNAKE_CASE   :						list[int]				):






				'''simple docstring'''



				if not isinstance(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				) or not all(isinstance(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				) for day in days				):
								raise ValueError('''The parameter days should be a list of integers'''				)

				if len(_SCREAMING_SNAKE_CASE				) != 3 or not all(isinstance(_SCREAMING_SNAKE_CASE					,     _SCREAMING_SNAKE_CASE				) for cost in costs				):
								raise ValueError('''The parameter costs should be a list of three integers'''				)

				if len(_SCREAMING_SNAKE_CASE				) == 0:
								return 0

				if min(_SCREAMING_SNAKE_CASE				) <= 0:
								raise ValueError('''All days elements should be greater than 0'''				)

				if max(_SCREAMING_SNAKE_CASE				) >= 366:
								raise ValueError('''All days elements should be less than 366'''				)

				_UpperCAmelCase					=    set(_SCREAMING_SNAKE_CASE				)

				@functools.cache
				def dynamic_programming(_SCREAMING_SNAKE_CASE   :						int				) -> int:
								if index > 365:
												return 0

								if index not in days_set:
												return dynamic_programming(index + 1				)

								return min(
								    costs[0] + dynamic_programming(index + 1				)					,     costs[1] + dynamic_programming(index + 7				)					,     costs[2] + dynamic_programming(index + 30				)					,     )

				return dynamic_programming(1				)


if __name__ == "__main__":
					import doctest

					doctest.testmod()



