
    [Th                     @   S SK Jr  S SKJrJrJrJrJ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r/ S	QrS
 rS rS rSS jrS rSS jr\	" SSS9r\	" SSS9r " S S\\\4   5      r " S S\\\4   \5      r S S\S\!S\4S jjr"S S\S\!S\4S jjr#g)!    )update_wrapper)AnyCallableGenericoverloadUnion)TypeVarN)Tensoris_tensor_like)_Numbergox?)broadcast_alllogits_to_probsclamp_probsprobs_to_logitslazy_propertytril_matrix_to_vecvec_to_tril_matrixc                     [        S U  5       5      (       d  [        S5      e[        S U  5       5      (       d  [        [        R                  " 5       S9nU  HB  n[        U[        R                  5      (       d  M$  [        UR                  UR                  S9n  O   U  Vs/ s H,  n[        U5      (       a  UO[        R                  " U40 UD6PM.     nn[        R                  " U6 $ [        R                  " U 6 $ s  snf )a  
Given a list of values (possibly containing numbers), returns a list where each
value is broadcasted based on the following rules:
  - `torch.*Tensor` instances are broadcasted as per :ref:`_broadcasting-semantics`.
  - Number instances (scalars) are upcast to tensors having
    the same size and type as the first tensor passed to `values`.  If all the
    values are scalars, then they are upcasted to scalar Tensors.

Args:
    values (list of `Number`, `torch.*Tensor` or objects implementing __torch_function__)

Raises:
    ValueError: if any of the values is not a `Number` instance,
        a `torch.*Tensor` instance, or an instance implementing __torch_function__
c              3   f   #    U  H'  n[        U5      =(       d    [        U[        5      v   M)     g 7fN)r   
isinstancer   .0vs     Q/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/utils.py	<genexpr> broadcast_all.<locals>.<genexpr>*   s$     KFq~a :Jq'$::Fs   /1ziInput arguments must all be instances of Number, torch.Tensor or objects implementing __torch_function__.c              3   8   #    U  H  n[        U5      v   M     g 7fr   r   r   s     r   r   r   /   s     1&Q~a  &s   )dtyper    device)all
ValueErrordicttorchget_default_dtyper   r
   r    r"   r   tensorbroadcast_tensors)valuesoptionsvaluer   
new_valuess        r   r   r      s      KFKKKG
 	
 1&111"&U-D-D-F"GE%..U[[F 
 MS
LRq""AQ(B'(BBF 	 
 &&
33""F++	
s   "3C=c           	         [         R                  R                  5       (       a=  [         R                  " [         R                  " XUS9[         R
                  " XUS95      $ [         R                  " XUS9R                  5       $ )Nr!   )r&   _C_get_tracing_statenormalzerosonesemptynormal_)shaper    r"   s      r   _standard_normalr7   <   s^    xx""$$||KK6:JJu&9
 	
 ;;u&9AACC    c                 x    US:X  a  U $ U R                   SU*  S-   nU R                  U5      R                  S5      $ )z
Sum out ``dim`` many rightmost dimensions of a given tensor.

Args:
    value (Tensor): A tensor of ``.dim()`` at least ``dim``.
    dim (int): The number of rightmost dims to sum out.
r   N)r:   )r6   reshapesum)r,   dimrequired_shapes      r   _sum_rightmostr?   F   sA     ax[[3$'%/N==(,,R00r8   c                 f    U(       a  [         R                  " U 5      $ [        R                  " U SS9$ )z
Converts a tensor of logits into probabilities. Note that for the
binary case, each value denotes log odds, whereas for the
multi-dimensional case, the values along the last dimension denote
the log probabilities (possibly unnormalized) of the events.
r:   )r=   )r&   sigmoidFsoftmax)logits	is_binarys     r   r   r   T   s'     }}V$$99V$$r8   c                 |    [         R                  " U R                  5      R                  nU R	                  USU-
  S9$ )ah  Clamps the probabilities to be in the open interval `(0, 1)`.

The probabilities would be clamped between `eps` and `1 - eps`,
and `eps` would be the smallest representable positive number for the input data type.

Args:
    probs (Tensor): A tensor of probabilities.

Returns:
    Tensor: The clamped probabilities.

Examples:
    >>> probs = torch.tensor([0.0, 0.5, 1.0])
    >>> clamp_probs(probs)
    tensor([1.1921e-07, 5.0000e-01, 1.0000e+00])

    >>> probs = torch.tensor([0.0, 0.5, 1.0], dtype=torch.float64)
    >>> clamp_probs(probs)
    tensor([2.2204e-16, 5.0000e-01, 1.0000e+00], dtype=torch.float64)

   )minmax)r&   finfor    epsclamp)probsrK   s     r   r   r   `   s3    , ++ekk
"
&
&C;;3AG;,,r8   c                     [        U 5      nU(       a.  [        R                  " U5      [        R                  " U* 5      -
  $ [        R                  " U5      $ )a  
Converts a tensor of probabilities into logits. For the binary case,
this denotes the probability of occurrence of the event indexed by `1`.
For the multi-dimensional case, the values along the last dimension
denote the probabilities of occurrence of each of the events.
)r   r&   loglog1p)rM   rE   
ps_clampeds      r   r   r   z   s?     U#Jyy$u{{J;'???99Z  r8   TT)contravariantR)	covariantc                       \ rS rSrSrS\\/\4   SS4S jr\	 SSSS\
SS	4S
 jj5       r\	SS\S\
S\4S jj5       r SS\\S4   S\
SS4S jjrSrg)r      z
Used as a decorator for lazy loading of class attributes. This uses a
non-data descriptor that calls the wrapped method to compute the property on
first call; thereafter replacing the wrapped method into an instance
attribute.
wrappedreturnNc                 &    Xl         [        X5        g r   )rX   r   selfrX   s     r   __init__lazy_property.__init__   s    )0t%r8   instanceobj_typez!_lazy_property_and_property[T, R]c                     g r    r\   r_   r`   s      r   __get__lazy_property.__get__   s     /2r8   c                     g r   rb   rc   s      r   rd   re      s    ?Br8   z%R | _lazy_property_and_property[T, R]c                     Uc  [        U R                  5      $ [        R                  " 5          U R                  U5      nS S S 5        [	        XR                  R
                  W5        U$ ! , (       d  f       N0= fr   )_lazy_property_and_propertyrX   r&   enable_gradsetattr__name__)r\   r_   r`   r,   s       r   rd   re      sZ     .t||<< LL*E !,,//7 ! s   A**
A8)rX   r   )rk   
__module____qualname____firstlineno____doc__r   rR   rT   r]   r   r   rd   r   __static_attributes__rb   r8   r   r   r      s    &!a 0 &T & .222(+2	,2 2 BBSBAB B 9=ag25	0 r8   r   c                   6    \ rS rSrSrS\\/\4   SS4S jrSr	g)rh      zWe want lazy properties to look like multiple things.

* property when Sphinx autodoc looks
* lazy_property when Distribution validate_args looks
rX   rY   Nc                 .    [         R                  X5        g r   )propertyr]   r[   s     r   r]   $_lazy_property_and_property.__init__   s    $(r8   rb   )
rk   rl   rm   rn   ro   r   rR   rT   r]   rp   rb   r8   r   rh   rh      s%    )!a 0 )T )r8   rh   matdiagrY   c           	      0   U R                   S   n[        R                  R                  5       (       d$  X* :  d  X:  a  [	        SU SU*  SUS-
   S35      e[        R
                  " X R                  S9nX3R                  SS5      US-   -   :  nU SU4   nU$ )	z
Convert a `D x D` matrix or a batch of matrices into a (batched) vector
which comprises of lower triangular elements from the matrix in row order.
r:   zdiag (z) provided is outside [z, rG   z].r"   .)r6   r&   r/   r0   r$   aranger"   view)rv   rw   nrz   	tril_maskvecs         r   r   r      s    
 			"A88&&((dRi496$'>rd"QUG2NOO\\!JJ/FR+tax88I
c9n
CJr8   r~   c                    SSU-  -   * SSU-  -   S-  SU R                   S   -  -   S[        U5      -  US-   -  -   S-  -   S-  n[        R                  " U R                  5      R
                  n[        R                  R                  5       (       d1  [        U5      U-
  U:  a  [        SU R                   S    S3S	-   5      e[        U[        R                  5      (       a  [        UR                  5       5      O
[        U5      nU R                  U R                   S
S [        R                  " X"45      -   5      n[        R                  " X R                   S9nXUR#                  SS5      US-   -   :  nXSU4'   U$ )z
Convert a vector or a batch of vectors into a batched `D x D`
lower triangular matrix containing elements from the vector in row order.
rG         r:      g      ?zThe size of last dimension is z which cannot be expressed as z3the lower triangular part of a square D x D matrix.Nry   .)r6   absr&   rJ   r    rK   r/   r0   roundr$   r   r
   item	new_zerosSizerz   r"   r{   )r~   rw   r|   rK   rv   rz   r}   s          r   r   r      sT    a$h,DLQSYYr]!22QT]dQh5OOTW
W	X		
A ++cii
 
$
$C88&&((eAhlS.@,SYYr]O;YZCD
 	
 &a66affhE!HA
--		#2QF);;
<C\\!JJ/FR+tax88IYJr8   )F)r   )$	functoolsr   typingr   r   r   r   r   typing_extensionsr	   r&   torch.nn.functionalnn
functionalrB   r
   torch.overridesr   torch.typesr   euler_constant__all__r   r7   r?   r   r   r   rR   rT   r   rt   rh   intr   r   rb   r8   r   <module>r      s    $ : : %     *  (,DD1	%-4
! Ct$C4 GAqDM >)-1"5x )F # f F # f r8   