


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 			SCREAMING_SNAKE_CASE__  :
      snake_case__    :					Union[str, Any]					       =						LEDConfig
      snake_case__    :					Any					       =						{}
      snake_case__    :					int					       =						'''gelu'''
      def __init__(	self    :	str					,							SCREAMING_SNAKE_CASE__    :	Any					,							SCREAMING_SNAKE_CASE__    :	int=1_3					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=7					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=True					,							SCREAMING_SNAKE_CASE__    :	List[str]=False					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=9_9					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=3_2					,							SCREAMING_SNAKE_CASE__    :	List[str]=2					,							SCREAMING_SNAKE_CASE__    :	List[Any]=4					,							SCREAMING_SNAKE_CASE__    :	int=3_7					,							SCREAMING_SNAKE_CASE__    :	List[str]=0.1					,							SCREAMING_SNAKE_CASE__    :	str=0.1					,							SCREAMING_SNAKE_CASE__    :	Dict=2_0					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]=2					,							SCREAMING_SNAKE_CASE__    :	Dict=1					,							SCREAMING_SNAKE_CASE__    :	Optional[Any]=0					,							SCREAMING_SNAKE_CASE__    :	Any=4					,							)		->      List[str]:
             a_ :		Tuple							=  parent
             a_ :		Dict							=  batch_size
             a_ :		Tuple							=  seq_length
             a_ :		Optional[Any]							=  is_training
             a_ :		Tuple							=  use_labels
             a_ :		Union[str, Any]							=  vocab_size
             a_ :		List[str]							=  hidden_size
             a_ :		Union[str, Any]							=  num_hidden_layers
             a_ :		Union[str, Any]							=  num_attention_heads
             a_ :		Optional[Any]							=  intermediate_size

             a_ :		Any							=  hidden_dropout_prob
             a_ :		Optional[Any]							=  attention_probs_dropout_prob
             a_ :		Dict							=  max_position_embeddings
             a_ :		int							=  eos_token_id
             a_ :		Any							=  pad_token_id
             a_ :		Optional[int]							=  bos_token_id
             a_ :		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
             a_ :		str							=  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
             a_ :		Optional[int]							=  (
                 self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
             )
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      Tuple:
             a_ :		Tuple							=  ids_tensor([self.batch_size, self.seq_length - 1]					,							self.vocab_size					)
             a_ :		List[str]							=  tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size					)					,							1					)
             a_ :		Optional[Any]							=  tf.concat([input_ids, eos_tensor]					,							axis=1					)

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

             a_ :		str							=  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					,							)
             a_ :		Union[str, Any]							=  prepare_led_inputs_dict(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		str							=  tf.concat(
                 [tf.zeros_like(SCREAMING_SNAKE_CASE__					)[:, :-1], tf.ones_like(SCREAMING_SNAKE_CASE__					)[:, -1:]]					,							axis=-1					,							)
             a_ :		Union[str, Any]							=  global_attention_mask
             return config, inputs_dict

      def 	SCREAMING_SNAKE_CASE							(	self    :	Tuple					,							SCREAMING_SNAKE_CASE__    :	Union[str, Any]					,							SCREAMING_SNAKE_CASE__    :	int					)		->      Any:
             a_ :		Dict							=  TFLEDModel(config=SCREAMING_SNAKE_CASE__					).get_decoder()
             a_ :		Union[str, Any]							=  inputs_dict['input_ids']

             a_ :		Tuple							=  input_ids[:1, :]
             a_ :		Union[str, Any]							=  inputs_dict['attention_mask'][:1, :]
             a_ :		Tuple							=  1

             # first forward pass
             a_ :		Union[str, Any]							=  model(SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					,							use_cache=SCREAMING_SNAKE_CASE__					)

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

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

             # append to next input_ids and
             a_ :		Dict							=  tf.concat([input_ids, next_tokens]					,							axis=-1					)
             a_ :		str							=  tf.concat([attention_mask, next_attn_mask]					,							axis=-1					)

             a_ :		Any							=  model(SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					)[0]
             a_ :		Optional[Any]							=  model(SCREAMING_SNAKE_CASE__					,							attention_mask=SCREAMING_SNAKE_CASE__					,							past_key_values=SCREAMING_SNAKE_CASE__					)[0]

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

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

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







def 	SCREAMING_SNAKE_CASE_						(      __A			:      str  ,   __A			:      Union[str, Any]  ,   __A			:      Optional[int]  ,   __A			:      Optional[int]=None  ,   __A			:      Dict=None  ,   __A			:      Union[str, Any]=None  ,   __A			:      str=None  ,   )     ->		Dict:

       """simple docstring"""

       if attention_mask is None:
              a_ :		Any							=  tf.cast(tf.math.not_equal(__A  ,   config.pad_token_id    )  ,   tf.inta    )
       if decoder_attention_mask is None:
              a_ :		Union[str, Any]							=  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:
              a_ :		int							=  tf.ones((config.encoder_layers, config.encoder_attention_heads)    )
       if decoder_head_mask is None:
              a_ :		List[str]							=  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 			SCREAMING_SNAKE_CASE__  (    lowercase__						,   lowercase__						,   unittest.TestCase      ):
      snake_case__    :					str					       =						(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
      snake_case__    :					int					       =						(TFLEDForConditionalGeneration,) if is_tf_available() else ()
      snake_case__    :					str					       =						(
          {
              '''conversational''': TFLEDForConditionalGeneration,
              '''feature-extraction''': TFLEDModel,
              '''summarization''': TFLEDForConditionalGeneration,
              '''text2text-generation''': TFLEDForConditionalGeneration,
              '''translation''': TFLEDForConditionalGeneration,
          }
          if is_tf_available()
          else {}
      )
      snake_case__    :					str					       =						True
      snake_case__    :					List[str]					       =						False
      snake_case__    :					Any					       =						False
      snake_case__    :					List[Any]					       =						False
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Union[str, Any]:
             a_ :		List[str]							=  TFLEDModelTester(self					)
             a_ :		List[str]							=  ConfigTester(self					,							config_class=SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	str					)		->      Union[str, Any]:
             self.config_tester.run_common_tests()
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[str]					)		->      str:
             a_ :		str							=  self.model_tester.prepare_config_and_inputs_for_common()
             self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE__					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      Optional[Any]:
             a_							,		a_ :		Tuple							=  self.model_tester.prepare_config_and_inputs_for_common()
             a_ :		Tuple							=  tf.zeros_like(inputs_dict['attention_mask']					)
             a_ :		Optional[int]							=  2
             a_ :		int							=  tf.where(
                 tf.range(self.model_tester.seq_length					)[None, :] < num_global_attn_indices					,							1					,							inputs_dict['global_attention_mask']					,							)

             a_ :		Dict							=  True
             a_ :		Union[str, Any]							=  self.model_tester.seq_length
             a_ :		str							=  self.model_tester.encoder_seq_length

             def check_decoder_attentions_output(SCREAMING_SNAKE_CASE__    :	Any					):
                    a_ :		Optional[Any]							=  outputs.decoder_attentions
                    self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							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(SCREAMING_SNAKE_CASE__    :	Optional[Any]					):
                    a_ :		Any							=  [t.numpy() for t in outputs.encoder_attentions]
                    a_ :		Any							=  [t.numpy() for t in outputs.encoder_global_attentions]
                    self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							self.model_tester.num_hidden_layers					)
                    self.assertEqual(len(SCREAMING_SNAKE_CASE__					)					,							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:
                    a_ :		Optional[int]							=  True
                    a_ :		List[str]							=  False
                    a_ :		int							=  False
                    a_ :		Union[str, Any]							=  model_class(SCREAMING_SNAKE_CASE__					)
                    a_ :		Any							=  model(self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)
                    a_ :		Any							=  len(SCREAMING_SNAKE_CASE__					)
                    self.assertEqual(config.output_hidden_states					,							SCREAMING_SNAKE_CASE__					)
                    check_encoder_attentions_output(SCREAMING_SNAKE_CASE__					)

                    if self.is_encoder_decoder:
                           a_ :		Optional[Any]							=  model_class(SCREAMING_SNAKE_CASE__					)
                           a_ :		List[Any]							=  model(self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)
                           self.assertEqual(config.output_hidden_states					,							SCREAMING_SNAKE_CASE__					)
                           check_decoder_attentions_output(SCREAMING_SNAKE_CASE__					)

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

                    # Check attention is always last and order is fine
                    a_ :		str							=  True
                    a_ :		Optional[int]							=  True
                    a_ :		Any							=  model_class(SCREAMING_SNAKE_CASE__					)
                    a_ :		Optional[Any]							=  model(self._prepare_for_class(SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)					)

                    self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)					,							len(SCREAMING_SNAKE_CASE__					)					)
                    self.assertEqual(model.config.output_hidden_states					,							SCREAMING_SNAKE_CASE__					)
                    check_encoder_attentions_output(SCREAMING_SNAKE_CASE__					)
      @unittest.skip('LED keeps using potentially symbolic tensors in conditionals and breaks tracing.'					)
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      Optional[int]:
             pass

      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[int]					)		->      List[str]:
             # TODO: Head-masking not yet implement
             pass







def 	SCREAMING_SNAKE_CASE_						(      __A			:      Dict    )     ->		Union[str, Any]:

       """simple docstring"""

       return tf.constant(__A  ,   dtype=tf.intaa    )


UpperCAmelCase_				:			Union[str, Any]   =	1e-4
@slow
@require_tf
class 			SCREAMING_SNAKE_CASE__  (    unittest.TestCase      ):
      def 	SCREAMING_SNAKE_CASE							(	self    :	List[Any]					)		->      int:
             a_ :		Tuple							=  TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'					).led

             # change to intended input here
             a_ :		str							=  _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]					)
             a_ :		int							=  _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]					)
             a_ :		Tuple							=  prepare_led_inputs_dict(model.config					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Tuple							=  model(**SCREAMING_SNAKE_CASE__					)[0]
             a_ :		str							=  (1, 1_0_2_4, 7_6_8)
             self.assertEqual(output.shape					,							SCREAMING_SNAKE_CASE__					)
             # change to expected output here
             a_ :		str							=  tf.convert_to_tensor(
                 [[2.3050, 2.8279, 0.6531], [-1.8457, -0.1455, -3.5661], [-1.0186, 0.4586, -2.2043]]					,							)
             tf.debugging.assert_near(output[:, :3, :3]					,							SCREAMING_SNAKE_CASE__					,							atol=1E-3					)

      def 	SCREAMING_SNAKE_CASE							(	self    :	Optional[Any]					)		->      List[Any]:
             a_ :		str							=  TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'					)

             # change to intended input here
             a_ :		List[Any]							=  _long_tensor([5_1_2 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]					)
             a_ :		Any							=  _long_tensor([1_2_8 * [0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9]]					)
             a_ :		List[Any]							=  prepare_led_inputs_dict(model.config					,							SCREAMING_SNAKE_CASE__					,							SCREAMING_SNAKE_CASE__					)
             a_ :		Tuple							=  model(**SCREAMING_SNAKE_CASE__					)[0]
             a_ :		int							=  (1, 1_0_2_4, model.config.vocab_size)
             self.assertEqual(output.shape					,							SCREAMING_SNAKE_CASE__					)
             # change to expected output here
             a_ :		str							=  tf.convert_to_tensor(
                 [[33.6507, 6.4572, 16.8089], [5.8739, -2.4238, 11.2902], [-3.2139, -4.3149, 4.2783]]					,							)
             tf.debugging.assert_near(output[:, :3, :3]					,							SCREAMING_SNAKE_CASE__					,							atol=1E-3					,							rtol=1E-3					)






def 	SCREAMING_SNAKE_CASE_						(      __A			:      int    )     ->		int:

       """simple docstring"""

       if not isinstance(__A  ,   __A    ):
              raise ValueError('Input must be an integer'    )
       if input_num <= 0:
              raise ValueError('Input must be positive'    )
       return sum(
           divisor for divisor in range(1  ,   input_num // 2 + 1    ) if input_num % divisor == 0    )


if __name__ == "__main__":
   import doctest

   doctest.testmod()


