a
    –¥»aˆC  ã                   @   s(  d Z ddlZg d¢ZG dd„ deƒ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eƒ ZeZe	Ze
ƒ Z eƒ Z!edƒZ"ed4ƒZ#eZ$eƒ Z%ee%d4ƒZ&ed5ƒZ'ed5ƒZ(eZ)eZ*eZ+eZ,ed5d6ƒZ-eZ.eZ/eƒ Z0eƒ Z1eƒ Z2eƒ Z3eƒ Z4eZ5eZ6dS )7a_  
The following constraints are implemented:

- ``constraints.boolean``
- ``constraints.cat``
- ``constraints.corr_cholesky``
- ``constraints.dependent``
- ``constraints.greater_than(lower_bound)``
- ``constraints.greater_than_eq(lower_bound)``
- ``constraints.independent(constraint, reinterpreted_batch_ndims)``
- ``constraints.integer_interval(lower_bound, upper_bound)``
- ``constraints.interval(lower_bound, upper_bound)``
- ``constraints.less_than(upper_bound)``
- ``constraints.lower_cholesky``
- ``constraints.lower_triangular``
- ``constraints.multinomial``
- ``constraints.nonnegative_integer``
- ``constraints.one_hot``
- ``constraints.positive_definite``
- ``constraints.positive_integer``
- ``constraints.positive``
- ``constraints.real_vector``
- ``constraints.real``
- ``constraints.simplex``
- ``constraints.stack``
- ``constraints.unit_interval``
é    N)Ú
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ÚmultinomialÚnonnegative_integerÚpositiveÚpositive_definiteÚpositive_integerÚrealÚreal_vectorÚsimplexÚstackÚ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    úY/home/yuhw/wc/pochoir/venv/lib/python3.9/site-packages/torch/distributions/constraints.pyÚcheckN   s    zConstraint.checkc                 C   s   | j jdd … d S )Né   z())Ú	__class__Ú__name__©r   r    r    r!   Ú__repr__U   s    zConstraint.__repr__N)r%   Ú
__module__Ú__qualname__Ú__doc__Úis_discreteÚ	event_dimr"   r'   r    r    r    r!   r   =   s
   r   c                       sX   e Zd ZdZeedœ‡ fdd„
Zedd„ ƒZe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.
    ©r+   r,   c                   s   || _ || _tƒ  ¡  d S ©N)Ú_is_discreteÚ
_event_dimÚsuperÚ__init__©r   r+   r,   ©r$   r    r!   r3   f   s    z_Dependent.__init__c                 C   s   | j tu rtdƒ‚| j S )Nz,.is_discrete cannot be determined statically)r0   ÚNotImplementedr   r&   r    r    r!   r+   k   s    
z_Dependent.is_discretec                 C   s   | j tu rtdƒ‚| j S )Nz*.event_dim cannot be determined statically)r1   r6   r   r&   r    r    r!   r,   q   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)
        r.   )r6   r0   r1   r-   r4   r    r    r!   Ú__call__w   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%   r(   r)   r*   r6   r3   Úpropertyr+   r,   r7   r"   Ú__classcell__r    r    r5   r!   r-   Y   s   

r-   c                 C   s
   t | tƒS r/   )Ú
isinstancer-   )Ú
constraintr    r    r!   r   ‡   s    r   c                       s2   e Zd ZdZdeedœ‡ fdd„Z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.
    Nr.   c                   s   t ƒ  |¡ || _|| _d S r/   )r2   r3   r0   r1   )r   Úfnr+   r,   r5   r    r!   r3   £   s    z_DependentProperty.__init__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):
                ...
        r.   )r?   r0   r1   )r   r@   r    r    r!   r7   ¨   s    z_DependentProperty.__call__)N)r%   r(   r)   r*   r6   r3   r7   r<   r    r    r5   r!   r?   ‹   s   r?   c                       sH   e Zd ZdZ‡ fdd„Zedd„ ƒZe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   )r=   r   ÚintÚbase_constraintÚreinterpreted_batch_ndimsr2   r3   )r   rD   rE   r5   r    r!   r3   ¹   s    z_IndependentConstraint.__init__c                 C   s   | j jS r/   )rD   r+   r&   r    r    r!   r+   Á   s    z"_IndependentConstraint.is_discretec                 C   s   | j j| j S r/   )rD   r,   rE   r&   r    r    r!   r,   Å   s    z _IndependentConstraint.event_dimc                 C   sp   | j  |¡}| ¡ | jk r@| j j| j }td|› d| ¡ › �ƒ‚| |jd | ¡ | j … d ¡}| d¡}|S )NzExpected value.dim() >= z	 but got ©éÿÿÿÿrG   )	rD   r"   ÚdimrE   r,   r8   ZreshapeÚshapeÚall)r   r   ÚresultÚexpectedr    r    r!   r"   É   s    "
z_IndependentConstraint.checkc                 C   s"   d  | jjdd … t| jƒ| j¡S )Nz
{}({}, {})r#   )Úformatr$   r%   ÚreprrD   rE   r&   r    r    r!   r'   Ò   s    ÿz_IndependentConstraint.__repr__)r%   r(   r)   r*   r3   r;   r+   r,   r"   r'   r<   r    r    r5   r!   rA   ³   s   

	rA   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"   Ý   s    z_Boolean.checkN)r%   r(   r)   r*   r+   r"   r    r    r    r!   rO   ×   s   rO   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#   rG   )ÚsumÚeqrJ   )r   r   Z
is_booleanÚis_normalizedr    r    r!   r"   è   s    z_OneHot.checkN)r%   r(   r)   r*   r+   r,   r"   r    r    r    r!   rP   á   s   rP   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 r/   ©Úlower_boundÚupper_boundr2   r3   ©r   rV   rW   r5   r    r!   r3   ô   s    z_IntegerInterval.__init__c                 C   s    |d dk| j |k@ || jk@ S ©Nr#   r   ©rV   rW   r   r    r    r!   r"   ù   s    z_IntegerInterval.checkc                 C   s(   | j jdd … }|d | j| j¡7 }|S ©Nr#   z (lower_bound={}, upper_bound={})©r$   r%   rM   rV   rW   ©r   Z
fmt_stringr    r    r!   r'   ü   s    z_IntegerInterval.__repr__©	r%   r(   r)   r*   r+   r3   r"   r'   r<   r    r    r5   r!   rT   î   s
   rT   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 r/   ©rW   r2   r3   ©r   rW   r5   r    r!   r3     s    z_IntegerLessThan.__init__c                 C   s   |d dk|| j k@ S rY   ©rW   r   r    r    r!   r"     s    z_IntegerLessThan.checkc                 C   s$   | j jdd … }|d | j¡7 }|S ©Nr#   z(upper_bound={})©r$   r%   rM   rW   r]   r    r    r!   r'     s    z_IntegerLessThan.__repr__r^   r    r    r5   r!   r_     s
   r_   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 r/   ©rV   r2   r3   ©r   rV   r5   r    r!   r3     s    z_IntegerGreaterThan.__init__c                 C   s   |d dk|| j k@ S rY   ©rV   r   r    r    r!   r"     s    z_IntegerGreaterThan.checkc                 C   s$   | j jdd … }|d | j¡7 }|S ©Nr#   z(lower_bound={})©r$   r%   rM   rV   r]   r    r    r!   r'   "  s    z_IntegerGreaterThan.__repr__r^   r    r    r5   r!   re     s
   re   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 r/   r    r   r    r    r!   r"   ,  s    z_Real.checkN)r%   r(   r)   r*   r"   r    r    r    r!   rk   (  s   rk   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 r/   rf   rg   r5   r    r!   r3   4  s    z_GreaterThan.__init__c                 C   s
   | j |k S r/   rh   r   r    r    r!   r"   8  s    z_GreaterThan.checkc                 C   s$   | j jdd … }|d | j¡7 }|S ri   rj   r]   r    r    r!   r'   ;  s    z_GreaterThan.__repr__©r%   r(   r)   r*   r3   r"   r'   r<   r    r    r5   r!   rl   0  s   rl   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 r/   rf   rg   r5   r    r!   r3   E  s    z_GreaterThanEq.__init__c                 C   s
   | j |kS r/   rh   r   r    r    r!   r"   I  s    z_GreaterThanEq.checkc                 C   s$   | j jdd … }|d | j¡7 }|S ri   rj   r]   r    r    r!   r'   L  s    z_GreaterThanEq.__repr__rm   r    r    r5   r!   rn   A  s   rn   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 r/   r`   ra   r5   r    r!   r3   V  s    z_LessThan.__init__c                 C   s
   || j k S r/   rb   r   r    r    r!   r"   Z  s    z_LessThan.checkc                 C   s$   | j jdd … }|d | j¡7 }|S rc   rd   r]   r    r    r!   r'   ]  s    z_LessThan.__repr__rm   r    r    r5   r!   ro   R  s   ro   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 r/   rU   rX   r5   r    r!   r3   g  s    z_Interval.__init__c                 C   s   | j |k|| jk@ S r/   rZ   r   r    r    r!   r"   l  s    z_Interval.checkc                 C   s(   | j jdd … }|d | j| j¡7 }|S r[   r\   r]   r    r    r!   r'   o  s    z_Interval.__repr__rm   r    r    r5   r!   rp   c  s   rp   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 r/   rU   rX   r5   r    r!   r3   y  s    z_HalfOpenInterval.__init__c                 C   s   | j |k|| jk @ S r/   rZ   r   r    r    r!   r"   ~  s    z_HalfOpenInterval.checkc                 C   s(   | j jdd … }|d | j| j¡7 }|S r[   r\   r]   r    r    r!   r'   �  s    z_HalfOpenInterval.__repr__rm   r    r    r5   r!   rq   u  s   rq   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   rG   ©rH   r#   g�íµ ÷Æ°>)ÚtorchrJ   rQ   Úabsr   r    r    r!   r"   Ž  s    z_Simplex.checkN©r%   r(   r)   r*   r,   r"   r    r    r    r!   rr   ‡  s   rr   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 r/   rb   ra   r    r    r!   r3   �  s    z_Multinomial.__init__c                 C   s"   |dkj dd�|jdd�| jk@ S )Nr   rG   rs   )rJ   rQ   rW   r9   r    r    r!   r"      s    z_Multinomial.checkN)r%   r(   r)   r*   r+   r,   r3   r"   r    r    r    r!   rw   ’  s
   rw   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 )NéþÿÿÿrF   rG   r   )ÚtrilÚviewrI   Úmin)r   r   Ú
value_trilr    r    r!   r"   ª  s    z_LowerTriangular.checkNrv   r    r    r    r!   rx   ¤  s   rx   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.
    ry   c                 C   sR   |  ¡ }||k |jd d… d ¡ d¡d }|jddd�dk d¡d }||@ S )Nrz   rF   rG   r   )Zdim1Zdim2)r{   r|   rI   r}   Zdiagonal)r   r   r~   r   Zpositive_diagonalr    r    r!   r"   µ  s    &z_LowerCholesky.checkNrv   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.
    ry   c                 C   sZ   t  |j¡j| d¡ d }t jj| ¡ dd�}|d  ¡  	|¡j
dd�}tƒ  |¡|@ S )NrG   é
   rs   ç      ð?)rt   ZfinfoZdtypeZepsÚsizeÚlinalgZnormÚdetachru   ÚlerJ   r   r"   )r   r   ZtolZrow_normZunit_row_normr    r    r!   r"   Ä  s    z_CorrCholesky.checkNrv   r    r    r    r!   r€   ½  s   r€   c                   @   s   e Zd ZdZdZdd„ ZdS )Ú_PositiveDefinitez2
    Constrain to positive-definite matrices.
    ry   c                 C   s   t j |¡j d¡ d¡S rB   )rt   r„   Zcholesky_exÚinforR   Z	unsqueezer   r    r    r!   r"   Ñ  s    z_PositiveDefinite.checkNrv   r    r    r    r!   r‡   Ë  s   r‡   c                       sB   e Zd ZdZd‡ fdd„	Zedd„ ƒZe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 r/   ©r=   r   ©Ú.0Úcr    r    r!   Ú	<genexpr>Þ  ó    z _Cat.__init__.<locals>.<genexpr>r#   )rJ   ÚlistÚcseqÚlenÚlengthsrH   r2   r3   )r   r‘   rH   r“   r5   r    r!   r3   Ý  s    

z_Cat.__init__c                 C   s   t dd„ | jD ƒƒS )Nc                 s   s   | ]}|j V  qd S r/   ©r+   r‹   r    r    r!   rŽ   é  r�   z#_Cat.is_discrete.<locals>.<genexpr>©Úanyr‘   r&   r    r    r!   r+   ç  s    z_Cat.is_discretec                 C   s   t dd„ | jD ƒƒS )Nc                 s   s   | ]}|j V  qd S r/   ©r,   r‹   r    r    r!   rŽ   í  r�   z!_Cat.event_dim.<locals>.<genexpr>)Úmaxr‘   r&   r    r    r!   r,   ë  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 rB   )	rH   Úzipr‘   r“   ZnarrowÚappendr"   rt   r   )r   r   ZchecksÚstartÚconstrÚlengthÚvr    r    r!   r"   ï  s    &
z
_Cat.check)r   N©
r%   r(   r)   r*   r3   r;   r+   r,   r"   r<   r    r    r5   r!   r‰   ×  s   


r‰   c                       sB   e Zd ZdZd‡ fdd„	Zedd„ ƒZe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 r/   rŠ   r‹   r    r    r!   rŽ     r�   z"_Stack.__init__.<locals>.<genexpr>)rJ   r�   r‘   rH   r2   r3   )r   r‘   rH   r5   r    r!   r3      s    
z_Stack.__init__c                 C   s   t dd„ | jD ƒƒS )Nc                 s   s   | ]}|j V  qd S r/   r”   r‹   r    r    r!   rŽ     r�   z%_Stack.is_discrete.<locals>.<genexpr>r•   r&   r    r    r!   r+     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 r/   r—   r‹   r    r    r!   rŽ     r�   z#_Stack.event_dim.<locals>.<genexpr>r   r#   )r˜   r‘   rH   )r   rH   r    r    r!   r,   
  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    )ÚselectrH   )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£     s   ÿ)rH   Úrangerƒ   rt   r   r™   r‘   )r   r   Úvsr    r   r!   r"     s    & 

ÿÿz_Stack.check)r   rŸ   r    r    r5   r!   r    ú  s   

r    r#   g        r‚   )7r*   rt   Ú__all__Úobjectr   r-   r   r;   r?   rA   rO   rP   rT   r_   re   rk   rl   rn   ro   rp   rq   rr   rw   rx   r   r€   r‡   r‰   r    r   r   r
   r   Zone_hotr   r   r   r   r   r   Znonnegativer   r	   r   r   r   r   r   r   r   r   r   r   r   r   r    r    r    r!   Ú<module>   sh   .($
#

