o
    +ôjÎ  ã                   @   sê   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	  m
Z d dlmZ dZg 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ejdedejfdd„Zd"dejdedejfdd „ZdS )#é    )Úupdate_wrapper)ÚNumber)ÚAnyÚDictN©Úis_tensor_likeg¶oüŒxâ?)Úbroadcast_allÚlogits_to_probsÚclamp_probsÚprobs_to_logitsÚlazy_propertyÚtril_matrix_to_vecÚvec_to_tril_matrixc                     sŠ   t dd„ | D ƒƒstdƒ‚t dd„ | D ƒƒs@tt ¡ d�‰ | D ]}t|tjƒr1t|j|jd�‰  nq‡ 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   úl/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/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i ˆ ¤Ž‘qS r   )r   ÚtorchÚtensorr   ©Úoptionsr   r   Ú
<listcomp>1   s    ÿz!broadcast_all.<locals>.<listcomp>)
ÚallÚ
ValueErrorÚdictr   Úget_default_dtyper   ÚTensorr   r   Úbroadcast_tensors)ÚvaluesÚvalueÚ
new_valuesr   r   r   r      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ÚzerosÚonesÚemptyÚnormal_)Úshaper   r   r   r   r   Ú_standard_normal8   s   
þr0   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)éÿÿÿÿr1   )r/   ÚreshapeÚsum)r&   ÚdimZrequired_shaper   r   r   Ú_sum_rightmostB   s   r5   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.
    r1   )r4   )r   ÚsigmoidÚFÚsoftmax)ÚlogitsÚ	is_binaryr   r   r   r	   P   s   
r	   c                 C   s    t  | j¡j}| j|d| d�S )Né   )ÚminÚmax)r   Úfinfor   ÚepsÚclamp)Úprobsr?   r   r   r   r
   \   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)rA   r:   Z
ps_clampedr   r   r   r   a   s   
r   c                   @   s"   e Zd ZdZdd„ Zd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                 C   s   || _ t| |ƒ d S r   )Úwrappedr   ©ÚselfrD   r   r   r   Ú__init__v   s   zlazy_property.__init__Nc                 C   sX   |d u r	t | jƒS t ¡ � |  |¡}W d   ƒ n1 sw   Y  t|| jj|ƒ |S r   )Ú_lazy_property_and_propertyrD   r   Úenable_gradÚsetattrÚ__name__)rF   ÚinstanceZobj_typer&   r   r   r   Ú__get__z   s   

ÿzlazy_property.__get__r   )rK   Ú
__module__Ú__qualname__Ú__doc__rG   rM   r   r   r   r   r   n   s    r   c                   @   s   e Zd ZdZdd„ ZdS )rH   zžWe want lazy properties to look like multiple things.

    * property when Sphinx autodoc looks
    * lazy_property when Distribution validate_args looks
    c                 C   s   t  | |¡ d S r   )ÚpropertyrG   rE   r   r   r   rG   Š   s   z$_lazy_property_and_property.__init__N)rK   rN   rO   rP   rG   r   r   r   r   rH   ƒ   s    rH   ÚmatÚdiagÚreturnc                 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.
    r1   zdiag (z) provided is outside [z, r;   z].©r   .)r/   r   r(   r)   r    Úaranger   Úview)rR   rS   ÚnrV   Ú	tril_maskÚvecr   r   r   r   Ž   s   
"r   rZ   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;   é   é   r1   é   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.NrU   .)r/   Úabsr   r>   r   r?   r(   r)   Úroundr    r   r#   ÚitemÚ	new_zerosÚSizerV   r   rW   )rZ   rS   rX   r?   rR   rV   rY   r   r   r   r   œ   s$   4ÿýÿÿ "r   )F)r   )Ú	functoolsr   Únumbersr   Útypingr   r   r   Útorch.nn.functionalÚnnÚ
functionalr7   Útorch.overridesr   Zeuler_constantÚ__all__r   r0   r5   r	   r
   r   r   rQ   rH   r#   Úintr   r   r   r   r   r   Ú<module>   s$    "


 