
    9i                        S SK r S SKrS SKrS SKrS SKJr  S SKJr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      r0 " S. S/\5      r1 " S0 S1\5      r2 " S2 S3\5      r3g)4    N)Sequence)OptionalUnion)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#   1   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     ^/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/transforms.pyr2   Transform.__init__b   s@    %@D	?1_)D899    c                 D    U R                   R                  5       nS US'   U$ )Nr.   )__dict__copy)r3   states     r5   __getstate__Transform.__getstate__m   s"    ""$fr7   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(   r0   r3   s    r5   r?   Transform.event_dimr   s:    ;;  DMM$;$;;;;(((UVVr7   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)r3   invs     r5   rF   Transform.invx   sB     99 ))+C;#D)CC(DI
r7   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    r5   signTransform.sign   s
     "!r7   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-   typer2   r#   rJ   r3   r)   s     r5   
with_cacheTransform.with_cache   sS    z)K:)"4"44:44!T$ZL0N"OPPr7   c                     XL $ N r3   others     r5   __eq__Transform.__eq__   s
    }r7   c                 .    U R                  U5      (       + $ rT   )rX   rV   s     r5   __ne__Transform.__ne__   s    ;;u%%%r7   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/   )r3   xx_oldy_oldys        r5   __call__Transform.__call__   sS     q ::a= '':LJJqM4r7   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/   )r3   rb   r`   ra   r_   s        r5   	_inv_callTransform._inv_call   sU     q ==##'':LMM!4r7   c                     [         e)z4
Abstract method to compute forward transformation.
rI   r3   r_   s     r5   r^   Transform._call   
     "!r7   c                     [         e)z4
Abstract method to compute inverse transformation.
rI   r3   rb   s     r5   rf   Transform._inverse   rl   r7   c                     [         e)zE
Computes the log det jacobian `log |dy/dx|` given input and output.
rI   r3   r_   rb   s      r5   log_abs_det_jacobianTransform.log_abs_det_jacobian   rl   r7   c                 4    U R                   R                  S-   $ )Nz())r4   __name__r@   s    r5   __repr__Transform.__repr__   s    ~~&&--r7   c                     U$ )zc
Infers the shape of the forward computation, given the input shape.
Defaults to preserving shape.
rU   r3   shapes     r5   forward_shapeTransform.forward_shape   	    
 r7   c                     U$ )ze
Infers the shapes of the inverse computation, given the output shape.
Defaults to preserving shape.
rU   ry   s     r5   inverse_shapeTransform.inverse_shape   r}   r7   )r-   r/   r.   r   )r*   r#   r,   )ru   
__module____qualname____firstlineno____doc__	bijectiver   
Constraint__annotations__intr2   r<   propertyr?   rF   rK   rQ   rX   r[   rc   rg   r^   rf   rr   rv   r{   r   __static_attributes____classcell__r4   s   @r5   r#   r#   1   s    *X I"""$$$	3 	t 	 	
 W3 W W
   "c " "Q&""". r7   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$ )rC      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 NrN   )r1   r2   r-   r.   )r3   r   r4   s     r5   r2   _InverseTransform.__init__   s    I$9$9:(	r7   Fis_discretec                 L    U R                   c   eU R                   R                  $ rT   )r.   r(   r@   s    r5   r'   _InverseTransform.domain   s"    yy$$$yy!!!r7   c                 L    U R                   c   eU R                   R                  $ rT   )r.   r'   r@   s    r5   r(   _InverseTransform.codomain   s"    yy$$$yyr7   c                 L    U R                   c   eU R                   R                  $ rT   )r.   r   r@   s    r5   r   _InverseTransform.bijective   s"    yy$$$yy"""r7   c                 L    U R                   c   eU R                   R                  $ rT   )r.   rK   r@   s    r5   rK   _InverseTransform.sign   s     yy$$$yy~~r7   c                     U R                   $ rT   r.   r@   s    r5   rF   _InverseTransform.inv   s    yyr7   c                 j    U R                   c   eU R                  R                  U5      R                  $ rT   )r.   rF   rQ   rP   s     r5   rQ   _InverseTransform.with_cache   s-    yy$$$xx"":.222r7   c                 ~    [        U[        5      (       d  gU R                  c   eU R                  UR                  :H  $ NF)
isinstancerC   r.   rV   s     r5   rX   _InverseTransform.__eq__   s6    %!233yy$$$yyEJJ&&r7   c                 `    U R                   R                   S[        U R                  5       S3$ )N())r4   ru   reprr.   r@   s    r5   rv   _InverseTransform.__repr__  s)    ..))*!DO+<A>>r7   c                 V    U R                   c   eU R                   R                  U5      $ rT   )r.   rg   rj   s     r5   rc   _InverseTransform.__call__	  s'    yy$$$yy""1%%r7   c                 X    U R                   c   eU R                   R                  X!5      * $ rT   )r.   rr   rq   s      r5   rr   &_InverseTransform.log_abs_det_jacobian  s*    yy$$$		..q444r7   c                 8    U R                   R                  U5      $ rT   )r.   r   ry   s     r5   r{   _InverseTransform.forward_shape      yy&&u--r7   c                 8    U R                   R                  U5      $ rT   )r.   r{   ry   s     r5   r   _InverseTransform.inverse_shape  r   r7   r   r   )ru   r   r   r   r   r#   r2   r   dependent_propertyr'   r(   r   boolr   r   rK   rF   rQ   rX   rv   rc   rr   r{   r   r   r   r   s   @r5   rC   rC      s    
)) ) ) ##6" 7" ##6  7  #4 # # c   Y  3'?&5.. .r7   rC   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   )rQ   r1   r2   r   )r3   r   r)   partr4   s       r5   r2   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   rV   s     r5   rX   ComposeTransform.__eq__)  s&    %!122zzU[[((r7   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)r3   r'   r?   r   s       r5   r'   ComposeTransform.domain.  s    zz###A%%JJrN++55	TZZ(D..1H1HHHII{{'<'<=I ) ,,,,,''' ,,VAQAQ5QRFr7   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   )r3   r(   r?   r   s       r5   r(   ComposeTransform.codomain=  s    zz###::b>**JJqM((22	JJD004;;3H3HHHII}}'>'>?I  .....)))"..xXEWEW9WXHr7   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frT   r   ).0ps     r5   	<genexpr>-ComposeTransform.bijective.<locals>.<genexpr>N  s     3
1;;
   )allr   r@   s    r5   r   ComposeTransform.bijectiveL  s    3

333r7   c                 L    SnU R                    H  nXR                  -  nM     U$ Nr,   )r   rK   )r3   rK   r   s      r5   rK   ComposeTransform.signP  s%    A&&=D r7   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 rT   )r.   r   r   r   rF   rD   rE   )r3   rF   r   s      r5   rF   ComposeTransform.invW  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   rP   s     r5   rQ   ComposeTransform.with_cacheb  s&    z)K

zBBr7   c                 <    U R                    H  nU" U5      nM     U$ rT   )r   )r3   r_   r   s      r5   rc   ComposeTransform.__call__g  s    JJDQA r7   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	   rr   r(   	functoolsreduceoperatoradd)r3   r_   rb   xsr   termsr?   s          r5   rr   %ComposeTransform.log_abs_det_jacobianl  s    zz##A&& SJJsODIId2b6l# $
		!KK))	djj"Sb'2ab6:JDQLL--a3YAVAV5V
 004;;3H3HHHI ; e44r7   c                 N    U R                    H  nUR                  U5      nM     U$ rT   )r   r{   r3   rz   r   s      r5   r{   ComposeTransform.forward_shape  s%    JJD&&u-E r7   c                 `    [        U R                  5       H  nUR                  U5      nM     U$ rT   )r   r   r   r   s      r5   r   ComposeTransform.inverse_shape  s*    TZZ(D&&u-E )r7   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
))r4   ru   joinr   rv   )r3   
fmt_stringr   s      r5   rv   ComposeTransform.__repr__  sV    ^^,,y8
innDJJ%GJqjjlJ%GHH
e
 &Hs   A
)r.   r   r   r   )ru   r   r   r   r   listr#   r   r2   rX   r   r   r'   r(   r   r   r   rK   r   rF   rQ   rc   rr   r{   r   rv   r   r   r   s   @r5   r   r     s    d9o 3 t  )
 ##6 7 ##6 7 44 4 4 c   Y  C

5*

 r7   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   )r1   r2   rQ   r   r   )r3   r   r   r)   r4   s       r5   r2   IndependentTransform.__init__  s.     	J/,77
C)B&r7   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r-   r   r   r   rP   s     r5   rQ   IndependentTransform.with_cache  s5    z)K#!?!?J
 	
r7   Fr   c                 l    [         R                  " U R                  R                  U R                  5      $ rT   )r   r   r   r'   r   r@   s    r5   r'   IndependentTransform.domain  s,    &&&&(F(F
 	
r7   c                 l    [         R                  " U R                  R                  U R                  5      $ rT   )r   r   r   r(   r   r@   s    r5   r(   IndependentTransform.codomain  s,    &&(($*H*H
 	
r7   c                 .    U R                   R                  $ rT   )r   r   r@   s    r5   r   IndependentTransform.bijective  s    "",,,r7   c                 .    U R                   R                  $ rT   )r   rK   r@   s    r5   rK   IndependentTransform.sign  s    ""'''r7   c                     UR                  5       U R                  R                  :  a  [        S5      eU R	                  U5      $ NToo few dimensions on input)dimr'   r?   r0   r   rj   s     r5   r^   IndependentTransform._call  s7    557T[[***:;;""1%%r7   c                     UR                  5       U R                  R                  :  a  [        S5      eU R                  R                  U5      $ r  )r  r(   r?   r0   r   rF   rn   s     r5   rf   IndependentTransform._inverse  s=    557T]],,,:;;""&&q))r7   c                 f    U R                   R                  X5      n[        X0R                  5      nU$ rT   )r   rr   r	   r   )r3   r_   rb   results       r5   rr   )IndependentTransform.log_abs_det_jacobian  s-    $$99!?(F(FGr7   c                 z    U R                   R                   S[        U R                  5       SU R                   S3$ )Nr   z, r   )r4   ru   r   r   r   r@   s    r5   rv   IndependentTransform.__repr__  s:    ..))*!D1D1D,E+FbIgIgHhhijjr7   c                 8    U R                   R                  U5      $ rT   )r   r{   ry   s     r5   r{   "IndependentTransform.forward_shape      ""0077r7   c                 8    U R                   R                  U5      $ rT   )r   r   ry   s     r5   r   "IndependentTransform.inverse_shape  r  r7   )r   r   r   r   )ru   r   r   r   r   r#   r   r2   rQ   r   r   r'   r(   r   r   r   rK   r^   rf   rr   rv   r{   r   r   r   r   s   @r5   r   r     s    " 	C!C $'C 	C
 
C C
 ##6
 7

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

 -4 - - (c ( (&
*

k88 8r7   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 elementsrN   )r   Sizer  r  numelr0   r1   r2   )r3   r  r  r)   r4   s       r5   r2   ReshapeTransform.__init__  sc     

8,I.== DNN$8$8$::UVVJ/r7   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rT   )r   r   r   lenr  r@   s    r5   r'   ReshapeTransform.domain  s$    &&{'7'7T]]9KLLr7   c                 r    [         R                  " [         R                  [        U R                  5      5      $ rT   )r   r   r   r  r  r@   s    r5   r(   ReshapeTransform.codomain  s$    &&{'7'7T^^9LMMr7   c                 d    U R                   U:X  a  U $ [        U R                  U R                  US9$ r   )r-   r   r  r  rP   s     r5   rQ   ReshapeTransform.with_cache  s,    z)Kt~~*UUr7   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rT   )rz   r  r  r  reshaper  )r3   r_   batch_shapes      r5   r^   ReshapeTransform._call
  s=    gg<#dmm*< <=yy~~566r7   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  X R
                  -   5      $ rT   )rz   r  r  r  r   r  )r3   rb   r!  s      r5   rf   ReshapeTransform._inverse  s=    gg=#dnn*= =>yy}}455r7   c                     UR                   S UR                  5       [        U R                  5      -
   nUR	                  U5      $ rT   )rz   r  r  r  	new_zeros)r3   r_   rb   r!  s       r5   rr   %ReshapeTransform.log_abs_det_jacobian  s6    gg<#dmm*< <={{;''r7   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  r0   r  r3   rz   cuts      r5   r{   ReshapeTransform.forward_shape  s    u:DMM**:;;%j3t}}--;$--'+E$K=	$--Q  Tc{T^^++r7   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  r0   r  r*  s      r5   r   ReshapeTransform.inverse_shape   s    u:DNN++:;;%j3t~~..;$..(+E$K=	$..AQR  Tc{T]]**r7   )r  r  r   r   )ru   r   r   r   r   r   r   r  r   r2   r   r   r'   r(   rQ   r^   rf   rr   r{   r   r   r   r   s   @r5   r   r     s     I 	
0**
0 ::
0 	
0
 

0 
0 ##M $M ##N $NV
76(,+ +r7   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      $ rT   )r   r   rV   s     r5   rX   ExpTransform.__eq__5      %..r7   c                 "    UR                  5       $ rT   )exprj   s     r5   r^   ExpTransform._call8      uuwr7   c                 "    UR                  5       $ rT   logrn   s     r5   rf   ExpTransform._inverse;  r6  r7   c                     U$ rT   rU   rq   s      r5   rr   !ExpTransform.log_abs_det_jacobian>      r7   rU   Nru   r   r   r   r   r   r   r'   positiver(   r   rK   rX   r^   rf   rr   r   rU   r7   r5   r   r   +  s=     F##HID/r7   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   iB  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   )r1   r2   r
   rA  )r3   rA  r)   r4   s      r5   r2   PowerTransform.__init__K  s"    J/(2r7   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r-   r   rA  rP   s     r5   rQ   PowerTransform.with_cacheO  s&    z)Kdmm
CCr7   c                 6    U R                   R                  5       $ rT   )rA  rK   r@   s    r5   rK   PowerTransform.signT  s    }}!!##r7   c                     [        U[        5      (       d  gU R                  R                  UR                  5      R	                  5       R                  5       $ r   )r   r   rA  eqr   itemrV   s     r5   rX   PowerTransform.__eq__X  s=    %00}}/335::<<r7   c                 8    UR                  U R                  5      $ rT   powrA  rj   s     r5   r^   PowerTransform._call]  s    uuT]]##r7   c                 >    UR                  SU R                  -  5      $ r   rM  rn   s     r5   rf   PowerTransform._inverse`  s    uuQ&''r7   c                 ^    U R                   U-  U-  R                  5       R                  5       $ rT   )rA  absr9  rq   s      r5   rr   #PowerTransform.log_abs_det_jacobianc  s(    !A%**,0022r7   c                 Z    [         R                  " U[        U R                  SS5      5      $ Nrz   rU   r   broadcast_shapesgetattrrA  ry   s     r5   r{   PowerTransform.forward_shapef  "    %%eWT]]GR-PQQr7   c                 Z    [         R                  " U[        U R                  SS5      5      $ rV  rW  ry   s     r5   r   PowerTransform.inverse_shapei  r[  r7   )rA  r   r   )ru   r   r   r   r   r   r?  r'   r(   r   r   r   r2   rQ   r   rK   rX   r^   rf   rr   r{   r   r   r   r   s   @r5   r   r   B  s     !!F##HI3 3S 3 3 3D
 $c $ $=
$(3RR Rr7   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_   rc  s     r5   _clipped_sigmoidri  m  s<    KK E;;u}}Q'UZZS599_MMr7   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   ir  z_
Transform via the mapping :math:`y = \frac{1}{1 + \exp(-x)}` and :math:`x = \text{logit}(y)`.
Tr,   c                 "    [        U[        5      $ rT   )r   r   rV   s     r5   rX   SigmoidTransform.__eq__|      %!122r7   c                     [        U5      $ rT   )ri  rj   s     r5   r^   SigmoidTransform._call  s    ""r7   c                     [         R                  " UR                  5      nUR                  UR                  SUR
                  -
  S9nUR                  5       U* R                  5       -
  $ r_  )r   rc  rd  re  rg  rh  r9  log1p)r3   rb   rc  s      r5   rf   SigmoidTransform._inverse  sK    AGG$GG

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

A2A..r7   rU   N)ru   r   r   r   r   r   r   r'   unit_intervalr(   r   rK   rX   r^   rf   rr   r   rU   r7   r5   r   r   r  s=     F((HID3#&
/r7   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      $ rT   )r   r   rV   s     r5   rX   SoftplusTransform.__eq__  s    %!233r7   c                     [        U5      $ rT   r   rj   s     r5   r^   SoftplusTransform._call  s    {r7   c                 b    U* R                  5       R                  5       R                  5       U-   $ rT   )expm1negr9  rn   s     r5   rf   SoftplusTransform._inverse  s'    zz|!%%'!++r7   c                     [        U* 5      * $ rT   r{  rq   s      r5   rr   &SoftplusTransform.log_abs_det_jacobian  s    !}r7   rU   Nr>  rU   r7   r5   r   r     s=    
 F##HID4,r7   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      $ rT   )r   r   rV   s     r5   rX   TanhTransform.__eq__  s    %//r7   c                 "    UR                  5       $ rT   )tanhrj   s     r5   r^   TanhTransform._call  s    vvxr7   c                 .    [         R                  " U5      $ rT   )r   atanhrn   s     r5   rf   TanhTransform._inverse  s     {{1~r7   c                 X    S[         R                  " S5      U-
  [        SU-  5      -
  -  $ )N       @g       )mathr9  r   rq   s      r5   rr   "TanhTransform.log_abs_det_jacobian  s*     dhhsma'(4!8*<<==r7   rU   N)ru   r   r   r   r   r   r   r'   intervalr(   r   rK   rX   r^   rf   rr   r   rU   r7   r5   r   r     sD    , F##D#.HID0
>r7   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      $ rT   )r   r   rV   s     r5   rX   AbsTransform.__eq__  r2  r7   c                 "    UR                  5       $ rT   )rS  rj   s     r5   r^   AbsTransform._call  r6  r7   c                     U$ rT   rU   rn   s     r5   rf   AbsTransform._inverse  r=  r7   rU   N)ru   r   r   r   r   r   r   r'   r?  r(   rX   r^   rf   r   rU   r7   r5   r   r     s*    5F##H/r7   r   c                   $  ^  \ rS rSrSrSr  SS\\\4   S\\\4   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   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   )r1   r2   r  r  
_event_dim)r3   r  r  r?   r)   r4   s        r5   r2   AffineTransform.__init__  s$     	J/
#r7   c                     U R                   $ rT   )r  r@   s    r5   r?   AffineTransform.event_dim  s    r7   Fr   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ Nr   r?   r   r   r   r@   s    r5   r'   AffineTransform.domain  7    >>Q###&&{'7'7HHr7   c                     U R                   S:X  a  [        R                  $ [        R                  " [        R                  U R                   5      $ r  r  r@   s    r5   r(   AffineTransform.codomain  r  r7   c                 z    U R                   U:X  a  U $ [        U R                  U R                  U R                  US9$ r   )r-   r   r  r  r?   rP   s     r5   rQ   AffineTransform.with_cache
  s7    z)KHHdjj$..Z
 	
r7   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   rJ  r  rV   s     r5   rX   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;;r7   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   floatrK   r@   s    r5   rK   AffineTransform.sign%  sU    djj'**djj)A-1Utzz9JQ9N2UTUUzz  r7   c                 :    U R                   U R                  U-  -   $ rT   r  r  rj   s     r5   r^   AffineTransform._call+  s    xx$**q.((r7   c                 8    XR                   -
  U R                  -  $ rT   r  rn   s     r5   rf   AffineTransform._inverse.  s    HH

**r7   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   )rz   r  r   r   r   	full_liker  r9  rS  r?   sizeviewsumexpand)r3   r_   rb   rz   r  r  result_sizes          r5   rr   $AffineTransform.log_abs_det_jacobian1  s    

eW%%__QU(<=FYYu%))+F>> ++-(94>>/:UBK[[-11"5F+T^^O,E}}U##r7   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rV  r   rX  rY  r  r  ry   s     r5   r{   AffineTransform.forward_shape>  7    %%7488Wb174::wPR3S
 	
r7   c           	          [         R                  " U[        U R                  SS5      [        U R                  SS5      5      $ rV  r  ry   s     r5   r   AffineTransform.inverse_shapeC  r  r7   )r  r  r  r   r   r   )ru   r   r   r   r   r   r   r   r  r   r2   r   r?   r   r   r'   r(   rQ   rX   rK   r^   rf   rr   r{   r   r   r   r   s   @r5   r   r     s   	 I 
$65=!
$ VU]#
$ 	
$
 
$ 

$ 
$ 3   ##6I 7I
 ##6I 7I

( !eFCK( ! !
)+$


 
r7   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   iI  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,   ra  diag   )rd  device.r   value)r   r  rc  rd  rh  re  r   sqrtcumprodeyerz   r  r   )r3   r_   rh  rzz1m_cumprod_sqrtrb   s          r5   r^   CorrCholeskyTransform._call^  s    JJqMkk!''"&&GGSa#gG.qr* qDE<<>11"5		!''"+QWWQXXFF$S#2#X.Aa@@r7   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  rq  r  )r3   rb   y_cumsumy_cumsum_shiftedy_vecy_cumsum_vectr_   s           r5   rf   CorrCholeskyTransform._inversem  s     u||AEr22xSbS1Aq6C"12.)*:D''))WWY(A-r7   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   r9  r  r   r  )r3   r_   rb   intermediates
y1m_cumsumy1m_cumsum_trilstick_breaking_logdettanh_logdets           r5   rr   *CorrCholeskyTransform.log_abs_det_jacobiany  s     !%B//
 -ZbA #&;&;&=&A&A"&E EAa 00488C=@EE"EMM$22r7   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  r0   round)r3   rz   NDs       r5   r{   #CorrCholeskyTransform.forward_shape  sp    u:>:;;"I4!a%<C'#-.A;!q MNNSbzQF""r7   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  r0   )r3   rz   r  r  s       r5   r   #CorrCholeskyTransform.inverse_shape  sa    u:>:;;9b	!233"IQK1SbzQD  r7   rU   rT   )ru   r   r   r   r   r   real_vectorr'   corr_choleskyr(   r   r^   rf   rr   r{   r   r   rU   r7   r5   r   r   I  s=      $$F((HI
3#!r7   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      $ rT   )r   r    rV   s     r5   rX   SoftmaxTransform.__eq__  rm  r7   c                 x    UnX"R                  SS5      S   -
  R                  5       nX3R                  SS5      -  $ )Nr   Tr   )r   r4  r  )r3   r_   logprobsprobss       r5   r^   SoftmaxTransform._call  s<    LLT2155::<yyT***r7   c                 &    UnUR                  5       $ rT   r8  )r3   rb   r  s      r5   rf   SoftmaxTransform._inverse  s    yy{r7   c                 :    [        U5      S:  a  [        S5      eU$ Nr,   r  r  ry   s     r5   r{   SoftmaxTransform.forward_shape      u:>:;;r7   c                 :    [        U5      S:  a  [        S5      eU$ r  r  ry   s     r5   r   SoftmaxTransform.inverse_shape  r  r7   rU   N)ru   r   r   r   r   r   r  r'   simplexr(   rX   r^   rf   r{   r   r   rU   r7   r5   r    r      s8     $$F""H3+

r7   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      $ rT   )r   r"   rV   s     r5   rX   StickBreakingTransform.__eq__      %!788r7   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  )rz   new_onesr  ri  r9  r  r   )r3   r_   offsetr  	z_cumprodrb   s         r5   r^   StickBreakingTransform._call  s    q1::aggbk#:#A#A"#EEQ-.UOOB'	Aq6#c)aV1&EEr7   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,   )rb  )	rz   r  r  r   re  rc  rd  rg  r9  )r3   rb   y_cropr  sfr_   s         r5   rf   StickBreakingTransform._inverse  s    38qzz&,,r*:;BB2FFr"" [[QWW!5!:!:;JJL2668#fjjl2r7   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,   .)rz   r  r  r9  rt  
logsigmoidr  )r3   r_   rb   r  detJs        r5   rr   +StickBreakingTransform.log_abs_det_jacobian  s~    q1::aggbk#:#A#A"#EE

Q\\!_$qcrc{'88==bAr7   c                 T    [        U5      S:  a  [        S5      eUS S US   S-   4-   $ Nr,   r  r   r  ry   s     r5   r{   $StickBreakingTransform.forward_shape  5    u:>:;;SbzU2Y],,,r7   c                 T    [        U5      S:  a  [        S5      eUS S US   S-
  4-   $ r  r  ry   s     r5   r   $StickBreakingTransform.inverse_shape  r  r7   rU   N)ru   r   r   r   r   r   r  r'   r  r(   r   rX   r^   rf   rr   r{   r   r   rU   r7   r5   r"   r"     sB     $$F""HI9-
-r7   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      $ rT   )r   r   rV   s     r5   rX   LowerCholeskyTransform.__eq__  r   r7   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ Nr   r  )dim1dim2)trildiagonalr4  
diag_embedrj   s     r5   r^   LowerCholeskyTransform._call  4    vvbzAJJBRJ8<<>IIKKKr7   c                 ~    UR                  S5      UR                  SSS9R                  5       R                  5       -   $ r  )r  r  r9  r  rn   s     r5   rf   LowerCholeskyTransform._inverse
  r  r7   rU   N)ru   r   r   r   r   r   r   r   r'   lower_choleskyr(   rX   r^   rf   r   rU   r7   r5   r   r     s=     $$[%5%5q9F))H9LLr7   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      $ rT   )r   r   rV   s     r5   rX    PositiveDefiniteTransform.__eq__  s    %!:;;r7   c                 >    [        5       " U5      nXR                  -  $ rT   )r   mTrj   s     r5   r^   PositiveDefiniteTransform._call  s    "$Q'44xr7   c                 r    [         R                  R                  U5      n[        5       R	                  U5      $ rT   )r   linalgcholeskyr   rF   rn   s     r5   rf   "PositiveDefiniteTransform._inverse  s*    LL!!!$%'++A..r7   rU   N)ru   r   r   r   r   r   r   r   r'   positive_definiter(   rX   r^   rf   r   rU   r7   r5   r   r     s;     $$[%5%5q9F,,H</r7   r   c                     ^  \ rS rSr% Sr\\   \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   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rT   r   r#   r   r  s     r5   r   (CatTransform.__init__.<locals>.<genexpr>:       :T:a++T   rN   r,   )	r   rQ   r1   r2   r   r/  r  r1  r  )r3   r0  r  r1  r)   r  r4   s         r5   r2   CatTransform.__init__3  s     :T:::::6:;dLL,dD;J/t*?cC00GG}4<< C$8888 <s   C c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frT   )r?   r5  s     r5   r   )CatTransform.event_dim.<locals>.<genexpr>G       81;;r   )r   r/  r@   s    r5   r?   CatTransform.event_dimE      8888r7   c                 ,    [        U R                  5      $ rT   )r  r1  r@   s    r5   lengthCatTransform.lengthI  s    4<<  r7   c                 ~    U R                   U:X  a  U $ [        U R                  U R                  U R                  U5      $ rT   )r-   r   r/  r  r1  rP   s     r5   rQ   CatTransform.with_cacheM  s2    z)KDOOTXXt||ZPPr7   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  rA  r   r/  r1  narrowr   r   cat)r3   r_   yslicesstarttransrA  xslices          r5   r^   CatTransform._callR  s    x488-aeeg-----vvdhh4;;... $,,?MEXXdhh6FNN5=)NE @ yyhh//r7   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$ rF  )r  r  rA  r   r/  r1  rG  r   rF   r   rH  )r3   rb   xslicesrJ  rK  rA  yslices          r5   rf   CatTransform._inverse]  s    x488-aeeg-----vvdhh4;;... $,,?MEXXdhh6FNN599V,-NE @ yyhh//r7   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      $ rF  )r  r  rA  r   r/  r1  rG  rr   r?   r	   r   r   rH  r  )r3   r_   rb   
logdetjacsrJ  rK  rA  rL  rP  	logdetjacr  s              r5   rr   !CatTransform.log_abs_det_jacobianh  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?"r7   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frT   r   r5  s     r5   r   )CatTransform.bijective.<locals>.<genexpr>  r=  r   r   r/  r@   s    r5   r   CatTransform.bijective  r?  r7   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  U R
                  5      $ s  snf rT   )r   rH  r/  r'   r  r1  r3   r  s     r5   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 rT   )r   rH  r/  r(   r  r1  r\  s     r5   r(   CatTransform.codomain  s:    !%1AZZ1488T\\
 	
1r^  )r  r1  r/  )r   Nr   r   )ru   r   r   r   r   r   r#   r   r   r   r   r2   r   r?   rA  rQ   r^   rf   rr   r   r   r   r   r   r'   r(   r   r   r   s   @r5   r   r   "  s    Y
 +/y!  (3-(	
  
 $ 93 9 9 ! ! !Q
	0	0#2 94 9 9 ##
 $

 ##
 $
r7   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)
r/  r0  r  r)   r*   Nc                    > [        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rT   r4  r5  s     r5   r   *StackTransform.__init__.<locals>.<genexpr>  r7  r8  rN   )r   rQ   r1   r2   r   r/  r  )r3   r0  r  r)   r  r4   s        r5   r2   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      $ rT   )r-   r!   r/  r  rP   s     r5   rQ   StackTransform.with_cache  s,    z)KdootxxDDr7   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 rT   )ranger  r  select)r3   r  is      r5   _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   rl  r   r   stack)r3   r_   rI  rL  rK  s        r5   r^   StackTransform._call  s    x488-aeeg-----vvdhh3t#7777 QAMFNN5=) B{{711r7   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$ ro  )
r  r  r  r/  r   rl  r   rF   r   rp  )r3   rb   rO  rP  rK  s        r5   rf   StackTransform._inverse  s    x488-aeeg-----vvdhh3t#7777 QAMFNN599V,- B{{711r7   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$ ro  )
r  r  r  r/  rl  r   r   rr   r   rp  )	r3   r_   rb   rS  rI  rO  rL  rP  rK  s	            r5   rr   #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4r7   c                 :    [        S U R                   5       5      $ )Nc              3   8   #    U  H  oR                   v   M     g 7frT   r   r5  s     r5   r   +StackTransform.bijective.<locals>.<genexpr>  r=  r   rY  r@   s    r5   r   StackTransform.bijective  r?  r7   c                     [         R                  " U R                   Vs/ s H  oR                  PM     snU R                  5      $ s  snf rT   )r   rp  r/  r'   r  r\  s     r5   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 rT   )r   rp  r/  r(   r  r\  s     r5   r(   StackTransform.codomain  s1      doo!Fo**o!FQQ!Fr|  )r  r/  r  r   )ru   r   r   r   r   r   r#   r   r   r   r2   rQ   rl  r^   rf   rr   r   r   r   r   r   r'   r(   r   r   r   s   @r5   r!   r!     s    
 Y JKY'.1CF	 E
H22
5 94 9 9 ##P $P ##R $Rr7   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                     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   )r1   r2   r  )r3   r  r)   r4   s      r5   r2   (CumulativeDistributionTransform.__init__  s    J/(r7   c                 .    U R                   R                  $ rT   )r  supportr@   s    r5   r'   &CumulativeDistributionTransform.domain  s      (((r7   c                 8    U R                   R                  U5      $ rT   )r  cdfrj   s     r5   r^   %CumulativeDistributionTransform._call  s      $$Q''r7   c                 8    U R                   R                  U5      $ rT   )r  icdfrn   s     r5   rf   (CumulativeDistributionTransform._inverse  s      %%a((r7   c                 8    U R                   R                  U5      $ rT   )r  log_probrq   s      r5   rr   4CumulativeDistributionTransform.log_abs_det_jacobian  s      ))!,,r7   c                 N    U R                   U:X  a  U $ [        U R                  US9$ r   )r-   r   r  rP   s     r5   rQ   *CumulativeDistributionTransform.with_cache  s(    z)K.t/@/@ZXXr7   )r  r   r   )ru   r   r   r   r   r   r   rv  r(   rK   r   r   r2   r   r   r   r'   r^   rf   rr   rQ   r   r   r   s   @r5   r   r     s    $ I((HD)\ )s )4 ) ) )!7!78 ) )()-Y Yr7   r   )4r   r  r   rD   collections.abcr   typingr   r   r   torch.nn.functionalnn
functionalrt  r   torch.distributionsr    torch.distributions.distributionr   torch.distributions.utilsr	   r
   r   r   r   r   r   torch.typesr   __all__r#   rC   r   r$   r   r   r   r   ri  r   r   r   r   r   r   r    r"   r   r   r   r!   r   rU   r7   r5   <module>r     sh       $ "     + 9  . 0f fR;.	 ;.|wy wt &b) I89 I8XG+y G+T9 .(RY (RVN
/y /2	 0*>I *>Z9  f
i f
RP!I P!f!y !H5-Y 5-pLY L,/	 /(m
9 m
`GRY GRT+Yi +Yr7   