


"""simple docstring"""


from typing import Callable, Dict, Optional, Tuple

import torch
from torch import nn
from torch.distributions import (
    AffineTransform,
    Distribution,
    Independent,
    NegativeBinomial,
    Normal,
    StudentT,
    TransformedDistribution,
)


class 			A__	(						__lowercase):


     """simple docstring"""

     def __init__(	self:						List[Any]     ,		__a:						Distribution     ,		__a:						List[Any]=None     ,		__a:						Optional[int]=None     ,		__a:						Union[str, Any]=0	)->    Any:
           lowerCamelCase					:					Any        =	1.0 if scale is None else scale
           lowerCamelCase					:					int        =	0.0 if loc is None else loc

           super().__init__(__a     ,		[AffineTransform(loc=self.loc     ,		scale=self.scale     ,		event_dim=__a	)]	)

     @property
     def 		a__						(	self:						Tuple	)->    Union[str, Any]:
           return self.base_dist.mean * self.scale + self.loc

     @property
     def 		a__						(	self:						Dict	)->    List[str]:
           return self.base_dist.variance * self.scale**2







     @property
     def 		a__						(	self:						List[str]	)->    int:
           return self.variance.sqrt()


class 			A__	(						nn.Module):


     """simple docstring"""

     def __init__(	self:						Optional[int]     ,		__a:						int     ,		__a:						Dict[str, int]     ,		__a:						Callable[..., Tuple[torch.Tensor]]     ,		**__a:						Optional[Any]	)->    None:
           super().__init__(**__a	)
           lowerCamelCase					:					Optional[int]        =	args_dim
           lowerCamelCase					:					List[str]        =	nn.ModuleList([nn.Linear(__a     ,		__a	) for dim in args_dim.values()]	)
           lowerCamelCase					:					str        =	domain_map







     def 		a__						(	self:						List[str]     ,		__a:						torch.Tensor	)->    Tuple[torch.Tensor]:
           lowerCamelCase					:					List[Any]        =	[proj(__a	) for proj in self.proj]

           return self.domain_map(*__a	)


class 			A__	(						nn.Module):


     """simple docstring"""

     def __init__(	self:						Union[str, Any]     ,		__a:						str	)->    Tuple:
           super().__init__()
           lowerCamelCase					:					Any        =	function







     def 		a__						(	self:						str     ,		__a:						Union[str, Any]     ,		*__a:						Optional[Any]	)->    Any:
           return self.function(__a     ,		*__a	)


class 			A__	:


     """simple docstring"""
     snake_case__	:					type
     snake_case__	:					int
     snake_case__	:					Dict[str, int]

     def __init__(	self:						Dict     ,		__a:						int = 1	)->    None:
           lowerCamelCase					:					List[Any]        =	dim
           lowerCamelCase					:					List[Any]        =	{k: dim * self.args_dim[k] for k in self.args_dim}

     def 		a__						(	self:						List[str]     ,		__a:						Dict	)->    List[Any]:
           if self.dim == 1:
                 return self.distribution_class(*__a	)
           else:
                 return Independent(self.distribution_class(*__a	)     ,		1	)

     def 		a__						(	self:						Any     ,		__a:						str     ,		__a:						Optional[torch.Tensor] = None     ,		__a:						Optional[torch.Tensor] = None     ,		)->    Distribution:
           lowerCamelCase					:					Union[str, Any]        =	self._base_distribution(__a	)
           if loc is None and scale is None:
                 return distr
           else:
                 return AffineTransformed(__a     ,		loc=__a     ,		scale=__a     ,		event_dim=self.event_dim	)

     @property
     def 		a__						(	self:						str	)->    Tuple:
           return () if self.dim == 1 else (self.dim,)

     @property
     def 		a__						(	self:						Union[str, Any]	)->    int:
           return len(self.event_shape	)

     @property
     def 		a__						(	self:						Dict	)->    float:
           return 0.0

     def 		a__						(	self:						Union[str, Any]     ,		__a:						int	)->    nn.Module:
           return ParameterProjection(
               in_features=__a     ,		args_dim=self.args_dim     ,		domain_map=LambdaLayer(self.domain_map	)     ,		)

     def 		a__						(	self:						Any     ,		*__a:						torch.Tensor	)->    str:
           raise NotImplementedError()







     @staticmethod
     def 		a__						(	__a:						torch.Tensor	)->    torch.Tensor:
           return (x + torch.sqrt(torch.square(__a	) + 4.0	)) / 2.0


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Dict[str, int]													={"df": 1, "loc": 1, "scale": 1}
     snake_case__	:					type													=StudentT

     @classmethod
     def 		a__						(	cls:						Optional[Any]     ,		__a:						torch.Tensor     ,		__a:						torch.Tensor     ,		__a:						torch.Tensor	)->    Tuple:
           lowerCamelCase					:					Optional[Any]        =	cls.squareplus(__a	).clamp_min(torch.finfo(scale.dtype	).eps	)
           lowerCamelCase					:					Optional[int]        =	2.0 + cls.squareplus(__a	)
           return df.squeeze(-1	), loc.squeeze(-1	), scale.squeeze(-1	)


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Dict[str, int]													={"loc": 1, "scale": 1}
     snake_case__	:					type													=Normal

     @classmethod
     def 		a__						(	cls:						Optional[int]     ,		__a:						torch.Tensor     ,		__a:						torch.Tensor	)->    str:
           lowerCamelCase					:					int        =	cls.squareplus(__a	).clamp_min(torch.finfo(scale.dtype	).eps	)
           return loc.squeeze(-1	), scale.squeeze(-1	)


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					Dict[str, int]													={"total_count": 1, "logits": 1}
     snake_case__	:					type													=NegativeBinomial

     @classmethod
     def 		a__						(	cls:						Tuple     ,		__a:						torch.Tensor     ,		__a:						torch.Tensor	)->    Union[str, Any]:
           lowerCamelCase					:					int        =	cls.squareplus(__a	)
           return total_count.squeeze(-1	), logits.squeeze(-1	)

     def 		a__						(	self:						List[Any]     ,		__a:						Union[str, Any]	)->    Distribution:
           lowerCamelCase					:					Optional[int]        =	distr_args
           if self.dim == 1:
                 return self.distribution_class(total_count=__a     ,		logits=__a	)
           else:
                 return Independent(self.distribution_class(total_count=__a     ,		logits=__a	)     ,		1	)







     def 		a__						(	self:						str     ,		__a:						Any     ,		__a:						Optional[torch.Tensor] = None     ,		__a:						Optional[torch.Tensor] = None	)->    Distribution:
           lowerCamelCase					:					List[str]        =	distr_args

           if scale is not None:
                 # See scaling property of Gamma.
                 logits += scale.log()

           return self._base_distribution((total_count, logits)	)








"""simple docstring"""


from typing import List, Optional, Union

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


__lowerCamelCase      :str	    =		logging.get_logger(__name__)

__lowerCamelCase      :Any	    =		{
    'huggingface/time-series-transformer-tourism-monthly': (
        'https://huggingface.co/huggingface/time-series-transformer-tourism-monthly/resolve/main/config.json'
    ),
    # See all TimeSeriesTransformer models at https://huggingface.co/models?filter=time_series_transformer
}


class 			A__	(						__lowercase):


     """simple docstring"""
     snake_case__	:					List[Any]													='''time_series_transformer'''
     snake_case__	:					List[Any]													={
         '''hidden_size''': '''d_model''',
         '''num_attention_heads''': '''encoder_attention_heads''',
         '''num_hidden_layers''': '''encoder_layers''',
     }

     def __init__(	self:						List[str]     ,		__a:						Optional[int] = None     ,		__a:						Optional[int] = None     ,		__a:						str = "student_t"     ,		__a:						str = "nll"     ,		__a:						int = 1     ,		__a:						List[int] = [1, 2, 3, 4, 5, 6, 7]     ,		__a:						Optional[Union[str, bool]] = "mean"     ,		__a:						int = 0     ,		__a:						int = 0     ,		__a:						int = 0     ,		__a:						int = 0     ,		__a:						Optional[List[int]] = None     ,		__a:						Optional[List[int]] = None     ,		__a:						int = 32     ,		__a:						int = 32     ,		__a:						int = 2     ,		__a:						int = 2     ,		__a:						int = 2     ,		__a:						int = 2     ,		__a:						bool = True     ,		__a:						str = "gelu"     ,		__a:						int = 64     ,		__a:						float = 0.1     ,		__a:						float = 0.1     ,		__a:						float = 0.1     ,		__a:						float = 0.1     ,		__a:						float = 0.1     ,		__a:						int = 100     ,		__a:						float = 0.02     ,		__a:						Tuple=True     ,		**__a:						str     ,		)->    Any:
           # time series specific configuration
           lowerCamelCase					:					str        =	prediction_length
           lowerCamelCase					:					Optional[Any]        =	context_length or prediction_length
           lowerCamelCase					:					Tuple        =	distribution_output
           lowerCamelCase					:					Any        =	loss
           lowerCamelCase					:					List[Any]        =	input_size
           lowerCamelCase					:					int        =	num_time_features
           lowerCamelCase					:					Dict        =	lags_sequence
           lowerCamelCase					:					Optional[int]        =	scaling
           lowerCamelCase					:					int        =	num_dynamic_real_features
           lowerCamelCase					:					Tuple        =	num_static_real_features
           lowerCamelCase					:					Any        =	num_static_categorical_features
           if cardinality and num_static_categorical_features > 0:
                 if len(__a	) != num_static_categorical_features:
                       raise ValueError(
                           """The cardinality should be a list of the same length as `num_static_categorical_features`"""	)
                 lowerCamelCase					:					int        =	cardinality
           else:
                 lowerCamelCase					:					Dict        =	[0]
           if embedding_dimension and num_static_categorical_features > 0:
                 if len(__a	) != num_static_categorical_features:
                       raise ValueError(
                           """The embedding dimension should be a list of the same length as `num_static_categorical_features`"""	)
                 lowerCamelCase					:					str        =	embedding_dimension
           else:
                 lowerCamelCase					:					str        =	[min(50     ,		(cat + 1) // 2	) for cat in self.cardinality]
           lowerCamelCase					:					Any        =	num_parallel_samples

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

           lowerCamelCase					:					Optional[int]        =	dropout
           lowerCamelCase					:					List[str]        =	attention_dropout
           lowerCamelCase					:					Tuple        =	activation_dropout
           lowerCamelCase					:					Optional[int]        =	encoder_layerdrop
           lowerCamelCase					:					int        =	decoder_layerdrop

           lowerCamelCase					:					Optional[int]        =	activation_function
           lowerCamelCase					:					Optional[Any]        =	init_std

           lowerCamelCase					:					Optional[Any]        =	use_cache

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







     @property
     def 		a__						(	self:						int	)->    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
           )




