




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       a_       :
	A    							= LEDConfig
	A    							= {}
	A    							= '''gelu'''





	def __init__(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE=13      ,     SCREAMING_SNAKE_CASE=7      ,     SCREAMING_SNAKE_CASE=True      ,     SCREAMING_SNAKE_CASE=False      ,     SCREAMING_SNAKE_CASE=99      ,     SCREAMING_SNAKE_CASE=32      ,     SCREAMING_SNAKE_CASE=2      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE=37      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=20      ,     SCREAMING_SNAKE_CASE=2      ,     SCREAMING_SNAKE_CASE=1      ,     SCREAMING_SNAKE_CASE=0      ,     SCREAMING_SNAKE_CASE=4      ,     )  ->					Optional[Any]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						parent
				SCREAMING_SNAKE_CASE_								=						batch_size
				SCREAMING_SNAKE_CASE_								=						seq_length
				SCREAMING_SNAKE_CASE_								=						is_training
				SCREAMING_SNAKE_CASE_								=						use_labels
				SCREAMING_SNAKE_CASE_								=						vocab_size
				SCREAMING_SNAKE_CASE_								=						hidden_size
				SCREAMING_SNAKE_CASE_								=						num_hidden_layers
				SCREAMING_SNAKE_CASE_								=						num_attention_heads
				SCREAMING_SNAKE_CASE_								=						intermediate_size

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





	def 						A_(							self      )  ->					Tuple:




				"""simple docstring"""



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

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

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





	def 						A_(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )  ->					List[str]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						TFLEDModel(config=SCREAMING_SNAKE_CASE      ).get_decoder()
				SCREAMING_SNAKE_CASE_								=						inputs_dict['input_ids']

				SCREAMING_SNAKE_CASE_								=						input_ids[:1, :]
				SCREAMING_SNAKE_CASE_								=						inputs_dict['attention_mask'][:1, :]
				SCREAMING_SNAKE_CASE_								=						1

				# first forward pass
				SCREAMING_SNAKE_CASE_								=						model(SCREAMING_SNAKE_CASE      ,     attention_mask=SCREAMING_SNAKE_CASE      ,     use_cache=SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						outputs.to_tuple()

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

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

				SCREAMING_SNAKE_CASE_								=						model(SCREAMING_SNAKE_CASE      ,     attention_mask=SCREAMING_SNAKE_CASE      )[0]
				SCREAMING_SNAKE_CASE_								=						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
				SCREAMING_SNAKE_CASE_								=						int(ids_tensor((1,)      ,     output_from_past.shape[-1]      )      )
				SCREAMING_SNAKE_CASE_								=						output_from_no_past[:, -3:, random_slice_idx]
				SCREAMING_SNAKE_CASE_								=						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 lowercase    (  SCREAMING_SNAKE_CASE       ,    SCREAMING_SNAKE_CASE       ,    SCREAMING_SNAKE_CASE       ,    SCREAMING_SNAKE_CASE=None       ,    SCREAMING_SNAKE_CASE=None       ,    SCREAMING_SNAKE_CASE=None       ,    SCREAMING_SNAKE_CASE=None       ,    )			->							List[str]:


			'''simple docstring'''







			if attention_mask is None:
						SCREAMING_SNAKE_CASE_								=						tf.cast(tf.math.not_equal(SCREAMING_SNAKE_CASE       ,    config.pad_token_id )       ,    tf.inta )
			if decoder_attention_mask is None:
						SCREAMING_SNAKE_CASE_								=						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:
						SCREAMING_SNAKE_CASE_								=						tf.ones((config.encoder_layers, config.encoder_attention_heads) )
			if decoder_head_mask is None:
						SCREAMING_SNAKE_CASE_								=						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       a_       (     SCREAMING_SNAKE_CASE__  ,    SCREAMING_SNAKE_CASE__  ,    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 						A_(							self      )  ->					Optional[int]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						TFLEDModelTester(self      )
				SCREAMING_SNAKE_CASE_								=						ConfigTester(self      ,     config_class=SCREAMING_SNAKE_CASE      )





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




				"""simple docstring"""



				self.config_tester.run_common_tests()





	def 						A_(							self      )  ->					Tuple:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_decoder_model_past_large_inputs(*SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					Tuple:




				"""simple docstring"""



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

				SCREAMING_SNAKE_CASE_								=						True
				SCREAMING_SNAKE_CASE_								=						self.model_tester.seq_length
				SCREAMING_SNAKE_CASE_								=						self.model_tester.encoder_seq_length

				def check_decoder_attentions_output(SCREAMING_SNAKE_CASE      ):
							SCREAMING_SNAKE_CASE_								=						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      ):
							SCREAMING_SNAKE_CASE_								=						[t.numpy() for t in outputs.encoder_attentions]
							SCREAMING_SNAKE_CASE_								=						[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:
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						False
							SCREAMING_SNAKE_CASE_								=						False
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						model(self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )
							SCREAMING_SNAKE_CASE_								=						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:
										SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
										SCREAMING_SNAKE_CASE_								=						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"]
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						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
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						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 						A_(							self      )  ->					str:




				"""simple docstring"""



				pass





	def 						A_(							self      )  ->					Optional[Any]:




				"""simple docstring"""



				pass




def lowercase    (  SCREAMING_SNAKE_CASE )			->							Any:


			'''simple docstring'''







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


SCREAMING_SNAKE_CASE__     :     str		    =       1E-4



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





	def 						A_(							self      )  ->					Optional[int]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'      ).led

				# change to intended input here
				SCREAMING_SNAKE_CASE_								=						_long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]      )
				SCREAMING_SNAKE_CASE_								=						_long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]      )
				SCREAMING_SNAKE_CASE_								=						prepare_led_inputs_dict(model.config      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						model(**SCREAMING_SNAKE_CASE      )[0]
				SCREAMING_SNAKE_CASE_								=						(1, 1024, 768)
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )
				# change to expected output here
				SCREAMING_SNAKE_CASE_								=						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]      ,     SCREAMING_SNAKE_CASE      ,     atol=1e-3      )





	def 						A_(							self      )  ->					Optional[int]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						TFLEDForConditionalGeneration.from_pretrained('allenai/led-base-16384'      )

				# change to intended input here
				SCREAMING_SNAKE_CASE_								=						_long_tensor([512 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]      )
				SCREAMING_SNAKE_CASE_								=						_long_tensor([128 * [0, 3_1414, 232, 328, 740, 1140, 1_2695, 69]]      )
				SCREAMING_SNAKE_CASE_								=						prepare_led_inputs_dict(model.config      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						model(**SCREAMING_SNAKE_CASE      )[0]
				SCREAMING_SNAKE_CASE_								=						(1, 1024, model.config.vocab_size)
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )
				# change to expected output here
				SCREAMING_SNAKE_CASE_								=						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]      ,     SCREAMING_SNAKE_CASE      ,     atol=1e-3      ,     rtol=1e-3      )






import inspect
import tempfile
import unittest

from huggingface_hub import hf_hub_download

from transformers import is_torch_available
from transformers.testing_utils import is_flaky, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


SCREAMING_SNAKE_CASE__     :     Optional[Any]		    =       1E-4

if is_torch_available():
				import torch

				from transformers import AutoformerConfig, AutoformerForPrediction, AutoformerModel
				from transformers.models.autoformer.modeling_autoformer import AutoformerDecoder, AutoformerEncoder



@require_torch
class       a_       :





	def __init__(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE=16      ,     SCREAMING_SNAKE_CASE=13      ,     SCREAMING_SNAKE_CASE=7      ,     SCREAMING_SNAKE_CASE=14      ,     SCREAMING_SNAKE_CASE=10      ,     SCREAMING_SNAKE_CASE=19      ,     SCREAMING_SNAKE_CASE=5      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE=True      ,     SCREAMING_SNAKE_CASE=16      ,     SCREAMING_SNAKE_CASE=2      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE=4      ,     SCREAMING_SNAKE_CASE="gelu"      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=0.1      ,     SCREAMING_SNAKE_CASE=[1, 2, 3, 4, 5]      ,     SCREAMING_SNAKE_CASE=25      ,     SCREAMING_SNAKE_CASE=5      ,     )  ->					Union[str, Any]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						d_model
				SCREAMING_SNAKE_CASE_								=						parent
				SCREAMING_SNAKE_CASE_								=						batch_size
				SCREAMING_SNAKE_CASE_								=						prediction_length
				SCREAMING_SNAKE_CASE_								=						context_length
				SCREAMING_SNAKE_CASE_								=						cardinality
				SCREAMING_SNAKE_CASE_								=						num_time_features
				SCREAMING_SNAKE_CASE_								=						lags_sequence
				SCREAMING_SNAKE_CASE_								=						embedding_dimension
				SCREAMING_SNAKE_CASE_								=						is_training
				SCREAMING_SNAKE_CASE_								=						hidden_size
				SCREAMING_SNAKE_CASE_								=						num_hidden_layers
				SCREAMING_SNAKE_CASE_								=						num_attention_heads
				SCREAMING_SNAKE_CASE_								=						intermediate_size
				SCREAMING_SNAKE_CASE_								=						hidden_act
				SCREAMING_SNAKE_CASE_								=						hidden_dropout_prob
				SCREAMING_SNAKE_CASE_								=						attention_probs_dropout_prob

				SCREAMING_SNAKE_CASE_								=						context_length
				SCREAMING_SNAKE_CASE_								=						prediction_length + label_length
				SCREAMING_SNAKE_CASE_								=						label_length

				SCREAMING_SNAKE_CASE_								=						moving_average
				SCREAMING_SNAKE_CASE_								=						autocorrelation_factor





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




				"""simple docstring"""



				return AutoformerConfig(
				    d_model=self.d_model      ,     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      ,     prediction_length=self.prediction_length      ,     context_length=self.context_length      ,     label_length=self.label_length      ,     lags_sequence=self.lags_sequence      ,     num_time_features=self.num_time_features      ,     num_static_categorical_features=1      ,     cardinality=[self.cardinality]      ,     embedding_dimension=[self.embedding_dimension]      ,     moving_average=self.moving_average      ,     )





	def 						A_(							self      ,     SCREAMING_SNAKE_CASE      )  ->					Optional[int]:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						config.context_length + max(config.lags_sequence      )

				SCREAMING_SNAKE_CASE_								=						ids_tensor([self.batch_size, 1]      ,     config.cardinality[0]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length, config.num_time_features]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, _past_length]      ) > 0.5

				# decoder inputs
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]      )
				SCREAMING_SNAKE_CASE_								=						floats_tensor([self.batch_size, config.prediction_length]      )

				SCREAMING_SNAKE_CASE_								=						{
				    'past_values': past_values,
				    'static_categorical_features': static_categorical_features,
				    'past_time_features': past_time_features,
				    'past_observed_mask': past_observed_mask,
				    'future_time_features': future_time_features,
				    'future_values': future_values,
				}
				return inputs_dict





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						self.get_config()
				SCREAMING_SNAKE_CASE_								=						self.prepare_autoformer_inputs_dict(SCREAMING_SNAKE_CASE      )
				return config, inputs_dict





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.prepare_config_and_inputs()
				return config, inputs_dict





	def 						A_(							self      ,     SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModel(config=SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      ).eval()
				SCREAMING_SNAKE_CASE_								=						model(**SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						outputs.encoder_last_hidden_state
				SCREAMING_SNAKE_CASE_								=						outputs.last_hidden_state

				with tempfile.TemporaryDirectory() as tmpdirname:
							SCREAMING_SNAKE_CASE_								=						model.get_encoder()
							encoder.save_pretrained(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						AutoformerEncoder.from_pretrained(SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model.create_network_inputs(**SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]      )

				SCREAMING_SNAKE_CASE_								=						torch.cat(
				    (transformer_inputs[:, : config.context_length, ...], feature[:, : config.context_length, ...])      ,     dim=-1      ,     )
				SCREAMING_SNAKE_CASE_								=						encoder(inputs_embeds=SCREAMING_SNAKE_CASE      )[0]
				self.parent.assertTrue((encoder_last_hidden_state_a - encoder_last_hidden_state).abs().max().item() < 1e-3      )

				SCREAMING_SNAKE_CASE_								=						(
				    torch.mean(transformer_inputs[:, : config.context_length, ...]      ,     dim=1      )
				    .unsqueeze(1      )
				    .repeat(1      ,     config.prediction_length      ,     1      )
				)
				SCREAMING_SNAKE_CASE_								=						torch.zeros(
				    [transformer_inputs.shape[0], config.prediction_length, transformer_inputs.shape[2]]      ,     device=enc_input.device      ,     )

				SCREAMING_SNAKE_CASE_								=						torch.cat(
				    (
				        torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)      ,     dim=1      ),
				        feature[:, config.context_length - config.label_length :, ...],
				    )      ,     dim=-1      ,     )
				SCREAMING_SNAKE_CASE_								=						torch.cat(
				    (
				        torch.cat((trend_input[:, -config.label_length :, ...], mean)      ,     dim=1      ),
				        feature[:, config.context_length - config.label_length :, ...],
				    )      ,     dim=-1      ,     )

				with tempfile.TemporaryDirectory() as tmpdirname:
							SCREAMING_SNAKE_CASE_								=						model.get_decoder()
							decoder.save_pretrained(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						AutoformerDecoder.from_pretrained(SCREAMING_SNAKE_CASE      ).to(SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						decoder(
				    trend=SCREAMING_SNAKE_CASE      ,     inputs_embeds=SCREAMING_SNAKE_CASE      ,     encoder_hidden_states=SCREAMING_SNAKE_CASE      ,     )[0]

				self.parent.assertTrue((last_hidden_state_a - last_hidden_state).abs().max().item() < 1e-3      )




@require_torch
class       a_       (     SCREAMING_SNAKE_CASE__  ,    SCREAMING_SNAKE_CASE__  ,    unittest.TestCase     ):
	A    							= (AutoformerModel, AutoformerForPrediction) if is_torch_available() else ()
	A    							= (AutoformerForPrediction,) if is_torch_available() else ()
	A    							= {'''feature-extraction''': AutoformerModel} if is_torch_available() else {}
	A    							= False
	A    							= False
	A    							= False
	A    							= False
	A    							= False
	A    							= False





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModelTester(self      )
				SCREAMING_SNAKE_CASE_								=						ConfigTester(self      ,     config_class=SCREAMING_SNAKE_CASE      ,     has_text_modality=SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					int:




				"""simple docstring"""



				self.config_tester.run_common_tests()





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs()
				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )

							with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(SCREAMING_SNAKE_CASE      )
										SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						model_class.from_pretrained(SCREAMING_SNAKE_CASE      ,     output_loading_info=SCREAMING_SNAKE_CASE      )
							self.assertEqual(info['missing_keys']      ,     []      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()
				self.model_tester.check_encoder_decoder_model_standalone(*SCREAMING_SNAKE_CASE      )





	@unittest.skip(reason='Model has no tokens embeddings'      )
	def 						A_(							self      )  ->					Union[str, Any]:




				"""simple docstring"""



				pass





	def 						A_(							self      )  ->					int:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						inspect.signature(getattr(SCREAMING_SNAKE_CASE      ,     'forward'      )      )
				# The main input is the name of the argument after `self`
				SCREAMING_SNAKE_CASE_								=						list(model_signature.parameters.keys()      )[1]
				self.assertEqual(AutoformerModel.main_input_name      ,     SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()

				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							SCREAMING_SNAKE_CASE_								=						inspect.signature(model.forward      )
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							SCREAMING_SNAKE_CASE_								=						[*signature.parameters.keys()]

							SCREAMING_SNAKE_CASE_								=						[
							    'past_values',
							    'past_time_features',
							    'past_observed_mask',
							    'static_categorical_features',
							    'static_real_features',
							    'future_values',
							    'future_time_features',
							]

							if model.__class__.__name__ in ["AutoformerForPrediction"]:
										expected_arg_names.append('future_observed_mask'      )

							expected_arg_names.extend(
							    [
							        'decoder_attention_mask',
							        'head_mask',
							        'decoder_head_mask',
							        'cross_attn_head_mask',
							        'encoder_outputs',
							        'past_key_values',
							        'output_hidden_states',
							        'output_attentions',
							        'use_cache',
							        'return_dict',
							    ]      )

							self.assertListEqual(arg_names[: len(SCREAMING_SNAKE_CASE      )]      ,     SCREAMING_SNAKE_CASE      )





	def 						A_(							self      )  ->					str:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_						,		SCREAMING_SNAKE_CASE_								=						self.model_tester.prepare_config_and_inputs_for_common()
				SCREAMING_SNAKE_CASE_								=						True

				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'decoder_seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'encoder_seq_length'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'd_model'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						getattr(self.model_tester      ,     'num_attention_heads'      ,     SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						d_model // num_attention_heads

				for model_class in self.all_model_classes:
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						False
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							model.to(SCREAMING_SNAKE_CASE      )
							model.eval()
							with torch.no_grad():
										SCREAMING_SNAKE_CASE_								=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )
							SCREAMING_SNAKE_CASE_								=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
							self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     self.model_tester.num_hidden_layers      )

							# check that output_attentions also work using config
							del inputs_dict["output_attentions"]
							SCREAMING_SNAKE_CASE_								=						True
							SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
							model.to(SCREAMING_SNAKE_CASE      )
							model.eval()
							with torch.no_grad():
										SCREAMING_SNAKE_CASE_								=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )
							SCREAMING_SNAKE_CASE_								=						outputs.encoder_attentions
							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, encoder_seq_length, dim]      ,     )
							SCREAMING_SNAKE_CASE_								=						len(SCREAMING_SNAKE_CASE      )

							SCREAMING_SNAKE_CASE_								=						7

							if "last_hidden_state" in outputs:
										correct_outlen += 1

							if "trend" in outputs:
										correct_outlen += 1

							if "past_key_values" in outputs:
										correct_outlen += 1  # past_key_values have been returned

							if "loss" in outputs:
										correct_outlen += 1

							if "params" in outputs:
										correct_outlen += 1

							self.assertEqual(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )

							# decoder attentions
							SCREAMING_SNAKE_CASE_								=						outputs.decoder_attentions
							self.assertIsInstance(SCREAMING_SNAKE_CASE      ,     (list, tuple)      )
							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, decoder_seq_length, dim]      ,     )

							# cross attentions
							SCREAMING_SNAKE_CASE_								=						outputs.cross_attentions
							self.assertIsInstance(SCREAMING_SNAKE_CASE      ,     (list, tuple)      )
							self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     self.model_tester.num_hidden_layers      )
							self.assertListEqual(
							    list(cross_attentions[0].shape[-3:]      )      ,     [self.model_tester.num_attention_heads, decoder_seq_length, dim]      ,     )

				# Check attention is always last and order is fine
				SCREAMING_SNAKE_CASE_								=						True
				SCREAMING_SNAKE_CASE_								=						True
				SCREAMING_SNAKE_CASE_								=						model_class(SCREAMING_SNAKE_CASE      )
				model.to(SCREAMING_SNAKE_CASE      )
				model.eval()
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						model(**self._prepare_for_class(SCREAMING_SNAKE_CASE      ,     SCREAMING_SNAKE_CASE      )      )

				self.assertEqual(out_len + 2      ,     len(SCREAMING_SNAKE_CASE      )      )

				SCREAMING_SNAKE_CASE_								=						outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

				self.assertEqual(len(SCREAMING_SNAKE_CASE      )      ,     self.model_tester.num_hidden_layers      )
				self.assertListEqual(
				    list(self_attentions[0].shape[-3:]      )      ,     [self.model_tester.num_attention_heads, encoder_seq_length, dim]      ,     )





	@is_flaky()
	def 						A_(							self      )  ->					Any:




				"""simple docstring"""



				super().test_retain_grad_hidden_states_attentions()




def lowercase    (  SCREAMING_SNAKE_CASE="train-batch.pt" )			->							str:


			'''simple docstring'''







			SCREAMING_SNAKE_CASE_								=						hf_hub_download(repo_id='hf-internal-testing/tourism-monthly-batch'       ,    filename=SCREAMING_SNAKE_CASE       ,    repo_type='dataset' )
			SCREAMING_SNAKE_CASE_								=						torch.load(SCREAMING_SNAKE_CASE       ,    map_location=SCREAMING_SNAKE_CASE )
			return batch



@require_torch
@slow
class       a_       (     unittest.TestCase     ):





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




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch()

				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						model(
							    past_values=batch['past_values']      ,     past_time_features=batch['past_time_features']      ,     past_observed_mask=batch['past_observed_mask']      ,     static_categorical_features=batch['static_categorical_features']      ,     future_values=batch['future_values']      ,     future_time_features=batch['future_time_features']      ,     )[0]

				SCREAMING_SNAKE_CASE_								=						torch.Size(
				    (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)      )
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor(
				    [[0.3_5_9_3, -1.3_3_9_8, 0.6_3_3_0], [0.2_2_7_9, 1.5_3_9_6, -0.1_7_9_2], [0.0_4_5_0, 1.3_2_2_5, -0.2_3_3_5]]      ,     device=SCREAMING_SNAKE_CASE      )
				self.assertTrue(torch.allclose(output[0, :3, :3]      ,     SCREAMING_SNAKE_CASE      ,     atol=SCREAMING_SNAKE_CASE      )      )





	def 						A_(							self      )  ->					Dict:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch('val-batch.pt'      )
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						model(
							    past_values=batch['past_values']      ,     past_time_features=batch['past_time_features']      ,     past_observed_mask=batch['past_observed_mask']      ,     static_categorical_features=batch['static_categorical_features']      ,     ).encoder_last_hidden_state
				SCREAMING_SNAKE_CASE_								=						torch.Size((64, model.config.context_length, model.config.d_model)      )
				self.assertEqual(output.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor(
				    [[-0.0_7_3_4, -0.9_0_3_6, 0.8_3_5_8], [4.7_1_8_6, 2.4_1_1_3, 1.9_5_8_1], [1.7_9_5_3, 2.3_5_5_8, 1.2_9_7_0]]      ,     device=SCREAMING_SNAKE_CASE      )
				self.assertTrue(torch.allclose(output[0, :3, :3]      ,     SCREAMING_SNAKE_CASE      ,     atol=SCREAMING_SNAKE_CASE      )      )





	def 						A_(							self      )  ->					Any:




				"""simple docstring"""



				SCREAMING_SNAKE_CASE_								=						AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'      ).to(SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						prepare_batch('val-batch.pt'      )
				with torch.no_grad():
							SCREAMING_SNAKE_CASE_								=						model.generate(
							    static_categorical_features=batch['static_categorical_features']      ,     past_time_features=batch['past_time_features']      ,     past_values=batch['past_values']      ,     future_time_features=batch['future_time_features']      ,     past_observed_mask=batch['past_observed_mask']      ,     )
				SCREAMING_SNAKE_CASE_								=						torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)      )
				self.assertEqual(outputs.sequences.shape      ,     SCREAMING_SNAKE_CASE      )

				SCREAMING_SNAKE_CASE_								=						torch.tensor([3_1_3_0.6_7_6_3, 4_0_5_6.5_2_9_3, 7_0_5_3.0_7_8_6]      ,     device=SCREAMING_SNAKE_CASE      )
				SCREAMING_SNAKE_CASE_								=						outputs.sequences.mean(dim=1      )
				self.assertTrue(torch.allclose(mean_prediction[0, -3:]      ,     SCREAMING_SNAKE_CASE      ,     rtol=1e-1      )      )
