o
    hҢ                     @   s  d dl Z d dlZd dlZd dlZd dlmZ d dlZd dlm  m	Z
 d dlmZ d dlmZmZmZmZmZ d dlmZmZ d dlmZ g dZG dd	 d	ZG d
d deZG dd deZeg 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dd 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-dS )2    N)Optional)constraints)_sum_rightmostbroadcast_alllazy_propertytril_matrix_to_vecvec_to_tril_matrix)padsoftplus)_Number)AbsTransformAffineTransformCatTransformComposeTransformCorrCholeskyTransformCumulativeDistributionTransformExpTransformIndependentTransformLowerCholeskyTransformPositiveDefiniteTransformPowerTransformReshapeTransformSigmoidTransformSoftplusTransformTanhTransformSoftmaxTransformStackTransformStickBreakingTransform	Transformidentity_transformc                       s   e Zd ZU dZdZejed< ejed< d( fdd	Zdd	 Z	e
d
efddZe
d)ddZe
d
efddZd*ddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Z  ZS )+r   a  
    Abstract class for invertable transformations with computable log
    det jacobians. They are primarily used in
    :class:`torch.distributions.TransformedDistribution`.

    Caching is useful for transforms whose inverses are either expensive or
    numerically unstable. Note that care must be taken with memoized values
    since the autograd graph may be reversed. For example while the following
    works with or without caching::

        y = t(x)
        t.log_abs_det_jacobian(x, y).backward()  # x will receive gradients.

    However the following will error when caching due to dependency reversal::

        y = t(x)
        z = t.inv(y)
        grad(z.sum(), [y])  # error because z is x

    Derived classes should implement one or both of :meth:`_call` or
    :meth:`_inverse`. Derived classes that set `bijective=True` should also
    implement :meth:`log_abs_det_jacobian`.

    Args:
        cache_size (int): Size of cache. If zero, no caching is done. If one,
            the latest single value is cached. Only 0 and 1 are supported.

    Attributes:
        domain (:class:`~torch.distributions.constraints.Constraint`):
            The constraint representing valid inputs to this transform.
        codomain (:class:`~torch.distributions.constraints.Constraint`):
            The constraint representing valid outputs to this transform
            which are inputs to the inverse transform.
        bijective (bool): Whether this transform is bijective. A transform
            ``t`` is bijective iff ``t.inv(t(x)) == x`` and
            ``t(t.inv(y)) == y`` for every ``x`` in the domain and ``y`` in
            the codomain. Transforms that are not bijective should at least
            maintain the weaker pseudoinverse properties
            ``t(t.inv(t(x)) == t(x)`` and ``t.inv(t(t.inv(y))) == t.inv(y)``.
        sign (int or Tensor): For bijective univariate transforms, this
            should be +1 or -1 depending on whether transform is monotone
            increasing or decreasing.
    Fdomaincodomainr   c                    s<   || _ d | _|dkrn|dkrd| _ntdt   d S )Nr      )NNzcache_size must be 0 or 1)_cache_size_inv_cached_x_y
ValueErrorsuper__init__self
cache_size	__class__ r/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/torch/distributions/transforms.pyr(   _   s   zTransform.__init__c                 C   s   | j  }d |d< |S )Nr$   )__dict__copy)r*   stater.   r.   r/   __getstate__j   s   
zTransform.__getstate__returnc                 C   s    | j j| jjkr| j jS td)Nz:Please use either .domain.event_dim or .codomain.event_dim)r    	event_dimr!   r&   r*   r.   r.   r/   r5   o   s   zTransform.event_dimc                 C   s6   d}| j dur|   }|du rt| }t|| _ |S )z{
        Returns the inverse :class:`Transform` of this transform.
        This should satisfy ``t.inv.inv is t``.
        N)r$   _InverseTransformweakrefrefr*   invr.   r.   r/   r;   u   s   
zTransform.invc                 C      t )z
        Returns the sign of the determinant of the Jacobian, if applicable.
        In general this only makes sense for bijective transforms.
        NotImplementedErrorr6   r.   r.   r/   sign   s   zTransform.signr"   c                 C   s>   | j |kr| S t| jtju rt| |dS tt|  d)Nr+   z.with_cache is not implemented)r#   typer(   r   r>   r)   r.   r.   r/   
with_cache   s
   
zTransform.with_cachec                 C   s   | |u S Nr.   r*   otherr.   r.   r/   __eq__      zTransform.__eq__c                 C   s   |  | S rC   )rF   rD   r.   r.   r/   __ne__   s   zTransform.__ne__c                 C   sB   | j dkr
| |S | j\}}||u r|S | |}||f| _|S )z2
        Computes the transform `x => y`.
        r   )r#   _callr%   )r*   xx_oldy_oldyr.   r.   r/   __call__      




zTransform.__call__c                 C   sB   | j dkr
| |S | j\}}||u r|S | |}||f| _|S )z1
        Inverts the transform `y => x`.
        r   )r#   _inverser%   )r*   rM   rK   rL   rJ   r.   r.   r/   	_inv_call   rO   zTransform._inv_callc                 C   r<   )zD
        Abstract method to compute forward transformation.
        r=   r*   rJ   r.   r.   r/   rI         zTransform._callc                 C   r<   )zD
        Abstract method to compute inverse transformation.
        r=   r*   rM   r.   r.   r/   rP      rS   zTransform._inversec                 C   r<   )zU
        Computes the log det jacobian `log |dy/dx|` given input and output.
        r=   r*   rJ   rM   r.   r.   r/   log_abs_det_jacobian   rS   zTransform.log_abs_det_jacobianc                 C   s   | j jd S )Nz())r-   __name__r6   r.   r.   r/   __repr__      zTransform.__repr__c                 C      |S )z{
        Infers the shape of the forward computation, given the input shape.
        Defaults to preserving shape.
        r.   r*   shaper.   r.   r/   forward_shape      zTransform.forward_shapec                 C   rZ   )z}
        Infers the shapes of the inverse computation, given the output shape.
        Defaults to preserving shape.
        r.   r[   r.   r.   r/   inverse_shape   r^   zTransform.inverse_shaper   )r4   r   r"   )rW   
__module____qualname____doc__	bijectiver   
Constraint__annotations__r(   r3   propertyintr5   r;   r?   rB   rF   rH   rN   rQ   rI   rP   rV   rX   r]   r_   __classcell__r.   r.   r,   r/   r   .   s0   
 ,


r   c                       s   e Zd ZdZdef fddZejdddd Zejdd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!ddZdd Zdd Zdd Zdd Zdd Zdd  Z  ZS )"r7   z|
    Inverts a single :class:`Transform`.
    This class is private; please instead use the ``Transform.inv`` property.
    	transformc                    s   t  j|jd || _d S Nr@   )r'   r(   r#   r$   )r*   rk   r,   r.   r/   r(      s   
z_InverseTransform.__init__Fis_discretec                 C      | j d usJ | j jS rC   )r$   r!   r6   r.   r.   r/   r          z_InverseTransform.domainc                 C   ro   rC   )r$   r    r6   r.   r.   r/   r!      rp   z_InverseTransform.codomainr4   c                 C   ro   rC   )r$   re   r6   r.   r.   r/   re      rp   z_InverseTransform.bijectivec                 C   ro   rC   )r$   r?   r6   r.   r.   r/   r?      rp   z_InverseTransform.signc                 C      | j S rC   )r$   r6   r.   r.   r/   r;         z_InverseTransform.invr"   c                 C   s   | j d usJ | j|jS rC   )r$   r;   rB   r)   r.   r.   r/   rB      s   z_InverseTransform.with_cachec                 C   s(   t |tsdS | jd usJ | j|jkS NF)
isinstancer7   r$   rD   r.   r.   r/   rF      s   
z_InverseTransform.__eq__c                 C   s   | j j dt| j dS )N())r-   rW   reprr$   r6   r.   r.   r/   rX     s   z_InverseTransform.__repr__c                 C   s   | j d usJ | j |S rC   )r$   rQ   rR   r.   r.   r/   rN     s   z_InverseTransform.__call__c                 C   s   | j d usJ | j || S rC   )r$   rV   rU   r.   r.   r/   rV   
     z&_InverseTransform.log_abs_det_jacobianc                 C      | j |S rC   )r$   r_   r[   r.   r.   r/   r]     rY   z_InverseTransform.forward_shapec                 C   ry   rC   )r$   r]   r[   r.   r.   r/   r_     rY   z_InverseTransform.inverse_shapera   )rW   rb   rc   rd   r   r(   r   dependent_propertyr    r!   rh   boolre   ri   r?   r;   rB   rF   rX   rN   rV   r]   r_   rj   r.   r.   r,   r/   r7      s(    




r7   c                       s   e Zd ZdZd"dee f fddZdd Zej	dd	d
d Z
ej	dd	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#ddZdd Zdd Zdd Zdd Zd d! Z  ZS )$r   ab  
    Composes multiple transforms in a chain.
    The transforms being composed are responsible for caching.

    Args:
        parts (list of :class:`Transform`): A list of transforms to compose.
        cache_size (int): Size of cache. If zero, no caching is done. If one,
            the latest single value is cached. Only 0 and 1 are supported.
    r   partsc                    s.    r fdd|D }t  j d || _d S )Nc                       g | ]}|  qS r.   rB   ).0partr@   r.   r/   
<listcomp>"      z-ComposeTransform.__init__.<locals>.<listcomp>r@   )r'   r(   r|   )r*   r|   r+   r,   r@   r/   r(      s   
zComposeTransform.__init__c                 C   s   t |tsdS | j|jkS rs   )rt   r   r|   rD   r.   r.   r/   rF   &  s   
zComposeTransform.__eq__Frm   c                 C   s   | j stjS | j d j}| j d jj}t| j D ]}||jj|jj 7 }t||jj}q||jks3J ||jkrAt|||j }|S )Nr   )	r|   r   realr    r!   r5   reversedmaxindependent)r*   r    r5   r   r.   r.   r/   r    +  s   
zComposeTransform.domainc                 C   s   | j stjS | j d j}| j d jj}| j D ]}||jj|jj 7 }t||jj}q||jks1J ||jkr?t|||j }|S )Nr   r   )r|   r   r   r!   r    r5   r   r   )r*   r!   r5   r   r.   r.   r/   r!   :  s   

zComposeTransform.codomainr4   c                 C      t dd | jD S )Nc                 s       | ]}|j V  qd S rC   re   r   pr.   r.   r/   	<genexpr>K      z-ComposeTransform.bijective.<locals>.<genexpr>)allr|   r6   r.   r.   r/   re   I     zComposeTransform.bijectivec                 C   s   d}| j D ]}||j }q|S Nr"   )r|   r?   )r*   r?   r   r.   r.   r/   r?   M  s   
zComposeTransform.signc                 C   sR   d }| j d ur|   }|d u r'tdd t| jD }t|| _ t| |_ |S )Nc                 S      g | ]}|j qS r.   )r;   r   r.   r.   r/   r   Z      z(ComposeTransform.inv.<locals>.<listcomp>)r$   r   r   r|   r8   r9   r:   r.   r.   r/   r;   T  s   
zComposeTransform.invr"   c                 C      | j |kr| S t| j|dS rl   )r#   r   r|   r)   r.   r.   r/   rB   _     
zComposeTransform.with_cachec                 C   s   | j D ]}||}q|S rC   )r|   )r*   rJ   r   r.   r.   r/   rN   d  s   

zComposeTransform.__call__c                 C   s   | j st|S |g}| j d d D ]}|||d  q|| g }| jj}t| j |d d |dd  D ]\}}}|t|||||jj  ||j	j|jj 7 }q8t
tj|S )Nr   r"   )r|   torch
zeros_likeappendr    r5   zipr   rV   r!   	functoolsreduceoperatoradd)r*   rJ   rM   xsr   termsr5   r.   r.   r/   rV   i  s    

(z%ComposeTransform.log_abs_det_jacobianc                 C   s   | j D ]}||}q|S rC   )r|   r]   r*   r\   r   r.   r.   r/   r]   ~  s   
zComposeTransform.forward_shapec                 C   s   t | jD ]}||}q|S rC   )r   r|   r_   r   r.   r.   r/   r_        zComposeTransform.inverse_shapec                 C   s2   | j jd }|ddd | jD 7 }|d7 }|S )Nz(
    z,
    c                 S   s   g | ]}|  qS r.   )rX   r   r.   r.   r/   r     s    z-ComposeTransform.__repr__.<locals>.<listcomp>z
))r-   rW   joinr|   )r*   
fmt_stringr.   r.   r/   rX     s   zComposeTransform.__repr__r`   ra   )rW   rb   rc   rd   listr   r(   rF   r   rz   r    r!   r   r{   re   ri   r?   rh   r;   rB   rN   rV   r]   r_   rX   rj   r.   r.   r,   r/   r     s(    






r   c                       s   e Zd ZdZd fdd	Zd ddZejdd	d
d Zejdd	dd Z	e
defddZe
defddZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )!r   a  
    Wrapper around another transform to treat
    ``reinterpreted_batch_ndims``-many extra of the right most dimensions as
    dependent. This has no effect on the forward or backward transforms, but
    does sum out ``reinterpreted_batch_ndims``-many of the rightmost dimensions
    in :meth:`log_abs_det_jacobian`.

    Args:
        base_transform (:class:`Transform`): A base transform.
        reinterpreted_batch_ndims (int): The number of extra rightmost
            dimensions to treat as dependent.
    r   c                    s$   t  j|d ||| _|| _d S rl   )r'   r(   rB   base_transformreinterpreted_batch_ndims)r*   r   r   r+   r,   r.   r/   r(     s   
zIndependentTransform.__init__r"   c                 C       | j |kr| S t| j| j|dS rl   )r#   r   r   r   r)   r.   r.   r/   rB     s
   

zIndependentTransform.with_cacheFrm   c                 C      t | jj| jS rC   )r   r   r   r    r   r6   r.   r.   r/   r         
zIndependentTransform.domainc                 C   r   rC   )r   r   r   r!   r   r6   r.   r.   r/   r!     r   zIndependentTransform.codomainr4   c                 C      | j jS rC   )r   re   r6   r.   r.   r/   re        zIndependentTransform.bijectivec                 C   r   rC   )r   r?   r6   r.   r.   r/   r?     r   zIndependentTransform.signc                 C   s"   |  | jjk rtd| |S NToo few dimensions on input)dimr    r5   r&   r   rR   r.   r.   r/   rI     s   
zIndependentTransform._callc                 C   s$   |  | jjk rtd| j|S r   )r   r!   r5   r&   r   r;   rT   r.   r.   r/   rP     s   zIndependentTransform._inversec                 C   s   | j ||}t|| j}|S rC   )r   rV   r   r   )r*   rJ   rM   resultr.   r.   r/   rV     r   z)IndependentTransform.log_abs_det_jacobianc                 C   s"   | j j dt| j d| j dS )Nru   z, rv   )r-   rW   rw   r   r   r6   r.   r.   r/   rX     s   "zIndependentTransform.__repr__c                 C   ry   rC   )r   r]   r[   r.   r.   r/   r]     rY   z"IndependentTransform.forward_shapec                 C   ry   rC   )r   r_   r[   r.   r.   r/   r_     rY   z"IndependentTransform.inverse_shaper`   ra   )rW   rb   rc   rd   r(   rB   r   rz   r    r!   rh   r{   re   ri   r?   rI   rP   rV   rX   r]   r_   rj   r.   r.   r,   r/   r     s$    




r   c                       st   e Zd ZdZdZd fdd	Zejdd Zejdd	 Z	dddZ
dd Zdd Zdd Zdd Zdd Z  ZS )r   a  
    Unit Jacobian transform to reshape the rightmost part of a tensor.

    Note that ``in_shape`` and ``out_shape`` must have the same number of
    elements, just as for :meth:`torch.Tensor.reshape`.

    Arguments:
        in_shape (torch.Size): The input event shape.
        out_shape (torch.Size): The output event shape.
        cache_size (int): Size of cache. If zero, no caching is done. If one,
            the latest single value is cached. Only 0 and 1 are supported. (Default 0.)
    Tr   c                    sF   t || _t || _| j | j krtdt j|d d S )Nz6in_shape, out_shape have different numbers of elementsr@   )r   Sizein_shape	out_shapenumelr&   r'   r(   )r*   r   r   r+   r,   r.   r/   r(     s
   zReshapeTransform.__init__c                 C      t t jt| jS rC   )r   r   r   lenr   r6   r.   r.   r/   r      r   zReshapeTransform.domainc                 C   r   rC   )r   r   r   r   r   r6   r.   r.   r/   r!     r   zReshapeTransform.codomainr"   c                 C   r   rl   )r#   r   r   r   r)   r.   r.   r/   rB     s   
zReshapeTransform.with_cachec                 C   ,   |j d | t| j  }||| j S rC   )r\   r   r   r   reshaper   )r*   rJ   batch_shaper.   r.   r/   rI        zReshapeTransform._callc                 C   r   rC   )r\   r   r   r   r   r   )r*   rM   r   r.   r.   r/   rP     r   zReshapeTransform._inversec                 C   s&   |j d | t| j  }||S rC   )r\   r   r   r   	new_zeros)r*   rJ   rM   r   r.   r.   r/   rV     s   
z%ReshapeTransform.log_abs_det_jacobianc                 C   n   t |t | jk rtdt |t | j }||d  | jkr.td||d   d| j |d | | j S Nr   zShape mismatch: expected z	 but got )r   r   r&   r   r*   r\   cutr.   r.   r/   r]   	     zReshapeTransform.forward_shapec                 C   r   r   )r   r   r&   r   r   r.   r.   r/   r_     r   zReshapeTransform.inverse_shaper`   ra   )rW   rb   rc   rd   re   r(   r   rz   r    r!   rB   rI   rP   rV   r]   r_   rj   r.   r.   r,   r/   r     s    



r   c                   @   D   e Zd ZdZejZejZdZ	dZ
dd Zdd Zdd	 Zd
d ZdS )r   z8
    Transform via the mapping :math:`y = \exp(x)`.
    Tr"   c                 C   
   t |tS rC   )rt   r   rD   r.   r.   r/   rF   (     
zExpTransform.__eq__c                 C      |  S rC   )exprR   r.   r.   r/   rI   +  rG   zExpTransform._callc                 C   r   rC   logrT   r.   r.   r/   rP   .  rG   zExpTransform._inversec                 C   rZ   rC   r.   rU   r.   r.   r/   rV   1     z!ExpTransform.log_abs_det_jacobianNrW   rb   rc   rd   r   r   r    positiver!   re   r?   rF   rI   rP   rV   r.   r.   r.   r/   r     s    r   c                       s~   e Zd ZdZejZejZdZd fdd	Z	dddZ
ed	efd
dZdd Zdd Zdd Zdd Zdd Zdd Z  ZS )r   zD
    Transform via the mapping :math:`y = x^{\text{exponent}}`.
    Tr   c                    s   t  j|d t|\| _d S rl   )r'   r(   r   exponent)r*   r   r+   r,   r.   r/   r(   >  rx   zPowerTransform.__init__r"   c                 C   r   rl   )r#   r   r   r)   r.   r.   r/   rB   B  r   zPowerTransform.with_cacher4   c                 C   s
   | j  S rC   )r   r?   r6   r.   r.   r/   r?   G     
zPowerTransform.signc                 C   s$   t |tsdS | j|j  S rs   )rt   r   r   eqr   itemrD   r.   r.   r/   rF   K  s   
zPowerTransform.__eq__c                 C   s   | | jS rC   powr   rR   r.   r.   r/   rI   P  rY   zPowerTransform._callc                 C   s   | d| j S r   r   rT   r.   r.   r/   rP   S     zPowerTransform._inversec                 C   s   | j | |   S rC   )r   absr   rU   r.   r.   r/   rV   V     z#PowerTransform.log_abs_det_jacobianc                 C      t |t| jddS Nr\   r.   r   broadcast_shapesgetattrr   r[   r.   r.   r/   r]   Y  r   zPowerTransform.forward_shapec                 C   r   r   r   r[   r.   r.   r/   r_   \  r   zPowerTransform.inverse_shaper`   ra   )rW   rb   rc   rd   r   r   r    r!   re   r(   rB   r   ri   r?   rF   rI   rP   rV   r]   r_   rj   r.   r.   r,   r/   r   5  s    
r   c                 C   s*   t | j}t jt | |jd|j dS N      ?minr   )r   finfodtypeclampsigmoidtinyeps)rJ   r   r.   r.   r/   _clipped_sigmoid`  s   r   c                   @   r   )r   zg
    Transform via the mapping :math:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.
    Tr"   c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF   o  r   zSigmoidTransform.__eq__c                 C      t |S rC   )r   rR   r.   r.   r/   rI   r  rG   zSigmoidTransform._callc                 C   s4   t |j}|j|jd|j d}| |   S r   )r   r   r   r   r   r   r   log1p)r*   rM   r   r.   r.   r/   rP   u  s   zSigmoidTransform._inversec                 C   s   t |  t | S rC   )Fr
   rU   r.   r.   r/   rV   z  s   z%SigmoidTransform.log_abs_det_jacobianN)rW   rb   rc   rd   r   r   r    unit_intervalr!   re   r?   rF   rI   rP   rV   r.   r.   r.   r/   r   e  s    r   c                   @   r   )r   z
    Transform via the mapping :math:`\text{Softplus}(x) = \log(1 + \exp(x))`.
    The implementation reverts to the linear function when :math:`x > 20`.
    Tr"   c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zSoftplusTransform.__eq__c                 C   r   rC   r
   rR   r.   r.   r/   rI     rG   zSoftplusTransform._callc                 C   s   |     | S rC   )expm1negr   rT   r.   r.   r/   rP     r   zSoftplusTransform._inversec                 C   s   t |  S rC   r   rU   r.   r.   r/   rV     rY   z&SoftplusTransform.log_abs_det_jacobianNr   r.   r.   r.   r/   r   ~  s    r   c                   @   sJ   e Zd ZdZejZeddZdZ	dZ
dd Zdd	 Zd
d Zdd ZdS )r   a  
    Transform via the mapping :math:`y = \tanh(x)`.

    It is equivalent to

    .. code-block:: python

        ComposeTransform(
            [
                AffineTransform(0.0, 2.0),
                SigmoidTransform(),
                AffineTransform(-1.0, 2.0),
            ]
        )

    However this might not be numerically stable, thus it is recommended to use `TanhTransform`
    instead.

    Note that one should use `cache_size=1` when it comes to `NaN/Inf` values.

    g      r   Tr"   c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zTanhTransform.__eq__c                 C   r   rC   )tanhrR   r.   r.   r/   rI     rG   zTanhTransform._callc                 C   s
   t |S rC   )r   atanhrT   r.   r.   r/   rP     s   
zTanhTransform._inversec                 C   s   dt d| td|   S )N       @g       )mathr   r
   rU   r.   r.   r/   rV     s   z"TanhTransform.log_abs_det_jacobianN)rW   rb   rc   rd   r   r   r    intervalr!   re   r?   rF   rI   rP   rV   r.   r.   r.   r/   r     s    r   c                   @   s4   e Zd ZdZejZejZdd Z	dd Z
dd ZdS )	r   z*Transform via the mapping :math:`y = |x|`.c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zAbsTransform.__eq__c                 C   r   rC   )r   rR   r.   r.   r/   rI     rG   zAbsTransform._callc                 C   rZ   rC   r.   rT   r.   r.   r/   rP     r   zAbsTransform._inverseN)rW   rb   rc   rd   r   r   r    r   r!   rF   rI   rP   r.   r.   r.   r/   r     s    r   c                       s   e Zd ZdZdZd  fdd	ZedefddZe	j
d	d
dd Ze	j
d	d
dd Zd!ddZdd ZedefddZdd Zdd Zdd Zdd Zdd Z  ZS )"r   a  
    Transform via the pointwise affine mapping :math:`y = \text{loc} + \text{scale} \times x`.

    Args:
        loc (Tensor or float): Location parameter.
        scale (Tensor or float): Scale parameter.
        event_dim (int): Optional size of `event_shape`. This should be zero
            for univariate random variables, 1 for distributions over vectors,
            2 for distributions over matrices, etc.
    Tr   c                    s$   t  j|d || _|| _|| _d S rl   )r'   r(   locscale
_event_dim)r*   r   r   r5   r+   r,   r.   r/   r(     s   
zAffineTransform.__init__r4   c                 C   rq   rC   )r   r6   r.   r.   r/   r5     rr   zAffineTransform.event_dimFrm   c                 C       | j dkrtjS ttj| j S Nr   r5   r   r   r   r6   r.   r.   r/   r         
zAffineTransform.domainc                 C   r   r   r   r6   r.   r.   r/   r!     r   zAffineTransform.codomainr"   c                 C   s$   | j |kr| S t| j| j| j|dS rl   )r#   r   r   r   r5   r)   r.   r.   r/   rB     s
   
zAffineTransform.with_cachec                 C   s   t |tsdS t | jtrt |jtr| j|jkrdS n| j|jk  s(dS t | jtr>t |jtr>| j|jkr<dS dS | j|jk  sJdS dS )NFT)rt   r   r   r   r   r   r   rD   r.   r.   r/   rF     s   
zAffineTransform.__eq__c                 C   s>   t | jtrt| jdkrdS t| jdk rdS dS | j S )Nr   r"   r   )rt   r   r   floatr?   r6   r.   r.   r/   r?     s   (
zAffineTransform.signc                 C   s   | j | j|  S rC   r   r   rR   r.   r.   r/   rI     r   zAffineTransform._callc                 C   s   || j  | j S rC   r   rT   r.   r.   r/   rP     r   zAffineTransform._inversec                 C   s   |j }| j}t|trt|tt|}nt| }| j	r=|
 d | j	  d }||d}|d | j	  }||S )N)r   r   )r\   r   rt   r   r   	full_liker   r   r   r5   sizeviewsumexpand)r*   rJ   rM   r\   r   r   result_sizer.   r.   r/   rV     s   

z$AffineTransform.log_abs_det_jacobianc                 C   "   t |t| jddt| jddS r   r   r   r   r   r   r[   r.   r.   r/   r]   +     zAffineTransform.forward_shapec                 C   r   r   r   r[   r.   r.   r/   r_   0  r   zAffineTransform.inverse_shaper   r   ra   )rW   rb   rc   rd   re   r(   rh   ri   r5   r   rz   r    r!   rB   rF   r?   rI   rP   rV   r]   r_   rj   r.   r.   r,   r/   r     s&    




r   c                   @   sJ   e Zd ZdZejZejZdZ	dd Z
dd Zddd	Zd
d Zdd ZdS )r   a  
    Transforms an uncontrained real vector :math:`x` with length :math:`D*(D-1)/2` into the
    Cholesky factor of a D-dimension correlation matrix. This Cholesky factor is a lower
    triangular matrix with positive diagonals and unit Euclidean norm for each row.
    The transform is processed as follows:

        1. First we convert x into a lower triangular matrix in row order.
        2. For each row :math:`X_i` of the lower triangular part, we apply a *signed* version of
           class :class:`StickBreakingTransform` to transform :math:`X_i` into a
           unit Euclidean length vector using the following steps:
           - Scales into the interval :math:`(-1, 1)` domain: :math:`r_i = \tanh(X_i)`.
           - Transforms into an unsigned domain: :math:`z_i = r_i^2`.
           - Applies :math:`s_i = StickBreakingTransform(z_i)`.
           - Transforms back into signed domain: :math:`y_i = sign(r_i) * \sqrt{s_i}`.
    Tc                 C   s   t |}t |jj}|jd| d| d}t|dd}|d }d|  d}|t j	|j
d |j|jd }|t|dd df ddgdd	 }|S )
Nr   r"   r   diag   )r   device.r   value)r   r   r   r   r   r   r   sqrtcumprodeyer\   r  r	   )r*   rJ   r   rzz1m_cumprod_sqrtrM   r.   r.   r/   rI   K  s   
"zCorrCholeskyTransform._callc                 C   st   dt j|| dd }t|dd df ddgdd}t|dd}t|dd}||  }| |   d }|S )	Nr"   r   r   .r   r  r  r  )r   cumsumr	   r   r  r   r   )r*   rM   y_cumsumy_cumsum_shiftedy_vecy_cumsum_vectrJ   r.   r.   r/   rP   Z  s   zCorrCholeskyTransform._inverseNc                 C   s`   d|| j dd }t|dd}d| d }d|td|  td jdd }|| S )Nr"   r   r  r        ?r   )r  r   r   r   r
   r   )r*   rJ   rM   intermediates
y1m_cumsumy1m_cumsum_trilstick_breaking_logdettanh_logdetr.   r.   r/   rV   f  s
   &z*CorrCholeskyTransform.log_abs_det_jacobianc                 C   sd   t |dk r
td|d }tdd|  d d }||d  d |kr(td|d d ||f S )Nr"   r   r   g      ?r  r  z-Input is not a flattend lower-diagonal number)r   r&   round)r*   r\   NDr.   r.   r/   r]   t  s   z#CorrCholeskyTransform.forward_shapec                 C   sV   t |dk r
td|d |d krtd|d }||d  d }|d d |f S )Nr  r   r  r   zInput is not squarer"   r   r&   )r*   r\   r  r  r.   r.   r/   r_   ~  s   z#CorrCholeskyTransform.inverse_shaperC   )rW   rb   rc   rd   r   real_vectorr    corr_choleskyr!   re   rI   rP   rV   r]   r_   r.   r.   r.   r/   r   6  s    

r   c                   @   sD   e Zd ZdZejZejZdd Z	dd Z
dd Zdd	 Zd
d ZdS )r   a<  
    Transform from unconstrained space to the simplex via :math:`y = \exp(x)` then
    normalizing.

    This is not bijective and cannot be used for HMC. However this acts mostly
    coordinate-wise (except for the final normalization), and thus is
    appropriate for coordinate-wise optimization algorithms.
    c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zSoftmaxTransform.__eq__c                 C   s,   |}|| ddd   }||dd S )Nr   Tr   )r   r   r   )r*   rJ   logprobsprobsr.   r.   r/   rI     s   zSoftmaxTransform._callc                 C   s   |}|  S rC   r   )r*   rM   r#  r.   r.   r/   rP     s   zSoftmaxTransform._inversec                 C      t |dk r
td|S Nr"   r   r  r[   r.   r.   r/   r]        zSoftmaxTransform.forward_shapec                 C   r$  r%  r  r[   r.   r.   r/   r_     r&  zSoftmaxTransform.inverse_shapeN)rW   rb   rc   rd   r   r   r    simplexr!   rF   rI   rP   r]   r_   r.   r.   r.   r/   r     s    	r   c                   @   sP   e Zd ZdZejZejZdZ	dd Z
dd Zdd Zd	d
 Zdd Zdd ZdS )r   a  
    Transform from unconstrained space to the simplex of one additional
    dimension via a stick-breaking process.

    This transform arises as an iterated sigmoid transform in a stick-breaking
    construction of the `Dirichlet` distribution: the first logit is
    transformed via sigmoid to the first probability and the probability of
    everything else, and then the process recurses.

    This is bijective and appropriate for use in HMC; however it mixes
    coordinates together and is less appropriate for optimization.
    Tc                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zStickBreakingTransform.__eq__c                 C   sj   |j d d ||j d d }t||  }d| d}t|ddgddt|ddgdd }|S )Nr   r"   r   r  )r\   new_onesr  r   r   r	  r	   )r*   rJ   offsetr  	z_cumprodrM   r.   r.   r/   rI     s
   $$zStickBreakingTransform._callc                 C   sr   |dd df }|j d ||j d d }d|d }tj|t|jjd}| |  |  }|S )N.r   r"   )r   )	r\   r(  r  r   r   r   r   r   r   )r*   rM   y_cropr)  sfrJ   r.   r.   r/   rP     s    zStickBreakingTransform._inversec                 C   s^   |j d d ||j d d }||  }| t| |dd df   d}|S )Nr   r"   .)r\   r(  r  r   r   
logsigmoidr   )r*   rJ   rM   r)  detJr.   r.   r/   rV     s   $*z+StickBreakingTransform.log_abs_det_jacobianc                 C   s.   t |dk r
td|d d |d d f S Nr"   r   r   r  r[   r.   r.   r/   r]        z$StickBreakingTransform.forward_shapec                 C   s.   t |dk r
td|d d |d d f S r/  r  r[   r.   r.   r/   r_     r0  z$StickBreakingTransform.inverse_shapeN)rW   rb   rc   rd   r   r   r    r'  r!   re   rF   rI   rP   rV   r]   r_   r.   r.   r.   r/   r     s    
r   c                   @   <   e Zd ZdZeejdZejZ	dd Z
dd Zdd Zd	S )
r   z
    Transform from unconstrained matrices to lower-triangular matrices with
    nonnegative diagonal entries.

    This is useful for parameterizing positive definite matrices in terms of
    their Cholesky factorization.
    r  c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   zLowerCholeskyTransform.__eq__c                 C       | d|jddd   S Nr   r  )dim1dim2)trildiagonalr   
diag_embedrR   r.   r.   r/   rI         zLowerCholeskyTransform._callc                 C   r2  r3  )r6  r7  r   r8  rT   r.   r.   r/   rP     r9  zLowerCholeskyTransform._inverseN)rW   rb   rc   rd   r   r   r   r    lower_choleskyr!   rF   rI   rP   r.   r.   r.   r/   r     s    r   c                   @   r1  )
r   zN
    Transform from unconstrained matrices to positive-definite matrices.
    r  c                 C   r   rC   )rt   r   rD   r.   r.   r/   rF     r   z PositiveDefiniteTransform.__eq__c                 C   s   t  |}||j S rC   )r   mTrR   r.   r.   r/   rI     s   

zPositiveDefiniteTransform._callc                 C   s   t j|}t |S rC   )r   linalgcholeskyr   r;   rT   r.   r.   r/   rP   
  s   z"PositiveDefiniteTransform._inverseN)rW   rb   rc   rd   r   r   r   r    positive_definiter!   rF   rI   rP   r.   r.   r.   r/   r     s    r   c                       s   e Zd ZU dZee ed< d fdd	Zede	fdd	Z
ede	fd
dZdddZdd Zdd Zdd ZedefddZejdd Zejdd Z  ZS )r   a  
    Transform functor that applies a sequence of transforms `tseq`
    component-wise to each submatrix at `dim`, of length `lengths[dim]`,
    in a way compatible with :func:`torch.cat`.

    Example::

       x0 = torch.cat([torch.range(1, 10), torch.range(1, 10)], dim=0)
       x = torch.cat([x0, x0], dim=0)
       t0 = CatTransform([ExpTransform(), identity_transform], dim=0, lengths=[10, 10])
       t = CatTransform([t0, t0], dim=0, lengths=[20, 20])
       y = t(x)
    
transformsr   Nc                    s   t dd |D sJ  r fdd|D }t j d t|| _|d u r.dgt| j }t|| _t| jt| jks?J || _d S )Nc                 s       | ]}t |tV  qd S rC   rt   r   r   r  r.   r.   r/   r   !      z(CatTransform.__init__.<locals>.<genexpr>c                    r}   r.   r~   rB  r@   r.   r/   r   #  r   z)CatTransform.__init__.<locals>.<listcomp>r@   r"   )r   r'   r(   r   r?  r   lengthsr   )r*   tseqr   rD  r+   r,   r@   r/   r(      s   


zCatTransform.__init__r4   c                 C   r   )Nc                 s   r   rC   )r5   rB  r.   r.   r/   r   .  r   z)CatTransform.event_dim.<locals>.<genexpr>)r   r?  r6   r.   r.   r/   r5   ,  r   zCatTransform.event_dimc                 C   s
   t | jS rC   )r   rD  r6   r.   r.   r/   length0  r   zCatTransform.lengthr"   c                 C   s"   | j |kr| S t| j| j| j|S rC   )r#   r   r?  r   rD  r)   r.   r.   r/   rB   4  s   
zCatTransform.with_cachec                 C   s   |   | j   kr|  k sJ  J || j | jksJ g }d}t| j| jD ]\}}|| j ||}||| || }q*tj	|| j dS Nr   r  )
r   r   rF  r   r?  rD  narrowr   r   cat)r*   rJ   yslicesstarttransrF  xslicer.   r.   r/   rI   9  s   (
zCatTransform._callc                 C   s   |   | j   kr|  k sJ  J || j | jksJ g }d}t| j| jD ]\}}|| j ||}||| || }q*t	j
|| j dS rG  )r   r   rF  r   r?  rD  rH  r   r;   r   rI  )r*   rM   xslicesrK  rL  rF  yslicer.   r.   r/   rP   D  s   (
zCatTransform._inversec                 C   s:  |   | j   kr|  k sJ  J || j | jksJ |   | j   kr0|  k s3J  J || j | jks>J g }d}t| j| jD ]2\}}|| j ||}|| j ||}|||}	|j| jk rrt	|	| j|j }	|
|	 || }qI| j }
|
dkr|
|   }
|
| j }
|
dk rtj||
dS t|S rG  )r   r   rF  r   r?  rD  rH  rV   r5   r   r   r   rI  r   )r*   rJ   rM   
logdetjacsrK  rL  rF  rM  rO  	logdetjacr   r.   r.   r/   rV   O  s*   ((


z!CatTransform.log_abs_det_jacobianc                 C   r   )Nc                 s   r   rC   r   rB  r.   r.   r/   r   j  r   z)CatTransform.bijective.<locals>.<genexpr>r   r?  r6   r.   r.   r/   re   h  r   zCatTransform.bijectivec                 C      t dd | jD | j| jS )Nc                 S   r   r.   r    rB  r.   r.   r/   r   o  r   z'CatTransform.domain.<locals>.<listcomp>r   rI  r?  r   rD  r6   r.   r.   r/   r    l     zCatTransform.domainc                 C   rS  )Nc                 S   r   r.   r!   rB  r.   r.   r/   r   u  r   z)CatTransform.codomain.<locals>.<listcomp>rU  r6   r.   r.   r/   r!   r  rV  zCatTransform.codomain)r   Nr   ra   )rW   rb   rc   rd   r   r   rg   r(   r   ri   r5   rF  rB   rI   rP   rV   rh   r{   re   r   rz   r    r!   rj   r.   r.   r,   r/   r     s$   
 

r   c                       s   e Zd ZU dZee ed< d fdd	ZdddZd	d
 Z	dd Z
dd Zdd ZedefddZejdd Zejdd Z  ZS )r   aW  
    Transform functor that applies a sequence of transforms `tseq`
    component-wise to each submatrix at `dim`
    in a way compatible with :func:`torch.stack`.

    Example::

       x = torch.stack([torch.range(1, 10), torch.range(1, 10)], dim=1)
       t = StackTransform([ExpTransform(), identity_transform], dim=1)
       y = t(x)
    r?  r   c                    sN   t dd |D sJ  r fdd|D }t j d t|| _|| _d S )Nc                 s   r@  rC   rA  rB  r.   r.   r/   r     rC  z*StackTransform.__init__.<locals>.<genexpr>c                    r}   r.   r~   rB  r@   r.   r/   r     r   z+StackTransform.__init__.<locals>.<listcomp>r@   )r   r'   r(   r   r?  r   )r*   rE  r   r+   r,   r@   r/   r(     s   

zStackTransform.__init__r"   c                 C   s   | j |kr| S t| j| j|S rC   )r#   r   r?  r   r)   r.   r.   r/   rB     s   
zStackTransform.with_cachec                    s     fddt  jD S )Nc                    s   g | ]	}  j|qS r.   )selectr   )r   ir*   r  r.   r/   r     s    z)StackTransform._slice.<locals>.<listcomp>)ranger   r   rZ  r.   rZ  r/   _slice  r9  zStackTransform._slicec                 C   s   |   | j   kr|  k sJ  J || j t| jks!J g }t| || jD ]\}}||| q,tj|| j dS Nr  )	r   r   r   r?  r   r\  r   r   stack)r*   rJ   rJ  rM  rL  r.   r.   r/   rI     s   (zStackTransform._callc                 C   s   |   | j   kr|  k sJ  J || j t| jks!J g }t| || jD ]\}}||| q,tj	|| j dS r]  )
r   r   r   r?  r   r\  r   r;   r   r^  )r*   rM   rN  rO  rL  r.   r.   r/   rP     s   (zStackTransform._inversec           	      C   s   |   | j   kr|  k sJ  J || j t| jks!J |   | j   kr2|  k s5J  J || j t| jksBJ g }| |}| |}t||| jD ]\}}}|||| qUtj	|| j dS r]  )
r   r   r   r?  r\  r   r   rV   r   r^  )	r*   rJ   rM   rP  rJ  rN  rM  rO  rL  r.   r.   r/   rV     s   ((

z#StackTransform.log_abs_det_jacobianr4   c                 C   r   )Nc                 s   r   rC   r   rB  r.   r.   r/   r     r   z+StackTransform.bijective.<locals>.<genexpr>rR  r6   r.   r.   r/   re     r   zStackTransform.bijectivec                 C      t dd | jD | jS )Nc                 S   r   r.   rT  rB  r.   r.   r/   r     r   z)StackTransform.domain.<locals>.<listcomp>r   r^  r?  r   r6   r.   r.   r/   r         zStackTransform.domainc                 C   r_  )Nc                 S   r   r.   rW  rB  r.   r.   r/   r     r   z+StackTransform.codomain.<locals>.<listcomp>r`  r6   r.   r.   r/   r!     ra  zStackTransform.codomainr  ra   )rW   rb   rc   rd   r   r   rg   r(   rB   r\  rI   rP   rV   rh   r{   re   r   rz   r    r!   rj   r.   r.   r,   r/   r   y  s   
 

r   c                       sf   e Zd ZdZdZejZdZd fdd	Z	e
dejfdd	Zd
d Zdd Zdd ZdddZ  ZS )r   aA  
    Transform via the cumulative distribution function of a probability distribution.

    Args:
        distribution (Distribution): Distribution whose cumulative distribution function to use for
            the transformation.

    Example::

        # Construct a Gaussian copula from a multivariate normal.
        base_dist = MultivariateNormal(
            loc=torch.zeros(2),
            scale_tril=LKJCholesky(2).sample(),
        )
        transform = CumulativeDistributionTransform(Normal(0, 1))
        copula = TransformedDistribution(base_dist, [transform])
    Tr"   r   c                    s   t  j|d || _d S rl   )r'   r(   distribution)r*   rb  r+   r,   r.   r/   r(     s   
z(CumulativeDistributionTransform.__init__r4   c                 C   r   rC   )rb  supportr6   r.   r.   r/   r      r   z&CumulativeDistributionTransform.domainc                 C   ry   rC   )rb  cdfrR   r.   r.   r/   rI     rY   z%CumulativeDistributionTransform._callc                 C   ry   rC   )rb  icdfrT   r.   r.   r/   rP     rY   z(CumulativeDistributionTransform._inversec                 C   ry   rC   )rb  log_probrU   r.   r.   r/   rV     rY   z4CumulativeDistributionTransform.log_abs_det_jacobianc                 C   r   rl   )r#   r   rb  r)   r.   r.   r/   rB     r   z*CumulativeDistributionTransform.with_cacher`   ra   )rW   rb   rc   rd   re   r   r   r!   r?   r(   rh   rf   r    rI   rP   rV   rB   rj   r.   r.   r,   r/   r     s    r   ).r   r   r   r8   typingr   r   torch.nn.functionalnn
functionalr   torch.distributionsr   torch.distributions.utilsr   r   r   r   r   r	   r
   torch.typesr   __all__r   r7   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>   sF    *>zGE+-cS$8jH