
     `iJ                       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 dd
lmZ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 ddlmZ  ej        e           Z!	 	 	 dode	j"        dej#        dej#        dej#        deej#                 dee$         de$deej#                 fdZ% G d de	j"                  Z& G d de	j"                  Z'	 	 	 dpdej#        d e$d!ee(         d"e)d#e*f
d$Z+	 	 dqdej#        d%ee(e*f         d!ee(         d#e*fd&Z, G d' d(e	j"                  Z- G d) d*e	j"                  Z. G d+ d,e	j"                  Z/e G d- d.e                      Z0 G d/ d0e	j"                  Z1 G d1 d2e	j"                  Z2 G d3 d4e0          Z3e ed56           G d7 d8e                                  Z4e ed96           G d: d;e                                  Z5e ed<6           G d= d>e                                  Z6e ed?6           G d@ dAe                                  Z7e edB6           G dC dDe                                  Z8e edE6           G dF dGe                                  Z9dHej:        j;        dIej#        dJej#        fdKZ<drdLej#        dMeej#                 dJej#        fdNZ= G dO dPe	j"                  Z> G dQ dRe	j"                  Z? G dS dTe	j"                  Z@ G dU dVe	j"                  ZAe G dW dXe0                      ZB G dY dZe	j"                  ZC ed[6           G d\ d]e0                      ZD G d^ d_e	j"                  ZE ed`6           G da dbe0                      ZF edc6           G dd dee	j"                              ZG edf6           G dg dhe0                      ZH G di dje	j"                  ZI edk6           G dl dme0                      ZJg dnZKdS )szPyTorch PatchTST model.    N)	dataclass)CallableOptionalUnion)nn   )ACT2CLS)FlashAttentionKwargs)BaseModelOutput)ALL_ATTENTION_FUNCTIONSPreTrainedModel)Unpack)NegativeBinomialOutputNormalOutputStudentTOutput)ModelOutputauto_docstringlogging   )PatchTSTConfig        modulequerykeyvalueattention_maskscalingdropout	head_maskc                    ||                     d          dz  }t          j        ||                    dd                    |z  }	||	|z   }	t          j                            |	d          }	||	|                    dddd          z  }	t          j                            |	|| j	                  }	t          j        |	|          }
|
                    dd          
                                }
|
|	fS )N         r   dimr   )ptraining)sizetorchmatmul	transposer   
functionalsoftmaxviewr   r'   
contiguous)r   r   r   r   r   r   r   r   kwargsattn_weightsattn_outputs              /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/transformers/models/patchtst/modeling_patchtst.pyeager_attention_forwardr4   &   s     **R..D(<s}}Q':':;;gEL!#n4=((2(>>L#innQAq&A&AA=((6?([[L,|U33K''1--88::K$$    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de
j        dee
j                 dee
j                 dee
j                 dee         dee         dee
j        ee
j                 eee
j                          f         fdZ xZS )PatchTSTAttentionz=Multi-headed attention from 'Attention Is All You Need' paperr   FTN	embed_dim	num_headsr   
is_decoderbias	is_causalconfigc                 
   t                                                       || _        || _        || _        ||z  | _        || _        | j        |z  | j        k    rt          d| j         d| d          | j        dz  | _        || _	        || _
        t          j        |||          | _        t          j        |||          | _        t          j        |||          | _        t          j        |||          | _        d S )Nz;embed_dim must be divisible by num_heads (got `embed_dim`: z and `num_heads`: ).r"   r;   )super__init__r8   r9   r   head_dimr=   
ValueErrorr   r:   r<   r   Lineark_projv_projq_projout_proj)	selfr8   r9   r   r:   r;   r<   r=   	__class__s	           r3   rB   zPatchTSTAttention.__init__H   s    	""!Y.MI%$.883dn 3 3%.3 3 3   }d*$"i	94@@@i	94@@@i	94@@@	)YTBBBr5   hidden_stateskey_value_statesr   layer_head_maskoutput_attentionsr0   returnc                    |du}|j         dd         \  }}	|r|j         d         n|	}
||	d| j        f}||
d| j        f} |                     |          j        |                     dd          }|r|n|} |                     |          j        |                     dd          } |                     |          j        |                     dd          }t          }| j        j	        dk    rt          | j        j	                 } || ||||f| j        sdn| j        | j        ||d|\  }}|                    ||	d                                          }|                     |          }||dfS )z#Input shape: Batch x Time x ChannelNr!   r   r#   eagerr   )r   r   rO   r   )shaperC   rH   r.   r+   rF   rG   r4   r=   _attn_implementationr   r'   r   r   reshaper/   rI   )rJ   rL   rM   r   rN   rO   r0   is_cross_attentionbsztgt_lensrc_lenq_input_shapekv_input_shapequery_statescurrent_states
key_statesvalue_statesattention_interfacer2   r1   s                       r3   forwardzPatchTSTAttention.forwardg   s    .T9 %*3B3/W/AN"(++wgr4=9wDM: 7t{{=116FPPQRTUVV-?R))]5T[[005~FPPQRTUVV
7t{{>227HRRSTVWXX(?;+w66"9$+:Z"[$7$7%
  $}>CC$,L/%%
 %
 %
 %
!\ "))#w;;FFHHmmK00L$..r5   )r   FTFN)NNNF)__name__
__module____qualname____doc__intfloatboolr   r   rB   r)   Tensorr   r
   tuplera   __classcell__rK   s   @r3   r7   r7   E   s^       GG  +/C CC C 	C
 C C C (C C C C C CD 481526,13/ 3/|3/ #5<03/ !.	3/
 "%,/3/ $D>3/ -.3/ 
u|Xel3XeEL>Q5RR	S3/ 3/ 3/ 3/ 3/ 3/ 3/ 3/r5   r7   c                   >     e Zd ZdZdef fdZdej        fdZ xZ	S )PatchTSTBatchNormzP
    Compute batch normalization over the sequence length (time) dimension.
    r=   c                     t                                                       t          j        |j        |j                  | _        d S )Neps)rA   rB   r   BatchNorm1dd_modelnorm_eps	batchnormrJ   r=   rK   s     r3   rB   zPatchTSTBatchNorm.__init__   s7    FOLLLr5   inputsc                     |                     dd          }|                     |          }|                     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   r#   )r+   ru   )rJ   rw   outputs      r3   ra   zPatchTSTBatchNorm.forward   s@     !!!Q''''1%%%r5   
rb   rc   rd   re   r   rB   r)   ri   ra   rk   rl   s   @r3   rn   rn      sr         M~ M M M M M M
&el 
& 
& 
& 
& 
& 
& 
& 
&r5   rn   Frw   
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            }
|r0t	          j        |d||	          }|                    d|d          }nt	          j        ||||	          }t	          j        ||||	          }d|ddddd|
f<   t	          j        |d          }t	          j        |d          }t	          j	        |d|	          }|
                    d                              ddd|          }|d|dd|ddddf<   |                     |                                |          }||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   )rD   rS   r   rf   r)   randrepeatonesargsortgather	unsqueezemasked_fillrh   )rw   r{   r|   r}   r~   
batch_sizenum_channelssequence_lengthnum_featuresr   len_keepnoisemaskids_shuffleids_restoreinputs_masks                   r3   random_maskingr      s   4 A~~qNzNNNOOO>Dl;Jo|]F?a*n566H! U
:q/&IIIQa00 
:|_VTTT :j,OOODDAAAyy -2...K-444K<"K888D>>"$$Q1l;;D+23QQQ(!!!QQQ./$$TYY[[*==KV$$r5   num_forecast_mask_patchesc                    t          |t                    r|g}d |D             }| j        \  }}}}t          j        |||| j                  }	g }
d}t          |          }t          ||          D ]V\  }}|dk    s||k    rt          d| d          t          ||z  |z            }|
	                    |||g           ||z  }Wt          |
d           }
||k     r|
d         d         ||z
  z   |
d         d<   n#||k    r|
d	         d         ||z
  z   |
d	         d<   d}|
D ]\  }}}||z   }d
|	||dd| df<   |}t          j        |	j        d                   }|	|         }	|	                    d	                              d
d
d
|          }	|d|	dd|ddddf<   |                     |	                                |          }||	d         fS )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)`
    c                     g | ]}d S )r    ).0_s     r3   
<listcomp>z$forecast_masking.<locals>.<listcomp>  s    AAA!AAAAr5   r   r   znum_forecast_mask_patches z6 should be greater than 0 and less than total patches.c                     | d         S )Nr#   r   )xs    r3   <lambda>z"forecast_masking.<locals>.<lambda>  s
    !A$ r5   )r   r#   r!   r   Nr   )
isinstancerf   rS   r)   zerosr   sumziprD   appendsortedrandpermr   r   r   rh   )rw   r   r|   r~   forecast_mask_ratiosr   r   r   r   r   t_listtotal_lengthtotal_ratiopatch_lengthratiotemp_lenbatch1	patch_lenr   batch2permr   s                         r3   forecast_maskingr      sU   0 +S11 @%>$?!AA'@AAA>Dl;Jo|;z<WWWDFL*++K"#<>RSS ! !e1 ? ?q\qqq   zE)K788|UH5666 F///Fj  ay|zL'@Aq	!	
	"	"r
1
)BCr
1F"(  	1h("./VF]AAA	z{{*+>$*Q-((D:D>>"$$Q1l;;D+23QQQ(!!!QQQ./$$TYY[[*==KV$$r5   c                   >     e Zd ZdZdef fdZdej        fdZ xZ	S )PatchTSTPatchifyz
    A class to patchify the time series sequence into different patches

    Returns:
        `torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`
    r=   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
  | _	        d S )NzSequence length (z+) has to be greater than the patch length ()r   )
rA   rB   context_lengthr   r   patch_striderD   maxnum_patchessequence_start)rJ   r=   new_sequence_lengthrK   s      r3   rB   zPatchTSTPatchify.__init__9  s    %4"/"/4#444yD$8yyeievyyy  
   4d6GHH4K\\aearruvv"/$2CtGWZ[G[2\\"25HHr5   past_valuesc                 ,   |j         d         }|| j        k    rt          d| d| j         d          |dd| j        dddf         }|                    d| j        | j                  }|                    dd                                          }|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)`
        zInput sequence length (z%) doesn't match model configuration (r?   N)	dimensionr(   step)	rS   r   rD   r   unfoldr   r   r+   r/   )rJ   r   r   ry   s       r3   ra   zPatchTSTPatchify.forwardJ  s     &+B/d222x/xx`d`txxx   QQQ 3 5 5qqq89$2C$J[\\!!"b))4466r5   rz   rl   s   @r3   r   r   1  sr         I~ I I I I I I"5<        r5   r   c                   >     e Zd ZdZdef fdZdej        fdZ xZ	S )PatchTSTMaskinga  
    Class to perform random or forecast masking.

    Parameters:
        config (`PatchTSTConfig`): 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
    r=   c                    t                                                       |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        |j        | _        | j        t          | j                  | _        d S d S N)	rA   rB   random_mask_ratior}   	mask_typer   r|   r~   r   rv   s     r3   rB   zPatchTSTMasking.__init__n  s    !'!9*0*K'))/)I&(.(G% +(4,243P,Q,QD))) 54r5   patch_inputc                 2   | j         dk    r,t          || j        | j        | j        | j                  \  }}nI| j         dk    r&t          || j        | j        | j                  \  }}nt          d| j          d          |	                                }||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

        random)rw   r{   r|   r}   r~   forecast)rw   r   r|   r~   zInvalid mask type .)
r   r   r   r|   r}   r~   r   r   rD   rh   )rJ   r   masked_inputr   s       r3   ra   zPatchTSTMasking.forwardy  s     >X%%!/"1)-)F+/+J?" " "L$$ ^z))!1"*.*H)-)F?	" " "L$$ C$.CCCDDD yy{{T!!r5   rz   rl   s   @r3   r   r   a  sr        
 
	R~ 	R 	R 	R 	R 	R 	R!"5< !" !" !" !" !" !" !" !"r5   r   c                   P     e Zd ZdZdef fdZddej        dee	         fdZ
 xZS )	PatchTSTEncoderLayerz 
    PatchTST encoder layer
    r=   c           
         t                                                       |j        | _        t          |j        |j        |j        |          | _        |j        dk    rt          j
        |j                  nt          j                    | _        |j        dk    rt          |          | _        nH|j        dk    r&t          j        |j        |j                  | _        nt%          |j         d          | j        r|j        dk    rt          j
        |j                  nt          j                    | _        |j        dk    rt          |          | _        nH|j        dk    r&t          j        |j        |j                  | _        nt%          |j         d          t          j        t          j        |j        |j        |j                  t3          |j                             |j        dk    rt          j
        |j                  nt          j                    t          j        |j        |j        |j                            | _        |j        dk    rt          j
        |j                  nt          j                    | _        |j        dk    rt          |          | _        nH|j        dk    r&t          j        |j        |j                  | _        nt%          |j         d          |j        | _        d S )N)r8   r9   r   r=   r   ru   	layernormrp   z$ is not a supported norm layer type.r@   ) rA   rB   channel_attentionr7   rs   num_attention_headsattention_dropout	self_attnpath_dropoutr   DropoutIdentitydropout_path1	norm_typern   norm_sublayer1	LayerNormrt   rD   dropout_path2norm_sublayer2
SequentialrE   ffn_dimr;   r	   activation_function
ff_dropoutffdropout_path3norm_sublayer3pre_normrv   s     r3   rB   zPatchTSTEncoderLayer.__init__  s   !'!9*n0,	
 
 
 AG@SVW@W@WRZ(;<<<]_]h]j]j{**"3F";";D,,"$,v~6?"S"S"SD 0VVVWWW ! 	\DJDWZ[D[D[F,?!@!@!@acalananD;..&7&?&?##![00&(l6>v&W&W&W## F$4!Z!Z!Z[[[ -Ifnfn6;GGGF./11-3->-B-BBJv()))Ifnfn6;GGG	
 
 AG@SVW@W@WRZ(;<<<]_]h]j]j{**"3F";";D,,"$,v~6?"S"S"SD 0VVVWWWr5   Nhidden_staterO   c                 ~   |j         \  }}}}|                    ||z  ||          }| j        rG|                     |                     |          |          \  }}}	||                     |          z   }nF|                     ||          \  }}}	|                     ||                     |          z             }|                    ||||          }| j        r|                    dd          	                                }|                    ||z  ||          }| j        rG|                     | 
                    |          |          \  }}
}	||                     |          z   }nF|                     ||          \  }}
}	| 
                    ||                     |          z             }|                    ||||          }|                    dd          	                                }|                    ||z  ||          }| j        r?||                     |                     |                     |                              z   }n>|                     ||                     |                     |                    z             }|                    ||||          }|f}|r|| j        r||
fn|fz  }|S )a  
        Parameters:
            hidden_state (`torch.Tensor` of shape `(batch_size, num_channels, sequence_length, d_model)`, *required*):
                Past values of the time series
            output_attentions (`bool`, *optional*):
                Whether or not to return the output attention of all layers
        Return:
            `torch.Tensor` of shape `(batch_size, num_channels, sequence_length, d_model)`

        )rL   rO   r#   r   )rS   r.   r   r   r   r   rU   r   r+   r/   r   r   r   r   r   )rJ   r   rO   r   num_input_channelsr   rs   r2   r1   r   channel_attn_weightsoutputss               r3   ra   zPatchTSTEncoderLayer.forward  s%    DPCU@
& $((6H)H/[bcc= 	_+/>>"11,??Sd ,: , ,(Kq ($*<*<[*I*IILL ,0>>*>O ,: , ,(Kq  ..|d>P>PQ\>]>]/]^^L $++J8JO]dee ! 	E'11!Q77BBDDL',,Z/-IK]_fggL} c7;~~"&"5"5l"C"CWh 8F 8 8411  ,d.@.@.M.MM 8<~~".BS 8F 8 8411  $22<$BTBTU`BaBa3abb (//
OM_ahiiL'11!Q77BBDDL $((6H)H/[bcc= 	i ($*<*<TWWTEXEXYeEfEf=g=g*h*hhLL  ..|d>P>PQUQXQXYeQfQf>g>g/ghhL $++J8JO]dee/ 	kt?Uj&:;;\h[jjGr5   r   )rb   rc   rd   re   r   rB   r)   ri   r   rh   ra   rk   rl   s   @r3   r   r     s         0(~ 0( 0( 0( 0( 0( 0(dQ QEL QXd^ Q Q Q Q Q Q Q Qr5   r   c                   D    e Zd ZU eed<   dZdZdZdej	        fdZ
d	dZdS )
PatchTSTPreTrainedModelr=   modelr   Fr   c                    t          |t                    rt          | j        j        | j        j                  | j        j        z
  | j        j        z  dz   }| j        j        r+t          j	        
                    |j        d           |dz  }|                    | j        |          |_        dS t          |t          j                  r?|j        j                                         |j        j                            d           dS t          |t(                    rI|j        j        j                                         |j        j        j                            d           dS t          |t          j                  rR|j        j        
                    d| j        j                   |j        "|j        j                                         dS dS dS )z$
        Initialize weights
        r   g{Gz?)std      ?r   )meanr   N)r   PatchTSTPositionalEncodingr   r=   r   r   r   use_cls_tokenr   initnormal_	cls_token_init_peposition_encr   r;   datazero_weightfill_rn   ru   rE   init_std)rJ   r   r   s      r3   _init_weightsz%PatchTSTPreTrainedModel._init_weights/  s    f899 	) DK.0HIIDKLdd)*,-.K {( ! 0d;;;q "(//$+{"K"KF-- 		)K""$$$M$$S))))) 122 	)!&,,...#(..s33333	** 	)M&&CT[5I&JJJ{& &&(((((	) 	)&&r5   c                 B    t          |t                    r	||_        d S d S r   )r   PatchTSTEncodergradient_checkpointing)rJ   r   r   s      r3   _set_gradient_checkpointingz3PatchTSTPreTrainedModel._set_gradient_checkpointingI  s,    f00 	2,1F)))	2 	2r5   N)F)rb   rc   rd   r   __annotations__base_model_prefixmain_input_namesupports_gradient_checkpointingr   Moduler  r  r   r5   r3   r   r   (  sd         #O&+#)BI ) ) ) )42 2 2 2 2 2r5   r   c                   :     e Zd Zdef fdZdej        fdZ xZS )PatchTSTEmbeddingr=   c                    t                                                       |j        | _        |j        | _        | j        r&t	          j        |j        |j                  | _        d S t	          j	                    | _        t          |j                  D ]9}| j                            t	          j        |j        |j                             :d S r   )rA   rB   r   share_embeddingr   rE   r   rs   input_embedding
ModuleListranger   )rJ   r=   r   rK   s      r3   rB   zPatchTSTEmbedding.__init__O  s    "(";%5 	\#%9V-@&.#Q#QD   #%=??D 6455 \ \$++BIf6I6>,Z,Z[[[[\ \r5   r   c                     j         d         }| j        k    rt          d j         d| d           j        r                               }n2 fdt          |          D             }t          j        |d          }|S )a%  
        Parameters:
            patch_input (`torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`, *required*):
                Patch input for embedding
        return:
            `torch.Tensor` of shape `(batch_size, num_channels, num_patches, d_model)`
        r   z&The defined number of input channels (zQ) in the config has to be the same as the number of channels in the batch input (r   c           
      \    g | ](} j         |         d d |d d d d f                   )S r   )r  )r   ir   rJ   s     r3   r   z-PatchTSTEmbedding.forward.<locals>.<listcomp>m  sE    nnnq1$.q1+aaaAAAqqqj2IJJnnnr5   r$   )rS   r   rD   r  r  r  r)   stack)rJ   r   r   
embeddingss   ``  r3   ra   zPatchTSTEmbedding.forward[  s     ).q1!888j9P j jTfj j j    	8--k::JJnnnnnTYZlTmTmnnnJZQ777Jr5   	rb   rc   rd   r   rB   r)   ri   ra   rk   rl   s   @r3   r  r  N  sh        
\~ 
\ 
\ 
\ 
\ 
\ 
\5<        r5   r  c                   p     e Zd ZdZdedef fdZedededej	        fd            Z
dej        fdZ xZS )	r   z'
    Class for positional encoding
    r=   r   c                    t                                                       |j        | _        |j        | _        |j        r8t	          j        t          j        ddd|j                            | _	        |dz  }| 
                    ||          | _        |j        dk    rt	          j        |j                  nt	          j                    | _        d S )Nr   r   )rA   rB   r   r   r   	Parameterr)   r   rs   r   r   r   positional_dropoutr   r   rJ   r=   r   rK   s      r3   rB   z#PatchTSTPositionalEncoding.__init__w  s    #1"("; 	\%+aAv~*N*NOODN1K MM&+>> 6<5NQR5R5RBJv0111XZXcXeXe 	r5   rP   c                    | j         dk    r0t          j        t          j        || j                  d          }n:| j         dk    rt          j        || j                  }t          j        d|                              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<   ||                                z
  }||                                d	z  z  }t          j        |d
          }nt!          | j          d          |S )Nr   Trequires_gradsincosr   r   r#   g     @
   FzN is not a valid positional encoder. Available types are 'random' and 'sincos'.)positional_encoding_typer   r  r)   randnrs   r   aranger   expmathlogsincosr   r   rD   )r=   r   r   positiondiv_terms        r3   r   z#PatchTSTPositionalEncoding._init_pe  sy    *h66<K(P(P`deeeLL,88 ;{FNCCL|A{33==a@@Hya!C!CQXHYHY\b\jHjFk!kllH$)Ih.A$B$BLADqD!$)Ih.A$B$BLADqD!',*;*;*=*==L'<+;+;+=+=+BCL<EJJJLL2  C  C  C   r5   r   c                 X   | j         r|                     || j        dd d d f         z             }| j        | j        d dd d f         z   }|                    |j        d         | j        dd          }t          j        ||fd          }n|                     || j        z             }|S )Nr   r   r!   r#   r$   )	r   r  r   r   expandrS   r   r)   cat)rJ   r   r   
cls_tokensr   s        r3   ra   z"PatchTSTPositionalEncoding.forward  s     	T11+@QRSRTRTVWVWVWRW@X2XYYK):2A2qqq5)AAI"))+*;A*>@WY[]_``J 9j+%>AFFFLL  22;AR3RSSLr5   )rb   rc   rd   re   r   rf   rB   staticmethodr   r  r   r)   ri   ra   rk   rl   s   @r3   r   r   r  s         
~ 
C 
 
 
 
 
 
  c bl    \&5<        r5   r   c            	       l     e Zd ZdZdedef fdZ	 	 ddej        de	e
         de	e
         d	efd
Z xZS )r  z
    PatchTST Encoder
    r=   r   c                 B   t                                                     d| _        t                    | _        t          |          | _        t          j        fdt          j
                  D                       | _        |                                  d S )NFc                 .    g | ]}t                    S r   )r   )r   r  r=   s     r3   r   z,PatchTSTEncoder.__init__.<locals>.<listcomp>  s"    $k$k$ka%9&%A%A$k$k$kr5   )rA   rB   r  r  embedderr   positional_encoderr   r  r  num_hidden_layerslayers	post_initr  s    ` r3   rB   zPatchTSTEncoder.__init__  s       &+# *&11"<V["Q"Qm$k$k$k$k5QWQiKjKj$k$k$kll 	r5   Nr   output_hidden_statesrO   rP   c                 <   ||n| j         j        }||n| j         j        }|                     |          }|                     |          }|rdnd}|rdnd}| j        D ]-}|r||fz   } |||          }|d         }|r||d         fz   }.t          |||          S )a  
        Parameters:
            patch_input (`torch.Tensor` of shape `(batch_size, num_channels, num_patches, patch_length)`, *required*):
                Past values of the time series
            output_hidden_states (bool, optional): Indicates if hidden states should be outputted.
            output_attentions (bool, optional): Indicates if attentions should be outputted.

        return:
            `BaseModelOutput`
        Nr   )r   rO   r   r   )last_hidden_staterL   
attentions)r=   rO   r9  r4  r5  r7  r   )	rJ   r   r9  rO   r   encoder_statesall_attentionsencoder_layerlayer_outputss	            r3   ra   zPatchTSTEncoder.forward  s      2C1N--TXT_Tq$8$D  $+Jj 	
 mmK00..{;;3=0:d![ 
	F 
	FM# B!/</!A)M|WhiiiM )+L  F!/=3C2E!E^hvwwwwr5   NN)rb   rc   rd   re   r   rf   rB   r)   ri   r   rh   r   ra   rk   rl   s   @r3   r  r    s         ~ C      " 04,0	(x (x\(x 'tn(x $D>	(x
 
(x (x (x (x (x (x (x (xr5   r  zG
    Base class for model's outputs, with potential hidden states.
    )custom_introc                   (   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
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	<   dS )
PatchTSTModelOutputa>  
    last_hidden_state (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches, patch_length)`):
        Sequence of hidden-states at the output of the last layer of the model.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each layer) of shape `(batch_size, num_channels, height, width)`. Hidden-states of
        the model at the output of each layer plus the optional initial embedding outputs.
    mask (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches)`, *optional*):
        Bool masked tensor indicating which patches are masked
    loc (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`, *optional*):
        Mean of the input data (batch_size, sequence_length, num_channels) over the sequence_length
    scale (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`, *optional*):
        Std of the input data (batch_size, sequence_length, num_channels) over the sequence_length
    patch_input (`torch.FloatTensor` of shape `(batch_size, num_channels, num_patches, patch_length)`):
        Patched input to the Transformer
    Nr;  rL   r<  r   locscaler   )rb   rc   rd   re   r;  r   r)   FloatTensorr  rL   rj   r<  r   rE  rF  r   r   r5   r3   rD  rD    s          " 6:x 129998<M8E%"345<<<59Ju012999(,D(5$
%,,,'+C%#	$+++)-E8E%&---/3K%+,33333r5   rD  z4
    Output type of [`PatchTSTForPretraining`].
    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ej                          ed<   dZeeej                          ed<   dS )PatchTSTForPretrainingOutputa  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        MSE loss.
    prediction_output (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`):
        Prediction outputs of the time series modeling heads.
    Nlossprediction_outputrL   r<  )rb   rc   rd   re   rJ  r   r)   rG  r  rK  rL   rj   r<  r   r5   r3   rI  rI  	  s           )-D(5$
%,,,59x 129998<M8E%"345<<<59Ju01299999r5   rI  z3
    Output type of [`PatchTSTForRegression`].
    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ej                          ed<   dZeeej                          ed<   dS )PatchTSTForRegressionOutputa  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        MSE loss.
    regression_outputs (`torch.FloatTensor` of shape `(batch_size, num_targets)`):
        Regression outputs of the time series modeling heads.
    NrJ  regression_outputsrL   r<  )rb   rc   rd   re   rJ  r   r)   rG  r  rN  rL   rj   r<  r   r5   r3   rM  rM    s           )-D(5$
%,,,6:!23:::8<M8E%"345<<<59Ju01299999r5   rM  z3
    Output type of [`PatchTSTForPrediction`].
    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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S )	PatchTSTForPredictionOutputa!  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        MSE loss.
    prediction_outputs (`torch.FloatTensor` of shape `(batch_size, prediction_length, -1)`):
        Prediction outputs of the time series modeling heads.
    attentions (`tuple(torch.FloatTensor)`, *optional*, returned when `output_attentions=True` is passed or when `config.output_attentions=True`):
        Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length,
        sequence_length)`.

        Attentions weights after the attention softmax, used to compute the weighted average in the self-attention
        heads.
    loc: (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`, *optional*)
        Mean of the input data (batch_size, sequence_length, num_channels) over the sequence_length
    scale: (`torch.FloatTensor` of shape `(batch_size, 1, num_channels)`, *optional*)
        Std of the input data (batch_size, sequence_length, num_channels) over the sequence_length
    NrJ  prediction_outputsrL   r<  rE  rF  )rb   rc   rd   re   rJ  r   r)   rG  r  rQ  rL   rj   r<  rE  rF  r   r5   r3   rP  rP  1  s          " )-D(5$
%,,,6:!23:::8<M8E%"345<<<59Ju012999'+C%#	$+++)-E8E%&-----r5   rP  z7
    Output type of [`PatchTSTForClassification`].
    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ej                          ed<   dZeeej                          ed<   dS )PatchTSTForClassificationOutputa  
    loss (*optional*, returned when `labels` is provided, `torch.FloatTensor` of shape `(1,)`):
        Total loss as the sum of the masked language modeling loss and the next sequence prediction
        (classification) loss.
    prediction_logits (`torch.FloatTensor` of shape `(batch_size, num_targets)`):
        Prediction scores of the PatchTST modeling head (scores before SoftMax).
    NrJ  prediction_logitsrL   r<  )rb   rc   rd   re   rJ  r   r)   rG  r  rT  rL   rj   r<  r   r5   r3   rS  rS  Q  s           )-D(5$
%,,,59x 129998<M8E%"345<<<59Ju01299999r5   rS  z
    Base class for time series model's predictions outputs that contains the sampled values from the chosen
    distribution.
    c                   8    e Zd ZU dZdZeej                 ed<   dS )SamplePatchTSTOutputz
    sequences (`torch.FloatTensor` of shape `(batch_size, num_samples, prediction_length, num_targets)`):
        Sampled values from the chosen distribution.
    N	sequences)	rb   rc   rd   re   rW  r   r)   rG  r  r   r5   r3   rV  rV  f  s6          
 .2Ix)*11111r5   rV  inputtargetrP   c                 .    |                      |           S )zc
    Computes the negative log likelihood loss from input distribution with respect to target.
    )log_prob)rX  rY  s     r3   nllr\  w  s     NN6""""r5   input_tensorweightsc                 n   |t          j        |dk    | |z  t          j        |                     }t          j        |r|                    |          n|                                d          }|r|                    |          n|                                |z  S |                     |          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`.
    Nr   r$   r   min)r)   where
zeros_likeclampr   r   )r]  r^  r%   weighted_tensorsum_weightss        r3   weighted_averagerg    s      +glL74JEL\]iLjLjkkk#"P'++#+"6"6"67;;==VYZZZ03N###,,,9L9L9N9NR]]]  S )))r5   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 )PatchTSTStdScalerz
    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.
    r=   c                     t                                                       t          |d          r|j        nd| _        t          |d          r|j        nd| _        t          |d          r|j        nd| _        d S )Nscaling_dimr   keepdimTminimum_scalegh㈵>)rA   rB   hasattrrk  r%   rl  rm  rv   s     r3   rB   zPatchTSTStdScaler.__init__  sy    )0)G)GN6%%Q)0)C)CMv~~5<V_5U5U_V11[_r5   r   observed_indicatorrP   c                 d   |                     | j        | j                  }|                    d          }||z                       | j        | j                  |z  }||z
  |z  dz                       | 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)`)
        rl  r   r#   )r   r%   rl  	clamp_minr)   sqrtrm  )rJ   r   ro  denominatorrE  variancerF  s          r3   ra   zPatchTSTStdScaler.forward  s     ),,TXt|,LL!++C00((--dh-MMP[[Sj$661<AA$(TXT`Aaadoo
8d&8899s
e#S%//r5   rb   rc   rd   re   r   rB   r)   ri   rj   ra   rk   rl   s   @r3   ri  ri    s         
`~ ` ` ` ` ` `0L06;l0	u|U\5<7	80 0 0 0 0 0 0 0r5   ri  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 )PatchTSTMeanScalerz
    Computes a scaling factor as the weighted average absolute value along the first dimension, and scales the data
    accordingly.
    r=   c                 8   t                                                       t          |d          r|j        nd| _        t          |d          r|j        nd| _        t          |d          r|j        nd| _        t          |d          r|j        nd | _        d S )Nrk  r   rl  Trm  绽|=default_scale)rA   rB   rn  rk  r%   rl  rm  r|  rv   s     r3   rB   zPatchTSTMeanScaler.__init__  s    )0)G)GN6%%Q)0)C)CMv~~5<V_5U5U`V11[`5<V_5U5U_V11[_r5   r   ro  rP   c                    ||z                                                       | j        d          }|                    | j        d          }|t          j        |d          z  }| j        W|                    d          }t          j        |                    d          d          }t          j        ||z            }n| j        t          j        |          z  }t          j        |dk    ||          }t          j        || j	                  }||z  }	| j
        s|                    | j                  }|	t          j        |          |fS )rq  Trr  r   r`  Nr   r$   )absr   r%   r)   rd  r|  squeeze	ones_likerb  rm  rl  rc  )
rJ   r   ro  ts_sumnum_observedrF  	batch_sumbatch_observationsr|  scaled_datas
             r3   ra   zPatchTSTMeanScaler.forward  sE    ++002266tx6NN)--dh-EE\q9999 %

q
))I!&\-=-=a-@-@a!H!H!H!M)6H*HIIMM .1G1GGM L1,e]CC Et'9:::Ul| 	0MMdhM//EE,U33U::r5   rw  rl   s   @r3   ry  ry    s         
`~ ` ` ` ` ` `&;L&;6;l&;	u|U\5<7	8&; &; &; &; &; &; &; &;r5   ry  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 )
PatchTSTNOPScalerz|
    Assigns a scaling factor equal to 1 along the first dimension, and therefore applies no scaling to the input data.
    r=   c                     t                                                       t          |d          r|j        nd| _        t          |d          r|j        nd| _        d S )Nrk  r   rl  T)rA   rB   rn  rk  r%   rl  rv   s     r3   rB   zPatchTSTNOPScaler.__init__  sW    )0)G)GN6%%Q)0)C)CMv~~r5   Nr   ro  rP   c                     t          j        |d                              | j        | j                  }t          j        |d                              | 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)`)
        Fr  )r%   rl  )r)   r  r   r%   rl  rc  )rJ   r   ro  rF  rE  s        r3   ra   zPatchTSTNOPScaler.forward  sl     E:::??DHVZVb?cct5999>>48UYUa>bbS%r5   r   )rb   rc   rd   re   r   rB   r)   ri   r   rj   ra   rk   rl   s   @r3   r  r    s         N~ N N N N N N PT   L 6>u|6L 	u|U\5<7	8               r5   r  c            	       |     e 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 )PatchTSTScalerr=   c                    t                                                       |j        dk    s	|j        du rt          |          | _        d S |j        dk    rt          |          | _        d S t          |          | _        d S )Nr   Tr   )rA   rB   r   ry  scalerri  r  rv   s     r3   rB   zPatchTSTScaler.__init__  sx    >V##v~'='=,V44DKKK^u$$+F33DKKK+F33DKKKr5   r   ro  rP   c                 @    |                      ||          \  }}}|||fS )a>  
        Parameters:
            data (`torch.Tensor` of shape `(batch_size, sequence_length, num_input_channels)`):
                Input for scaler 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, um_input_channels)`)
        )r  )rJ   r   ro  rE  rF  s        r3   ra   zPatchTSTScaler.forward  s,      ;;t-?@@c5S%r5   )
rb   rc   rd   r   rB   r)   ri   rj   ra   rk   rl   s   @r3   r  r    s        4~ 4 4 4 4 4 4 L 6;l 	u|U\5<7	8               r5   r  c                        e Zd Zdef fdZ	 	 	 	 	 ddej        deej                 deej                 dee         dee         d	ee         d
e	e
ef         fdZ xZS )PatchTSTModelr=   c                    t                                          |           t          |          | _        t	          |          | _        |j        | _        | j        j        }| j        rt          |          | _	        nt          j                    | _	        t          ||          | _        |                                  d S )N)r   )rA   rB   r  r  r   
patchifierdo_mask_inputr   r   maskingr   r   r  encoderr8  r  s      r3   rB   zPatchTSTModel.__init__,  s       $V,,*622#1o1 	)*622DLL;==DL&v;GGG 	r5   Nr   past_observed_maskfuture_valuesr9  rO   return_dictrP   c           	      D   ||n| j         j        }||n| j         j        }||n| j         j        }|t	          j        |          }|                     ||          \  }}}	|                     |          }
| j        r| 	                    |
          \  }}n| 	                    |
          d}}| 
                    |||          }|s6|j        |j        |j        f}||||	|
fz   }t          d |D                       S t          |j        |j        |j        |||	|
          S )a  
        Parameters:
            past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*):
                Input sequence to the model
            past_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).
            future_values (`torch.BoolTensor` of shape `(batch_size, prediction_length, num_input_channels)`, *optional*):
                Future target values associated with the `past_values`
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers
            output_attentions (`bool`, *optional*):
                Whether or not to return the output attention of all layers
            return_dict (`bool`, *optional*):
                Whether or not to return a `ModelOutput` instead of a plain tuple.

        Returns:
            `PatchTSTModelOutput` or tuple of `torch.Tensor` (if `return_dict`=False or `config.return_dict`=False)

        Examples:

        ```python
        >>> from huggingface_hub import hf_hub_download
        >>> import torch
        >>> from transformers import PatchTSTModel

        >>> file = hf_hub_download(
        ...     repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
        ... )
        >>> batch = torch.load(file)

        >>> model = PatchTSTModel.from_pretrained("namctin/patchtst_etth1_pretrain")

        >>> # during training, one provides both past and future values
        >>> outputs = model(
        ...     past_values=batch["past_values"],
        ...     future_values=batch["future_values"],
        ... )

        >>> last_hidden_state = outputs.last_hidden_state
        ```N)r   r9  rO   c              3      K   | ]}||V  	d S r   r   )r   vs     r3   	<genexpr>z(PatchTSTModel.forward.<locals>.<genexpr>  s"      ==qq}}}}}==r5   )r;  rL   r<  r   rE  rF  r   )r=   use_return_dictrO   r9  r)   r  r  r  r  r  r  r;  rL   r<  rj   rD  )rJ   r   r  r  r9  rO   r  scaled_past_valuesrE  rF  patched_valuesmasked_valuesr   encoder_outputr   s                  r3   ra   zPatchTSTModel.forward>  sq   l &1%<kk$+B]1B1N--TXT_Tq$8$D  $+Jj 	 %!&!=!= *.[BT)U)U&C );<< 	E"&,,~">">M44"&,,~">">4M%<Pdu & 
 
  	>%79UWeWpqGsE> BBG==G======",>(6%0&
 
 
 	
r5   NNNNN)rb   rc   rd   r   rB   r)   ri   r   rh   r   rj   rD  ra   rk   rl   s   @r3   r  r  *  s        ~      * 6:04/3,0&*Z
 Z
\Z
 %U\2Z
  -	Z

 'tnZ
 $D>Z
 d^Z
 
u))	*Z
 Z
 Z
 Z
 Z
 Z
 Z
 Z
r5   r  c                   L     e Zd ZdZdef fdZdej        dej        fdZ xZ	S )PatchTSTMaskPretrainHeadz-
    Pretraining head for mask modelling
    r=   c                     t                                                       |j        dk    rt          j        |j                  nt          j                    | _        t          j        |j        |j	                  | _
        |j        | _        d S Nr   )rA   rB   head_dropoutr   r   r   r   rE   rs   r   linearr   rv   s     r3   rB   z!PatchTSTMaskPretrainHead.__init__  ss    :@:MPQ:Q:Qrz&"5666WYWbWdWdi0CDD#1r5   	embeddingrP   c                     |                      |                     |                    }| j        r|ddddddddf         }|S )a  
        Parameters:
            embedding (`torch.Tensor` of shape `(bs, num_channels, num_patches, d_model)` or
                    `(bs, num_channels, num_patches+1, d_model)` if `cls_token` is set to True, *required*):
                Embedding from the model
        Returns:
            `torch.Tensor` of shape `(bs, num_channels, num_patches, d_model)` or
                            `(bs, num_channels, num_patches+1, d_model)` if `cls_token` is set to True

        Nr   )r  r   r   )rJ   r  s     r3   ra   z PatchTSTMaskPretrainHead.forward  sT     KKY 7 788	 	/!!!!QQQAAA+.Ir5   rz   rl   s   @r3   r  r    st         2~ 2 2 2 2 2 2 %,        r5   r  z*
    The PatchTST for pretrain model.
    c                        e Zd Zdef fdZ	 	 	 	 ddej        deej                 dee         dee         dee         d	e	e
ef         fd
Z xZS )PatchTSTForPretrainingr=   c                     t                                          |           d|_        t          |          | _        t          |          | _        |                                  d S )NT)r=   )rA   rB   r  r  r   r  headr8  rv   s     r3   rB   zPatchTSTForPretraining.__init__  s\       #"&111
,V44	 	r5   Nr   r  r9  rO   r  rP   c                    ||n| j         j        }|                     ||||d          }|                     |j                  }t          j        d          } |||j                  }	|	                    d          |j	        z  
                                |j	        
                                dz   z  }
|j        }|s|f|d	d
         z   }|
|
f|z   n|}|S t          |
|||j                  S )a	  
        Parameters:
            past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*):
                Input sequence to the model
            past_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).
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers
            output_attentions (`bool`, *optional*):
                Whether or not to return the output attention of all layers
            return_dict (`bool`, *optional*): Whether or not to return a `ModelOutput` instead of a plain tuple.

        Returns:
            `PatchTSTForPretrainingOutput` or tuple of `torch.Tensor` (if `return_dict`=False or
            `config.return_dict`=False)

        Examples:

        ```python
        >>> from huggingface_hub import hf_hub_download
        >>> import torch
        >>> from transformers import PatchTSTConfig, PatchTSTForPretraining

        >>> file = hf_hub_download(
        ...     repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
        ... )
        >>> batch = torch.load(file)

        >>> # Config for random mask pretraining
        >>> config = PatchTSTConfig(
        ...     num_input_channels=7,
        ...     context_length=512,
        ...     patch_length=12,
        ...     stride=12,
        ...     mask_type='random',
        ...     random_mask_ratio=0.4,
        ...     use_cls_token=True,
        ... )
        >>> # Config for forecast mask pretraining
        >>> config = PatchTSTConfig(
        ...     num_input_channels=7,
        ...     context_length=512,
        ...     patch_length=12,
        ...     stride=12,
        ...     mask_type='forecast',
        ...     num_forecast_mask_patches=5,
        ...     use_cls_token=True,
        ... )
        >>> model = PatchTSTForPretraining(config)

        >>> # during training, one provides both past and future values
        >>> outputs = model(past_values=batch["past_values"])

        >>> loss = outputs.loss
        >>> loss.backward()
        ```NTr   r  r9  rO   r  none	reductionr!   r$   r{  r   )rJ  rK  rL   r<  )r=   r  r   r  r;  r   MSELossr   r   r   r   rL   rI  r<  )rJ   r   r  r9  rO   r  model_outputx_hatrJ  loss_valmasked_lossr=  r   s                r3   ra   zPatchTSTForPretraining.forward  s$   J &1%<kk$+B] zz#1!5/ " 
 
 		,899 zF+++4|788}}},,|/@@EEGG<K\K`K`KbKbejKjk%3 	had!33G2=2I{nw..wGN+^`l`w
 
 
 	
r5   )NNNN)rb   rc   rd   r   rB   r)   ri   r   rh   r   rj   rI  ra   rk   rl   s   @r3   r  r    s        ~       6:/3,0&*a
 a
\a
 %U\2a
 'tn	a

 $D>a
 d^a
 
u22	3a
 a
 a
 a
 a
 a
 a
 a
r5   r  c                   :     e Zd Zdef fdZdej        fdZ xZS )PatchTSTClassificationHeadr=   c                 |   t                                                       |j        | _        |j        | _        t	          j        d          | _        |j        dk    rt	          j        |j                  nt	          j	                    | _
        t	          j        |j        |j        z  |j                  | _        d S Nr   	start_dimr   )rA   rB   r   pooling_typer   Flattenflattenr  r   r   r   rE   r   rs   num_targetsr  rv   s     r3   rB   z#PatchTSTClassificationHead.__init__,  s    #1"/zA...:@:MPQ:Q:Qrz&"5666WYWbWdWdi 9FN JFL^__r5   r  c                 v   | j         r|dddddddf         }na| j        dk    r|                    d          }n?| j        dk    r|                    d          j        }nt          d| j         d          |                     |          }|                     |                     |                    }|S )	a[  
        Parameters:
            embedding (`torch.Tensor` of shape `(bs, num_channels, num_patches, d_model)` or
                     `(bs, num_channels, num_patches+1, d_model)` if `cls_token` is set to True, *required*):
                Embedding from the model
        Returns:
            `torch.Tensor` of shape `(bs, num_targets)`

        Nr   r   r#   r$   r   pooling operator  is not implemented yet)	r   r  r   r   valuesrD   r  r  r   rJ   r  pooled_embeddingry   s       r3   ra   z"PatchTSTClassificationHead.forward4  s      
	](AAAq!!!4&(((~~!~44%''(}}}33:[1B[[[\\\<<(899T\\*:;;<<r5   r  rl   s   @r3   r  r  +  sh        `~ ` ` ` ` ` `        r5   r  z0
    The PatchTST for classification model.
    c                        e Zd Zdef fdZe	 	 	 	 	 ddej        deej                 dee	         dee	         dee	         d	ee	         d
e
eef         fd            Z xZS )PatchTSTForClassificationr=   c                    t                                          |           |j        r!t                              d           d|_        t          |          | _        t          |          | _        | 	                                 d S )N+Setting `do_mask_input` parameter to False.F)
rA   rB   r  loggerwarningr  r   r  r  r8  rv   s     r3   rB   z"PatchTSTForClassification.__init__V  sy         	)NNHIII#(F "6**
.v66	 	r5   Nr   target_valuesr  r9  rO   r  rP   c                 B   ||n| j         j        }|                     ||||d          }|                     |j                  }d}	|t          j                    }
 |
||          }	|s|f|dd         z   }|	|	f|z   n|}|S t          |	||j        |j	                  S )ac  
        past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*):
            Input sequence to the model
        target_values (`torch.Tensor`, *optional*):
            Labels associates with the `past_values`
        past_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).

        Examples:

        ```python
        >>> from transformers import PatchTSTConfig, PatchTSTForClassification

        >>> # classification task with two input channel2 and 3 classes
        >>> config = PatchTSTConfig(
        ...     num_input_channels=2,
        ...     num_targets=3,
        ...     context_length=512,
        ...     patch_length=12,
        ...     stride=12,
        ...     use_cls_token=True,
        ... )
        >>> model = PatchTSTForClassification(config=config)

        >>> # during inference, one only provides past values
        >>> past_values = torch.randn(20, 512, 2)
        >>> outputs = model(past_values=past_values)
        >>> labels = outputs.prediction_logits
        ```NTr  r   r   )rJ  rT  rL   r<  )
r=   r  r   r  r;  r   CrossEntropyLossrS  rL   r<  )rJ   r   r  r  r9  rO   r  r  y_hatr  rJ  r   s               r3   ra   z!PatchTSTForClassification.forwardd  s    X &1%<kk$+B]zz#1!5/ " 
 
 		,899$&((DtE=11H 	had!33G/7/CxkG++GN.#&4#.	
 
 
 	
r5   r  )rb   rc   rd   r   rB   r   r)   ri   r   rh   r   rj   rS  ra   rk   rl   s   @r3   r  r  P  s        ~        15-1/3,0&*D
 D
\D
  -D
 %TN	D

 'tnD
 $D>D
 d^D
 
u55	6D
 D
 D
 ^D
 D
 D
 D
 D
r5   r  z,
    The PatchTST for regression Model.
    c                   @     e Zd Zddedef fdZdej        fdZ xZ	S )PatchTSTPredictionHeadNr=   r   c                    t                                                       |j        | _        |j        | _        |j        | _        |j        | _        | j        s| j        r|j        }n
|j        |z  }| j        s?t          j                    | _	        t          j                    | _
        t          j                    | _        t          | j                  D ]}| j                            t          j        d                     |3| j	                            t          j        ||j                             n-| j	                            |                    |                     | j
                            |j        dk    rt          j        |j                  nt          j                               dS t          j        d          | _        | t          j        ||j                  | _        n|                    |          | _        |j        dk    rt          j        |j                  nt          j                    | _        dS )a  
        num_patches (`int`):
            The number of patches in the input sequence.
        distribution_output (`DistributionOutput`, *optional*):
            The distribution output layer for probabilistic forecasting. If None, a linear output layer is used.
        r#   r  Nr   )rA   rB   share_projectionr   r   r  rs   r   r  projectionsdropoutsflattensr  r   r  rE   prediction_lengthget_parameter_projectionr  r   r   r  
projectionr   )rJ   r=   r   distribution_outputrC   r  rK   s         r3   rB   zPatchTSTPredictionHead.__init__  s    	 & 7"(";#1"/ 	4 2 	4~HH~3H$ 	i!}DMOODMMOODM4233 t t$$RZ!%<%<%<===&.$++BIh@X,Y,YZZZZ $++,?,X,XYa,b,bccc$$H[^_H_H_RZ0C%D%D%Degeperersssst t :222DL"*"$)Hf6N"O"O #6"N"Nx"X"X>D>QTU>U>U2:f&9:::[][f[h[hDLLLr5   r  c                    | j         r|dddddddf         }nK| j        dk    r|                    d          }n)| j        dk    r|                    d          j        }n|}| j        sg }t          | j                  D ]f} | j        |         |dd|ddf                   } | j	        |         |          } | j
        |         |          }|                    |           gt          j        |d          }n?|                     |          }|                     |          }|                     |          }t#          |t$                    rt%          d |D                       }n|                    dd          }|S )	aj  
        Parameters:
            embedding (`torch.Tensor` of shape `(bs, num_channels, num_patches, d_model)` or
                     `(bs, num_channels, num_patches+1, d_model)` if `cls_token` is set to True, *required*):
                Embedding from the model
        Returns:
            `torch.Tensor` of shape `(bs, forecast_len, num_channels)`

        Nr   r   r#   r$   r   r   c              3   B   K   | ]}|                     d d          V  dS )r#   r   N)r+   )r   zs     r3   r  z1PatchTSTPredictionHead.forward.<locals>.<genexpr>  s0      ==1;;q!,,======r5   )r   r  r   r   r  r  r  r   r  r  r  r   r)   r  r  r   r  r   rj   r+   )rJ   r  r  ry   r  s        r3   ra   zPatchTSTPredictionHead.forward  s     	-(AAAq!!!4 F**#,>>a>#8#8  "e++#,==Q=#7#7#>   $- $ 	7F4233 0 0#34=#34DQQQ111W4M#N#N #34=#34D#E#E  $74#3A#67G#H#H .////[Q///FF  $||,<==#||,<== __%566Ffe$$ 	,==f=====FF%%a++Fr5   r   )
rb   rc   rd   r   rf   rB   r)   ri   ra   rk   rl   s   @r3   r  r    sw        )i )i~ )iC )i )i )i )i )i )iV1 1 1 1 1 1 1 1 1r5   r  z,
    The PatchTST for prediction model.
    c                        e Zd Zdef fdZ	 	 	 	 	 ddej        deej                 deej                 dee         dee         d	ee         d
e	e
ef         fdZ ej                    	 ddej        deej                 d
efd            Z xZS )PatchTSTForPredictionr=   c                 x   t                                          |           |j        r!t                              d           d|_        t          |          | _        |j        dk    rd | _        n|j        dk    rt          |j
                  | _        nc|j        dk    rt          |j
                  | _        n=|j        dk    rt          |j
                  | _        nt          d|j                   t          || j        j        j        | j        	          | _        |                                  d S )
Nr  Fmse	student_tr$   normalnegative_binomialUnknown distribution output )r  )rA   rB   r  r  r  r  r   rJ  r  r   r  r   r   rD   r  r  r   r  r8  rv   s     r3   rB   zPatchTSTForPrediction.__init__  s:        	)NNHIII#(F "6**
;%'+D$$)[88+9f>V+W+W+W((+x77+7F<T+U+U+U((+/BBB+AfF^+_+_+_(( !\@Z!\!\]]]*DJ)54Kc
 
 
	
 	r5   Nr   r  r  r9  rO   r  rP   c                 :   ||n| j         j        }|                     ||||d          }|                     |j                  }d}	| j        r|}
n||j        z  |j        z   }
|o| j        rG| j                            ||j        |j                  }t          ||          }	t          |	          }	n!t          j        d          } ||
|          }	|j        }|j        }|s|
f|dd         z   }|	|	f|z   n|}|S t          |	|
|j        |j        ||	          S )
aV	  
        Parameters:
            past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*):
                Input sequence to the model
            past_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).
            future_values (`torch.Tensor` of shape `(bs, forecast_len, num_input_channels)`, *optional*):
                Future target values associated with the `past_values`
            output_hidden_states (`bool`, *optional*):
                Whether or not to return the hidden states of all layers
            output_attentions (`bool`, *optional*):
                Whether or not to return the output attention of all layers
            return_dict (`bool`, *optional*):
                Whether or not to return a `ModelOutput` instead of a plain tuple.

        Returns:
            `PatchTSTForPredictionOutput` or tuple of `torch.Tensor` (if `return_dict`=False or
            `config.return_dict`=False)

        Examples:

        ```python
        >>> from huggingface_hub import hf_hub_download
        >>> import torch
        >>> from transformers import PatchTSTConfig, PatchTSTForPrediction

        >>> file = hf_hub_download(
        ...     repo_id="hf-internal-testing/etth1-hourly-batch", filename="train-batch.pt", repo_type="dataset"
        ... )
        >>> batch = torch.load(file)

        >>> # Prediction task with 7 input channels and prediction length is 96
        >>> model = PatchTSTForPrediction.from_pretrained("namctin/patchtst_etth1_forecast")

        >>> # during training, one provides both past and future values
        >>> outputs = model(
        ...     past_values=batch["past_values"],
        ...     future_values=batch["future_values"],
        ... )

        >>> loss = outputs.loss
        >>> loss.backward()

        >>> # during inference, one only provides past values, the model outputs future values
        >>> outputs = model(past_values=batch["past_values"])
        >>> prediction_outputs = outputs.prediction_outputs
        ```NTr  rE  rF  r   r  r   r!   )rJ  rQ  rL   r<  rE  rF  )r=   r  r   r  r;  r  rF  rE  distributionr\  rg  r   r  rP  rL   r<  )rJ   r   r  r  r9  rO   r  r  r  r  	y_hat_outr  rJ  rE  rF  r   s                   r3   ra   zPatchTSTForPrediction.forward4  sz   z &1%<kk$+B] zz#1!5/ " 
 
 		,899# 	FII 22\5EEI$' 	:#7DD|/|7I  E     |];;+H55zF3334	=99" 	 l\!B$%77G/7/CxkG++GN*(&4#.
 
 
 	
r5   c                 X   | j         j        } | |d|d          }| j        r^| j                            |j        |j        |j                  fdt          |          D             }t          j	        |d          }n|j        
                    d          }t          |          S )	a   
        Generate sequences of sample predictions from a model with a probability distribution head.

        Parameters:
            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.
            past_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:
            [`SamplePatchTSTOutput`] where the outputs `sequences` tensor will have shape `(batch_size, number of
            samples, prediction_length, 1)` or `(batch_size, number of samples, prediction_length, num_input_channels)`
            for multivariate predictions.
        NF)r   r  r  r9  r  c                 8    g | ]}                                 S r   sampler   r   r  s     r3   r   z2PatchTSTForPrediction.generate.<locals>.<listcomp>  s%    RRR|**,,RRRr5   r   r$   rW  )r=   num_parallel_samplesr  r  rQ  rE  rF  r  r)   r  r   rV  rJ   r   r  r  r   samplesr  s         @r3   generatezPatchTSTForPrediction.generate  s    2  ${? $#1!&	
 
 
 # 
	>3@@*7= A  L SRRRe<P6Q6QRRRGk'q111GG0::1==G#g6666r5   r  r   )rb   rc   rd   r   rB   r)   ri   r   rh   r   rj   rP  ra   no_gradrV  r  rk   rl   s   @r3   r  r    s>       ~      @ 6:04/3,0&*k
 k
\k
 %U\2k
  -	k

 'tnk
 $D>k
 d^k
 
u11	2k
 k
 k
 k
Z U]__ 6:-7 -7\-7 %U\2-7 
	-7 -7 -7 _-7 -7 -7 -7 -7r5   r  c                   @     e Zd ZdZddef fdZdej        fdZ xZ	S )PatchTSTRegressionHeadz
    Regression head
    Nr=   c                    t                                                       |j        | _        |j        | _        |j        | _        || _        |j        |j        z  }t          j
        d          | _        |j        dk    rt          j        |j                  nt          j                    | _        |!t          j        ||j                  | _        d S |                    |          | _        d S r  )rA   rB   output_rangey_ranger   r  r  r   rs   r   r  r  r  r   r   r   rE   r  r  r  )rJ   r=   r  rC   rK   s       r3   rB   zPatchTSTRegressionHead.__init__  s    *#1"/#6 ,v~=zA...:@:MPQ:Q:Qrz&"5666WYWbWdWd& i&2DEEDOOO1JJ8TTDOOOr5   r  c                    | j         r|dddddddf         }na| j        dk    r|                    d          }n?| j        dk    r|                    d          j        }nt          d| j         d          |                     |                     |                    }|                     |          }| j	        du | j
        duz  r>t          j        |          | j
        d	         | j
        d         z
  z  | j
        d         z   }|S )
aY  
        Parameters:
            embedding (`torch.Tensor` of shape `(bs, num_channels, num_patches, d_model)` or
                    `(bs, num_channels, num_patches+1, d_model)` if `cls_token` is set to True, *required*):
                Embedding from the model
        Returns:
            `torch.Tensor` of shape `(bs, output_dim)`

        Nr   r   r#   r$   r   r  r  r   )r   r  r   r   r  rD   r   r  r  r  r  r)   sigmoidr  s       r3   ra   zPatchTSTRegressionHead.forward  s'     
	](AAAq!!!4&(((~~!~44%''(}}}33:[1B[[[\\\  <<5E(F(FGG !122$,T1IJ 	c]6**dl1oQ.OPSWS_`aSbbFr5   r   rz   rl   s   @r3   r  r    sx         U U~ U U U U U U"        r5   r  z,
    The PatchTST for regression model.
    c                   0    e Zd Zdef fdZe	 	 	 	 	 ddej        deej                 deej                 dee	         dee	         d	ee	         d
e
eef         fd            Z ej                    	 ddej        deej                 d
efd            Z xZS )PatchTSTForRegressionr=   c                 V   t                                          |           |j        r!t                              d           d|_        t          |          | _        |j        dk    rd | _        n|j        dk    rt          |j
                  | _        nc|j        dk    rt          |j
                  | _        n=|j        dk    rt          |j
                  | _        nt          d|j                   t          || j                  | _        |                                  d S )	Nr  Fr  r  r$   r  r  r  )rA   rB   r  r  r  r  r   rJ  r  r   r  r   r   rD   r  r  r8  rv   s     r3   rB   zPatchTSTForRegression.__init__  s&        	)NNHIII#(F "6**
;%'+D$$)[88+9f>P+Q+Q+Q((+x77+7F<N+O+O+O((+/BBB+AfFX+Y+Y+Y(( !\@Z!\!\]]]*643KLL	 	r5   Nr   r  r  r9  rO   r  rP   c                      ||n j         j        }                     ||||d          }                     |j                  }d}	|} j        rU j                            |          }
t           fd|D                       }t          |
|          }	t          |	          }	n!t          j        d          }	 |	||          }	|s|f|dd         z   }|	|	f|z   n|}|S t          |	||j        |j        	          S )
a#  
        past_values (`torch.Tensor` of shape `(bs, sequence_length, num_input_channels)`, *required*):
            Input sequence to the model
        target_values (`torch.Tensor` of shape `(bs, num_input_channels)`):
            Target values associates with the `past_values`
        past_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).
            Whether or not to return a `ModelOutput` instead of a plain tuple.

        Examples:

        ```python
        >>> from transformers import PatchTSTConfig, PatchTSTForRegression

        >>> # Regression task with 6 input channels and regress 2 targets
        >>> model = PatchTSTForRegression.from_pretrained("namctin/patchtst_etth1_regression")

        >>> # during inference, one only provides past values, the model outputs future values
        >>> past_values = torch.randn(20, 512, 6)
        >>> outputs = model(past_values=past_values)
        >>> regression_outputs = outputs.regression_outputs
        ```NTr  c              3   X   K   | ]$}|                     d j        j                  V  %dS )r!   N)r.   r=   r  )r   itemrJ   s     r3   r  z0PatchTSTForRegression.forward.<locals>.<genexpr>_  s6      WWdiiDK,CDDWWWWWWr5   r   r  r   r   )rJ  rN  rL   r<  )r=   r  r   r  r;  r  r  rj   r\  rg  r   r  rM  rL   r<  )rJ   r   r  r  r9  rO   r  r  r  rJ  r  r   s   `           r3   ra   zPatchTSTForRegression.forward)  sG   J &1%<kk$+B]zz#1!5/ " 
 
 		,899$' 	2#7DDUKKWWWWQVWWWWW<77'--zF333tE=11 	had!33G+/+;tg''GN*$&4#.	
 
 
 	
r5   c                 8   | j         j        } | |d|d          }| j                            |j                  fdt          |          D             }t          j        |d                              d|| j         j	                  }t          |          S )	a  
        Generate sequences of sample predictions from a model with a probability distribution head.

        Parameters:
            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.
            past_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:
            [`SamplePatchTSTOutput`] where the outputs `sequences` tensor will have shape `(batch_size, number of
            samples, num_targets)`.
        NF)r   r  r  r9  c                 8    g | ]}                                 S r   r  r  s     r3   r   z2PatchTSTForRegression.generate.<locals>.<listcomp>  s%    NNNQ<&&((NNNr5   r   r$   r!   r  )r=   r  r  r  rN  r  r)   r  r.   r  rV  r  s         @r3   r  zPatchTSTForRegression.generates  s    0  ${? $#1!&	
 
 
 /<<W=WXXNNNN%8L2M2MNNN+g1---2227KT[Mdee#g6666r5   r  r   )rb   rc   rd   r   rB   r   r)   ri   r   rh   r   rj   rM  ra   r  rV  r  rk   rl   s   @r3   r  r  	  sI       ~      4  1559/3,0&*G
 G
\G
  -G
 %U\2	G

 'tnG
 $D>G
 d^G
 
u11	2G
 G
 G
 ^G
R U]__ 6:'7 '7\'7 %U\2'7 
	'7 '7 '7 _'7 '7 '7 '7 '7r5   r  )r  r   r  r  r  r  )Nr   N)NFr   r  rA  )Lre   r&  dataclassesr   typingr   r   r   r)   r   activationsr	   modeling_flash_attention_utilsr
   modeling_outputsr   modeling_utilsr   r   processing_utilsr   time_series_utilsr   r   r   utilsr   r   r   configuration_patchtstr   
get_loggerrb   r  r
  ri   rg   r4   r7   rn   listrh   rf   r   r   r   r   r   r   r  r   r  rD  rI  rM  rP  rS  rV  distributionsDistributionr\  rg  ri  ry  r  r  r  r  r  r  r  r  r  r  r  __all__r   r5   r3   <module>r     s	      ! ! ! ! ! ! , , , , , , , , , ,        " " " " " " B B B B B B / / / / / / F F F F F F F F & & & & & & U U U U U U U U U U 9 9 9 9 9 9 9 9 9 9 2 2 2 2 2 2 
	H	%	%  $(,% %I%<% 
% <	%
 U\*% e_% % %% % % %>U/ U/ U/ U/ U/	 U/ U/ U/p& & & & &	 & & &2 04',7% 7%L7%7% 'tn7% !%	7%
 7% 7% 7% 7%z 04	A% A%LA%$T3Y/A% 'tnA% 	A% A% A% A%H- - - - -ry - - -`9" 9" 9" 9" 9"bi 9" 9" 9"xH H H H H29 H H HV "2 "2 "2 "2 "2o "2 "2 "2J! ! ! ! !	 ! ! !H5 5 5 5 5 5 5 5p;x ;x ;x ;x ;x- ;x ;x ;x|   
4 4 4 4 4+ 4 4  46   
: : : : :; : :  :   
: : : : :+ : :  :   
. . . . .+ . .  .4   
: : : : :k : :  :   2 2 2 2 2; 2 2  2#u"/ # #%, # # # #* *5< *(5<:P *fkfr * * * *2 0  0  0  0  0	  0  0  0H3; 3; 3; 3; 3; 3; 3; 3;n         	      6         RY      8 m
 m
 m
 m
 m
+ m
 m
 m
`    ry   8   
l
 l
 l
 l
 l
4 l
 l
 
l
^" " " " " " " "J   
T
 T
 T
 T
 T
 7 T
 T
 
T
n   
] ] ] ] ]RY ] ] 
]@   
y7 y7 y7 y7 y73 y7 y7 
y7x4 4 4 4 4RY 4 4 4n   
M7 M7 M7 M7 M73 M7 M7 
M7`  r5   