






'''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 _lowercase	:
							a				      =       LEDConfig
							a				      =       {}
							a				      =       """gelu"""




							def __init__(							self:				Optional[Any]			,							UpperCamelCase__:				Union[str, Any]			,							UpperCamelCase__:				List[Any]=13			,							UpperCamelCase__:				Tuple=7			,							UpperCamelCase__:				Union[str, Any]=True			,							UpperCamelCase__:				List[str]=False			,							UpperCamelCase__:				List[str]=99			,							UpperCamelCase__:				Any=32			,							UpperCamelCase__:				int=2			,							UpperCamelCase__:				str=4			,							UpperCamelCase__:				List[str]=37			,							UpperCamelCase__:				Optional[int]=0.1			,							UpperCamelCase__:				List[str]=0.1			,							UpperCamelCase__:				List[str]=20			,							UpperCamelCase__:				Dict=2			,							UpperCamelCase__:				int=1			,							UpperCamelCase__:				int=0			,							UpperCamelCase__:				int=4			,							):
														lowerCamelCase__					:	Any         =       parent
														lowerCamelCase__					:	Optional[int]         =       batch_size
														lowerCamelCase__					:	Optional[Any]         =       seq_length
														lowerCamelCase__					:	Optional[Any]         =       is_training
														lowerCamelCase__					:	List[Any]         =       use_labels
														lowerCamelCase__					:	Optional[int]         =       vocab_size
														lowerCamelCase__					:	Union[str, Any]         =       hidden_size
														lowerCamelCase__					:	List[Any]         =       num_hidden_layers
														lowerCamelCase__					:	str         =       num_attention_heads
														lowerCamelCase__					:	Tuple         =       intermediate_size

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




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

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

														lowerCamelCase__					:	Dict         =       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__					:	int         =       prepare_led_inputs_dict(UpperCamelCase__			,							UpperCamelCase__			,							UpperCamelCase__     )
														lowerCamelCase__					:	List[str]         =       tf.concat(
														    [tf.zeros_like(UpperCamelCase__     )[:, :-1], tf.ones_like(UpperCamelCase__     )[:, -1:]]			,							axis=-1			,							)
														lowerCamelCase__					:	Optional[int]         =       global_attention_mask
														return config, inputs_dict




							def 		lowerCamelCase_				(							self:				Optional[int]			,							UpperCamelCase__:				Optional[Any]			,							UpperCamelCase__:				str     ):
														lowerCamelCase__					:	int         =       TFLEDModel(config=UpperCamelCase__     ).get_decoder()
														lowerCamelCase__					:	Optional[int]         =       inputs_dict["""input_ids"""]

														lowerCamelCase__					:	Optional[int]         =       input_ids[:1, :]
														lowerCamelCase__					:	Dict         =       inputs_dict["""attention_mask"""][:1, :]
														lowerCamelCase__					:	List[str]         =       1

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

														lowerCamelCase__  ,		lowerCamelCase__					:	int         =       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__					:	Optional[Any]         =       tf.cast(ids_tensor((self.batch_size, 3)			,							2     )			,							tf.inta     )

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

														lowerCamelCase__					:	List[Any]         =       model(UpperCamelCase__			,							attention_mask=UpperCamelCase__     )[0]
														lowerCamelCase__					:	Union[str, Any]         =       model(UpperCamelCase__			,							attention_mask=UpperCamelCase__			,							past_key_values=UpperCamelCase__     )[0]

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

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

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





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase=None				,			UpperCamelCase=None				,			UpperCamelCase=None				,			UpperCamelCase=None				,			)      ->       int:
							if attention_mask is None:
														lowerCamelCase__					:	Any         =       tf.cast(tf.math.not_equal(UpperCamelCase				,			config.pad_token_id      )				,			tf.inta      )
							if decoder_attention_mask is None:
														lowerCamelCase__					:	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:
														lowerCamelCase__					:	Optional[int]         =       tf.ones((config.encoder_layers, config.encoder_attention_heads)      )
							if decoder_head_mask is None:
														lowerCamelCase__					:	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 _lowercase	(				_lowercase				,      _lowercase				,      unittest.TestCase		):
							a				      =       (TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
							a				      =       (TFLEDForConditionalGeneration,) if is_tf_available() else ()
							a				      =       (
							    {
							        """conversational""": TFLEDForConditionalGeneration,
							        """feature-extraction""": TFLEDModel,
							        """summarization""": TFLEDForConditionalGeneration,
							        """text2text-generation""": TFLEDForConditionalGeneration,
							        """translation""": TFLEDForConditionalGeneration,
							    }
							    if is_tf_available()
							    else {}
							)
							a				      =       True
							a				      =       False
							a				      =       False
							a				      =       False




							def 		lowerCamelCase_				(							self:				str     ):
														lowerCamelCase__					:	Union[str, Any]         =       TFLEDModelTester(self     )
														lowerCamelCase__					:	Tuple         =       ConfigTester(self			,							config_class=UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				List[str]     ):
														self.config_tester.run_common_tests()




							def 		lowerCamelCase_				(							self:				List[Any]     ):
														lowerCamelCase__					:	Optional[int]         =       self.model_tester.prepare_config_and_inputs_for_common()
														self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase__     )




							def 		lowerCamelCase_				(							self:				Union[str, Any]     ):
														lowerCamelCase__  ,		lowerCamelCase__					:	Optional[int]         =       self.model_tester.prepare_config_and_inputs_for_common()
														lowerCamelCase__					:	int         =       tf.zeros_like(inputs_dict["""attention_mask"""]     )
														lowerCamelCase__					:	Union[str, Any]         =       2
														lowerCamelCase__					:	List[Any]         =       tf.where(
														    tf.range(self.model_tester.seq_length     )[None, :] < num_global_attn_indices			,							1			,							inputs_dict["""global_attention_mask"""]			,							)

														lowerCamelCase__					:	List[str]         =       True
														lowerCamelCase__					:	Dict         =       self.model_tester.seq_length
														lowerCamelCase__					:	Tuple         =       self.model_tester.encoder_seq_length

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

																					if self.is_encoder_decoder:
																												lowerCamelCase__					:	List[Any]         =       model_class(UpperCamelCase__     )
																												lowerCamelCase__					:	Tuple         =       model(self._prepare_for_class(UpperCamelCase__			,							UpperCamelCase__     )     )
																												self.assertEqual(config.output_hidden_states			,							UpperCamelCase__     )
																												check_decoder_attentions_output(UpperCamelCase__     )

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

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

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




							@unittest.skip("""LED keeps using potentially symbolic tensors in conditionals and breaks tracing."""     )
							def 		lowerCamelCase_				(							self:				List[str]     ):
														pass




							def 		lowerCamelCase_				(							self:				Tuple     ):
														# TODO: Head-masking not yet implement
														pass





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase      )      ->       Any:
							return tf.constant(UpperCamelCase				,			dtype=tf.intaa      )


_A	:			Any         =1e-4



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




							def 		lowerCamelCase_				(							self:				List[str]     ):
														lowerCamelCase__					:	List[str]         =       TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""     ).led

														# change to intended input here
														lowerCamelCase__					:	Union[str, Any]         =       _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__					:	Union[str, Any]         =       prepare_led_inputs_dict(model.config			,							UpperCamelCase__			,							UpperCamelCase__     )
														lowerCamelCase__					:	Tuple         =       model(**UpperCamelCase__     )[0]
														lowerCamelCase__					:	Union[str, Any]         =       (1, 1_024, 768)
														self.assertEqual(output.shape			,							UpperCamelCase__     )
														# change to expected output here
														lowerCamelCase__					:	Tuple         =       tf.convert_to_tensor(
														    [[2.3_050, 2.8_279, 0.6_531], [-1.8_457, -0.1_455, -3.5_661], [-1.0_186, 0.4_586, -2.2_043]]			,							)
														tf.debugging.assert_near(output[:, :3, :3]			,							UpperCamelCase__			,							atol=1e-3     )




							def 		lowerCamelCase_				(							self:				List[Any]     ):
														lowerCamelCase__					:	Optional[int]         =       TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""     )

														# change to intended input here
														lowerCamelCase__					:	int         =       _long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]     )
														lowerCamelCase__					:	Union[str, Any]         =       _long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]     )
														lowerCamelCase__					:	List[str]         =       prepare_led_inputs_dict(model.config			,							UpperCamelCase__			,							UpperCamelCase__     )
														lowerCamelCase__					:	Optional[int]         =       model(**UpperCamelCase__     )[0]
														lowerCamelCase__					:	Dict         =       (1, 1_024, model.config.vocab_size)
														self.assertEqual(output.shape			,							UpperCamelCase__     )
														# change to expected output here
														lowerCamelCase__					:	Union[str, Any]         =       tf.convert_to_tensor(
														    [[33.6_507, 6.4_572, 16.8_089], [5.8_739, -2.4_238, 11.2_902], [-3.2_139, -4.3_149, 4.2_783]]			,							)
														tf.debugging.assert_near(output[:, :3, :3]			,							UpperCamelCase__			,							atol=1e-3			,							rtol=1e-3     )









'''simple docstring'''







_A	:			Union[str, Any]         =range(2, 20 + 1)
_A	:			List[str]         =[10**k for k in range(ks[-1] + 1)]
_A	:			dict[int, dict[int, list[list[int]]]]         ={}





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase      )      ->       List[Any]:
							lowerCamelCase__					:	List[str]         =       sum(a_i[j] for j in range(UpperCamelCase				,			len(UpperCamelCase      )      )      )
							lowerCamelCase__					:	int         =       sum(a_i[j] * base[j] for j in range(min(len(UpperCamelCase      )				,			UpperCamelCase      )      )      )

							lowerCamelCase__  ,		lowerCamelCase__					:	Dict         =       0, 0
							lowerCamelCase__					:	List[str]         =       n - i

							lowerCamelCase__					:	Optional[Any]         =       memo.get(UpperCamelCase      )

							if sub_memo is not None:
														lowerCamelCase__					:	str         =       sub_memo.get(UpperCamelCase      )

														if jumps is not None and len(UpperCamelCase      ) > 0:
																					# find and make the largest jump without going over
																					lowerCamelCase__					:	Optional[Any]         =       -1
																					for _k in range(len(UpperCamelCase      ) - 1				,			-1				,			-1      ):
																												if jumps[_k][2] <= k and jumps[_k][1] <= max_dn:
																																			lowerCamelCase__					:	Dict         =       _k
																																			break

																					if max_jump >= 0:
																												lowerCamelCase__  ,		lowerCamelCase__  ,		lowerCamelCase__					:	str         =       jumps[max_jump]
																												# since the difference between jumps is cached, add c
																												lowerCamelCase__					:	Dict         =       diff + c
																												for j in range(min(UpperCamelCase				,			len(UpperCamelCase      )      )      ):
																																			lowerCamelCase__  ,		lowerCamelCase__					:	List[Any]         =       divmod(UpperCamelCase				,			10      )
																												if new_c > 0:
																																			add(UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase      )

														else:
																					lowerCamelCase__					:	Any         =       []
							else:
														lowerCamelCase__					:	str         =       {c: []}
														lowerCamelCase__					:	Tuple         =       sub_memo

							if dn >= max_dn or c + diff >= base[k]:
														return diff, dn

							if k > ks[0]:
														while True:
																					# keep doing smaller jumps
																					lowerCamelCase__  ,		lowerCamelCase__					:	Dict         =       next_term(UpperCamelCase				,			k - 1				,			i + dn				,			UpperCamelCase      )
																					diff += _diff
																					dn += terms_jumped

																					if dn >= max_dn or c + diff >= base[k]:
																												break
							else:
														# would be too small a jump, just compute sequential terms instead
														lowerCamelCase__  ,		lowerCamelCase__					:	Optional[int]         =       compute(UpperCamelCase				,			UpperCamelCase				,			i + dn				,			UpperCamelCase      )
														diff += _diff
														dn += terms_jumped

							lowerCamelCase__					:	str         =       sub_memo[c]

							# keep jumps sorted by # of terms skipped
							lowerCamelCase__					:	List[Any]         =       0
							while j < len(UpperCamelCase      ):
														if jumps[j][1] > dn:
																					break
														j += 1

							# cache the jump for this value digitsum(b) and c
							sub_memo[c].insert(UpperCamelCase				,			(diff, dn, k)      )
							return (diff, dn)





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase      )      ->       List[Any]:
							if i >= n:
														return 0, i
							if k > len(UpperCamelCase      ):
														a_i.extend([0 for _ in range(k - len(UpperCamelCase      )      )]      )

							# note: a_i -> b * 10^k + c
							# ds_b -> digitsum(b)
							# ds_c -> digitsum(c)
							lowerCamelCase__					:	Optional[Any]         =       i
							lowerCamelCase__  ,		lowerCamelCase__  ,		lowerCamelCase__					:	Optional[int]         =       0, 0, 0
							for j in range(len(UpperCamelCase      )      ):
														if j >= k:
																					ds_b += a_i[j]
														else:
																					ds_c += a_i[j]

							while i < n:
														i += 1
														lowerCamelCase__					:	Optional[int]         =       ds_c + ds_b
														diff += addend
														lowerCamelCase__					:	int         =       0
														for j in range(UpperCamelCase      ):
																					lowerCamelCase__					:	str         =       a_i[j] + addend
																					lowerCamelCase__  ,		lowerCamelCase__					:	int         =       divmod(UpperCamelCase				,			10      )

																					ds_c += a_i[j]

														if addend > 0:
																					break

							if addend > 0:
														add(UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase      )
							return diff, i - start_i





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase				,			UpperCamelCase				,			UpperCamelCase      )      ->       str:
							for j in range(UpperCamelCase				,			len(UpperCamelCase      )      ):
														lowerCamelCase__					:	List[Any]         =       digits[j] + addend
														if s >= 10:
																					lowerCamelCase__  ,		lowerCamelCase__					:	Union[str, Any]         =       divmod(UpperCamelCase				,			10      )
																					lowerCamelCase__					:	Any         =       addend // 10 + quotient
														else:
																					lowerCamelCase__					:	Any         =       s
																					lowerCamelCase__					:	Optional[Any]         =       addend // 10

														if addend == 0:
																					break

							while addend > 0:
														lowerCamelCase__  ,		lowerCamelCase__					:	Any         =       divmod(UpperCamelCase				,			10      )
														digits.append(UpperCamelCase      )





def  SCREAMING_SNAKE_CASE_       (UpperCamelCase = 10**15      )      ->       int:
							lowerCamelCase__					:	Any         =       [1]
							lowerCamelCase__					:	List[str]         =       1
							lowerCamelCase__					:	Tuple         =       0
							while True:
														lowerCamelCase__  ,		lowerCamelCase__					:	Any         =       next_term(UpperCamelCase				,			20				,			i + dn				,			UpperCamelCase      )
														dn += terms_jumped
														if dn == n - i:
																					break

							lowerCamelCase__					:	Union[str, Any]         =       0
							for j in range(len(UpperCamelCase      )      ):
														a_n += digits[j] * 10**j
							return a_n


if __name__ == "__main__":
			print(F'{solution() = }')

