
    sga                        d Z ddlmZmZmZ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.
    )CallableDictOptionalTupleN)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        Q/var/www/html/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.variance1   s!    
 ~~&&Q66r   c                 6    | j                   j                         S )zE
        Returns the standard deviation of the distribution.
        )r$   sqrtr!   s    r   stddevzAffineTransformed.stddev8   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   r1   r   
ModuleListvaluesLinearprojr2   )r   r0   r1   r2   kwargsdimr   s         r   r   zParameterProjection.__init__A   sV     	"6" MM(//J["\3299[##>"\]	$ #]s   A*xc                 h    | j                   D cg c]
  } ||       }} | j                  | S c c}w N)r9   r2   )r   r<   r9   params_unboundeds       r   forwardzParameterProjection.forwardI   s5    04		:DG::t 011 ;s   /)r(   r)   r*   intr   strr   r   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   rH   r   s     r   r   zLambdaLayer.__init__P   s     r   c                 (     | j                   |g| S r>   )rH   )r   r<   argss      r   r@   zLambdaLayer.forwardT   s    t}}Q&&&r   )r(   r)   r*   r   r@   r,   r-   s   @r   rF   rF   O   s    !'r   rF   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
ej                     d
e
ej                     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_classr0   r1   r;   r3   Nc                 |    || _         | j                  D ci c]  }||| j                  |   z   c}| _        y c c}w r>   )r;   r1   )r   r;   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   r;   rM   r
   )r   
distr_argss     r   _base_distributionz%DistributionOutput._base_distributiona   s;    88q=*4**J776t66
CQGGr   r   r   c                 b    | j                  |      }|||S t        |||| j                        S )Nr   )rT   r   r   )r   rS   r   r   distrs        r   distributionzDistributionOutput.distributiong   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.
        rQ   r5   )r;   r!   s    r   event_shapezDistributionOutput.event_shapes   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.
        )lenrY   r!   s    r   r   zDistributionOutput.event_dimz   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   r5   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.
        )r0   r1   r2   )r/   r1   rF   r2   )r   r0   s     r   get_parameter_projectionz+DistributionOutput.get_parameter_projection   s'     ##]]"4??3
 	
r   rJ   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   rJ   s     r   r2   zDistributionOutput.domain_map   s     "##r   r<   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      @       @)rC   r&   square)r<   s    r   
squarepluszDistributionOutput.squareplus   s*     EJJu||A455<<r   )rQ   NN)r(   r)   r*   type__annotations__rA   r   rB   r   rT   r   rC   rD   r	   rW   r+   r   rY   r   floatr]   r   Moduler_   r2   staticmethodre   r5   r   r   rL   rL   X   s   38nKC K KH '+(,	
\ ell#
\ %	
\
 

\ 4U 4 4 %3 % % %  
C 
BII 
$ $ =ell =u|| = =r   rL   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.
    rQ   )dfr   r   r1   rM   rn   r   r   c                    | j                  |      j                  t        j                  |j                        j
                        }d| j                  |      z   }|j                  d      |j                  d      |j                  d      fS )Nrc   re   	clamp_minrC   finfodtypeepssqueeze)clsrn   r   r   s       r   r2   zStudentTOutput.domain_map   sg    u%//EKK0H0L0LM3>>"%%zz"~s{{2b0AAAr   N)r(   r)   r*   __doc__r1   r   rB   rA   rh   r   rM   rg   classmethodrC   rD   r2   r5   r   r   rm   rm      se     '(A>Hd38n>''BELL Bu|| BELL B Br   rm   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.
    rQ   )r   r   r1   rM   r   r   c                     | j                  |      j                  t        j                  |j                        j
                        }|j                  d      |j                  d      fS Nrp   rq   )rw   r   r   s      r   r2   zNormalOutput.domain_map   sJ    u%//EKK0H0L0LM{{2b 111r   N)r(   r)   r*   rx   r1   r   rB   rA   rh   r   rM   rg   ry   rC   rD   r2   r5   r   r   r{   r{      sS     ()15Hd38n5%%2U\\ 2%,, 2 2r   r{   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ej                     deej                     d	efdZy)NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rQ   total_countlogitsr1   rM   r   r   c                 h    | j                  |      }|j                  d      |j                  d      fS r}   )re   rv   )rw   r   r   s      r   r2   z!NegativeBinomialOutput.domain_map   s/    nn[1""2&r(:::r   r3   c                     |\  }}| j                   dk(  r| j                  ||      S t        | j                  ||      d      S )NrQ   r   rR   )r   rS   r   r   s       r   rT   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>   )logrT   )r   rS   r   r   r   r   s         r   rW   z#NegativeBinomialOutput.distribution   s:     )Veiik!F&&V'<==r   rf   )r(   r)   r*   rx   r1   r   rB   rA   rh   r   rM   rg   ry   rC   rD   r2   r	   rT   r   rW   r5   r   r   r   r      s     01A>Hd38n>//;U\\ ;5<< ; ;c c _c	>'5	>EMellE[	>		>r   r   )rx   typingr   r   r   r   rC   r   torch.distributionsr   r	   r
   r   r   r   r   r   rj   r/   rF   rL   rm   r{   r   r5   r   r   <module>r      s     3 2    $/ $:2")) 2'")) 'J= J=ZB' B2% 2 >/  >r   