
    [ThҢ                     b   S SK r S SKrS SKrS SKrS SKJr  S SKrS SKJs  J	r
  S SKJr  S SKJrJrJrJrJr  S SKJrJr  S SKJr  / SQr " S S	5      r " S
 S\5      r " S S\5      r\" / 5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS r  " S S\5      r! " S S\5      r" " S S\5      r# " S S\5      r$ " S S \5      r% " S! S"\5      r& " S# S$\5      r' " S% S&\5      r( " S' S(\5      r) " S) S*\5      r* " S+ S,\5      r+ " S- S.\5      r, " S/ S0\5      r-g)1    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                     ^  \ rS rSr% SrSr\R                  \S'   \R                  \S'   SU 4S jjr	S r
\S\4S	 j5       r\SS
 j5       r\S\4S j5       rSS jrS rS rS rS rS rS rS rS rS rS rSrU =r$ )r   .   ac  
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                 |   > Xl         S U l        US:X  a  OUS:X  a  SU l        O[        S5      e[        TU ]  5         g )Nr      )NNzcache_size must be 0 or 1)_cache_size_inv_cached_x_y
ValueErrorsuper__init__)self
cache_size	__class__s     V/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/transforms.pyr,   Transform.__init___   s@    %@D	?1_)D899    c                 D    U R                   R                  5       nS US'   U$ )Nr(   )__dict__copy)r-   states     r0   __getstate__Transform.__getstate__j   s"    ""$fr2   returnc                     U R                   R                  U R                  R                  :X  a  U R                   R                  $ [        S5      e)Nz:Please use either .domain.event_dim or .codomain.event_dim)r#   	event_dimr$   r*   r-   s    r0   r;   Transform.event_dimo   s:    ;;  DMM$;$;;;;(((UVVr2   c                     SnU R                   b  U R                  5       nUc&  [        U 5      n[        R                  " U5      U l         U$ )zc
Returns the inverse :class:`Transform` of this transform.
This should satisfy ``t.inv.inv is t``.
N)r(   _InverseTransformweakrefref)r-   invs     r0   rB   Transform.invu   sB     99 ))+C;#D)CC(DI
r2   c                     [         e)z
Returns the sign of the determinant of the Jacobian, if applicable.
In general this only makes sense for bijective transforms.
NotImplementedErrorr<   s    r0   signTransform.sign   s
     "!r2   c                     U R                   U:X  a  U $ [        U 5      R                  [        R                  L a  [        U 5      " US9$ [	        [        U 5       S35      e)Nr.   z.with_cache is not implemented)r'   typer,   r   rF   r-   r.   s     r0   
with_cacheTransform.with_cache   sS    z)K:)"4"44:44!T$ZL0N"OPPr2   c                     XL $ N r-   others     r0   __eq__Transform.__eq__   s
    }r2   c                 .    U R                  U5      (       + $ rP   )rT   rR   s     r0   __ne__Transform.__ne__   s    ;;u%%%r2   c                     U R                   S:X  a  U R                  U5      $ U R                  u  p#XL a  U$ U R                  U5      nX4U l        U$ )z"
Computes the transform `x => y`.
r   )r'   _callr)   )r-   xx_oldy_oldys        r0   __call__Transform.__call__   sS     q ::a= '':LJJqM4r2   c                     U R                   S:X  a  U R                  U5      $ U R                  u  p#XL a  U$ U R                  U5      nXA4U l        U$ )z!
Inverts the transform `y => x`.
r   )r'   _inverser)   )r-   r^   r\   r]   r[   s        r0   	_inv_callTransform._inv_call   sU     q ==##'':LMM!4r2   c                     [         e)z4
Abstract method to compute forward transformation.
rE   r-   r[   s     r0   rZ   Transform._call   
     "!r2   c                     [         e)z4
Abstract method to compute inverse transformation.
rE   r-   r^   s     r0   rb   Transform._inverse   rh   r2   c                     [         e)zE
Computes the log det jacobian `log |dy/dx|` given input and output.
rE   r-   r[   r^   s      r0   log_abs_det_jacobianTransform.log_abs_det_jacobian   rh   r2   c                 4    U R                   R                  S-   $ )Nz())r/   __name__r<   s    r0   __repr__Transform.__repr__   s    ~~&&--r2   c                     U$ )zc
Infers the shape of the forward computation, given the input shape.
Defaults to preserving shape.
rQ   r-   shapes     r0   forward_shapeTransform.forward_shape   	    
 r2   c                     U$ )ze
Infers the shapes of the inverse computation, given the output shape.
Defaults to preserving shape.
rQ   ru   s     r0   inverse_shapeTransform.inverse_shape   ry   r2   )r'   r)   r(   r   )r9   r   r&   )rq   
__module____qualname____firstlineno____doc__	bijectiver   
Constraint__annotations__r,   r7   propertyintr;   rB   rG   rM   rT   rW   r_   rc   rZ   rb   rn   rr   rw   r{   __static_attributes____classcell__r/   s   @r0   r   r   .   s    *X I"""$$$	
 W3 W W
   "c " "Q&""". r2   r   c                     ^  \ rS rSrSrS\4U 4S jjr\R                  " SS9S 5       r	\R                  " SS9S 5       r
\S	\4S
 j5       r\S	\4S j5       r\S	\4S j5       rSS jrS rS rS rS rS rS rSrU =r$ )r?      zp
Inverts a single :class:`Transform`.
This class is private; please instead use the ``Transform.inv`` property.
	transformc                 @   > [         TU ]  UR                  S9  Xl        g NrJ   )r+   r,   r'   r(   )r-   r   r/   s     r0   r,   _InverseTransform.__init__   s    I$9$9:(	r2   Fis_discretec                 L    U R                   c   eU R                   R                  $ rP   )r(   r$   r<   s    r0   r#   _InverseTransform.domain   s"    yy$$$yy!!!r2   c                 L    U R                   c   eU R                   R                  $ rP   )r(   r#   r<   s    r0   r$   _InverseTransform.codomain   s"    yy$$$yyr2   r9   c                 L    U R                   c   eU R                   R                  $ rP   )r(   r   r<   s    r0   r   _InverseTransform.bijective   s"    yy$$$yy"""r2   c                 L    U R                   c   eU R                   R                  $ rP   )r(   rG   r<   s    r0   rG   _InverseTransform.sign   s     yy$$$yy~~r2   c                     U R                   $ rP   r(   r<   s    r0   rB   _InverseTransform.inv   s    yyr2   c                 j    U R                   c   eU R                  R                  U5      R                  $ rP   )r(   rB   rM   rL   s     r0   rM   _InverseTransform.with_cache   s-    yy$$$xx"":.222r2   c                 ~    [        U[        5      (       d  gU R                  c   eU R                  UR                  :H  $ NF)
isinstancer?   r(   rR   s     r0   rT   _InverseTransform.__eq__   s6    %!233yy$$$yyEJJ&&r2   c                 `    U R                   R                   S[        U R                  5       S3$ )N())r/   rq   reprr(   r<   s    r0   rr   _InverseTransform.__repr__  s)    ..))*!DO+<A>>r2   c                 V    U R                   c   eU R                   R                  U5      $ rP   )r(   rc   rf   s     r0   r_   _InverseTransform.__call__  s'    yy$$$yy""1%%r2   c                 X    U R                   c   eU R                   R                  X!5      * $ rP   )r(   rn   rm   s      r0   rn   &_InverseTransform.log_abs_det_jacobian
  s*    yy$$$		..q444r2   c                 8    U R                   R                  U5      $ rP   )r(   r{   ru   s     r0   rw   _InverseTransform.forward_shape      yy&&u--r2   c                 8    U R                   R                  U5      $ rP   )r(   rw   ru   s     r0   r{   _InverseTransform.inverse_shape  r   r2   r   r~   )rq   r   r   r   r   r   r,   r   dependent_propertyr#   r$   r   boolr   r   rG   rB   rM   rT   rr   r_   rn   rw   r{   r   r   r   s   @r0   r?   r?      s    
)) ) ##6" 7" ##6  7  #4 # # c   Y  3'?&5.. .r2   r?   c                     ^  \ rS rSrSrSS\\   4U 4S jjjrS r\	R                  " SS9S 5       r\	R                  " SS9S	 5       r\S
\4S j5       r\S
\4S j5       r\S
\4S j5       rSS jrS rS rS rS rS rSrU =r$ )r   i  aF  
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                    > U(       a   U Vs/ s H  o3R                  U5      PM     nn[        TU ]	  US9  Xl        g s  snf r   )rM   r+   r,   r   )r-   r   r.   partr/   s       r0   r,   ComposeTransform.__init__   s<    =BCUT__Z0UECJ/
 Ds   =c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ r   )r   r   r   rR   s     r0   rT   ComposeTransform.__eq__&  s&    %!122zzU[[((r2   Fr   c                    U R                   (       d  [        R                  $ U R                   S   R                  nU R                   S   R                  R
                  n[        U R                   5       HQ  nX#R                  R
                  UR                  R
                  -
  -  n[        X#R                  R
                  5      nMS     X!R
                  :  d   eX!R
                  :  a#  [        R                  " XUR
                  -
  5      nU$ )Nr   )	r   r   realr#   r$   r;   reversedmaxindependent)r-   r#   r;   r   s       r0   r#   ComposeTransform.domain+  s    zz###A%%JJrN++55	TZZ(D..1H1HHHII{{'<'<=I ) ,,,,,''' ,,VAQAQ5QRFr2   c                    U R                   (       d  [        R                  $ U R                   S   R                  nU R                   S   R                  R
                  nU R                    HQ  nX#R                  R
                  UR                  R
                  -
  -  n[        X#R                  R
                  5      nMS     X!R
                  :  d   eX!R
                  :  a#  [        R                  " XUR
                  -
  5      nU$ )Nr   r   )r   r   r   r$   r#   r;   r   r   )r-   r$   r;   r   s       r0   r$   ComposeTransform.codomain:  s    zz###::b>**JJqM((22	JJD004;;3H3HHHII}}'>'>?I  .....)))"..xXEWEW9WXHr2   r9   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frP   r   ).0ps     r0   	<genexpr>-ComposeTransform.bijective.<locals>.<genexpr>K  s     3
1;;
   )allr   r<   s    r0   r   ComposeTransform.bijectiveI  s    3

333r2   c                 L    SnU R                    H  nXR                  -  nM     U$ Nr&   )r   rG   )r-   rG   r   s      r0   rG   ComposeTransform.signM  s%    A&&=D r2   c                 0   S nU R                   b  U R                  5       nUcn  [        [        U R                  5       Vs/ s H  o"R                  PM     sn5      n[
        R                  " U5      U l         [
        R                  " U 5      Ul         U$ s  snf rP   )r(   r   r   r   rB   r@   rA   )r-   rB   r   s      r0   rB   ComposeTransform.invT  sr    99 ))+C;"8DJJ3G#H3GaEE3G#HICC(DI{{4(CH
 $Is   Bc                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r'   r   r   rL   s     r0   rM   ComposeTransform.with_cache_  s&    z)K

zBBr2   c                 <    U R                    H  nU" U5      nM     U$ rP   )r   )r-   r[   r   s      r0   r_   ComposeTransform.__call__d  s    JJDQA r2   c           	      ~   U R                   (       d  [        R                  " U5      $ U/nU R                   S S  H  nUR                  U" US   5      5        M     UR                  U5        / nU R                  R
                  n[        U R                   US S USS  5       Hu  u  pAnUR                  [        UR                  X5      XdR                  R
                  -
  5      5        XdR                  R
                  UR                  R
                  -
  -  nMw     [        R                  " [        R                  U5      $ )Nr   r&   )r   torch
zeros_likeappendr#   r;   zipr   rn   r$   	functoolsreduceoperatoradd)r-   r[   r^   xsr   termsr;   s          r0   rn   %ComposeTransform.log_abs_det_jacobiani  s    zz##A&& SJJsODIId2b6l# $
		!KK))	djj"Sb'2ab6:JDQLL--a3YAVAV5V
 004;;3H3HHHI ; e44r2   c                 N    U R                    H  nUR                  U5      nM     U$ rP   )r   rw   r-   rv   r   s      r0   rw   ComposeTransform.forward_shape~  s%    JJD&&u-E r2   c                 `    [        U R                  5       H  nUR                  U5      nM     U$ rP   )r   r   r{   r   s      r0   r{   ComposeTransform.inverse_shape  s*    TZZ(D&&u-E )r2   c                     U R                   R                  S-   nUSR                  U R                   Vs/ s H  o"R	                  5       PM     sn5      -  nUS-  nU$ s  snf )Nz(
    z,
    z
))r/   rq   joinr   rr   )r-   
fmt_stringr   s      r0   rr   ComposeTransform.__repr__  sV    ^^,,y8
innDJJ%GJqjjlJ%GHH
e
 &Hs   A
)r(   r   r}   r~   )rq   r   r   r   r   listr   r,   rT   r   r   r#   r$   r   r   r   r   rG   r   rB   rM   r_   rn   rw   r{   rr   r   r   r   s   @r0   r   r     s    d9o  )
 ##6 7 ##6 7 44 4 4 c   Y  C

5*

 r2   r   c                      ^  \ rS rSrSrSU 4S jjrSS jr\R                  " SS9S 5       r	\R                  " SS9S 5       r
\S	\4S
 j5       r\S	\4S j5       rS rS rS rS rS rS rSrU =r$ )r   i  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                 X   > [         TU ]  US9  UR                  U5      U l        X l        g r   )r+   r,   rM   base_transformreinterpreted_batch_ndims)r-   r   r   r.   r/   s       r0   r,   IndependentTransform.__init__  s,    J/,77
C)B&r2   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r'   r   r   r   rL   s     r0   rM   IndependentTransform.with_cache  s5    z)K#!?!?J
 	
r2   Fr   c                 l    [         R                  " U R                  R                  U R                  5      $ rP   )r   r   r   r#   r   r<   s    r0   r#   IndependentTransform.domain  s,    &&&&(F(F
 	
r2   c                 l    [         R                  " U R                  R                  U R                  5      $ rP   )r   r   r   r$   r   r<   s    r0   r$   IndependentTransform.codomain  s,    &&(($*H*H
 	
r2   r9   c                 .    U R                   R                  $ rP   )r   r   r<   s    r0   r   IndependentTransform.bijective  s    "",,,r2   c                 .    U R                   R                  $ rP   )r   rG   r<   s    r0   rG   IndependentTransform.sign  s    ""'''r2   c                     UR                  5       U R                  R                  :  a  [        S5      eU R	                  U5      $ NToo few dimensions on input)dimr#   r;   r*   r   rf   s     r0   rZ   IndependentTransform._call  s7    557T[[***:;;""1%%r2   c                     UR                  5       U R                  R                  :  a  [        S5      eU R                  R                  U5      $ r   )r   r$   r;   r*   r   rB   rj   s     r0   rb   IndependentTransform._inverse  s=    557T]],,,:;;""&&q))r2   c                 f    U R                   R                  X5      n[        X0R                  5      nU$ rP   )r   rn   r   r   )r-   r[   r^   results       r0   rn   )IndependentTransform.log_abs_det_jacobian  s-    $$99!?(F(FGr2   c                 z    U R                   R                   S[        U R                  5       SU R                   S3$ )Nr   z, r   )r/   rq   r   r   r   r<   s    r0   rr   IndependentTransform.__repr__  s:    ..))*!D1D1D,E+FbIgIgHhhijjr2   c                 8    U R                   R                  U5      $ rP   )r   rw   ru   s     r0   rw   "IndependentTransform.forward_shape      ""0077r2   c                 8    U R                   R                  U5      $ rP   )r   r{   ru   s     r0   r{   "IndependentTransform.inverse_shape  r
  r2   )r   r   r}   r~   )rq   r   r   r   r   r,   rM   r   r   r#   r$   r   r   r   r   rG   rZ   rb   rn   rr   rw   r{   r   r   r   s   @r0   r   r     s    C

 ##6
 7

 ##6
 7

 -4 - - (c ( (&
*

k88 8r2   r   c                      ^  \ rS rSrSrSrSU 4S jjr\R                  S 5       r	\R                  S 5       r
SS jrS rS	 rS
 rS rS rSrU =r$ )r   i  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.)
Tc                   > [         R                  " U5      U l        [         R                  " U5      U l        U R                  R	                  5       U R                  R	                  5       :w  a  [        S5      e[        TU ]  US9  g )Nz6in_shape, out_shape have different numbers of elementsrJ   )r   Sizein_shape	out_shapenumelr*   r+   r,   )r-   r  r  r.   r/   s       r0   r,   ReshapeTransform.__init__  sa    

8,I.== DNN$8$8$::UVVJ/r2   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rP   )r   r   r   lenr  r<   s    r0   r#   ReshapeTransform.domain  s$    &&{'7'7T]]9KLLr2   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rP   )r   r   r   r  r  r<   s    r0   r$   ReshapeTransform.codomain  s$    &&{'7'7T^^9LMMr2   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r'   r   r  r  rL   s     r0   rM   ReshapeTransform.with_cache  s,    z)Kt~~*UUr2   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rP   )rv   r   r  r  reshaper  )r-   r[   batch_shapes      r0   rZ   ReshapeTransform._call  s=    gg<#dmm*< <=yy~~566r2   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rP   )rv   r   r  r  r  r  )r-   r^   r  s      r0   rb   ReshapeTransform._inverse  s=    gg=#dnn*= =>yy}}455r2   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  U5      $ rP   )rv   r   r  r  	new_zeros)r-   r[   r^   r  s       r0   rn   %ReshapeTransform.log_abs_det_jacobian  s6    gg<#dmm*< <={{;''r2   c                     [        U5      [        U R                  5      :  a  [        S5      e[        U5      [        U R                  5      -
  nXS  U R                  :w  a  [        SXS   SU R                   35      eUS U U R                  -   $ Nr   zShape mismatch: expected z	 but got )r  r  r*   r  r-   rv   cuts      r0   rw   ReshapeTransform.forward_shape	  s    u:DMM**:;;%j3t}}--;$--'+E$K=	$--Q  Tc{T^^++r2   c                     [        U5      [        U R                  5      :  a  [        S5      e[        U5      [        U R                  5      -
  nXS  U R                  :w  a  [        SXS   SU R                   35      eUS U U R                  -   $ r%  )r  r  r*   r  r&  s      r0   r{   ReshapeTransform.inverse_shape  s    u:DNN++:;;%j3t~~..;$..(+E$K=	$..AQR  Tc{T]]**r2   )r  r  r}   r~   )rq   r   r   r   r   r   r,   r   r   r#   r$   rM   rZ   rb   rn   rw   r{   r   r   r   s   @r0   r   r     sp     I0 ##M $M ##N $NV
76(,+ +r2   r   c                   h    \ rS rSrSr\R                  r\R                  r	Sr
SrS rS rS rS rS	rg
)r   i  z0
Transform via the mapping :math:`y = \exp(x)`.
Tr&   c                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   ExpTransform.__eq__(      %..r2   c                 "    UR                  5       $ rP   )exprf   s     r0   rZ   ExpTransform._call+      uuwr2   c                 "    UR                  5       $ rP   logrj   s     r0   rb   ExpTransform._inverse.  r2  r2   c                     U$ rP   rQ   rm   s      r0   rn   !ExpTransform.log_abs_det_jacobian1      r2   rQ   Nrq   r   r   r   r   r   r   r#   positiver$   r   rG   rT   rZ   rb   rn   r   rQ   r2   r0   r   r     s=     F##HID/r2   r   c                      ^  \ rS rSrSr\R                  r\R                  rSr	SU 4S jjr
SS jr\S\4S j5       rS rS	 rS
 rS rS rS rSrU =r$ )r   i5  z<
Transform via the mapping :math:`y = x^{\text{exponent}}`.
Tc                 D   > [         TU ]  US9  [        U5      u  U l        g r   )r+   r,   r   exponent)r-   r>  r.   r/   s      r0   r,   PowerTransform.__init__>  s"    J/(2r2   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r'   r   r>  rL   s     r0   rM   PowerTransform.with_cacheB  s&    z)Kdmm
CCr2   r9   c                 6    U R                   R                  5       $ rP   )r>  rG   r<   s    r0   rG   PowerTransform.signG  s    }}!!##r2   c                     [        U[        5      (       d  gU R                  R                  UR                  5      R	                  5       R                  5       $ r   )r   r   r>  eqr   itemrR   s     r0   rT   PowerTransform.__eq__K  s=    %00}}/335::<<r2   c                 8    UR                  U R                  5      $ rP   powr>  rf   s     r0   rZ   PowerTransform._callP  s    uuT]]##r2   c                 >    UR                  SU R                  -  5      $ r   rI  rj   s     r0   rb   PowerTransform._inverseS  s    uuQ&''r2   c                 ^    U R                   U-  U-  R                  5       R                  5       $ rP   )r>  absr5  rm   s      r0   rn   #PowerTransform.log_abs_det_jacobianV  s(    !A%**,0022r2   c                 Z    [         R                  " U[        U R                  SS5      5      $ Nrv   rQ   r   broadcast_shapesgetattrr>  ru   s     r0   rw   PowerTransform.forward_shapeY  "    %%eWT]]GR-PQQr2   c                 Z    [         R                  " U[        U R                  SS5      5      $ rR  rS  ru   s     r0   r{   PowerTransform.inverse_shape\  rW  r2   )r>  r}   r~   )rq   r   r   r   r   r   r;  r#   r$   r   r,   rM   r   r   rG   rT   rZ   rb   rn   rw   r{   r   r   r   s   @r0   r   r   5  sq     !!F##HI3D
 $c $ $=
$(3RR Rr2   r   c                     [         R                  " U R                  5      n[         R                  " [         R                  " U 5      UR
                  SUR                  -
  S9$ N      ?minr   )r   finfodtypeclampsigmoidtinyeps)r[   r_  s     r0   _clipped_sigmoidre  `  s<    KK E;;u}}Q'UZZS599_MMr2   c                   h    \ rS rSrSr\R                  r\R                  r	Sr
SrS rS rS rS rS	rg
)r   ie  z_
Transform via the mapping :math:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.
Tr&   c                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   SigmoidTransform.__eq__o      %!122r2   c                     [        U5      $ rP   )re  rf   s     r0   rZ   SigmoidTransform._callr  s    ""r2   c                     [         R                  " UR                  5      nUR                  UR                  SUR
                  -
  S9nUR                  5       U* R                  5       -
  $ r[  )r   r_  r`  ra  rc  rd  r5  log1p)r-   r^   r_  s      r0   rb   SigmoidTransform._inverseu  sK    AGG$GG

eiiG8uuw1"%%r2   c                 `    [         R                  " U* 5      * [         R                  " U5      -
  $ rP   )Fr   rm   s      r0   rn   %SigmoidTransform.log_abs_det_jacobianz  s!    

A2A..r2   rQ   N)rq   r   r   r   r   r   r   r#   unit_intervalr$   r   rG   rT   rZ   rb   rn   r   rQ   r2   r0   r   r   e  s=     F((HID3#&
/r2   r   c                   h    \ rS rSrSr\R                  r\R                  r	Sr
SrS rS rS rS rS	rg
)r   i~  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                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   SoftplusTransform.__eq__  s    %!233r2   c                     [        U5      $ rP   r   rf   s     r0   rZ   SoftplusTransform._call  s    {r2   c                 b    U* R                  5       R                  5       R                  5       U-   $ rP   )expm1negr5  rj   s     r0   rb   SoftplusTransform._inverse  s'    zz|!%%'!++r2   c                     [        U* 5      * $ rP   rw  rm   s      r0   rn   &SoftplusTransform.log_abs_det_jacobian  s    !}r2   rQ   Nr:  rQ   r2   r0   r   r   ~  s=    
 F##HID4,r2   r   c                   v    \ rS rSrSr\R                  r\R                  " SS5      r	Sr
SrS rS rS	 rS
 rSrg)r   i  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                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   TanhTransform.__eq__  s    %//r2   c                 "    UR                  5       $ rP   )tanhrf   s     r0   rZ   TanhTransform._call  s    vvxr2   c                 .    [         R                  " U5      $ rP   )r   atanhrj   s     r0   rb   TanhTransform._inverse  s     {{1~r2   c                 X    S[         R                  " S5      U-
  [        SU-  5      -
  -  $ )N       @g       )mathr5  r   rm   s      r0   rn   "TanhTransform.log_abs_det_jacobian  s*     dhhsma'(4!8*<<==r2   rQ   N)rq   r   r   r   r   r   r   r#   intervalr$   r   rG   rT   rZ   rb   rn   r   rQ   r2   r0   r   r     sD    , F##D#.HID0
>r2   r   c                   Z    \ rS rSrSr\R                  r\R                  r	S r
S rS rSrg)r   i  z*Transform via the mapping :math:`y = |x|`.c                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   AbsTransform.__eq__  r.  r2   c                 "    UR                  5       $ rP   )rO  rf   s     r0   rZ   AbsTransform._call  r2  r2   c                     U$ rP   rQ   rj   s     r0   rb   AbsTransform._inverse  r9  r2   rQ   N)rq   r   r   r   r   r   r   r#   r;  r$   rT   rZ   rb   r   rQ   r2   r0   r   r     s*    5F##H/r2   r   c                      ^  \ rS rSrSrSrSU 4S jjr\S\4S j5       r	\
R                  " SS9S	 5       r\
R                  " SS9S
 5       rSS jrS r\S\4S j5       rS rS rS rS rS rSrU =r$ )r   i  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                 D   > [         TU ]  US9  Xl        X l        X0l        g r   )r+   r,   locscale
_event_dim)r-   r  r  r;   r.   r/   s        r0   r,   AffineTransform.__init__  s"    J/
#r2   r9   c                     U R                   $ rP   )r  r<   s    r0   r;   AffineTransform.event_dim  s    r2   Fr   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ Nr   r;   r   r   r   r<   s    r0   r#   AffineTransform.domain  7    >>Q###&&{'7'7HHr2   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ r  r  r<   s    r0   r$   AffineTransform.codomain  r  r2   c                 z    U R                   U:X  a  U $ [        U R                  U R                  U R                  US9$ r   )r'   r   r  r  r;   rL   s     r0   rM   AffineTransform.with_cache  s7    z)KHHdjj$..Z
 	
r2   c                    [        U[        5      (       d  g[        U R                  [        5      (       a;  [        UR                  [        5      (       a  U R                  UR                  :w  a  gO;U R                  UR                  :H  R	                  5       R                  5       (       d  g[        U R                  [        5      (       a<  [        UR                  [        5      (       a  U R                  UR                  :w  a  g gU R                  UR                  :H  R	                  5       R                  5       (       d  gg)NFT)r   r   r  r   r   rF  r  rR   s     r0   rT   AffineTransform.__eq__  s    %11dhh((Z		7-K-Kxx599$ % HH		)..05577djj'**z%++w/O/OzzU[[( )  JJ%++-22499;;r2   c                     [        U R                  [        5      (       a8  [        U R                  5      S:  a  S$ [        U R                  5      S:  a  S$ S$ U R                  R	                  5       $ )Nr   r&   r   )r   r  r   floatrG   r<   s    r0   rG   AffineTransform.sign  sU    djj'**djj)A-1Utzz9JQ9N2UTUUzz  r2   c                 :    U R                   U R                  U-  -   $ rP   r  r  rf   s     r0   rZ   AffineTransform._call  s    xx$**q.((r2   c                 8    XR                   -
  U R                  -  $ rP   r  rj   s     r0   rb   AffineTransform._inverse  s    HH

**r2   c                    UR                   nU R                  n[        U[        5      (       a5  [        R
                  " U[        R                  " [        U5      5      5      nO$[        R                  " U5      R                  5       nU R                  (       aQ  UR                  5       S U R                  *  S-   nUR                  U5      R                  S5      nUS U R                  *  nUR                  U5      $ )N)r   r   )rv   r  r   r   r   	full_liker  r5  rO  r;   sizeviewsumexpand)r-   r[   r^   rv   r  r  result_sizes          r0   rn   $AffineTransform.log_abs_det_jacobian  s    

eW%%__QU(<=FYYu%))+F>> ++-(94>>/:UBK[[-11"5F+T^^O,E}}U##r2   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rR  r   rT  rU  r  r  ru   s     r0   rw   AffineTransform.forward_shape+  7    %%7488Wb174::wPR3S
 	
r2   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rR  r  ru   s     r0   r{   AffineTransform.inverse_shape0  r  r2   )r  r  r  r   r   r~   )rq   r   r   r   r   r   r,   r   r   r;   r   r   r#   r$   rM   rT   rG   rZ   rb   rn   rw   r{   r   r   r   s   @r0   r   r     s    	 I$ 3   ##6I 7I
 ##6I 7I

( !c ! !
)+$


 
r2   r   c                   n    \ rS rSrSr\R                  r\R                  r	Sr
S rS rSS jrS rS	 rS
rg)r   i6  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                    [         R                  " U5      n[         R                  " UR                  5      R                  nUR                  SU-   SU-
  S9n[        USS9nUS-  nSU-
  R                  5       R                  S5      nU[         R                  " UR                  S   UR                  UR                  S9-   nU[        USS S24   SS/SS	9-  nU$ )
Nr   r&   r]  diag   )r`  device.r   value)r   r  r_  r`  rd  ra  r	   sqrtcumprodeyerv   r  r
   )r-   r[   rd  rzz1m_cumprod_sqrtr^   s          r0   rZ   CorrCholeskyTransform._callK  s    JJqMkk!''"&&GGSa#gG.qr* qDE<<>11"5		!''"+QWWQXXFF$S#2#X.Aa@@r2   c                    S[         R                  " X-  SS9-
  n[        USS S24   SS/SS9n[        USS9n[        USS9nXER	                  5       -  nUR                  5       UR                  5       R                  5       -
  S-  nU$ )	Nr&   r   r   .r   r  r  r  )r   cumsumr
   r   r  rm  r{  )r-   r^   y_cumsumy_cumsum_shiftedy_vecy_cumsum_vectr[   s           r0   rb   CorrCholeskyTransform._inverseZ  s     u||AEr22xSbS1Aq6C"12.)*:D''))WWY(A-r2   Nc                     SX"-  R                  SS9-
  n[        USS9nSUR                  5       R                  S5      -  nSU[	        SU-  5      -   [
        R                  " S5      -
  R                  SS9-  nXg-   $ )Nr&   r   r  r        ?r  )r  r   r5  r  r   r  )r-   r[   r^   intermediates
y1m_cumsumy1m_cumsum_trilstick_breaking_logdettanh_logdets           r0   rn   *CorrCholeskyTransform.log_abs_det_jacobianf  s     !%B//
 -ZbA #&;&;&=&A&A"&E EAa 00488C=@EE"EMM$22r2   c                     [        U5      S:  a  [        S5      eUS   n[        SSU-  -   S-  S-   5      nX3S-
  -  S-  U:w  a  [        S5      eUS S X34-   $ )Nr&   r   r   g      ?r  r  z-Input is not a flattend lower-diagonal number)r  r*   round)r-   rv   NDs       r0   rw   #CorrCholeskyTransform.forward_shapet  sp    u:>:;;"I4!a%<C'#-.A;!q LMMSbzQF""r2   c                     [        U5      S:  a  [        S5      eUS   US   :w  a  [        S5      eUS   nX"S-
  -  S-  nUS S U4-   $ )Nr  r   r  r   zInput is not squarer&   r  r*   )r-   rv   r  r  s       r0   r{   #CorrCholeskyTransform.inverse_shape~  sa    u:>:;;9b	!233"IQK1SbzQD  r2   rQ   rP   )rq   r   r   r   r   r   real_vectorr#   corr_choleskyr$   r   rZ   rb   rn   rw   r{   r   rQ   r2   r0   r   r   6  s=      $$F((HI
3#!r2   r   c                   f    \ rS rSrSr\R                  r\R                  r	S r
S rS rS rS rSrg	)
r   i  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                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   SoftmaxTransform.__eq__  ri  r2   c                 x    UnX"R                  SS5      S   -
  R                  5       nX3R                  SS5      -  $ )Nr   Tr   )r   r0  r  )r-   r[   logprobsprobss       r0   rZ   SoftmaxTransform._call  s<    LLT2155::<yyT***r2   c                 &    UnUR                  5       $ rP   r4  )r-   r^   r  s      r0   rb   SoftmaxTransform._inverse  s    yy{r2   c                 :    [        U5      S:  a  [        S5      eU$ Nr&   r   r  ru   s     r0   rw   SoftmaxTransform.forward_shape      u:>:;;r2   c                 :    [        U5      S:  a  [        S5      eU$ r  r  ru   s     r0   r{   SoftmaxTransform.inverse_shape  r  r2   rQ   N)rq   r   r   r   r   r   r  r#   simplexr$   rT   rZ   rb   rw   r{   r   rQ   r2   r0   r   r     s8     $$F""H3+

r2   r   c                   p    \ rS rSrSr\R                  r\R                  r	Sr
S rS rS rS rS rS	 rS
rg)r   i  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                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   StickBreakingTransform.__eq__      %!788r2   c                    UR                   S   S-   UR                  UR                   S   5      R                  S5      -
  n[        XR	                  5       -
  5      nSU-
  R                  S5      n[        USS/SS9[        USS/SS9-  nU$ )Nr   r&   r   r  )rv   new_onesr  re  r5  r  r
   )r-   r[   offsetr  	z_cumprodr^   s         r0   rZ   StickBreakingTransform._call  s    q1::aggbk#:#A#A"#EEQ-.UOOB'	Aq6#c)aV1&EEr2   c                    USS S24   nUR                   S   UR                  UR                   S   5      R                  S5      -
  nSUR                  S5      -
  n[        R                  " U[        R
                  " UR                  5      R                  S9nUR                  5       UR                  5       -
  UR                  5       -   nU$ )N.r   r&   )r^  )	rv   r  r  r   ra  r_  r`  rc  r5  )r-   r^   y_cropr  sfr[   s         r0   rb   StickBreakingTransform._inverse  s    38qzz&,,r*:;BB2FFr"" [[QWW!5!:!:;JJL2668#fjjl2r2   c                 ,   UR                   S   S-   UR                  UR                   S   5      R                  S5      -
  nXR                  5       -
  nU* [        R
                  " U5      -   USS S24   R                  5       -   R                  S5      nU$ )Nr   r&   .)rv   r  r  r5  rp  
logsigmoidr  )r-   r[   r^   r  detJs        r0   rn   +StickBreakingTransform.log_abs_det_jacobian  s~    q1::aggbk#:#A#A"#EE

Q\\!_$qcrc{'88==bAr2   c                 T    [        U5      S:  a  [        S5      eUS S US   S-   4-   $ Nr&   r   r   r  ru   s     r0   rw   $StickBreakingTransform.forward_shape  5    u:>:;;SbzU2Y],,,r2   c                 T    [        U5      S:  a  [        S5      eUS S US   S-
  4-   $ r  r  ru   s     r0   r{   $StickBreakingTransform.inverse_shape  r  r2   rQ   N)rq   r   r   r   r   r   r  r#   r  r$   r   rT   rZ   rb   rn   rw   r{   r   rQ   r2   r0   r   r     sB     $$F""HI9-
-r2   r   c                   |    \ rS rSrSr\R                  " \R                  S5      r\R                  r
S rS rS rSrg)	r   i  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                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT   LowerCholeskyTransform.__eq__  r  r2   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ Nr   r  )dim1dim2)trildiagonalr0  
diag_embedrf   s     r0   rZ   LowerCholeskyTransform._call  4    vvbzAJJBRJ8<<>IIKKKr2   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ r  )r  r  r5  r  rj   s     r0   rb   LowerCholeskyTransform._inverse  r  r2   rQ   N)rq   r   r   r   r   r   r   r   r#   lower_choleskyr$   rT   rZ   rb   r   rQ   r2   r0   r   r     s=     $$[%5%5q9F))H9LLr2   r   c                   |    \ rS rSrSr\R                  " \R                  S5      r\R                  r
S rS rS rSrg)	r   i  zF
Transform from unconstrained matrices to positive-definite matrices.
r  c                 "    [        U[        5      $ rP   )r   r   rR   s     r0   rT    PositiveDefiniteTransform.__eq__  s    %!:;;r2   c                 >    [        5       " U5      nXR                  -  $ rP   )r   mTrf   s     r0   rZ   PositiveDefiniteTransform._call  s    "$Q'44xr2   c                 r    [         R                  R                  U5      n[        5       R	                  U5      $ rP   )r   linalgcholeskyr   rB   rj   s     r0   rb   "PositiveDefiniteTransform._inverse
  s*    LL!!!$%'++A..r2   rQ   N)rq   r   r   r   r   r   r   r   r#   positive_definiter$   rT   rZ   rb   r   rQ   r2   r0   r   r     s;     $$[%5%5q9F,,H</r2   r   c                      ^  \ rS rSr% Sr\\   \S'   SU 4S jjr\	S\
4S j5       r\	S\
4S j5       rSS jrS	 rS
 rS r\S\4S j5       r\R*                  S 5       r\R*                  S 5       rSrU =r$ )r   i  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                   > [        S U 5       5      (       d   eU(       a   U Vs/ s H  oUR                  U5      PM     nn[        TU ]  US9  [	        U5      U l        Uc  S/[        U R
                  5      -  n[	        U5      U l        [        U R                  5      [        U R
                  5      :X  d   eX l        g s  snf )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frP   r   r   r   r  s     r0   r   (CatTransform.__init__.<locals>.<genexpr>!       :T:a++T   rJ   r&   )	r   rM   r+   r,   r   r+  r  lengthsr   )r-   tseqr   r3  r.   r  r/   s         r0   r,   CatTransform.__init__   s    :T:::::6:;dLL,dD;J/t*?cC00GG}4<< C$8888 <s   C r9   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frP   )r;   r/  s     r0   r   )CatTransform.event_dim.<locals>.<genexpr>.       81;;r   )r   r+  r<   s    r0   r;   CatTransform.event_dim,      8888r2   c                 ,    [        U R                  5      $ rP   )r  r3  r<   s    r0   lengthCatTransform.length0  s    4<<  r2   c                 ~    U R                   U:X  a  U $ [        U R                  U R                  U R                  U5      $ rP   )r'   r   r+  r   r3  rL   s     r0   rM   CatTransform.with_cache4  s2    z)KDOOTXXt||ZPPr2   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      U R                  :X  d   e/ nSn[        U R                  U R
                  5       H<  u  pEUR                  U R                   X55      nUR                  U" U5      5        X5-   nM>     [        R                  " X R                   S9$ Nr   r  )
r   r  r=  r   r+  r3  narrowr   r   cat)r-   r[   yslicesstarttransr=  xslices          r0   rZ   CatTransform._call9  s    x488-aeeg-----vvdhh4;;... $,,?MEXXdhh6FNN5=)NE @ yyhh//r2   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      U R                  :X  d   e/ nSn[        U R                  U R
                  5       HE  u  pEUR                  U R                   X55      nUR                  UR                  U5      5        X5-   nMG     [        R                  " X R                   S9$ rB  )r   r  r=  r   r+  r3  rC  r   rB   r   rD  )r-   r^   xslicesrF  rG  r=  yslices          r0   rb   CatTransform._inverseD  s    x488-aeeg-----vvdhh4;;... $,,?MEXXdhh6FNN599V,-NE @ yyhh//r2   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      U R                  :X  d   eUR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      U R                  :X  d   e/ nSn[        U R                  U R
                  5       H  u  pVUR                  U R                   XF5      nUR                  U R                   XF5      nUR                  Xx5      n	UR                  U R                  :  a"  [        XR                  UR                  -
  5      n	UR                  U	5        XF-   nM     U R                   n
U
S:  a  XR                  5       -
  n
XR                  -   n
U
S:  a  [        R                  " X:S9$ [        U5      $ rB  )r   r  r=  r   r+  r3  rC  rn   r;   r   r   r   rD  r  )r-   r[   r^   
logdetjacsrF  rG  r=  rH  rL  	logdetjacr   s              r0   rn   !CatTransform.log_abs_det_jacobianO  s{   x488-aeeg-----vvdhh4;;...x488-aeeg-----vvdhh4;;...
 $,,?MEXXdhh6FXXdhh6F226BI/*9nnu6VW	i(NE @ hh!8-CNN"799Z11z?"r2   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frP   r   r/  s     r0   r   )CatTransform.bijective.<locals>.<genexpr>j  r9  r   r   r+  r<   s    r0   r   CatTransform.bijectiveh  r;  r2   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  U R
                  5      $ s  snf rP   )r   rD  r+  r#   r   r3  r-   r  s     r0   r#   CatTransform.domainl  s:    #/!XX/4<<
 	
/   Ac                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  U R
                  5      $ s  snf rP   )r   rD  r+  r$   r   r3  rX  s     r0   r$   CatTransform.codomainr  s:    !%1AZZ1488T\\
 	
1rZ  )r   r3  r+  )r   Nr   r~   )rq   r   r   r   r   r   r   r   r,   r   r   r;   r=  rM   rZ   rb   rn   r   r   r   r   r   r#   r$   r   r   r   s   @r0   r   r     s     Y
 93 9 9 ! ! !Q
	0	0#2 94 9 9 ##
 $

 ##
 $
r2   r   c                      ^  \ rS rSr% Sr\\   \S'   SU 4S jjrSS jr	S r
S rS rS	 r\S
\4S j5       r\R$                  S 5       r\R$                  S 5       rSrU =r$ )r   iy  a7  
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                    > [        S U 5       5      (       d   eU(       a   U Vs/ s H  oDR                  U5      PM     nn[        TU ]  US9  [	        U5      U l        X l        g s  snf )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frP   r.  r/  s     r0   r   *StackTransform.__init__.<locals>.<genexpr>  r1  r2  rJ   )r   rM   r+   r,   r   r+  r   )r-   r4  r   r.   r  r/   s        r0   r,   StackTransform.__init__  s]    :T:::::6:;dLL,dD;J/t* <s   A&c                 h    U R                   U:X  a  U $ [        U R                  U R                  U5      $ rP   )r'   r   r+  r   rL   s     r0   rM   StackTransform.with_cache  s,    z)KdootxxDDr2   c                     [        UR                  U R                  5      5       Vs/ s H  o!R                  U R                  U5      PM      sn$ s  snf rP   )ranger  r   select)r-   r  is      r0   _sliceStackTransform._slice  s;    /4QVVDHH5E/FG/F!1%/FGGGs   %Ac                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      [        U R                  5      :X  d   e/ n[	        U R                  U5      U R                  5       H  u  p4UR                  U" U5      5        M     [        R                  " X R                   S9$ Nr  )	r   r  r  r+  r   rh  r   r   stack)r-   r[   rE  rH  rG  s        r0   rZ   StackTransform._call  s    x488-aeeg-----vvdhh3t#7777 QAMFNN5=) B{{711r2   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      [        U R                  5      :X  d   e/ n[	        U R                  U5      U R                  5       H%  u  p4UR                  UR                  U5      5        M'     [        R                  " X R                   S9$ rk  )
r   r  r  r+  r   rh  r   rB   r   rl  )r-   r^   rK  rL  rG  s        r0   rb   StackTransform._inverse  s    x488-aeeg-----vvdhh3t#7777 QAMFNN599V,- B{{711r2   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      [        U R                  5      :X  d   eUR                  5       * U R                   s=::  a  UR                  5       :  d   e   eUR                  U R                   5      [        U R                  5      :X  d   e/ nU R	                  U5      nU R	                  U5      n[        XTU R                  5       H&  u  pgnUR                  UR                  Xg5      5        M(     [        R                  " X0R                   S9$ rk  )
r   r  r  r+  rh  r   r   rn   r   rl  )	r-   r[   r^   rO  rE  rK  rH  rL  rG  s	            r0   rn   #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!FEe88HI &L{{:8844r2   r9   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frP   r   r/  s     r0   r   +StackTransform.bijective.<locals>.<genexpr>  r9  r   rU  r<   s    r0   r   StackTransform.bijective  r;  r2   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf rP   )r   rl  r+  r#   r   rX  s     r0   r#   StackTransform.domain  s1      DOO!DOq((O!DdhhOO!D   Ac                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf rP   )r   rl  r+  r$   r   rX  s     r0   r$   StackTransform.codomain  s1      doo!Fo**o!FQQ!Frx  )r   r+  r  r~   )rq   r   r   r   r   r   r   r   r,   rM   rh  rZ   rb   rn   r   r   r   r   r   r#   r$   r   r   r   s   @r0   r   r   y  s    
 YE
H22
5 94 9 9 ##P $P ##R $Rr2   r   c                      ^  \ rS rSrSrSr\R                  rSr	SU 4S jjr
\S\R                  4S j5       rS rS	 rS
 rSS jrSrU =r$ )r   i  a  
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                 ,   > [         TU ]  US9  Xl        g r   )r+   r,   distribution)r-   r}  r.   r/   s      r0   r,   (CumulativeDistributionTransform.__init__  s    J/(r2   r9   c                 .    U R                   R                  $ rP   )r}  supportr<   s    r0   r#   &CumulativeDistributionTransform.domain  s      (((r2   c                 8    U R                   R                  U5      $ rP   )r}  cdfrf   s     r0   rZ   %CumulativeDistributionTransform._call  s      $$Q''r2   c                 8    U R                   R                  U5      $ rP   )r}  icdfrj   s     r0   rb   (CumulativeDistributionTransform._inverse  s      %%a((r2   c                 8    U R                   R                  U5      $ rP   )r}  log_probrm   s      r0   rn   4CumulativeDistributionTransform.log_abs_det_jacobian  s      ))!,,r2   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r'   r   r}  rL   s     r0   rM   *CumulativeDistributionTransform.with_cache  s(    z)K.t/@/@ZXXr2   )r}  r}   r~   )rq   r   r   r   r   r   r   rr  r$   rG   r,   r   r   r#   rZ   rb   rn   rM   r   r   r   s   @r0   r   r     s`    $ I((HD) ).. ) )()-Y Yr2   r   ).r   r  r   r@   typingr   r   torch.nn.functionalnn
functionalrp  torch.distributionsr   torch.distributions.utilsr   r   r   r   r	   r
   r   torch.typesr   __all__r   r?   r   r    r   r   r   r   re  r   r   r   r   r   r   r   r   r   r   r   r   r   rQ   r2   r0   <module>r     s_           +  . 0f fR;.	 ;.|wy wt &b) D89 D8NB+y B+J9 .(RY (RVN
/y /2	 0*>I *>Z9  `
i `
FP!I P!f!y !H5-Y 5-pLY L,/	 /(g
9 g
TERY ERP+Yi +Yr2   