


from typing import List, Optional

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


UpperCAmelCase_   :				Dict						      = logging.get_logger(__name__)

UpperCAmelCase_   :				int						      = {
    'huggingface/autoformer-tourism-monthly': 'https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json',
}
class 				SCREAMING_SNAKE_CASE__						(       lowercase__    ):
      snake_case__       :  Union[str, Any]       =		'''autoformer'''
      snake_case__       :  Any       =		{
          '''hidden_size''': '''d_model''',
          '''num_attention_heads''': '''encoder_attention_heads''',
          '''num_hidden_layers''': '''encoder_layers''',
      }
      def __init__( self       : Optional[int]  ,							SCREAMING_SNAKE_CASE__       : Optional[int] = None  ,							SCREAMING_SNAKE_CASE__       : Optional[int] = None  ,							SCREAMING_SNAKE_CASE__       : str = "student_t"  ,							SCREAMING_SNAKE_CASE__       : str = "nll"  ,							SCREAMING_SNAKE_CASE__       : int = 1  ,							SCREAMING_SNAKE_CASE__       : List[int] = [1, 2, 3, 4, 5, 6, 7]  ,							SCREAMING_SNAKE_CASE__       : bool = True  ,							SCREAMING_SNAKE_CASE__       : int = 0  ,							SCREAMING_SNAKE_CASE__       : int = 0  ,							SCREAMING_SNAKE_CASE__       : int = 0  ,							SCREAMING_SNAKE_CASE__       : int = 0  ,							SCREAMING_SNAKE_CASE__       : Optional[List[int]] = None  ,							SCREAMING_SNAKE_CASE__       : Optional[List[int]] = None  ,							SCREAMING_SNAKE_CASE__       : int = 6_4  ,							SCREAMING_SNAKE_CASE__       : int = 2  ,							SCREAMING_SNAKE_CASE__       : int = 2  ,							SCREAMING_SNAKE_CASE__       : int = 2  ,							SCREAMING_SNAKE_CASE__       : int = 2  ,							SCREAMING_SNAKE_CASE__       : int = 3_2  ,							SCREAMING_SNAKE_CASE__       : int = 3_2  ,							SCREAMING_SNAKE_CASE__       : str = "gelu"  ,							SCREAMING_SNAKE_CASE__       : float = 0.1  ,							SCREAMING_SNAKE_CASE__       : float = 0.1  ,							SCREAMING_SNAKE_CASE__       : float = 0.1  ,							SCREAMING_SNAKE_CASE__       : float = 0.1  ,							SCREAMING_SNAKE_CASE__       : float = 0.1  ,							SCREAMING_SNAKE_CASE__       : int = 1_0_0  ,							SCREAMING_SNAKE_CASE__       : float = 0.02  ,							SCREAMING_SNAKE_CASE__       : bool = True  ,							SCREAMING_SNAKE_CASE__       : List[str]=True  ,							SCREAMING_SNAKE_CASE__       : int = 1_0  ,							SCREAMING_SNAKE_CASE__       : int = 2_5  ,							SCREAMING_SNAKE_CASE__       : int = 3  ,							**SCREAMING_SNAKE_CASE__       : int  ,							)	->    Union[str, Any]:
             # time series specific configuration
             a_      :	Optional[int]							=						prediction_length
             a_      :	Tuple							=						context_length if context_length is not None else prediction_length
             a_      :	Tuple							=						distribution_output
             a_      :	int							=						loss
             a_      :	Tuple							=						input_size
             a_      :	int							=						num_time_features
             a_      :	Dict							=						lags_sequence
             a_      :	List[str]							=						scaling
             a_      :	Tuple							=						num_dynamic_real_features
             a_      :	Dict							=						num_static_real_features
             a_      :	Dict							=						num_static_categorical_features
             if cardinality is not None and num_static_categorical_features > 0:
                    if len(SCREAMING_SNAKE_CASE__  ) != num_static_categorical_features:
                           raise ValueError(
                               'The cardinality should be a list of the same length as `num_static_categorical_features`'  )
                    a_      :	Any							=						cardinality
             else:
                    a_      :	Tuple							=						[0]
             if embedding_dimension is not None and num_static_categorical_features > 0:
                    if len(SCREAMING_SNAKE_CASE__  ) != num_static_categorical_features:
                           raise ValueError(
                               'The embedding dimension should be a list of the same length as `num_static_categorical_features`'  )
                    a_      :	Tuple							=						embedding_dimension
             else:
                    a_      :	Optional[Any]							=						[min(5_0  ,							(cat + 1) // 2  ) for cat in self.cardinality]
             a_      :	Optional[Any]							=						num_parallel_samples

             # Transformer architecture configuration
             a_      :	Dict							=						input_size * len(self.lags_sequence  ) + self._number_of_features
             a_      :	Tuple							=						d_model
             a_      :	int							=						encoder_attention_heads
             a_      :	Optional[int]							=						decoder_attention_heads
             a_      :	str							=						encoder_ffn_dim
             a_      :	List[str]							=						decoder_ffn_dim
             a_      :	Any							=						encoder_layers
             a_      :	Any							=						decoder_layers

             a_      :	List[Any]							=						dropout
             a_      :	Tuple							=						attention_dropout
             a_      :	Union[str, Any]							=						activation_dropout
             a_      :	List[Any]							=						encoder_layerdrop
             a_      :	List[str]							=						decoder_layerdrop

             a_      :	Optional[Any]							=						activation_function
             a_      :	Union[str, Any]							=						init_std

             a_      :	List[str]							=						use_cache

             # Autoformer
             a_      :	Tuple							=						label_length
             a_      :	List[Any]							=						moving_average
             a_      :	Optional[Any]							=						autocorrelation_factor

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

      @property
      def  SCREAMING_SNAKE_CASE							( self       : List[Any]  )	->    int:
             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 torch

from diffusers import DDIMParallelScheduler

from .test_schedulers import SchedulerCommonTest
class 				SCREAMING_SNAKE_CASE__						(       lowercase__    ):
      snake_case__       :  Optional[Any]       =		(DDIMParallelScheduler,)
      snake_case__       :  Union[str, Any]       =		(('''eta''', 0.0), ('''num_inference_steps''', 50))
      def  SCREAMING_SNAKE_CASE							( self       : str  ,							**SCREAMING_SNAKE_CASE__       : List[Any]  )	->    List[Any]:
             a_      :	Union[str, Any]							=						{
                 'num_train_timesteps': 1_0_0_0,
                 'beta_start': 0.0001,
                 'beta_end': 0.02,
                 'beta_schedule': 'linear',
                 'clip_sample': True,
             }

             config.update(**SCREAMING_SNAKE_CASE__  )
             return config
      def  SCREAMING_SNAKE_CASE							( self       : str  ,							**SCREAMING_SNAKE_CASE__       : Tuple  )	->    int:
             a_      :	Any							=						self.scheduler_classes[0]
             a_      :	str							=						self.get_scheduler_config(**SCREAMING_SNAKE_CASE__  )
             a_      :	Optional[Any]							=						scheduler_class(**SCREAMING_SNAKE_CASE__  )

             a_							,	a_      :	Optional[Any]							=						1_0, 0.0

             a_      :	str							=						self.dummy_model()
             a_      :	Dict							=						self.dummy_sample_deter

             scheduler.set_timesteps(SCREAMING_SNAKE_CASE__  )

             for t in scheduler.timesteps:
                    a_      :	List[Any]							=						model(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  )
                    a_      :	Optional[Any]							=						scheduler.step(SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ,							SCREAMING_SNAKE_CASE__  ).prev_sample

             return sample
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    Any:
             for timesteps in [1_0_0, 5_0_0, 1_0_0_0]:
                    self.check_over_configs(num_train_timesteps=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    List[Any]:
             for steps_offset in [0, 1]:
                    self.check_over_configs(steps_offset=SCREAMING_SNAKE_CASE__  )

             a_      :	Tuple							=						self.scheduler_classes[0]
             a_      :	int							=						self.get_scheduler_config(steps_offset=1  )
             a_      :	str							=						scheduler_class(**SCREAMING_SNAKE_CASE__  )
             scheduler.set_timesteps(5  )
             assert torch.equal(scheduler.timesteps  ,							torch.LongTensor([8_0_1, 6_0_1, 4_0_1, 2_0_1, 1]  )  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    List[str]:
             for beta_start, beta_end in zip([0.0001, 0.001, 0.01, 0.1]  ,							[0.002, 0.02, 0.2, 2]  ):
                    self.check_over_configs(beta_start=SCREAMING_SNAKE_CASE__  ,							beta_end=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[Any]  )	->    List[str]:
             for schedule in ["linear", "squaredcos_cap_v2"]:
                    self.check_over_configs(beta_schedule=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    Dict:
             for prediction_type in ["epsilon", "v_prediction"]:
                    self.check_over_configs(prediction_type=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Optional[int]  )	->    Tuple:
             for clip_sample in [True, False]:
                    self.check_over_configs(clip_sample=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    Union[str, Any]:
             for timestep_spacing in ["trailing", "leading"]:
                    self.check_over_configs(timestep_spacing=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    int:
             for rescale_betas_zero_snr in [True, False]:
                    self.check_over_configs(rescale_betas_zero_snr=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : int  )	->    Tuple:
             self.check_over_configs(thresholding=SCREAMING_SNAKE_CASE__  )
             for threshold in [0.5, 1.0, 2.0]:
                    for prediction_type in ["epsilon", "v_prediction"]:
                           self.check_over_configs(
                               thresholding=SCREAMING_SNAKE_CASE__  ,							prediction_type=SCREAMING_SNAKE_CASE__  ,							sample_max_value=SCREAMING_SNAKE_CASE__  ,							)
      def  SCREAMING_SNAKE_CASE							( self       : Any  )	->    Tuple:
             for t in [1, 1_0, 4_9]:
                    self.check_over_forward(time_step=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    Dict:
             for t, num_inference_steps in zip([1, 1_0, 5_0]  ,							[1_0, 5_0, 5_0_0]  ):
                    self.check_over_forward(time_step=SCREAMING_SNAKE_CASE__  ,							num_inference_steps=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    Optional[Any]:
             for t, eta in zip([1, 1_0, 4_9]  ,							[0.0, 0.5, 1.0]  ):
                    self.check_over_forward(time_step=SCREAMING_SNAKE_CASE__  ,							eta=SCREAMING_SNAKE_CASE__  )
      def  SCREAMING_SNAKE_CASE							( self       : Dict  )	->    Tuple:
             a_      :	Union[str, Any]							=						self.scheduler_classes[0]
             a_      :	List[Any]							=						self.get_scheduler_config()
             a_      :	Dict							=						scheduler_class(**SCREAMING_SNAKE_CASE__  )

             assert torch.sum(torch.abs(scheduler._get_variance(0  ,							0  ) - 0.0  )  ) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(4_2_0  ,							4_0_0  ) - 0.14771  )  ) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(9_8_0  ,							9_6_0  ) - 0.32460  )  ) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(0  ,							0  ) - 0.0  )  ) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(4_8_7  ,							4_8_6  ) - 0.00979  )  ) < 1E-5
             assert torch.sum(torch.abs(scheduler._get_variance(9_9_9  ,							9_9_8  ) - 0.02  )  ) < 1E-5
      def  SCREAMING_SNAKE_CASE							( self       : Union[str, Any]  )	->    Dict:
             a_      :	Dict							=						self.scheduler_classes[0]
             a_      :	int							=						self.get_scheduler_config()
             a_      :	List[str]							=						scheduler_class(**SCREAMING_SNAKE_CASE__  )

             a_							,	a_      :	int							=						1_0, 0.0
             scheduler.set_timesteps(SCREAMING_SNAKE_CASE__  )

             a_      :	Union[str, Any]							=						self.dummy_model()
             a_      :	Optional[int]							=						self.dummy_sample_deter
             a_      :	Optional[Any]							=						self.dummy_sample_deter + 0.1
             a_      :	Any							=						self.dummy_sample_deter - 0.1

             a_      :	List[Any]							=						samplea.shape[0]
             a_      :	List[str]							=						torch.stack([samplea, samplea, samplea]  ,							dim=0  )
             a_      :	Union[str, Any]							=						torch.arange(SCREAMING_SNAKE_CASE__  )[0:3, None].repeat(1  ,							SCREAMING_SNAKE_CASE__  )

             a_      :	int							=						model(samples.flatten(0  ,							1  )  ,							timesteps.flatten(0  ,							1  )  )
             a_      :	Optional[Any]							=						scheduler.batch_step_no_noise(SCREAMING_SNAKE_CASE__  ,							timesteps.flatten(0  ,							1  )  ,							samples.flatten(0  ,							1  )  ,							SCREAMING_SNAKE_CASE__  )

             a_      :	Optional[int]							=						torch.sum(torch.abs(SCREAMING_SNAKE_CASE__  )  )
             a_      :	Optional[int]							=						torch.mean(torch.abs(SCREAMING_SNAKE_CASE__  )  )

             assert abs(result_sum.item() - 1147.7904  ) < 1E-2
             assert abs(result_mean.item() - 0.4982  ) < 1E-3
      def  SCREAMING_SNAKE_CASE							( self       : Optional[Any]  )	->    Tuple:
             a_      :	Tuple							=						self.full_loop()

             a_      :	List[str]							=						torch.sum(torch.abs(SCREAMING_SNAKE_CASE__  )  )
             a_      :	Any							=						torch.mean(torch.abs(SCREAMING_SNAKE_CASE__  )  )

             assert abs(result_sum.item() - 172.0067  ) < 1E-2
             assert abs(result_mean.item() - 0.223967  ) < 1E-3
      def  SCREAMING_SNAKE_CASE							( self       : int  )	->    List[str]:
             a_      :	Optional[int]							=						self.full_loop(prediction_type='v_prediction'  )

             a_      :	str							=						torch.sum(torch.abs(SCREAMING_SNAKE_CASE__  )  )
             a_      :	Union[str, Any]							=						torch.mean(torch.abs(SCREAMING_SNAKE_CASE__  )  )

             assert abs(result_sum.item() - 52.5302  ) < 1E-2
             assert abs(result_mean.item() - 0.0684  ) < 1E-3
      def  SCREAMING_SNAKE_CASE							( self       : str  )	->    Optional[Any]:
             # We specify different beta, so that the first alpha is 0.99
             a_      :	Dict							=						self.full_loop(set_alpha_to_one=SCREAMING_SNAKE_CASE__  ,							beta_start=0.01  )
             a_      :	int							=						torch.sum(torch.abs(SCREAMING_SNAKE_CASE__  )  )
             a_      :	str							=						torch.mean(torch.abs(SCREAMING_SNAKE_CASE__  )  )

             assert abs(result_sum.item() - 149.8295  ) < 1E-2
             assert abs(result_mean.item() - 0.1951  ) < 1E-3

      def  SCREAMING_SNAKE_CASE							( self       : Any  )	->    Union[str, Any]:
             # We specify different beta, so that the first alpha is 0.99
             a_      :	Tuple							=						self.full_loop(set_alpha_to_one=SCREAMING_SNAKE_CASE__  ,							beta_start=0.01  )
             a_      :	Dict							=						torch.sum(torch.abs(SCREAMING_SNAKE_CASE__  )  )
             a_      :	str							=						torch.mean(torch.abs(SCREAMING_SNAKE_CASE__  )  )

             assert abs(result_sum.item() - 149.0784  ) < 1E-2
             assert abs(result_mean.item() - 0.1941  ) < 1E-3


