a
    ßñh!  ã                   @   sz   d dl mZ d dlmZ d dlZd dlm  m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 dd„ | D ƒƒsvtt ¡ d�‰ | D ]$}t|tjƒr>t|j|jd�‰  qdq>‡ 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 |tjƒpt |tƒV  qd S ©N)Ú
isinstanceÚtorchÚTensorr   ©Ú.0Úv© r   úY/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torch/distributions/utils.pyÚ	<genexpr>   ó    z broadcast_all.<locals>.<genexpr>zHInput arguments must all be instances of numbers.Number or torch.tensor.c                 S   s   g | ]}t |tjƒ‘qS r   )r   r   r   r   r   r   r   Ú
<listcomp>   r   z!broadcast_all.<locals>.<listcomp>)Údtype©r   Údevicec                    s.   g | ]&}t |tjƒr|ntj|fi ˆ ¤Ž‘qS r   )r   r   r   Ztensorr   ©Úoptionsr   r   r      s   ÿ)
ÚallÚ
ValueErrorÚdictr   Zget_default_dtyper   r   r   r   Zbroadcast_tensors)ÚvaluesÚvaluer   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   Z_CZ_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_rightmost,   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_probs:   s    
r&   c                 C   s    t  | j¡j}| j|d| d�S )Né   )ÚminÚmax)r   Zfinfor   ÚepsÚclamp)Úprobsr*   r   r   r   Úclamp_probsF   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   Úprobs_to_logitsK   s    r0   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   )Úselfr2   r   r   r   Ú__init___   s    zlazy_property.__init__Nc                 C   sR   |d u r| S t  ¡ � |  |¡}W d   ƒ n1 s40    Y  t|| jj|ƒ |S r   )r   Zenable_gradr2   ÚsetattrÚ__name__)r3   ÚinstanceZobj_typer   r   r   r   Ú__get__c   s    
(zlazy_property.__get__)N)r6   Ú
__module__Ú__qualname__Ú__doc__r4   r8   r   r   r   r   r1   X   s   r1   )F)F)Ú	functoolsr   Znumbersr   r   Ztorch.nn.functionalÚnnZ
functionalr$   r   r   r#   r&   r-   r0   Úobjectr1   r   r   r   r   Ú<module>   s   

