U
    d                     @   s   d dl mZ d dl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Zdd Zd	d
 Zdd ZdddZdd ZdddZG dd dZG dd deeZdddZdddZdS )     )update_wrapper)NumberN)DictAnyis_tensor_likegox?c                     s   t dd | D stdt dd | D stt d | D ]$}t|tjr>t|j|jd  qdq> 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`.
      - 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                 s   s    | ]}t |pt|tV  qd S N)r   
isinstancer   .0v r   =/tmp/pip-unpacked-wheel-ua33x9lu/torch/distributions/utils.py	<genexpr>   s   z broadcast_all.<locals>.<genexpr>zqInput arguments must all be instances of numbers.Number, torch.Tensor or objects implementing __torch_function__.c                 s   s   | ]}t |V  qd S r   r   r
   r   r   r   r      s     )dtyper   devicec                    s&   g | ]}t |r|ntj|f qS r   )r   torchZtensorr
   optionsr   r   
<listcomp>%   s   z!broadcast_all.<locals>.<listcomp>)
all
ValueErrordictr   Zget_default_dtyper	   Tensorr   r   Zbroadcast_tensors)valuesvalueZ
new_valuesr   r   r   broadcast_all   s    

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normalzerosZonesemptyZnormal_)shaper   r   r   r   r   _standard_normal+   s
    
r$   c                 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)r%   )r#   Zreshapesum)r   dimZrequired_shaper   r   r   _sum_rightmost3   s    r(   Fc                 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.
    r%   )r'   )r   ZsigmoidFZsoftmax)Zlogits	is_binaryr   r   r   logits_to_probsA   s    
r+   c                 C   s    t | jj}| j|d| dS )N   )minmax)r   finfor   epsclamp)probsr0   r   r   r   clamp_probsM   s    r3   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.
    )r3   r   loglog1p)r2   r*   Z
ps_clampedr   r   r   probs_to_logitsR   s    r6   c                   @   s"   e Zd ZdZdd ZdddZdS )lazy_propertyz
    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                 C   s   || _ t| | d S r   )wrappedr   selfr8   r   r   r   __init__f   s    zlazy_property.__init__Nc              	   C   sD   |d krt | jS t  | |}W 5 Q R X t|| jj| |S r   )_lazy_property_and_propertyr8   r   Zenable_gradsetattr__name__)r:   instanceZobj_typer   r   r   r   __get__j   s    

zlazy_property.__get__)N)r>   
__module____qualname____doc__r;   r@   r   r   r   r   r7   _   s   r7   c                   @   s   e Zd ZdZdd ZdS )r<   zWe want lazy properties to look like multiple things.

    * property when Sphinx autodoc looks
    * lazy_property when Distribution validate_args looks
    c                 C   s   t | |S r   )propertyr;   r9   r   r   r   r;   y   s    z$_lazy_property_and_property.__init__N)r>   rA   rB   rC   r;   r   r   r   r   r<   s   s   r<   c                 C   s   | j d }tj sH|| k s&||krHt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.
    r%   zdiag (z) provided is outside [z, r,   z].r   .)r#   r   r   r   r   aranger   view)matdiagnrF   	tril_maskvecr   r   r   tril_matrix_to_vec}   s    
"rM   c                 C   s  dd|   dd|  d d| j d   dt| |d   d  d }t| jj}tj st|| |krt	d| j d  dd	 t
|tjrt| 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,         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.NrE   .)r#   absr   r/   r   r0   r   r   roundr   r	   r   longZ	new_zerosSizerF   r   rG   )rL   rI   rJ   r0   rH   rF   rK   r   r   r   vec_to_tril_matrix   s    H""rU   )F)F)r   )r   )	functoolsr   Znumbersr   r   Ztorch.nn.functionalnnZ
functionalr)   typingr   r   Ztorch.overridesr   Zeuler_constantr   r$   r(   r+   r3   r6   r7   rD   r<   rM   rU   r   r   r   r   <module>   s     



