



'''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_				:
	lowercase    =				LEDConfig
	lowercase    =				{}
	lowercase    =				'gelu'





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

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





	def 				_lowercase(						self					)    ->    Optional[int]:
								UpperCAmelCase  :	Any							  =	ids_tensor([self.batch_size, self.seq_length - 1]	,	self.vocab_size					)
								UpperCAmelCase  :	Tuple							  =	tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size					)	,	1					)
								UpperCAmelCase  :	str							  =	tf.concat([input_ids, eos_tensor]	,	axis=1					)

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

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






	def 				_lowercase(						self	,	A	,	A					)    ->    Optional[int]:
								UpperCAmelCase  :	Union[str, Any]							  =	TFLEDModel(config=A					).get_decoder()
								UpperCAmelCase  :	Union[str, Any]							  =	inputs_dict["""input_ids"""]

								UpperCAmelCase  :	str							  =	input_ids[:1, :]
								UpperCAmelCase  :	int							  =	inputs_dict["""attention_mask"""][:1, :]
								UpperCAmelCase  :	str							  =	1

								# first forward pass
								UpperCAmelCase  :	Optional[int]							  =	model(A	,	attention_mask=A	,	use_cache=A					)

								UpperCAmelCase  ,  UpperCAmelCase  :	Dict							  =	outputs.to_tuple()

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

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

								UpperCAmelCase  :	Dict							  =	model(A	,	attention_mask=A					)[0]
								UpperCAmelCase  :	List[Any]							  =	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
								UpperCAmelCase  :	Tuple							  =	int(ids_tensor((1,)	,	output_from_past.shape[-1]					)					)
								UpperCAmelCase  :	Optional[Any]							  =	output_from_no_past[:, -3:, random_slice_idx]
								UpperCAmelCase  :	Tuple							  =	output_from_past[:, :, random_slice_idx]

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




def    __lowerCamelCase						(      _lowercase			,       _lowercase			,       _lowercase			,       _lowercase=None			,       _lowercase=None			,       _lowercase=None			,       _lowercase=None			,       )   ->    Optional[Any]:
							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  :	List[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:
														UpperCAmelCase  :	Dict							  =	tf.ones((config.encoder_layers, config.encoder_attention_heads)	)
							if decoder_head_mask is None:
														UpperCAmelCase  :	Union[str, 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     UpperCamelCase_				(							__magic_name__   ,  __magic_name__   ,  unittest.TestCase    ):
	lowercase    =				(TFLEDForConditionalGeneration, TFLEDModel) if is_tf_available() else ()
	lowercase    =				(TFLEDForConditionalGeneration,) if is_tf_available() else ()
	lowercase    =				(
	    {
	        'conversational': TFLEDForConditionalGeneration,
	        'feature-extraction': TFLEDModel,
	        'summarization': TFLEDForConditionalGeneration,
	        'text2text-generation': TFLEDForConditionalGeneration,
	        'translation': TFLEDForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	lowercase    =				True
	lowercase    =				False
	lowercase    =				False
	lowercase    =				False





	def 				_lowercase(						self					)    ->    Optional[Any]:
								UpperCAmelCase  :	Optional[int]							  =	TFLEDModelTester(self					)
								UpperCAmelCase  :	Any							  =	ConfigTester(self	,	config_class=A					)





	def 				_lowercase(						self					)    ->    Optional[int]:
								self.config_tester.run_common_tests()





	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	Optional[Any]							  =	self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.check_decoder_model_past_large_inputs(*A					)





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

								UpperCAmelCase  :	int							  =	True
								UpperCAmelCase  :	Any							  =	self.model_tester.seq_length
								UpperCAmelCase  :	Any							  =	self.model_tester.encoder_seq_length

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

															if self.is_encoder_decoder:
																						UpperCAmelCase  :	Dict							  =	model_class(A					)
																						UpperCAmelCase  :	Dict							  =	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"]
															UpperCAmelCase  :	Any							  =	True
															UpperCAmelCase  :	Dict							  =	model_class(A					)
															UpperCAmelCase  :	int							  =	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
															UpperCAmelCase  :	Any							  =	True
															UpperCAmelCase  :	Any							  =	True
															UpperCAmelCase  :	int							  =	model_class(A					)
															UpperCAmelCase  :	Dict							  =	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 				_lowercase(						self					)    ->    List[Any]:
								pass






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




def    __lowerCamelCase						(      _lowercase	)   ->    List[str]:
							return tf.constant(_lowercase			,       dtype=tf.intaa	)


a     :    int      						=					1E-4


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





	def 				_lowercase(						self					)    ->    List[str]:
								UpperCAmelCase  :	Optional[int]							  =	TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""					).led

								# change to intended input here
								UpperCAmelCase  :	List[Any]							  =	_long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]					)
								UpperCAmelCase  :	Optional[Any]							  =	_long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]					)
								UpperCAmelCase  :	Tuple							  =	prepare_led_inputs_dict(model.config	,	A	,	A					)
								UpperCAmelCase  :	List[Any]							  =	model(**A					)[0]
								UpperCAmelCase  :	Optional[Any]							  =	(1, 1024, 768)
								self.assertEqual(output.shape	,	A					)
								# change to expected output here
								UpperCAmelCase  :	int							  =	tf.convert_to_tensor(
								    [[2.3_0_5_0, 2.8_2_7_9, 0.6_5_3_1], [-1.8_4_5_7, -0.1_4_5_5, -3.5_6_6_1], [-1.0_1_8_6, 0.4_5_8_6, -2.2_0_4_3]]	,	)
								tf.debugging.assert_near(output[:, :3, :3]	,	A	,	atol=1e-3					)






	def 				_lowercase(						self					)    ->    str:
								UpperCAmelCase  :	str							  =	TFLEDForConditionalGeneration.from_pretrained("""allenai/led-base-16384"""					)

								# change to intended input here
								UpperCAmelCase  :	List[str]							  =	_long_tensor([512 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]					)
								UpperCAmelCase  :	Union[str, Any]							  =	_long_tensor([128 * [0, 31414, 232, 328, 740, 1140, 12695, 69]]					)
								UpperCAmelCase  :	Optional[int]							  =	prepare_led_inputs_dict(model.config	,	A	,	A					)
								UpperCAmelCase  :	Union[str, Any]							  =	model(**A					)[0]
								UpperCAmelCase  :	Optional[Any]							  =	(1, 1024, model.config.vocab_size)
								self.assertEqual(output.shape	,	A					)
								# change to expected output here
								UpperCAmelCase  :	str							  =	tf.convert_to_tensor(
								    [[3_3.6_5_0_7, 6.4_5_7_2, 1_6.8_0_8_9], [5.8_7_3_9, -2.4_2_3_8, 1_1.2_9_0_2], [-3.2_1_3_9, -4.3_1_4_9, 4.2_7_8_3]]	,	)
								tf.debugging.assert_near(output[:, :3, :3]	,	A	,	atol=1e-3	,	rtol=1e-3					)






'''simple docstring'''




import math



def    __lowerCamelCase						(      _lowercase	)   ->    bool:
							assert isinstance(_lowercase			,       _lowercase	) and (
							    number >= 0
							), "'number' must been an int and positive"

							if 1 < number < 4:
														# 2 and 3 are primes
														return True
							elif number < 2 or not number % 2:
														# Negatives, 0, 1 and all even numbers are not primes
														return False

							UpperCAmelCase  :	str							  =	range(3			,       int(math.sqrt(_lowercase	) + 1	)			,       2	)
							return not any(not number % i for i in odd_numbers	)



def    __lowerCamelCase						(      _lowercase			,       _lowercase=1			,       **_lowercase	)   ->    Union[str, Any]:
							UpperCAmelCase  :	Optional[int]							  =	factor * value
							UpperCAmelCase  :	List[Any]							  =	value

							while not is_prime(_lowercase	):
														value += 1 if not ("desc" in kwargs and kwargs["desc"] is True) else -1

							if value == first_value_val:
														return next_prime(value + 1			,       **_lowercase	)
							return value

