
    謜i8                        d Z ddlmZ ddlZddlmZ ddlmZmZmZm	Z	m
Z
mZmZ  G d de      Z G d d	ej                        Z G d
 dej                        Z G d d      Z G d de      Z G d de      Z G d de      Zy)z:
Time series distributional output classes and utilities.
    )CallableN)nn)AffineTransformDistributionIndependentNegativeBinomialNormalStudentTTransformedDistributionc                   V     e Zd Zddef fdZed        Zed        Zed        Z xZ	S )AffineTransformedbase_distributionc                     |dn|| _         |dn|| _        t        |   |t	        | j                  | j                   |      g       y )Ng      ?        locscale	event_dim)r   r   super__init__r   )selfr   r   r   r   	__class__s        Z/mnt/e/genesis-system/.venv/lib/python3.12/site-packages/transformers/time_series_utils.pyr   zAffineTransformed.__init__#   sE    !MSu
+33*_QUQ[Q[gp-q,rs    c                 b    | j                   j                  | j                  z  | j                  z   S )z7
        Returns the mean of the distribution.
        )	base_distmeanr   r   r   s    r   r   zAffineTransformed.mean)   s&    
 ~~""TZZ/$((::r   c                 N    | j                   j                  | j                  dz  z  S )z;
        Returns the variance of the distribution.
           )r   variancer   r   s    r   r!   zAffineTransformed.variance0   s!    
 ~~&&Q66r   c                 6    | j                   j                         S )zE
        Returns the standard deviation of the distribution.
        )r!   sqrtr   s    r   stddevzAffineTransformed.stddev7   s    
 }}!!##r   )NNr   )
__name__
__module____qualname__r   r   propertyr   r!   r$   __classcell__r   s   @r   r   r   "   sM    t, t ; ; 7 7 $ $r   r   c            	            e Zd Zdedeeef   dedeej                     f   ddf fdZ
dej                  deej                     fd	Z xZS )
ParameterProjectionin_featuresargs_dim
domain_map.returnNc           	          t        |   di | || _        t        j                  |j                         D cg c]  }t        j                  ||       c}      | _        || _        y c c}w )N )	r   r   r.   r   
ModuleListvaluesLinearprojr/   )r   r-   r.   r/   kwargsdimr   s         r   r   zParameterProjection.__init__@   sV     	"6" MM(//J["\3299[##>"\]	$ #]s   A*xc                 h    | j                   D cg c]
  } ||       }} | j                  | S c c}w N)r6   r/   )r   r9   r6   params_unboundeds       r   forwardzParameterProjection.forwardH   s5    04		:DG::t 011 ;s   /)r%   r&   r'   intdictstrr   tupletorchTensorr   r=   r)   r*   s   @r   r,   r,   ?   sg    %%*.sCx.%FNsTYZ_ZfZfTgOgFh%	%2 2%*= 2r   r,   c                   $     e Zd Z fdZd Z xZS )LambdaLayerc                 0    t         |           || _        y r;   )r   r   function)r   rG   r   s     r   r   zLambdaLayer.__init__O   s     r   c                 (     | j                   |g| S r;   )rG   )r   r9   argss      r   r=   zLambdaLayer.forwardS   s    t}}Q&&&r   )r%   r&   r'   r   r=   r)   r*   s   @r   rE   rE   N   s    !'r   rE   c                   d   e Zd ZU eed<   eed<   eeef   ed<   ddeddfdZd Z		 	 dd	e
j                  dz  d
e
j                  dz  defdZedefd       Zedefd       Zedefd       Zdedej*                  fdZde
j                  fdZede
j                  de
j                  fd       Zy)DistributionOutputdistribution_classr-   r.   r8   r0   Nc                 |    || _         | j                  D ci c]  }||| j                  |   z   c}| _        y c c}w r;   )r8   r.   )r   r8   ks      r   r   zDistributionOutput.__init__\   s5    <@MMJqC$--"222JJs   9c                 p    | j                   dk(  r | j                  | S t         | j                  | d      S )N   r8   rL   r   )r   
distr_argss     r   _base_distributionz%DistributionOutput._base_distribution`   s;    88q=*4**J776t66
CQGGr   r   r   c                 b    | j                  |      }|||S t        |||| j                        S )Nr   )rS   r   r   )r   rR   r   r   distrs        r   distributionzDistributionOutput.distributionf   s7     ''
3;5=L$U5DNN[[r   c                 >    | j                   dk(  rdS | j                   fS )zo
        Shape of each individual event contemplated by the distributions that this object constructs.
        rP   r2   )r8   r   s    r   event_shapezDistributionOutput.event_shaper   s    
 XX]r33r   c                 ,    t        | j                        S )z
        Number of event dimensions, i.e., length of the `event_shape` tuple, of the distributions that this object
        constructs.
        )lenrX   r   s    r   r   zDistributionOutput.event_dimy   s     4##$$r   c                      y)z
        A float that will have a valid numeric value when computing the log-loss of the corresponding distribution. By
        default 0.0. This value will be used when padding data series.
        r   r2   r   s    r   value_in_supportz#DistributionOutput.value_in_support   s     r   c                 X    t        || j                  t        | j                              S )z~
        Return the parameter projection layer that maps the input to the appropriate parameters of the distribution.
        )r-   r.   r/   )r,   r.   rE   r/   )r   r-   s     r   get_parameter_projectionz+DistributionOutput.get_parameter_projection   s'     ##]]"4??3
 	
r   rI   c                     t               )a  
        Converts arguments to the right shape and domain. The domain depends on the type of distribution, while the
        correct shape is obtained by reshaping the trailing axis in such a way that the returned tensors define a
        distribution of the right event_shape.
        )NotImplementedError)r   rI   s     r   r/   zDistributionOutput.domain_map   s     "##r   r9   c                 d    | t        j                  t        j                  |       dz         z   dz  S )z
        Helper to map inputs to the positive orthant by applying the square-plus operation. Reference:
        https://twitter.com/jon_barron/status/1387167648669048833
        g      @       @)rB   r#   square)r9   s    r   
squarepluszDistributionOutput.squareplus   s*     EJJu||A455<<r   )rP   NN)r%   r&   r'   type__annotations__r>   r?   r@   r   rS   rB   rC   r   rV   r(   rA   rX   r   floatr\   r   Moduler^   r/   staticmethodrd   r2   r   r   rK   rK   W   s   38nKC K KH $(%)	
\ \\D 
\ ||d"	
\
 

\ 4U 4 4 %3 % % %  
C 
BII 
$ $ =ell =u|| = =r   rK   c                       e Zd ZU dZddddZeeef   ed<   e	Z
eed<   edej                  dej                  dej                  fd	       Zy
)StudentTOutputz.
    Student-T distribution output class.
    rP   )dfr   r   r.   rL   rm   r   r   c                    | j                  |      j                  t        j                  |j                        j
                        }d| j                  |      z   }|j                  d      |j                  d      |j                  d      fS )Nrb   rd   	clamp_minrB   finfodtypeepssqueeze)clsrm   r   r   s       r   r/   zStudentTOutput.domain_map   sg    u%//EKK0H0L0LM3>>"%%zz"~s{{2b0AAAr   N)r%   r&   r'   __doc__r.   r?   r@   r>   rg   r
   rL   rf   classmethodrB   rC   r/   r2   r   r   rl   rl      se     '(A>Hd38n>''BELL Bu|| BELL B Br   rl   c                       e Zd ZU dZdddZeeef   ed<   e	Z
eed<   edej                  dej                  fd       Zy	)
NormalOutputz+
    Normal distribution output class.
    rP   )r   r   r.   rL   r   r   c                     | j                  |      j                  t        j                  |j                        j
                        }|j                  d      |j                  d      fS Nro   rp   )rv   r   r   s      r   r/   zNormalOutput.domain_map   sJ    u%//EKK0H0L0LM{{2b 111r   N)r%   r&   r'   rw   r.   r?   r@   r>   rg   r	   rL   rf   rx   rB   rC   r/   r2   r   r   rz   rz      sS     ()15Hd38n5%%2U\\ 2%,, 2 2r   rz   c                       e Zd ZU dZdddZeeef   ed<   e	Z
eed<   edej                  dej                  fd       Zd	efd
Z	 ddej                  dz  dej                  dz  d	efdZy)NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rP   total_countlogitsr.   rL   r   r   c                 h    | j                  |      }|j                  d      |j                  d      fS r|   )rd   ru   )rv   r   r   s      r   r/   z!NegativeBinomialOutput.domain_map   s/    nn[1""2&r(:::r   r0   c                     |\  }}| j                   dk(  r| j                  ||      S t        | j                  ||      d      S )NrP   r   rQ   )r   rR   r   r   s       r   rS   z)NegativeBinomialOutput._base_distribution   sL    (V88q=**{6*RRt66;W]6^`abbr   Nr   r   c                 \    |\  }}|||j                         z  }| j                  ||f      S r;   )logrS   )r   rR   r   r   r   r   s         r   rV   z#NegativeBinomialOutput.distribution   s:     )Veiik!F&&V'<==r   re   )r%   r&   r'   rw   r.   r?   r@   r>   rg   r   rL   rf   rx   rB   rC   r/   r   rS   rV   r2   r   r   r~   r~      s     01A>Hd38n>//;U\\ ;5<< ; ;c c Y]	>$||d2	>BG,,QUBU	>		>r   r~   )rw   collections.abcr   rB   r   torch.distributionsr   r   r   r   r	   r
   r   r   ri   r,   rE   rK   rl   rz   r~   r2   r   r   <module>r      s    %    $/ $:2")) 2'")) 'J= J=ZB' B2% 2 >/  >r   