Ë
    ÜÓ^j¸A ã                   ó¸  — d Z ddlZddlmZ ddl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 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jB                  e"«      Z# G d„ dejH                  «      Z% G d„ dejH                  «      Z& G d„ dejH                  «      Z' G d„ dejH                  «      Z( G d„ dejH                  «      Z) G d„ dejH                  «      Z*	 	 dzdejH                  dejV                  dejV                  dejV                  d ejV                  dz  d!e,dz  d"e,d#ee   fd$„Z- G d%„ d&ejH                  «      Z. G d'„ d(ejH                  «      Z/ G d)„ d*ejH                  «      Z0 G d+„ d,ejH                  «      Z1 G d-„ d.ejH                  «      Z2 G d/„ d0ejH                  «      Z3 G d1„ d2ejH                  «      Z4e G d3„ d4e
«      «       Z5 G d5„ d6ejH                  «      Z6	 	 	 d{d7ejV                  d8e,d9e7dz  d:e8d;e9f
d<„Z:	 	 d|d7ejV                  d=e7e9z  d9e7dz  d;e9fd>„Z; G d?„ d@ejH                  «      Z< G dA„ dBejH                  «      Z= G dC„ dDejH                  «      Z> G dE„ dFejH                  «      Z? G dG„ dHejH                  «      Z@ edI¬J«      e G dK„ dLe«      «       «       ZA G dM„ dNe5«      ZB edO¬J«      e G dP„ dQe«      «       «       ZC edR¬J«       G dS„ dTe5«      «       ZD edU¬J«      e G dV„ dWe«      «       «       ZE edX¬J«       G dY„ dZe5«      «       ZF ed[¬J«      e G d\„ d]e«      «       «       ZG ed^¬J«      e G d_„ d`e«      «       «       ZH ed^¬J«      e G da„ dbe«      «       «       ZIdcej”                  j–                  ddejV                  deejV                  fdf„ZLd}dgejV                  dhejV                  dz  deejV                  fdi„ZM G dj„ dke5«      ZN edl¬J«      e G dm„ dne«      «       «       ZO G do„ dpe5«      ZP edq¬J«      e G dr„ dse«      «       «       ZQ G dt„ duejH                  «      ZR edv¬J«       G dw„ dxe5«      «       ZSg dy¢ZTy)~zPyTorch PatchTSMixer model.é    N)ÚCallable)Ú	dataclass)ÚPreTrainedModel)ÚModelOutputé   )Úinitialization)ÚFlashAttentionKwargs)ÚALL_ATTENTION_FUNCTIONS)ÚUnpack)ÚNegativeBinomialOutputÚNormalOutputÚStudentTOutput)ÚTransformersKwargsÚauto_docstringÚcan_return_tupleÚloggingé   )ÚPatchTSMixerConfigc                   ó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      €ú�/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/patchtsmixer/modeling_patchtsmixer.pyr   z#PatchTSMixerGatedAttention.__init__/   s1   ø€ Ü‰ÑÔÜŸ)™) G¨XÓ6ˆŒÜŸJ™J¨2Ô.ˆÕó    c                 óP   — | j                  | j                  |«      «      }||z  }|S ©N)r#   r!   )r$   ÚinputsÚattn_weights      r&   Úforwardz"PatchTSMixerGatedAttention.forward4   s*   € Ø×'Ñ'¨¯©¸Ó(?Ó@ˆØ˜+Ñ%ˆØˆr'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úintr   r,   Ú__classcell__©r%   s   @r&   r   r   &   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$   r6   r%   s     €r&   r   zPatchTSMixerBatchNorm.__init__@   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.forwardD   s7   € ð ×!Ñ! ! QÓ'ˆØ—‘ Ó'ˆØ×Ñ  1Ó%Ð%r'   ©
r-   r.   r/   r0   r   r   ÚtorchÚTensorr,   r2   r3   s   @r&   r5   r5   ;   s'   ø„ ñðMÐ1õ Mð
&˜eŸl™l÷ 
&r'   r5   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
    r6   c                 óì   •— t         ‰| �  «        |j                  r| j                  |«      | _        y t        j                  t        j                  |j                  |j                  «      «      | _        y r)   )r   r   Úuse_positional_encodingÚ_init_peÚposition_encr   Ú	ParameterrD   ÚzerosÚnum_patchesr;   r>   s     €r&   r   z'PatchTSMixerPositionalEncoding.__init__V   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   rL   rD   ÚrandnrN   r;   rM   ÚarangeÚ	unsqueezeÚexpÚmathÚlogÚsinÚcosÚmeanÚstdÚ
ValueError)r6   rK   ÚpositionÚdiv_terms       r&   rJ   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)   )rK   )r$   rd   Úhidden_states      r&   r,   z&PatchTSMixerPositionalEncoding.forwardr   s   € à" T×%6Ñ%6Ñ6ˆØÐr'   )r-   r.   r/   r0   r   r   Ústaticmethodr   rL   rJ   rD   rE   r,   r2   r3   s   @r&   rG   rG   Q   sN   ø„ ñð^Ð1õ ^ð ðÐ+ð °·±ò ó ðð& 5§<¡<÷ r'   rG   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.
    r6   c                 óþ   •— t         ‰| �  «        |j                  | _        d|j                  j                  «       v rt	        |«      | _        y t        j                  |j                  |j                  ¬«      | _        y )NÚbatchr8   )
r   r   Únorm_mlpÚlowerr5   Únormr   Ú	LayerNormr;   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))`
        rk   r   r   r@   r   )rl   rm   rD   ÚreshapeÚshapern   )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'   rC   r3   s   @r&   ri   ri   x   s'   ø„ ñðJÐ1õ Jð˜eŸl™l÷ r'   ri   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_featuresr6   Ú
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`
        )r{   r   Ú
functionalÚgelurx   r|   r}   )r$   r*   s     r&   r,   zPatchTSMixerMLP.forward²   sK   € ð —‘œrŸ}™}×1Ñ1°$·(±(¸6Ó2BÓCÓDˆØ—‘˜&Ó!ˆØ—‘˜vÓ&ˆØˆr'   )r-   r.   r/   r   rD   rE   r,   r2   r3   s   @r&   ru   ru   ©   s   ø„ ô3ð˜eŸl™l÷ r'   ru   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.
    r6   c                 ó  •— t         ‰| �  «        t        |«      | _        |j                  | _        t        |j                  |j                  |¬«      | _        |j                  r't        |j                  |j                  ¬«      | _	        y y ©N©r~   r   r6   ©r   r   )
r   r   ri   rn   Ú
gated_attnru   Ú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   )rn   ÚpermuterŠ   r�   rŒ   )r$   r*   ÚresidualÚouts       r&   r,   z,PatchTSMixerChannelFeatureMixerBlock.forwardØ   ss   € ð ˆØ—‘˜6Ó"ˆà—‘  1 a¨Ó+ˆà�?Š?Ø×&Ñ& vÓ.ˆFà—‘˜&Ó!ˆà—‘  1 a¨Ó+ˆà�xÑˆØˆ
r'   rC   r3   s   @r&   r…   r…   À   s%   ø„ ñðÐ1õ ð ˜eŸl™l÷ r'   r…   ÚmoduleÚqueryÚkeyÚvalueÚattention_maskÚscalingrz   Úkwargsc                 óœ  — |€|j                  d«      dz  }t        j                  ||j                  dd«      «      |z  }|�||z   }t        j
                  j                  |d¬«      }t        j
                  j                  ||| j                  ¬«      }t        j                  ||«      }	|	j                  dd«      j                  «       }	|	|fS )Nr   ç      à¿r@   r   r   )ÚpÚtrainingr   )
ÚsizerD   ÚmatmulrA   r   r‚   Úsoftmaxrz   rœ   Ú
contiguous)
r’   r“   r”   r•   r–   r—   rz   r˜   Úattn_weightsÚattn_outputs
             r&   Úeager_attention_forwardr£   ñ   s»   € ð €Ø—*‘*˜R“. DÑ(ˆô —<‘<  s§}¡}°Q¸Ó':Ó;¸gÑE€LàÐ!Ø# nÑ4ˆä—=‘=×(Ñ(¨¸2Ð(Ó>€LÜ—=‘=×(Ñ(¨¸È6Ï?É?Ð(Ó[€Lä—,‘,˜|¨UÓ3€KØ×'Ñ'¨¨1Ó-×8Ñ8Ó:€Kà˜Ð$Ð$r'   c                   ó(  ‡ — e Zd ZdZ	 	 	 	 	 ddedededededed	edz  fˆ fd
„Z	 	 	 dde	j                  de	j                  dz  de	j                  dz  dedz  dee   dee	j                  e	j                  dz  ee	j                     dz  f   fd„Zˆ xZS )ÚPatchTSMixerAttentionz=Multi-headed attention from 'Attention Is All You Need' paperNÚ	embed_dimÚ	num_headsrz   Ú
is_decoderÚbiasÚ	is_causalr6   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`: ú).rš   )r©   )r   r   r¦   r§   rz   Úhead_dimr6   ra   r—   r¨   rª   r   r    Úk_projÚv_projÚq_projÚout_proj)	r$   r¦   r§   rz   r¨   r©   rª   r6   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'   Úhidden_statesÚkey_value_statesr–   Úoutput_attentionsr˜   rO   c                 óò  — |du}|j                   dd }g |¢d‘| j                  ‘­}| j                  |«      j                  |«      j	                  dd«      }	|r|n|}
g |
j                   dd ¢d‘| j                  ‘­}| j                  |
«      j                  |«      j	                  dd«      }| j                  |
«      j                  |«      j	                  dd«      }t        j                  | j                  j                  t        «      } || |	|||f| j                  sdn| j                  | j                  |dœ|¤Ž\  }} |j                  g |¢d‘­Ž j!                  «       }| j#                  |«      }||dfS )z#Input shape: Batch x Time x ChannelNr   r   r@   ç        )rz   r—   r´   )rr   r­   r°   ÚviewrA   r®   r¯   r
   Úget_interfacer6   Ú_attn_implementationr£   rœ   rz   r—   rq   r    r±   )r$   r²   r³   r–   r´   r˜   Úis_cross_attentionÚinput_shapeÚhidden_shapeÚquery_statesÚcurrent_statesÚkv_shapeÚ
key_statesÚvalue_statesÚattention_interfacer¢   r¡   s                    r&   r,   zPatchTSMixerAttention.forward0  sŠ  € ð .°TÐ9Ðð $×)Ñ)¨#¨2Ð.ˆà8˜Ð8 bÐ8¨$¯-©-Ñ8ˆð —{‘{ =Ó1×6Ñ6°|ÓD×NÑNÈqÐRSÓTˆá-?Ñ)À]ˆØB�^×)Ñ)¨#¨2Ð.ÐB°ÐB°D·M±MÑBˆØ—[‘[ Ó0×5Ñ5°hÓ?×IÑIÈ!ÈQÓOˆ
Ø—{‘{ >Ó2×7Ñ7¸ÓA×KÑKÈAÈqÓQˆä(?×(MÑ(MØ�K‰K×,Ñ,Ô.Eó)
Ðñ %8ØØØØØð
%
ð  $Ÿ}š}‘C°$·,±,Ø—L‘LØ/ñ
%
ð ñ
%
Ñ!ˆ�\ð *�k×)Ñ)Ð;¨;Ð;¸Ò;×FÑFÓHˆØ—m‘m KÓ0ˆà˜L¨$Ð.Ð.r'   )r¶   FTFN)NNF)r-   r.   r/   r0   r1   ÚfloatÚboolr   r   rD   rE   r   r	   Útupler,   r2   r3   s   @r&   r¥   r¥     sÿ   ø„ ÙGð Ø ØØØ,0ñCàðCð ðCð ð	Cð
 ðCð ðCð ðCð # TÑ)õCðD 15Ø.2Ø).ñ0/à—|‘|ð0/ð  Ÿ,™,¨Ñ-ð0/ð Ÿ™ tÑ+ð	0/ð
   $™;ð0/ð Ð-Ñ.ð0/ð 
ˆu�|‰|˜UŸ\™\¨DÑ0°%¸¿¹Ñ2EÈÑ2LÐLÑ	M÷0/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.
    r6   c                 óÖ  •— t         ‰| �  «        t        |«      | _        |j                  | _        |j
                  | _        t        |j                  |j                  |¬«      | _        |j
                  r&t        |j                  |j                  ¬«      | _
        |j                  rCt        |j                  |j                  |j                  |¬«      | _        t        |«      | _        y y )Nrˆ   r‰   )r¦   r§   rz   r6   )r   r   ri   rn   Ú	self_attnrŠ   ru   rN   rŒ   r   r�   r¥   r;   Úself_attn_headsrz   Úself_attn_layerÚ	norm_attnr>   s     €r&   r   zPatchMixerBlock.__init__k  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   )
rn   rÉ   rr   rq   rË   rA   rŒ   rŠ   r�   rÌ   )r$   rf   r�   Ú
batch_sizeÚn_varsrN   r;   Úhidden_state_reshapedÚx_attnÚ_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/   r0   r   r   r,   r2   r3   s   @r&   rÇ   rÇ   c  s   ø„ ñð;Ð1õ ;ö4!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.

    r6   c                 ó  •— t         ‰| �  «        t        |«      | _        |j                  | _        t        |j                  |j                  |¬«      | _        |j                  r't        |j                  |j                  ¬«      | _	        y y r‡   )
r   r   ri   rn   rŠ   ru   r;   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.
        )rn   rŒ   rŠ   r�   )r$   r×   r�   r‘   s       r&   r,   zFeatureMixerBlock.forwardÂ  sK   € ð ˆØ—‘˜6Ó"ˆØ—‘˜&Ó!ˆà�?Š?Ø×&Ñ& vÓ.ˆFà�xÑˆØˆ
r'   rC   r3   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.

    r6   c                 óÊ   •— t         ‰| �  «        t        |¬«      | _        t	        |¬«      | _        |j                  | _        |j                  dk(  rt        |¬«      | _        y y )N©r6   Úmix_channel)	r   r   rÇ   Úpatch_mixerrÕ   Úfeature_mixerÚmoder…   Úchannel_feature_mixerr>   s     €r&   r   zPatchTSMixerLayer.__init__à  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'   rC   r3   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.
    r6   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$   r6   rè   rÒ   r%   s       €r&   r   zPatchTSMixerBlock.__init__  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$   rf   rì   Úall_hidden_statesÚ	embeddingÚmods         r&   r,   zPatchTSMixerBlock.forward  sW   € ð Ðà ˆ	à—;‘;ò 	4ˆCÙ˜I›ˆIÚ#Ø!×(Ñ(¨Õ3ð	4ñ
  ØÐ/Ð/Ð/à˜d�?Ð"r'   ©F)	r-   r.   r/   r0   r   r   rÄ   r,   r2   r3   s   @r&   rå   rå   ü  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.
    r6   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   ry   Úhead_dropoutÚdropout_layerr    rN   r;   Úprediction_lengthÚbase_forecast_blockÚget_parameter_projectionÚFlattenÚflatten)r$   r6   Ú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)rA   )Ú.0Úzs     r&   ú	<genexpr>z8PatchTSMixerForPredictionHead.forward.<locals>.<genexpr>P  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>V  s!   øè ø€ Ò [ÈQ  3¨×(GÑ(GÐ#GÕ!HÑ [ùs   ƒ.)r  rü   rþ   Ú
isinstancerÅ   rA   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Ó   r3   s   @r&   rô   rô   %  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.
    r6   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_rangerN   r  r   r    r;   r‹   Únum_targetsÚ
projectionrÿ   r   r  ry   rû   rz   )r$   r6   r  Ú
mul_factorr%   s       €r&   r   zPatchTSMixerLinearHead.__init__e  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   )rA   r  ÚmaxÚvaluesr_   r  rz   r  r  r  rD   Ú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Ó   r3   s   @r&   r  r  ]  s   ø„ ññ7Ð1õ 7ö8 r'   r  c                   ób   ‡ — e Zd ZU eed<   dZdZdZdZ e	j                  «       ˆ fd„«       Zˆ xZS )ÚPatchTSMixerPreTrainedModelr6   ÚmodelÚpast_values)ÚtimeFc                 ó„  •— t         ‰| �  |«       t        |t        «      r=| j                  j
                  dk(  r#t        j                  |j                  dd¬«       yyt        |t        «      rSt        j                  |j                  j                  «       t        j                  |j                  j                  «       yy)zInitialize weightsrQ   r¶   gš™™™™™¹?)r_   r`   N)r   Ú_init_weightsr	  rG   r6   rV   ÚinitÚnormal_rK   r5   Úzeros_r=   r©   Úones_Úweight)r$   r’   r%   s     €r&   r#  z)PatchTSMixerPreTrainedModel._init_weights­  s‹   ø€ ô 	‰Ñ˜fÔ%Ü�fÔ<Ô=à�{‰{×3Ñ3°xÒ?Ü—‘˜V×0Ñ0°sÀÖDð @ä˜Ô 5Ô6Ü�K‰K˜×(Ñ(×-Ñ-Ô.Ü�J‰J�v×'Ñ'×.Ñ.Õ/ð 7r'   )r-   r.   r/   r   Ú__annotations__Úbase_model_prefixÚmain_input_nameÚinput_modalitiesÚsupports_gradient_checkpointingrD   Úno_gradr#  r2   r3   s   @r&   r  r  ¤  s<   ø… ð ÓØÐØ#€OØ ÐØ&+Ð#à€U‡]�]ƒ_ó	0ó ô	0r'   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.
    r6   c                 óÈ   •— t         ‰| �  «        t        j                  |j                  «      | _        t        j                  |j                  |j                  «      | _	        y r)   )
r   r   r   ry   rû   rü   r    r;   Ú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ü   r3  r
  s      r&   r,   z PatchTSMixerPretrainHead.forwardÈ  s)   € ð ×,Ñ,¨_Ó=ˆØ×%Ñ% oÓ6ˆØˆr'   rÓ   r3   s   @r&   r0  r0  º  s   ø„ ñðLÐ1õ Lör'   r0  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   )ra   rr   r;  r1   rD   ÚrandÚrepeatÚonesÚargsortÚgatherrY   Úmasked_fillrÄ   )r*   r5  r6  r7  r8  rÎ   Únum_channelsÚsequence_lengthÚnum_featuresr;  Úlen_keepÚnoiseÚmaskÚids_shuffleÚids_restoreÚinputs_masks                   r&   Úrandom_maskingrM  Ù  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   r:  r   znum_forecast_mask_patches z6 should be greater than 0 and less than total patches.c                 ó   — | d   S ©Nr@   © )Úxs    r&   ú<lambda>z"forecast_masking.<locals>.<lambda>@  s
   € ¨!¨A©$€ r'   )r”   r@   r   Nr=  )r	  r1   rr   rD   rM   r;  ÚsumÚzipra   rî   ÚsortedÚrandpermrY   r?  rC  rÄ   )r*   rN  r6  r8  rÒ   Úforecast_mask_ratiosrÎ   rD  rE  rF  rI  Út_listÚtotal_lengthÚtotal_ratior2  ÚratioÚtemp_lenÚbatch1Ú	patch_lenÚbatch2ÚpermrL  s                         r&   Úforecast_maskingrc    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)`
    r6   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_lengthrE  r2  Úpatch_stridera   r  rN   Úsequence_start)r$   r6   Únew_sequence_lengthr%   s      €r&   r   zPatchTSMixerPatchify.__init__a  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  )	rr   rE  ra   rj  Úunfoldr2  ri  rA   r    )r$   r   rE  rB   s       r&   r,   zPatchTSMixerPatchify.forwardr  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'   rC   r3   s   @r&   re  re  Y  s'   ø„ ñðIÐ1õ Ið" 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 )Ú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
    r6   c                 ó<  •— t         ‰| �  «        |j                  | _        |j                  | _        |j                  | _        |j
                  | _        |j                  | _        |j                  | _        | j                  �t        | j                  «      | _        y y r)   )	r   r   Úrandom_mask_ratior7  Ú	mask_typerN  r6  r8  rW  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'   rd   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

        rQ   )r*   r5  r6  r7  r8  r  )r*   rN  r6  r8  zInvalid mask type ú.)
rt  rM  rs  r6  r7  r8  rc  rN  ra   rÄ   )r$   rd   Úmasked_inputrI  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'   rC   r3   s   @r&   rq  rq  Š  s'   ø„ ñ
ð	RÐ1õ 	Rð!" 5§<¡<÷ !"r'   rq  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.
    r6   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   Úhasattrr{  r   r|  r}  r>   s     €r&   r   zPatchTSMixerStdScaler.__init__Í  s[   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~’~ÈˆŒÜ5<¸VÀ_Ô5U˜V×1Ñ1ˆÕÐ[_ˆÕr'   ÚdataÚobserved_indicatorrO   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)`)
        ©r|  ç      ð?r@   )rU  r   r|  Ú	clamp_minrD   Ú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/   r0   r   r   rD   rE   rÅ   r,   r2   r3   s   @r&   ry  ry  Ç  sT   ø„ ñð
`Ð1õ `ð0Ø—L‘Lð0Ø6;·l±lð0à	ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8÷0r'   ry  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.
    r6   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 )Nr{  r   r|  Tr}  ç»½×Ùß|Û=Údefault_scale)r   r   r~  r{  r   r|  r}  r�  r>   s     €r&   r   zPatchTSMixerMeanScaler.__init__ñ  su   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~’~ÈˆŒÜ5<¸VÀ_Ô5U˜V×1Ò1Ð[`ˆÔÜ5<¸VÀ_Ô5U˜V×1Ñ1ˆÕÐ[_ˆÕr'   r  r€  rO   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   )ÚabsrU  r   rD   Úclampr�  ÚsqueezeÚ	ones_likeÚwherer}  r|  Ú
zeros_like)
r$   r  r€  Úts_sumÚnum_observedrŠ  Ú	batch_sumÚbatch_observationsr�  Úscaled_datas
             r&   r,   zPatchTSMixerMeanScaler.forwardø  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‹  r3   s   @r&   r�  r�  ë  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j                  dz  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.
    r6   c                 óª   •— t         ‰| �  «        t        |d«      r|j                  nd| _        t        |d«      r|j
                  | _        y d| _        y )Nr{  r   r|  T)r   r   r~  r{  r   r|  r>   s     €r&   r   zPatchTSMixerNOPScaler.__init__'  s@   ø€ Ü‰ÑÔÜ)0°¸Ô)G�6×%Ò%ÈQˆŒÜ)0°¸Ô)C�v—~‘~ˆ�Èˆ�r'   Nr  r€  rO   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)`)
        FrR   )r   r|  )rD   r—  r_   r   r|  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‹  r3   s   @r&   r   r   "  s`   ø„ ñðNÐ1õ Nð MQñ Ø—L‘Lð Ø6;·l±lÀTÑ6Ið à	ˆu�|‰|˜UŸ\™\¨5¯<©<Ð7Ñ	8÷ r'   r   zS
    Base class for `PatchTSMixerEncoderOutput`, with potential hidden states.
    )Úcustom_introc                   óh   — e Zd ZU dZdZej                  dz  ed<   dZe	ej                     dz  ed<   y)ÚPatchTSMixerEncoderOutputa-  
    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/   r0   r¦  rD   ÚFloatTensorr)  r²   rÅ   rR  r'   r&   r¥  r¥  =  s9   … ñð 37Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ô9r'   r¥  c            	       ón   ‡ — e Zd ZdZdefˆ fd„Zee	 d	dej                  de
dz  defd„«       «       Zˆ xZS )
ÚPatchTSMixerEncoderz°
    Encoder for PatchTSMixer which inputs patched time-series and outputs patched embeddings.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.
    r6   c                 ó  •— t         ‰| �  |«       t        j                  |j                  |j
                  «      | _        |j                  rt        |¬«      | _	        nd | _	        t        |¬«      | _        | j                  «        y rç   )r   r   r   r    r2  r;   ÚpatcherrI   rG   Úpositional_encoderrå   Úmlp_mixer_encoderÚ	post_initr>   s     €r&   r   zPatchTSMixerEncoder.__init__X  sc   ø€ Ü‰Ñ˜Ô ä—y‘y ×!4Ñ!4°f·n±nÓEˆŒØ×)Ò)Ü&DÈFÔ&SˆDÕ#à&*ˆDÔ#Ü!2¸&Ô!AˆÔð 	�‰Õr'   Nr   rì   rO   c                 óØ   — |�|n| j                   j                  }| j                  |«      }| j                  �| j                  |«      }| j	                  ||¬«      \  }}t        ||¬«      S )aÑ  
        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.

        Returns:
            `torch.FloatTensor` of shape `(batch_size, n_vars, num_patches, d_model)`
        ©rì   )r¦  r²   )r6   rì   r«  r¬  r­  r¥  )r$   r   rì   r˜   Úpatchesr¦  r²   s          r&   r,   zPatchTSMixerEncoder.forwarde  sw   € ð. %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð
 —,‘,˜{Ó+ˆð ×"Ñ"Ð.Ø×-Ñ-¨gÓ6ˆGà+/×+AÑ+AÀ'Ð`tÐ+AÓ+uÑ(Ð˜=ä(Ð;LÐ\iÔjÐjr'   r)   )r-   r.   r/   r0   r   r   r   r   rD   rE   rÄ   r¥  r,   r2   r3   s   @r&   r©  r©  O  s_   ø„ ñðÐ1õ ð Øð -1ñ!kà—\‘\ð!kð # T™kð!kð
 
#ò!kó ó ô!kr'   r©  zG
    Base class for model's outputs, with potential hidden states.
    c                   ó  — e Zd ZU dZdZej                  dz  ed<   dZe	ej                     dz  ed<   dZ
ej                  dz  ed<   dZej                  dz  ed<   dZej                  dz  ed<   dZej                  dz  ed<   y)	ÚPatchTSMixerModelOutputa  
    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²   rd   rI  rˆ  rŠ  )r-   r.   r/   r0   r¦  rD   r§  r)  r²   rÅ   rd   rI  rˆ  rŠ  rR  r'   r&   r³  r³  ‹  s�   … ñð" 37Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø,0€K�×"Ñ" TÑ)Ó0Ø%)€Dˆ%×
Ñ
˜dÑ
"Ó)Ø$(€Cˆ×	Ñ	˜TÑ	!Ó(Ø&*€Eˆ5×Ñ˜tÑ#Ô*r'   r³  z=
    The PatchTSMixer Model for time-series forecasting.
    c                   ó�   ‡ — e Zd Zd
dedefˆ fd„Zee	 	 ddej                  dej                  dz  dedz  de
fd	„«       «       Zˆ xZS )ÚPatchTSMixerModelr6   Ú
mask_inputc                 óx  •— t         ‰| �  |«       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                  «        y)z•
        mask_input (bool, *optional*, defaults to `False`):
            Whether to mask the input using the [`PatchTSMixerMasking`] module.
        TNr_   r`   )r   r   r©  Úencoderre  Úpatchingrq  Úmaskingr—   r�  Úscalerry  r   r®  )r$   r6   r¶  r%   s      €r&   r   zPatchTSMixerModel.__init__±  s˜   ø€ ô
 	‰Ñ˜Ô ä*¨6Ó2ˆŒÜ,¨VÓ4ˆŒà˜ÑÜ.¨vÓ6ˆD�LàˆDŒLà�>‰>˜VÒ#Ü0°Ó8ˆD�KØ�^‰^˜uÒ$¨¯©¸$Ñ(>Ü/°Ó7ˆD�Kä/°Ó7ˆDŒKð 	�‰Õr'   Nr   Úobserved_maskrì   rO   c                 óš  — |�|n| j                   j                  }d}|€t        j                  |«      }| j	                  ||«      \  }}}| j                  |«      }	|	}
| j                  �| j                  |	«      \  }
}| j                  |
|¬«      }t        |t        «      rt        |Ž }t        |j                  |j                  |	|||¬«      S )aë  
        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.
        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).
        Nr°  )r¦  r²   rd   rI  rˆ  rŠ  )r6   rì   rD   r—  r»  r¹  rº  r¸  r	  rÅ   r¥  r³  r¦  r²   )r$   r   r¼  rì   r˜   rI  Úscaled_past_valuesrˆ  rŠ  Ú	patched_xÚ	enc_inputÚencoder_outputs               r&   r,   zPatchTSMixerModel.forwardÊ  sÝ   € ð0 %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð ˆØÐ Ü!ŸO™O¨KÓ8ˆMØ)-¯©°[À-Ó)PÑ&Ð˜C à—M‘MÐ"4Ó5ˆ	àˆ	Ø�<‰<Ð#Ø"Ÿl™l¨9Ó5‰OˆI�tð Ÿ™ØØ!5ð &ó 
ˆô
 �n¤eÔ,Ü6¸ÐGˆNä&Ø,×>Ñ>Ø(×6Ñ6Ø!ØØØô
ð 	
r'   rò   ©NN)r-   r.   r/   r   rÄ   r   r   r   rD   rE   r³  r,   r2   r3   s   @r&   rµ  rµ  «  sr   ø„ ñÐ1ð ¸tõ ð2 Øð .2Ø,0ñ	5
à—\‘\ð5
ð —|‘| dÑ*ð5
ð # T™kð	5
ð 
!ò5
ó ó ô5
r'   rµ  z>
    Output type of [`PatchTSMixerForPreTrainingOutput`].
    c                   ó¸   — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   y)Ú PatchTSMixerForPreTrainingOutputa@  
    loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
        Total loss
    prediction_outputs (`torch.FloatTensor` of shape `(batch_size, num_input_channels, num_patches, patch_length)`):
        Prediction output from the pretrain 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.
    NÚlossÚprediction_outputsr¦  r²   ©r-   r.   r/   r0   rÅ  rD   r§  r)  rÆ  r¦  r²   rÅ   rR  r'   r&   rÄ  rÄ    ód   … ñ	ð &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø37Ð˜×)Ñ)¨DÑ0Ó7Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ô9r'   rÄ  z.
    `PatchTSMixer` for mask pretraining.
    c                   ó�   ‡ — e Zd Zdefˆ fd„Zee	 	 	 d
dej                  dej                  dz  de	dz  de	de
f
d	„«       «       Zˆ xZS )ÚPatchTSMixerForPretrainingr6   c                 ó¬   •— t         ‰| �  |«       t        |d¬«      | _        t	        |¬«      | _        |j                  | _        | j                  «        y )NT)r¶  rÝ   )r   r   rµ  r  r0  ÚheadÚmasked_lossr®  r>   s     €r&   r   z#PatchTSMixerForPretraining.__init__"  sD   ø€ Ü‰Ñ˜Ô Ü& v¸$Ô?ˆŒ
Ü,°FÔ;ˆŒ	Ø!×-Ñ-ˆÔð 	�‰Õr'   Nr   r¼  rì   Úreturn_lossrO   c                 ó’  — |�|n| j                   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  }	t!        |	||j                  |j"                  ¬
«      S )aT  
        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.
        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.
        NTÚnone©Ú	reductionr_   ©r¼  rì   r   r   r�  ©rÅ  rÆ  r¦  r²   )r6   rì   rÍ  rD   r   ÚMSELossr  r	  rÅ   r³  rÌ  r¦  rd   r_   rI  rU  rÄ  r²   )
r$   r   r¼  rì   rÎ  r˜   rÅ  Úmodel_outputÚx_hatÚloss_vals
             r&   r,   z"PatchTSMixerForPretraining.forward+  s;  € ð6 %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð ×Ñ˜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ä/ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
r'   ©NNT)r-   r.   r/   r   r   r   r   rD   rE   rÄ   rÄ  r,   r2   r3   s   @r&   rÊ  rÊ    sx   ø„ ðÐ1õ ð Øð .2Ø,0Ø ñ:
à—\‘\ð:
ð —|‘| dÑ*ð:
ð # T™kð	:
ð
 ð:
ð 
*ò:
ó ó ô:
r'   rÊ  z=
    Output type of [`PatchTSMixerForPredictionOutput`].
    c                   ó  — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   dZej                  dz  ed<   dZej                  dz  ed<   y)	ÚPatchTSMixerForPredictionOutputaD  
    loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
        Total loss.
    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.
    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/   r0   rÅ  rD   r§  r)  rÆ  r¦  r²   rÅ   rˆ  rŠ  rR  r'   r&   rÛ  rÛ  j  sŽ   … ñð &*€Dˆ%×
Ñ
˜dÑ
"Ó)Ø37Ð˜×)Ñ)¨DÑ0Ó7Ø26Ð�u×(Ñ(¨4Ñ/Ó6Ø59€M�5˜×*Ñ*Ñ+¨dÑ2Ó9Ø$(€Cˆ×	Ñ	˜TÑ	!Ó(Ø&*€Eˆ5×Ñ˜tÑ#Ô*r'   rÛ  zƒ
    Base class for time series model's predictions outputs that contains the sampled values from the chosen
    distribution.
    c                   ó:   — e Zd ZU dZdZej                  dz  ed<   y)Ú"SamplePatchTSMixerPredictionOutputú¨
    sequences (`torch.FloatTensor` of shape `(batch_size, num_samples, prediction_length, number_channels)`):
        Sampled values from the chosen distribution.
    NÚ	sequences©r-   r.   r/   r0   rß  rD   r§  r)  rR  r'   r&   rÝ  rÝ  ˆ  ó   … ñð
 +/€Iˆu× Ñ  4Ñ'Ô.r'   rÝ  c                   ó:   — e Zd ZU dZdZej                  dz  ed<   y)Ú"SamplePatchTSMixerRegressionOutputrÞ  Nrß  rà  rR  r'   r&   rã  rã  ˜  rá  r'   rã  ÚinputÚtargetrO   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’  )rD   r˜  r™  r•  rU  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	 	 	 	 ddej                  dej                  dz  dej                  dz  de
dz  d	e
d
efd„«       «       Z ej                  «       	 ddej                  dej                  dz  d
efd„«       Zˆ xZS )ÚPatchTSMixerForPredictionz 
    `PatchTSMixer` for forecasting application.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r6   c                 óô  •— t         ‰| �  |«       |j                  | _        |j                  | _        |j                  | _        |j                  dk(  rd | _        nb|j                  }t        t        t        dœ}|j                  |j
                  «      }|� ||¬«      | _        nt        d|j
                  › �«      ‚t        |«      | _        t        || j
                  ¬«      | _        | j!                  «        y )NÚmse©Ú	student_tÚnormalÚnegative_binomialr   úUnknown distribution output ©r6   r  )r   r   rÅ  rù   Únum_parallel_samplesr  rý   r   r   r   Úgetra   rµ  r  rô   rÌ  r®  )r$   r6   r   Údistribution_output_mapÚoutput_classr%   s        €r&   r   z"PatchTSMixerForPrediction.__init__Õ  sÜ   ø€ Ü‰Ñ˜Ô Ø—K‘KˆŒ	Ø*0×*KÑ*KˆÔ'Ø$*×$?Ñ$?ˆÔ!à�;‰;˜%ÒØ'+ˆDÕ$à×*Ñ*ˆCä+Ü&Ü%;ñ'Ð#ð
 3×6Ñ6°v×7QÑ7QÓRˆLØÐ'Ù+7¸CÔ+@�Õ(ä Ð#?À×@ZÑ@ZÐ?[Ð!\Ó]Ð]ä& vÓ.ˆŒ
Ü1ØØ $× 8Ñ 8ô
ˆŒ	ð 	�‰Õr'   Nr   r¼  Úfuture_valuesrì   rÎ  rO   c                 ó8  — | j                   dk(  rt        j                  d¬«      }n!| j                   dk(  rt        }nt	        d«      ‚|�|n| j
                  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"                  }t'        |
|	|j                  |j(                  ||¬«      S )aÖ  
        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.
        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.
        rò  r_   rÑ  rè  ú2Invalid loss function: Allowed values: mse and nllNrÓ  .©rˆ  rŠ  T)rÅ  rÆ  r¦  r²   rˆ  rŠ  )rÅ  r   rÕ  rè  ra   r6   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.forwardó  sË  € ðJ �9‰9˜ÒÜ—:‘:¨Ô/‰DØ�Y‰Y˜%ÒÜ‰DäÐQÓRÐRð %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð
 —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ä.ØØ$Ø*×<Ñ<Ø&×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ê   ÚsamplerD   ÚstackrÝ  )r$   r   r¼  rù  Úoutputsr  rÒ   Úsampless           r&   Úgeneratez"PatchTSMixerForPrediction.generateb  s¡   € ð2  $×8Ñ8Ðñ Ø#ØØ'Ø!&ô	
ˆð ×/Ñ/×<Ñ<Ø×&Ñ&¨G¯K©K¸w¿}¹}ð =ó 
ˆô
 38Ð8LÓ2MÖN¨Q�<×&Ñ&Õ(ÐNˆÐNô —+‘+˜g¨1Ô-ˆÜ1¸GÔDÐDùò	 Os   Á"B)NNNTr)   )r-   r.   r/   r0   r   r   r   r   rD   rE   rÄ   rÛ  r,   r.  rÝ  r	  r2   r3   s   @r&   rð  rð  É  së   ø„ ñ	ðÐ1õ ð< Øð .2Ø-1Ø,0Ø ñk
à—\‘\ðk
ð —|‘| dÑ*ðk
ð —|‘| dÑ*ð	k
ð
 # T™kðk
ð ðk
ð 
)òk
ó ó ðk
ðZ €U‡]�]ƒ_ð .2ñ-Eà—\‘\ð-Eð —|‘| dÑ*ð-Eð 
,ò	-Eó ô-Er'   rð  zK
    Output type of [`PatchTSMixerForTimeSeriesClassificationOutput`].
    c                   ó¸   — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   y)Ú-PatchTSMixerForTimeSeriesClassificationOutputaP  
    loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
        Total loss.
    prediction_outputs (`torch.FloatTensor` of shape `(batch_size, num_labels)`):
        Prediction output from the classification 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.
    NrÅ  rÆ  r¦  r²   rÇ  rR  r'   r&   r  r  “  rÈ  r'   r  c                   ó”   ‡ — e Zd ZdZdefˆ fd„Zee	 	 	 ddej                  dej                  dz  de
dz  de
d	ef
d
„«       «       Zˆ xZS )Ú'PatchTSMixerForTimeSeriesClassificationz£
    `PatchTSMixer` for classification application.

    Args:
        config (`PatchTSMixerConfig`):
            Configuration.

    Returns:
        `None`.
    r6   c                 óþ   •— t         ‰| �  |«       t        |«      | _        t	        |¬«      | _        |j                  dv r't        |j                  |j                  ¬«      | _
        nd | _
        | j                  «        y )NrÝ   ©r`   r_   T©r;   rN   )r   r   rµ  r  r  rÌ  r—   ÚInjectScalerStatistics4Dr;   rN   Úinject_scaler®  r>   s     €r&   r   z0PatchTSMixerForTimeSeriesClassification.__init__·  sh   ø€ Ü‰Ñ˜Ô ä& vÓ.ˆŒ
Ü*Øô
ˆŒ	ð �>‰>Ð2Ñ2Ü 8ÀÇÁÐ]c×]oÑ]oÔ pˆDÕà $ˆDÔð 	�‰Õr'   Nr   Útarget_valuesrì   rÎ  rO   c                 óî  — t         j                  j                  «       }|�|n| j                  j                  }| j                  ||¬«      }t        |t        «      rt        |Ž }| j                  �7| j                  |j                  |j                  |j                  ¬«      |_
        | j                  |j                  «      }|�|du r
 |||«      }	nd}	t        |	||j                  |j                  ¬«      S )að  
        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.
        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.
        Nr°  r   TrÔ  )rD   r   ÚCrossEntropyLossr6   rì   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ü   € ôJ �x‰x×(Ñ(Ó*ˆð %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð —z‘zØØ!5ð "ó 
ˆô �l¤EÔ*Ü2°LÐAˆLà×ÑÐ(Ø-1×->Ñ->Ø×.Ñ.Ø ×$Ñ$Ø"×(Ñ(ð .?ó .ˆLÔ*ð —	‘	˜,×8Ñ8Ó9ˆàÐ$¨¸Ñ)<Ù˜E =Ó1‰HàˆHä<ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
r'   rÙ  )r-   r.   r/   r0   r   r   r   r   rD   rE   rÄ   r  r,   r2   r3   s   @r&   r  r  «  s„   ø„ ñ	ðÐ1õ ð Øð .2Ø,0Ø ñC
à—\‘\ðC
ð —|‘| dÑ*ðC
ð # T™kð	C
ð
 ðC
ð 
7òC
ó ó ôC
r'   r  z=
    Output type of [`PatchTSMixerForRegressionOutput`].
    c                   ó¸   — e Zd ZU dZdZej                  dz  ed<   dZej                  dz  ed<   dZ	ej                  dz  ed<   dZ
eej                     dz  ed<   y)ÚPatchTSMixerForRegressionOutputaM  
    loss (*optional*, returned when `y` is provided, `torch.FloatTensor` of shape `()`):
        Total loss.
    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.
    NrÅ  Úregression_outputsr¦  r²   )r-   r.   r/   r0   rÅ  rD   r§  r)  r  r¦  r²   rÅ   rR  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  r;   rN   Ú	expansionc                 ó&  •— t         ‰| �  «        t        j                  |dz   ||z  «      | _        t        j                  ||z  |«      | _        t        j                  dd|z  «      | _        t        j                  d|z  d«      | _        || _        y rQ  )	r   r   r   r    Úinverse_trans_expansionÚinverse_trans_compressionÚmap_scale_expansionÚmap_scale_compressionrN   )r$   r;   rN   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   )
rA   rY   r?  rN   rD   Úcatr  r  r  r  )r$   r*   rˆ  rŠ  r_   ÚstdevÚconcat_statss          r&   r,   z InjectScalerStatistics4D.forward0  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/   r1   r   rD   rE   r,   r2   r3   s   @r&   r  r  &  sC   ø„ ñ' ð '°#ð 'À#õ 'ð˜eŸl™lð °·±ð ÀeÇlÁl÷ r'   r  z4
    `PatchTSMixer` for regression application.
    c                   óÜ   ‡ — e Zd Zdefˆ fd„Zee	 	 	 ddej                  dej                  dz  de	dz  de	de
f
d	„«       «       Z ej                  «       dej                  defd
„«       Zˆ xZS )ÚPatchTSMixerForRegressionr6   c                 óh  •— t         ‰| �  |«       t        |«      | _        |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)                  «        y )Nrò  ró  r   r÷  r  r  rø  )r   r   rµ  r  rÅ  r  rù  r   r   r   rú  r  ra   r—   r  r;   rN   r  r  rÌ  r®  )r$   r6   rû  rü  r%   s       €r&   r   z"PatchTSMixerForRegression.__init__T  s  ø€ Ü‰Ñ˜Ô ä& vÓ.ˆŒ
à—K‘KˆŒ	Ø#)×#=Ñ#=ˆÔ à$*×$?Ñ$?ˆÔ!à�;‰;˜%ÒØ'+ˆ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ô
ˆŒ	ð 	�‰Õr'   Nr   r  rì   rÎ  rO   c                 óF  ‡ — ‰ j                   dk(  rt        j                  d¬«      }n!‰ j                   dk(  rt        }nt	        d«      ‚|�|n‰ j
                  j                  }‰ j                  ||¬«      }t        |t        «      rt        |Ž }‰ j                  �7‰ j                  |j                  |j                  |j                  ¬«      |_        ‰ j                  |j                  «      }|��|d	u rŒ‰ j                   rv‰ j                   d
k(  r#t#        j$                  |dk  «      rt'        d«      ‚‰ j                   j)                  |«      }	t        ˆ fd„|D «       «      } ||	|«      }
t+        |
«      }
n |||«      }
nd}
t-        |
||j                  |j.                  ¬«      S )aä  
        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.
        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.
        rò  r_   rÑ  rè  rÿ  Nr°  r   Trö  r   zDtarget_values cannot be negative for negative_binomial distribution.c              3   ój   •K  — | ]*  }|j                  d ‰j                  j                  «      –— Œ, y­w)r   N)r·   r6   r  )r  Úitemr$   s     €r&   r  z4PatchTSMixerForRegression.forward.<locals>.<genexpr>½  s&   øè ø€ ÒWÈ˜dŸi™i¨¨D¯K©K×,CÑ,C×DÑWùs   ƒ03)rÅ  r  r¦  r²   )rÅ  r   rÕ  rè  ra   r6   rì   r  r	  rÅ   r³  r  r¦  rˆ  rŠ  rÌ  r  rD   ÚanyÚ	Exceptionr  rî  r  r²   )r$   r   r  rì   rÎ  r˜   rÅ  rÖ  r  r  rØ  s   `          r&   r,   z!PatchTSMixerForRegression.forwardy  s•  ø€ ðH �9‰9˜ÒÜ—:‘:¨Ô/‰DØ�Y‰Y˜%ÒÜ‰DäÐQÓRÐRð %9Ð$DÑ È$Ï+É+×JjÑJjð 	ð —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�äÓWÐQVÔWÓW�Ù ¨mÓ<�ä+¨HÓ5‘á  }Ó5‘àˆHä.ØØ$Ø*×<Ñ<Ø&×4Ñ4ô	
ð 	
r'   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  rD   r  r·   r6   r  rã  )r$   r   rù  r  r  rÒ   r  s          r&   r	  z"PatchTSMixerForRegression.generateÍ  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   rD   rE   rÄ   r  r,   r.  rã  r	  r2   r3   s   @r&   r%  r%  N  s·   ø„ ð#Ð1õ #ðJ Øð .2Ø,0Ø ñP
à—\‘\ðP
ð —|‘| dÑ*ðP
ð # T™kð	P
ð
 ðP
ð 
)òP
ó ó ðP
ðd €U‡]�]ƒ_ð#Eà—\‘\ð#Eð 
,ò#Eó ô#Er'   r%  )r  rµ  rÊ  rð  r  r%  )Nr¶   )NFr   )Nr   rÂ  )Ur0   r[   Úcollections.abcr   Údataclassesr   rD   Útorch.nnr   Útransformers.modeling_utilsr   Útransformers.utilsr   Ú r   r$  Úmodeling_flash_attention_utilsr	   Úmodeling_utilsr
   Úprocessing_utilsr   Útime_series_utilsr   r   r   Úutilsr   r   r   r   Úconfiguration_patchtsmixerr   Ú
get_loggerr-   ÚloggerÚModuler   r5   rG   ri   ru   r…   rE   rÃ   r£   r¥   rÇ   rÕ   rÛ   rå   rô   r  r  r0  ÚlistrÄ   r1   rM  rc  re  rq  ry  r�  r   r¥  r©  r³  rµ  rÄ  rÊ  rÛ  rÝ  rã  ÚdistributionsÚDistributionrè  rî  rð  r  r  r  r  r%  Ú__all__rR  r'   r&   ú<module>r@     sS  ðñ "ã Ý $Ý !ã Ý å 7Ý *å &Ý BÝ 5Ý &ß UÑ Uß RÓ RÝ :ð 
ˆ×	Ñ	˜HÓ	%€ô §¡ô ô*&˜BŸI™Iô &ô,$ R§Y¡Yô $ôN.˜BŸI™Iô .ôb�b—i‘iô ô.-¨2¯9©9ô -ðn !Øñ%Ø�I‰Ið%à�<‰<ð%ð 
�‰ð%ð �<‰<ð	%ð
 —L‘L 4Ñ'ð%ð �T‰\ð%ð ð%ð Ð'Ñ(ó%ô:R/˜BŸI™Iô R/ôjC�b—i‘iô CôL*˜Ÿ	™	ô *ôZ#˜Ÿ	™	ô #ôL&#˜Ÿ	™	ô &#ôR5 B§I¡Iô 5ôpD˜RŸY™Yô DðN ô0 /ó 0ó ð0ô*˜rŸy™yô ðD -1Ø',Øñ7%Ø�L‰Lð7%àð7%ð # T™kð7%ð !%ð	7%ð
 ó7%ð| -1Øñ	A%Ø�L‰LðA%à# c™zðA%ð # T™kð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 ðôð
 ô	: ó 	:ó óð	:ô9kÐ5ô 9kñx ðôð
 ô+˜kó +ó óð+ñ4 ðôô
Q
Ð3ó Q
óð
Q
ñh ðôð
 ô: {ó :ó óð:ñ$ ðôô
F
Ð!<ó F
óð
F
ñR ðôð
 ô+ kó +ó óð+ñ0 ðôð ô/¨ó /ó óð/ñ ðôð ô/¨ó /ó óð/ð#ˆu×"Ñ"×/Ñ/ð #¸¿¹ð #È%Ï,É,ó #ñ* 5§<¡<ð *¸%¿,¹,ÈÑ:Mð *Ðch×coÑcoó *ô0GEÐ ;ô GEñT ðôð
 ô:°Kó :ó óð:ô$`
Ð.Iô `
ñF ðôð
 ô: kó :ó óð:ô$%˜rŸy™yô %ñP ðôô
^EÐ ;ó ^Eóð
^EòB�r'   