o
    h[                     @   s  U d dl mZ d dlmZ d dlmZmZmZmZm	Z	m
Z
mZmZ d dlZd dlm  mZ d dlmZmZ d dlmZ d dlmZmZmZmZ dZee ed	< g d
Zdeeef deedf fddZ deee!ef  de	e de	e defddZ"dede!defddZ#d3dede$defddZ%dedefdd Z&d3dede$defd!d"Z'ed#d$d%Z(ed&d$d'Z)G d(d) d)ee(e)f Z*G d*d+ d+e*e(e)f e+Z,d4d,ed-e!defd.d/Z-d4d0ed-e!defd1d2Z.dS )5    )Sequence)update_wrapper)AnyCallableFinalGenericOptionaloverloadTypeVarUnionN)SymIntTensoris_tensor_like)_dtype_NumberDeviceNumbergox?euler_constant)broadcast_alllogits_to_probsclamp_probsprobs_to_logitslazy_propertytril_matrix_to_vecvec_to_tril_matrixvaluesreturn.c                     s   t dd | D stdt dd | D s@tt d | D ]}t|tjr1t|j|jd  q2q fd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`.
      - 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                 s   s"    | ]}t |pt|tV  qd S N)r   
isinstancer   .0v r#   _/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/distributions/utils.py	<genexpr>+   s     z broadcast_all.<locals>.<genexpr>ziInput arguments must all be instances of Number, torch.Tensor or objects implementing __torch_function__.c                 s   s    | ]}t |V  qd S r   r   r    r#   r#   r$   r%   0   s    )dtyper&   devicec                    s*   g | ]}t |r
|ntj|fi  qS r#   )r   torchZtensorr    optionsr#   r$   
<listcomp>6   s    z!broadcast_all.<locals>.<listcomp>)
all
ValueErrordictr)   Zget_default_dtyper   r   r&   r(   Zbroadcast_tensors)r   value
new_valuesr#   r*   r$   r      s    


r   shaper&   r(   c                 C   sB   t j rt t j| ||dt j| ||dS t j| ||d S )Nr'   )r)   _C_get_tracing_statenormalZzerosZonesemptyZnormal_)r2   r&   r(   r#   r#   r$   _standard_normal=   s   
r7   r0   dimc                 C   s0   |dkr| S | j d|  d }| |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   )r2   Zreshapesum)r0   r8   Zrequired_shaper#   r#   r$   _sum_rightmostK   s   r;   Flogits	is_binaryc                 C   s   |rt | S tj| d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   )r8   )r)   ZsigmoidFZsoftmax)r<   r=   r#   r#   r$   r   Y   s   
r   probsc                 C   s    t | jj}| j|d| d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)r?   rD   r#   r#   r$   r   e   s   r   c                 C   s,   t | }|rt|t|  S t|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)r?   r=   Z
ps_clampedr#   r#   r$   r      s   
r   TT)contravariantR)	covariantc                   @   s   e Zd ZdZdeegef ddfddZe	dddde	dd	fd
dZ
eddede	defddZ
	ddeedf de	ddf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.
    wrappedr   Nc                 C   s   || _ t| | d S r   )rL   r   selfrL   r#   r#   r$   __init__   s   zlazy_property.__init__instanceobj_typez!_lazy_property_and_property[T, R]c                 C      d S r   r#   rN   rP   rQ   r#   r#   r$   __get__   s   zlazy_property.__get__c                 C   rR   r   r#   rS   r#   r#   r$   rT      s   z%R | _lazy_property_and_property[T, R]c                 C   sX   |d u r	t | jS t  | |}W d    n1 sw   Y  t|| jj| |S r   )_lazy_property_and_propertyrL   r)   Zenable_gradsetattr__name__)rN   rP   rQ   r0   r#   r#   r$   rT      s   

r   )rW   
__module____qualname____doc__r   rH   rJ   rO   r	   r   rT   r   r#   r#   r#   r$   r      s,    
r   c                   @   s,   e Zd ZdZdeegef ddfddZdS )rU   zWe want lazy properties to look like multiple things.

    * property when Sphinx autodoc looks
    * lazy_property when Distribution validate_args looks
    rL   r   Nc                 C   s   t | | d S r   )propertyrO   rM   r#   r#   r$   rO      s   z$_lazy_property_and_property.__init__)rW   rX   rY   rZ   r   rH   rJ   rO   r#   r#   r#   r$   rU      s     rU   matdiagc                 C   s   | j d }tj s$|| k s||kr$td| d|  d|d  dtj|| jd}||dd|d  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, r@   z].r(   .)r2   r)   r3   r4   r.   aranger(   view)r\   r]   nr_   	tril_maskvecr#   r#   r$   r      s   
"r   rc   c                 C   s  dd|   dd|  d d| j d   dt| |d   d  d }t| jj}tj sEt|| |krEt	d| j d  dd	 t
|tjrQt| nt|}| | j d
d t||f }tj|| jd}||dd|d  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.
    r@         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.Nr^   .)r2   absr)   rC   r&   rD   r3   r4   roundr.   r   r   itemZ	new_zerosSizer_   r(   r`   )rc   r]   ra   rD   r\   r_   rb   r#   r#   r$   r      s$   4 "r   )F)r   )/collections.abcr   	functoolsr   typingr   r   r   r   r   r	   r
   r   r)   Ztorch.nn.functionalnnZ
functionalr>   r   r   Ztorch.overridesr   Ztorch.typesr   r   r   r   r   float__annotations____all__tupler   intr7   r;   boolr   r   r   rH   rJ   r   r[   rU   r   r   r#   r#   r#   r$   <module>   s<    (""
