


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


snake_case							:       List[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 						_snake_case    :




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=16   ,    _lowerCamelCase=13   ,    _lowerCamelCase=7   ,    _lowerCamelCase=14   ,    _lowerCamelCase=10   ,    _lowerCamelCase=19   ,    _lowerCamelCase=5   ,    _lowerCamelCase=4   ,    _lowerCamelCase=True   ,    _lowerCamelCase=16   ,    _lowerCamelCase=2   ,    _lowerCamelCase=4   ,    _lowerCamelCase=4   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=[1, 2, 3, 4, 5]   ,    _lowerCamelCase=25   ,    _lowerCamelCase=5   ,    ):
       a		:Optional[Any]									=			d_model
       a		:Any									=			parent
       a		:Optional[Any]									=			batch_size
       a		:List[Any]									=			prediction_length
       a		:Optional[Any]									=			context_length
       a		:str									=			cardinality
       a		:Dict									=			num_time_features
       a		:List[str]									=			lags_sequence
       a		:Tuple									=			embedding_dimension
       a		:Tuple									=			is_training
       a		:Dict									=			hidden_size
       a		:Dict									=			num_hidden_layers
       a		:str									=			num_attention_heads
       a		:Tuple									=			intermediate_size
       a		:Any									=			hidden_act
       a		:Tuple									=			hidden_dropout_prob
       a		:Optional[Any]									=			attention_probs_dropout_prob

       a		:Any									=			context_length
       a		:Tuple									=			prediction_length + label_length
       a		:Union[str, Any]									=			label_length

       a		:int									=			moving_average
       a		:Dict									=			autocorrelation_factor




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       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 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase       ):
       a		:Optional[Any]									=			config.context_length + max(config.lags_sequence       )

       a		:str									=			ids_tensor([self.batch_size, 1]   ,    config.cardinality[0]       )
       a		:Union[str, Any]									=			floats_tensor([self.batch_size, _past_length, config.num_time_features]       )
       a		:Optional[Any]									=			floats_tensor([self.batch_size, _past_length]       )
       a		:List[Any]									=			floats_tensor([self.batch_size, _past_length]       ) > 0.5

       # decoder inputs
       a		:List[Any]									=			floats_tensor([self.batch_size, config.prediction_length, config.num_time_features]       )
       a		:Optional[int]									=			floats_tensor([self.batch_size, config.prediction_length]       )

       a		:List[Any]									=			{
           '''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 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[int]									=			self.get_config()
       a		:Optional[int]									=			self.prepare_autoformer_inputs_dict(_lowerCamelCase       )
       return config, inputs_dict




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:str									=			self.prepare_config_and_inputs()
       return config, inputs_dict




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:List[Any]									=			AutoformerModel(config=_lowerCamelCase       ).to(_lowerCamelCase       ).eval()
       a		:Dict									=			model(**_lowerCamelCase       )

       a		:Optional[Any]									=			outputs.encoder_last_hidden_state
       a		:List[str]									=			outputs.last_hidden_state

       with tempfile.TemporaryDirectory() as tmpdirname:
             a		:Tuple									=			model.get_encoder()
             encoder.save_pretrained(_lowerCamelCase       )
             a		:Union[str, Any]									=			AutoformerEncoder.from_pretrained(_lowerCamelCase       ).to(_lowerCamelCase       )

       a				,							a				,							a				,							a				,							a		:Optional[int]									=			model.create_network_inputs(**_lowerCamelCase       )
       a				,							a		:Any									=			model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]       )

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

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

       a		:Union[str, Any]									=			torch.cat(
           (
               torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)   ,    dim=1       ),
               feature[:, config.context_length - config.label_length :, ...],
           )   ,    dim=-1   ,    )
       a		:Tuple									=			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:
             a		:Tuple									=			model.get_decoder()
             decoder.save_pretrained(_lowerCamelCase       )
             a		:Dict									=			AutoformerDecoder.from_pretrained(_lowerCamelCase       ).to(_lowerCamelCase       )

       a		:Optional[Any]									=			decoder(
           trend=_lowerCamelCase   ,    inputs_embeds=_lowerCamelCase   ,    encoder_hidden_states=_lowerCamelCase   ,    )[0]

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







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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[int]									=			AutoformerModelTester(self       )
       a		:str									=			ConfigTester(self   ,    config_class=_lowerCamelCase   ,    has_text_modality=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.config_tester.run_common_tests()




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:List[str]									=			self.model_tester.prepare_config_and_inputs()
       for model_class in self.all_model_classes:
             a		:List[str]									=			model_class(_lowerCamelCase       )

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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Union[str, Any]									=			self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_encoder_decoder_model_standalone(*_lowerCamelCase       )




 @unittest.skip(reason='''Model has no tokens embeddings'''       )
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       pass




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Any									=			inspect.signature(getattr(_lowerCamelCase   ,    '''forward'''       )       )
       # The main input is the name of the argument after `self`
       a		:Dict									=			list(model_signature.parameters.keys()       )[1]
       self.assertEqual(AutoformerModel.main_input_name   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Optional[int]									=			self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             a		:List[Any]									=			model_class(_lowerCamelCase       )
             a		:Union[str, Any]									=			inspect.signature(model.forward       )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             a		:Optional[int]									=			[*signature.parameters.keys()]

             a		:Tuple									=			[
                 '''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(_lowerCamelCase       )]   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a				,							a		:Tuple									=			self.model_tester.prepare_config_and_inputs_for_common()
       a		:Dict									=			True

       a		:str									=			getattr(self.model_tester   ,    '''seq_length'''   ,    _lowerCamelCase       )
       a		:Dict									=			getattr(self.model_tester   ,    '''decoder_seq_length'''   ,    _lowerCamelCase       )
       a		:Optional[int]									=			getattr(self.model_tester   ,    '''encoder_seq_length'''   ,    _lowerCamelCase       )
       a		:Tuple									=			getattr(self.model_tester   ,    '''d_model'''   ,    _lowerCamelCase       )
       a		:int									=			getattr(self.model_tester   ,    '''num_attention_heads'''   ,    _lowerCamelCase       )
       a		:str									=			d_model // num_attention_heads

       for model_class in self.all_model_classes:
             a		:Union[str, Any]									=			True
             a		:Dict									=			False
             a		:Union[str, Any]									=			True
             a		:List[str]									=			model_class(_lowerCamelCase       )
             model.to(_lowerCamelCase       )
             model.eval()
             with torch.no_grad():
                   a		:List[str]									=			model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase       )       )
             a		:Tuple									=			outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions
             self.assertEqual(len(_lowerCamelCase       )   ,    self.model_tester.num_hidden_layers       )

             # check that output_attentions also work using config
             del inputs_dict["output_attentions"]
             a		:Dict									=			True
             a		:Optional[int]									=			model_class(_lowerCamelCase       )
             model.to(_lowerCamelCase       )
             model.eval()
             with torch.no_grad():
                   a		:int									=			model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase       )       )
             a		:str									=			outputs.encoder_attentions
             self.assertEqual(len(_lowerCamelCase       )   ,    self.model_tester.num_hidden_layers       )

             self.assertListEqual(
                 list(attentions[0].shape[-3:]       )   ,    [self.model_tester.num_attention_heads, encoder_seq_length, dim]   ,    )
             a		:Dict									=			len(_lowerCamelCase       )

             a		:Optional[Any]									=			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(_lowerCamelCase   ,    _lowerCamelCase       )

             # decoder attentions
             a		:List[str]									=			outputs.decoder_attentions
             self.assertIsInstance(_lowerCamelCase   ,    (list, tuple)       )
             self.assertEqual(len(_lowerCamelCase       )   ,    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
             a		:Tuple									=			outputs.cross_attentions
             self.assertIsInstance(_lowerCamelCase   ,    (list, tuple)       )
             self.assertEqual(len(_lowerCamelCase       )   ,    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
       a		:int									=			True
       a		:Any									=			True
       a		:Union[str, Any]									=			model_class(_lowerCamelCase       )
       model.to(_lowerCamelCase       )
       model.eval()
       with torch.no_grad():
             a		:Optional[Any]									=			model(**self._prepare_for_class(_lowerCamelCase   ,    _lowerCamelCase       )       )

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

       a		:Union[str, Any]									=			outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions

       self.assertEqual(len(_lowerCamelCase       )   ,    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 			SCREAMING_SNAKE_CASE__							(						self       ):
       super().test_retain_grad_hidden_states_attentions()







def 							__lowerCamelCase		(					UpperCAmelCase_		:						str="train-batch.pt"		):




      """simple docstring"""





      a		:str									=			hf_hub_download(repo_id='''hf-internal-testing/tourism-monthly-batch'''					,    filename=UpperCAmelCase_					,    repo_type='''dataset'''		)
      a		:Tuple									=			torch.load(UpperCAmelCase_					,    map_location=UpperCAmelCase_		)
      return batch







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




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:str									=			AutoformerModel.from_pretrained('''huggingface/autoformer-tourism-monthly'''       ).to(_lowerCamelCase       )
       a		:Optional[int]									=			prepare_batch()

       with torch.no_grad():
             a		:List[str]									=			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]

       a		:Optional[Any]									=			torch.Size(
           (64, model.config.prediction_length + model.config.label_length, model.config.feature_size)       )
       self.assertEqual(output.shape   ,    _lowerCamelCase       )

       a		:int									=			torch.tensor(
           [[0.3593, -1.3398, 0.6330], [0.2279, 1.5396, -0.1792], [0.0450, 1.3225, -0.2335]]   ,    device=_lowerCamelCase       )
       self.assertTrue(torch.allclose(output[0, :3, :3]   ,    _lowerCamelCase   ,    atol=_lowerCamelCase       )       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:str									=			AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly'''       ).to(_lowerCamelCase       )
       a		:Any									=			prepare_batch('''val-batch.pt'''       )
       with torch.no_grad():
             a		:List[Any]									=			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
       a		:Union[str, Any]									=			torch.Size((64, model.config.context_length, model.config.d_model)       )
       self.assertEqual(output.shape   ,    _lowerCamelCase       )

       a		:Dict									=			torch.tensor(
           [[-0.0734, -0.9036, 0.8358], [4.7186, 2.4113, 1.9581], [1.7953, 2.3558, 1.2970]]   ,    device=_lowerCamelCase       )
       self.assertTrue(torch.allclose(output[0, :3, :3]   ,    _lowerCamelCase   ,    atol=_lowerCamelCase       )       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Optional[int]									=			AutoformerForPrediction.from_pretrained('''huggingface/autoformer-tourism-monthly'''       ).to(_lowerCamelCase       )
       a		:Any									=			prepare_batch('''val-batch.pt'''       )
       with torch.no_grad():
             a		:Dict									=			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''']   ,    )
       a		:Tuple									=			torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)       )
       self.assertEqual(outputs.sequences.shape   ,    _lowerCamelCase       )

       a		:List[Any]									=			torch.tensor([3130.6763, 4056.5293, 7053.0786]   ,    device=_lowerCamelCase       )
       a		:str									=			outputs.sequences.mean(dim=1       )
       self.assertTrue(torch.allclose(mean_prediction[0, -3:]   ,    _lowerCamelCase   ,    rtol=1e-1       )       )









snake_case							:       Any										=	{0: [2, 3], 1: [0], 2: [1], 3: [4], 4: []}

snake_case							:       Any										=	{0: [1, 2, 3], 1: [2], 2: [0], 3: [4], 4: [5], 5: [3]}
def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						list[bool]		):




      """simple docstring"""





      a		:Any									=			True
      a		:Any									=			[]

      for neighbour in graph[vert]:
            if not visited[neighbour]:
                  order += topology_sort(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      order.append(UpperCAmelCase_		)

      return order
def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]					,    UpperCAmelCase_		:						int					,    UpperCAmelCase_		:						list[bool]		):




      """simple docstring"""





      a		:Optional[Any]									=			True
      a		:int									=			[vert]

      for neighbour in reversed_graph[vert]:
            if not visited[neighbour]:
                  component += find_components(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      return component






def 							__lowerCamelCase		(					UpperCAmelCase_		:						dict[int, list[int]]		):




      """simple docstring"""





      a		:int									=			len(UpperCAmelCase_		) * [False]
      a		:dict[int, list[int]]									=			{vert: [] for vert in range(len(UpperCAmelCase_		)		)}

      for vert, neighbours in graph.items():
            for neighbour in neighbours:
                  reversed_graph[neighbour].append(UpperCAmelCase_		)

      a		:Optional[Any]									=			[]
      for i, was_visited in enumerate(UpperCAmelCase_		):
            if not was_visited:
                  order += topology_sort(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)

      a		:Tuple									=			[]
      a		:int									=			len(UpperCAmelCase_		) * [False]

      for i in range(len(UpperCAmelCase_		)		):
            a		:Union[str, Any]									=			order[len(UpperCAmelCase_		) - i - 1]
            if not visited[vert]:
                  a		:Union[str, Any]									=			find_components(UpperCAmelCase_					,    UpperCAmelCase_					,    UpperCAmelCase_		)
                  components_list.append(UpperCAmelCase_		)

      return components_list





