
    ȅi6                     z   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  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/ S0\5      r/ " S1 S2\5      r0g)3    N)Sequence)Tensor)constraints)Distribution)_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S\	SS4U 4S	 j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!   0   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codomain
cache_sizereturnNc                 |   > 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__)selfr'   	__class__s     X/home/james-whalen/.local/lib/python3.13/site-packages/torch/distributions/transforms.pyr0   Transform.__init__a   s@    %=A	?1_)D899    c                 D    U R                   R                  5       nS US'   U$ )Nr,   )__dict__copy)r1   states     r3   __getstate__Transform.__getstate__l   s"    ""$fr5   c                     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.   r1   s    r3   r=   Transform.event_dimq   s:    ;;  DMM$;$;;;;(((UVVr5   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)r1   invs     r3   rD   Transform.invw   sB     99 ))+C;#D)CC(DI
r5   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    r3   signTransform.sign   s
     "!r5   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+   typer0   r!   rH   r1   r'   s     r3   
with_cacheTransform.with_cache   sS    z)K:)"4"44:44!T$ZL0N"OPPr5   c                     XL $ N r1   others     r3   __eq__Transform.__eq__   s
    }r5   c                 .    U R                  U5      (       + $ rR   )rV   rT   s     r3   __ne__Transform.__ne__   s    ;;u%%%r5   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-   )r1   xx_oldy_oldys        r3   __call__Transform.__call__   sS     q ::a= '':LJJqM4r5   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-   )r1   r`   r^   r_   r]   s        r3   	_inv_callTransform._inv_call   sU     q ==##'':LMM!4r5   c                     [         e)z4
Abstract method to compute forward transformation.
rG   r1   r]   s     r3   r\   Transform._call   
     "!r5   c                     [         e)z4
Abstract method to compute inverse transformation.
rG   r1   r`   s     r3   rd   Transform._inverse   rj   r5   c                     [         e)zE
Computes the log det jacobian `log |dy/dx|` given input and output.
rG   r1   r]   r`   s      r3   log_abs_det_jacobianTransform.log_abs_det_jacobian   rj   r5   c                 4    U R                   R                  S-   $ )Nz())r2   __name__r>   s    r3   __repr__Transform.__repr__   s    ~~&&--r5   c                     U$ )zc
Infers the shape of the forward computation, given the input shape.
Defaults to preserving shape.
rS   r1   shapes     r3   forward_shapeTransform.forward_shape   	    
 r5   c                     U$ )ze
Infers the shapes of the inverse computation, given the output shape.
Defaults to preserving shape.
rS   rw   s     r3   inverse_shapeTransform.inverse_shape   r{   r5   )r+   r-   r,   r   )r(   r!   r*   )rs   
__module____qualname____firstlineno____doc__	bijectiver   
Constraint__annotations__intr0   r:   propertyr=   rD   rI   rO   rV   rY   ra   re   r\   rd   rp   rt   ry   r}   __static_attributes____classcell__r2   s   @r3   r!   r!   0   s    *X I"""$$$	3 	t 	 	
 W3 W W
   "c " "Q&""". r5   r!   c                     ^  \ rS rSrSrS\S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$ )rA      zp
Inverts a single :class:`Transform`.
This class is private; please instead use the ``Transform.inv`` property.
	transformr(   Nc                 @   > [         TU ]  UR                  S9  Xl        g NrL   )r/   r0   r+   r,   )r1   r   r2   s     r3   r0   _InverseTransform.__init__   s    I$9$9:(	r5   Fis_discretec                 ^    U R                   c  [        S5      eU R                   R                  $ N_inv must not be None)r,   AssertionErrorr&   r>   s    r3   r%   _InverseTransform.domain   s*     99 !899yy!!!r5   c                 ^    U R                   c  [        S5      eU R                   R                  $ r   )r,   r   r%   r>   s    r3   r&   _InverseTransform.codomain   s*     99 !899yyr5   c                 ^    U R                   c  [        S5      eU R                   R                  $ r   )r,   r   r   r>   s    r3   r   _InverseTransform.bijective   s(    99 !899yy"""r5   c                 ^    U R                   c  [        S5      eU R                   R                  $ r   )r,   r   rI   r>   s    r3   rI   _InverseTransform.sign   s&    99 !899yy~~r5   c                     U R                   $ rR   r,   r>   s    r3   rD   _InverseTransform.inv   s    yyr5   c                 |    U R                   c  [        S5      eU R                  R                  U5      R                  $ r   )r,   r   rD   rO   rN   s     r3   rO   _InverseTransform.with_cache  s3    99 !899xx"":.222r5   c                     [        U[        5      (       d  gU R                  c  [        S5      eU R                  UR                  :H  $ )NFr   )
isinstancerA   r,   r   rT   s     r3   rV   _InverseTransform.__eq__  s<    %!23399 !899yyEJJ&&r5   c                 `    U R                   R                   S[        U R                  5       S3$ )N())r2   rs   reprr,   r>   s    r3   rt   _InverseTransform.__repr__  s)    ..))*!DO+<A>>r5   c                 h    U R                   c  [        S5      eU R                   R                  U5      $ r   )r,   r   re   rh   s     r3   ra   _InverseTransform.__call__  s-    99 !899yy""1%%r5   c                 j    U R                   c  [        S5      eU R                   R                  X!5      * $ r   )r,   r   rp   ro   s      r3   rp   &_InverseTransform.log_abs_det_jacobian  s0    99 !899		..q444r5   c                 8    U R                   R                  U5      $ rR   )r,   r}   rw   s     r3   ry   _InverseTransform.forward_shape      yy&&u--r5   c                 8    U R                   R                  U5      $ rR   )r,   ry   rw   s     r3   r}   _InverseTransform.inverse_shape  r   r5   r   r   )rs   r   r   r   r   r!   r0   r   dependent_propertyr%   r&   r   boolr   r   rI   rD   rO   rV   rt   ra   rp   ry   r}   r   r   r   s   @r3   rA   rA      s    
)) ) ) ##6" 7"
 ##6  7 
 #4 # #
 c  
 Y  3
'?&
5
.. .r5   rA   c                     ^  \ rS rSrSrSS\\   S\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.
partsr'   r(   Nc                    > 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   )rO   r/   r0   r   )r1   r   r'   partr2   s       r3   r0   ComposeTransform.__init__,  s<    =BCUT__Z0UECJ/
 Ds   =c                 `    [        U[        5      (       d  gU R                  UR                  :H  $ NF)r   r   r   rT   s     r3   rV   ComposeTransform.__eq__2  s&    %!122zzU[[((r5   Fr   c                 L   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
                  :  a  [        SU SUR
                   35      eX!R
                  :  a#  [        R                  " XUR
                  -
  5      nU$ )Nr   
event_dim z must be >= domain.event_dim )
r   r   realr%   r&   r=   reversedmaxr   independent)r1   r%   r=   r   s       r3   r%   ComposeTransform.domain7  s     zz###A%%JJrN++55	TZZ(D..1H1HHHII{{'<'<=I ) ''' YK'DVEUEUDVW  ''' ,,VAQAQ5QRFr5   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
                  :  a  [        SU SUR
                   35      eX!R
                  :  a#  [        R                  " XUR
                  -
  5      nU$ )Nr   r   r   z must be >= codomain.event_dim )	r   r   r   r&   r%   r=   r   r   r   )r1   r&   r=   r   s       r3   r&   ComposeTransform.codomainJ  s     zz###::b>**JJqM((22	JJD004;;3H3HHHII}}'>'>?I  ))) YK'FxGYGYFZ[  )))"..xXEWEW9WXHr5   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frR   r   ).0ps     r3   	<genexpr>-ComposeTransform.bijective.<locals>.<genexpr>_  s     3
1;;
   )allr   r>   s    r3   r   ComposeTransform.bijective]  s    3

333r5   c                 L    SnU R                    H  nXR                  -  nM     U$ Nr*   )r   rI   )r1   rI   r   s      r3   rI   ComposeTransform.signa  s%    A&&=D r5   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 rR   )r,   r   r   r   rD   rB   rC   )r1   rD   r   s      r3   rD   ComposeTransform.invh  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   rN   s     r3   rO   ComposeTransform.with_caches  s&    z)K

zBBr5   c                 <    U R                    H  nU" U5      nM     U$ rR   )r   )r1   r]   r   s      r3   ra   ComposeTransform.__call__x  s    JJDQA r5   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   rp   r&   	functoolsreduceoperatoradd)r1   r]   r`   xsr   termsr=   s          r3   rp   %ComposeTransform.log_abs_det_jacobian}  s    zz##A&& SJJsODIId2b6l# $
		!KK))	djj"Sb'2ab6:JDQLL--a3YAVAV5V
 004;;3H3HHHI ; e44r5   c                 N    U R                    H  nUR                  U5      nM     U$ rR   )r   ry   r1   rx   r   s      r3   ry   ComposeTransform.forward_shape  s%    JJD&&u-E r5   c                 `    [        U R                  5       H  nUR                  U5      nM     U$ rR   )r   r   r}   r   s      r3   r}   ComposeTransform.inverse_shape  s*    TZZ(D&&u-E )r5   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
))r2   rs   joinr   rt   )r1   
fmt_stringr   s      r3   rt   ComposeTransform.__repr__  sV    ^^,,y8
innDJJ%GJqjjlJ%GHH
e
 &Hs   A
)r,   r   r   r   )rs   r   r   r   r   listr!   r   r0   rV   r   r   r%   r&   r	   r   r   rI   r   rD   rO   ra   rp   ry   r}   rt   r   r   r   s   @r3   r   r   !  s    d9o 3 t  )
 ##6 7" ##6 7" 44 4 4 c   Y  C

5*

 r5   r   c            	          ^  \ rS rSrSr SS\S\S\SS4U 4S j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.
base_transformreinterpreted_batch_ndimsr'   r(   Nc                 X   > [         TU ]  US9  UR                  U5      U l        X l        g r   )r/   r0   rO   r   r   )r1   r   r   r'   r2   s       r3   r0   IndependentTransform.__init__  s.     	J/,77
C)B&r5   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r+   r   r   r   rN   s     r3   rO   IndependentTransform.with_cache  s5    z)K#!?!?J
 	
r5   Fr   c                 l    [         R                  " U R                  R                  U R                  5      $ rR   )r   r   r   r%   r   r>   s    r3   r%   IndependentTransform.domain  s.     &&&&(F(F
 	
r5   c                 l    [         R                  " U R                  R                  U R                  5      $ rR   )r   r   r   r&   r   r>   s    r3   r&   IndependentTransform.codomain  s.     &&(($*H*H
 	
r5   c                 .    U R                   R                  $ rR   )r   r   r>   s    r3   r   IndependentTransform.bijective  s    "",,,r5   c                 .    U R                   R                  $ rR   )r   rI   r>   s    r3   rI   IndependentTransform.sign  s    ""'''r5   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   rh   s     r3   r\   IndependentTransform._call  s7    557T[[***:;;""1%%r5   c                     UR                  5       U R                  R                  :  a  [        S5      eU R                  R                  U5      $ r  )r  r&   r=   r.   r   rD   rl   s     r3   rd   IndependentTransform._inverse  s=    557T]],,,:;;""&&q))r5   c                 f    U R                   R                  X5      n[        X0R                  5      nU$ rR   )r   rp   r   r   )r1   r]   r`   results       r3   rp   )IndependentTransform.log_abs_det_jacobian  s-    $$99!?(F(FGr5   c                 z    U R                   R                   S[        U R                  5       SU R                   S3$ )Nr   z, r   )r2   rs   r   r   r   r>   s    r3   rt   IndependentTransform.__repr__  s:    ..))*!D1D1D,E+FbIgIgHhhijjr5   c                 8    U R                   R                  U5      $ rR   )r   ry   rw   s     r3   ry   "IndependentTransform.forward_shape      ""0077r5   c                 8    U R                   R                  U5      $ rR   )r   r}   rw   s     r3   r}   "IndependentTransform.inverse_shape  r  r5   )r   r   r   r   )rs   r   r   r   r   r!   r   r0   rO   r   r   r%   r&   r   r   r   rI   r\   rd   rp   rt   ry   r}   r   r   r   s   @r3   r   r     s    " 	C!C $'C 	C
 
C C
 ##6
 7

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

 -4 - - (c ( (&
*

k88 8r5   r   c            	          ^  \ rS rSrSrSr SS\R                  S\R                  S\SS4U 4S	 j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.)
Tin_shape	out_shaper'   r(   Nc                   > [         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 elementsrL   )r   Sizer  r  numelr.   r/   r0   )r1   r  r  r'   r2   s       r3   r0   ReshapeTransform.__init__  sc     

8,I.== DNN$8$8$::UVVJ/r5   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rR   )r   r   r   lenr  r>   s    r3   r%   ReshapeTransform.domain  s&     &&{'7'7T]]9KLLr5   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rR   )r   r   r   r  r  r>   s    r3   r&   ReshapeTransform.codomain  s&     &&{'7'7T^^9LMMr5   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r+   r   r  r  rN   s     r3   rO   ReshapeTransform.with_cache  s,    z)Kt~~*UUr5   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rR   )rx   r  r  r  reshaper  )r1   r]   batch_shapes      r3   r\   ReshapeTransform._call  s=    gg<#dmm*< <=yy~~566r5   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rR   )rx   r  r  r  r"  r  )r1   r`   r#  s      r3   rd   ReshapeTransform._inverse#  s=    gg=#dnn*= =>yy}}455r5   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  U5      $ rR   )rx   r  r  r  	new_zeros)r1   r]   r`   r#  s       r3   rp   %ReshapeTransform.log_abs_det_jacobian'  s6    gg<#dmm*< <={{;''r5   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  r1   rx   cuts      r3   ry   ReshapeTransform.forward_shape+  s    u:DMM**:;;%j3t}}--;$--'+E$K=	$--Q  Tc{T^^++r5   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      r3   r}   ReshapeTransform.inverse_shape5  s    u:DNN++:;;%j3t~~..;$..(+E$K=	$..AQR  Tc{T]]**r5   )r  r  r   r   )rs   r   r   r   r   r   r   r  r   r0   r   r   r%   r&   rO   r\   rd   rp   ry   r}   r   r   r   s   @r3   r   r     s     I 	
0**
0 ::
0 	
0
 

0 
0 ##M $M ##N $NV
76(,+ +r5   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      $ rR   )r   r   rT   s     r3   rV   ExpTransform.__eq__J      %..r5   c                 "    UR                  5       $ rR   )exprh   s     r3   r\   ExpTransform._callM      uuwr5   c                 "    UR                  5       $ rR   logrl   s     r3   rd   ExpTransform._inverseP  r8  r5   c                     U$ rR   rS   ro   s      r3   rp   !ExpTransform.log_abs_det_jacobianS      r5   rS   Nrs   r   r   r   r   r   r   r%   positiver&   r   rI   rV   r\   rd   rp   r   rS   r5   r3   r   r   @  s=     F##HID/r5   r   c                      ^  \ rS rSrSr\R                  r\R                  rSr	SS\
S\SS4U 4S j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   iW  z<
Transform via the mapping :math:`y = x^{\text{exponent}}`.
Texponentr'   r(   Nc                 D   > [         TU ]  US9  [        U5      u  U l        g r   )r/   r0   r   rC  )r1   rC  r'   r2   s      r3   r0   PowerTransform.__init__`  s"    J/(2r5   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r+   r   rC  rN   s     r3   rO   PowerTransform.with_cached  s&    z)Kdmm
CCr5   c                 6    U R                   R                  5       $ rR   )rC  rI   r>   s    r3   rI   PowerTransform.signi  s    }}!!##r5   c                     [        U[        5      (       d  gU R                  R                  UR                  5      R	                  5       R                  5       $ r   )r   r   rC  eqr   itemrT   s     r3   rV   PowerTransform.__eq__m  s=    %00}}/335::<<r5   c                 8    UR                  U R                  5      $ rR   powrC  rh   s     r3   r\   PowerTransform._callr  s    uuT]]##r5   c                 >    UR                  SU R                  -  5      $ r   rO  rl   s     r3   rd   PowerTransform._inverseu  s    uuQ&''r5   c                 ^    U R                   U-  U-  R                  5       R                  5       $ rR   )rC  absr;  ro   s      r3   rp   #PowerTransform.log_abs_det_jacobianx  s(    !A%**,0022r5   c                 Z    [         R                  " U[        U R                  SS5      5      $ Nrx   rS   r   broadcast_shapesgetattrrC  rw   s     r3   ry   PowerTransform.forward_shape{  "    %%eWT]]GR-PQQr5   c                 Z    [         R                  " U[        U R                  SS5      5      $ rX  rY  rw   s     r3   r}   PowerTransform.inverse_shape~  r]  r5   )rC  r   r   )rs   r   r   r   r   r   rA  r%   r&   r   r   r   r0   rO   r	   rI   rV   r\   rd   rp   ry   r}   r   r   r   s   @r3   r   r   W  s     !!F##HI3 3S 3 3 3D
 $c $ $=
$(3RR Rr5   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]   re  s     r3   _clipped_sigmoidrk    s<    KK E;;u}}Q'UZZS599_MMr5   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:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.
Tr*   c                 "    [        U[        5      $ rR   )r   r   rT   s     r3   rV   SigmoidTransform.__eq__      %!122r5   c                     [        U5      $ rR   )rk  rh   s     r3   r\   SigmoidTransform._call  s    ""r5   c                     [         R                  " UR                  5      nUR                  UR                  SUR
                  -
  S9nUR                  5       U* R                  5       -
  $ ra  )r   re  rf  rg  ri  rj  r;  log1p)r1   r`   re  s      r3   rd   SigmoidTransform._inverse  sK    AGG$GG

eiiG8uuw1"%%r5   c                 `    [         R                  " U* 5      * [         R                  " U5      -
  $ rR   )Fr   ro   s      r3   rp   %SigmoidTransform.log_abs_det_jacobian  s!    

A2A..r5   rS   N)rs   r   r   r   r   r   r   r%   unit_intervalr&   r   rI   rV   r\   rd   rp   r   rS   r5   r3   r   r     s=     F((HID3#&
/r5   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      $ rR   )r   r   rT   s     r3   rV   SoftplusTransform.__eq__  s    %!233r5   c                     [        U5      $ rR   r   rh   s     r3   r\   SoftplusTransform._call  s    {r5   c                 b    U* R                  5       R                  5       R                  5       U-   $ rR   )expm1negr;  rl   s     r3   rd   SoftplusTransform._inverse  s'    zz|!%%'!++r5   c                     [        U* 5      * $ rR   r}  ro   s      r3   rp   &SoftplusTransform.log_abs_det_jacobian  s    !}r5   rS   Nr@  rS   r5   r3   r   r     s=    
 F##HID4,r5   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      rb  Tr*   c                 "    [        U[        5      $ rR   )r   r   rT   s     r3   rV   TanhTransform.__eq__  s    %//r5   c                 "    UR                  5       $ rR   )tanhrh   s     r3   r\   TanhTransform._call  s    vvxr5   c                 .    [         R                  " U5      $ rR   )r   atanhrl   s     r3   rd   TanhTransform._inverse  s     {{1~r5   c                 X    S[         R                  " S5      U-
  [        SU-  5      -
  -  $ )N       @g       )mathr;  r   ro   s      r3   rp   "TanhTransform.log_abs_det_jacobian  s*     dhhsma'(4!8*<<==r5   rS   N)rs   r   r   r   r   r   r   r%   intervalr&   r   rI   rV   r\   rd   rp   r   rS   r5   r3   r   r     sD    , F##D#.HID0
>r5   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      $ rR   )r   r   rT   s     r3   rV   AbsTransform.__eq__  r4  r5   c                 "    UR                  5       $ rR   )rU  rh   s     r3   r\   AbsTransform._call  r8  r5   c                     U$ rR   rS   rl   s     r3   rd   AbsTransform._inverse  r?  r5   rS   N)rs   r   r   r   r   r   r   r%   rA  r&   rV   r\   rd   r   rS   r5   r3   r   r     s*    5F##H/r5   r   c                     ^  \ rS rSrSrSr  SS\\-  S\\-  S\S\SS	4
U 4S
 j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.
Tlocscaler=   r'   r(   Nc                 D   > [         TU ]  US9  Xl        X l        X0l        g r   )r/   r0   r  r  
_event_dim)r1   r  r  r=   r'   r2   s        r3   r0   AffineTransform.__init__  s$     	J/
#r5   c                     U R                   $ rR   )r  r>   s    r3   r=   AffineTransform.event_dim  s    r5   Fr   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ Nr   r=   r   r   r   r>   s    r3   r%   AffineTransform.domain  9     >>Q###&&{'7'7HHr5   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ r  r  r>   s    r3   r&   AffineTransform.codomain  r  r5   c                 z    U R                   U:X  a  U $ [        U R                  U R                  U R                  US9$ r   )r+   r   r  r  r=   rN   s     r3   rO   AffineTransform.with_cache!  s7    z)KHHdjj$..Z
 	
r5   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   rL  r  rT   s     r3   rV   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;;r5   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   floatrI   r>   s    r3   rI   AffineTransform.sign<  sU    djj'**djj)A-1Utzz9JQ9N2UTUUzz  r5   c                 :    U R                   U R                  U-  -   $ rR   r  r  rh   s     r3   r\   AffineTransform._callB  s    xx$**q.((r5   c                 8    XR                   -
  U R                  -  $ rR   r  rl   s     r3   rd   AffineTransform._inverseE  s    HH

**r5   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   )rx   r  r   r   r   	full_liker  r;  rU  r=   sizeviewsumexpand)r1   r]   r`   rx   r  r
  result_sizes          r3   rp   $AffineTransform.log_abs_det_jacobianH  s    

eW%%__QU(<=FYYu%))+F>> ++-(94>>/:UBK[[-11"5F+T^^O,E}}U##r5   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rX  r   rZ  r[  r  r  rw   s     r3   ry   AffineTransform.forward_shapeU  7    %%7488Wb174::wPR3S
 	
r5   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rX  r  rw   s     r3   r}   AffineTransform.inverse_shapeZ  r  r5   )r  r  r  r   r   r   )rs   r   r   r   r   r   r   r  r   r0   r   r=   r   r   r%   r&   rO   rV   rI   r\   rd   rp   ry   r}   r   r   r   s   @r3   r   r     s    	 I 
$e^
$ ~
$ 	
$
 
$ 

$ 
$ 3   ##6I 7I
 ##6I 7I

( !fsl ! !
)+$


 
r5   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   i`  a|  
Transforms an unconstrained 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*   rc  diag   )rf  device.r   value)r   r  re  rf  rj  rg  r   sqrtcumprodeyerx   r  r   )r1   r]   rj  rzz1m_cumprod_sqrtr`   s          r3   r\   CorrCholeskyTransform._callu  s    JJqMkk!''"&&GGSa#gG.qr*
 qDE<<>11"5		!''"+QWWQXXFF$S#2#X.Aa@@r5   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  rs  r  )r1   r`   y_cumsumy_cumsum_shiftedy_vecy_cumsum_vectr]   s           r3   rd   CorrCholeskyTransform._inverse  s     u||AEr22xSbS1Aq6C"12.)*:D''))WWY(A-r5   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
   r;  r  r   r  )r1   r]   r`   intermediates
y1m_cumsumy1m_cumsum_trilstick_breaking_logdettanh_logdets           r3   rp   *CorrCholeskyTransform.log_abs_det_jacobian  s     !%B//
 -ZbA #&;&;&=&A&A"&E EAa 00488C=@EE"EMM$22r5   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 flattened lower-diagonal number)r  r.   round)r1   rx   NDs       r3   ry   #CorrCholeskyTransform.forward_shape  sp    u:>:;;"I4!a%<C'#-.A;!q MNNSbzQF""r5   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.   )r1   rx   r  r  s       r3   r}   #CorrCholeskyTransform.inverse_shape  sa    u:>:;;9b	!233"IQK1SbzQD  r5   rS   rR   )rs   r   r   r   r   r   real_vectorr%   corr_choleskyr&   r   r\   rd   rp   ry   r}   r   rS   r5   r3   r   r   `  s=      $$F((HI 
3#!r5   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      $ rR   )r   r   rT   s     r3   rV   SoftmaxTransform.__eq__  ro  r5   c                 x    UnX"R                  SS5      S   -
  R                  5       nX3R                  SS5      -  $ )Nr   Tr   )r   r6  r  )r1   r]   logprobsprobss       r3   r\   SoftmaxTransform._call  s<    LLT2155::<yyT***r5   c                 &    UnUR                  5       $ rR   r:  )r1   r`   r  s      r3   rd   SoftmaxTransform._inverse  s    yy{r5   c                 :    [        U5      S:  a  [        S5      eU$ Nr*   r  r  rw   s     r3   ry   SoftmaxTransform.forward_shape      u:>:;;r5   c                 :    [        U5      S:  a  [        S5      eU$ r  r  rw   s     r3   r}   SoftmaxTransform.inverse_shape  r  r5   rS   N)rs   r   r   r   r   r   r  r%   simplexr&   rV   r\   rd   ry   r}   r   rS   r5   r3   r   r     s8     $$F""H3+

r5   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      $ rR   )r   r    rT   s     r3   rV   StickBreakingTransform.__eq__      %!788r5   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  )rx   new_onesr  rk  r;  r  r   )r1   r]   offsetr  	z_cumprodr`   s         r3   r\   StickBreakingTransform._call  s    q1::aggbk#:#A#A"#EEQ-.UOOB'	Aq6#c)aV1&EEr5   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*   )rd  )	rx   r  r  r   rg  re  rf  ri  r;  )r1   r`   y_cropr  sfr]   s         r3   rd   StickBreakingTransform._inverse  s    38qzz&,,r*:;BB2FFr"" [[QWW!5!:!:;JJL2668#fjjl2r5   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*   .)rx   r  r  r;  rv  
logsigmoidr  )r1   r]   r`   r  detJs        r3   rp   +StickBreakingTransform.log_abs_det_jacobian  s~    q1::aggbk#:#A#A"#EE

Q\\!_$qcrc{'88==bAr5   c                 T    [        U5      S:  a  [        S5      eUS S US   S-   4-   $ Nr*   r  r   r  rw   s     r3   ry   $StickBreakingTransform.forward_shape  5    u:>:;;SbzU2Y],,,r5   c                 T    [        U5      S:  a  [        S5      eUS S US   S-
  4-   $ r  r  rw   s     r3   r}   $StickBreakingTransform.inverse_shape
  r  r5   rS   N)rs   r   r   r   r   r   r  r%   r  r&   r   rV   r\   rd   rp   ry   r}   r   rS   r5   r3   r    r      sB     $$F""HI9-
-r5   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      $ rR   )r   r   rT   s     r3   rV   LowerCholeskyTransform.__eq__  r  r5   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ Nr   r  )dim1dim2)trildiagonalr6  
diag_embedrh   s     r3   r\   LowerCholeskyTransform._call  4    vvbzAJJBRJ8<<>IIKKKr5   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ r  )r  r  r;  r  rl   s     r3   rd   LowerCholeskyTransform._inverse"  r!  r5   rS   N)rs   r   r   r   r   r   r   r   r%   lower_choleskyr&   rV   r\   rd   r   rS   r5   r3   r   r     s=     $$[%5%5q9F))H9LLr5   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      $ rR   )r   r   rT   s     r3   rV    PositiveDefiniteTransform.__eq__.  s    %!:;;r5   c                 >    [        5       " U5      nXR                  -  $ rR   )r   mTrh   s     r3   r\   PositiveDefiniteTransform._call1  s    "$Q'44xr5   c                 r    [         R                  R                  U5      n[        5       R	                  U5      $ rR   )r   linalgcholeskyr   rD   rl   s     r3   rd   "PositiveDefiniteTransform._inverse5  s*    LL!!!$%'++A..r5   rS   N)rs   r   r   r   r   r   r   r   r%   positive_definiter&   rV   r\   rd   r   rS   r5   r3   r   r   &  s;     $$[%5%5q9F,,H</r5   r   c                     ^  \ rS rSr% Sr\\   \S'      SS\\   S\	S\\	   S-  S\	S	S4
U 4S
 j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)

transformsNtseqr  lengthsr'   r(   c                   > [        S U 5       5      (       d  [        S5      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      :w  a8  [        S[        U R                  5       S[        U R                  5       S35      eX l	        g s  snf )Nc              3   B   #    U  H  n[        U[        5      v   M     g 7frR   r   r!   r   r  s     r3   r   (CatTransform.__init__.<locals>.<genexpr>R       :T:a++T   0All elements of tseq must be Transform instancesrL   r*   z	lengths (z) must match transforms (r   )
r   r   rO   r/   r0   r   r1  r  r3  r  )r1   r2  r  r3  r'   r  r2   s         r3   r0   CatTransform.__init__K  s     :T::: !STT6:;dLL,dD;J/t*?cC00GG}t||DOO 44 C-..GDOOH\G]]^_   <s   C?c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frR   )r=   r7  s     r3   r   )CatTransform.event_dim.<locals>.<genexpr>c       81;;r   )r   r1  r>   s    r3   r=   CatTransform.event_dima      8888r5   c                 ,    [        U R                  5      $ rR   )r  r3  r>   s    r3   lengthCatTransform.lengthe  s    4<<  r5   c                 ~    U R                   U:X  a  U $ [        U R                  U R                  U R                  U5      $ rR   )r+   r   r1  r  r3  rN   s     r3   rO   CatTransform.with_cachei  s2    z)KDOOTXXt||ZPPr5   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      U R                  :w  aA  [        SU R                    SUR                  U R                   5       SU R                   35      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$ )	Ndim  out of range for tensor with  dimensionsx.size() =  must equal length r   r  )r  r   r  rD  r   r1  r3  narrowr   r   cat)r1   r]   yslicesstarttransrD  xslices          r3   r\   CatTransform._calln  s   DHH.quuw. txxj >quuwi{S  66$((t{{* $((4txx(8'99LT[[MZ   $,,?MEXXdhh6FNN5=)NE @ yyhh//r5   c                    UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      U R                  :w  aA  [        SU R                    SUR                  U R                   5       SU R                   35      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$ )	NrI  rJ  rK  y.size(rM  rN  r   r  )r  r   r  rD  r   r1  r3  rO  r   rD   r   rP  )r1   r`   xslicesrR  rS  rD  yslices          r3   rd   CatTransform._inverse  s   DHH.quuw. txxj >quuwi{S  66$((t{{* $((4txx(8'99LT[[MZ   $,,?MEXXdhh6FNN599V,-NE @ yyhh//r5   c                 f   UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      U R                  :w  aA  [        SU R                    SUR                  U R                   5       SU R                   35      eUR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      U R                  :w  aA  [        SU R                    SUR                  U R                   5       SU R                   35      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      $ )NrI   out of range for x with rK  rL  rM  rN   out of range for y with rW  r   r  )r  r   r  rD  r   r1  r3  rO  rp   r=   r   r   r   rP  r  )r1   r]   r`   
logdetjacsrR  rS  rD  rT  rY  	logdetjacr  s              r3   rp   !CatTransform.log_abs_det_jacobian  s'   DHH.quuw. txxj 9!%%'+N  66$((t{{* $((4txx(8'99LT[[MZ  DHH.quuw. txxj 9!%%'+N  66$((t{{* $((4txx(8'99LT[[MZ  
 $,,?MEXXdhh6FXXdhh6F226BI/*9nnu6VW	i(NE @ hh!8-CNN"799Z11z?"r5   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frR   r   r7  s     r3   r   )CatTransform.bijective.<locals>.<genexpr>  r@  r   r   r1  r>   s    r3   r   CatTransform.bijective  rB  r5   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  U R
                  5      $ s  snf rR   )r   rP  r1  r%   r  r3  r1   r  s     r3   r%   CatTransform.domain  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 rR   )r   rP  r1  r&   r  r3  rg  s     r3   r&   CatTransform.codomain  s<     !%1AZZ1488T\\
 	
1ri  )r  r3  r1  )r   Nr   r   )rs   r   r   r   r   r   r!   r   r   r   r0   r	   r=   rD  rO   r\   rd   rp   r   r   r   r   r   r%   r&   r   r   r   s   @r3   r   r   :  s    Y
 (,y!  #%	
  
 , 93 9 9 ! ! !Q
0"0"##J 94 9 9 ##
 $

 ##
 $
r5   r   c            	          ^  \ rS rSr% Sr\\   \S'    SS\\   S\	S\	SS4U 4S	 j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   i  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)
r1  r2  r  r'   r(   Nc                    > [        S U 5       5      (       d  [        S5      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rR   r6  r7  s     r3   r   *StackTransform.__init__.<locals>.<genexpr>  r9  r:  r;  rL   )r   r   rO   r/   r0   r   r1  r  )r1   r2  r  r'   r  r2   s        r3   r0   StackTransform.__init__  se     :T::: !STT6:;dLL,dD;J/t* <s   A/c                 h    U R                   U:X  a  U $ [        U R                  U R                  U5      $ rR   )r+   r   r1  r  rN   s     r3   rO   StackTransform.with_cache  s,    z)KdootxxDDr5   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 rR   )ranger  r  select)r1   r  is      r3   _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,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      [        U R                  5      :w  aJ  [        SU R                    SUR                  U R                   5       S[        U R                  5       35      e/ n[        U R                  U5      U R                  5       H  u  p4UR                  U" U5      5        M     [        R                  " X R                   S9$ )NrI  rJ  rK  rL  rM   must equal len(transforms) r  )
r  r   r  r  r1  r   rw  r   r   stack)r1   r]   rQ  rT  rS  s        r3   r\   StackTransform._call  s    DHH.quuw. txxj >quuwi{S  66$((s4??33 $((4txx(8'99UVYZ^ZiZiVjUkl   QAMFNN5=) B{{711r5   c           
         UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      [        U R                  5      :w  aJ  [        SU R                    SUR                  U R                   5       S[        U R                  5       35      e/ n[        U R                  U5      U R                  5       H%  u  p4UR                  UR                  U5      5        M'     [        R                  " X R                   S9$ )NrI  rJ  rK  rW  rM  rz  r  )r  r   r  r  r1  r   rw  r   rD   r   r{  )r1   r`   rX  rY  rS  s        r3   rd   StackTransform._inverse  s    DHH.quuw. txxj >quuwi{S  66$((s4??33 $((4txx(8'99UVYZ^ZiZiVjUkl   QAMFNN599V,- B{{711r5   c           
      v   UR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      [        U R                  5      :w  aJ  [        SU R                    SUR                  U R                   5       S[        U R                  5       35      eUR                  5       * U R                   s=::  a  UR                  5       :  d,  O  [        SU R                    SUR                  5        S35      eUR                  U R                   5      [        U R                  5      :w  aJ  [        SU R                    SUR                  U R                   5       S[        U R                  5       35      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$ )
NrI  r\  rK  rL  rM  rz  r]  rW  r  )r  r   r  r  r1  rw  r   r   rp   r   r{  )	r1   r]   r`   r^  rQ  rX  rT  rY  rS  s	            r3   rp   #StackTransform.log_abs_det_jacobian  s   DHH.quuw. txxj 9!%%'+N  66$((s4??33 $((4txx(8'99UVYZ^ZiZiVjUkl  DHH.quuw. txxj 9!%%'+N  66$((s4??33 $((4txx(8'99UVYZ^ZiZiVjUkl  
++a.++a.%(4??%K!FEe88HI &L{{:8844r5   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frR   r   r7  s     r3   r   +StackTransform.bijective.<locals>.<genexpr>   r@  r   rd  r>   s    r3   r   StackTransform.bijective  rB  r5   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf rR   )r   r{  r1  r%   r  rg  s     r3   r%   StackTransform.domain"  s3       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 rR   )r   r{  r1  r&   r  rg  s     r3   r&   StackTransform.codomain'  s3       doo!Fo**o!FQQ!Fr  )r  r1  r  r   )rs   r   r   r   r   r   r!   r   r   r   r0   rO   rw  r\   rd   rp   r   r   r   r   r   r%   r&   r   r   r   s   @r3   r   r     s    
 Y JK	Y'	.1	CF			 	E
H2250 94 9 9 ##P $P ##R $Rr5   r   c                      ^  \ rS rSrSrSr\R                  rSr	SS\
S\SS4U 4S	 jjjr\S\R                  S-  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*   distributionr'   r(   Nc                 ,   > [         TU ]  US9  Xl        g r   )r/   r0   r  )r1   r  r'   r2   s      r3   r0   (CumulativeDistributionTransform.__init__D  s    J/(r5   c                 .    U R                   R                  $ rR   )r  supportr>   s    r3   r%   &CumulativeDistributionTransform.domainH  s      (((r5   c                 8    U R                   R                  U5      $ rR   )r  cdfrh   s     r3   r\   %CumulativeDistributionTransform._callL  s      $$Q''r5   c                 8    U R                   R                  U5      $ rR   )r  icdfrl   s     r3   rd   (CumulativeDistributionTransform._inverseO  s      %%a((r5   c                 8    U R                   R                  U5      $ rR   )r  log_probro   s      r3   rp   4CumulativeDistributionTransform.log_abs_det_jacobianR  s      ))!,,r5   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r+   r   r  rN   s     r3   rO   *CumulativeDistributionTransform.with_cacheU  s(    z)K.t/@/@ZXXr5   )r  r   r   )rs   r   r   r   r   r   r   rx  r&   rI   r   r   r0   r   r   r%   r\   rd   rp   rO   r   r   r   s   @r3   r   r   -  s    $ I((HD)\ )s )4 ) ) )..5 ) )()-Y Yr5   r   )1r   r  r   rB   collections.abcr   r   torch.nn.functionalnn
functionalrv  r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr   r   r	   r
   r   r   r   torch.typesr   __all__r!   rA   r   r"   r   r   r   r   rk  r   r   r   r   r   r   r   r    r   r   r   r   r   rS   r5   r3   <module>r     sg       $     + 9  . 0f fRE.	 E.Py D &b) K89 K8\I+y I+X9 .(RY (RVN
/y /2	 0*>I *>Z9  h
i h
VQ!I Q!h!y !H5-Y 5-pLY L,/	 /(K
9 K
\bRY bRJ+Yi +Yr5   