
"""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 __lowerCAmelCase				:




			"""simple docstring"""







			snake_case	      =				LEDConfig
			snake_case	      =				{}
			snake_case	      =				"gelu"




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







								"""simple docstring"""
								A_				 =						parent
								A_				 =						batch_size
								A_				 =						seq_length
								A_				 =						is_training
								A_				 =						use_labels
								A_				 =						vocab_size
								A_				 =						hidden_size
								A_				 =						num_hidden_layers
								A_				 =						num_attention_heads
								A_				 =						intermediate_size

								A_				 =						hidden_dropout_prob
								A_				 =						attention_probs_dropout_prob
								A_				 =						max_position_embeddings
								A_				 =						eos_token_id
								A_				 =						pad_token_id
								A_				 =						bos_token_id
								A_				 =						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_				 =						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_				 =						(
								    self.seq_length + (self.attention_window - self.seq_length % self.attention_window) % self.attention_window
								)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> List[str]:







								"""simple docstring"""
								A_				 =						ids_tensor([self.batch_size, self.seq_length - 1]       ,   self.vocab_size	)
								A_				 =						tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size	)       ,   1	)
								A_				 =						tf.concat([input_ids, eos_tensor]       ,   axis=1	)

								A_				 =						ids_tensor([self.batch_size, self.seq_length]       ,   self.vocab_size	)

								A_				 =						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_				 =						prepare_led_inputs_dict(_snake_case       ,   _snake_case       ,   _snake_case	)
								A_				 =						tf.concat(
								    [tf.zeros_like(_snake_case	)[:, :-1], tf.ones_like(_snake_case	)[:, -1:]]       ,   axis=-1       ,   )
								A_				 =						global_attention_mask
								return config, inputs_dict






			def      lowerCamelCase__     (				self						:					List[str]       ,   _snake_case						:					Tuple       ,   _snake_case						:					int	)      -> Any:







								"""simple docstring"""
								A_				 =						TFLEDModel(config=_snake_case	).get_decoder()
								A_				 =						inputs_dict["input_ids"]

								A_				 =						input_ids[:1, :]
								A_				 =						inputs_dict["attention_mask"][:1, :]
								A_				 =						1

								# first forward pass
								A_				 =						model(_snake_case       ,   attention_mask=_snake_case       ,   use_cache=_snake_case	)

								A_ ,    A_				 =						outputs.to_tuple()

								# create hypothetical next token and extent to next_input_ids
								A_				 =						ids_tensor((self.batch_size, 3)       ,   config.vocab_size	)
								A_				 =						tf.cast(ids_tensor((self.batch_size, 3)       ,   2	)       ,   tf.inta	)

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

								A_				 =						model(_snake_case       ,   attention_mask=_snake_case	)[0]
								A_				 =						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
								A_				 =						int(ids_tensor((1,)       ,   output_from_past.shape[-1]	)	)
								A_				 =						output_from_no_past[:, -3:, random_slice_idx]
								A_				 =						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 							A_     (__a				,   __a				,   __a				,   __a=None				,   __a=None				,   __a=None				,   __a=None				,   ):

					'''simple docstring'''





					if attention_mask is None:
										A_				 =						tf.cast(tf.math.not_equal(__a				,   config.pad_token_id				)				,   tf.inta				)
					if decoder_attention_mask is None:
										A_				 =						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_				 =						tf.ones((config.encoder_layers, config.encoder_attention_heads)				)
					if decoder_head_mask is None:
										A_				 =						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 __lowerCAmelCase				(					_lowercase     ,				_lowercase     ,				unittest.TestCase      ):




			"""simple docstring"""







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




			def      lowerCamelCase__     (				self						:					List[Any]	)      -> Optional[Any]:







								"""simple docstring"""
								A_				 =						TFLEDModelTester(self	)
								A_				 =						ConfigTester(self       ,   config_class=_snake_case	)




			def      lowerCamelCase__     (				self						:					List[Any]	)      -> Union[str, Any]:







								"""simple docstring"""
								self.config_tester.run_common_tests()




			def      lowerCamelCase__     (				self						:					Optional[Any]	)      -> str:







								"""simple docstring"""
								A_				 =						self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.check_decoder_model_past_large_inputs(*_snake_case	)




			def      lowerCamelCase__     (				self						:					Union[str, Any]	)      -> Union[str, Any]:







								"""simple docstring"""
								A_ ,    A_				 =						self.model_tester.prepare_config_and_inputs_for_common()
								A_				 =						tf.zeros_like(inputs_dict["attention_mask"]	)
								A_				 =						2
								A_				 =						tf.where(
								    tf.range(self.model_tester.seq_length	)[None, :] < num_global_attn_indices       ,   1       ,   inputs_dict["global_attention_mask"]       ,   )

								A_				 =						True
								A_				 =						self.model_tester.seq_length
								A_				 =						self.model_tester.encoder_seq_length

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

													if self.is_encoder_decoder:
																		A_				 =						model_class(_snake_case	)
																		A_				 =						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"]
													A_				 =						True
													A_				 =						model_class(_snake_case	)
													A_				 =						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
													A_				 =						True
													A_				 =						True
													A_				 =						model_class(_snake_case	)
													A_				 =						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      lowerCamelCase__     (				self						:					int	)      -> int:







								"""simple docstring"""
								pass






			def      lowerCamelCase__     (				self						:					List[str]	)      -> Any:







								"""simple docstring"""
								# TODO: Head-masking not yet implement
								pass





def 							A_     (__a				):

					'''simple docstring'''





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


UpperCamelCase_					:						str				= 1e-4




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




			"""simple docstring"""







			def      lowerCamelCase__     (				self						:					Tuple	)      -> Any:







								"""simple docstring"""
								A_				 =						TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384"	).led

								# change to intended input here
								A_				 =						_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]	)
								A_				 =						_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]	)
								A_				 =						prepare_led_inputs_dict(model.config       ,   _snake_case       ,   _snake_case	)
								A_				 =						model(**_snake_case	)[0]
								A_				 =						(1, 1_024, 768)
								self.assertEqual(output.shape       ,   _snake_case	)
								# change to expected output here
								A_				 =						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]       ,   _snake_case       ,   atol=1e-3	)






			def      lowerCamelCase__     (				self						:					List[str]	)      -> str:







								"""simple docstring"""
								A_				 =						TFLEDForConditionalGeneration.from_pretrained("allenai/led-base-16384"	)

								# change to intended input here
								A_				 =						_long_tensor([512 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]	)
								A_				 =						_long_tensor([128 * [0, 31_414, 232, 328, 740, 1_140, 12_695, 69]]	)
								A_				 =						prepare_led_inputs_dict(model.config       ,   _snake_case       ,   _snake_case	)
								A_				 =						model(**_snake_case	)[0]
								A_				 =						(1, 1_024, model.config.vocab_size)
								self.assertEqual(output.shape       ,   _snake_case	)
								# change to expected output here
								A_				 =						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]       ,   _snake_case       ,   atol=1e-3       ,   rtol=1e-3	)






"""simple docstring"""





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


UpperCamelCase_					:						Optional[int]				= logging.get_logger(__name__)

UpperCamelCase_					:						Dict				= {
    '''microsoft/focalnet-tiny''': '''https://huggingface.co/microsoft/focalnet-tiny/resolve/main/config.json''',
}



class __lowerCAmelCase				(					_lowercase     ,				_lowercase      ):




			"""simple docstring"""







			snake_case	      =				"focalnet"




			def __init__(				self						:					Any       ,   _snake_case						:					Optional[int]=224       ,   _snake_case						:					List[str]=4       ,   _snake_case						:					Any=3       ,   _snake_case						:					List[Any]=96       ,   _snake_case						:					Union[str, Any]=False       ,   _snake_case						:					Optional[Any]=[192, 384, 768, 768]       ,   _snake_case						:					List[str]=[2, 2, 6, 2]       ,   _snake_case						:					Any=[2, 2, 2, 2]       ,   _snake_case						:					List[Any]=[3, 3, 3, 3]       ,   _snake_case						:					Optional[Any]="gelu"       ,   _snake_case						:					Dict=4.0       ,   _snake_case						:					List[Any]=0.0       ,   _snake_case						:					List[str]=0.1       ,   _snake_case						:					Optional[Any]=False       ,   _snake_case						:					Any=1e-4       ,   _snake_case						:					str=False       ,   _snake_case						:					Optional[int]=False       ,   _snake_case						:					Tuple=False       ,   _snake_case						:					Any=0.0_2       ,   _snake_case						:					List[Any]=1e-5       ,   _snake_case						:					Union[str, Any]=32       ,   _snake_case						:					Tuple=None       ,   _snake_case						:					Any=None       ,   **_snake_case						:					Tuple       ,   )      -> str:







								"""simple docstring"""
								super().__init__(**_snake_case	)

								A_				 =						image_size
								A_				 =						patch_size
								A_				 =						num_channels
								A_				 =						embed_dim
								A_				 =						use_conv_embed
								A_				 =						hidden_sizes
								A_				 =						depths
								A_				 =						focal_levels
								A_				 =						focal_windows
								A_				 =						hidden_act
								A_				 =						mlp_ratio
								A_				 =						hidden_dropout_prob
								A_				 =						drop_path_rate
								A_				 =						use_layerscale
								A_				 =						layerscale_value
								A_				 =						use_post_layernorm
								A_				 =						use_post_layernorm_in_modulation
								A_				 =						normalize_modulator
								A_				 =						initializer_range
								A_				 =						layer_norm_eps
								A_				 =						encoder_stride
								A_				 =						["stem"] + [F'stage{idx}' for idx in range(1       ,   len(self.depths	) + 1	)]
								A_ ,    A_				 =						get_aligned_output_features_output_indices(
								    out_features=_snake_case       ,   out_indices=_snake_case       ,   stage_names=self.stage_names	)




