






"""simple docstring"""






from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE    :    Optional[Any]        =							logging.get_logger(__name__)

SCREAMING_SNAKE_CASE    :    Any        =							{
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}





class    snake_case_							(     __SCREAMING_SNAKE_CASE							):



	"""simple docstring"""

	SCREAMING_SNAKE_CASE_:				Dict       =       """autoformer"""
	SCREAMING_SNAKE_CASE_:				List[str]       =       {
	    """hidden_size""": """d_model""",
	    """num_attention_heads""": """encoder_attention_heads""",
	    """num_hidden_layers""": """encoder_layers""",
	}


	def __init__(   self	,       __a = None	,       __a = None	,       __a = "student_t"	,       __a = "nll"	,       __a = 1	,       __a = [1, 2, 3, 4, 5, 6, 7]	,       __a = True	,       __a = 0	,       __a = 0	,       __a = 0	,       __a = 0	,       __a = None	,       __a = None	,       __a = 64	,       __a = 2	,       __a = 2	,       __a = 2	,       __a = 2	,       __a = 32	,       __a = 32	,       __a = "gelu"	,       __a = 0.1	,       __a = 0.1	,       __a = 0.1	,       __a = 0.1	,       __a = 0.1	,       __a = 100	,       __a = 0.02	,       __a = True	,       __a=True	,       __a = 10	,       __a = 25	,       __a = 3	,       **__a	,       ):


					"""simple docstring"""

					A__							=							prediction_length
					A__							=							context_length if context_length is not None else prediction_length
					A__							=							distribution_output
					A__							=							loss
					A__							=							input_size
					A__							=							num_time_features
					A__							=							lags_sequence
					A__							=							scaling
					A__							=							num_dynamic_real_features
					A__							=							num_static_real_features
					A__							=							num_static_categorical_features
					if cardinality is not None and num_static_categorical_features > 0:
									if len(UpperCamelCase__    ) != num_static_categorical_features:
													raise ValueError(
													    'The cardinality should be a list of the same length as `num_static_categorical_features`'    )
									A__							=							cardinality
					else:
									A__							=							[0]
					if embedding_dimension is not None and num_static_categorical_features > 0:
									if len(UpperCamelCase__    ) != num_static_categorical_features:
													raise ValueError(
													    'The embedding dimension should be a list of the same length as `num_static_categorical_features`'    )
									A__							=							embedding_dimension
					else:
									A__							=							[min(50	,       (cat + 1) // 2    ) for cat in self.cardinality]
					A__							=							num_parallel_samples

					# Transformer architecture configuration
					A__							=							input_size * len(self.lags_sequence    ) + self._number_of_features
					A__							=							d_model
					A__							=							encoder_attention_heads
					A__							=							decoder_attention_heads
					A__							=							encoder_ffn_dim
					A__							=							decoder_ffn_dim
					A__							=							encoder_layers
					A__							=							decoder_layers

					A__							=							dropout
					A__							=							attention_dropout
					A__							=							activation_dropout
					A__							=							encoder_layerdrop
					A__							=							decoder_layerdrop

					A__							=							activation_function
					A__							=							init_std

					A__							=							use_cache

					# Autoformer
					A__							=							label_length
					A__							=							moving_average
					A__							=							autocorrelation_factor

					super().__init__(is_encoder_decoder=UpperCamelCase__	,       **UpperCamelCase__    )






	@property
	def    _UpperCAmelCase    (   self    ):


					"""simple docstring"""

					return (
					    sum(self.embedding_dimension    )
					    + self.num_dynamic_real_features
					    + self.num_time_features
					    + self.num_static_real_features
					    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
					)




'''simple docstring'''
from dataclasses import dataclass
from typing import Optional

import torch
from torch import nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput
from .attention import BasicTransformerBlock
from .modeling_utils import ModelMixin






@dataclass
class 	snake_case__       (      __SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		lowerCamelCase     							=						42







class 	snake_case__       (      __SCREAMING_SNAKE_CASE    ,					__SCREAMING_SNAKE_CASE					):







		"""simple docstring"""

		@register_to_config
		def __init__(      self				:			List[str]  ,	UpperCamelCase__				:			int = 16  ,	UpperCamelCase__				:			int = 88  ,	UpperCamelCase__				:			Optional[int] = None  ,	UpperCamelCase__				:			Optional[int] = None  ,	UpperCamelCase__				:			int = 1  ,	UpperCamelCase__				:			float = 0.0  ,	UpperCamelCase__				:			int = 32  ,	UpperCamelCase__				:			Optional[int] = None  ,	UpperCamelCase__				:			bool = False  ,	UpperCamelCase__				:			Optional[int] = None  ,	UpperCamelCase__				:			str = "geglu"  ,	UpperCamelCase__				:			bool = True  ,	UpperCamelCase__				:			bool = True  ,	)						->			List[Any]:






				"""simple docstring"""







				super().__init__()
				snake_case  :       Optional[int]     			=						num_attention_heads
				snake_case  :       int     			=						attention_head_dim
				snake_case  :       Dict     			=						num_attention_heads * attention_head_dim

				snake_case  :       Tuple     			=						in_channels

				snake_case  :       Optional[int]     			=						torch.nn.GroupNorm(num_groups=UpperCamelCase__  ,	num_channels=UpperCamelCase__  ,	eps=1e-6  ,	affine=UpperCamelCase__      )
				snake_case  :       List[Any]     			=						nn.Linear(UpperCamelCase__  ,	UpperCamelCase__      )

				# 3. Define transformers blocks
				snake_case  :       Union[str, Any]     			=						nn.ModuleList(
				    [
				        BasicTransformerBlock(
				            UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	dropout=UpperCamelCase__  ,	cross_attention_dim=UpperCamelCase__  ,	activation_fn=UpperCamelCase__  ,	attention_bias=UpperCamelCase__  ,	double_self_attention=UpperCamelCase__  ,	norm_elementwise_affine=UpperCamelCase__  ,	)
				        for d in range(UpperCamelCase__      )
				    ]      )

				snake_case  :       Any     			=						nn.Linear(UpperCamelCase__  ,	UpperCamelCase__      )



		def       lowerCAmelCase				(      self				:			List[str]  ,	UpperCamelCase__				:			Tuple  ,	UpperCamelCase__				:			Any=None  ,	UpperCamelCase__				:			Dict=None  ,	UpperCamelCase__				:			Dict=None  ,	UpperCamelCase__				:			Union[str, Any]=1  ,	UpperCamelCase__				:			Optional[int]=None  ,	UpperCamelCase__				:			bool = True  ,	)						->			Any:






				"""simple docstring"""







				snake_case			,snake_case			,snake_case			,snake_case  :       Tuple     			=						hidden_states.shape
				snake_case  :       List[Any]     			=						batch_frames // num_frames

				snake_case  :       Union[str, Any]     			=						hidden_states

				snake_case  :       Optional[Any]     			=						hidden_states[None, :].reshape(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )
				snake_case  :       int     			=						hidden_states.permute(0  ,	2  ,	1  ,	3  ,	4      )

				snake_case  :       Tuple     			=						self.norm(UpperCamelCase__      )
				snake_case  :       Union[str, Any]     			=						hidden_states.permute(0  ,	3  ,	4  ,	2  ,	1      ).reshape(batch_size * height * width  ,	UpperCamelCase__  ,	UpperCamelCase__      )

				snake_case  :       Optional[Any]     			=						self.proj_in(UpperCamelCase__      )

				# 2. Blocks
				for block in self.transformer_blocks:
						snake_case  :       Union[str, Any]     			=						block(
						    UpperCamelCase__  ,	encoder_hidden_states=UpperCamelCase__  ,	timestep=UpperCamelCase__  ,	cross_attention_kwargs=UpperCamelCase__  ,	class_labels=UpperCamelCase__  ,	)

				# 3. Output
				snake_case  :       Dict     			=						self.proj_out(UpperCamelCase__      )
				snake_case  :       Tuple     			=						(
				    hidden_states[None, None, :]
				    .reshape(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )
				    .permute(0  ,	3  ,	4  ,	1  ,	2      )
				    .contiguous()
				)
				snake_case  :       List[Any]     			=						hidden_states.reshape(UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__  ,	UpperCamelCase__      )

				snake_case  :       Any     			=						hidden_states + residual

				if not return_dict:
						return (output,)

				return TransformerTemporalModelOutput(sample=UpperCamelCase__      )


