






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 		UpperCAmelCase_    (						snake_case_       ):


						'''simple docstring'''




						def __init__(						self		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE=None		,					_SCREAMING_SNAKE_CASE=None		,					_SCREAMING_SNAKE_CASE=0					)			->					Any:
								snake_case_					:				Optional[Any]    			=							1.0 if scale is None else scale
								snake_case_					:				str    			=							0.0 if loc is None else loc

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






						@property
						def    _lowerCAmelCase							(						self					)			->					List[str]:
								return self.base_dist.mean * self.scale + self.loc






						@property
						def    _lowerCAmelCase							(						self					)			->					Dict:
								return self.base_dist.variance * self.scale**2






						@property
						def    _lowerCAmelCase							(						self					)			->					List[Any]:
								return self.variance.sqrt()




class 		UpperCAmelCase_    (						nn.Module       ):


						'''simple docstring'''




						def __init__(						self		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE		,					**_SCREAMING_SNAKE_CASE					)			->					None:
								super().__init__(**UpperCamelCase__					)
								snake_case_					:				Optional[int]    			=							args_dim
								snake_case_					:				str    			=							nn.ModuleList([nn.Linear(UpperCamelCase__		,					UpperCamelCase__					) for dim in args_dim.values()]					)
								snake_case_					:				Optional[int]    			=							domain_map






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE					)			->					Tuple[torch.Tensor]:
								snake_case_					:				List[str]    			=							[proj(UpperCamelCase__					) for proj in self.proj]

								return self.domain_map(*UpperCamelCase__					)




class 		UpperCAmelCase_    (						nn.Module       ):


						'''simple docstring'''




						def __init__(						self		,					_SCREAMING_SNAKE_CASE					)			->					Union[str, Any]:
								super().__init__()
								snake_case_					:				str    			=							function






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE		,					*_SCREAMING_SNAKE_CASE					)			->					Tuple:
								return self.function(UpperCamelCase__		,					*UpperCamelCase__					)




class 		UpperCAmelCase_    :


						'''simple docstring'''




						A  : type
						A  : int
						A  : Dict[str, int]






						def __init__(						self		,					_SCREAMING_SNAKE_CASE = 1					)			->					None:
								snake_case_					:				Optional[int]    			=							dim
								snake_case_					:				Optional[int]    			=							{k: dim * self.args_dim[k] for k in self.args_dim}






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE					)			->					Union[str, Any]:




								if self.dim == 1:
										return self.distribution_class(*UpperCamelCase__					)
								else:
										return Independent(self.distribution_class(*UpperCamelCase__					)		,					1					)






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE = None		,					_SCREAMING_SNAKE_CASE = None		,					)			->					Distribution:
								snake_case_					:				Optional[Any]    			=							self._base_distribution(UpperCamelCase__					)
								if loc is None and scale is None:
										return distr
								else:
										return AffineTransformed(UpperCamelCase__		,					loc=UpperCamelCase__		,					scale=UpperCamelCase__		,					event_dim=self.event_dim					)






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






						@property
						def    _lowerCAmelCase							(						self					)			->					int:
								return len(self.event_shape					)






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






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE					)			->					nn.Module:
								return ParameterProjection(
								    in_features=UpperCamelCase__		,					args_dim=self.args_dim		,					domain_map=LambdaLayer(self.domain_map					)		,					)






						def    _lowerCAmelCase							(						self		,					*_SCREAMING_SNAKE_CASE					)			->					Dict:
								raise NotImplementedError()






						@staticmethod
						def    _lowerCAmelCase							(						_SCREAMING_SNAKE_CASE					)			->					torch.Tensor:
								return (x + torch.sqrt(torch.square(UpperCamelCase__					) + 4.0					)) / 2.0




class 		UpperCAmelCase_    (						snake_case_       ):


						'''simple docstring'''




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






						@classmethod
						def    _lowerCAmelCase							(						cls		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE					)			->					str:
								snake_case_					:				List[Any]    			=							cls.squareplus(UpperCamelCase__					).clamp_min(torch.finfo(scale.dtype					).eps					)
								snake_case_					:				Dict    			=							2.0 + cls.squareplus(UpperCamelCase__					)
								return df.squeeze(-1					), loc.squeeze(-1					), scale.squeeze(-1					)




class 		UpperCAmelCase_    (						snake_case_       ):


						'''simple docstring'''




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






						@classmethod
						def    _lowerCAmelCase							(						cls		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE					)			->					Optional[Any]:
								snake_case_					:				str    			=							cls.squareplus(UpperCamelCase__					).clamp_min(torch.finfo(scale.dtype					).eps					)
								return loc.squeeze(-1					), scale.squeeze(-1					)




class 		UpperCAmelCase_    (						snake_case_       ):


						'''simple docstring'''




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






						@classmethod
						def    _lowerCAmelCase							(						cls		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE					)			->					Dict:
								snake_case_					:				int    			=							cls.squareplus(UpperCamelCase__					)
								return total_count.squeeze(-1					), logits.squeeze(-1					)






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE					)			->					Distribution:
								snake_case_					:				int    			=							distr_args
								if self.dim == 1:
										return self.distribution_class(total_count=UpperCamelCase__		,					logits=UpperCamelCase__					)
								else:
										return Independent(self.distribution_class(total_count=UpperCamelCase__		,					logits=UpperCamelCase__					)		,					1					)






						def    _lowerCAmelCase							(						self		,					_SCREAMING_SNAKE_CASE		,					_SCREAMING_SNAKE_CASE = None		,					_SCREAMING_SNAKE_CASE = None					)			->					Distribution:
								snake_case_					:				Optional[int]    			=							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 TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__snake_case       =       {
    """configuration_time_series_transformer""": [
        """TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """TimeSeriesTransformerConfig""",
    ],
}

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __snake_case       =       [
      """TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TimeSeriesTransformerForPrediction""",
      """TimeSeriesTransformerModel""",
      """TimeSeriesTransformerPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_time_series_transformer import (
      TIME_SERIES_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
      TimeSeriesTransformerConfig,
  )

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_time_series_transformer import (
        TIME_SERIES_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TimeSeriesTransformerForPrediction,
        TimeSeriesTransformerModel,
        TimeSeriesTransformerPreTrainedModel,
    )

else:
  import sys

  __snake_case       =       _LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)





