






from typing import List, Optional

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


UpperCAmelCase__				   =		logging.get_logger(__name__)

UpperCAmelCase__				   =		{
    "huggingface/autoformer-tourism-monthly": "https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json",
}






class 		lowercase_					(	lowercase   ):






      '''simple docstring'''




      __snake_case       =  '''autoformer'''
      __snake_case       =  {
          '''hidden_size''': '''d_model''',
          '''num_attention_heads''': '''encoder_attention_heads''',
          '''num_hidden_layers''': '''encoder_layers''',
      }







      def __init__(      self      :						Optional[int] ,					__UpperCAmelCase      :						Optional[int] = None ,					__UpperCAmelCase      :						Optional[int] = None ,					__UpperCAmelCase      :						str = "student_t" ,					__UpperCAmelCase      :						str = "nll" ,					__UpperCAmelCase      :						int = 1 ,					__UpperCAmelCase      :						List[int] = [1, 2, 3, 4, 5, 6, 7] ,					__UpperCAmelCase      :						bool = True ,					__UpperCAmelCase      :						int = 0 ,					__UpperCAmelCase      :						int = 0 ,					__UpperCAmelCase      :						int = 0 ,					__UpperCAmelCase      :						int = 0 ,					__UpperCAmelCase      :						Optional[List[int]] = None ,					__UpperCAmelCase      :						Optional[List[int]] = None ,					__UpperCAmelCase      :						int = 64 ,					__UpperCAmelCase      :						int = 2 ,					__UpperCAmelCase      :						int = 2 ,					__UpperCAmelCase      :						int = 2 ,					__UpperCAmelCase      :						int = 2 ,					__UpperCAmelCase      :						int = 32 ,					__UpperCAmelCase      :						int = 32 ,					__UpperCAmelCase      :						str = "gelu" ,					__UpperCAmelCase      :						float = 0.1 ,					__UpperCAmelCase      :						float = 0.1 ,					__UpperCAmelCase      :						float = 0.1 ,					__UpperCAmelCase      :						float = 0.1 ,					__UpperCAmelCase      :						float = 0.1 ,					__UpperCAmelCase      :						int = 100 ,					__UpperCAmelCase      :						float = 0.02 ,					__UpperCAmelCase      :						bool = True ,					__UpperCAmelCase      :						Dict=True ,					__UpperCAmelCase      :						int = 10 ,					__UpperCAmelCase      :						int = 25 ,					__UpperCAmelCase      :						int = 3 ,					**__UpperCAmelCase      :						int ,					)    ->str:




       """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       __lowerCAmelCase					(      self      :						Union[str, Any]  )    ->int:




       """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
       )




import flax.linen as nn
import jax.numpy as jnp

from .attention_flax import FlaxTransformeraDModel
from .resnet_flax import FlaxDownsampleaD, FlaxResnetBlockaD, FlaxUpsampleaD




class 						lowercase__  (						nn.Module  ):
	A__    :   int
	A__    :   int
	A__    :   float				       =0.0
	A__    :   int				       =1
	A__    :   int				       =1
	A__    :   bool				       =True
	A__    :   bool				       =False
	A__    :   bool				       =False
	A__    :   bool				       =False
	A__    :   jnp.dtype				       =jnp.floataa
	def      A_				(			self							:   Optional[Any]							):
						SCREAMING_SNAKE_CASE__													= []
						SCREAMING_SNAKE_CASE__													= []

						for i in range(self.num_layers							):
											SCREAMING_SNAKE_CASE__													= self.in_channels if i == 0 else self.out_channels

											SCREAMING_SNAKE_CASE__													= FlaxResnetBlockaD(
											    in_channels=UpperCAmelCase_       ,   out_channels=self.out_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
											resnets.append(UpperCAmelCase_							)

											SCREAMING_SNAKE_CASE__													= FlaxTransformeraDModel(
											    in_channels=self.out_channels       ,   n_heads=self.num_attention_heads       ,   d_head=self.out_channels // self.num_attention_heads       ,   depth=1       ,   use_linear_projection=self.use_linear_projection       ,   only_cross_attention=self.only_cross_attention       ,   use_memory_efficient_attention=self.use_memory_efficient_attention       ,   dtype=self.dtype       ,   )
											attentions.append(UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= resnets
						SCREAMING_SNAKE_CASE__													= attentions

						if self.add_downsample:
											SCREAMING_SNAKE_CASE__													= FlaxDownsampleaD(self.out_channels       ,   dtype=self.dtype							)





	def __call__(			self							:   int       ,   UpperCAmelCase_							:   str       ,   UpperCAmelCase_							:   Tuple       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   int=True							):
						SCREAMING_SNAKE_CASE__													= ()

						for resnet, attn in zip(self.resnets       ,   self.attentions							):
											SCREAMING_SNAKE_CASE__													= resnet(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)
											SCREAMING_SNAKE_CASE__													= attn(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)
											output_states += (hidden_states,)

						if self.add_downsample:
											SCREAMING_SNAKE_CASE__													= self.downsamplers_a(UpperCAmelCase_							)
											output_states += (hidden_states,)

						return hidden_states, output_states





class 						lowercase__  (						nn.Module  ):
	A__    :   int
	A__    :   int
	A__    :   float				       =0.0
	A__    :   int				       =1
	A__    :   bool				       =True
	A__    :   jnp.dtype				       =jnp.floataa
	def      A_				(			self							:   List[str]							):
						SCREAMING_SNAKE_CASE__													= []

						for i in range(self.num_layers							):
											SCREAMING_SNAKE_CASE__													= self.in_channels if i == 0 else self.out_channels

											SCREAMING_SNAKE_CASE__													= FlaxResnetBlockaD(
											    in_channels=UpperCAmelCase_       ,   out_channels=self.out_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
											resnets.append(UpperCAmelCase_							)
						SCREAMING_SNAKE_CASE__													= resnets

						if self.add_downsample:
											SCREAMING_SNAKE_CASE__													= FlaxDownsampleaD(self.out_channels       ,   dtype=self.dtype							)





	def __call__(			self							:   List[str]       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   Union[str, Any]       ,   UpperCAmelCase_							:   List[Any]=True							):
						SCREAMING_SNAKE_CASE__													= ()

						for resnet in self.resnets:
											SCREAMING_SNAKE_CASE__													= resnet(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)
											output_states += (hidden_states,)

						if self.add_downsample:
											SCREAMING_SNAKE_CASE__													= self.downsamplers_a(UpperCAmelCase_							)
											output_states += (hidden_states,)

						return hidden_states, output_states





class 						lowercase__  (						nn.Module  ):
	A__    :   int
	A__    :   int
	A__    :   int
	A__    :   float				       =0.0
	A__    :   int				       =1
	A__    :   int				       =1
	A__    :   bool				       =True
	A__    :   bool				       =False
	A__    :   bool				       =False
	A__    :   bool				       =False
	A__    :   jnp.dtype				       =jnp.floataa
	def      A_				(			self							:   List[str]							):
						SCREAMING_SNAKE_CASE__													= []
						SCREAMING_SNAKE_CASE__													= []

						for i in range(self.num_layers							):
											SCREAMING_SNAKE_CASE__													= self.in_channels if (i == self.num_layers - 1) else self.out_channels
											SCREAMING_SNAKE_CASE__													= self.prev_output_channel if i == 0 else self.out_channels

											SCREAMING_SNAKE_CASE__													= FlaxResnetBlockaD(
											    in_channels=resnet_in_channels + res_skip_channels       ,   out_channels=self.out_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
											resnets.append(UpperCAmelCase_							)

											SCREAMING_SNAKE_CASE__													= FlaxTransformeraDModel(
											    in_channels=self.out_channels       ,   n_heads=self.num_attention_heads       ,   d_head=self.out_channels // self.num_attention_heads       ,   depth=1       ,   use_linear_projection=self.use_linear_projection       ,   only_cross_attention=self.only_cross_attention       ,   use_memory_efficient_attention=self.use_memory_efficient_attention       ,   dtype=self.dtype       ,   )
											attentions.append(UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= resnets
						SCREAMING_SNAKE_CASE__													= attentions

						if self.add_upsample:
											SCREAMING_SNAKE_CASE__													= FlaxUpsampleaD(self.out_channels       ,   dtype=self.dtype							)





	def __call__(			self							:   str       ,   UpperCAmelCase_							:   str       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   str       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   Union[str, Any]=True							):
						for resnet, attn in zip(self.resnets       ,   self.attentions							):
											# pop res hidden states
											SCREAMING_SNAKE_CASE__													= res_hidden_states_tuple[-1]
											SCREAMING_SNAKE_CASE__													= res_hidden_states_tuple[:-1]
											SCREAMING_SNAKE_CASE__													= jnp.concatenate((hidden_states, res_hidden_states)       ,   axis=-1							)

											SCREAMING_SNAKE_CASE__													= resnet(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)
											SCREAMING_SNAKE_CASE__													= attn(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)

						if self.add_upsample:
											SCREAMING_SNAKE_CASE__													= self.upsamplers_a(UpperCAmelCase_							)

						return hidden_states





class 						lowercase__  (						nn.Module  ):
	A__    :   int
	A__    :   int
	A__    :   int
	A__    :   float				       =0.0
	A__    :   int				       =1
	A__    :   bool				       =True
	A__    :   jnp.dtype				       =jnp.floataa
	def      A_				(			self							:   List[Any]							):
						SCREAMING_SNAKE_CASE__													= []

						for i in range(self.num_layers							):
											SCREAMING_SNAKE_CASE__													= self.in_channels if (i == self.num_layers - 1) else self.out_channels
											SCREAMING_SNAKE_CASE__													= self.prev_output_channel if i == 0 else self.out_channels

											SCREAMING_SNAKE_CASE__													= FlaxResnetBlockaD(
											    in_channels=resnet_in_channels + res_skip_channels       ,   out_channels=self.out_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
											resnets.append(UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= resnets

						if self.add_upsample:
											SCREAMING_SNAKE_CASE__													= FlaxUpsampleaD(self.out_channels       ,   dtype=self.dtype							)





	def __call__(			self							:   Tuple       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   Any       ,   UpperCAmelCase_							:   Dict       ,   UpperCAmelCase_							:   Dict=True							):
						for resnet in self.resnets:
											# pop res hidden states
											SCREAMING_SNAKE_CASE__													= res_hidden_states_tuple[-1]
											SCREAMING_SNAKE_CASE__													= res_hidden_states_tuple[:-1]
											SCREAMING_SNAKE_CASE__													= jnp.concatenate((hidden_states, res_hidden_states)       ,   axis=-1							)

											SCREAMING_SNAKE_CASE__													= resnet(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)

						if self.add_upsample:
											SCREAMING_SNAKE_CASE__													= self.upsamplers_a(UpperCAmelCase_							)

						return hidden_states





class 						lowercase__  (						nn.Module  ):
	A__    :   int
	A__    :   float				       =0.0
	A__    :   int				       =1
	A__    :   int				       =1
	A__    :   bool				       =False
	A__    :   bool				       =False
	A__    :   jnp.dtype				       =jnp.floataa
	def      A_				(			self							:   Optional[int]							):
						# there is always at least one resnet
						SCREAMING_SNAKE_CASE__													= [
						    FlaxResnetBlockaD(
						        in_channels=self.in_channels       ,   out_channels=self.in_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
						]

						SCREAMING_SNAKE_CASE__													= []

						for _ in range(self.num_layers							):
											SCREAMING_SNAKE_CASE__													= FlaxTransformeraDModel(
											    in_channels=self.in_channels       ,   n_heads=self.num_attention_heads       ,   d_head=self.in_channels // self.num_attention_heads       ,   depth=1       ,   use_linear_projection=self.use_linear_projection       ,   use_memory_efficient_attention=self.use_memory_efficient_attention       ,   dtype=self.dtype       ,   )
											attentions.append(UpperCAmelCase_							)

											SCREAMING_SNAKE_CASE__													= FlaxResnetBlockaD(
											    in_channels=self.in_channels       ,   out_channels=self.in_channels       ,   dropout_prob=self.dropout       ,   dtype=self.dtype       ,   )
											resnets.append(UpperCAmelCase_							)

						SCREAMING_SNAKE_CASE__													= resnets
						SCREAMING_SNAKE_CASE__													= attentions





	def __call__(			self							:   str       ,   UpperCAmelCase_							:   Union[str, Any]       ,   UpperCAmelCase_							:   Optional[int]       ,   UpperCAmelCase_							:   List[str]       ,   UpperCAmelCase_							:   Any=True							):
						SCREAMING_SNAKE_CASE__													= self.resnets[0](UpperCAmelCase_       ,   UpperCAmelCase_							)
						for attn, resnet in zip(self.attentions       ,   self.resnets[1:]							):
											SCREAMING_SNAKE_CASE__													= attn(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)
											SCREAMING_SNAKE_CASE__													= resnet(UpperCAmelCase_       ,   UpperCAmelCase_       ,   deterministic=UpperCAmelCase_							)

						return hidden_states





