Ë
    T^(h"X ã                   óö  — d Z ddlZddlmZ ddlmZmZmZ ddlZddl	m
Z
 ddlmZ ddlmZ ddlmZmZmZ dd	lmZmZmZmZ d
dlmZ  ej4                  e«      ZdZdZdZ G d„ de
j@                  «      Z! G d„ de
j@                  «      Z" G d„ de
j@                  «      Z# G d„ de
j@                  «      Z$ G d„ de
j@                  «      Z% G d„ de
j@                  «      Z& G d„ de
j@                  «      Z' G d„ de
j@                  «      Z( G d„ d e
j@                  «      Z) G d!„ d"e
j@                  «      Z* G d#„ d$e
j@                  «      Z+ G d%„ d&e
j@                  «      Z, G d'„ d(e
j@                  «      Z- G d)„ d*e«      Z. G d+„ d,e
j@                  «      Z/	 	 	 dfd-ej`                  d.e1d/e2d0e3d1e4f
d2„Z5	 	 dgd-ej`                  d3ee2e4f   d/e2d1e4fd4„Z6 G d5„ d6e
j@                  «      Z7 G d7„ d8e
j@                  «      Z8 G d9„ d:e
j@                  «      Z9 G d;„ d<e
j@                  «      Z: G d=„ d>e
j@                  «      Z;e G d?„ d@e«      «       Z< G dA„ dBe.«      Z=e G dC„ dDe«      «       Z> edEe«       G dF„ dGe.«      «       Z?e G dH„ dIe«      «       Z@ G dJ„ dKe.«      ZAe G dL„ dMe«      «       ZBe G dN„ dOe«      «       ZCe G dP„ dQe«      «       ZDdRejŠ                  jŒ                  dSej`                  dTej`                  fdU„ZGdhdVej`                  dWeej`                     dTej`                  fdX„ZH G dY„ dZe.«      ZIe G d[„ d\e«      «       ZJ G d]„ d^e.«      ZKe G d_„ d`e«      «       ZL G da„ dbe
j@                  «      ZM G dc„ dde.«      ZNg de¢ZOy)izPyTorch PatchTSMixer model.é    N)Ú	dataclass)ÚOptionalÚTupleÚUnion)ÚPreTrainedModel)ÚModelOutputé   )ÚNegativeBinomialOutputÚNormalOutputÚStudentTOutput)Úadd_start_docstringsÚ%add_start_docstrings_to_model_forwardÚloggingÚreplace_return_docstringsé   )ÚPatchTSMixerConfigr   aÏ  

    This model inherits from [`PreTrainedModel`]. Check the superclass documentation for the generic methods the
    library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads
    etc.)

    This model is also a PyTorch [torch.nn.Module](https://pytorch.org/docs/stable/nn.html#torch.nn.Module) subclass.
    Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage
    and behavior.

    Parameters:
        config ([`PatchTSMixerConfig`]):
            Model configuration class with all the parameters of the model. Initializing with a config file does not
            load the weights associated with the model, only the configuration. Check out the
            [`~PreTrainedModel.from_pretrained`] method to load the model weights.
        mask_input (`bool`, *optional*, defaults to `False`):
            If True, Masking will be enabled. False otherwise.
a\  
    Args:
        past_values (`torch.FloatTensor` of shape `(batch_size, seq_length, num_input_channels)`):
            Context values of the time series. For a pretraining task, this denotes the input time series to predict
            the masked portion. For a forecasting task, this denotes the history/past time series values. Similarly,
            for classification or regression tasks, it denotes the appropriate context values of the time series.

            For univariate time series, `num_input_channels` dimension should be 1. For multivariate time series, it is
            greater than 1.

        output_hidden_states (`bool`, *optional*):
            Whether or not to return the hidden states of all layers.

        return_dict (`bool`, *optional*):
            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.
c                   ó2   ‡ — e Zd ZdZdedefˆ fd„Zd„ Zˆ xZS )ÚPatchTSMixerGatedAttentionz›
    Module that applies gated attention to input data.

    Args:
        in_size (`int`): The input size.
        out_size (`int`): The output size.
    Úin_sizeÚout_sizec                 óŽ   •— t         ‰| �  «        t        j                  ||«      | _        t        j
                  d¬«      | _        y )Néÿÿÿÿ©Údim)ÚsuperÚ__init__ÚnnÚLinearÚ
attn_layerÚSoftmaxÚattn_softmax)Úselfr   r   Ú	__class__s      €út/var/www/skyplay_api_hub/venv/lib/python3.12/site-packages/transformers/models/patchtsmixer/modeling_patchtsmixer.pyr   z#PatchTSMixerGatedAttention.__init__X   s1   ø€ Ü‰ÑÔÜŸ)™) G¨XÓ6ˆŒÜŸJ™J¨2Ô.ˆÕó    c                 óP   — | j                  | j                  |«      «      }||z  }|S ©N)r!   r   )r"   ÚinputsÚattn_weights      r$   Úforwardz"PatchTSMixerGatedAttention.forward]   s*   € Ø×'Ñ'¨¯©¸Ó(?Ó@ˆØ˜+Ñ%ˆØˆr%   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r*   Ú__classcell__©r#   s   @r$   r   r   O   s    ø„ ñð/ ð /¨sõ /ö
r%   r   c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚPatchTSMixerBatchNormzP
    Compute batch normalization over the sequence length (time) dimension.
    Úconfigc                 ó‚   •— t         ‰| �  «        t        j                  |j                  |j
                  ¬«      | _        y )N©Úeps)r   r   r   ÚBatchNorm1dÚd_modelÚnorm_epsÚ	batchnorm©r"   r4   r#   s     €r$   r   zPatchTSMixerBatchNorm.__init__i   s(   ø€ Ü‰ÑÔÜŸ™¨¯©¸F¿O¹OÔLˆ�r%   r(   c                 ól   — |j                  dd«      }| j                  |«      }|j                  dd«      S )a  
        Parameters:
            inputs (`torch.Tensor` of shape `(batch_size, sequence_length, d_model)`):
                input for Batch norm calculation
        Returns:
            `torch.Tensor` of shape `(batch_size, sequence_length, d_model)`
        r   é   )Ú	transposer;   )r"   r(   Úoutputs      r$   r*   zPatchTSMixerBatchNorm.forwardm   s7   € ð ×!Ñ! ! QÓ'ˆØ—‘ Ó'ˆØ×Ñ  1Ó%Ð%r%   ©
r+   r,   r-   r.   r   r   ÚtorchÚTensorr*   r0   r1   s   @r$   r3   r3   d   s'   ø„ ñðMÐ1õ Mð
&˜eŸl™l÷ 
&r%   r3   c                   óv   ‡ — e Zd ZdZdefˆ fd„Zededej                  fd„«       Z	de
j                  fd„Zˆ xZS )ÚPatchTSMixerPositionalEncodingz'
    Class for positional encoding
    r4   c                 óì   •— t         ‰| �  «        |j                  r| j                  |«      | _        y t        j                  t        j                  |j                  |j                  «      «      | _        y r'   )r   r   Úuse_positional_encodingÚ_init_peÚposition_encr   Ú	ParameterrB   ÚzerosÚnum_patchesr9   r<   s     €r$   r   z'PatchTSMixerPositionalEncoding.__init__   sN   ø€ Ü‰ÑÔà×)Ò)Ø $§¡¨fÓ 5ˆDÕä "§¡¬U¯[©[¸×9KÑ9KÈVÏ^É^Ó-\Ó ]ˆDÕr%   Úreturnc                 ó`  — | j                   dk(  rAt        j                  t        j                  | j
                  | j                  «      d¬«      }|S | j                   dk(  �r7t        j                  | j
                  | j                  «      }t        j                  d| j
                  «      j                  d«      }t        j                  t        j                  d| j                  d«      t        j                  d«      | j                  z   z  «      }t        j                  ||z  «      |d d …dd d…f<   t        j                  ||z  «      |d d …dd d…f<   ||j                  «       z
  }||j!                  «       d	z  z  }t        j                  |d
¬«      }|S t#        | j                   › d�«      ‚)NÚrandomT©Úrequires_gradÚsincosr   r   r>   g     ˆÃ@é
   FzN is not a valid positional encoder. Available types are 'random' and 'sincos'.)Úpositional_encoding_typer   rJ   rB   ÚrandnrL   r9   rK   ÚarangeÚ	unsqueezeÚexpÚmathÚlogÚsinÚcosÚmeanÚstdÚ
ValueError)r4   rI   ÚpositionÚdiv_terms       r$   rH   z'PatchTSMixerPositionalEncoding._init_pe‡   sv  € ð ×*Ñ*¨hÒ6ÜŸ<™<¬¯©°F×4FÑ4FÈÏÉÓ(WÐgkÔlˆLð Ðð ×,Ñ,°Ó8Ü Ÿ;™; v×'9Ñ'9¸6¿>¹>ÓJˆLÜ—|‘| A v×'9Ñ'9Ó:×DÑDÀQÓGˆHÜ—y‘y¤§¡¨a°·±ÀÓ!CÌÏÉÐQXÓHYÐ\b×\jÑ\jÑHjÐFkÑ!kÓlˆHÜ$)§I¡I¨h¸Ñ.AÓ$BˆLš˜A˜D˜q˜D˜Ñ!Ü$)§I¡I¨h¸Ñ.AÓ$BˆLš˜A˜D˜q˜D˜Ñ!Ø'¨,×*;Ñ*;Ó*=Ñ=ˆLØ'¨<×+;Ñ+;Ó+=ÀÑ+BÑCˆLÜŸ<™<¨ÀEÔJˆLð
 Ðô Ø×2Ñ2Ð3ð  4Bð  Cóð r%   Úpatch_inputc                 ó$   — || j                   z   }|S r'   )rI   )r"   rb   Úhidden_states      r$   r*   z&PatchTSMixerPositionalEncoding.forward›   s   € à" T×%6Ñ%6Ñ6ˆØÐr%   )r+   r,   r-   r.   r   r   Ústaticmethodr   rJ   rH   rB   rC   r*   r0   r1   s   @r$   rE   rE   z   sN   ø„ ñð^Ð1õ ^ð ðÐ+ð °·±ò ó ðð& 5§<¡<÷ r%   rE   c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚPatchTSMixerNormLayerzeNormalization block

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óþ   •— t         ‰| �  «        |j                  | _        d|j                  j                  «       v rt	        |«      | _        y t        j                  |j                  |j                  ¬«      | _        y )NÚbatchr6   )
r   r   Únorm_mlpÚlowerr3   Únormr   Ú	LayerNormr9   r:   r<   s     €r$   r   zPatchTSMixerNormLayer.__init__©   sT   ø€ Ü‰ÑÔàŸ™ˆŒà�f—o‘o×+Ñ+Ó-Ñ-Ü-¨fÓ5ˆD�IäŸ™ V§^¡^¸¿¹ÔIˆD�Ir%   r(   c                 óf  — d| j                   j                  «       v rƒt        j                  ||j                  d   |j                  d   z  |j                  d   |j                  d   f«      }| j                  |«      }t        j                  ||j                  «      }|S | j                  |«      }|S )a  
        Args:
            inputs (`torch.Tensor` of shape `((batch_size, num_channels, num_patches, d_model))`):
                Input to the normalization layer.
        Returns:
            `torch.Tensor` of shape `((batch_size, num_channels, num_patches, d_model))`
        ri   r   r   r>   r	   )rj   rk   rB   ÚreshapeÚshaperl   )r"   r(   Úinputs_reshapeds      r$   r*   zPatchTSMixerNormLayer.forward³   sŸ   € ð �d—m‘m×)Ñ)Ó+Ñ+ä#Ÿm™mØà—L‘L ‘O f§l¡l°1¡oÑ5Ø—L‘L ‘OØ—L‘L ‘OðóˆOð #Ÿi™i¨Ó8ˆOô —]‘] ?°F·L±LÓAˆFð
 ˆð —Y‘Y˜vÓ&ˆFàˆr%   rA   r1   s   @r$   rg   rg   ¡   s'   ø„ ñðJÐ1õ Jð˜eŸl™l÷ r%   rg   c                   ó>   ‡ — e Zd Zˆ fd„Zdej
                  fd„Zˆ xZS )ÚPatchTSMixerMLPc                 ó<  •— t         ‰| �  «        ||j                  z  }t        j                  ||«      | _        t        j                  |j                  «      | _        t        j                  ||«      | _	        t        j                  |j                  «      | _
        y r'   )r   r   Úexpansion_factorr   r   Úfc1ÚDropoutÚdropoutÚdropout1Úfc2Údropout2)r"   Úin_featuresÚout_featuresr4   Ú
num_hiddenr#   s        €r$   r   zPatchTSMixerMLP.__init__Ó   sj   ø€ Ü‰ÑÔØ  6×#:Ñ#:Ñ:ˆ
Ü—9‘9˜[¨*Ó5ˆŒÜŸ
™
 6§>¡>Ó2ˆŒÜ—9‘9˜Z¨Ó6ˆŒÜŸ
™
 6§>¡>Ó2ˆ�r%   r(   c                 óÄ   — | j                  t        j                  j                  | j	                  |«      «      «      }| j                  |«      }| j                  |«      }|S )zì
        Args:
            inputs (`torch.Tensor` of shape `((batch_size, num_channels, num_patches, d_model))`):
                Input to the MLP layer.
        Returns:
            `torch.Tensor` of the same shape as `inputs`
        )ry   r   Ú
functionalÚgelurv   rz   r{   )r"   r(   s     r$   r*   zPatchTSMixerMLP.forwardÛ   sK   € ð —‘œrŸ}™}×1Ñ1°$·(±(¸6Ó2BÓCÓDˆØ—‘˜&Ó!ˆØ—‘˜vÓ&ˆØˆr%   )r+   r,   r-   r   rB   rC   r*   r0   r1   s   @r$   rs   rs   Ò   s   ø„ ô3ð˜eŸl™l÷ r%   rs   c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )Ú$PatchTSMixerChannelFeatureMixerBlockzŠThis module mixes the features in the channel dimension.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 ó  •— t         ‰| �  «        t        |«      | _        |j                  | _        t        |j                  |j                  |¬«      | _        |j                  r't        |j                  |j                  ¬«      | _	        y y ©N©r|   r}   r4   ©r   r   )
r   r   rg   rl   Ú
gated_attnrs   Únum_input_channelsÚmlpr   Úgating_blockr<   s     €r$   r   z-PatchTSMixerChannelFeatureMixerBlock.__init__ñ   sv   ø€ Ü‰ÑÔä)¨&Ó1ˆŒ	Ø ×+Ñ+ˆŒÜ"Ø×1Ñ1Ø×2Ñ2Øô
ˆŒð ×ÒÜ :Ø×1Ñ1¸F×<UÑ<Uô!ˆDÕð r%   r(   c                 óâ   — |}| j                  |«      }|j                  dddd«      }| j                  r| j                  |«      }| j	                  |«      }|j                  dddd«      }||z   }|S )zë
        Args:
            inputs (`torch.Tensor` of shape `((batch_size, num_channels, num_patches, d_model))`):
                input to the MLP layer
        Returns:
            `torch.Tensor` of the same shape as `inputs`
        r   r	   r>   r   )rl   Úpermuterˆ   r‹   rŠ   )r"   r(   ÚresidualÚouts       r$   r*   z,PatchTSMixerChannelFeatureMixerBlock.forward  ss   € ð ˆØ—‘˜6Ó"ˆà—‘  1 a¨Ó+ˆà�?Š?Ø×&Ñ& vÓ.ˆFà—‘˜&Ó!ˆà—‘  1 a¨Ó+ˆà�xÑˆØˆ
r%   rA   r1   s   @r$   rƒ   rƒ   é   s%   ø„ ñðÐ1õ ð ˜eŸl™l÷ r%   rƒ   c                   ó†  ‡ — e Zd ZdZ	 	 	 	 	 ddededededededee   fˆ fd	„Z	d
e
j                  dedefd„Z	 	 	 	 	 dde
j                  dee
j                     deee
j                        dee
j                     dee
j                     dedee
j                  ee
j                     eee
j                        f   fd„Zˆ xZS )ÚPatchTSMixerAttentionz=Multi-headed attention from 'Attention Is All You Need' paperÚ	embed_dimÚ	num_headsrx   Ú
is_decoderÚbiasÚ	is_causalr4   c                 ó
  •— t         ‰| �  «        || _        || _        || _        ||z  | _        || _        | j
                  |z  | j                  k7  rt        d| j                  › d|› d�«      ‚| j
                  dz  | _        || _	        || _
        t        j                  |||¬«      | _        t        j                  |||¬«      | _        t        j                  |||¬«      | _        t        j                  |||¬«      | _        y )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: ú).g      à¿)r•   )r   r   r’   r“   rx   Úhead_dimr4   r_   Úscalingr”   r–   r   r   Úk_projÚv_projÚq_projÚout_proj)	r"   r’   r“   rx   r”   r•   r–   r4   r#   s	           €r$   r   zPatchTSMixerAttention.__init__  sä   ø€ ô 	‰ÑÔØ"ˆŒØ"ˆŒØˆŒØ! YÑ.ˆŒØˆŒà�M‰M˜IÑ%¨$¯.©.Ò8ÜØMÈdÏnÉnÐM]Ø$ Y K¨rð3óð ð —}‘} dÑ*ˆŒØ$ˆŒØ"ˆŒä—i‘i 	¨9¸4Ô@ˆŒÜ—i‘i 	¨9¸4Ô@ˆŒÜ—i‘i 	¨9¸4Ô@ˆŒÜŸ	™	 )¨Y¸TÔBˆ�r%   ÚtensorÚseq_lenÚbszc                 óŽ   — |j                  ||| j                  | j                  «      j                  dd«      j	                  «       S )Nr   r>   )Úviewr“   r™   r?   Ú
contiguous)r"   rŸ   r    r¡   s       r$   Ú_shapezPatchTSMixerAttention._shape<  s7   € Ø�{‰{˜3 ¨¯©¸¿¹ÓG×QÑQÐRSÐUVÓW×bÑbÓdÐdr%   Úhidden_statesÚkey_value_statesÚpast_key_valueÚattention_maskÚlayer_head_maskÚoutput_attentionsrM   c                 ó
  — |du}|j                  «       \  }}	}
| j                  |«      | j                  z  }|r0|�.|d   j                  d   |j                  d   k(  r|d   }|d   }�n
|rE| j	                  | j                  |«      d|«      }| j	                  | j                  |«      d|«      }nÃ|�}| j	                  | j                  |«      d|«      }| j	                  | j                  |«      d|«      }t        j                  |d   |gd¬«      }t        j                  |d   |gd¬«      }nD| j	                  | j                  |«      d|«      }| j	                  | j                  |«      d|«      }| j                  r||f}|| j                  z  d| j                  f} | j	                  ||	|«      j                  |Ž } |j                  |Ž } |j                  |Ž }|j                  d«      }t        j                  ||j                  dd«      «      }|j                  «       || j                  z  |	|fk7  r/t!        d|| j                  z  |	|f› d|j                  «       › �«      ‚|�{|j                  «       |d|	|fk7  r#t!        d	|d|	|f› d|j                  «       › �«      ‚|j                  || j                  |	|«      |z   }|j                  || j                  z  |	|«      }t"        j$                  j'                  |d¬«      }|�›|j                  «       | j                  fk7  r*t!        d
| j                  f› d|j                  «       › �«      ‚|j                  dddd«      |j                  || j                  |	|«      z  }|j                  || j                  z  |	|«      }|r?|j                  || j                  |	|«      }|j                  || j                  z  |	|«      }nd}t"        j$                  j)                  || j(                  | j*                  ¬«      }t        j                  ||«      }|j                  «       || j                  z  |	| j                  fk7  r9t!        d|| j                  z  |	| j                  f› d|j                  «       › �«      ‚|j                  || j                  |	| j                  «      }|j                  dd«      }|j                  ||	| j,                  «      }| j/                  |«      }|||fS )z#Input shape: Batch x Time x ChannelNr   r>   r   r   r   z$Attention weights should be of size z	, but is z!Attention mask should be of size z/Head mask for a single layer should be of size )ÚpÚtrainingz `attn_output` should be of size )Úsizer�   rš   rp   r¥   r›   rœ   rB   Úcatr”   r“   r™   r£   ro   Úbmmr?   r_   r   r€   Úsoftmaxrx   r®   r’   rž   )r"   r¦   r§   r¨   r©   rª   r«   Úis_cross_attentionr¡   Útgt_lenÚ_Úquery_statesÚ
key_statesÚvalue_statesÚ
proj_shapeÚsrc_lenÚattn_weightsÚattn_weights_reshapedÚ
attn_probsÚattn_outputs                       r$   r*   zPatchTSMixerAttention.forward?  s  € ð .°TÐ9Ðà'×,Ñ,Ó.‰ˆˆW�að —{‘{ =Ó1°D·L±LÑ@ˆñ ØÐ*Ø˜qÑ!×'Ñ'¨Ñ*Ð.>×.DÑ.DÀQÑ.GÒGð (¨Ñ*ˆJØ)¨!Ñ,ŠLÙàŸ™ T§[¡[Ð1AÓ%BÀBÈÓLˆJØŸ;™; t§{¡{Ð3CÓ'DÀbÈ#ÓN‰LØÐ'àŸ™ T§[¡[°Ó%?ÀÀSÓIˆJØŸ;™; t§{¡{°=Ó'AÀ2ÀsÓKˆLÜŸ™ N°1Ñ$5°zÐ#BÈÔJˆJÜ Ÿ9™9 n°QÑ&7¸Ð%FÈAÔN‰Lð Ÿ™ T§[¡[°Ó%?ÀÀSÓIˆJØŸ;™; t§{¡{°=Ó'AÀ2ÀsÓKˆLà�?Š?ð )¨,Ð7ˆNà˜DŸN™NÑ*¨B°·±Ð>ˆ
ØC�t—{‘{ <°¸#Ó>×CÑCÀZÐPˆØ'�Z×'Ñ'¨Ð4ˆ
Ø+�|×+Ñ+¨ZÐ8ˆà—/‘/ !Ó$ˆÜ—y‘y ¨z×/CÑ/CÀAÀqÓ/IÓJˆà×ÑÓ 3¨¯©Ñ#7¸À'Ð"JÒJÜØ6¸¸d¿n¹nÑ8LÈgÐW^Ð7_Ð6`ð aØ ×%Ñ%Ó'Ð(ð*óð ð
 Ð%Ø×"Ñ"Ó$¨¨a°¸'Ð(BÒBÜ Ø7¸¸aÀÈ'Ð8RÐ7SÐS\Ð]k×]pÑ]pÓ]rÐ\sÐtóð ð (×,Ñ,¨S°$·.±.À'È7ÓSÐVdÑdˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLä—}‘}×,Ñ,¨\¸rÐ,ÓBˆàÐ&Ø×#Ñ#Ó%¨$¯.©.Ð):Ò:Ü ØEÀtÇ~Á~ÐFWÐEXð YØ'×,Ñ,Ó.Ð/ð1óð ð +×/Ñ/°°2°q¸!Ó<¸|×?PÑ?PÐQTÐVZ×VdÑVdÐfmÐovÓ?wÑwˆLØ'×,Ñ,¨S°4·>±>Ñ-AÀ7ÈGÓTˆLáð
 %1×$5Ñ$5°c¸4¿>¹>È7ÐT[Ó$\Ð!Ø0×5Ñ5°c¸D¿N¹NÑ6JÈGÐU\Ó]‰Là$(Ð!ä—]‘]×*Ñ*¨<¸4¿<¹<ÐRV×R_ÑR_Ð*Ó`ˆ
ä—i‘i 
¨LÓ9ˆà×ÑÓ #¨¯©Ñ"6¸ÀÇÁÐ!OÒOÜØ2°C¸$¿.¹.Ñ4HÈ'ÐSW×S`ÑS`Ð3aÐ2bð cØ×$Ñ$Ó&Ð'ð)óð ð
 "×&Ñ& s¨D¯N©N¸GÀTÇ]Á]ÓSˆØ!×+Ñ+¨A¨qÓ1ˆð "×)Ñ)¨#¨w¸¿¹ÓGˆà—m‘m KÓ0ˆàÐ1°>ÐAÐAr%   )ç        FTFN)NNNNF)r+   r,   r-   r.   r/   ÚfloatÚboolr   r   r   rB   rC   r¥   r   r*   r0   r1   s   @r$   r‘   r‘     sN  ø„ ÙGð Ø ØØØ/3ñCàðCð ðCð ð	Cð
 ðCð ðCð ðCð Ð+Ñ,õCð>e˜UŸ\™\ð e°Cð e¸có eð 48Ø8<Ø15Ø26Ø"'ñvBà—|‘|ðvBð # 5§<¡<Ñ0ðvBð !  u§|¡|Ñ!4Ñ5ð	vBð
 ! §¡Ñ.ðvBð " %§,¡,Ñ/ðvBð  ðvBð 
ˆu�|‰|˜X e§l¡lÑ3°X¸eÀEÇLÁLÑ>QÑ5RÐRÑ	S÷vBr%   r‘   c                   ó.   ‡ — e Zd ZdZdefˆ fd„Zd„ Zˆ xZS )ÚPatchMixerBlockzxThis module mixes the patch dimension.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óÔ  •— t         ‰| �  «        t        |«      | _        |j                  | _        |j
                  | _        t        |j                  |j                  |¬«      | _        |j
                  r&t        |j                  |j                  ¬«      | _
        |j                  rBt        |j                  |j                  |j                  ¬«      | _        t        |«      | _        y y )Nr†   r‡   )r’   r“   rx   )r   r   rg   rl   Ú	self_attnrˆ   rs   rL   rŠ   r   r‹   r‘   r9   Úself_attn_headsrx   Úself_attn_layerÚ	norm_attnr<   s     €r$   r   zPatchMixerBlock.__init__À  s¼   ø€ Ü‰ÑÔä)¨&Ó1ˆŒ	à×)Ñ)ˆŒØ ×+Ñ+ˆŒä"Ø×*Ñ*Ø×+Ñ+Øô
ˆŒð ×ÒÜ :À6×CUÑCUÐ`f×`rÑ`rÔ sˆDÔà×ÒÜ#8Ø Ÿ.™.Ø ×0Ñ0ØŸ™ô$ˆDÔ ô
 3°6Ó:ˆD�Nð r%   c                 óÖ  — |}| j                  |«      }| j                  rR|j                  \  }}}}|j                  ||z  ||«      }| j	                  |d¬«      \  }}	}	|j                  ||||«      }|j                  dd«      }| j                  |«      }| j                  r| j                  |«      }|j                  dd«      }| j                  r| j                  |z   «      }||z   }
|
S )z’
        Args:
            hidden_state (`torch.Tensor`): Input tensor.

        Returns:
            `torch.Tensor`: Transformed tensor.
        F)r«   r>   r	   )
rl   rÅ   rp   ro   rÇ   r?   rŠ   rˆ   r‹   rÈ   )r"   rd   rŽ   Ú
batch_sizeÚn_varsrL   r9   Úhidden_state_reshapedÚx_attnrµ   r�   s              r$   r*   zPatchMixerBlock.forwardÙ  sô   € ð  ˆà—y‘y Ó.ˆà�>Š>Ø7C×7IÑ7IÑ4ˆJ˜ ¨WØ$0×$8Ñ$8¸ÀfÑ9LÈkÐ[bÓ$cÐ!à×/Ñ/Ð0EÐY^Ð/Ó_‰LˆF�A�qØ—^‘^ J°¸ÀWÓMˆFð $×-Ñ-¨a°Ó3ˆØ—x‘x Ó-ˆà�?Š?Ø×,Ñ,¨\Ó:ˆLð $×-Ñ-¨a°Ó3ˆà�>Š>ØŸ>™>¨,¸Ñ*?Ó@ˆLà˜XÑ%ˆØˆ
r%   ©r+   r,   r-   r.   r   r   r*   r0   r1   s   @r$   rÃ   rÃ   ¸  s   ø„ ñð;Ð1õ ;ö2!r%   rÃ   c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚFeatureMixerBlockz‚This module mixes the hidden feature dimension.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    r4   c                 ó  •— t         ‰| �  «        t        |«      | _        |j                  | _        t        |j                  |j                  |¬«      | _        |j                  r't        |j                  |j                  ¬«      | _	        y y r…   )
r   r   rg   rl   rˆ   rs   r9   rŠ   r   r‹   r<   s     €r$   r   zFeatureMixerBlock.__init__  sn   ø€ Ü‰ÑÔä)¨&Ó1ˆŒ	à ×+Ñ+ˆŒä"ØŸ™ØŸ™Øô
ˆŒð ×ÒÜ :À6Ç>Á>Ð\b×\jÑ\jÔ kˆDÕð r%   Úhiddenc                 ó’   — |}| j                  |«      }| j                  |«      }| j                  r| j                  |«      }||z   }|S )ú×
        Args:
            hidden (`torch.Tensor` of shape `(batch_size, num_patches, d_model)`):
                Input tensor to the layer.

        Returns:
            `torch.Tensor`: Transformed tensor.
        )rl   rŠ   rˆ   r‹   )r"   rÒ   rŽ   r�   s       r$   r*   zFeatureMixerBlock.forward  sK   € ð ˆØ—‘˜6Ó"ˆØ—‘˜&Ó!ˆà�?Š?Ø×&Ñ& vÓ.ˆFà�xÑˆØˆ
r%   rA   r1   s   @r$   rÐ   rÐ   ý  s'   ø„ ñðlÐ1õ lð ˜eŸl™l÷ r%   rÐ   c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚPatchTSMixerLayerz•
    The `PatchTSMixer` layer that does all three kinds of mixing.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    r4   c                 óÊ   •— t         ‰| �  «        t        |¬«      | _        t	        |¬«      | _        |j                  | _        |j                  dk(  rt        |¬«      | _        y y )N©r4   Úmix_channel)	r   r   rÃ   Úpatch_mixerrÐ   Úfeature_mixerÚmoderƒ   Úchannel_feature_mixerr<   s     €r$   r   zPatchTSMixerLayer.__init__4  sR   ø€ Ü‰ÑÔä*°&Ô9ˆÔÜ.°fÔ=ˆÔà—K‘KˆŒ	à�;‰;˜-Ò'Ü)MÐU[Ô)\ˆDÕ&ð (r%   rÒ   c                 óŠ   — | j                   dk(  r| j                  |«      }| j                  |«      }| j                  |«      }|S )rÔ   rÙ   )rÜ   rÝ   rÚ   rÛ   )r"   rÒ   s     r$   r*   zPatchTSMixerLayer.forward?  sE   € ð �9‰9˜Ò%Ø×/Ñ/°Ó7ˆFà×!Ñ! &Ó)ˆØ×#Ñ# FÓ+ˆØˆr%   rA   r1   s   @r$   rÖ   rÖ   *  s'   ø„ ñð	]Ð1õ 	]ð˜eŸl™l÷ r%   rÖ   c                   ó6   ‡ — e Zd ZdZdefˆ fd„Zddefd„Zˆ xZS )ÚPatchTSMixerBlockz‹The main computing framework of the `PatchTSMixer` model.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óº   •— t         ‰| �  «        |j                  }t        j                  t        |«      D �cg c]  }t        |¬«      ‘Œ c}«      | _        y c c}w ©NrØ   )r   r   Ú
num_layersr   Ú
ModuleListÚrangerÖ   Úmixers)r"   r4   rã   rµ   r#   s       €r$   r   zPatchTSMixerBlock.__init__X  sC   ø€ Ü‰ÑÔà×&Ñ&ˆ
ä—m‘mÌuÐU_ÓO`Ö$aÈ!Ô%6¸fÖ%EÒ$aÓbˆ�ùÒ$as   ¸AÚoutput_hidden_statesc                 óx   — g }|}| j                   D ]  } ||«      }|sŒ|j                  |«       Œ  |r||fS |dfS )as  
        Args:
            hidden_state (`torch.Tensor`): The input tensor.
            output_hidden_states (`bool`, *optional*, defaults to False.):
                Whether to output the hidden states as well.

        Returns:
            `torch.Tensor`: The embedding. `list`: List of all hidden states if `output_hidden_states` is set to
            `True`.
        N)ræ   Úappend)r"   rd   rç   Úall_hidden_statesÚ	embeddingÚmods         r$   r*   zPatchTSMixerBlock.forward_  sW   € ð Ðà ˆ	à—;‘;ò 	4ˆCÙ˜I›ˆIÚ#Ø!×(Ñ(¨Õ3ð	4ñ
  ØÐ/Ð/Ð/à˜d�?Ð"r%   ©F)	r+   r,   r-   r.   r   r   rÁ   r*   r0   r1   s   @r$   rà   rà   P  s#   ø„ ñðcÐ1õ cñ#¸$÷ #r%   rà   c                   ó0   ‡ — e Zd ZdZddefˆ fd„Zd„ Zˆ xZS )ÚPatchTSMixerForPredictionHeadzqPrediction Head for Forecasting

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óæ  •— t         ‰| �  «        |j                  | _        | j                  �| j                  j                  «        t	        j
                  |j                  «      | _        |€=t	        j                  |j                  |j                  z  |j                  «      | _        n-|j                  |j                  |j                  z  «      | _        t	        j                  d¬«      | _        y )Néþÿÿÿ©Ú	start_dim)r   r   Úprediction_channel_indicesÚsortr   rw   Úhead_dropoutÚdropout_layerr   rL   r9   Úprediction_lengthÚbase_forecast_blockÚget_parameter_projectionÚFlattenÚflatten)r"   r4   Údistribution_outputr#   s      €r$   r   z&PatchTSMixerForPredictionHead.__init__�  s·   ø€ Ü‰ÑÔà*0×*KÑ*KˆÔ'à×*Ñ*Ð6Ø×+Ñ+×0Ñ0Ô2äŸZ™Z¨×(;Ñ(;Ó<ˆÔØÐ&Ü')§y¡y°&×2DÑ2DÀvÇ~Á~Ñ2UÐX^×XpÑXpÓ'qˆDÕ$à':×'SÑ'SØ×"Ñ" V§^¡^Ñ3ó(ˆDÔ$ô —z‘z¨BÔ/ˆ�r%   c                 ó^  ‡ — ‰ j                  |«      }‰ j                  |«      }‰ j                  |«      }t        |t        «      rt	        d„ |D «       «      }n|j                  dd«      }‰ j                  �7t        |t        «      rt	        ˆ fd„|D «       «      }|S |d‰ j                  f   }|S )ar  

        Args:
            hidden_features (`torch.Tensor` of shape `(batch_size, num_patch, d_model)` in `flatten` mode
                or `(batch_size, n_vars, num_patch, d_model)` in `common_channel`/`mix_channel` mode.): Input hidden
                features.

        Returns:
            `torch.Tensor` of shape `(batch_size, prediction_length, nvars)`.

        c              3   ó@   K  — | ]  }|j                  d d«      –— Œ y­w)r   rñ   N)r?   )Ú.0Úzs     r$   ú	<genexpr>z8PatchTSMixerForPredictionHead.forward.<locals>.<genexpr>¤  s   è ø€ ÒC°Q˜QŸ[™[¨¨R×0ÑCùs   ‚r   rñ   c              3   ó@   •K  — | ]  }|d ‰j                   f   –— Œ y­w).N)rô   )r   r  r"   s     €r$   r  z8PatchTSMixerForPredictionHead.forward.<locals>.<genexpr>ª  s!   øè ø€ Ò [ÈQ  3¨×(GÑ(GÐ#GÕ!HÑ [ùs   ƒ.)rü   r÷   rù   Ú
isinstanceÚtupler?   rô   ©r"   Úhidden_featuresÚforecasts   `  r$   r*   z%PatchTSMixerForPredictionHead.forward“  s©   ø€ ð Ÿ,™, Ó7ˆØ×,Ñ,¨_Ó=ˆØ×+Ñ+¨OÓ<ˆÜ�h¤Ô&ÜÑC¸(ÔCÓC‰Hà×)Ñ)¨"¨bÓ1ˆHà×*Ñ*Ð6Ü˜(¤EÔ*Ü Ó [ÐRZÔ [Ó[�ð ˆð $ C¨×)HÑ)HÐ$HÑI�àˆr%   r'   rÎ   r1   s   @r$   rï   rï   y  s   ø„ ññ0Ð1õ 0ö$r%   rï   c                   ó0   ‡ — e Zd ZdZddefˆ fd„Zd„ Zˆ xZS )ÚPatchTSMixerLinearHeadz€Linear head for Classification and Regression.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 ó\  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  €|j                  }nd}|| _        |€@t        j                  |j                  |j                  z  |z  |j                  «      | _        n0|j                  |j                  |j                  z  |z  «      | _        |j                  €t        j                  d¬«      | _        nt        j                  d¬«      | _        t        j                  |j                   «      | _        y )Nr   éýÿÿÿrò   rñ   )r   r   Úhead_aggregationÚoutput_rangerL   rý   r   r   r9   r‰   Únum_targetsÚ
projectionrú   rû   rü   rw   rö   rx   )r"   r4   rý   Ú
mul_factorr#   s       €r$   r   zPatchTSMixerLinearHead.__init__¹  só   ø€ Ü‰ÑÔà &× 7Ñ 7ˆÔØ"×/Ñ/ˆÔà×"Ñ"Ð*Ø×+Ñ+‰JàˆJØ#6ˆÔ ØÐ&Ü Ÿi™iØ—‘ ×!:Ñ!:Ñ:¸ZÑGØ×"Ñ"óˆD�Oð
 2×JÑJØ—‘ ×!:Ñ!:Ñ:¸ZÑGóˆDŒOð ×"Ñ"Ð*ÜŸ:™:°Ô3ˆD�LäŸ:™:°Ô3ˆDŒLä—z‘z &×"5Ñ"5Ó6ˆ�r%   c                 ó&  — |j                  dd«      }| j                  dk(  r|d   }nM| j                  dk(  r|j                  d¬«      j                  }n!| j                  dk(  r|j	                  d¬«      }| j
                  r| j                  |«      }| j                  |«      }| j                  |«      }| j                  €Q| j                  �Et        j                  |«      | j                  d   | j                  d	   z
  z  | j                  d	   z   }|S )
ai  
        Args:
            hidden_features (`torch.Tensor` of shape `(batch_size x num_patch x d_model)` in `flatten` mode
                or `(batch_size x n_vars x num_patch x d_model)` in `common_channel`/`mix_channel` mode.): Input hidden
                features.

        Returns:
            `torch.Tensor` of shape `(batch_size x num_targets)`.
        r   rñ   Úuse_last).r   Úmax_poolr   Úavg_poolr   r   )r?   r  ÚmaxÚvaluesr]   rü   rx   r  rý   r  rB   Úsigmoid)r"   r  s     r$   r*   zPatchTSMixerLinearHead.forwardÕ  s
  € ð *×3Ñ3°B¸Ó;ˆØ× Ñ  JÒ.à-¨gÑ6‰OØ×"Ñ" jÒ0à-×1Ñ1°bÐ1Ó9×@Ñ@‰OØ×"Ñ" jÒ0à-×2Ñ2°rÐ2Ó:ˆOà�<Š<Ø"Ÿl™l¨?Ó;ˆOØŸ,™, Ó7ˆØŸ/™/¨/Ó:ˆà×$Ñ$Ð,°4×3DÑ3DÐ3Pä—‘˜oÓ.°$×2CÑ2CÀAÑ2FÈ×IZÑIZÐ[\ÑI]Ñ2]Ñ^Ðae×arÑarÐstÑauÑuð ð Ðr%   r'   rÎ   r1   s   @r$   r
  r
  ±  s   ø„ ññ7Ð1õ 7ö8 r%   r
  c                   ó"   — e Zd ZeZdZdZdZd„ Zy)ÚPatchTSMixerPreTrainedModelÚmodelÚpast_valuesFc                 ó„  — t        |t        «      rG| j                  j                  dk(  r-t        j
                  j                  |j                  dd¬«       yyt        |t        j                  t        j                  f«      rJ|j                  j                  j                  «        |j                  j                  j                  d«       yt        |t        «      r^|j                   j                  j                  j                  «        |j                   j                  j                  j                  d«       yt        |t        j"                  «      rm|j                  j                  j                  d| j                  j$                  ¬«       |j                  �%|j                  j                  j                  «        yyy)zInitialize weightsrO   r¿   gš™™™™™¹?)r]   r^   ç      ð?N)r  rE   r4   rT   r   ÚinitÚnormal_rI   rm   r8   r•   ÚdataÚzero_ÚweightÚfill_r3   r;   r   Úinit_std)r"   Úmodules     r$   Ú_init_weightsz)PatchTSMixerPreTrainedModel._init_weightsÿ  s.  € ä�fÔ<Ô=à�{‰{×3Ñ3°xÒ?Ü—‘—‘ × 3Ñ 3¸#À3�ÕGð @ä˜¤§¡¬r¯~©~Ð >Ô?Ø�K‰K×Ñ×"Ñ"Ô$Ø�M‰M×Ñ×$Ñ$ SÕ)Ü˜Ô 5Ô6Ø×Ñ×!Ñ!×&Ñ&×,Ñ,Ô.Ø×Ñ×#Ñ#×(Ñ(×.Ñ.¨sÕ3Ü˜¤§	¡	Ô*Ø�M‰M×Ñ×&Ñ&¨C°T·[±[×5IÑ5IÐ&ÔJØ�{‰{Ð&Ø—‘× Ñ ×&Ñ&Õ(ð 'ð +r%   N)	r+   r,   r-   r   Úconfig_classÚbase_model_prefixÚmain_input_nameÚsupports_gradient_checkpointingr'  © r%   r$   r  r  ø  s   „ à%€LØÐØ#€OØ&+Ð#ó)r%   r  c                   ó.   ‡ — e Zd ZdZdefˆ fd„Zd„ Zˆ xZS )ÚPatchTSMixerPretrainHeadzcPretraining head.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óÈ   •— t         ‰| �  «        t        j                  |j                  «      | _        t        j                  |j                  |j                  «      | _	        y r'   )
r   r   r   rw   rö   r÷   r   r9   Úpatch_lengthÚbase_pt_blockr<   s     €r$   r   z!PatchTSMixerPretrainHead.__init__  sB   ø€ Ü‰ÑÔäŸZ™Z¨×(;Ñ(;Ó<ˆÔÜŸY™Y v§~¡~°v×7JÑ7JÓKˆÕr%   c                 óJ   — | j                  |«      }| j                  |«      }|S )a  
        Args:
            hidden_features (`torch.Tensor` of shape `(batch_size x num_patch x d_model)` in `flatten` mode
                or `(batch_size x n_vars x num_patch x d_model)` in `common_channel`/`mix_channel` mode.): Input hidden
                features.

        Returns:
            `torch.Tensor` of shape `(batch_size x n_vars x num_patch x patch_length)`.
        )r÷   r1  r  s      r$   r*   z PatchTSMixerPretrainHead.forward  s)   € ð ×,Ñ,¨_Ó=ˆØ×%Ñ% oÓ6ˆØˆr%   rÎ   r1   s   @r$   r.  r.    s   ø„ ñðLÐ1õ Lör%   r.  r(   Ú
mask_ratioÚunmasked_channel_indicesÚchannel_consistent_maskingÚ
mask_valuec                 ó°  — |dk  s|dk\  rt        d|› d�«      ‚| j                  \  }}}}| j                  }	t        |d|z
  z  «      }
|r-t	        j
                  |d||	¬«      }|j                  d|d«      }nt	        j
                  ||||	¬«      }t	        j                  ||||	¬«      }d|dd…dd…d|
…f<   t	        j                  |d¬«      }t	        j                  |d¬«      }t	        j                  |d|¬	«      }|j                  d«      j                  ddd|«      }|�d|dd…|dd…dd…f<   | j                  |j                  «       |«      }||d
   fS )aÆ  random_masking: Mask the input considering the control variables.

    Args:
        inputs (`torch.Tensor` of shape `(batch_size, num_channels, sequence_length, num_features)`):
            The input tensor to mask.
        mask_ratio (`float`):
            Masking ratio applied to mask the input data during random pretraining. It is the number between 0 and 1.
        unmasked_channel_indices (list, *optional*):
            Indices of channels that will not be masked.
        channel_consistent_masking (bool, *optional*, defaults to `False`):
            When true, masking will be same across all channels of a timeseries. Otherwise, masking positions will vary
            across channels.
        mask_value (int, *optional*, defaults to 0):
            Define the value of masked patches for pretraining.

    Returns:
        `tuple(torch.Tensor)`: inputs_mask, masked input, same shape as input Tensor and mask tensor of shape [bs x c x
        n]
    r   r   zMask ratio z has to be between 0 and 1.©ÚdeviceNr   r   )r   Úindex©.r   )r_   rp   r9  r/   rB   ÚrandÚrepeatÚonesÚargsortÚgatherrW   Úmasked_fillrÁ   )r(   r3  r4  r5  r6  rÊ   Únum_channelsÚsequence_lengthÚnum_featuresr9  Úlen_keepÚnoiseÚmaskÚids_shuffleÚids_restoreÚinputs_masks                   r$   Úrandom_maskingrK  0  sQ  € ð4 �A‚~˜ qšÜ˜; z lÐ2MÐNÓOÐOà>D¿l¹lÑ;€J�˜o¨|Ø�]‰]€Fä�? a¨*¡nÑ5Ó6€Há!Ü—
‘
˜: q¨/À&ÔIˆØ—‘˜Q ¨aÓ0‰ô —
‘
˜: |°_ÈVÔTˆô �:‰:�j ,°ÈÔO€DØ€DŠŠAˆy�ˆyˆÑô —-‘- ¨2Ô.€KÜ—-‘- °Ô4€Kä�<‰<˜ "¨KÔ8€DØ�>‰>˜"Ó×$Ñ$ Q¨¨1¨lÓ;€DØÐ+Ø23ˆŠQÐ(ª!ªQÐ.Ñ/à×$Ñ$ T§Y¡Y£[°*Ó=€KØ˜˜V™Ð$Ð$r%   Únum_forecast_mask_patchesc                 óP  — t        |t        «      r|g}|D �cg c]  }d‘Œ }}| j                  \  }}}}	t        j                  |||| j
                  ¬«      }
g }d}t        |«      }t        ||«      D ]H  \  }}|dk  s||k\  rt        d|› d�«      ‚t        ||z  |z  «      }|j                  |||g«       ||z  }ŒJ t        |d„ ¬«      }||k  r|d   d   ||z
  z   |d   d<   n||kD  r|d	   d   ||z
  z   |d	   d<   d}|D ]  \  }}}||z   }d|
||…d
d
…| d
…f<   |}Œ t        j                  |
j                  d   «      }|
|   }
|
j                  d	«      j                  ddd|	«      }
|�d|
d
d
…|d
d
…d
d
…f<   | j                  |
j                  «       |«      }||
d   fS c c}w )a¡  Forecast masking that masks the last K patches where K is from the num_forecast_mask_patches.
    If num_forecast_mask_patches is a list, samples in the batch will be randomly masked by numbers defined in the list.

    Parameters:
        inputs (`torch.Tensor`):
            Input of shape `(bs, num_channels, num_patch, patch_length)`
        num_forecast_mask_patches (`list`):
            Number of patches to be masked at the end of each batch sample. e.g. 4 or [3, 5].
        unmasked_channel_indices (`list`, *optional*):
            Indices of channels that are not masked.
        mask_value (`int`, *optional*, defaults to 0):
            Values in the masked patches will be filled by `mask_value`.

    Returns:
        `tuple(torch.Tensor)`: inputs_mask, masked input, same shape as inputs Tensor and Mask tensor of shape `(bs,
        num_channels , num_patch)` or `(bs, tsg1, tsg2, num_channels, num_patch)`
    r   r8  r   znum_forecast_mask_patches z6 should be greater than 0 and less than total patches.c                 ó   — | d   S ©Nr>   r,  )Úxs    r$   ú<lambda>z"forecast_masking.<locals>.<lambda>—  s
   € ¨!¨A©$€ r%   )Úkeyr>   r   Nr;  )r  r/   rp   rB   rK   r9  ÚsumÚzipr_   ré   ÚsortedÚrandpermrW   r=  rA  rÁ   )r(   rL  r4  r6  rµ   Úforecast_mask_ratiosrÊ   rB  rC  rD  rG  Út_listÚtotal_lengthÚtotal_ratior0  ÚratioÚtemp_lenÚbatch1Ú	patch_lenÚbatch2ÚpermrJ  s                         r$   Úforecast_maskingra  k  s  € ô0 Ð+¬SÔ1Ø%>Ð$?Ð!Ø'@ÖA !šAÐAÐÐAà>D¿l¹lÑ;€J�˜o¨|Ü�;‰;�z <°ÈÏÉÔW€Dà€FØ€LÜÐ*Ó+€Kä"Ð#<Ð>RÓSò !Ñˆ�eØ˜1Ò °Ò ?ÜØ,¨\¨NÐ:pÐqóð ô �z EÑ)¨KÑ7Ó8ˆØ�‰�| U¨HÐ5Ô6Ø˜Ñ ‰ð!ô �F¡Ô/€Fà�jÒ Ø˜a‘y ‘| z°LÑ'@ÑAˆˆq‰	�!ŠØ	˜
Ò	"Ø˜r™
 1™¨¸
Ñ)BÑCˆˆr‰
�1‰à€FØ"(ò Ñˆ	�1�hØ˜(Ñ"ˆØ./ˆˆV�Fˆ]šA 	˜z™{Ð*Ñ+Ø‰ðô
 �>‰>˜$Ÿ*™* Q™-Ó(€DØ�‰:€Dà�>‰>˜"Ó×$Ñ$ Q¨¨1¨lÓ;€DØÐ+Ø23ˆŠQÐ(ª!ªQÐ.Ñ/à×$Ñ$ T§Y¡Y£[°*Ó=€KØ˜˜V™Ð$Ð$ùòO Bs   ˜	F#c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚPatchTSMixerPatchifyz³
    A class to patchify the time series sequence into different patches

    Returns:
        `torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`
    r4   c                 ó  •— t         ‰| �  «        |j                  | _        |j                  | _        |j
                  | _        | j                  | j                  k  r&t        d| j                  › d| j                  › d�«      ‚t        | j                  | j                  «      | j                  z
  | j
                  z  dz   | _        | j                  | j
                  | j                  dz
  z  z   }| j                  |z
  | _	        y )NzSequence length (z+) has to be greater than the patch length (ú)r   )
r   r   Úcontext_lengthrC  r0  Úpatch_strider_   r  rL   Úsequence_start)r"   r4   Únew_sequence_lengthr#   s      €r$   r   zPatchTSMixerPatchify.__init__¸  sò   ø€ Ü‰ÑÔà%×4Ñ4ˆÔØ"×/Ñ/ˆÔØ"×/Ñ/ˆÔà×Ñ 4×#4Ñ#4Ò4ÜØ# D×$8Ñ$8Ð#9Ð9dÐei×evÑevÐdwÐwxÐyóð ô
   × 4Ñ 4°d×6GÑ6GÓHÈ4×K\ÑK\Ñ\Ðae×arÑarÑrÐuvÑvˆÔØ"×/Ñ/°$×2CÑ2CÀt×GWÑGWÐZ[ÑG[Ñ2\Ñ\ÐØ"×2Ñ2Ð5HÑHˆÕr%   r  c                 ó:  — |j                   d   }|| j                  k7  rt        d|› d| j                  › d�«      ‚|dd…| j                  d…dd…f   }|j	                  d| j
                  | j                  ¬«      }|j                  dd«      j                  «       }|S )a!  
        Parameters:
            past_values (`torch.Tensor` of shape `(batch_size, sequence_length, num_channels)`, *required*):
                Input for patchification

        Returns:
            `torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`
        rñ   zInput sequence length (z%) doesn't match model configuration (r˜   N)Ú	dimensionr¯   Ústepr  )	rp   rC  r_   rh  Úunfoldr0  rg  r?   r¤   )r"   r  rC  r@   s       r$   r*   zPatchTSMixerPatchify.forwardÉ  s¨   € ð &×+Ñ+¨BÑ/ˆØ˜d×2Ñ2Ò2ÜØ)¨/Ð):Ð:_Ð`d×`tÑ`tÐ_uÐuwÐxóð ð šQ × 3Ñ 3Ñ 5²qÐ8Ñ9ˆà—‘¨°$×2CÑ2CÈ$×J[ÑJ[�Ó\ˆà×!Ñ! " bÓ)×4Ñ4Ó6ˆØˆr%   rA   r1   s   @r$   rc  rc  °  s'   ø„ ñðIÐ1õ Ið" 5§<¡<÷ r%   rc  c                   óH   ‡ — e Zd ZdZdefˆ fd„Zdej                  fd„Zˆ xZ	S )ÚPatchTSMixerMaskinga”  
    Class to perform random or forecast masking.

    Parameters:
        config (`PatchTSMixerConfig`): model config
    Returns:
        x_mask (`torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`)
            Masked patched input
        mask (`torch.Tensor` of shape `(batch_size, num_channels, num_patches)`)
            Bool tensor indicating True on masked points
    r4   c                 ó<  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        | j                  �t        | j                  «      | _        y y r'   )	r   r   Úrandom_mask_ratior5  Ú	mask_typerL  r4  r6  rU  r<   s     €r$   r   zPatchTSMixerMasking.__init__î  s„   ø€ Ü‰ÑÔØ!'×!9Ñ!9ˆÔØ*0×*KÑ*KˆÔ'Ø×)Ñ)ˆŒØ)/×)IÑ)IˆÔ&Ø(.×(GÑ(GˆÔ%Ø ×+Ñ+ˆŒØ×(Ñ(Ð4Ü,2°4×3PÑ3PÓ,QˆDÕ)ð 5r%   rb   c                 ór  — | j                   dk(  r<t        || j                  | j                  | j                  | j
                  ¬«      \  }}nY| j                   dk(  r1t        || j                  | j                  | j
                  ¬«      \  }}nt        d| j                   › d�«      ‚|j                  «       }||fS )aä  
        Parameters:
            patch_input (`torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`, *required*):
                Patch input

        Return:
            masked_input (`torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`)
                Masked patched input
            mask (`torch.Tensor` of shape `(batch_size, num_channels, num_patches)`)
                Bool tensor indicating True on masked points

        rO   )r(   r3  r4  r5  r6  r  )r(   rL  r4  r6  zInvalid mask type ú.)
rr  rK  rq  r4  r5  r6  ra  rL  r_   rÁ   )r"   rb   Úmasked_inputrG  s       r$   r*   zPatchTSMixerMasking.forwardù  s±   € ð �>‰>˜XÒ%Ü!/Ø"Ø×1Ñ1Ø)-×)FÑ)FØ+/×+JÑ+JØŸ?™?ô"ÑˆL™$ð �^‰^˜zÒ)Ü!1Ø"Ø*.×*HÑ*HØ)-×)FÑ)FØŸ?™?ô	"ÑˆL™$ô Ð1°$·.±.Ð1AÀÐCÓDÐDð �y‰y‹{ˆØ˜TÐ!Ð!r%   rA   r1   s   @r$   ro  ro  á  s'   ø„ ñ
ð	RÐ1õ 	Rð!" 5§<¡<÷ !"r%   ro  c            	       ó¬   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  deej                  ej                  ej                  f   fd„Z	ˆ xZ
S )ÚPatchTSMixerStdScalerz½
    Standardize features by calculating the mean and scaling along the first dimension, and then normalizes it by
    subtracting from the mean and dividing by the standard deviation.
    r4   c                 óè   •— t         ‰| �  «        t        |d«      r|j                  nd| _        t        |d«      r|j
                  nd| _        t        |d«      r|j                  | _        y d| _        y )NÚscaling_dimr   ÚkeepdimTÚminimum_scalegñhãˆµøä>)r   r   Úhasattrry  r   rz  r{  r<   s     €r$   r   zPatchTSMixerStdScaler.__init__$  s[   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~’~ÈˆŒÜ5<¸VÀ_Ô5U˜V×1Ñ1ˆÕÐ[_ˆÕr%   r!  Úobserved_indicatorrM   c                 óŒ  — |j                  | j                  | j                  ¬«      }|j                  d«      }||z  j                  | j                  | j                  ¬«      |z  }||z
  |z  dz  j                  | j                  | j                  ¬«      |z  }t	        j
                  || j                  z   «      }||z
  |z  ||fS )áC  
        Parameters:
            data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                input for Batch norm calculation
            observed_indicator (`torch.BoolTensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                Calculating the scale on the observed indicator.
        Returns:
            tuple of `torch.Tensor` of shapes
                (`(batch_size, sequence_length, num_input_channels)`,`(batch_size, 1, num_input_channels)`,
                `(batch_size, 1, num_input_channels)`)
        ©rz  r  r>   )rS  r   rz  Ú	clamp_minrB   Úsqrtr{  )r"   r!  r}  ÚdenominatorÚlocÚvarianceÚscales          r$   r*   zPatchTSMixerStdScaler.forward*  s¾   € ð )×,Ñ,¨T¯X©X¸t¿|¹|Ð,ÓLˆØ!×+Ñ+¨CÓ0ˆØÐ(Ñ(×-Ñ-¨d¯h©hÀÇÁÐ-ÓMÐP[Ñ[ˆà˜S‘jÐ$6Ñ6¸1Ñ<×AÑAÀ$Ç(Á(ÐTX×T`ÑT`ÐAÓaÐdoÑoˆÜ—
‘
˜8 d×&8Ñ&8Ñ8Ó9ˆØ�s‘
˜eÑ# S¨%Ð/Ð/r%   ©r+   r,   r-   r.   r   r   rB   rC   r   r*   r0   r1   s   @r$   rw  rw    sT   ø„ ñð
`Ð1õ `ð0Ø—L‘Lð0Ø6;·l±lð0à	ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8÷0r%   rw  c            	       ó¬   ‡ — e Zd ZdZdefˆ fd„Zdej                  dej                  deej                  ej                  ej                  f   fd„Z	ˆ xZ
S )ÚPatchTSMixerMeanScalerzŠ
    Computes a scaling factor as the weighted average absolute value along the first dimension, and scales the data
    accordingly.
    r4   c                 ó&  •— t         ‰| �  «        t        |d«      r|j                  nd| _        t        |d«      r|j
                  nd| _        t        |d«      r|j                  nd| _        t        |d«      r|j                  | _        y d | _        y )Nry  r   rz  Tr{  ç»½×Ùß|Û=Údefault_scale)r   r   r|  ry  r   rz  r{  rŒ  r<   s     €r$   r   zPatchTSMixerMeanScaler.__init__H  su   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~’~ÈˆŒÜ5<¸VÀ_Ô5U˜V×1Ò1Ð[`ˆÔÜ5<¸VÀ_Ô5U˜V×1Ñ1ˆÕÐ[_ˆÕr%   r!  r}  rM   c                 óÊ  — ||z  j                  «       j                  | j                  d¬«      }|j                  | j                  d¬«      }|t        j                  |d¬«      z  }| j
                  €Q|j                  d¬«      }t        j                  |j                  d«      d¬«      }t        j                  ||z  «      }n"| j
                  t        j                  |«      z  }t        j                  |dkD  ||«      }t        j                  || j                  ¬«      }||z  }	| j                  s|j                  | j                  ¬«      }|	t        j                  |«      |fS )r  Tr€  r   ©Úminr   r   )ÚabsrS  r   rB   ÚclamprŒ  ÚsqueezeÚ	ones_likeÚwherer{  rz  Ú
zeros_like)
r"   r!  r}  Úts_sumÚnum_observedr†  Ú	batch_sumÚbatch_observationsrŒ  Úscaled_datas
             r$   r*   zPatchTSMixerMeanScaler.forwardO  s.  € ð Ð+Ñ+×0Ñ0Ó2×6Ñ6°t·x±xÈÐ6ÓNˆØ)×-Ñ-¨d¯h©hÀÐ-ÓEˆàœŸ™ \°qÔ9Ñ9ˆð ×ÑÐ%ØŸ
™
 q˜
Ó)ˆIÜ!&§¡¨\×-=Ñ-=¸aÓ-@ÀaÔ!HÐÜ!ŸM™M¨)Ð6HÑ*HÓI‰Mà ×.Ñ.´·±ÀÓ1GÑGˆMô —‘˜L¨1Ñ,¨e°]ÓCˆô —‘˜E t×'9Ñ'9Ô:ˆØ˜U‘lˆà�|Š|Ø—M‘M d§h¡h�MÓ/ˆEàœE×,Ñ,¨UÓ3°UÐ:Ð:r%   r‡  r1   s   @r$   r‰  r‰  B  sT   ø„ ñð
`Ð1õ `ð&;Ø—L‘Lð&;Ø6;·l±lð&;à	ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8÷&;r%   r‰  c            
       ó¶   ‡ — e Zd ZdZdefˆ fd„Z	 ddej                  deej                     de	ej                  ej                  ej                  f   fd„Z
ˆ xZS )	ÚPatchTSMixerNOPScalerz|
    Assigns a scaling factor equal to 1 along the first dimension, and therefore applies no scaling to the input data.
    r4   c                 óª   •— t         ‰| �  «        t        |d«      r|j                  nd| _        t        |d«      r|j
                  | _        y d| _        y )Nry  r   rz  T)r   r   r|  ry  r   rz  r<   s     €r$   r   zPatchTSMixerNOPScaler.__init__~  s@   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~‘~ˆ�Èˆ�r%   r!  r}  rM   c                 óü   — t        j                  |d¬«      j                  | j                  | j                  ¬«      }t        j
                  |d¬«      j                  | j                  | j                  ¬«      }|||fS )a�  
        Parameters:
            data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                input for Batch norm calculation
        Returns:
            tuple of `torch.Tensor` of shapes
                (`(batch_size, sequence_length, num_input_channels)`,`(batch_size, 1, num_input_channels)`,
                `(batch_size, 1, num_input_channels)`)
        FrP   )r   rz  )rB   r“  r]   r   rz  r•  )r"   r!  r}  r†  r„  s        r$   r*   zPatchTSMixerNOPScaler.forwardƒ  si   € ô —‘ °EÔ:×?Ñ?ÀDÇHÁHÐVZ×VbÑVbÐ?ÓcˆÜ×Ñ˜t°5Ô9×>Ñ>À4Ç8Á8ÐUY×UaÑUaÐ>ÓbˆØ�S˜%ÐÐr%   r'   )r+   r,   r-   r.   r   r   rB   rC   r   r   r*   r0   r1   s   @r$   rœ  rœ  y  s`   ø„ ñðNÐ1õ Nð PTñ Ø—L‘Lð Ø6>¸u¿|¹|Ñ6Lð à	ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8÷ r%   rœ  c                   óh   — e Zd ZU dZdZeej                     ed<   dZ	ee
ej                        ed<   y)ÚPatchTSMixerEncoderOutputa–  
    Base class for `PatchTSMixerEncoderOutput`, with potential hidden states.

    Args:
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches, d_model)`):
            Hidden-state at the output of the last layer of the model.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer.
    NÚlast_hidden_stater¦   )r+   r,   r-   r.   r¡  r   rB   ÚFloatTensorÚ__annotations__r¦   r   r,  r%   r$   r   r   ”  s9   … ñð 6:Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ô<r%   r   c                   óŠ   ‡ — e Zd ZdZdefˆ fd„Z eee¬«      	 	 d
de	j                  dee   dee   deeef   fd	„«       Zˆ xZS )ÚPatchTSMixerEncoderz°
    Encoder for PatchTSMixer which inputs patched time-series and outputs patched embeddings.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r4   c                 óJ  •— t         ‰| �  |«       |j                  | _        t        j                  |j
                  |j                  «      | _        |j                  rt        |¬«      | _
        nd | _
        t        |¬«      | _        |j                  r| j                  «        y y râ   )r   r   Úuse_return_dictr   r   r0  r9   ÚpatcherrG   rE   Úpositional_encoderrà   Úmlp_mixer_encoderÚ	post_initr<   s     €r$   r   zPatchTSMixerEncoder.__init__­  s   ø€ Ü‰Ñ˜Ô à%×5Ñ5ˆÔä—y‘y ×!4Ñ!4°f·n±nÓEˆŒØ×)Ò)Ü&DÈFÔ&SˆDÕ#à&*ˆDÔ#Ü!2¸&Ô!AˆÔð ×ÒØ�N‰NÕð r%   ©Úoutput_typer(  r  rç   Úreturn_dictrM   c                 óð   — |�|n| j                   }| j                  |«      }| j                  �| j                  |«      }| j                  ||¬«      \  }}|st	        d„ ||fD «       «      S t        ||¬«      S )a  
        Args:
            past_values (`torch.FloatTensor` of shape `(batch_size, seq_length, num_input_channels)`):
                Context values of the time series. For a pretraining task, this denotes the input time series to
                predict the masked portion. For a forecasting task, this denotes the history/past time series values.
                Similarly, for classification or regression tasks, it denotes the appropriate context values of the
                time series.

                For univariate time series, `num_input_channels` dimension should be 1. For multivariate time series,
                it is greater than 1.

            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers.

            return_dict (`bool`, *optional*):
                Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.

        Returns:
            `torch.FloatTensor` of shape `(batch_size, n_vars, num_patches, d_model)`
        )rç   c              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  ©r   Úvs     r$   r  z.PatchTSMixerEncoder.forward.<locals>.<genexpr>å  s   è ø€ ò àô ñùó   ‚)r¡  r¦   )r§  r¨  r©  rª  r  r   )r"   r  rç   r®  Úpatchesr¡  r¦   s          r$   r*   zPatchTSMixerEncoder.forward½  s˜   € ð8 &1Ð%<‘kÀ$×BVÑBVˆð —,‘,˜{Ó+ˆð ×"Ñ"Ð.Ø×-Ñ-¨gÓ6ˆGà+/×+AÑ+AÀ'Ð`tÐ+AÓ+uÑ(Ð˜=áÜñ ð &Ø!ðôó ð ô )Ð;LÐ\iÔjÐjr%   )FN)r+   r,   r-   r.   r   r   r   r   Ú_CONFIG_FOR_DOCrB   rC   r   rÁ   r   r   r*   r0   r1   s   @r$   r¥  r¥  ¤  s~   ø„ ñðÐ1õ ñ  Ð+DÐSbÔcð 05Ø&*ñ	/kà—\‘\ð/kð ' t™nð/kð ˜d‘^ð	/kð
 
ˆuÐ/Ð/Ñ	0ò/kó dô/kr%   r¥  c                   ó  — e Zd ZU dZdZeej                     ed<   dZ	ee
ej                        ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed<   dZeej                     ed<   y)	ÚPatchTSMixerModelOutputa•  
    Base class for model's outputs, with potential hidden states.

    Args:
        last_hidden_state (`torch.FloatTensor`  of shape `(batch_size, num_channels, num_patches, d_model)`):
            Hidden-state at the output of the last layer of the model.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer.
        patch_input (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches, patch_length)`):
            Patched input data to the model.
        mask: (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches)`,*optional*):
            Bool Tensor indicating True in masked patches and False otherwise.
        loc: (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`,*optional*):
            Gives the mean of the context window per channel. Used for revin denorm outside the model, if revin
            enabled.
        scale: (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`,*optional*):
            Gives the std dev of the context window per channel. Used for revin denorm outside the model, if revin
            enabled.
    Nr¡  r¦   rb   rG  r„  r†  )r+   r,   r-   r.   r¡  r   rB   r¢  r£  r¦   r   rb   rG  r„  r†  r,  r%   r$   r·  r·  ð  s�   … ñð( 6:Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø/3€K�˜%×+Ñ+Ñ,Ó3Ø(,€Dˆ(�5×$Ñ$Ñ
%Ó,Ø'+€Cˆ�%×#Ñ#Ñ	$Ó+Ø)-€Eˆ8�E×%Ñ%Ñ&Ô-r%   r·  z3The PatchTSMixer Model for time-series forecasting.c                   ó¸   ‡ — e Zd Zddedefˆ fd„Z ee«       ee	e
¬«      	 	 	 ddej                  deej                     dee   dee   d	e	f
d
„«       «       Zˆ xZS )ÚPatchTSMixerModelr4   Ú
mask_inputc                 ó´  •— t         ‰| �  |«       |j                  | _        t        |«      | _        t        |«      | _        |du rt        |«      | _        nd | _        |j                  dk(  rt        |«      | _        n>|j                  dk(  s|j                  du rt        |«      | _        nt        |«      | _        |j                  r| j                  «        y y )NTr]   r^   )r   r   r§  r¥  Úencoderrc  Úpatchingro  Úmaskingrš   r‰  Úscalerrw  rœ  r«  )r"   r4   rº  r#   s      €r$   r   zPatchTSMixerModel.__init__  s²   ø€ Ü‰Ñ˜Ô à%×5Ñ5ˆÔÜ*¨6Ó2ˆŒÜ,¨VÓ4ˆŒà˜ÑÜ.¨vÓ6ˆD�LàˆDŒLà�>‰>˜VÒ#Ü0°Ó8ˆD�KØ�^‰^˜uÒ$¨¯©¸$Ñ(>Ü/°Ó7ˆD�Kä/°Ó7ˆDŒKð ×ÒØ�N‰NÕð r%   r¬  r  Úobserved_maskrç   r®  rM   c           	      óä  — |�|n| j                   }d}|€t        j                  |«      }| j                  ||«      \  }}}| j	                  |«      }	|	}
| j
                  �| j                  |	«      \  }
}| j                  |
||¬«      }t        |t        «      rt        |Ž }|s,t        d„ |j                  |j                  |	|||fD «       «      S t        |j                  |j                  |	|||¬«      S )a²  
        observed_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_input_channels)`, *optional*):
            Boolean mask to indicate which `past_values` were observed and which were missing. Mask values selected
            in `[0, 1]`:
                - 1 for values that are **observed**,
                - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).

        Returns:

        N©rç   r®  c              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  r±  s     r$   r  z,PatchTSMixerModel.forward.<locals>.<genexpr>V  ó   è ø€ ò 
àô ñ
ùr³  )r¡  r¦   rb   rG  r„  r†  )r§  rB   r“  r¿  r½  r¾  r¼  r  r  r   r¡  r¦   r·  )r"   r  rÀ  rç   r®  rG  Úscaled_past_valuesr„  r†  Ú	patched_xÚ	enc_inputÚencoder_outputs               r$   r*   zPatchTSMixerModel.forward*  s  € ð& &1Ð%<‘kÀ$×BVÑBVˆàˆØÐ Ü!ŸO™O¨KÓ8ˆMØ)-¯©°[À-Ó)PÑ&Ð˜C à—M‘MÐ"4Ó5ˆ	àˆ	Ø�<‰<Ð#Ø"Ÿl™l¨9Ó5‰OˆI�tð Ÿ™ØØ!5Ø#ð &ó 
ˆô �n¤eÔ,Ü6¸ÐGˆNáÜñ 
ð #×4Ñ4Ø"×0Ñ0ØØØØðô
ó 
ð 
ô 'Ø,×>Ñ>Ø(×6Ñ6Ø!ØØØô
ð 	
r%   rí   )NFN)r+   r,   r-   r   rÁ   r   r   ÚPATCHTSMIXER_INPUTS_DOCSTRINGr   r·  rµ  rB   rC   r   r*   r0   r1   s   @r$   r¹  r¹    s”   ø„ ñ
Ð1ð ¸tõ ñ. +Ð+HÓIÙÐ+BÐQ`Ôað 15Ø/4Ø&*ñ=
à—\‘\ð=
ð   §¡Ñ-ð=
ð ' t™nð	=
ð
 ˜d‘^ð=
ð 
!ò=
ó bó Jô=
r%   r¹  c                   ó¸   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   y)Ú PatchTSMixerForPreTrainingOutputa¤  
    Output type of [`PatchTSMixerForPreTrainingOutput`].

    Args:
        prediction_outputs (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, patch_length)`):
            Prediction output from the pretrain head.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer.
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, d_model)`):
            Backbone embeddings before passing through the head.
        loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
            Total loss
    NÚlossÚprediction_outputsr¡  r¦   ©r+   r,   r-   r.   rÌ  r   rB   r¢  r£  rÍ  r¡  r¦   r   r,  r%   r$   rË  rË  l  ód   … ñð )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø6:Ð˜ ×!2Ñ!2Ñ3Ó:Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ô<r%   rË  c                   ó¼   ‡ — e Zd ZdZdefˆ fd„Z ee«       ee	e
¬«      	 	 	 	 ddej                  deej                     dee   ded	ee   d
e	fd„«       «       Zˆ xZS )ÚPatchTSMixerForPretrainingz™
    `PatchTSMixer` for mask pretraining.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r4   c                 óè   •— t         ‰| �  |«       t        |d¬«      | _        t	        |¬«      | _        |j                  | _        |j                  | _        |j                  r| j                  «        y y )NT)rº  rØ   )	r   r   r¹  r  r.  ÚheadÚmasked_lossr§  r«  r<   s     €r$   r   z#PatchTSMixerForPretraining.__init__Ž  s`   ø€ Ü‰Ñ˜Ô Ü& v¸$Ô?ˆŒ
Ü,°FÔ;ˆŒ	Ø!×-Ñ-ˆÔØ%×5Ñ5ˆÔð ×ÒØ�N‰NÕð r%   r¬  r  rÀ  rç   Úreturn_lossr®  rM   c                 óØ  — |�|n| j                   }| j                  du r!t        j                  j	                  d¬«      }n t        j                  j	                  d¬«      }| j                  ||||¬«      }t        |t        «      rt        |Ž }| j                  |j                  «      }|du r |||j                  «      }	nd}	| j                  du rM|	�K|	j                  d¬«      |j                  z  j                  «       |j                  j                  «       d	z   z  }	|s*t        d
„ |	||j                  |j                  fD «       «      S t!        |	||j                  |j                  ¬«      S )a  
        observed_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_input_channels)`, *optional*):
            Boolean mask to indicate which `past_values` were observed and which were missing. Mask values selected
            in `[0, 1]`:
                - 1 for values that are **observed**,
                - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).
        return_loss (`bool`,  *optional*):
            Whether to return the loss in the `forward` call.

        Returns:

        NTÚnone©Ú	reductionr]   ©rÀ  rç   r®  r   r   r‹  c              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  r±  s     r$   r  z5PatchTSMixerForPretraining.forward.<locals>.<genexpr>Ì  ó   è ø€ ò àô ñùr³  ©rÌ  rÍ  r¡  r¦   )r§  rÔ  rB   r   ÚMSELossr  r  r  r·  rÓ  r¡  rb   r]   rG  rS  r¦   rË  )
r"   r  rÀ  rç   rÕ  r®  rÌ  Úmodel_outputÚx_hatÚloss_vals
             r$   r*   z"PatchTSMixerForPretraining.forward™  so  € ð, &1Ð%<‘kÀ$×BVÑBVˆà×Ñ˜tÑ#Ü—8‘8×#Ñ#¨fÐ#Ó5‰Dä—8‘8×#Ñ#¨fÐ#Ó5ˆDð —z‘zØØ'Ø!5Ø#ð	 "ó 
ˆô �l¤EÔ*Ü2°LÐAˆLà—	‘	˜,×8Ñ8Ó9ˆà˜$ÑÙ˜E <×#;Ñ#;Ó<‰HàˆHð ×Ñ˜tÑ#¨Ð(<Ø Ÿ™¨"˜Ó-°×0AÑ0AÑA×FÑFÓHÈL×L]ÑL]×LaÑLaÓLcÐfkÑLkÑlˆHáÜñ ð ØØ ×2Ñ2Ø ×.Ñ.ð	ôó ð ô 0ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
r%   ©NFTN)r+   r,   r-   r.   r   r   r   rÉ  r   rË  rµ  rB   rC   r   rÁ   r*   r0   r1   s   @r$   rÑ  rÑ  ‚  s§   ø„ ñ	ð	Ð1õ 	ñ +Ð+HÓIÙÐ+KÐZiÔjð 15Ø/4Ø Ø&*ñ@
à—\‘\ð@
ð   §¡Ñ-ð@
ð ' t™nð	@
ð
 ð@
ð ˜d‘^ð@
ð 
*ò@
ó kó Jô@
r%   rÑ  c                   ó  — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   dZeej                     ed<   dZeej                     ed<   y)	ÚPatchTSMixerForPredictionOutputa¸  
    Output type of [`PatchTSMixerForPredictionOutput`].

    Args:
        prediction_outputs (`torch.FloatTensor` of shape `(batch_size, prediction_length, num_input_channels)`):
            Prediction output from the forecast head.
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, d_model)`):
            Backbone embeddings before passing through the head.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
            Total loss.
        loc (`torch.FloatTensor`, *optional* of shape `(batch_size, 1, num_input_channels)`):
            Input mean
        scale (`torch.FloatTensor`, *optional* of shape `(batch_size, 1, num_input_channels)`):
            Input std dev

    NrÌ  rÍ  r¡  r¦   r„  r†  )r+   r,   r-   r.   rÌ  r   rB   r¢  r£  rÍ  r¡  r¦   r   r„  r†  r,  r%   r$   rä  rä  Þ  sŽ   … ñð& )-€Dˆ(�5×$Ñ$Ñ
%Ó,Ø6:Ð˜ ×!2Ñ!2Ñ3Ó:Ø59Ð�x × 1Ñ 1Ñ2Ó9Ø8<€M�8˜E %×"3Ñ"3Ñ4Ñ5Ó<Ø'+€Cˆ�%×#Ñ#Ñ	$Ó+Ø)-€Eˆ8�E×%Ñ%Ñ&Ô-r%   rä  c                   ó:   — e Zd ZU dZdZeej                     ed<   y)Ú"SamplePatchTSMixerPredictionOutputa9  
    Base class for time series model's predictions outputs that contains the sampled values from the chosen
    distribution.

    Args:
        sequences (`torch.FloatTensor` of shape `(batch_size, num_samples, prediction_length, number_channels)`):
            Sampled values from the chosen distribution.
    NÚ	sequences©	r+   r,   r-   r.   rç  r   rB   r¢  r£  r,  r%   r$   ræ  ræ  û  ó   … ñð .2€Iˆx˜×)Ñ)Ñ*Ô1r%   ræ  c                   ó:   — e Zd ZU dZdZeej                     ed<   y)Ú"SamplePatchTSMixerRegressionOutputa$  
    Base class for time series model's predictions outputs that contains the sampled values from the chosen
    distribution.

    Args:
        sequences (`torch.FloatTensor` of shape `(batch_size, num_samples, num_targets)`
                Sampled values from the chosen distribution.
    Nrç  rè  r,  r%   r$   rë  rë  	  ré  r%   rë  ÚinputÚtargetrM   c                 ó&   — | j                  |«       S )zc
    Computes the negative log likelihood loss from input distribution with respect to target.
    )Úlog_prob)rì  rí  s     r$   Únllrð    s   € ð �N‰N˜6Ó"Ð"Ð"r%   Úinput_tensorÚweightsc                 óP  — |�“t        j                  |dk7  | |z  t        j                  | «      «      }t        j                  |r|j	                  |¬«      n|j	                  «       d¬«      }|r|j	                  |¬«      |z  S |j	                  «       |z  S | j                  |¬«      S )aj  
    Computes the weighted average of a given tensor across a given `dim`, masking values associated with weight zero,
    meaning instead of `nan * 0 = nan` you will get `0 * 0 = 0`.

    Args:
        input_tensor (`torch.FloatTensor`):
            Input tensor, of which the average must be computed.
        weights (`torch.FloatTensor`, *optional*):
            Weights tensor, of the same shape as `input_tensor`.
        dim (`int`, *optional*):
            The dim along which to average `input_tensor`.

    Returns:
        `torch.FloatTensor`: The tensor with values averaged along the specified `dim`.
    r   r   r  rŽ  )rB   r”  r•  r‘  rS  r]   )rñ  rò  r   Úweighted_tensorÚsum_weightss        r$   Úweighted_averagerö     s›   € ð  ÐÜŸ+™+ g°¡l°LÀ7Ñ4JÌE×L\ÑL\Ð]iÓLjÓkˆÜ—k‘k¹# '§+¡+°# +Ô"6À7Ç;Á;Ã=ÐVYÔZˆÙ03�×#Ñ#¨Ð#Ó,ÐR]Ñ]Ð]¸×9LÑ9LÓ9NÐR]Ñ]Ð]à× Ñ  SÐ Ó)Ð)r%   c                   ó"  ‡ — e Zd ZdZdefˆ fd„Z ee«       ee	e
¬«      	 	 	 	 	 ddej                  deej                     deej                     dee   d	ed
ee   de	fd„«       «       Z	 ddej                  deej                     defd„Zˆ xZS )ÚPatchTSMixerForPredictionz 
    `PatchTSMixer` for forecasting application.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r4   c                 ó2  •— t         ‰| �  |«       |j                  | _        |j                  | _        |j                  | _        |j
                  | _        |j                  dk(  rd | _        nc|j                  }t        t        t        dœ}|j                  |j                  d «      }|� ||¬«      | _        nt        d|j                  › �«      ‚t        |«      | _        t        || j                  ¬«      | _        |j"                  r| j#                  «        y y )NÚmse©Ú	student_tÚnormalÚnegative_binomialr   úUnknown distribution output ©r4   rý   )r   r   rÌ  r§  rô   Únum_parallel_samplesrý   rø   r   r   r
   Úgetr_   r¹  r  rï   rÓ  r«  )r"   r4   r   Údistribution_output_mapÚoutput_classr#   s        €r$   r   z"PatchTSMixerForPrediction.__init__D  sû   ø€ Ü‰Ñ˜Ô Ø—K‘KˆŒ	Ø%×5Ñ5ˆÔØ*0×*KÑ*KˆÔ'Ø$*×$?Ñ$?ˆÔ!à�;‰;˜%ÒØ'+ˆDÕ$à×*Ñ*ˆCä+Ü&Ü%;ñ'Ð#ð
 3×6Ñ6°v×7QÑ7QÐSWÓXˆLØÐ'Ù+7¸CÔ+@�Õ(ä Ð#?À×@ZÑ@ZÐ?[Ð!\Ó]Ð]ä& vÓ.ˆŒ
Ü1ØØ $× 8Ñ 8ô
ˆŒ	ð ×ÒØ�N‰NÕð r%   r¬  r  rÀ  Úfuture_valuesrç   rÕ  r®  rM   c           	      ó‚  — | j                   dk(  rt        j                  d¬«      }n!| j                   dk(  rt        }nt	        d«      ‚|�|n| j
                  }| j                  ||||¬«      }t        |t        «      rt        |Ž }| j                  |j                  «      }	d}
| j                  �á| j                  r|| j                  j                  |	|j                  d| j                  f   |j                   d| j                  f   ¬	«      }|��|d
u �r |||d| j                  f   «      }
t#        |
«      }
nÝ|	|j                   d| j                  f   z  |j                  d| j                  f   z   }	|�¡|d
u r� ||	|d| j                  f   «      }
n„| j                  rM| j                  j                  |	|j                  |j                   ¬	«      }|�D|d
u r@ |||«      }
t#        |
«      }
n+|	|j                   z  |j                  z   }	|�|d
u r	 ||	|«      }
| j                  �7|j                  d| j                  f   }|j                   d| j                  f   }n|j                  }|j                   }|s,t        d„ |
|	|j                  |j$                  ||fD «       «      S t'        |
|	|j                  |j$                  ||¬«      S )a�  
        observed_mask (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_input_channels)`, *optional*):
            Boolean mask to indicate which `past_values` were observed and which were missing. Mask values selected
            in `[0, 1]`:
                - 1 for values that are **observed**,
                - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).
        future_values (`torch.FloatTensor` of shape `(batch_size, target_len, num_input_channels)` for forecasting,:
            `(batch_size, num_targets)` for regression, or `(batch_size,)` for classification, *optional*): Target
            values of the time series, that serve as labels for the model. The `future_values` is what the
            Transformer needs during training to learn to output, given the `past_values`. Note that, this is NOT
            required for a pretraining task.

            For a forecasting task, the shape is be `(batch_size, target_len, num_input_channels)`. Even if we want
            to forecast only specific channels by setting the indices in `prediction_channel_indices` parameter,
            pass the target data with all channels, as channel Filtering for both prediction and target will be
            manually applied before the loss computation.
        return_loss (`bool`,  *optional*):
            Whether to return the loss in the `forward` call.

        Returns:

        rú  r]   rØ  rð  ú2Invalid loss function: Allowed values: mse and nllNrÚ  .©r„  r†  Tc              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  r±  s     r$   r  z4PatchTSMixerForPrediction.forward.<locals>.<genexpr>Æ  rÄ  r³  )rÌ  rÍ  r¡  r¦   r„  r†  )rÌ  r   rÞ  rð  r_   r§  r  r  r  r·  rÓ  r¡  rô   rý   Údistributionr„  r†  rö  r¦   rä  )r"   r  rÀ  r  rç   rÕ  r®  rÌ  rß  Úy_hatrá  r
  r„  r†  s                 r$   r*   z!PatchTSMixerForPrediction.forwardd  s  € ðB �9‰9˜ÒÜ—:‘:¨Ô/‰DØ�Y‰Y˜%ÒÜ‰DäÐQÓRÐRà%0Ð%<‘kÀ$×BVÑBVˆð —z‘zØØ'Ø!5Ø#ð	 "ó 
ˆô �l¤EÔ*Ü2°LÐAˆLð —	‘	˜,×8Ñ8Ó9ˆàˆØ×*Ñ*Ð6Ø×'Ò'Ø#×7Ñ7×DÑDØØ$×(Ñ(¨¨d×.MÑ.MÐ)MÑNØ&×,Ñ,¨S°$×2QÑ2QÐ-QÑRð  Eó  �ð
 !Ñ,°ÀÒ1DÙ#Ø$Ø% c¨4×+JÑ+JÐ&JÑKó �Hô
  0°Ó9‘Hð ˜L×.Ñ.¨s°D×4SÑ4SÐ/SÑTÑTØ"×&Ñ& s¨D×,KÑ,KÐ'KÑLñMð ð !Ð,°ÀÑ1DÙ# E¨=¸¸d×>]Ñ>]Ð9]Ñ+^Ó_‘Hà×'Ò'Ø#×7Ñ7×DÑDØ˜|×/Ñ/°|×7IÑ7Ið  Eó  �ð !Ð,°ÀÑ1DÙ# L°-Ó@�HÜ/°Ó9‘Hà × 2Ñ 2Ñ2°\×5EÑ5EÑE�Ø Ð,°ÀÑ1DÙ# E¨=Ó9�Hà×*Ñ*Ð6Ø×"Ñ" 3¨×(GÑ(GÐ#GÑHˆCØ ×&Ñ& s¨D×,KÑ,KÐ'KÑL‰Eà×"Ñ"ˆCØ ×&Ñ&ˆEáÜñ 
ð ØØ ×2Ñ2Ø ×.Ñ.ØØðô
ó 
ð 
ô /ØØ$Ø*×<Ñ<Ø&×4Ñ4ØØô
ð 	
r%   c                 óF  — | j                   } | |d|d¬«      }| j                  j                  |j                  |j                  |j
                  ¬«      }t        |«      D �cg c]  }|j                  «       ‘Œ }}t        j                  |d¬«      }t        |¬«      S c c}w )aÀ  
        Generate sequences of sample predictions from a model with a probability distribution head.

        Args:
            past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                Past values of the time series that serves as context in order to predict the future.

            observed_mask (`torch.BoolTensor` of shape `(batch_size, sequence_length, num_input_channels)`, *optional*):
                Boolean mask to indicate which `past_values` were observed and which were missing. Mask values selected
                in `[0, 1]`:

                - 1 for values that are **observed**,
                - 0 for values that are **missing** (i.e. NaNs that were replaced by zeros).

        Return:
            [`SamplePatchTSMixerPredictionOutput`] where the outputs `sequences` tensor will have shape `(batch_size,
            number of samples, prediction_length, num_input_channels)`.
        NF)r  r  rÀ  rç   r  r   r   ©rç  )r  rý   r
  rÍ  r„  r†  rå   ÚsamplerB   Ústackræ  )r"   r  rÀ  r  Úoutputsr
  rµ   Úsampless           r$   Úgeneratez"PatchTSMixerForPrediction.generateÛ  s¡   € ð0  $×8Ñ8Ðñ Ø#ØØ'Ø!&ô	
ˆð ×/Ñ/×<Ñ<Ø×&Ñ&¨G¯K©K¸w¿}¹}ð =ó 
ˆô
 38Ð8LÓ2MÖN¨Q�<×&Ñ&Õ(ÐNˆÐNô —+‘+˜g¨1Ô-ˆÜ1¸GÔDÐDùò	 Os   Á"B)NNFTNr'   )r+   r,   r-   r.   r   r   r   rÉ  r   rä  rµ  rB   rC   r   rÁ   r*   ræ  r  r0   r1   s   @r$   rø  rø  8  s÷   ø„ ñ	ðÐ1õ ñ@ +Ð+HÓIÙÐ+JÐYhÔið 15Ø04Ø/4Ø Ø&*ñs
à—\‘\ðs
ð   §¡Ñ-ðs
ð   §¡Ñ-ð	s
ð
 ' t™nðs
ð ðs
ð ˜d‘^ðs
ð 
)òs
ó jó Jðs
ðp 15ñ-Eà—\‘\ð-Eð   §¡Ñ-ð-Eð 
,÷	-Er%   rø  c                   ó¸   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   y)Ú-PatchTSMixerForTimeSeriesClassificationOutputaÀ  
    Output type of [`PatchTSMixerForTimeSeriesClassificationOutput`].

    Args:
        prediction_outputs (`torch.FloatTensor` of shape `(batch_size, num_labels)`):
            Prediction output from the classfication head.
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, d_model)`):
            Backbone embeddings before passing through the head.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
            Total loss.
    NrÌ  rÍ  r¡  r¦   rÎ  r,  r%   r$   r  r    rÏ  r%   r  c                   ó¼   ‡ — e Zd ZdZdefˆ fd„Z ee«       ee	e
¬«      	 	 	 	 ddej                  deej                     dee   ded	ee   d
e	fd„«       «       Zˆ xZS )Ú'PatchTSMixerForTimeSeriesClassificationz£
    `PatchTSMixer` for classification application.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r4   c                 ó:  •— t         ‰| �  |«       t        |«      | _        t	        |¬«      | _        |j                  | _        |j                  dv r't        |j                  |j                  ¬«      | _        nd | _        |j                  r| j                  «        y y )NrØ   ©r^   r]   T©r9   rL   )r   r   r¹  r  r
  rÓ  r§  rš   ÚInjectScalerStatistics4Dr9   rL   Úinject_scaler«  r<   s     €r$   r   z0PatchTSMixerForTimeSeriesClassification.__init__-  s„   ø€ Ü‰Ñ˜Ô ä& vÓ.ˆŒ
Ü*Øô
ˆŒ	ð  &×5Ñ5ˆÔØ�>‰>Ð2Ñ2Ü 8ÀÇÁÐ]c×]oÑ]oÔ pˆDÕà $ˆDÔð ×ÒØ�N‰NÕð r%   r¬  r  Útarget_valuesrç   rÕ  r®  rM   c                 ó4  — t         j                  j                  «       }|�|n| j                  }| j	                  |||¬«      }t        |t        «      rt        |Ž }| j                  �7| j                  |j                  |j                  |j                  ¬«      |_	        | j                  |j                  «      }|�|du r
 |||«      }	nd}	|s*t        d„ |	||j                  |j                  fD «       «      S t        |	||j                  |j                  ¬«      S )á£  
        target_values (`torch.FloatTensor` of shape `(batch_size, target_len, num_input_channels)` for forecasting,
            `(batch_size, num_targets)` for regression, or `(batch_size,)` for classification, *optional*): Target
            values of the time series, that serve as labels for the model. The `target_values` is what the
            Transformer needs during training to learn to output, given the `past_values`. Note that, this is NOT
            required for a pretraining task.

            For a forecasting task, the shape is be `(batch_size, target_len, num_input_channels)`. Even if we want
            to forecast only specific channels by setting the indices in `prediction_channel_indices` parameter,
            pass the target data with all channels, as channel Filtering for both prediction and target will be
            manually applied before the loss computation.

            For a classification task, it has a shape of `(batch_size,)`.

            For a regression task, it has a shape of `(batch_size, num_targets)`.
        return_loss (`bool`, *optional*):
            Whether to return the loss in the `forward` call.

        Returns:

        NrÂ  r  Tc              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  r±  s     r$   r  zBPatchTSMixerForTimeSeriesClassification.forward.<locals>.<genexpr>|  rÜ  r³  rÝ  )rB   r   ÚCrossEntropyLossr§  r  r  r  r·  r  r¡  r„  r†  rÓ  r¦   r  )
r"   r  r  rç   rÕ  r®  rÌ  rß  r  rá  s
             r$   r*   z/PatchTSMixerForTimeSeriesClassification.forward>  s.  € ôF �x‰x×(Ñ(Ó*ˆà%0Ð%<‘kÀ$×BVÑBVˆà—z‘zØØ!5Ø#ð "ó 
ˆô
 �l¤EÔ*Ü2°LÐAˆLà×ÑÐ(Ø-1×->Ñ->Ø×.Ñ.Ø ×$Ñ$Ø"×(Ñ(ð .?ó .ˆLÔ*ð —	‘	˜,×8Ñ8Ó9ˆàÐ$¨¸Ñ)<Ù˜E =Ó1‰HàˆHáÜñ ð ØØ ×2Ñ2Ø ×.Ñ.ð	ôó ð ô =ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
r%   râ  )r+   r,   r-   r.   r   r   r   rÉ  r   r  rµ  rB   rC   r   rÁ   r*   r0   r1   s   @r$   r  r  !  s¨   ø„ ñ	ðÐ1õ ñ" +Ð+HÓIÙØAØ$ôð 15Ø/4Ø Ø&*ñH
à—\‘\ðH
ð   §¡Ñ-ðH
ð ' t™nð	H
ð
 ðH
ð ˜d‘^ðH
ð 
7òH
ó	ó Jô
H
r%   r  c                   ó¸   — e Zd ZU dZdZeej                     ed<   dZ	eej                     ed<   dZ
eej                     ed<   dZeeej                        ed<   y)ÚPatchTSMixerForRegressionOutputa°  
    Output type of [`PatchTSMixerForRegressionOutput`].

    Args:
        regression_outputs (`torch.FloatTensor` of shape `(batch_size, num_targets)`):
            Prediction output from the regression head.
        last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, d_model)`):
            Backbone embeddings before passing through the head.
        hidden_states (`tuple(torch.FloatTensor)`, *optional*):
            Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.
        loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
            Total loss.
    NrÌ  Úregression_outputsr¡  r¦   )r+   r,   r-   r.   rÌ  r   rB   r¢  r£  r#  r¡  r¦   r   r,  r%   r$   r"  r"  Ž  rÏ  r%   r"  c                   ó~   ‡ — e Zd Zd	dededefˆ fd„Zdej                  dej                  dej                  fd„Zˆ xZS )
r  r9   rL   Ú	expansionc                 ó&  •— t         ‰| �  «        t        j                  |dz   ||z  «      | _        t        j                  ||z  |«      | _        t        j                  dd|z  «      | _        t        j                  d|z  d«      | _        || _        y rO  )	r   r   r   r   Úinverse_trans_expansionÚinverse_trans_compressionÚmap_scale_expansionÚmap_scale_compressionrL   )r"   r9   rL   r%  r#   s       €r$   r   z!InjectScalerStatistics4D.__init__¥  sx   ø€ Ü‰ÑÔä')§y¡y°¸1±¸iÈ'Ñ>QÓ'RˆÔ$Ü)+¯©°9¸wÑ3FÈÓ)PˆÔ&Ü#%§9¡9¨Q°°I±Ó#>ˆÔ Ü%'§Y¡Y¨q°9©}¸aÓ%@ˆÔ"Ø&ˆÕr%   r(   r„  r†  c                 óö  — |j                  dd«      }|j                  d«      }|j                  dd| j                  d«      }|j                  dd«      }|j                  d«      }|j                  dd| j                  d«      }t	        j
                  ||gd¬«      }| j                  |«      }| j                  |«      }t	        j
                  ||gd¬«      }| j                  |«      }| j                  |«      }|S )a‰  
        Args:
            inputs (`torch.Tensor` of shape `(batch_size, num_input_channels, num_patch, d_model)`)
            loc (`torch.Tensor` of shape `(batch_size, 1, num_input_channels)`)
            scale (`torch.Tensor` of shape `(batch_size, 1, num_input_channels)`)
        Returns:
            `torch.Tensor` of shape `(batch_size, num_input_channels, num_patch, d_model)`
        r   rñ   r   r   )
r?   rW   r=  rL   rB   r°   r)  r*  r'  r(  )r"   r(   r„  r†  r]   ÚstdevÚconcat_statss          r$   r*   z InjectScalerStatistics4D.forward®  sâ   € ð �}‰}˜R Ó$ˆØ�~‰~˜bÓ!ˆØ�{‰{˜1˜a ×!1Ñ!1°1Ó5ˆà—‘  BÓ'ˆØ—‘ Ó#ˆØ—‘˜Q  4×#3Ñ#3°QÓ7ˆä—y‘y $¨ °BÔ7ˆà×/Ñ/°Ó=ˆØ×1Ñ1°,Ó?ˆä—‘˜F LÐ1°rÔ:ˆØ×-Ñ-¨fÓ5ˆØ×/Ñ/°Ó7ˆàˆr%   )r>   )	r+   r,   r-   r/   r   rB   rC   r*   r0   r1   s   @r$   r  r  ¤  sC   ø„ ñ' ð '°#ð 'À#õ 'ð˜eŸl™lð °·±ð ÀeÇlÁl÷ r%   r  c                   óà   ‡ — e Zd ZdZdefˆ fd„Z ee«       ee	e
¬«      	 	 	 	 ddej                  deej                     dee   ded	ee   d
e	fd„«       «       Zdej                  d
efd„Zˆ xZS )ÚPatchTSMixerForRegressionzŸ
    `PatchTSMixer` for regression application.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r4   c                 ó¤  •— t         ‰| �  |«       t        |«      | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        |j                  dk(  rd | _        n`t        t        t        dœ}|j                  |j
                  «      }|� ||j                  ¬«      | _        nt        d|j
                  › �«      ‚|j                  dv r't        |j                   |j"                  ¬«      | _        nd | _        t'        || j
                  ¬«      | _        |j*                  r| j+                  «        y y )Nrú  rû  r   rÿ  r  r  r   )r   r   r¹  r  rÌ  rý   r§  r  r   r   r
   r  r  r_   rš   r  r9   rL   r  r
  rÓ  r«  )r"   r4   r  r  r#   s       €r$   r   z"PatchTSMixerForRegression.__init__Ø  s$  ø€ Ü‰Ñ˜Ô ä& vÓ.ˆŒ
à—K‘KˆŒ	Ø#)×#=Ñ#=ˆÔ à%×5Ñ5ˆÔØ$*×$?Ñ$?ˆÔ!à�;‰;˜%ÒØ'+ˆDÕ$ô ,Ü&Ü%;ñ'Ð#ð
 3×6Ñ6°v×7QÑ7QÓRˆLØÐ'Ù+7¸F×<NÑ<NÔ+O�Õ(ä Ð#?À×@ZÑ@ZÐ?[Ð!\Ó]Ð]à�>‰>Ð2Ñ2Ü 8ÀÇÁÐ]c×]oÑ]oÔ pˆDÕà $ˆDÔä*ØØ $× 8Ñ 8ô
ˆŒ	ð ×ÒØ�N‰NÕð r%   r¬  r  r  rç   rÕ  r®  rM   c           	      óæ  — | j                   dk(  rt        j                  d¬«      }n!| j                   dk(  rt        }nt	        d«      ‚|�|n| j
                  }| j                  |||¬«      }t        |t        «      rt        |Ž }| j                  �7| j                  |j                  |j                  |j                  ¬«      |_        | j                  |j                  «      }|�¹|d	u rµ| j                  rŸ| j                  d
k(  r#t!        j"                  |dk  «      rt%        d«      ‚| j                  j'                  |«      }	t        |D �
cg c](  }
|
j)                  d| j*                  j,                  «      ‘Œ* c}
«      } ||	|«      }t/        |«      }n |||«      }nd}|s*t        d„ |||j                  |j0                  fD «       «      S t3        |||j                  |j0                  ¬«      S c c}
w )r  rú  r]   rØ  rð  r  NrÂ  r  Trþ  r   zDtarget_values cannot be negative for negative_binomial distribution.r   c              3   ó    K  — | ]  }|–— Œ y ­wr'   r,  r±  s     r$   r  z4PatchTSMixerForRegression.forward.<locals>.<genexpr>H  rÜ  r³  )rÌ  r#  r¡  r¦   )rÌ  r   rÞ  rð  r_   r§  r  r  r  r·  r  r¡  r„  r†  rÓ  rý   rB   ÚanyÚ	Exceptionr
  r£   r4   r  rö  r¦   r"  )r"   r  r  rç   rÕ  r®  rÌ  rß  r  r
  Úitemrá  s               r$   r*   z!PatchTSMixerForRegression.forwardÿ  sæ  € ð@ �9‰9˜ÒÜ—:‘:¨Ô/‰DØ�Y‰Y˜%ÒÜ‰DäÐQÓRÐRà%0Ð%<‘kÀ$×BVÑBVˆØ—z‘zØØ!5Ø#ð "ó 
ˆô
 �l¤EÔ*Ü2°LÐAˆLà×ÑÐ(Ø-1×->Ñ->Ø×.Ñ.Ø ×$Ñ$Ø"×(Ñ(ð .?ó .ˆLÔ*ð —	‘	˜,×8Ñ8Ó9ˆàÐ$¨¸Ñ)<Ø×'Ò'Ø×+Ñ+Ð/BÒBÄuÇyÁyÐQ^ÐabÑQbÔGcÜ#Ð$jÓkÐkØ#×7Ñ7×DÑDÀUÓK�äÐRWÖXÈ$˜tŸy™y¨¨T¯[©[×-DÑ-DÕEÒXÓY�Ù ¨mÓ<�ä+¨HÓ5‘á  }Ó5‘àˆHáÜñ ð ØØ ×2Ñ2Ø ×.Ñ.ð	ôó ð ô /ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
ùò) Ys   Å
-G.c                 ó`  — | j                   } | |dd¬«      }| j                  j                  |j                  «      }t	        |«      D �cg c]  }|j                  «       ‘Œ }}t        j                  |d¬«      j                  d|| j                  j                  «      }t        |¬«      S c c}w )a
  
        Generate sequences of sample predictions from a model with a probability distribution head.

        Args:
            past_values (`torch.FloatTensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                Past values of the time series that serves as context in order to predict the target values.

        Return:
            [`SamplePatchTSMixerRegressionOutput`] where the outputs `sequences` tensor will have shape `(batch_size,
            number of samples, num_targets)`.
        NF)r  r  rç   r   r   r   r  )r  rý   r
  r#  rå   r  rB   r  r£   r4   r  rë  )r"   r  r  r  r
  rµ   r  s          r$   r  z"PatchTSMixerForRegression.generateY  sª   € ð   $×8Ñ8Ðñ Ø#ØØ!&ô
ˆð ×/Ñ/×<Ñ<¸W×=WÑ=WÓXˆô ,1Ð1EÓ+Fö
Ø&'ˆL×ÑÕ!ð
ˆð 
ô
 —+‘+˜g¨1Ô-×2Ñ2°2Ð7KÈTÏ[É[×MdÑMdÓeˆÜ1¸GÔDÐDùò
s   Á
B+râ  )r+   r,   r-   r.   r   r   r   rÉ  r   r"  rµ  rB   rC   r   rÁ   r*   rë  r  r0   r1   s   @r$   r/  r/  Ì  sÇ   ø„ ñ	ð%Ð1õ %ñN +Ð+HÓIÙÐ+JÐYhÔið 15Ø/4Ø Ø&*ñV
à—\‘\ðV
ð   §¡Ñ-ðV
ð ' t™nð	V
ð
 ðV
ð ˜d‘^ðV
ð 
)òV
ó jó JðV
ðp#Eà—\‘\ð#Eð 
,÷#Er%   r/  )r  r¹  rÑ  rø  r  r/  )NFr   )Nr   )NN)Pr.   rY   Údataclassesr   Útypingr   r   r   rB   Útorch.nnr   Útransformers.modeling_utilsr   Útransformers.utilsr   Útime_series_utilsr
   r   r   Úutilsr   r   r   r   Úconfiguration_patchtsmixerr   Ú
get_loggerr+   Úloggerrµ  ÚPATCHTSMIXER_START_DOCSTRINGrÉ  ÚModuler   r3   rE   rg   rs   rƒ   r‘   rÃ   rÐ   rÖ   rà   rï   r
  r  r.  rC   rÀ   ÚlistrÁ   r/   rK  ra  rc  ro  rw  r‰  rœ  r   r¥  r·  r¹  rË  rÑ  rä  ræ  rë  ÚdistributionsÚDistributionrð  rö  rø  r  r  r"  r  r/  Ú__all__r,  r%   r$   ú<module>rG     s  ðñ "ã Ý !ß )Ñ )ã Ý å 7Ý *ç UÑ U÷ó õ ;ð 
ˆ×	Ñ	˜HÓ	%€à&€ð Ð ð&!Ð ô$ §¡ô ô*&˜BŸI™Iô &ô,$ R§Y¡Yô $ôN.˜BŸI™Iô .ôb�b—i‘iô ô.-¨2¯9©9ô -ôb[B˜BŸI™Iô [Bô|B�b—i‘iô BôJ*˜Ÿ	™	ô *ôZ#˜Ÿ	™	ô #ôL&#˜Ÿ	™	ô &#ôR5 B§I¡Iô 5ôpD˜RŸY™Yô DôN) /ô )ô2˜rŸy™yô ðD &*Ø',Øñ7%Ø�L‰Lð7%àð7%ð #ð7%ð !%ð	7%ð
 ó7%ð| &*Øñ	A%Ø�L‰LðA%à$ T¨3 YÑ/ðA%ð #ðA%ð ó	A%ôJ-˜2Ÿ9™9ô -ôb9"˜"Ÿ)™)ô 9"ôz 0˜BŸI™Iô  0ôH3;˜RŸY™Yô 3;ôn ˜BŸI™Iô  ð6 ô= ó =ó ð=ôIkÐ5ô IkðX ô.˜kó .ó ð.ñ: Ø9Ø óôW
Ð3ó W
ó	ðW
ðt ô= {ó =ó ð=ô*Y
Ð!<ô Y
ðx ô. kó .ó ð.ð8 ô
2¨ó 
2ó ð
2ð ô
2¨ó 
2ó ð
2ð#ˆu×"Ñ"×/Ñ/ð #¸¿¹ð #È%Ï,É,ó #ñ* 5§<¡<ð *¸(À5Ç<Á<Ñ:Pð *Ðfk×frÑfró *ô0PEÐ ;ô PEðf ô=°Kó =ó ð=ô*j
Ð.Iô j
ðZ ô= kó =ó ð=ô*%˜rŸy™yô %ôPpEÐ ;ô pEòf�r%   