o
    ZhE                     @   s   d Z ddl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 deZG dd	 d	ejZG d
d dejZG dd dZG dd deZG dd deZG dd deZdS )z:
Time series distributional output classes and utilities.
    )CallableOptionalN)nn)AffineTransformDistributionIndependentNegativeBinomialNormalStudentTTransformedDistributionc                       sH   e Zd Zddef fddZedd Zedd	 Zed
d Z  Z	S )AffineTransformedNr   base_distributionc                    sF   |d u rdn|| _ |d u rdn|| _t |t| j| j |dg d S )Ng      ?        locscale	event_dim)r   r   super__init__r   )selfr   r   r   r   	__class__ M/var/www/auris/lib/python3.10/site-packages/transformers/time_series_utils.pyr   #   s   "zAffineTransformed.__init__c                 C   s   | j j| j | j S )z7
        Returns the mean of the distribution.
        )	base_distmeanr   r   r   r   r   r   r   )   s   zAffineTransformed.meanc                 C   s   | j j| jd  S )z;
        Returns the variance of the distribution.
           )r   variancer   r   r   r   r   r   0   s   zAffineTransformed.variancec                 C   s
   | j  S )zE
        Returns the standard deviation of the distribution.
        )r   sqrtr   r   r   r   stddev7   s   
zAffineTransformed.stddev)NNr   )
__name__
__module____qualname__r   r   propertyr   r   r    __classcell__r   r   r   r   r   "   s    

r   c                	       s^   e Zd Zdedeeef dedeej	 f ddf fddZ
d	ej	deej	 fd
dZ  ZS )ParameterProjectionin_featuresargs_dim
domain_map.returnNc                    s@   t  jdi | || _t fdd| D | _|| _d S )Nc                    s   g | ]}t  |qS r   )r   ZLinear).0dimr'   r   r   
<listcomp>E   s    z0ParameterProjection.__init__.<locals>.<listcomp>r   )r   r   r(   r   Z
ModuleListvaluesprojr)   )r   r'   r(   r)   kwargsr   r-   r   r   @   s   
zParameterProjection.__init__xc                    s    fdd| j D }| j| S )Nc                    s   g | ]}| qS r   r   )r+   r0   r2   r   r   r.   I   s    z/ParameterProjection.forward.<locals>.<listcomp>)r0   r)   )r   r2   Zparams_unboundedr   r3   r   forwardH   s   
zParameterProjection.forward)r!   r"   r#   intdictstrr   tupletorchTensorr   r4   r%   r   r   r   r   r&   ?   s    
"r&   c                       s$   e Zd Z fddZdd Z  ZS )LambdaLayerc                    s   t    || _d S N)r   r   function)r   r=   r   r   r   r   O   s   

zLambdaLayer.__init__c                 G   s   | j |g|R  S r<   )r=   )r   r2   argsr   r   r   r4   S   s   zLambdaLayer.forward)r!   r"   r#   r   r4   r%   r   r   r   r   r;   N   s    r;   c                   @   s   e Zd ZU eed< eed< eeef ed< ddeddfdd	Zd
d Z			dde
ej de
ej defddZedefddZedefddZedefddZdedejfddZdejfddZedejdejfddZdS ) DistributionOutputdistribution_classr'   r(      r,   r*   Nc                    s"    _  fddjD _d S )Nc                    s   i | ]
}| j |  qS r   )r(   )r+   kr,   r   r   r   
<dictcomp>^   s    z/DistributionOutput.__init__.<locals>.<dictcomp>)r,   r(   )r   r,   r   rC   r   r   \   s   zDistributionOutput.__init__c                 C   s$   | j dkr
| j| S t| j| dS )NrA   r,   r@   r   )r   
distr_argsr   r   r   _base_distribution`   s   

z%DistributionOutput._base_distributionr   r   c                 C   s0   |  |}|d u r|d u r|S t|||| jdS )Nr   )rG   r   r   )r   rF   r   r   Zdistrr   r   r   distributionf   s   
zDistributionOutput.distributionc                 C   s   | j dkrdS | j fS )zo
        Shape of each individual event contemplated by the distributions that this object constructs.
        rA   r   )r,   r   r   r   r   event_shaper   s   zDistributionOutput.event_shapec                 C   s
   t | jS )z
        Number of event dimensions, i.e., length of the `event_shape` tuple, of the distributions that this object
        constructs.
        )lenrI   r   r   r   r   r   y   s   
zDistributionOutput.event_dimc                 C   s   dS )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   r   r   r   r   r   value_in_support   s   z#DistributionOutput.value_in_supportc                 C   s   t || jt| jdS )z~
        Return the parameter projection layer that maps the input to the appropriate parameters of the distribution.
        )r'   r(   r)   )r&   r(   r;   r)   )r   r'   r   r   r   get_parameter_projection   s
   z+DistributionOutput.get_parameter_projectionr>   c                 G   s   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   r>   r   r   r   r)      s   zDistributionOutput.domain_mapr2   c                 C   s   | t t | d  d 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      @       @)r9   r   Zsquarer3   r   r   r   
squareplus   s   zDistributionOutput.squareplus)rA   NN)r!   r"   r#   type__annotations__r5   r6   r7   r   rG   r   r9   r:   r   rH   r$   r8   rI   r   floatrK   r   ModulerL   r)   staticmethodrO   r   r   r   r   r?   W   s2   
 	

r?   c                   @   sZ   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	d
ZdS )StudentTOutputz.
    Student-T distribution output class.
    rA   )dfr   r   r(   r@   rW   r   r   c                 C   sD   |  |t|jj}d|  | }|d|d|dfS )NrN   rO   Z	clamp_minr9   ZfinfoZdtypeZepssqueeze)clsrW   r   r   r   r   r   r)      s   zStudentTOutput.domain_mapN)r!   r"   r#   __doc__r(   r6   r7   r5   rR   r
   r@   rQ   classmethodr9   r:   r)   r   r   r   r   rV      s   
 "rV   c                   @   sR   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d	Zd
S )NormalOutputz+
    Normal distribution output class.
    rA   )r   r   r(   r@   r   r   c                 C   s.   |  |t|jj}|d|dfS NrX   rY   )r[   r   r   r   r   r   r)      s   zNormalOutput.domain_mapN)r!   r"   r#   r\   r(   r6   r7   r5   rR   r	   r@   rQ   r]   r9   r:   r)   r   r   r   r   r^      s   
 r^   c                   @   s   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d	Zd
efddZ	ddeej deej d
efddZdS )NegativeBinomialOutputz6
    Negative Binomial distribution output class.
    rA   total_countlogitsr(   r@   rb   rc   c                 C   s   |  |}|d|dfS r_   )rO   rZ   )r[   rb   rc   r   r   r   r)      s   
z!NegativeBinomialOutput.domain_mapr*   c                 C   s4   |\}}| j dkr| j||dS t| j||ddS )NrA   ra   rE   )r   rF   rb   rc   r   r   r   rG      s   
z)NegativeBinomialOutput._base_distributionNr   r   c                 C   s*   |\}}|d ur||  7 }| ||fS r<   )logrG   )r   rF   r   r   rb   rc   r   r   r   rH      s   z#NegativeBinomialOutput.distributionrP   )r!   r"   r#   r\   r(   r6   r7   r5   rR   r   r@   rQ   r]   r9   r:   r)   r   rG   r   rH   r   r   r   r   r`      s   
 r`   )r\   typingr   r   r9   r   Ztorch.distributionsr   r   r   r   r	   r
   r   r   rT   r&   r;   r?   rV   r^   r`   r   r   r   r   <module>   s   $	M