
"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import LEDConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import TFLEDForConditionalGeneration, TFLEDModel

@require_tf
class     A_	:



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_	      =   LEDConfig
							SCREAMING_SNAKE_CASE_	      =   {}
							SCREAMING_SNAKE_CASE_	      =   """gelu"""





							def __init__(  self  :str     ,							lowerCamelCase_  :Union[str, Any]     ,							lowerCamelCase_  :str=13     ,							lowerCamelCase_  :List[str]=7     ,							lowerCamelCase_  :List[Any]=True     ,							lowerCamelCase_  :Tuple=False     ,							lowerCamelCase_  :str=99     ,							lowerCamelCase_  :Tuple=32     ,							lowerCamelCase_  :Dict=2     ,							lowerCamelCase_  :List[Any]=4     ,							lowerCamelCase_  :Tuple=37     ,							lowerCamelCase_  :List[str]=0.1     ,							lowerCamelCase_  :Any=0.1     ,							lowerCamelCase_  :Union[str, Any]=20     ,							lowerCamelCase_  :List[Any]=2     ,							lowerCamelCase_  :Tuple=1     ,							lowerCamelCase_  :Optional[int]=0     ,							lowerCamelCase_  :List[str]=4     ,							):





										"""simple docstring"""

										lowerCamelCase__	:     str              =parent
										lowerCamelCase__	:     List[str]              =batch_size
										lowerCamelCase__	:     Dict              =seq_length
										lowerCamelCase__	:     Optional[int]              =is_training
										lowerCamelCase__	:     Tuple              =use_labels
										lowerCamelCase__	:     Tuple              =vocab_size
										lowerCamelCase__	:     List[str]              =hidden_size
										lowerCamelCase__	:     Dict              =num_hidden_layers
										lowerCamelCase__	:     Dict              =num_attention_heads
										lowerCamelCase__	:     Optional[int]              =intermediate_size

										lowerCamelCase__	:     List[str]              =hidden_dropout_prob
										lowerCamelCase__	:     List[Any]              =attention_probs_dropout_prob
										lowerCamelCase__	:     Any              =max_position_embeddings
										lowerCamelCase__	:     str              =eos_token_id
										lowerCamelCase__	:     Tuple              =pad_token_id
										lowerCamelCase__	:     Tuple              =bos_token_id
										lowerCamelCase__	:     List[Any]              =attention_window

										# `ModelTesterMixin.test_attention_outputs` is expecting attention tensors to be of size
										# [num_attention_heads, encoder_seq_length, encoder_key_length], but TFLongformerSelfAttention
										# returns attention of shape [num_attention_heads, encoder_seq_length, self.attention_window + 1]
										# because its local attention only attends to `self.attention_window` and one before and one after
										lowerCamelCase__	:     Dict              =self.attention_window + 2

										# because of padding `encoder_seq_length`, is different from `seq_length`. Relevant for
										# the `test_attention_outputs` and `test_hidden_states_output` tests
										lowerCamelCase__	:     str              =(
										    self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
										)





							def       UpperCAmelCase__							(  self  :List[str]       ):





										"""simple docstring"""

										lowerCamelCase__	:     str              =ids_tensor([self.batch_size, self.seq_length - 1]     ,							self.vocab_size       )
										lowerCamelCase__	:     Tuple              =tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size       )     ,							1       )
										lowerCamelCase__	:     Any              =tf.concat([input_ids, eos_tensor]     ,							axis=1       )

										lowerCamelCase__	:     List[str]              =ids_tensor([self.batch_size, self.seq_length]     ,							self.vocab_size       )

										lowerCamelCase__	:     Tuple              =self.config_cls(
										    vocab_size=self.vocab_size     ,							d_model=self.hidden_size     ,							encoder_layers=self.num_hidden_layers     ,							decoder_layers=self.num_hidden_layers     ,							encoder_attention_heads=self.num_attention_heads     ,							decoder_attention_heads=self.num_attention_heads     ,							encoder_ffn_dim=self.intermediate_size     ,							decoder_ffn_dim=self.intermediate_size     ,							dropout=self.hidden_dropout_prob     ,							attention_dropout=self.attention_probs_dropout_prob     ,							max_position_embeddings=self.max_position_embeddings     ,							eos_token_ids=[2]     ,							bos_token_id=self.bos_token_id     ,							pad_token_id=self.pad_token_id     ,							decoder_start_token_id=self.pad_token_id     ,							attention_window=self.attention_window     ,							**self.config_updates     ,							)
										lowerCamelCase__	:     str              =prepare_led_inputs_dict(A_     ,							A_     ,							A_       )
										lowerCamelCase__	:     Dict              =tf.concat(
										    [tf.zeros_like(A_       )[:, :-1], tf.ones_like(A_       )[:, -1:]]     ,							axis=-1     ,							)
										lowerCamelCase__	:     Union[str, Any]              =global_attention_mask
										return config, inputs_dict





							def       UpperCAmelCase__							(  self  :Tuple     ,							lowerCamelCase_  :Optional[int]     ,							lowerCamelCase_  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     Tuple              =TFLEDModel(config=A_       ).get_decoder()
										lowerCamelCase__	:     Dict              =inputs_dict['input_ids']

										lowerCamelCase__	:     int              =input_ids[:1, :]
										lowerCamelCase__	:     Tuple              =inputs_dict['attention_mask'][:1, :]
										lowerCamelCase__	:     Any              =1

										# first forward pass
										lowerCamelCase__	:     Dict              =model(A_     ,							attention_mask=A_     ,							use_cache=A_       )

										lowerCamelCase__       ,				lowerCamelCase__	:     List[Any]              =outputs.to_tuple()

										# create hypothetical next token and extent to next_input_ids
										lowerCamelCase__	:     List[Any]              =ids_tensor((self.batch_size, 3)     ,							config.vocab_size       )
										lowerCamelCase__	:     Any              =tf.cast(ids_tensor((self.batch_size, 3)     ,							2       )     ,							tf.inta       )

										# append to next input_ids and
										lowerCamelCase__	:     Optional[Any]              =tf.concat([input_ids, next_tokens]     ,							axis=-1       )
										lowerCamelCase__	:     List[Any]              =tf.concat([attention_mask, next_attn_mask]     ,							axis=-1       )

										lowerCamelCase__	:     Union[str, Any]              =model(A_     ,							attention_mask=A_       )[0]
										lowerCamelCase__	:     Optional[int]              =model(A_     ,							attention_mask=A_     ,							past_key_values=A_       )[0]

										self.parent.assertEqual(next_tokens.shape[1]     ,							output_from_past.shape[1]       )

										# select random slice
										lowerCamelCase__	:     List[Any]              =int(ids_tensor((1,)     ,							output_from_past.shape[-1]       )       )
										lowerCamelCase__	:     str              =output_from_no_past[:, -3:, random_slice_idx]
										lowerCamelCase__	:     List[str]              =output_from_past[:, :, random_slice_idx]

										# test that outputs are equal for slice
										tf.debugging.assert_near(A_     ,							A_     ,							rtol=1e-3       )






def    lowerCAmelCase_   (		snake_case_		:			Any							,    snake_case_		:			Any							,    snake_case_		:			List[str]							,    snake_case_		:			Tuple=None							,    snake_case_		:			Dict=None							,    snake_case_		:			str=None							,    snake_case_		:			Any=None							,    )   ->str:



			if attention_mask is None:
						lowerCamelCase__	:     str              =tf.cast(tf.math.not_equal(snake_case_							,    config.pad_token_id			)							,    tf.inta			)
			if decoder_attention_mask is None:
						lowerCamelCase__	:     Dict              =tf.concat(
						    [
						        tf.ones(decoder_input_ids[:, :1].shape							,    dtype=tf.inta			),
						        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]							,    config.pad_token_id			)							,    tf.inta			),
						    ]							,    axis=-1							,    )
			if head_mask is None:
						lowerCamelCase__	:     Union[str, Any]              =tf.ones((config.encoder_layers, config.encoder_attention_heads)			)
			if decoder_head_mask is None:
						lowerCamelCase__	:     Optional[Any]              =tf.ones((config.decoder_layers, config.decoder_attention_heads)			)
			return {
			    "input_ids": input_ids,
			    "attention_mask": attention_mask,
			    "decoder_input_ids": decoder_input_ids,
			    "decoder_attention_mask": decoder_attention_mask,
			    "head_mask": head_mask,
			    "decoder_head_mask": decoder_head_mask,
			}

@require_tf
class     A_	(     A__      ,					A__      ,					unittest.TestCase   ):



							"""simple docstring"""






							SCREAMING_SNAKE_CASE_	      =   (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
							SCREAMING_SNAKE_CASE_	      =   (TFLEDForConditionalGeneration,) if is_tf_available() else ()
							SCREAMING_SNAKE_CASE_	      =   (
							    {
							        """conversational""": TFLEDForConditionalGeneration,
							        """feature-extraction""": TFLEDModel,
							        """summarization""": TFLEDForConditionalGeneration,
							        """text2text-generation""": TFLEDForConditionalGeneration,
							        """translation""": TFLEDForConditionalGeneration,
							    }
							    if is_tf_available()
							    else {}
							)
							SCREAMING_SNAKE_CASE_	      =   True
							SCREAMING_SNAKE_CASE_	      =   False
							SCREAMING_SNAKE_CASE_	      =   False
							SCREAMING_SNAKE_CASE_	      =   False





							def       UpperCAmelCase__							(  self  :List[str]       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[int]              =TFLEDModelTester(self       )
										lowerCamelCase__	:     Tuple              =ConfigTester(self     ,							config_class=A_       )





							def       UpperCAmelCase__							(  self  :Tuple       ):





										"""simple docstring"""

										self.config_tester.run_common_tests()





							def       UpperCAmelCase__							(  self  :int       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =self.model_tester.prepare_config_and_inputs_for_common()
										self.model_tester.check_decoder_model_past_large_inputs(*A_       )





							def       UpperCAmelCase__							(  self  :Optional[Any]       ):





										"""simple docstring"""

										lowerCamelCase__       ,				lowerCamelCase__	:     Any              =self.model_tester.prepare_config_and_inputs_for_common()
										lowerCamelCase__	:     int              =tf.zeros_like(inputs_dict['attention_mask']       )
										lowerCamelCase__	:     Tuple              =2
										lowerCamelCase__	:     Dict              =tf.where(
										    tf.range(self.model_tester.seq_length       )[None, :] < num_global_attn_indices     ,							1     ,							inputs_dict['global_attention_mask']     ,							)

										lowerCamelCase__	:     Optional[int]              =True
										lowerCamelCase__	:     Optional[int]              =self.model_tester.seq_length
										lowerCamelCase__	:     int              =self.model_tester.encoder_seq_length

										def check_decoder_attentions_output(lowerCamelCase_  :Any       ):
													lowerCamelCase__	:     int              =outputs.decoder_attentions
													self.assertEqual(len(A_       )     ,							self.model_tester.num_hidden_layers       )
													self.assertListEqual(
													    list(decoder_attentions[0].shape[-3:]       )     ,							[self.model_tester.num_attention_heads, seq_length, seq_length]     ,							)

										def check_encoder_attentions_output(lowerCamelCase_  :Optional[Any]       ):
													lowerCamelCase__	:     List[str]              =[t.numpy() for t in outputs.encoder_attentions]
													lowerCamelCase__	:     Optional[int]              =[t.numpy() for t in outputs.encoder_global_attentions]
													self.assertEqual(len(A_       )     ,							self.model_tester.num_hidden_layers       )
													self.assertEqual(len(A_       )     ,							self.model_tester.num_hidden_layers       )
													self.assertListEqual(
													    list(attentions[0].shape[-3:]       )     ,							[self.model_tester.num_attention_heads, seq_length, seq_length]     ,							)
													self.assertListEqual(
													    list(global_attentions[0].shape[-3:]       )     ,							[self.model_tester.num_attention_heads, encoder_seq_length, num_global_attn_indices]     ,							)

										for model_class in self.all_model_classes:
													lowerCamelCase__	:     Optional[Any]              =True
													lowerCamelCase__	:     Union[str, Any]              =False
													lowerCamelCase__	:     Any              =False
													lowerCamelCase__	:     Union[str, Any]              =model_class(A_       )
													lowerCamelCase__	:     Optional[Any]              =model(self._prepare_for_class(A_     ,							A_       )       )
													lowerCamelCase__	:     Union[str, Any]              =len(A_       )
													self.assertEqual(config.output_hidden_states     ,							A_       )
													check_encoder_attentions_output(A_       )

													if self.is_encoder_decoder:
																lowerCamelCase__	:     Any              =model_class(A_       )
																lowerCamelCase__	:     Any              =model(self._prepare_for_class(A_     ,							A_       )       )
																self.assertEqual(config.output_hidden_states     ,							A_       )
																check_decoder_attentions_output(A_       )

													# Check that output attentions can also be changed via the config
													del inputs_dict["output_attentions"]
													lowerCamelCase__	:     Tuple              =True
													lowerCamelCase__	:     int              =model_class(A_       )
													lowerCamelCase__	:     List[str]              =model(self._prepare_for_class(A_     ,							A_       )       )
													self.assertEqual(config.output_hidden_states     ,							A_       )
													check_encoder_attentions_output(A_       )

													# Check attention is always last and order is fine
													lowerCamelCase__	:     List[Any]              =True
													lowerCamelCase__	:     Optional[Any]              =True
													lowerCamelCase__	:     Tuple              =model_class(A_       )
													lowerCamelCase__	:     Union[str, Any]              =model(self._prepare_for_class(A_     ,							A_       )       )

													self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)     ,							len(A_       )       )
													self.assertEqual(model.config.output_hidden_states     ,							A_       )
													check_encoder_attentions_output(A_       )





							@unittest.skip('LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'       )
							def       UpperCAmelCase__							(  self  :Tuple       ):





										"""simple docstring"""

										pass





							def       UpperCAmelCase__							(  self  :Optional[Any]       ):





										"""simple docstring"""

										pass






def    lowerCAmelCase_   (		snake_case_		:			Dict			)   ->Optional[Any]:
			return tf.constant(snake_case_							,    dtype=tf.intaa			)


lowerCAmelCase								=						1E-4

@slow
@require_tf
class     A_	(     unittest.TestCase   ):



							"""simple docstring"""






							def       UpperCAmelCase__							(  self  :Any       ):





										"""simple docstring"""

										lowerCamelCase__	:     Optional[Any]              =TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'       ).led

										# change to intended input here
										lowerCamelCase__	:     Optional[int]              =_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]       )
										lowerCamelCase__	:     Tuple              =_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]       )
										lowerCamelCase__	:     Optional[int]              =prepare_led_inputs_dict(model.config     ,							A_     ,							A_       )
										lowerCamelCase__	:     List[Any]              =model(**A_       )[0]
										lowerCamelCase__	:     List[str]              =(1, 1_024, 768)
										self.assertEqual(output.shape     ,							A_       )
										# change to expected output here
										lowerCamelCase__	:     Any              =tf.convert_to_tensor(
										    [[2.30_50, 2.82_79, 0.65_31], [-1.84_57, -0.14_55, -3.56_61], [-1.01_86, 0.45_86, -2.20_43]]     ,							)
										tf.debugging.assert_near(output[:, :3, :3]     ,							A_     ,							atol=1e-3       )





							def       UpperCAmelCase__							(  self  :List[str]       ):





										"""simple docstring"""

										lowerCamelCase__	:     int              =TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'       )

										# change to intended input here
										lowerCamelCase__	:     Tuple              =_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]       )
										lowerCamelCase__	:     Optional[Any]              =_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]       )
										lowerCamelCase__	:     Any              =prepare_led_inputs_dict(model.config     ,							A_     ,							A_       )
										lowerCamelCase__	:     int              =model(**A_       )[0]
										lowerCamelCase__	:     str              =(1, 1_024, model.config.vocab_size)
										self.assertEqual(output.shape     ,							A_       )
										# change to expected output here
										lowerCamelCase__	:     Optional[int]              =tf.convert_to_tensor(
										    [[33.65_07, 6.45_72, 16.80_89], [5.87_39, -2.42_38, 11.29_02], [-3.21_39, -4.31_49, 4.27_83]]     ,							)
										tf.debugging.assert_near(output[:, :3, :3]     ,							A_     ,							atol=1e-3     ,							rtol=1e-3       )





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


      '''simple docstring'''



      if discount_rate < 0:
            raise ValueError('Discount rate cannot be negative'     )
      if not cash_flows:
            raise ValueError('Cash flows list cannot be empty'     )
      lowerCamelCase_		     =    sum(
          cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(lowercase     )     )
      return round(lowercase	,							ndigits=2     )


if __name__ == "__main__":
 import doctest

 doctest.testmod()



