





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


    def 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size       )

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

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

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

        _SCREAMING_SNAKE_CASE									=			self.get_config()

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


    def 			snake_case_(		self       )		->   List[str]:
        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 			snake_case_(		self ,		A ,		A ,		A ,		A ,		A ,		A ,		A       )		->   int:
        _SCREAMING_SNAKE_CASE									=			OpenLlamaModel(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		attention_mask=A       )
        _SCREAMING_SNAKE_CASE									=			model(A       )
        self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)       )


    def 			snake_case_(		self ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		)		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			True
        _SCREAMING_SNAKE_CASE									=			OpenLlamaModel(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(
            A ,		attention_mask=A ,		encoder_hidden_states=A ,		encoder_attention_mask=A ,		)
        _SCREAMING_SNAKE_CASE									=			model(
            A ,		attention_mask=A ,		encoder_hidden_states=A ,		)
        _SCREAMING_SNAKE_CASE									=			model(A ,		attention_mask=A       )
        self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)       )


    def 			snake_case_(		self ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		)		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			OpenLlamaForCausalLM(config=A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		attention_mask=A ,		labels=A       )
        self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.vocab_size)       )


    def 			snake_case_(		self ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		A ,		)		->   Optional[int]:
        _SCREAMING_SNAKE_CASE									=			True
        _SCREAMING_SNAKE_CASE									=			True
        _SCREAMING_SNAKE_CASE									=			OpenLlamaForCausalLM(config=A       )
        model.to(A       )
        model.eval()

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

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

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

        _SCREAMING_SNAKE_CASE									=			model(
            A ,		attention_mask=A ,		encoder_hidden_states=A ,		encoder_attention_mask=A ,		output_hidden_states=A ,		)["""hidden_states"""][0]
        _SCREAMING_SNAKE_CASE									=			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
        _SCREAMING_SNAKE_CASE									=			ids_tensor((1,) ,		output_from_past.shape[-1]       ).item()
        _SCREAMING_SNAKE_CASE									=			output_from_no_past[:, -3:, random_slice_idx].detach()
        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self       )		->   List[str]:
        _SCREAMING_SNAKE_CASE									=			self.prepare_config_and_inputs()
        (
            (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       (
            _SCREAMING_SNAKE_CASE
        )				,       
        )									=			config_and_inputs
        _SCREAMING_SNAKE_CASE									=			{"""input_ids""": input_ids, """attention_mask""": input_mask}
        return config, inputs_dict




@require_torch
class 					a_     (				snake_case_      ,    snake_case_      ,    snake_case_      ,    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 			snake_case_(		self       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			OpenLlamaModelTester(self       )
        _SCREAMING_SNAKE_CASE									=			ConfigTester(self ,		config_class=A ,		hidden_size=37       )


    def 			snake_case_(		self       )		->   Union[str, Any]:
        self.config_tester.run_common_tests()


    def 			snake_case_(		self       )		->   Optional[Any]:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*A       )


    def 			snake_case_(		self       )		->   Tuple:
        _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
            _SCREAMING_SNAKE_CASE									=			type
            self.model_tester.create_and_check_model(*A       )


    def 			snake_case_(		self       )		->   Union[str, Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			3
        _SCREAMING_SNAKE_CASE									=			input_dict["""input_ids"""]
        _SCREAMING_SNAKE_CASE									=			input_ids.ne(1       ).to(A       )
        _SCREAMING_SNAKE_CASE									=			ids_tensor([self.model_tester.batch_size] ,		self.model_tester.type_sequence_label_size       )
        _SCREAMING_SNAKE_CASE									=			OpenLlamaForSequenceClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		attention_mask=A ,		labels=A       )
        self.assertEqual(result.logits.shape ,		(self.model_tester.batch_size, self.model_tester.num_labels)       )


    def 			snake_case_(		self       )		->   Optional[int]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			3
        _SCREAMING_SNAKE_CASE									=			"""single_label_classification"""
        _SCREAMING_SNAKE_CASE									=			input_dict["""input_ids"""]
        _SCREAMING_SNAKE_CASE									=			input_ids.ne(1       ).to(A       )
        _SCREAMING_SNAKE_CASE									=			ids_tensor([self.model_tester.batch_size] ,		self.model_tester.type_sequence_label_size       )
        _SCREAMING_SNAKE_CASE									=			OpenLlamaForSequenceClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			model(A ,		attention_mask=A ,		labels=A       )
        self.assertEqual(result.logits.shape ,		(self.model_tester.batch_size, self.model_tester.num_labels)       )


    def 			snake_case_(		self       )		->   Optional[Any]:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			3
        _SCREAMING_SNAKE_CASE									=			"""multi_label_classification"""
        _SCREAMING_SNAKE_CASE									=			input_dict["""input_ids"""]
        _SCREAMING_SNAKE_CASE									=			input_ids.ne(1       ).to(A       )
        _SCREAMING_SNAKE_CASE									=			ids_tensor(
            [self.model_tester.batch_size, config.num_labels] ,		self.model_tester.type_sequence_label_size       ).to(torch.float       )
        _SCREAMING_SNAKE_CASE									=			OpenLlamaForSequenceClassification(A       )
        model.to(A       )
        model.eval()
        _SCREAMING_SNAKE_CASE									=			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 			snake_case_(		self       )		->   Any:
        pass





    @parameterized.expand([("""linear""",), ("""dynamic""",)]       )
    def 			snake_case_(		self ,		A       )		->   Tuple:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			self.model_tester.prepare_config_and_inputs_for_common()
        _SCREAMING_SNAKE_CASE									=			ids_tensor([1, 10] ,		config.vocab_size       )
        _SCREAMING_SNAKE_CASE									=			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
        _SCREAMING_SNAKE_CASE									=			OpenLlamaModel(A       )
        original_model.to(A       )
        original_model.eval()
        _SCREAMING_SNAKE_CASE									=			original_model(A       ).last_hidden_state
        _SCREAMING_SNAKE_CASE									=			original_model(A       ).last_hidden_state

        set_seed(42       )  # Fixed seed at init time so the two models get the same random weights
        _SCREAMING_SNAKE_CASE									=			{"""type""": scaling_type, """factor""": 10.0}
        _SCREAMING_SNAKE_CASE									=			OpenLlamaModel(A       )
        scaled_model.to(A       )
        scaled_model.eval()
        _SCREAMING_SNAKE_CASE									=			scaled_model(A       ).last_hidden_state
        _SCREAMING_SNAKE_CASE									=			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 collections.abc import Sequence





def lowerCamelCase						(     __lowerCamelCase :    Sequence[float]					,       __lowerCamelCase :    bool = False    )						->float:
    if not arr:
        return 0

    _SCREAMING_SNAKE_CASE									=			0 if allow_empty_subarrays else float("""-inf"""    )
    _SCREAMING_SNAKE_CASE									=			0.0
    for num in arr:
        _SCREAMING_SNAKE_CASE									=			max(0 if allow_empty_subarrays else num					,       curr_sum + num    )
        _SCREAMING_SNAKE_CASE									=			max(__lowerCamelCase					,       __lowerCamelCase    )

    return max_sum


if __name__ == "__main__":
      from doctest import testmod

      testmod()

      lowercase_					= [-2, 1, -3, 4, -1, 2, 1, -5, 4]
      print(f"""{max_subarray_sum(nums) = }""")



