
    קg                        d dl mZ d dlmZ d dlmZmZ d dlZd dlm	c 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 G d d          Z G d dee          Zddej        dedej        fdZddej        dedej        fdZdS )    )update_wrapper)Number)AnyDictNis_tensor_likeg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                       syt          t          j                              | D ]9}t          |t          j                  rt          |j        |j                   n:fd| D             }t          j	        | S t          j	        |  S )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`.
      - numbers.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 `numbers.Number`, `torch.*Tensor` or objects implementing __torch_function__)

    Raises:
        ValueError: if any of the values is not a `numbers.Number` instance,
            a `torch.*Tensor` instance, or an instance implementing __torch_function__
    c              3   ^   K   | ](}t          |          pt          |t                    V  )d S N)r   
isinstancer   .0vs     U/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/distributions/utils.py	<genexpr>z broadcast_all.<locals>.<genexpr>(   s9      JJa~a  9Jq&$9$9JJJJJJ    zqInput arguments must all be instances of numbers.Number, torch.Tensor or objects implementing __torch_function__.c              3   4   K   | ]}t          |          V  d S r   r   r   s     r   r   z broadcast_all.<locals>.<genexpr>-   s*      11Q~a  111111r   )dtyper   devicec                 V    g | ]%}t          |          r|nt          j        |fi &S  )r   torchtensor)r   r   optionss     r   
<listcomp>z!broadcast_all.<locals>.<listcomp>3   sI     
 
 
GH""BAAQ(B(B'(B(B
 
 
r   )
all
ValueErrordictr    get_default_dtyper   Tensorr   r   broadcast_tensors)valuesvalue
new_valuesr"   s      @r   r	   r	      s      JJ6JJJJJ 
G
 
 	
 11&11111 	4"&U-D-F-F"G"G"G 	 	E%.. U[FFF
 
 
 
LR
 
 

 &
33"F++r   c                    t           j                                        r?t          j        t          j        | ||          t          j        | ||                    S t          j        | ||                                          S )Nr   )r    _C_get_tracing_statenormalzerosonesemptynormal_)shaper   r   s      r   _standard_normalr6   :   sy    x""$$ 
|KU6:::JuE&999
 
 	
 ;uE&999AACCCr   c                     |dk    r| S | j         d|          dz   }|                     |                              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)r8   )r5   reshapesum)r+   dimrequired_shapes      r   _sum_rightmostr=   D   sI     axx[3$'%/N==((,,R000r   Fc                 Z    |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.
    r8   )r;   )r    sigmoidFsoftmax)logits	is_binarys     r   r
   r
   R   s1      %}V$$$9V$$$$r   c                 r    t          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)probsrI   s     r   r   r   ^   s2    , +ek
"
"
&C;;3AG;,,,r   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)rK   rC   
ps_clampeds      r   r   r   x   sK     U##J @y$$u{J;'?'???9Z   r   c                        e Zd ZdZd ZddZdS )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.
    c                 4    || _         t          | |           d S r   )wrappedr   selfrR   s     r   __init__zlazy_property.__init__   s    tW%%%%%r   Nc                     |t          | j                  S t          j                    5  |                     |          }d d d            n# 1 swxY w Y   t	          || j        j        |           |S r   )_lazy_property_and_propertyrR   r    enable_gradsetattr__name__)rT   instanceobj_typer+   s       r   __get__zlazy_property.__get__   s    .t|<<<   	+ 	+LL**E	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+ 	+$,/777s   AAAr   )rZ   
__module____qualname____doc__rU   r]   r   r   r   r   r      sA         & & &     r   r   c                       e Zd ZdZd ZdS )rW   zWe want lazy properties to look like multiple things.

    * property when Sphinx autodoc looks
    * lazy_property when Distribution validate_args looks
    c                 <    t                               | |           d S r   )propertyrU   rS   s     r   rU   z$_lazy_property_and_property.__init__   s    $(((((r   N)rZ   r^   r_   r`   rU   r   r   r   rW   rW      s-         ) ) ) ) )r   rW   matdiagreturnc           	      :   | j         d         }t          j                                        s*|| k     s||k    rt	          d| d|  d|dz
   d          t          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.
    r8   zdiag (z) provided is outside [z, rE   z].r   .)r5   r    r.   r/   r%   aranger   view)rd   re   nri   	tril_maskvecs         r   r   r      s    
 		"A8&&(( NdaRii4199L$LLrLLQqSLLLMMM\!CJ///FR++tax88I
c9n
CJr   rm   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                                        s7t          |          |z
  |k    r!t          d| j         d          dd	z             t          |t          j                  r!t          |                                          nt          |          }|                     | j         d
d         t          j        ||f          z             }t          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.
    rE         r8      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.Nrh   .)r5   absr    rH   r   rI   r.   r/   roundr%   r   r(   item	new_zerosSizeri   r   rj   )rm   re   rk   rI   rd   ri   rl   s          r   r   r      sv    a$h,DLQSYr]!22QT]dQh5OOTW
W	X		
A +ci
 
 
$C8&&(( 
eAhhlS.@.@ZSYr]ZZZCD
 
 	
 &a66DaffhhE!HHA
--	#2#QF););;
<
<C\!CJ///FR++tax88ICYJr   )F)r   )	functoolsr   numbersr   typingr   r   r    torch.nn.functionalnn
functionalr@   torch.overridesr   euler_constant__all__r	   r6   r=   r
   r   r   r   rc   rW   r(   intr   r   r   r   r   <module>r      s   $ $ $ $ $ $                         * * * * * * (  , , ,DD D D1 1 1	% 	% 	% 	%- - -4
! 
! 
! 
!       *) ) ) ) )- ) ) ) EL  EL     EL  EL      r   