3
O4W^õ  ã               @   st   d dl mZ d dlmZ d dlZd dljj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eƒZdS )é    )Úupdate_wrapper)ÚNumberNc                 s~   t dd„ | D ƒƒstdƒ‚t ttj| ƒƒstttjƒ d�‰ x(| D ] }tj|ƒr>t|j|jd�‰ P q>W ‡ fdd„| D ƒ} 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` or `torch.*Tensor`)

    Raises:
        ValueError: if any of the values is not a `numbers.Number` or
            `torch.*Tensor` instance
    c             s   s"   | ]}t j|ƒpt|tƒV  qd S )N)ÚtorchÚ	is_tensorÚ
isinstancer   )Ú.0Úv© r	   ú:/tmp/pip-build-e66j97b2/torch/torch/distributions/utils.pyú	<genexpr>   s    z broadcast_all.<locals>.<genexpr>zHInput arguments must all be instances of numbers.Number or torch.tensor.)Údtype)r   Údevicec                s(   g | ] }t j|ƒr|nt j|fˆ Ž‘qS r	   )r   r   Ztensor)r   r   )Úoptionsr	   r
   ú
<listcomp>   s   z!broadcast_all.<locals>.<listcomp>)
ÚallÚ
ValueErrorÚmapr   r   ÚdictZget_default_dtyper   r   Zbroadcast_tensors)ÚvaluesÚvaluer	   )r   r
   Úbroadcast_all   s    


r   c             C   sB   t jjƒ r.t jt j| ||d�t j| ||d�ƒS t j| ||d�jƒ S )N)r   r   )r   Z_CZ_get_tracing_stateZnormalÚzerosZonesÚemptyZnormal_)Úshaper   r   r	   r	   r
   Ú_standard_normal$   s    
r   c             C   s0   |dkr| S | j d| … d }| 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é   éÿÿÿÿ)r   r   )r   ZreshapeÚsum)r   ÚdimZrequired_shaper	   r	   r
   Ú_sum_rightmost,   s    r   Fc             C   s   |rt j| ƒ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   )r   ZsigmoidÚFZsoftmax)ZlogitsÚ	is_binaryr	   r	   r
   Úlogits_to_probs:   s    
r"   c             C   s    t j| jƒj}| j|d| d�S )Nr   )ÚminÚmax)r   Zfinfor   ÚepsÚclamp)Úprobsr%   r	   r	   r
   Úclamp_probsF   s    r(   c             C   s,   t | ƒ}|r"tj|ƒtj| ƒ 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)r'   r!   Z
ps_clampedr	   r	   r
   Úprobs_to_logitsK   s    r+   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 )N)Úwrappedr   )Úselfr-   r	   r	   r
   Ú__init___   s    zlazy_property.__init__Nc             C   s>   |d kr| S t jƒ � | j|ƒ}W d Q R X t|| jj|ƒ |S )N)r   Zenable_gradr-   ÚsetattrÚ__name__)r.   ÚinstanceZobj_typer   r	   r	   r
   Ú__get__c   s    
zlazy_property.__get__)N)r1   Ú
__module__Ú__qualname__Ú__doc__r/   r3   r	   r	   r	   r
   r,   X   s   r,   )F)F)Ú	functoolsr   Znumbersr   r   Ztorch.nn.functionalÚnnZ
functionalr    r   r   r   r"   r(   r+   Úobjectr,   r	   r	   r	   r
   Ú<module>   s   

