o
    hE                     @   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__ r/var/www/html/construction_image-detection-poc/venv/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   Linear).0dimr'   r   r   
<listcomp>E   s    z0ParameterProjection.__init__.<locals>.<listcomp>r   )r   r   r(   r   
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,   r2   r4   r   r   r/   I   s    z/ParameterProjection.forward.<locals>.<listcomp>)r2   r)   )r   r4   params_unboundedr   r5   r   forwardH   s   
zParameterProjection.forward)r!   r"   r#   intdictstrr   tupletorchTensorr   r7   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   r4   argsr   r   r   r7   S   s   zLambdaLayer.forward)r!   r"   r#   r   r7   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   rF   r   r   \   s   zDistributionOutput.__init__c                 C   s$   | j dkr
| j| S t| j| dS )NrD   r-   rC   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   )rJ   r   r   )r   rI   r   r   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.
        rD   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.
        )lenrM   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_projectionrA   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   rA   r   r   r   r)      s   zDistributionOutput.domain_mapr4   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      @       @)r<   r   squarer5   r   r   r   
squareplus   s   zDistributionOutput.squareplus)rD   NN)r!   r"   r#   type__annotations__r8   r9   r:   r   rJ   r   r<   r=   r   rL   r$   r;   rM   r   floatrO   r   ModulerP   r)   staticmethodrT   r   r   r   r   rB   W   s2   
 	

rB   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.
    rD   )dfr   r   r(   rC   r\   r   r   c                 C   sD   |  |t|jj}d|  | }|d|d|dfS )NrR   rT   	clamp_minr<   finfodtypeepssqueeze)clsr\   r   r   r   r   r   r)      s   zStudentTOutput.domain_mapN)r!   r"   r#   __doc__r(   r9   r:   r8   rW   r
   rC   rV   classmethodr<   r=   r)   r   r   r   r   r[      s   
 "r[   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.
    rD   )r   r   r(   rC   r   r   c                 C   s.   |  |t|jj}|d|dfS Nr]   r^   )rd   r   r   r   r   r   r)      s   zNormalOutput.domain_mapN)r!   r"   r#   re   r(   r9   r:   r8   rW   r	   rC   rV   rf   r<   r=   r)   r   r   r   r   rg      s   
 rg   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.
    rD   total_countlogitsr(   rC   rk   rl   c                 C   s   |  |}|d|dfS rh   )rT   rc   )rd   rk   rl   r   r   r   r)      s   
z!NegativeBinomialOutput.domain_mapr*   c                 C   s4   |\}}| j dkr| j||dS t| j||ddS )NrD   rj   rH   )r   rI   rk   rl   r   r   r   rJ      s   
z)NegativeBinomialOutput._base_distributionNr   r   c                 C   s*   |\}}|d ur||  7 }| ||fS r?   )logrJ   )r   rI   r   r   rk   rl   r   r   r   rL      s   z#NegativeBinomialOutput.distributionrU   )r!   r"   r#   re   r(   r9   r:   r8   rW   r   rC   rV   rf   r<   r=   r)   r   rJ   r   rL   r   r   r   r   ri      s   
 ri   )re   typingr   r   r<   r   torch.distributionsr   r   r   r   r	   r
   r   r   rY   r&   r>   rB   r[   rg   ri   r   r   r   r   <module>   s   $	M