
    Ǆg                     ^   d dl Z 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	c 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 g dZ G d d      Z G d	 d
e      Z G d de      Z eg       Z G d de      Z G d de      Z G d de      Z G d de      Zd Z G d de      Z  G d de      Z! G d de      Z" G d de      Z# G d de      Z$ G d  d!e      Z% G d" d#e      Z& G d$ d%e      Z' G d& d'e      Z( G d( d)e      Z) G d* d+e      Z* G d, d-e      Z+ G d. d/e      Z,y)0    N)List)constraints)_sum_rightmostbroadcast_alllazy_propertytril_matrix_to_vecvec_to_tril_matrix)padsoftplus)AbsTransformAffineTransformCatTransformComposeTransformCorrCholeskyTransformCumulativeDistributionTransformExpTransformIndependentTransformLowerCholeskyTransformPositiveDefiniteTransformPowerTransformReshapeTransformSigmoidTransformSoftplusTransformTanhTransformSoftmaxTransformStackTransformStickBreakingTransform	Transformidentity_transformc                        e Zd ZU dZdZej                  ed<   ej                  ed<   d fd	Zd Z	e
d        Ze
d        Ze
d	        Zdd
Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z x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codomainc                 z    || _         d | _        |dk(  rn|dk(  rd| _        nt        d      t        |           y )Nr      )NNzcache_size must be 0 or 1)_cache_size_inv_cached_x_y
ValueErrorsuper__init__)self
cache_size	__class__s     f/home/mcse/projects/flask_80/flask-venv/lib/python3.12/site-packages/torch/distributions/transforms.pyr*   zTransform.__init___   sB    %	?1_)D899    c                 D    | j                   j                         }d |d<   |S )Nr&   )__dict__copy)r+   states     r.   __getstate__zTransform.__getstate__j   s"    ""$fr/   c                     | 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+   s    r.   r6   zTransform.event_dimo   s:    ;;  DMM$;$;;;;(((UVVr/   c                     d}| j                   | j                         }|%t        |       }t        j                  |      | _         |S )z{
        Returns the inverse :class:`Transform` of this transform.
        This should satisfy ``t.inv.inv is t``.
        N)r&   _InverseTransformweakrefref)r+   invs     r.   r<   zTransform.invu   sB     99 ))+C;#D)CC(DI
r/   c                     t         )z
        Returns the sign of the determinant of the Jacobian, if applicable.
        In general this only makes sense for bijective transforms.
        NotImplementedErrorr7   s    r.   signzTransform.sign   s
     "!r/   c                     | j                   |k(  r| S t        |       j                  t        j                  u r t        |       |      S t	        t        |        d      )Nr,   z.with_cache is not implemented)r%   typer*   r   r?   r+   r,   s     r.   
with_cachezTransform.with_cache   sU    z)K:)"4"444:44!T$ZL0N"OPPr/   c                 
    | |u S N r+   others     r.   __eq__zTransform.__eq__   s    u}r/   c                 &    | j                  |       S rG   )rK   rI   s     r.   __ne__zTransform.__ne__   s    ;;u%%%r/   c                     | j                   dk(  r| j                  |      S | j                  \  }}||u r|S | j                  |      }||f| _        |S )z2
        Computes the transform `x => y`.
        r   )r%   _callr'   )r+   xx_oldy_oldys        r.   __call__zTransform.__call__   sY     q ::a= ''u:LJJqMa4r/   c                     | j                   dk(  r| j                  |      S | j                  \  }}||u r|S | j                  |      }||f| _        |S )z1
        Inverts the transform `y => x`.
        r   )r%   _inverser'   )r+   rS   rQ   rR   rP   s        r.   	_inv_callzTransform._inv_call   s[     q ==##''u:LMM!a4r/   c                     t         )zD
        Abstract method to compute forward transformation.
        r>   r+   rP   s     r.   rO   zTransform._call   
     "!r/   c                     t         )zD
        Abstract method to compute inverse transformation.
        r>   r+   rS   s     r.   rV   zTransform._inverse   rZ   r/   c                     t         )zU
        Computes the log det jacobian `log |dy/dx|` given input and output.
        r>   r+   rP   rS   s      r.   log_abs_det_jacobianzTransform.log_abs_det_jacobian   rZ   r/   c                 4    | j                   j                  dz   S )Nz())r-   __name__r7   s    r.   __repr__zTransform.__repr__   s    ~~&&--r/   c                     |S )z{
        Infers the shape of the forward computation, given the input shape.
        Defaults to preserving shape.
        rH   r+   shapes     r.   forward_shapezTransform.forward_shape   	    
 r/   c                     |S )z}
        Infers the shapes of the inverse computation, given the output shape.
        Defaults to preserving shape.
        rH   rd   s     r.   inverse_shapezTransform.inverse_shape   rg   r/   r   r$   )ra   
__module____qualname____doc__	bijectiver   
Constraint__annotations__r*   r4   propertyr6   r<   r@   rE   rK   rM   rT   rW   rO   rV   r_   rb   rf   ri   __classcell__r-   s   @r.   r   r   .   s    *X I"""$$$	
 W W
   " "Q&""".r/   r   c                        e Zd ZdZdef fdZ ej                  d      d        Z ej                  d      d        Z	e
d        Ze
d	        Ze
d
        ZddZd Zd Zd Zd Zd Zd Z xZS )r9   z|
    Inverts a single :class:`Transform`.
    This class is private; please instead use the ``Transform.inv`` property.
    	transformc                 H    t         |   |j                         || _        y NrB   )r)   r*   r%   r&   )r+   rv   r-   s     r.   r*   z_InverseTransform.__init__   s     I$9$9:(	r/   Fis_discretec                 J    | j                   J | j                   j                  S rG   )r&   r"   r7   s    r.   r!   z_InverseTransform.domain   s"    yy$$$yy!!!r/   c                 J    | j                   J | j                   j                  S rG   )r&   r!   r7   s    r.   r"   z_InverseTransform.codomain   s"    yy$$$yyr/   c                 J    | j                   J | j                   j                  S rG   )r&   ro   r7   s    r.   ro   z_InverseTransform.bijective   s"    yy$$$yy"""r/   c                 J    | j                   J | j                   j                  S rG   )r&   r@   r7   s    r.   r@   z_InverseTransform.sign   s     yy$$$yy~~r/   c                     | j                   S rG   )r&   r7   s    r.   r<   z_InverseTransform.inv   s    yyr/   c                 h    | j                   J | j                  j                  |      j                  S rG   )r&   r<   rE   rD   s     r.   rE   z_InverseTransform.with_cache   s-    yy$$$xx"":.222r/   c                 r    t        |t              sy| j                  J | j                  |j                  k(  S NF)
isinstancer9   r&   rI   s     r.   rK   z_InverseTransform.__eq__   s3    %!23yy$$$yyEJJ&&r/   c                 `    | j                   j                   dt        | j                         dS )N())r-   ra   reprr&   r7   s    r.   rb   z_InverseTransform.__repr__  s)    ..))*!DO+<A>>r/   c                 T    | j                   J | j                   j                  |      S rG   )r&   rW   rY   s     r.   rT   z_InverseTransform.__call__  s'    yy$$$yy""1%%r/   c                 X    | j                   J | j                   j                  ||       S rG   )r&   r_   r^   s      r.   r_   z&_InverseTransform.log_abs_det_jacobian
  s,    yy$$$		..q!444r/   c                 8    | j                   j                  |      S rG   )r&   ri   rd   s     r.   rf   z_InverseTransform.forward_shape      yy&&u--r/   c                 8    | j                   j                  |      S rG   )r&   rf   rd   s     r.   ri   z_InverseTransform.inverse_shape  r   r/   rk   )ra   rl   rm   rn   r   r*   r   dependent_propertyr!   r"   rr   ro   r@   r<   rE   rK   rb   rT   r_   rf   ri   rs   rt   s   @r.   r9   r9      s    
)) ) $[##6" 7" $[##6  7  # #    3'?&5..r/   r9   c                        e Zd ZdZddee   f fdZd Z ej                  d      d        Z
 ej                  d      d        Zed	        Zed
        Zed        ZddZd Zd Zd Zd Zd Z x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.
    partsc                 ~    |r|D cg c]  }|j                  |       }}t        | 	  |       || _        y c c}w rx   )rE   r)   r*   r   )r+   r   r,   partr-   s       r.   r*   zComposeTransform.__init__   s?    =BCTT__Z0CECJ/
 Ds   :c                 V    t        |t              sy| j                  |j                  k(  S r   )r   r   r   rI   s     r.   rK   zComposeTransform.__eq__&  s#    %!12zzU[[((r/   Fry   c                    | j                   st        j                  S | j                   d   j                  }| j                   d   j                  j
                  }t        | j                         D ]R  }||j                  j
                  |j                  j
                  z
  z  }t        ||j                  j
                        }T ||j
                  k\  sJ ||j
                  kD  r#t        j                  |||j
                  z
        }|S )Nr   )	r   r   realr!   r"   r6   reversedmaxindependent)r+   r!   r6   r   s       r.   r!   zComposeTransform.domain+  s    zz###A%%JJrN++55	TZZ( 	>D..1H1HHHIIt{{'<'<=I	> F,,,,,v''' ,,VYAQAQ5QRFr/   c                    | j                   st        j                  S | j                   d   j                  }| j                   d   j                  j
                  }| j                   D ]R  }||j                  j
                  |j                  j
                  z
  z  }t        ||j                  j
                        }T ||j
                  k\  sJ ||j
                  kD  r#t        j                  |||j
                  z
        }|S )Nr   r   )r   r   r   r"   r!   r6   r   r   )r+   r"   r6   r   s       r.   r"   zComposeTransform.codomain:  s    zz###::b>**JJqM((22	JJ 	@D004;;3H3HHHIIt}}'>'>?I	@ H.....x)))"..xXEWEW9WXHr/   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wrG   ro   ).0ps     r.   	<genexpr>z-ComposeTransform.bijective.<locals>.<genexpr>K  s     311;;3   )allr   r7   s    r.   ro   zComposeTransform.bijectiveI  s    3

333r/   c                 J    d}| j                   D ]  }||j                  z  } |S Nr$   )r   r@   )r+   r@   r   s      r.   r@   zComposeTransform.signM  s,     	!A!&&=D	!r/   c                 $   d }| j                   | j                         }|jt        t        | j                        D cg c]  }|j                   c}      }t        j                  |      | _         t        j                  |       |_         |S c c}w rG   )r&   r   r   r   r<   r:   r;   )r+   r<   r   s      r.   r<   zComposeTransform.invT  sn    99 ))+C;"8DJJ3G#HaAEE#HICC(DI{{4(CH
 $Is   Bc                 R    | j                   |k(  r| S t        | j                  |      S rx   )r%   r   r   rD   s     r.   rE   zComposeTransform.with_cache_  s&    z)K

zBBr/   c                 8    | j                   D ]
  } ||      } |S rG   )r   )r+   rP   r   s      r.   rT   zComposeTransform.__call__d  s#    JJ 	DQA	r/   c           	      p   | j                   st        j                  |      S |g}| j                   d d D ]  }|j                   ||d                 |j                  |       g }| j                  j
                  }t        | j                   |d d |dd        D ]x  \  }}}|j                  t        |j                  ||      ||j                  j
                  z
               ||j                  j
                  |j                  j
                  z
  z  }z t        j                  t        j                  |      S )Nr   r$   )r   torch
zeros_likeappendr!   r6   zipr   r_   r"   	functoolsreduceoperatoradd)r+   rP   rS   xsr   termsr6   s          r.   r_   z%ComposeTransform.log_abs_det_jacobiani  s   zz##A&& SJJsO 	$DIId2b6l#	$
		!KK))	djj"Sb'2ab6: 	IJD!QLL--a3YAVAV5V
 004;;3H3HHHI	I e44r/   c                 J    | j                   D ]  }|j                  |      } |S rG   )r   rf   r+   re   r   s      r.   rf   zComposeTransform.forward_shape~  s*    JJ 	.D&&u-E	.r/   c                 \    t        | j                        D ]  }|j                  |      } |S rG   )r   r   ri   r   s      r.   ri   zComposeTransform.inverse_shape  s/    TZZ( 	.D&&u-E	.r/   c                     | j                   j                  dz   }|dj                  | j                  D cg c]  }|j	                          c}      z  }|dz  }|S c c}w )Nz(
    z,
    z
))r-   ra   joinr   rb   )r+   
fmt_stringr   s      r.   rb   zComposeTransform.__repr__  sT    ^^,,y8
innDJJ%Gqajjl%GHH
e
 &Hs   A
rj   rk   )ra   rl   rm   rn   r   r   r*   rK   r   r   r!   r"   r   ro   r@   rr   r<   rE   rT   r_   rf   ri   rb   rs   rt   s   @r.   r   r     s    d9o )
 $[##6 7 $[##6 7 4 4    C

5*

r/   r   c                        e Zd ZdZd fd	ZddZ ej                  d      d        Z ej                  d      d        Z	e
d        Ze
d	        Zd
 Zd Zd Zd Zd Zd Z x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.
    c                 `    t         |   |       |j                  |      | _        || _        y rx   )r)   r*   rE   base_transformreinterpreted_batch_ndims)r+   r   r   r,   r-   s       r.   r*   zIndependentTransform.__init__  s.    J/,77
C)B&r/   c                 h    | j                   |k(  r| S t        | j                  | j                  |      S rx   )r%   r   r   r   rD   s     r.   rE   zIndependentTransform.with_cache  s5    z)K#!?!?J
 	
r/   Fry   c                 j    t        j                  | j                  j                  | j                        S rG   )r   r   r   r!   r   r7   s    r.   r!   zIndependentTransform.domain  s,    &&&&(F(F
 	
r/   c                 j    t        j                  | j                  j                  | j                        S rG   )r   r   r   r"   r   r7   s    r.   r"   zIndependentTransform.codomain  s,    &&(($*H*H
 	
r/   c                 .    | j                   j                  S rG   )r   ro   r7   s    r.   ro   zIndependentTransform.bijective  s    "",,,r/   c                 .    | j                   j                  S rG   )r   r@   r7   s    r.   r@   zIndependentTransform.sign  s    ""'''r/   c                     |j                         | j                  j                  k  rt        d      | j	                  |      S NToo few dimensions on input)dimr!   r6   r(   r   rY   s     r.   rO   zIndependentTransform._call  s7    557T[[***:;;""1%%r/   c                     |j                         | j                  j                  k  rt        d      | j                  j                  |      S r   )r   r"   r6   r(   r   r<   r\   s     r.   rV   zIndependentTransform._inverse  s=    557T]],,,:;;""&&q))r/   c                 j    | j                   j                  ||      }t        || j                        }|S rG   )r   r_   r   r   )r+   rP   rS   results       r.   r_   z)IndependentTransform.log_abs_det_jacobian  s1    $$99!Q?(F(FGr/   c                 z    | j                   j                   dt        | j                         d| j                   dS )Nr   z, r   )r-   ra   r   r   r   r7   s    r.   rb   zIndependentTransform.__repr__  s:    ..))*!D1D1D,E+FbIgIgHhhijjr/   c                 8    | j                   j                  |      S rG   )r   rf   rd   s     r.   rf   z"IndependentTransform.forward_shape      ""0077r/   c                 8    | j                   j                  |      S rG   )r   ri   rd   s     r.   ri   z"IndependentTransform.inverse_shape  r   r/   rj   rk   )ra   rl   rm   rn   r*   rE   r   r   r!   r"   rr   ro   r@   rO   rV   r_   rb   rf   ri   rs   rt   s   @r.   r   r     s    C

 $[##6
 7

 $[##6
 7

 - - ( (&
*

k88r/   r   c                        e Zd ZdZdZd fd	Zej                  d        Zej                  d        Z	ddZ
d Zd Zd	 Zd
 Zd Z xZS )r   aM  
    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.
    Tc                    t        j                  |      | _        t        j                  |      | _        | j                  j	                         | j                  j	                         k7  rt        d      t        |   |       y )Nz6in_shape, out_shape have different numbers of elementsrB   )r   Sizein_shape	out_shapenumelr(   r)   r*   )r+   r   r   r,   r-   s       r.   r*   zReshapeTransform.__init__  sa    

8,I.== DNN$8$8$::UVVJ/r/   c                 p    t        j                  t         j                  t        | j                              S rG   )r   r   r   lenr   r7   s    r.   r!   zReshapeTransform.domain  s$    &&{'7'7T]]9KLLr/   c                 p    t        j                  t         j                  t        | j                              S rG   )r   r   r   r   r   r7   s    r.   r"   zReshapeTransform.codomain  s$    &&{'7'7T^^9LMMr/   c                 h    | j                   |k(  r| S t        | j                  | j                  |      S rx   )r%   r   r   r   rD   s     r.   rE   zReshapeTransform.with_cache  s,    z)Kt~~*UUr/   c                     |j                   d |j                         t        | j                        z
   }|j	                  || j
                  z         S rG   )re   r   r   r   reshaper   )r+   rP   batch_shapes      r.   rO   zReshapeTransform._call  s?    gg<#dmm*< <=yyt~~566r/   c                     |j                   d |j                         t        | j                        z
   }|j	                  || j
                  z         S rG   )re   r   r   r   r   r   )r+   rS   r   s      r.   rV   zReshapeTransform._inverse  s?    gg=#dnn*= =>yyt}}455r/   c                     |j                   d |j                         t        | j                        z
   }|j	                  |      S rG   )re   r   r   r   	new_zeros)r+   rP   rS   r   s       r.   r_   z%ReshapeTransform.log_abs_det_jacobian  s6    gg<#dmm*< <={{;''r/   c                     t        |      t        | j                        k  rt        d      t        |      t        | j                        z
  }||d  | j                  k7  rt        d||d   d| j                         |d | | j                  z   S Nr   zShape mismatch: expected z	 but got )r   r   r(   r   r+   re   cuts      r.   rf   zReshapeTransform.forward_shape  s    u:DMM**:;;%j3t}}--;$--'+E#$K=	$--Q  Tc{T^^++r/   c                     t        |      t        | j                        k  rt        d      t        |      t        | j                        z
  }||d  | j                  k7  rt        d||d   d| j                         |d | | j                  z   S r   )r   r   r(   r   r   s      r.   ri   zReshapeTransform.inverse_shape  s    u:DNN++:;;%j3t~~..;$..(+E#$K=	$..AQR  Tc{T]]**r/   rj   rk   )ra   rl   rm   rn   ro   r*   r   r   r!   r"   rE   rO   rV   r_   rf   ri   rs   rt   s   @r.   r   r     sk    	 I0 ##M $M ##N $NV
76(,+r/   r   c                   `    e Zd ZdZej
                  Zej                  ZdZ	dZ
d Zd Zd Zd Zy)	r   z8
    Transform via the mapping :math:`y = \exp(x)`.
    Tr$   c                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zExpTransform.__eq__%      %..r/   c                 "    |j                         S rG   )exprY   s     r.   rO   zExpTransform._call(      uuwr/   c                 "    |j                         S rG   logr\   s     r.   rV   zExpTransform._inverse+  r   r/   c                     |S rG   rH   r^   s      r.   r_   z!ExpTransform.log_abs_det_jacobian.      r/   Nra   rl   rm   rn   r   r   r!   positiver"   ro   r@   rK   rO   rV   r_   rH   r/   r.   r   r     s=     F##HID/r/   r   c                        e Zd ZdZej
                  Zej
                  ZdZd fd	Z	ddZ
ed        Zd Zd Zd Zd	 Zd
 Zd Z xZS )r   zD
    Transform via the mapping :math:`y = x^{\text{exponent}}`.
    Tc                 J    t         |   |       t        |      \  | _        y rx   )r)   r*   r   exponent)r+   r   r,   r-   s      r.   r*   zPowerTransform.__init__:  s"    J/(2r/   c                 R    | j                   |k(  r| S t        | j                  |      S rx   )r%   r   r   rD   s     r.   rE   zPowerTransform.with_cache>  s&    z)Kdmm
CCr/   c                 6    | j                   j                         S rG   )r   r@   r7   s    r.   r@   zPowerTransform.signC  s    }}!!##r/   c                     t        |t              sy| j                  j                  |j                        j	                         j                         S r   )r   r   r   eqr   itemrI   s     r.   rK   zPowerTransform.__eq__G  s:    %0}}/335::<<r/   c                 8    |j                  | j                        S rG   powr   rY   s     r.   rO   zPowerTransform._callL  s    uuT]]##r/   c                 >    |j                  d| j                  z        S r   r   r\   s     r.   rV   zPowerTransform._inverseO  s    uuQ&''r/   c                 ^    | j                   |z  |z  j                         j                         S rG   )r   absr   r^   s      r.   r_   z#PowerTransform.log_abs_det_jacobianR  s(    !A%**,0022r/   c                 X    t        j                  |t        | j                  dd            S Nre   rH   r   broadcast_shapesgetattrr   rd   s     r.   rf   zPowerTransform.forward_shapeU  "    %%eWT]]GR-PQQr/   c                 X    t        j                  |t        | j                  dd            S r   r   rd   s     r.   ri   zPowerTransform.inverse_shapeX  r  r/   rj   rk   )ra   rl   rm   rn   r   r   r!   r"   ro   r*   rE   r   r@   rK   rO   rV   r_   rf   ri   rs   rt   s   @r.   r   r   2  sd     !!F##HI3D
 $ $=
$(3RRr/   r   c                     t        j                  | j                        }t        j                  t        j                  |       |j
                  d|j                  z
        S N      ?minr   )r   finfodtypeclampsigmoidtinyeps)rP   r  s     r.   _clipped_sigmoidr  \  s<    KK E;;u}}Q'UZZS599_MMr/   c                   `    e Zd ZdZej
                  Zej                  ZdZ	dZ
d Zd Zd Zd Zy)	r   zg
    Transform via the mapping :math:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.
    Tr$   c                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zSigmoidTransform.__eq__j      %!122r/   c                     t        |      S rG   )r  rY   s     r.   rO   zSigmoidTransform._callm  s    ""r/   c                     t        j                  |j                        }|j                  |j                  d|j
                  z
        }|j                         | j                         z
  S r  )r   r  r	  r
  r  r  r   log1p)r+   rS   r  s      r.   rV   zSigmoidTransform._inversep  sK    AGG$GG

eiiG8uuw1"%%r/   c                 \    t        j                  |        t        j                  |      z
  S rG   )Fr   r^   s      r.   r_   z%SigmoidTransform.log_abs_det_jacobianu  s!    

A2A..r/   N)ra   rl   rm   rn   r   r   r!   unit_intervalr"   ro   r@   rK   rO   rV   r_   rH   r/   r.   r   r   a  s=     F((HID3#&
/r/   r   c                   `    e Zd ZdZej
                  Zej                  ZdZ	dZ
d Zd Zd Zd Zy)	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                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zSoftplusTransform.__eq__  s    %!233r/   c                     t        |      S rG   r   rY   s     r.   rO   zSoftplusTransform._call  s    {r/   c                 b    | j                         j                         j                         |z   S rG   )expm1negr   r\   s     r.   rV   zSoftplusTransform._inverse  s'    zz|!%%'!++r/   c                     t        |        S rG   r  r^   s      r.   r_   z&SoftplusTransform.log_abs_det_jacobian  s    !}r/   Nr   rH   r/   r.   r   r   y  s=     F##HID4,r/   r   c                   n    e Zd ZdZej
                  Z ej                  dd      ZdZ	dZ
d Zd Zd Zd	 Zy
)r   a~  
    Transform via the mapping :math:`y = \tanh(x)`.

    It is equivalent to
    ```
    ComposeTransform([AffineTransform(0., 2.), SigmoidTransform(), AffineTransform(-1., 2.)])
    ```
    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                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zTanhTransform.__eq__  s    %//r/   c                 "    |j                         S rG   )tanhrY   s     r.   rO   zTanhTransform._call  s    vvxr/   c                 ,    t        j                  |      S rG   )r   atanhr\   s     r.   rV   zTanhTransform._inverse  s     {{1~r/   c                 V    dt        j                  d      |z
  t        d|z        z
  z  S )N       @g       )mathr   r   r^   s      r.   r_   z"TanhTransform.log_abs_det_jacobian  s*     dhhsma'(4!8*<<==r/   N)ra   rl   rm   rn   r   r   r!   intervalr"   ro   r@   rK   rO   rV   r_   rH   r/   r.   r   r     sF     F#{##D#.HID0
>r/   r   c                   R    e Zd ZdZej
                  Zej                  Zd Z	d Z
d Zy)r   z4
    Transform via the mapping :math:`y = |x|`.
    c                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zAbsTransform.__eq__  r   r/   c                 "    |j                         S rG   )r   rY   s     r.   rO   zAbsTransform._call  r   r/   c                     |S rG   rH   r\   s     r.   rV   zAbsTransform._inverse  r   r/   N)ra   rl   rm   rn   r   r   r!   r   r"   rK   rO   rV   rH   r/   r.   r   r     s.     F##H/r/   r   c                        e Zd ZdZdZd fd	Zed        Z ej                  d      d        Z
 ej                  d      d        Zdd	Zd
 Zed        Zd Zd Zd Zd Zd Z x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.
    Tc                 P    t         |   |       || _        || _        || _        y rx   )r)   r*   locscale
_event_dim)r+   r0  r1  r6   r,   r-   s        r.   r*   zAffineTransform.__init__  s(    J/
#r/   c                     | j                   S rG   )r2  r7   s    r.   r6   zAffineTransform.event_dim  s    r/   Fry   c                     | j                   dk(  rt        j                  S t        j                  t        j                  | j                         S Nr   r6   r   r   r   r7   s    r.   r!   zAffineTransform.domain  7    >>Q###&&{'7'7HHr/   c                     | j                   dk(  rt        j                  S t        j                  t        j                  | j                         S r5  r6  r7   s    r.   r"   zAffineTransform.codomain  r7  r/   c                 ~    | j                   |k(  r| S t        | j                  | j                  | j                  |      S rx   )r%   r   r0  r1  r6   rD   s     r.   rE   zAffineTransform.with_cache  s7    z)KHHdjj$..Z
 	
r/   c                    t        |t              syt        | j                  t        j                        r>t        |j                  t        j                        r| j                  |j                  k7  r7y| j                  |j                  k(  j                         j                         syt        | j                  t        j                        r?t        |j                  t        j                        r| j                  |j                  k7  ryy| j                  |j                  k(  j                         j                         syy)NFT)r   r   r0  numbersNumberr   r   r1  rI   s     r.   rK   zAffineTransform.__eq__  s    %1dhh/JIIw~~5
 xx599$HH		)..0557djj'..1jKK7
 zzU[[(
  JJ%++-22499;r/   c                     t        | j                  t        j                        r6t	        | j                        dkD  rdS t	        | j                        dk  rdS dS | j                  j                         S )Nr   r$   r   )r   r1  r;  Realfloatr@   r7   s    r.   r@   zAffineTransform.sign  sV    djj',,/djj)A-1Utzz9JQ9N2UTUUzz  r/   c                 :    | j                   | j                  |z  z   S rG   r0  r1  rY   s     r.   rO   zAffineTransform._call  s    xx$**q.((r/   c                 :    || j                   z
  | j                  z  S rG   rA  r\   s     r.   rV   zAffineTransform._inverse  s    DHH

**r/   c                    |j                   }| j                  }t        |t        j                        r3t        j                  |t        j                  t        |                  }n#t        j                  |      j                         }| j                  rQ|j                         d | j                    dz   }|j                  |      j                  d      }|d | j                    }|j                  |      S )N)r   r   )re   r1  r   r;  r>  r   	full_liker(  r   r   r6   sizeviewsumexpand)r+   rP   rS   re   r1  r   result_sizes          r.   r_   z$AffineTransform.log_abs_det_jacobian  s    

eW\\*__QU(<=FYYu%))+F>> ++-(94>>/:UBK[[-11"5F+T^^O,E}}U##r/   c           	          t        j                  |t        | j                  dd      t        | j                  dd            S r   r   r   r   r0  r1  rd   s     r.   rf   zAffineTransform.forward_shape   7    %%7488Wb174::wPR3S
 	
r/   c           	          t        j                  |t        | j                  dd      t        | j                  dd            S r   rK  rd   s     r.   ri   zAffineTransform.inverse_shape%  rL  r/   r   r   rk   )ra   rl   rm   rn   ro   r*   rr   r6   r   r   r!   r"   rE   rK   r@   rO   rV   r_   rf   ri   rs   rt   s   @r.   r   r     s    	 I$   $[##6I 7I
 $[##6I 7I

0 ! !
)+$


r/   r   c                   d    e Zd ZdZej
                  Zej                  ZdZ	d Z
d Zd	dZd Zd Zy)
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                    t        j                  |      }t        j                  |j                        j                  }|j                  d|z   d|z
        }t        |d      }|dz  }d|z
  j                         j                  d      }|t        j                  |j                  d   |j                  |j                        z   }|t        |dd df   ddgd	      z  }|S )
Nr   r$   r  diag   )r	  device.r   value)r   r#  r  r	  r  r
  r	   sqrtcumprodeyere   rT  r
   )r+   rP   r  rzz1m_cumprod_sqrtrS   s          r.   rO   zCorrCholeskyTransform._call?  s    JJqMkk!''"&&GGSa#gG.qr* qDE<<>11"5		!''"+QWWQXXFF$S#2#X.Aa@@r/   c                 ,   dt        j                  ||z  d      z
  }t        |dd df   ddgd      }t        |d      }t        |d      }||j	                         z  }|j                         |j                         j                         z
  dz  }|S )	Nr$   r   r   .r   rU  rQ  rS  )r   cumsumr
   r   rW  r  r  )r+   rS   y_cumsumy_cumsum_shiftedy_vecy_cumsum_vectrP   s           r.   rV   zCorrCholeskyTransform._inverseN  s     u||AEr22xSbS1Aq6C"12.)*:D\''))WWY(A-r/   Nc                    d||z  j                  d      z
  }t        |d      }d|j                         j                  d      z  }d|t	        d|z        z   t        j                  d      z
  j                  d      z  }||z   S )Nr$   r   r^  rQ        ?r'  )r_  r   r   rG  r   r(  )r+   rP   rS   intermediates
y1m_cumsumy1m_cumsum_trilstick_breaking_logdettanh_logdets           r.   r_   z*CorrCholeskyTransform.log_abs_det_jacobianZ  s     !a%B//
 -ZbA #&;&;&=&A&A"&E EAa 00488C=@EE"EMM${22r/   c                     t        |      dk  rt        d      |d   }t        dd|z  z   dz  dz         }||dz
  z  dz  |k7  rt        d      |d d ||fz   S )Nr$   r   r   g      ?rS  rg  z-Input is not a flattend lower-diagonal number)r   r(   round)r+   re   NDs       r.   rf   z#CorrCholeskyTransform.forward_shapeh  st    u:>:;;"I4!a%<C'#-.A;!q LMMSbzQF""r/   c                     t        |      dk  rt        d      |d   |d   k7  rt        d      |d   }||dz
  z  dz  }|d d |fz   S )NrS  r   rf  r   zInput is not squarer$   r   r(   )r+   re   rp  ro  s       r.   ri   z#CorrCholeskyTransform.inverse_shaper  sc    u:>:;;9b	!233"IQK1SbzQD  r/   rG   )ra   rl   rm   rn   r   real_vectorr!   corr_choleskyr"   ro   rO   rV   r_   rf   ri   rH   r/   r.   r   r   +  s=     $$F((HI
3#!r/   r   c                   ^    e Zd ZdZej
                  Zej                  Zd Z	d Z
d Zd Zd Zy)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                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zSoftmaxTransform.__eq__  r  r/   c                 |    |}||j                  dd      d   z
  j                         }||j                  dd      z  S )Nr   Tr   )r   r   rG  )r+   rP   logprobsprobss       r.   rO   zSoftmaxTransform._call  s@    HLLT2155::<uyyT***r/   c                 &    |}|j                         S rG   r   )r+   rS   ry  s      r.   rV   zSoftmaxTransform._inverse  s    yy{r/   c                 8    t        |      dk  rt        d      |S Nr$   r   rr  rd   s     r.   rf   zSoftmaxTransform.forward_shape      u:>:;;r/   c                 8    t        |      dk  rt        d      |S r|  rr  rd   s     r.   ri   zSoftmaxTransform.inverse_shape  r}  r/   N)ra   rl   rm   rn   r   rs  r!   simplexr"   rK   rO   rV   rf   ri   rH   r/   r.   r   r   }  s8     $$F""H3+

r/   r   c                   h    e Zd ZdZej
                  Zej                  ZdZ	d Z
d Zd Zd Zd Zd Zy	)
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                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zStickBreakingTransform.__eq__      %!788r/   c                 (   |j                   d   dz   |j                  |j                   d         j                  d      z
  }t        ||j	                         z
        }d|z
  j                  d      }t        |ddgd      t        |ddgd      z  }|S )Nr   r$   r   rU  )re   new_onesr_  r  r   rX  r
   )r+   rP   offsetr[  	z_cumprodrS   s         r.   rO   zStickBreakingTransform._call  s    q1::aggbk#:#A#A"#EEQ-.UOOB'	Aq6#c)aV1&EEr/   c                    |dd df   }|j                   d   |j                  |j                   d         j                  d      z
  }d|j                  d      z
  }t        j                  |t        j
                  |j                        j                        }|j                         |j                         z
  |j                         z   }|S )N.r   r$   )r  )	re   r  r_  r   r
  r  r	  r  r   )r+   rS   y_cropr  sfrP   s         r.   rV   zStickBreakingTransform._inverse  s    38qzz&,,r*:;BB2FFr"" [[QWW!5!:!:;JJL2668#fjjl2r/   c                 ,   |j                   d   dz   |j                  |j                   d         j                  d      z
  }||j                         z
  }| t	        j
                  |      z   |dd df   j                         z   j                  d      }|S )Nr   r$   .)re   r  r_  r   r  
logsigmoidrG  )r+   rP   rS   r  detJs        r.   r_   z+StickBreakingTransform.log_abs_det_jacobian  s    q1::aggbk#:#A#A"#EE

Q\\!_$qcrc{'88==bAr/   c                 R    t        |      dk  rt        d      |d d |d   dz   fz   S Nr$   r   r   rr  rd   s     r.   rf   z$StickBreakingTransform.forward_shape  5    u:>:;;SbzU2Y],,,r/   c                 R    t        |      dk  rt        d      |d d |d   dz
  fz   S r  rr  rd   s     r.   ri   z$StickBreakingTransform.inverse_shape  r  r/   N)ra   rl   rm   rn   r   rs  r!   r  r"   ro   rK   rO   rV   r_   rf   ri   rH   r/   r.   r   r     sB     $$F""HI9-
-r/   r   c                   t    e Zd ZdZ ej
                  ej                  d      Zej                  Z	d Z
d Zd Zy)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.
    rS  c                 "    t        |t              S rG   )r   r   rI   s     r.   rK   zLowerCholeskyTransform.__eq__  r  r/   c                     |j                  d      |j                  dd      j                         j                         z   S Nr   rf  )dim1dim2)trildiagonalr   
diag_embedrY   s     r.   rO   zLowerCholeskyTransform._call  4    vvbzAJJBRJ8<<>IIKKKr/   c                     |j                  d      |j                  dd      j                         j                         z   S r  )r  r  r   r  r\   s     r.   rV   zLowerCholeskyTransform._inverse  r  r/   N)ra   rl   rm   rn   r   r   r   r!   lower_choleskyr"   rK   rO   rV   rH   r/   r.   r   r     s?     %[$$[%5%5q9F))H9LLr/   r   c                   t    e Zd ZdZ ej
                  ej                  d      Zej                  Z	d Z
d Zd Zy)r   zN
    Transform from unconstrained matrices to positive-definite matrices.
    rS  c                 "    t        |t              S rG   )r   r   rI   s     r.   rK   z PositiveDefiniteTransform.__eq__  s    %!:;;r/   c                 @     t               |      }||j                  z  S rG   )r   mTrY   s     r.   rO   zPositiveDefiniteTransform._call  s    $"$Q'144xr/   c                 r    t         j                  j                  |      }t               j	                  |      S rG   )r   linalgcholeskyr   r<   r\   s     r.   rV   z"PositiveDefiniteTransform._inverse  s*    LL!!!$%'++A..r/   N)ra   rl   rm   rn   r   r   r   r!   positive_definiter"   rK   rO   rV   rH   r/   r.   r   r     s=     %[$$[%5%5q9F,,H</r/   r   c                        e Zd ZU dZee   ed<   d fd	Zed        Z	ed        Z
ddZd Zd Zd	 Zed
        Zej$                  d        Zej$                  d        Z x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)
    
transformsc                 v   t        d |D              sJ |r|D cg c]  }|j                  |       }}t        |   |       t	        |      | _        |dgt        | j
                        z  }t	        |      | _        t        | j                        t        | j
                        k(  sJ || _        y c c}w )Nc              3   <   K   | ]  }t        |t                y wrG   r   r   r   rd  s     r.   r   z(CatTransform.__init__.<locals>.<genexpr>       ::a+:   rB   r$   )	r   rE   r)   r*   listr  r   lengthsr   )r+   tseqr   r  r,   rd  r-   s         r.   r*   zCatTransform.__init__  s    :T::::6:;ALL,;D;J/t*?cC00GG}4<< C$8888 <s   B6c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wrG   )r6   r  s     r.   r   z)CatTransform.event_dim.<locals>.<genexpr>!       811;;8r   )r   r  r7   s    r.   r6   zCatTransform.event_dim      8888r/   c                 ,    t        | j                        S rG   )rG  r  r7   s    r.   lengthzCatTransform.length#  s    4<<  r/   c                 |    | j                   |k(  r| S t        | j                  | j                  | j                  |      S rG   )r%   r   r  r   r  rD   s     r.   rE   zCatTransform.with_cache'  s2    z)KDOOTXXt||ZPPr/   c                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         | j                  k(  sJ g }d}t        | j                  | j
                        D ]>  \  }}|j                  | j                   ||      }|j                   ||             ||z   }@ t        j                  || j                         S Nr   r^  )
r   rE  r  r   r  r  narrowr   r   cat)r+   rP   yslicesstarttransr  xslices          r.   rO   zCatTransform._call,  s    x488-aeeg-----vvdhh4;;... $,,? 	#ME6XXdhhv6FNN5=)FNE	# yydhh//r/   c                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         | j                  k(  sJ g }d}t        | j                  | j
                        D ]G  \  }}|j                  | j                   ||      }|j                  |j                  |             ||z   }I t        j                  || j                         S r  )r   rE  r  r   r  r  r  r   r<   r   r  )r+   rS   xslicesr  r  r  yslices          r.   rV   zCatTransform._inverse7  s    x488-aeeg-----vvdhh4;;... $,,? 	#ME6XXdhhv6FNN599V,-FNE	# yydhh//r/   c                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         | j                  k(  sJ |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         | j                  k(  sJ g }d}t        | j                  | j
                        D ]  \  }}|j                  | j                   ||      }|j                  | j                   ||      }|j                  ||      }	|j                  | j                  k  r#t        |	| j                  |j                  z
        }	|j                  |	       ||z   } | j                   }
|
dk\  r|
|j                         z
  }
|
| j                  z   }
|
dk  rt        j                  ||
      S t        |      S r  )r   rE  r  r   r  r  r  r_   r6   r   r   r   r  rG  )r+   rP   rS   
logdetjacsr  r  r  r  r  	logdetjacr   s              r.   r_   z!CatTransform.log_abs_det_jacobianB  s   x488-aeeg-----vvdhh4;;...x488-aeeg-----vvdhh4;;...
 $,,? 	#ME6XXdhhv6FXXdhhv6F2266BI/*9dnnu6VW	i(FNE	# hh!8-CDNN"799ZS11z?"r/   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wrG   r   r  s     r.   r   z)CatTransform.bijective.<locals>.<genexpr>]  r  r   r   r  r7   s    r.   ro   zCatTransform.bijective[  r  r/   c                     t        j                  | j                  D cg c]  }|j                   c}| j                  | j
                        S c c}w rG   )r   r  r  r!   r   r  r+   rd  s     r.   r!   zCatTransform.domain_  s8    #/!QXX/4<<
 	
/   Ac                     t        j                  | j                  D cg c]  }|j                   c}| j                  | j
                        S c c}w rG   )r   r  r  r"   r   r  r  s     r.   r"   zCatTransform.codomaine  s8    !%1AQZZ1488T\\
 	
1r  )r   Nr   rk   )ra   rl   rm   rn   r   r   rq   r*   r   r6   r  rE   rO   rV   r_   rr   ro   r   r   r!   r"   rs   rt   s   @r.   r   r     s     Y
 9 9 ! !Q
	0	0#2 9 9 ##
 $

 ##
 $
r/   r   c                        e Zd ZU dZee   ed<   d fd	ZddZd Z	d Z
d Zd Zed	        Zej                   d
        Zej                   d        Z x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  c                     t        d |D              sJ |r|D cg c]  }|j                  |       }}t        |   |       t	        |      | _        || _        y c c}w )Nc              3   <   K   | ]  }t        |t                y wrG   r  r  s     r.   r   z*StackTransform.__init__.<locals>.<genexpr>|  r  r  rB   )r   rE   r)   r*   r  r  r   )r+   r  r   r,   rd  r-   s        r.   r*   zStackTransform.__init__{  s]    :T::::6:;ALL,;D;J/t* <s   Ac                 f    | j                   |k(  r| S t        | j                  | j                  |      S rG   )r%   r   r  r   rD   s     r.   rE   zStackTransform.with_cache  s,    z)KdootxxDDr/   c                     t        |j                  | j                              D cg c]  }|j                  | j                  |        c}S c c}w rG   )rangerE  r   select)r+   r[  is      r.   _slicezStackTransform._slice  s7    /4QVVDHH5E/FG!1%GGGs   #Ac                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         t        | j                        k(  sJ g }t	        | j                  |      | j                        D ]  \  }}|j                   ||              t        j                  || j                         S Nr^  )	r   rE  r   r  r   r  r   r   stack)r+   rP   r  r  r  s        r.   rO   zStackTransform._call  s    x488-aeeg-----vvdhh3t#7777 QA 	*MFENN5=)	*{{711r/   c                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         t        | j                        k(  sJ g }t	        | j                  |      | j                        D ]%  \  }}|j                  |j                  |             ' t        j                  || j                         S r  )
r   rE  r   r  r   r  r   r<   r   r  )r+   rS   r  r  r  s        r.   rV   zStackTransform._inverse  s    x488-aeeg-----vvdhh3t#7777 QA 	.MFENN599V,-	.{{711r/   c                    |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         t        | j                        k(  sJ |j                          | j                   cxk  r|j                         k  sJ  J |j                  | j                         t        | j                        k(  sJ g }| j	                  |      }| j	                  |      }t        ||| j                        D ]'  \  }}}|j                  |j                  ||             ) t        j                  || j                         S r  )
r   rE  r   r  r  r   r   r_   r   r  )	r+   rP   rS   r  r  r  r  r  r  s	            r.   r_   z#StackTransform.log_abs_det_jacobian  s   x488-aeeg-----vvdhh3t#7777x488-aeeg-----vvdhh3t#7777
++a.++a.%('4??%K 	J!FFEe88HI	J{{:48844r/   c                 :    t        d | j                  D              S )Nc              3   4   K   | ]  }|j                     y wrG   r   r  s     r.   r   z+StackTransform.bijective.<locals>.<genexpr>  r  r   r  r7   s    r.   ro   zStackTransform.bijective  r  r/   c                     t        j                  | j                  D cg c]  }|j                   c}| j                        S c c}w rG   )r   r  r  r!   r   r  s     r.   r!   zStackTransform.domain  s/      DOO!Dq!((!DdhhOO!D   Ac                     t        j                  | j                  D cg c]  }|j                   c}| j                        S c c}w rG   )r   r  r  r"   r   r  s     r.   r"   zStackTransform.codomain  s/      doo!F!**!FQQ!Fr  rN  rk   )ra   rl   rm   rn   r   r   rq   r*   rE   r  rO   rV   r_   rr   ro   r   r   r!   r"   rs   rt   s   @r.   r   r   l  s    
 YE
H22
5 9 9 ##P $P ##R $Rr/   r   c                   n     e Zd ZdZdZej                  ZdZd
 fd	Z	e
d        Zd Zd Zd Zdd	Z x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$   c                 4    t         |   |       || _        y rx   )r)   r*   distribution)r+   r  r,   r-   s      r.   r*   z(CumulativeDistributionTransform.__init__  s    J/(r/   c                 .    | j                   j                  S rG   )r  supportr7   s    r.   r!   z&CumulativeDistributionTransform.domain  s      (((r/   c                 8    | j                   j                  |      S rG   )r  cdfrY   s     r.   rO   z%CumulativeDistributionTransform._call  s      $$Q''r/   c                 8    | j                   j                  |      S rG   )r  icdfr\   s     r.   rV   z(CumulativeDistributionTransform._inverse  s      %%a((r/   c                 8    | j                   j                  |      S rG   )r  log_probr^   s      r.   r_   z4CumulativeDistributionTransform.log_abs_det_jacobian  s      ))!,,r/   c                 R    | j                   |k(  r| S t        | j                  |      S rx   )r%   r   r  rD   s     r.   rE   z*CumulativeDistributionTransform.with_cache  s(    z)K.t/@/@ZXXr/   rj   rk   )ra   rl   rm   rn   ro   r   r  r"   r@   r*   rr   r!   rO   rV   r_   rE   rs   rt   s   @r.   r   r     sM    $ I((HD) ) )()-Yr/   r   )-r   r(  r;  r   r:   typingr   r   torch.nn.functionalnn
functionalr  torch.distributionsr   torch.distributions.utilsr   r   r   r   r	   r
   r   __all__r   r9   r   r   r   r   r   r   r  r   r   r   r   r   r   r   r   r   r   r   r   r   rH   r/   r.   <module>r     s_            +  .0f fR;.	 ;.|wy wt &b) D89 D8N@+y @+F9 ,'RY 'RTN
/y /0	 .!>I !>H9 "c
i c
LO!I O!d y  F5-Y 5-pLY L,/	 /(g
9 g
TERY ERP+Yi +Yr/   