a
    hbT                     @   s  d dl mZmZmZ d dlZg dZG dd dZG dd deZdd	 ZG d
d de	eZ
G dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZG d d! d!eZG d"d# d#eZG d$d% d%eZG d&d' d'eZG d(d) d)eZG d*d+ d+eZG d,d- d-eZG d.d/ d/eZG d0d1 d1eZG d2d3 d3eZG d4d5 d5eZG d6d7 d7eZ G d8d9 d9eZ!G d:d; d;eZ"e Z#e
Z$eZ%e Z&e Z'ed Z(ed<Z)eZ*e Z+e%e+d<Z,ed=Z-ed=Z.eZ/eZ0eZ1eZ2ed=d>Z3eZ4eZ5e Z6e Z7e Z8e Z9e Z:e Z;e Z<e  Z=e!Z>e"Z?dS )?    )AnyCallableOptionalN) 
Constraintbooleancatcorr_cholesky	dependentdependent_propertygreater_thangreater_than_eqindependentinteger_intervalintervalhalf_open_intervalis_dependent	less_thanlower_choleskylower_triangularMixtureSameFamilyConstraintmultinomialnonnegativenonnegative_integerone_hotpositivepositive_semidefinitepositive_definitepositive_integerrealreal_vectorsimplexsquarestack	symmetricunit_intervalc                   @   s(   e Zd ZdZdZdZdd Zdd ZdS )	r   a  
    Abstract base class for constraints.

    A constraint object represents a region over which a variable is valid,
    e.g. within which a variable can be optimized.

    Attributes:
        is_discrete (bool): Whether constrained space is discrete.
            Defaults to False.
        event_dim (int): Number of rightmost dimensions that together define
            an event. The :meth:`check` method will remove this many dimensions
            when computing validity.
    Fr   c                 C   s   t dS )z
        Returns a byte tensor of ``sample_shape + batch_shape`` indicating
        whether each event in value satisfies this constraint.
        N)NotImplementedErrorselfvalue r)   M/var/www/auris/lib/python3.9/site-packages/torch/distributions/constraints.pychecka   s    zConstraint.checkc                 C   s   | j jdd  d S )N   z())	__class____name__r'   r)   r)   r*   __repr__h   s    zConstraint.__repr__N)r.   
__module____qualname____doc__is_discrete	event_dimr+   r0   r)   r)   r)   r*   r   O   s
   r   c                       sd   e Zd ZdZeed fdd
ZeedddZee	ddd	Z
eedd
dZdd Z  ZS )
_DependentaI  
    Placeholder for variables whose support depends on other variables.
    These variables obey no simple coordinate-wise constraints.

    Args:
        is_discrete (bool): Optional value of ``.is_discrete`` in case this
            can be computed statically. If not provided, access to the
            ``.is_discrete`` attribute will raise a NotImplementedError.
        event_dim (int): Optional value of ``.event_dim`` in case this
            can be computed statically. If not provided, access to the
            ``.event_dim`` attribute will raise a NotImplementedError.
    r4   r5   c                   s   || _ || _t   d S N)_is_discrete
_event_dimsuper__init__r'   r4   r5   r-   r)   r*   r<   z   s    z_Dependent.__init__returnc                 C   s   | j tu rtd| j S )Nz,.is_discrete cannot be determined statically)r9   NotImplementedr%   r/   r)   r)   r*   r4      s    
z_Dependent.is_discretec                 C   s   | j tu rtd| j S )Nz*.event_dim cannot be determined statically)r:   rA   r%   r/   r)   r)   r*   r5      s    
z_Dependent.event_dimc                C   s(   |t u r| j}|t u r| j}t||dS )z
        Support for syntax to customize static attributes::

            constraints.dependent(is_discrete=True, event_dim=1)
        r7   )rA   r9   r:   r6   r=   r)   r)   r*   __call__   s
    z_Dependent.__call__c                 C   s   t dd S )Nz1Cannot determine validity of dependent constraint)
ValueErrorr'   xr)   r)   r*   r+      s    z_Dependent.check)r.   r1   r2   r3   rA   r<   propertyboolr4   intr5   rB   r+   __classcell__r)   r)   r>   r*   r6   l   s   r6   c                 C   s
   t | tS )a  
    Checks if ``constraint`` is a ``_Dependent`` object.

    Args:
        constraint : A ``Constraint`` object.

    Returns:
        ``bool``: True if ``constraint`` can be refined to the type ``_Dependent``, False otherwise.

    Examples:
        >>> import torch
        >>> from torch.distributions import Bernoulli
        >>> from torch.distributions.constraints import is_dependent

        >>> dist = Bernoulli(probs=torch.tensor([0.6], requires_grad=True))
        >>> constraint1 = dist.arg_constraints["probs"]
        >>> constraint2 = dist.arg_constraints["logits"]

        >>> for constraint in [constraint1, constraint2]:
        >>>     if is_dependent(constraint):
        >>>         continue
    )
isinstancer6   )
constraintr)   r)   r*   r      s    r   c                       sb   e Zd ZdZdeedeedef  ee ee	 dd fddZ
edef d dd	d
Z  ZS )_DependentPropertya  
    Decorator that extends @property to act like a `Dependent` constraint when
    called on a class and act like a property when called on an object.

    Example::

        class Uniform(Distribution):
            def __init__(self, low, high):
                self.low = low
                self.high = high

            @constraints.dependent_property(is_discrete=False, event_dim=0)
            def support(self):
                return constraints.interval(self.low, self.high)

    Args:
        fn (Callable): The function to be decorated.
        is_discrete (bool): Optional value of ``.is_discrete`` in case this
            can be computed statically. If not provided, access to the
            ``.is_discrete`` attribute will raise a NotImplementedError.
        event_dim (int): Optional value of ``.event_dim`` in case this
            can be computed statically. If not provided, access to the
            ``.event_dim`` attribute will raise a NotImplementedError.
    Nr7   .)fnr4   r5   r@   c                   s   t  | || _|| _d S r8   )r;   r<   r9   r:   )r'   rM   r4   r5   r>   r)   r*   r<      s    z_DependentProperty.__init__)rM   r@   c                 C   s   t || j| jdS )z
        Support for syntax to customize static attributes::

            @constraints.dependent_property(is_discrete=True, event_dim=1)
            def support(self): ...
        r7   )rL   r9   r:   )r'   rM   r)   r)   r*   rB      s    
z_DependentProperty.__call__)N)r.   r1   r2   r3   rA   r   r   r   rG   rH   r<   rB   rI   r)   r)   r>   r*   rL      s    rL   c                       sT   e Zd ZdZ fddZeedddZeedddZ	d	d
 Z
dd Z  ZS )_IndependentConstraintz
    Wraps a constraint by aggregating over ``reinterpreted_batch_ndims``-many
    dims in :meth:`check`, so that an event is valid only if all its
    independent entries are valid.
    c                    sB   t |tsJ t |tsJ |dks(J || _|| _t   d S Nr   )rJ   r   rH   base_constraintreinterpreted_batch_ndimsr;   r<   )r'   rP   rQ   r>   r)   r*   r<      s    z_IndependentConstraint.__init__r?   c                 C   s   | j jS r8   rP   r4   r/   r)   r)   r*   r4      s    z"_IndependentConstraint.is_discretec                 C   s   | j j| j S r8   )rP   r5   rQ   r/   r)   r)   r*   r5      s    z _IndependentConstraint.event_dimc                 C   sp   | j |}| | jk r@| j j| j }td| d|  ||jd | | j  d }|d}|S )NExpected value.dim() >= 	 but got rV   )	rP   r+   dimrQ   r5   rC   reshapeshapeall)r'   r(   resultexpectedr)   r)   r*   r+      s    
z_IndependentConstraint.checkc                 C   s*   | j jdd   dt| j d| j dS )Nr,   (z, ))r-   r.   reprrP   rQ   r/   r)   r)   r*   r0   
  s    z_IndependentConstraint.__repr__r.   r1   r2   r3   r<   rF   rG   r4   rH   r5   r+   r0   rI   r)   r)   r>   r*   rN      s   rN   c                       sT   e Zd ZdZ fddZeedddZeedddZ	d	d
 Z
dd Z  ZS )r   a  
    Constraint for the :class:`~torch.distribution.MixtureSameFamily`
    distribution that adds back the rightmost batch dimension before
    performing the validity check with the component distribution
    constraint.

    Args:
        base_constraint: The ``Constraint`` object of
            the component distribution of
            the :class:`~torch.distribution.MixtureSameFamily` distribution.
    c                    s"   t |tsJ || _t   d S r8   )rJ   r   rP   r;   r<   )r'   rP   r>   r)   r*   r<     s    z$MixtureSameFamilyConstraint.__init__r?   c                 C   s   | j jS r8   rR   r/   r)   r)   r*   r4      s    z'MixtureSameFamilyConstraint.is_discretec                 C   s   | j jS r8   )rP   r5   r/   r)   r)   r*   r5   $  s    z%MixtureSameFamilyConstraint.event_dimc                 C   sx   | d| j }| j|}| | jk rDtd| j d|  | | j }||jd| d }|d}|S )z
        Check validity of ``value`` as a possible outcome of sampling
        the :class:`~torch.distribution.MixtureSameFamily` distribution.
        rV   rS   rT   NrU   )	Z	unsqueezer5   rP   r+   rW   rC   rX   rY   rZ   )r'   r(   Zunsqueezed_valuer[   Znum_dim_to_keepr)   r)   r*   r+   (  s    
z!MixtureSameFamilyConstraint.checkc                 C   s   | j j dt| j dS )Nr]   r^   )r-   r.   r_   rP   r/   r)   r)   r*   r0   8  s    z$MixtureSameFamilyConstraint.__repr__r`   r)   r)   r>   r*   r     s   r   c                   @   s   e Zd ZdZdZdd ZdS )_Booleanz/
    Constrain to the two values `{0, 1}`.
    Tc                 C   s   |dk|dkB S )Nr   r,   r)   r&   r)   r)   r*   r+   C  s    z_Boolean.checkN)r.   r1   r2   r3   r4   r+   r)   r)   r)   r*   ra   <  s   ra   c                   @   s    e Zd ZdZdZdZdd ZdS )_OneHotz'
    Constrain to one-hot vectors.
    Tr,   c                 C   s.   |dk|dkB }| dd}|d|@ S )Nr   r,   rV   )sumeqrZ   )r'   r(   Z
is_booleanis_normalizedr)   r)   r*   r+   O  s    z_OneHot.checkN)r.   r1   r2   r3   r4   r5   r+   r)   r)   r)   r*   rb   G  s   rb   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	_IntegerIntervalzH
    Constrain to an integer interval `[lower_bound, upper_bound]`.
    Tc                    s   || _ || _t   d S r8   lower_boundupper_boundr;   r<   r'   rh   ri   r>   r)   r*   r<   \  s    z_IntegerInterval.__init__c                 C   s    |d dk| j |k@ || jk@ S Nr,   r   rh   ri   r&   r)   r)   r*   r+   a  s    z_IntegerInterval.checkc                 C   s.   | j jdd  }|d| j d| j d7 }|S Nr,   (lower_bound=z, upper_bound=r^   r-   r.   rh   ri   r'   Z
fmt_stringr)   r)   r*   r0   f  s
    z_IntegerInterval.__repr__	r.   r1   r2   r3   r4   r<   r+   r0   rI   r)   r)   r>   r*   rf   U  s
   rf   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	_IntegerLessThanzA
    Constrain to an integer interval `(-inf, upper_bound]`.
    Tc                    s   || _ t   d S r8   ri   r;   r<   r'   ri   r>   r)   r*   r<   u  s    z_IntegerLessThan.__init__c                 C   s   |d dk|| j k@ S rk   ri   r&   r)   r)   r*   r+   y  s    z_IntegerLessThan.checkc                 C   s&   | j jdd  }|d| j d7 }|S Nr,   z(upper_bound=r^   r-   r.   ri   rp   r)   r)   r*   r0   |  s    z_IntegerLessThan.__repr__rq   r)   r)   r>   r*   rr   n  s
   rr   c                       s4   e Zd ZdZdZ fddZdd Zdd Z  ZS )	_IntegerGreaterThanz@
    Constrain to an integer interval `[lower_bound, inf)`.
    Tc                    s   || _ t   d S r8   rh   r;   r<   r'   rh   r>   r)   r*   r<     s    z_IntegerGreaterThan.__init__c                 C   s   |d dk|| j k@ S rk   rh   r&   r)   r)   r*   r+     s    z_IntegerGreaterThan.checkc                 C   s&   | j jdd  }|d| j d7 }|S Nr,   rn   r^   r-   r.   rh   rp   r)   r)   r*   r0     s    z_IntegerGreaterThan.__repr__rq   r)   r)   r>   r*   rx     s
   rx   c                   @   s   e Zd ZdZdd ZdS )_RealzF
    Trivially constrain to the extended real line `[-inf, inf]`.
    c                 C   s   ||kS r8   r)   r&   r)   r)   r*   r+     s    z_Real.checkN)r.   r1   r2   r3   r+   r)   r)   r)   r*   r~     s   r~   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )_GreaterThanz=
    Constrain to a real half line `(lower_bound, inf]`.
    c                    s   || _ t   d S r8   ry   rz   r>   r)   r*   r<     s    z_GreaterThan.__init__c                 C   s
   | j |k S r8   r{   r&   r)   r)   r*   r+     s    z_GreaterThan.checkc                 C   s&   | j jdd  }|d| j d7 }|S r|   r}   rp   r)   r)   r*   r0     s    z_GreaterThan.__repr__r.   r1   r2   r3   r<   r+   r0   rI   r)   r)   r>   r*   r     s   r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )_GreaterThanEqz=
    Constrain to a real half line `[lower_bound, inf)`.
    c                    s   || _ t   d S r8   ry   rz   r>   r)   r*   r<     s    z_GreaterThanEq.__init__c                 C   s
   | j |kS r8   r{   r&   r)   r)   r*   r+     s    z_GreaterThanEq.checkc                 C   s&   | j jdd  }|d| j d7 }|S r|   r}   rp   r)   r)   r*   r0     s    z_GreaterThanEq.__repr__r   r)   r)   r>   r*   r     s   r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )	_LessThanz>
    Constrain to a real half line `[-inf, upper_bound)`.
    c                    s   || _ t   d S r8   rs   rt   r>   r)   r*   r<     s    z_LessThan.__init__c                 C   s
   || j k S r8   ru   r&   r)   r)   r*   r+     s    z_LessThan.checkc                 C   s&   | j jdd  }|d| j d7 }|S rv   rw   rp   r)   r)   r*   r0     s    z_LessThan.__repr__r   r)   r)   r>   r*   r     s   r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )	_IntervalzD
    Constrain to a real interval `[lower_bound, upper_bound]`.
    c                    s   || _ || _t   d S r8   rg   rj   r>   r)   r*   r<     s    z_Interval.__init__c                 C   s   | j |k|| jk@ S r8   rl   r&   r)   r)   r*   r+     s    z_Interval.checkc                 C   s.   | j jdd  }|d| j d| j d7 }|S rm   ro   rp   r)   r)   r*   r0     s
    z_Interval.__repr__r   r)   r)   r>   r*   r     s   r   c                       s0   e Zd ZdZ fddZdd Zdd Z  ZS )_HalfOpenIntervalzD
    Constrain to a real interval `[lower_bound, upper_bound)`.
    c                    s   || _ || _t   d S r8   rg   rj   r>   r)   r*   r<     s    z_HalfOpenInterval.__init__c                 C   s   | j |k|| jk @ S r8   rl   r&   r)   r)   r*   r+     s    z_HalfOpenInterval.checkc                 C   s.   | j jdd  }|d| j d| j d7 }|S rm   ro   rp   r)   r)   r*   r0     s
    z_HalfOpenInterval.__repr__r   r)   r)   r>   r*   r     s   r   c                   @   s   e Zd ZdZdZdd ZdS )_Simplexz
    Constrain to the unit simplex in the innermost (rightmost) dimension.
    Specifically: `x >= 0` and `x.sum(-1) == 1`.
    r,   c                 C   s(   t j|dkdd|dd  dk @ S )Nr   rV   rW   r,   ư>)torchrZ   rc   absr&   r)   r)   r*   r+     s    z_Simplex.checkNr.   r1   r2   r3   r5   r+   r)   r)   r)   r*   r     s   r   c                   @   s(   e Zd ZdZdZdZdd Zdd ZdS )	_Multinomiala3  
    Constrain to nonnegative integer values summing to at most an upper bound.

    Note due to limitations of the Multinomial distribution, this currently
    checks the weaker condition ``value.sum(-1) <= upper_bound``. In the future
    this may be strengthened to ``value.sum(-1) == upper_bound``.
    Tr,   c                 C   s
   || _ d S r8   ru   rt   r)   r)   r*   r<     s    z_Multinomial.__init__c                 C   s"   |dkj dd|jdd| jk@ S )Nr   rV   r   )rZ   rc   ri   rD   r)   r)   r*   r+     s    z_Multinomial.checkN)r.   r1   r2   r3   r4   r5   r<   r+   r)   r)   r)   r*   r     s
   r   c                   @   s   e Zd ZdZdZdd ZdS )_LowerTriangularz8
    Constrain to lower-triangular square matrices.
       c                 C   s.   |  }||k|jd d d dd S )NrU   rV   r   )trilviewrY   min)r'   r(   
value_trilr)   r)   r*   r+   %  s    z_LowerTriangular.checkNr   r)   r)   r)   r*   r     s   r   c                   @   s   e Zd ZdZdZdd ZdS )_LowerCholeskyzP
    Constrain to lower-triangular square matrices with positive diagonals.
    r   c                 C   sR   |  }||k|jd d d dd }|jddddkdd }||@ S )Nr   rU   rV   r   )Zdim1Zdim2)r   r   rY   r   Zdiagonal)r'   r(   r   r   Zpositive_diagonalr)   r)   r*   r+   1  s
    $z_LowerCholesky.checkNr   r)   r)   r)   r*   r   *  s   r   c                   @   s   e Zd ZdZdZdd ZdS )_CorrCholeskyz}
    Constrain to lower-triangular square matrices with positive diagonals and each
    row vector being of unit length.
    r   c                 C   sZ   t |jj|d d }t jj| dd}|d  	|j
dd}t ||@ S )NrV   
   r         ?)r   ZfinfodtypeZepssizelinalgZnormdetachr   lerZ   r   r+   )r'   r(   ZtolZrow_normZunit_row_normr)   r)   r*   r+   C  s
    z_CorrCholesky.checkNr   r)   r)   r)   r*   r   ;  s   r   c                   @   s   e Zd ZdZdZdd ZdS )_Squarez'
    Constrain to square matrices.
    r   c                 C   s0   t j|jd d |jd |jd kt j|jdS )Nr   rV   )r   Z
fill_valuer   device)r   fullrY   rG   r   r&   r)   r)   r*   r+   S  s    z_Square.checkNr   r)   r)   r)   r*   r   L  s   r   c                       s    e Zd ZdZ fddZ  ZS )
_Symmetricz1
    Constrain to Symmetric square matrices.
    c                    s6   t  |}| s|S tj||jddddS )Nr   )Zatolr   rV   )r;   r+   rZ   r   iscloseZmT)r'   r(   Zsquare_checkr>   r)   r*   r+   a  s    z_Symmetric.checkr.   r1   r2   r3   r+   rI   r)   r)   r>   r*   r   \  s   r   c                       s    e Zd ZdZ fddZ  ZS )_PositiveSemidefinitez6
    Constrain to positive-semidefinite matrices.
    c                    s0   t  |}| s|S tj|ddS )Nr   rV   )r;   r+   rZ   r   r   Zeigvalshger'   r(   Z	sym_checkr>   r)   r*   r+   m  s    z_PositiveSemidefinite.checkr   r)   r)   r>   r*   r   h  s   r   c                       s    e Zd ZdZ fddZ  ZS )_PositiveDefinitez2
    Constrain to positive-definite matrices.
    c                    s,   t  |}| s|S tj|jdS rO   )r;   r+   rZ   r   r   Zcholesky_exinford   r   r>   r)   r*   r+   y  s    z_PositiveDefinite.checkr   r)   r)   r>   r*   r   t  s   r   c                       sN   e Zd ZdZd fdd	ZeedddZeedd	d
Z	dd Z
  ZS )_Catz
    Constraint functor that applies a sequence of constraints
    `cseq` at the submatrices at dimension `dim`,
    each of size `lengths[dim]`, in a way compatible with :func:`torch.cat`.
    r   Nc                    sn   t dd |D sJ t|| _|d u r8dgt| j }t|| _t| jt| jksZJ || _t   d S )Nc                 s   s   | ]}t |tV  qd S r8   rJ   r   .0cr)   r)   r*   	<genexpr>      z _Cat.__init__.<locals>.<genexpr>r,   )rZ   listcseqlenlengthsrW   r;   r<   )r'   r   rW   r   r>   r)   r*   r<     s    

z_Cat.__init__r?   c                 C   s   t dd | jD S )Nc                 s   s   | ]}|j V  qd S r8   r4   r   r)   r)   r*   r     r   z#_Cat.is_discrete.<locals>.<genexpr>anyr   r/   r)   r)   r*   r4     s    z_Cat.is_discretec                 C   s   t dd | jD S )Nc                 s   s   | ]}|j V  qd S r8   r5   r   r)   r)   r*   r     r   z!_Cat.event_dim.<locals>.<genexpr>)maxr   r/   r)   r)   r*   r5     s    z_Cat.event_dimc                 C   s|   |   | j   kr |  k s&n J g }d}t| j| jD ]0\}}|| j ||}||| || }q<t|| j S rO   )	rW   zipr   r   Znarrowappendr+   r   r   )r'   r(   Zchecksstartconstrlengthvr)   r)   r*   r+     s    &
z
_Cat.check)r   Nr.   r1   r2   r3   r<   rF   rG   r4   rH   r5   r+   rI   r)   r)   r>   r*   r     s   
r   c                       sN   e Zd ZdZd fdd	ZeedddZeeddd	Z	d
d Z
  ZS )_Stackz
    Constraint functor that applies a sequence of constraints
    `cseq` at the submatrices at dimension `dim`,
    in a way compatible with :func:`torch.stack`.
    r   c                    s4   t dd |D sJ t|| _|| _t   d S )Nc                 s   s   | ]}t |tV  qd S r8   r   r   r)   r)   r*   r     r   z"_Stack.__init__.<locals>.<genexpr>)rZ   r   r   rW   r;   r<   )r'   r   rW   r>   r)   r*   r<     s    
z_Stack.__init__r?   c                 C   s   t dd | jD S )Nc                 s   s   | ]}|j V  qd S r8   r   r   r)   r)   r*   r     r   z%_Stack.is_discrete.<locals>.<genexpr>r   r/   r)   r)   r*   r4     s    z_Stack.is_discretec                 C   s.   t dd | jD }| j| dk r*|d7 }|S )Nc                 s   s   | ]}|j V  qd S r8   r   r   r)   r)   r*   r     r   z#_Stack.event_dim.<locals>.<genexpr>r   r,   )r   r   rW   )r'   rW   r)   r)   r*   r5     s    z_Stack.event_dimc                    sf       j   kr   k s&n J  fddt j D }tdd t| jD  j S )Nc                    s   g | ]}  j|qS r)   )selectrW   )r   ir&   r)   r*   
<listcomp>  r   z _Stack.check.<locals>.<listcomp>c                 S   s   g | ]\}}| |qS r)   )r+   )r   r   r   r)   r)   r*   r     r   )rW   ranger   r   r"   r   r   )r'   r(   vsr)   r&   r*   r+     s
    & z_Stack.check)r   r   r)   r)   r>   r*   r     s   r   r,   g        r   )@typingr   r   r   r   __all__r   r6   r   rF   rL   rN   r   ra   rb   rf   rr   rx   r~   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r$   r   r   r    r   r   r   r!   r#   r   r   r   r"   r)   r)   r)   r*   <module>   sv   %$/1(.	$!

