





"""simple docstring"""
from typing import Optional

import torch
import torch.utils.checkpoint
from torch import Tensor, nn
from torch.nn import BCEWithLogitsLoss, CrossEntropyLoss, MSELoss

from ...activations import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_outputs import (
    BaseModelOutputWithNoAttention,
    BaseModelOutputWithPoolingAndNoAttention,
    ImageClassifierOutputWithNoAttention,
)
from ...modeling_utils import PreTrainedModel
from ...utils import logging
from .configuration_regnet import RegNetConfig


_snake_case     :		str       						= logging.get_logger(__name__)

# General docstring
_snake_case     :		Optional[int]       						= 'RegNetConfig'

# Base docstring
_snake_case     :		int       						= 'facebook/regnet-y-040'
_snake_case     :		Optional[int]       						= [1, 1088, 7, 7]

# Image classification docstring
_snake_case     :		Union[str, Any]       						= 'facebook/regnet-y-040'
_snake_case     :		List[Any]       						= 'tabby, tabby cat'

_snake_case     :		str       						= [
    'facebook/regnet-y-040',
    # See all regnet models at https://huggingface.co/models?filter=regnet
]






class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Union[str, Any]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:Dict = 3     ,       __UpperCamelCase			:Optional[int] = 1     ,       __UpperCamelCase			:List[Any] = 1     ,       __UpperCamelCase			:Dict = "relu"     ,       ):
												super().__init__()
												A		=  nn.Convad(
												    __UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=__UpperCamelCase     ,       stride=__UpperCamelCase     ,       padding=kernel_size // 2     ,       groups=__UpperCamelCase     ,       bias=__UpperCamelCase     ,       )
												A		=  nn.BatchNormad(__UpperCamelCase		)
												A		=  ACTaFN[activation] if activation is not None else nn.Identity()


					def 	lowerCamelCase       (					self			:int     ,       __UpperCamelCase			:int		):
												A		=  self.convolution(__UpperCamelCase		)
												A		=  self.normalization(__UpperCamelCase		)
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:List[Any]     ,       __UpperCamelCase			:List[str]		):
												super().__init__()
												A		=  RegNetConvLayer(
												    config.num_channels     ,       config.embedding_size     ,       kernel_size=3     ,       stride=2     ,       activation=config.hidden_act		)
												A		=  config.num_channels


					def 	lowerCamelCase       (					self			:str     ,       __UpperCamelCase			:str		):
												A		=  pixel_values.shape[1]
												if num_channels != self.num_channels:
																			raise ValueError(
																			    "Make sure that the channel dimension of the pixel values match with the one set in the configuration."		)
												A		=  self.embedder(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:List[Any]     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:int = 2		):
												super().__init__()
												A		=  nn.Convad(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1     ,       stride=__UpperCamelCase     ,       bias=__UpperCamelCase		)
												A		=  nn.BatchNormad(__UpperCamelCase		)


					def 	lowerCamelCase       (					self			:int     ,       __UpperCamelCase			:Optional[Any]		):
												A		=  self.convolution(__UpperCamelCase		)
												A		=  self.normalization(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Any     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:List[Any]		):
												super().__init__()

												A		=  nn.AdaptiveAvgPoolad((1, 1)		)
												A		=  nn.Sequential(
												    nn.Convad(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1		)     ,       nn.ReLU()     ,       nn.Convad(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1		)     ,       nn.Sigmoid()     ,       )


					def 	lowerCamelCase       (					self			:Optional[Any]     ,       __UpperCamelCase			:Optional[int]		):
												A		=  self.pooler(__UpperCamelCase		)
												A		=  self.attention(__UpperCamelCase		)
												A		=  hidden_state * attention
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:str     ,       __UpperCamelCase			:int     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:Union[str, Any]     ,       __UpperCamelCase			:Optional[int] = 1		):
												super().__init__()
												A		=  in_channels != out_channels or stride != 1
												A		=  max(1     ,       out_channels // config.groups_width		)
												A		=  (
												    RegNetShortCut(__UpperCamelCase     ,       __UpperCamelCase     ,       stride=__UpperCamelCase		) if should_apply_shortcut else nn.Identity()
												)
												A		=  nn.Sequential(
												    RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1     ,       activation=config.hidden_act		)     ,       RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       stride=__UpperCamelCase     ,       groups=__UpperCamelCase     ,       activation=config.hidden_act		)     ,       RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1     ,       activation=__UpperCamelCase		)     ,       )
												A		=  ACTaFN[config.hidden_act]


					def 	lowerCamelCase       (					self			:Optional[int]     ,       __UpperCamelCase			:Optional[int]		):
												A		=  hidden_state
												A		=  self.layer(__UpperCamelCase		)
												A		=  self.shortcut(__UpperCamelCase		)
												hidden_state += residual
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Dict     ,       __UpperCamelCase			:Tuple     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:Optional[Any] = 1		):
												super().__init__()
												A		=  in_channels != out_channels or stride != 1
												A		=  max(1     ,       out_channels // config.groups_width		)
												A		=  (
												    RegNetShortCut(__UpperCamelCase     ,       __UpperCamelCase     ,       stride=__UpperCamelCase		) if should_apply_shortcut else nn.Identity()
												)
												A		=  nn.Sequential(
												    RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1     ,       activation=config.hidden_act		)     ,       RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       stride=__UpperCamelCase     ,       groups=__UpperCamelCase     ,       activation=config.hidden_act		)     ,       RegNetSELayer(__UpperCamelCase     ,       reduced_channels=int(round(in_channels / 4		)		)		)     ,       RegNetConvLayer(__UpperCamelCase     ,       __UpperCamelCase     ,       kernel_size=1     ,       activation=__UpperCamelCase		)     ,       )
												A		=  ACTaFN[config.hidden_act]


					def 	lowerCamelCase       (					self			:Optional[int]     ,       __UpperCamelCase			:str		):
												A		=  hidden_state
												A		=  self.layer(__UpperCamelCase		)
												A		=  self.shortcut(__UpperCamelCase		)
												hidden_state += residual
												A		=  self.activation(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:List[str]     ,       __UpperCamelCase			:Optional[int]     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:List[str]     ,       __UpperCamelCase			:Any = 2     ,       __UpperCamelCase			:Dict = 2     ,       ):
												super().__init__()

												A		=  RegNetXLayer if config.layer_type == "x" else RegNetYLayer

												A		=  nn.Sequential(
												    # downsampling is done in the first layer with stride of 2
												    layer(
												        __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       stride=__UpperCamelCase     ,       )     ,       *[layer(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase		) for _ in range(depth - 1		)]     ,       )


					def 	lowerCamelCase       (					self			:str     ,       __UpperCamelCase			:Union[str, Any]		):
												A		=  self.layers(__UpperCamelCase		)
												return hidden_state







class 					_UpperCAmelCase   (	nn.Module ):


					def __init__(					self			:Optional[int]     ,       __UpperCamelCase			:Union[str, Any]		):
												super().__init__()
												A		=  nn.ModuleList([]		)
												# based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
												self.stages.append(
												    RegNetStage(
												        __UpperCamelCase     ,       config.embedding_size     ,       config.hidden_sizes[0]     ,       stride=2 if config.downsample_in_first_stage else 1     ,       depth=config.depths[0]     ,       )		)
												A		=  zip(config.hidden_sizes     ,       config.hidden_sizes[1:]		)
												for (in_channels, out_channels), depth in zip(__UpperCamelCase     ,       config.depths[1:]		):
																			self.stages.append(RegNetStage(__UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase     ,       depth=__UpperCamelCase		)		)


					def 	lowerCamelCase       (					self			:Union[str, Any]     ,       __UpperCamelCase			:Optional[Any]     ,       __UpperCamelCase			:List[Any] = False     ,       __UpperCamelCase			:Union[str, Any] = True		):
												A		=  () if output_hidden_states else None

												for stage_module in self.stages:
																			if output_hidden_states:
																										A		=  hidden_states + (hidden_state,)

																			A		=  stage_module(__UpperCamelCase		)

												if output_hidden_states:
																			A		=  hidden_states + (hidden_state,)

												if not return_dict:
																			return tuple(v for v in [hidden_state, hidden_states] if v is not None		)

												return BaseModelOutputWithNoAttention(last_hidden_state=__UpperCamelCase     ,       hidden_states=__UpperCamelCase		)







class 					_UpperCAmelCase   (	lowercase_ ):
					UpperCamelCase      =							RegNetConfig
					UpperCamelCase      =							'''regnet'''
					UpperCamelCase      =							'''pixel_values'''
					UpperCamelCase      =							True


					def 	lowerCamelCase       (					self			:int     ,       __UpperCamelCase			:Optional[Any]		):






												if isinstance(__UpperCamelCase     ,       nn.Convad		):
																			nn.init.kaiming_normal_(module.weight     ,       mode="fan_out"     ,       nonlinearity="relu"		)
												elif isinstance(__UpperCamelCase     ,       (nn.BatchNormad, nn.GroupNorm)		):
																			nn.init.constant_(module.weight     ,       1		)
																			nn.init.constant_(module.bias     ,       0		)


					def 	lowerCamelCase       (					self			:int     ,       __UpperCamelCase			:Any     ,       __UpperCamelCase			:str=False		):






												if isinstance(__UpperCamelCase     ,       __UpperCamelCase		):
																			A		=  value







_snake_case     :		Dict       						= r'\n    This model is a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass. Use it\n    as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and\n    behavior.\n\n    Parameters:\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~PreTrainedModel.from_pretrained`] method to load the model weights.\n'

_snake_case     :		Any       						= r'\n    Args:\n        pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConvNextImageProcessor.__call__`] for details.\n\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~file_utils.ModelOutput`] instead of a plain tuple.\n'






@add_start_docstrings(
    '''The bare RegNet model outputting raw features without any specific head on top.'''     ,							lowercase_     ,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetModel with RESNET->REGNET,ResNet->RegNet
class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Tuple     ,       __UpperCamelCase			:Tuple		):
												super().__init__(__UpperCamelCase		)
												A		=  config
												A		=  RegNetEmbeddings(__UpperCamelCase		)
												A		=  RegNetEncoder(__UpperCamelCase		)
												A		=  nn.AdaptiveAvgPoolad((1, 1)		)
												# Initialize weights and apply final processing
												self.post_init()


					@add_start_docstrings_to_model_forward(__UpperCamelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_CHECKPOINT_FOR_DOC     ,       output_type=__UpperCamelCase     ,       config_class=_CONFIG_FOR_DOC     ,       modality="vision"     ,       expected_output=_EXPECTED_OUTPUT_SHAPE     ,       )
					def 	lowerCamelCase       (					self			:Any     ,       __UpperCamelCase			:Dict     ,       __UpperCamelCase			:List[str] = None     ,       __UpperCamelCase			:Any = None		):
												A		=  (
												    output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
												)
												A		=  return_dict if return_dict is not None else self.config.use_return_dict

												A		=  self.embedder(__UpperCamelCase		)

												A		=  self.encoder(
												    __UpperCamelCase     ,       output_hidden_states=__UpperCamelCase     ,       return_dict=__UpperCamelCase		)

												A		=  encoder_outputs[0]

												A		=  self.pooler(__UpperCamelCase		)

												if not return_dict:
																			return (last_hidden_state, pooled_output) + encoder_outputs[1:]

												return BaseModelOutputWithPoolingAndNoAttention(
												    last_hidden_state=__UpperCamelCase     ,       pooler_output=__UpperCamelCase     ,       hidden_states=encoder_outputs.hidden_states     ,       )







@add_start_docstrings(
    '''
    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for
    ImageNet.
    '''     ,							lowercase_     ,							)
# Copied from transformers.models.resnet.modeling_resnet.ResNetForImageClassification with RESNET->REGNET,ResNet->RegNet,resnet->regnet
class 					_UpperCAmelCase   (	lowercase_ ):


					def __init__(					self			:Any     ,       __UpperCamelCase			:Tuple		):
												super().__init__(__UpperCamelCase		)
												A		=  config.num_labels
												A		=  RegNetModel(__UpperCamelCase		)
												# classification head
												A		=  nn.Sequential(
												    nn.Flatten()     ,       nn.Linear(config.hidden_sizes[-1]     ,       config.num_labels		) if config.num_labels > 0 else nn.Identity()     ,       )
												# initialize weights and apply final processing
												self.post_init()


					@add_start_docstrings_to_model_forward(__UpperCamelCase		)
					@add_code_sample_docstrings(
					    checkpoint=_IMAGE_CLASS_CHECKPOINT     ,       output_type=__UpperCamelCase     ,       config_class=_CONFIG_FOR_DOC     ,       expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT     ,       )
					def 	lowerCamelCase       (					self			:List[Any]     ,       __UpperCamelCase			:Tuple = None     ,       __UpperCamelCase			:Dict = None     ,       __UpperCamelCase			:Optional[int] = None     ,       __UpperCamelCase			:Union[str, Any] = None     ,       ):
												A		=  return_dict if return_dict is not None else self.config.use_return_dict

												A		=  self.regnet(__UpperCamelCase     ,       output_hidden_states=__UpperCamelCase     ,       return_dict=__UpperCamelCase		)

												A		=  outputs.pooler_output if return_dict else outputs[1]

												A		=  self.classifier(__UpperCamelCase		)

												A		=  None

												if labels is not None:
																			if self.config.problem_type is None:
																										if self.num_labels == 1:
																																	A		=  "regression"
																										elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
																																	A		=  "single_label_classification"
																										else:
																																	A		=  "multi_label_classification"
																			if self.config.problem_type == "regression":
																										A		=  MSELoss()
																										if self.num_labels == 1:
																																	A		=  loss_fct(logits.squeeze()     ,       labels.squeeze()		)
																										else:
																																	A		=  loss_fct(__UpperCamelCase     ,       __UpperCamelCase		)
																			elif self.config.problem_type == "single_label_classification":
																										A		=  CrossEntropyLoss()
																										A		=  loss_fct(logits.view(-1     ,       self.num_labels		)     ,       labels.view(-1		)		)
																			elif self.config.problem_type == "multi_label_classification":
																										A		=  BCEWithLogitsLoss()
																										A		=  loss_fct(__UpperCamelCase     ,       __UpperCamelCase		)

												if not return_dict:
																			A		=  (logits,) + outputs[2:]
																			return (loss,) + output if loss is not None else output

												return ImageClassifierOutputWithNoAttention(loss=__UpperCamelCase     ,       logits=__UpperCamelCase     ,       hidden_states=outputs.hidden_states		)





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


lowercase_									=   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  __lowerCAmelCase     :




  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							,   )  -> Optional[Any]:
        '''simple docstring'''






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

        _lowercase	       =context_length
        _lowercase	       =prediction_length + label_length
        _lowercase	       =label_length

        _lowercase	       =moving_average
        _lowercase	       =autocorrelation_factor




  def   A__     (				self					)  -> Optional[Any]:
        '''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							,   lowerCAmelCase					)  -> Dict:
        '''simple docstring'''






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

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

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

        _lowercase	       ={
            '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					)  -> List[str]:
        '''simple docstring'''






        _lowercase	       =self.get_config()
        _lowercase	       =self.prepare_autoformer_inputs_dict(lowerCAmelCase					)
        return config, inputs_dict




  def   A__     (				self					)  -> List[str]:
        '''simple docstring'''






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




  def   A__     (				self							,   lowerCAmelCase							,   lowerCAmelCase					)  -> Tuple:
        '''simple docstring'''






        _lowercase	       =AutoformerModel(config=lowerCAmelCase					).to(lowerCAmelCase					).eval()
        _lowercase	       =model(**lowerCAmelCase					)

        _lowercase	       =outputs.encoder_last_hidden_state
        _lowercase	       =outputs.last_hidden_state

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

        _lowercase       ,       _lowercase       ,       _lowercase       ,       _lowercase       ,       _lowercase	       =model.create_network_inputs(**lowerCAmelCase					)
        _lowercase       ,       _lowercase	       =model.decomposition_layer(transformer_inputs[:, : config.context_length, ...]					)

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

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

        _lowercase	       =torch.cat(
            (
                torch.cat((seasonal_input[:, -config.label_length :, ...], zeros)							,   dim=1					),
                feature[:, config.context_length - config.label_length :, ...],
            )							,   dim=-1							,   )
        _lowercase	       =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:
              _lowercase	       =model.get_decoder()
              decoder.save_pretrained(lowerCAmelCase					)
              _lowercase	       =AutoformerDecoder.from_pretrained(lowerCAmelCase					).to(lowerCAmelCase					)

        _lowercase	       =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  __lowerCAmelCase     (					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					)  -> str:
        '''simple docstring'''






        _lowercase	       =AutoformerModelTester(self					)
        _lowercase	       =ConfigTester(self							,   config_class=lowerCAmelCase							,   has_text_modality=lowerCAmelCase					)




  def   A__     (				self					)  -> Optional[Any]:
        '''simple docstring'''






        self.config_tester.run_common_tests()




  def   A__     (				self					)  -> Optional[Any]:
        '''simple docstring'''






        _lowercase       ,       _lowercase	       =self.model_tester.prepare_config_and_inputs()
        for model_class in self.all_model_classes:
              _lowercase	       =model_class(lowerCAmelCase					)

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




  def   A__     (				self					)  -> List[str]:
        '''simple docstring'''






        _lowercase	       =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   A__     (				self					)  -> int:
        '''simple docstring'''






        pass




  def   A__     (				self					)  -> Dict:
        '''simple docstring'''






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




  def   A__     (				self					)  -> List[Any]:
        '''simple docstring'''






        _lowercase       ,       _lowercase	       =self.model_tester.prepare_config_and_inputs_for_common()

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

              _lowercase	       =[
                  '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   A__     (				self					)  -> int:
        '''simple docstring'''






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

        _lowercase	       =getattr(self.model_tester							,   'seq_length'							,   lowerCAmelCase					)
        _lowercase	       =getattr(self.model_tester							,   'decoder_seq_length'							,   lowerCAmelCase					)
        _lowercase	       =getattr(self.model_tester							,   'encoder_seq_length'							,   lowerCAmelCase					)
        _lowercase	       =getattr(self.model_tester							,   'd_model'							,   lowerCAmelCase					)
        _lowercase	       =getattr(self.model_tester							,   'num_attention_heads'							,   lowerCAmelCase					)
        _lowercase	       =d_model // num_attention_heads

        for model_class in self.all_model_classes:
              _lowercase	       =True
              _lowercase	       =False
              _lowercase	       =True
              _lowercase	       =model_class(lowerCAmelCase					)
              model.to(lowerCAmelCase					)
              model.eval()
              with torch.no_grad():
                    _lowercase	       =model(**self._prepare_for_class(lowerCAmelCase							,   lowerCAmelCase					)					)
              _lowercase	       =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"]
              _lowercase	       =True
              _lowercase	       =model_class(lowerCAmelCase					)
              model.to(lowerCAmelCase					)
              model.eval()
              with torch.no_grad():
                    _lowercase	       =model(**self._prepare_for_class(lowerCAmelCase							,   lowerCAmelCase					)					)
              _lowercase	       =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]							,   )
              _lowercase	       =len(lowerCAmelCase					)

              _lowercase	       =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
              _lowercase	       =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
              _lowercase	       =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
        _lowercase	       =True
        _lowercase	       =True
        _lowercase	       =model_class(lowerCAmelCase					)
        model.to(lowerCAmelCase					)
        model.eval()
        with torch.no_grad():
              _lowercase	       =model(**self._prepare_for_class(lowerCAmelCase							,   lowerCAmelCase					)					)

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

        _lowercase	       =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   A__     (				self					)  -> Dict:
        '''simple docstring'''






        super().test_retain_grad_hidden_states_attentions()
def      a      (			A__	:   List[str]="train-batch.pt"     )    ->      str:





      """simple docstring"""



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







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




  def   A__     (				self					)  -> int:
        '''simple docstring'''






        _lowercase	       =AutoformerModel.from_pretrained('huggingface/autoformer-tourism-monthly'					).to(lowerCAmelCase					)
        _lowercase	       =prepare_batch()

        with torch.no_grad():
              _lowercase	       =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]

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

        _lowercase	       =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   A__     (				self					)  -> str:
        '''simple docstring'''






        _lowercase	       =AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'					).to(lowerCAmelCase					)
        _lowercase	       =prepare_batch('val-batch.pt'					)
        with torch.no_grad():
              _lowercase	       =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
        _lowercase	       =torch.Size((64, model.config.context_length, model.config.d_model)					)
        self.assertEqual(output.shape							,   lowerCAmelCase					)

        _lowercase	       =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   A__     (				self					)  -> Optional[int]:
        '''simple docstring'''






        _lowercase	       =AutoformerForPrediction.from_pretrained('huggingface/autoformer-tourism-monthly'					).to(lowerCAmelCase					)
        _lowercase	       =prepare_batch('val-batch.pt'					)
        with torch.no_grad():
              _lowercase	       =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']							,   )
        _lowercase	       =torch.Size((64, model.config.num_parallel_samples, model.config.prediction_length)					)
        self.assertEqual(outputs.sequences.shape							,   lowerCAmelCase					)

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


