
    Vh                     @   d dl mZ d dlmZmZmZmZmZ d dlm	Z	 d dl
Z
d dlmc mZ d dl
mZ d dlmZ d dlmZ dZg d	Zd
 Zd Zd ZddZd ZddZ e	dd      Z e	dd      Z G d deeef         Z G d deeef   e      Z d dede!defdZ"d dede!defdZ#y)!    )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                     t        d | D              st        d      t        d | D              st        t        j                               }| D ]?  }t        |t        j                        st        |j                  |j                        } n | D cg c]%  }t        |      r|nt        j                  |fi |' }}t        j                  | S t        j                  |  S c c}w )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   V   K   | ]!  }t        |      xs t        |t               # y wN)r   
isinstancer   .0vs     I/home/dcms/DCMS/lib/python3.12/site-packages/torch/distributions/utils.py	<genexpr>z broadcast_all.<locals>.<genexpr>*   s$     Kq~a :Jq'$::Ks   ')ziInput arguments must all be instances of Number, torch.Tensor or objects implementing __torch_function__.c              3   2   K   | ]  }t        |        y wr   r   r   s     r   r   z broadcast_all.<locals>.<genexpr>/   s     1Q~a 1s   )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G
 	
 1&11"&U-D-D-F"G 	E%.U[[F	
 MS
GH"AQ(B'(BB

 
 &&
33""F++	
s   *C&c                    t         j                  j                         rBt        j                  t        j                  | ||      t        j
                  | ||            S t        j                  | ||      j                         S )Nr    )r%   _C_get_tracing_statenormalzerosonesemptynormal_)shaper   r!   s      r   _standard_normalr6   <   sa    xx""$||KKU6:JJuE&9
 	
 ;;uE&9AACC    c                 v    |dk(  r| S | j                   d|  dz   }| j                  |      j                  d      S )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)r9   )r5   reshapesum)r+   dimrequired_shapes      r   _sum_rightmostr>   F   sA     ax[[3$'%/N==(,,R00r7   c                 ^    |rt        j                  |       S t        j                  | d      S )a  
    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.
    r9   )r<   )r%   sigmoidFsoftmax)logits	is_binarys     r   r   r   T   s'     }}V$$99V$$r7   c                     t        j                  | j                        j                  }| j	                  |d|z
        S )a  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)probsrJ   s     r   r   r   `   s3    , ++ekk
"
&
&C;;3AG;,,r7   c                     t        |       }|r,t        j                  |      t        j                  |       z
  S t        j                  |      S )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)rL   rD   
ps_clampeds      r   r   r   z   s?     U#Jyy$u{{J;'???99Z  r7   TT)contravariantR)	covariantc                       e Zd ZdZdeegef   ddfdZe	 dddde	ddfd	       Z
eddede	defd
       Z
	 ddeedf   de	ddfdZ
y)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                 *    || _         t        | |       y r   )rV   r   selfrV   s     r   __init__zlazy_property.__init__   s    )0tW%r7   instanceobj_typez!_lazy_property_and_property[T, R]c                      y r    rZ   r\   r]   s      r   __get__zlazy_property.__get__   s     /2r7   c                      y r   r_   r`   s      r   ra   zlazy_property.__get__   s    ?Br7   z%R | _lazy_property_and_property[T, R]c                     |t        | j                        S t        j                         5  | j                  |      }d d d        t	        || j                  j
                         |S # 1 sw Y   ,xY wr   )_lazy_property_and_propertyrV   r%   enable_gradsetattr__name__)rZ   r\   r]   r+   s       r   ra   zlazy_property.__get__   sc     .t||<<  	+LL*E	+$,,//7	+ 	+s   A))A2r   )rg   
__module____qualname____doc__r   rQ   rS   r[   r   r   ra   r   r_   r7   r   r   r      s    &!a 0 &T & .222(+2	,2 2 BBSBAB B 9=ag25	0r7   r   c                   ,    e Zd ZdZdeegef   ddfdZy)rd   zWe want lazy properties to look like multiple things.

    * property when Sphinx autodoc looks
    * lazy_property when Distribution validate_args looks
    rV   rW   Nc                 0    t         j                  | |       y r   )propertyr[   rY   s     r   r[   z$_lazy_property_and_property.__init__   s    $(r7   )rg   rh   ri   rj   r   rQ   rS   r[   r_   r7   r   rd   rd      s%    )!a 0 )T )r7   rd   matdiagrW   c           	      .   | j                   d   }t        j                  j                         s$|| k  s||k\  rt	        d| d|  d|dz
   d      t        j
                  || j                        }||j                  dd      |dz   z   k  }| d|f   }|S )	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.
    r9   zdiag (z) provided is outside [z, rF   z].r!   .)r5   r%   r.   r/   r#   aranger!   view)rn   ro   nrr   	tril_maskvecs         r   r   r      s    
 			"A88&&(daRi4196$'>rd"QUG2NOO\\!CJJ/FR+tax88I
c9n
CJr7   rv   c                    dd|z  z    dd|z  z   dz  d| j                   d   z  z   dt        |      z  |dz   z  z   dz  z   dz  }t        j                  | j                        j
                  }t        j                  j                         s0t        |      |z
  |kD  rt        d| j                   d    dd	z         t        |t        j                        rt        |j                               n
t        |      }| j                  | j                   d
d t        j                  ||f      z         }t        j                  || j                         }||j#                  dd      |dz   z   k  }| |d|f<   |S )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.
    rF         r9      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.Nrq   .)r5   absr%   rI   r   rJ   r.   r/   roundr#   r   r
   item	new_zerosSizerr   r!   rs   )rv   ro   rt   rJ   rn   rr   ru   s          r   r   r      sV    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affhE!HA
--		#2QF);;
<C\\!CJJ/FR+tax88ICYJr7   )F)r   )$	functoolsr   typingr   r   r   r   r   typing_extensionsr	   r%   torch.nn.functionalnn
functionalrA   r
   torch.overridesr   torch.typesr   euler_constant__all__r   r6   r>   r   r   r   rQ   rS   r   rm   rd   intr   r   r_   r7   r   <module>r      s    $ : : %     *  (,DD1	%-4
! Ct$C4 GAqDM >)-1"5x )F # f F # f r7   