
'''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 							UpperCAmelCase_							:

 '''simple docstring'''
 lowercase_					:    Tuple				=							LEDConfig
 lowercase_					:    Optional[Any]				=							{}
 lowercase_					:    str				=							"gelu"







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



   '''simple docstring'''




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

   UpperCAmelCase__      :					int				       =							hidden_dropout_prob
   UpperCAmelCase__      :					int				       =							attention_probs_dropout_prob
   UpperCAmelCase__      :					int				       =							max_position_embeddings
   UpperCAmelCase__      :					List[str]				       =							eos_token_id
   UpperCAmelCase__      :					List[Any]				       =							pad_token_id
   UpperCAmelCase__      :					str				       =							bos_token_id
   UpperCAmelCase__      :					int				       =							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
   UpperCAmelCase__      :					int				       =							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
   UpperCAmelCase__      :					Optional[int]				       =							(
       self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
   )







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__      :					Optional[int]				       =							ids_tensor([self.batch_size, self.seq_length - 1]			,				self.vocab_size )
   UpperCAmelCase__      :					Dict				       =							tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size )			,				1 )
   UpperCAmelCase__      :					int				       =							tf.concat([input_ids, eos_tensor]			,				axis=1 )

   UpperCAmelCase__      :					Any				       =							ids_tensor([self.batch_size, self.seq_length]			,				self.vocab_size )

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







 def 							UpperCamelCase  (					self		:  List[Any]			,				snake_case__		:  Union[str, Any]			,				snake_case__		:  Optional[Any] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							TFLEDModel(config=snake_case__ ).get_decoder()
   UpperCAmelCase__      :					Optional[int]				       =							inputs_dict["input_ids"]

   UpperCAmelCase__      :					str				       =							input_ids[:1, :]
   UpperCAmelCase__      :					Optional[Any]				       =							inputs_dict["attention_mask"][:1, :]
   UpperCAmelCase__      :					Dict				       =							1

   # first forward pass
   UpperCAmelCase__      :					List[str]				       =							model(snake_case__			,				attention_mask=snake_case__			,				use_cache=snake_case__ )

   UpperCAmelCase__       ,			UpperCAmelCase__      :					Optional[int]				       =							outputs.to_tuple()

   # create hypothetical next token and extent to next_input_ids
   UpperCAmelCase__      :					Optional[int]				       =							ids_tensor((self.batch_size, 3)			,				config.vocab_size )
   UpperCAmelCase__      :					Tuple				       =							tf.cast(ids_tensor((self.batch_size, 3)			,				2 )			,				tf.inta )

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

   UpperCAmelCase__      :					Union[str, Any]				       =							model(snake_case__			,				attention_mask=snake_case__ )[0]
   UpperCAmelCase__      :					List[str]				       =							model(snake_case__			,				attention_mask=snake_case__			,				past_key_values=snake_case__ )[0]

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

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

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







def snake_case_     (					lowercase__   ,       lowercase__   ,       lowercase__   ,       lowercase__=None   ,       lowercase__=None   ,       lowercase__=None   ,       lowercase__=None   ,       ):
  if attention_mask is None:
    UpperCAmelCase__      :					str				       =							tf.cast(tf.math.not_equal(lowercase__   ,       config.pad_token_id		)   ,       tf.inta		)
  if decoder_attention_mask is None:
    UpperCAmelCase__      :					str				       =							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:
    UpperCAmelCase__      :					Tuple				       =							tf.ones((config.encoder_layers, config.encoder_attention_heads)		)
  if decoder_head_mask is None:
    UpperCAmelCase__      :					Dict				       =							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 							UpperCAmelCase_							(    A					,				A					,				unittest.TestCase				):

 '''simple docstring'''
 lowercase_					:    List[Any]				=							(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
 lowercase_					:    Dict				=							(TFLEDForConditionalGeneration,) if is_tf_available() else ()
 lowercase_					:    Union[str, Any]				=							(
     {
         "conversational": TFLEDForConditionalGeneration,
         "feature-extraction": TFLEDModel,
         "summarization": TFLEDForConditionalGeneration,
         "text2text-generation": TFLEDForConditionalGeneration,
         "translation": TFLEDForConditionalGeneration,
     }
     if is_tf_available()
     else {}
 )
 lowercase_					:    Tuple				=							True
 lowercase_					:    Any				=							False
 lowercase_					:    List[str]				=							False
 lowercase_					:    List[str]				=							False







 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




   UpperCAmelCase__      :					str				       =							TFLEDModelTester(self )
   UpperCAmelCase__      :					Union[str, Any]				       =							ConfigTester(self			,				config_class=snake_case__ )







 def 							UpperCamelCase  (					self		:  List[Any] ):



   '''simple docstring'''




   self.config_tester.run_common_tests()







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   UpperCAmelCase__      :					List[str]				       =							self.model_tester.prepare_config_and_inputs_for_common()
   self.model_tester.check_decoder_model_past_large_inputs(*snake_case__ )







 def 							UpperCamelCase  (					self		:  str ):



   '''simple docstring'''




   UpperCAmelCase__       ,			UpperCAmelCase__      :					List[Any]				       =							self.model_tester.prepare_config_and_inputs_for_common()
   UpperCAmelCase__      :					Dict				       =							tf.zeros_like(inputs_dict["attention_mask"] )
   UpperCAmelCase__      :					List[Any]				       =							2
   UpperCAmelCase__      :					Any				       =							tf.where(
       tf.range(self.model_tester.seq_length )[None, :] < num_global_attn_indices			,				1			,				inputs_dict["global_attention_mask"]			,				)

   UpperCAmelCase__      :					Tuple				       =							True
   UpperCAmelCase__      :					Tuple				       =							self.model_tester.seq_length
   UpperCAmelCase__      :					List[Any]				       =							self.model_tester.encoder_seq_length

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

     if self.is_encoder_decoder:
       UpperCAmelCase__      :					Tuple				       =							model_class(snake_case__ )
       UpperCAmelCase__      :					List[str]				       =							model(self._prepare_for_class(snake_case__			,				snake_case__ ) )
       self.assertEqual(config.output_hidden_states			,				snake_case__ )
       check_decoder_attentions_output(snake_case__ )

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

     # Check attention is always last and order is fine
     UpperCAmelCase__      :					Optional[Any]				       =							True
     UpperCAmelCase__      :					Any				       =							True
     UpperCAmelCase__      :					Tuple				       =							model_class(snake_case__ )
     UpperCAmelCase__      :					Dict				       =							model(self._prepare_for_class(snake_case__			,				snake_case__ ) )

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







 @unittest.skip("LED keeps using potentially symbolic tensors in conditionals and breaks tracing." )
 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




   pass







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''simple docstring'''




   pass







def snake_case_     (					lowercase__		):
  return tf.constant(lowercase__   ,       dtype=tf.intaa		)


SCREAMING_SNAKE_CASE     =       1E-4






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

 '''simple docstring'''







 def 							UpperCamelCase  (					self		:  int ):



   '''simple docstring'''




   UpperCAmelCase__      :					Tuple				       =							TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384" ).led

   # change to intended input here
   UpperCAmelCase__      :					Dict				       =							_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] )
   UpperCAmelCase__      :					List[Any]				       =							_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] )
   UpperCAmelCase__      :					Tuple				       =							prepare_led_inputs_dict(model.config			,				snake_case__			,				snake_case__ )
   UpperCAmelCase__      :					Tuple				       =							model(**snake_case__ )[0]
   UpperCAmelCase__      :					Dict				       =							(1, 10_24, 7_68)
   self.assertEqual(output.shape			,				snake_case__ )
   # change to expected output here
   UpperCAmelCase__      :					Any				       =							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]			,				snake_case__			,				atol=1e-3 )







 def 							UpperCamelCase  (					self		:  Optional[int] ):



   '''simple docstring'''




   UpperCAmelCase__      :					Any				       =							TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384" )

   # change to intended input here
   UpperCAmelCase__      :					List[str]				       =							_long_tensor([5_12 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] )
   UpperCAmelCase__      :					int				       =							_long_tensor([1_28 * [0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69]] )
   UpperCAmelCase__      :					Any				       =							prepare_led_inputs_dict(model.config			,				snake_case__			,				snake_case__ )
   UpperCAmelCase__      :					Optional[Any]				       =							model(**snake_case__ )[0]
   UpperCAmelCase__      :					List[str]				       =							(1, 10_24, model.config.vocab_size)
   self.assertEqual(output.shape			,				snake_case__ )
   # change to expected output here
   UpperCAmelCase__      :					Tuple				       =							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]			,				snake_case__			,				atol=1e-3			,				rtol=1e-3 )





'''simple docstring'''





from math import factorial

SCREAMING_SNAKE_CASE     =       {str(digit): factorial(digit) for digit in range(1_0)}







def snake_case_     (					lowercase__		):
  if not isinstance(lowercase__   ,       lowercase__		):
    raise TypeError("Parameter number must be int"		)

  if number < 0:
    raise ValueError("Parameter number must be greater than or equal to 0"		)

  # Converts number in string to iterate on its digits and adds its factorial.
  return sum(DIGIT_FACTORIAL[digit] for digit in str(lowercase__		)		)







def snake_case_     (					lowercase__ = 6_0   ,       lowercase__ = 1_0_0_0_0_0_0		):

  if not isinstance(lowercase__   ,       lowercase__		) or not isinstance(lowercase__   ,       lowercase__		):
    raise TypeError("Parameters chain_length and number_limit must be int"		)

  if chain_length <= 0 or number_limit <= 0:
    raise ValueError(
        "Parameters chain_length and number_limit must be greater than 0"		)

  # the counter for the chains with the exact desired length
  UpperCAmelCase__      :					Union[str, Any]				       =							0
  # the cached sizes of the previous chains
  UpperCAmelCase__      :					dict[int, int]				       =							{}

  for start_chain_element in range(1   ,       lowercase__		):
    # The temporary set will contain the elements of the chain
    UpperCAmelCase__      :					Any				       =							set()
    UpperCAmelCase__      :					int				       =							0

    # Stop computing the chain when you find a cached size, a repeating item or the
    # length is greater then the desired one.
    UpperCAmelCase__      :					List[str]				       =							start_chain_element
    while (
        chain_element not in chain_sets_lengths
        and chain_element not in chain_set
        and chain_set_length <= chain_length
    ):
      chain_set.add(lowercase__		)
      chain_set_length += 1
      UpperCAmelCase__      :					List[str]				       =							digit_factorial_sum(lowercase__		)

    if chain_element in chain_sets_lengths:
      chain_set_length += chain_sets_lengths[chain_element]

    UpperCAmelCase__      :					List[str]				       =							chain_set_length

    # If chain contains the exact amount of elements increase the counter
    if chain_set_length == chain_length:
      chains_counter += 1

  return chains_counter


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      print(F'{solution()}')



