

from typing import List, Optional, Union

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


A__						=							logging.get_logger(__name__)

A__						=							{
    "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 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE			):
	_lowercase  :   Optional[int]       				=  "time_series_transformer"
	_lowercase  :   Dict       				=  {
	    "hidden_size": "d_model",
	    "num_attention_heads": "encoder_attention_heads",
	    "num_hidden_layers": "encoder_layers",
	}







	def __init__(   self: str  , __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: Optional[Union[str, bool]] = "mean"  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: int = 0  , __UpperCamelCase: Optional[List[int]] = None  , __UpperCamelCase: Optional[List[int]] = None  , __UpperCamelCase: int = 32  , __UpperCamelCase: int = 32  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: int = 2  , __UpperCamelCase: bool = True  , __UpperCamelCase: str = "gelu"  , __UpperCamelCase: int = 64  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: float = 0.1  , __UpperCamelCase: int = 1_00  , __UpperCamelCase: float = 0.02  , __UpperCamelCase: Any=True  , **__UpperCamelCase: Optional[Any]  , ):

						'''simple docstring'''





						__magic_name__							=    prediction_length
						__magic_name__							=    context_length or prediction_length
						__magic_name__							=    distribution_output
						__magic_name__							=    loss
						__magic_name__							=    input_size
						__magic_name__							=    num_time_features
						__magic_name__							=    lags_sequence
						__magic_name__							=    scaling
						__magic_name__							=    num_dynamic_real_features
						__magic_name__							=    num_static_real_features
						__magic_name__							=    num_static_categorical_features
						if cardinality 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`'      )
											__magic_name__							=    cardinality
						else:
											__magic_name__							=    [0]
						if embedding_dimension 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`'      )
											__magic_name__							=    embedding_dimension
						else:
											__magic_name__							=    [min(50  , (cat + 1) // 2      ) for cat in self.cardinality]
						__magic_name__							=    num_parallel_samples

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

						__magic_name__							=    dropout
						__magic_name__							=    attention_dropout
						__magic_name__							=    activation_dropout
						__magic_name__							=    encoder_layerdrop
						__magic_name__							=    decoder_layerdrop

						__magic_name__							=    activation_function
						__magic_name__							=    init_std

						__magic_name__							=    use_cache

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







	@property
	def 	_SCREAMING_SNAKE_CASE							(   self: str      ):

						'''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 torch
from torch import nn

from ...configuration_utils import ConfigMixin, register_to_config
from ...models import ModelMixin

class 				__UpperCamelCase				(					SCREAMING_SNAKE_CASE  ,						SCREAMING_SNAKE_CASE			):







	@register_to_config
	def __init__(   self: Optional[Any]  , *,
	    __UpperCamelCase: int = 4  , __UpperCamelCase: int = 7_68  , __UpperCamelCase: int  , __UpperCamelCase: Any  , ):

						'''simple docstring'''





						super().__init__()

						__magic_name__							=    nn.Parameter(torch.zeros(__UpperCamelCase      )      )

						# parameters for additional clip time embeddings
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )

						# parameters for encoder hidden states
						__magic_name__							=    clip_extra_context_tokens
						__magic_name__							=    nn.Linear(
						    __UpperCamelCase  , self.clip_extra_context_tokens * cross_attention_dim      )
						__magic_name__							=    nn.Linear(__UpperCamelCase  , __UpperCamelCase      )
						__magic_name__							=    nn.LayerNorm(__UpperCamelCase      )







	def 	_SCREAMING_SNAKE_CASE							(   self: List[str]  , *, __UpperCamelCase: Union[str, Any]  , __UpperCamelCase: str  , __UpperCamelCase: Optional[Any]  , __UpperCamelCase: Any      ):

						'''simple docstring'''





						if do_classifier_free_guidance:
											# Add the classifier free guidance embeddings to the image embeddings
											__magic_name__							=    image_embeddings.shape[0]
											__magic_name__							=    self.learned_classifier_free_guidance_embeddings.unsqueeze(0      )
											__magic_name__							=    classifier_free_guidance_embeddings.expand(
											    __UpperCamelCase  , -1      )
											__magic_name__							=    torch.cat([classifier_free_guidance_embeddings, image_embeddings]  , dim=0      )

						# The image embeddings batch size and the text embeddings batch size are equal
						assert image_embeddings.shape[0] == prompt_embeds.shape[0]

						__magic_name__							=    prompt_embeds.shape[0]

						# "Specifically, we modify the architecture described in Nichol et al. (2021) by projecting and
						# adding CLIP embeddings to the existing timestep embedding, ...
						__magic_name__							=    self.embedding_proj(__UpperCamelCase      )
						__magic_name__							=    self.clip_image_embeddings_project_to_time_embeddings(__UpperCamelCase      )
						__magic_name__							=    time_projected_image_embeddings + time_projected_prompt_embeds

						# ... and by projecting CLIP embeddings into four
						# extra tokens of context that are concatenated to the sequence of outputs from the GLIDE text encoder"
						__magic_name__							=    self.clip_extra_context_tokens_proj(__UpperCamelCase      )
						__magic_name__							=    clip_extra_context_tokens.reshape(__UpperCamelCase  , -1  , self.clip_extra_context_tokens      )
						__magic_name__							=    clip_extra_context_tokens.permute(0  , 2  , 1      )

						__magic_name__							=    self.encoder_hidden_states_proj(__UpperCamelCase      )
						__magic_name__							=    self.text_encoder_hidden_states_norm(__UpperCamelCase      )
						__magic_name__							=    torch.cat([clip_extra_context_tokens, text_encoder_hidden_states]  , dim=1      )

						return text_encoder_hidden_states, additive_clip_time_embeddings




