o
    +ôj$0  ã                   @   sd   d dl Z d dlmZmZmZmZ d dlZd dlmZ d dl	m
Z
 d dlmZ dgZG dd„ dƒZdS )é    N)ÚAnyÚDictÚOptionalÚTuple)Úconstraints)Úlazy_property)Ú_sizeÚDistributionc                       s:  e Zd ZdZdZdZdZededdfdd„ƒZ	e
 ¡ e
 ¡ dfd	e
jd
e
jdee f‡ fdd„Zd>d	e
jfdd„Zede
jfdd„ƒZede
jfdd„ƒZedeeejf fdd„ƒZedee fdd„ƒZede
jfdd„ƒZede
jfdd„ƒZede
jfdd„ƒZede
jfdd„ƒZe
 ¡ fd e
jde
jfd!d"„Ze
 ¡ fd e
jde
jfd#d$„Zd%e de
jfd&d'„Z!de
jde
jfd(d)„Z"de
jde
jfd*d+„Z#de
jde
jfd,d-„Z$d?d.ede
jfd/d0„Z%de
jfd1d2„Z&de
jfd3d4„Z'e
 ¡ fd e(de)e d5f fd6d7„Z*de
jddfd8d9„Z+d>d:d;„Z,defd<d=„Z-‡  Z.S )@r	   zP
    Distribution is the abstract base class for probability distributions.
    FTÚvalueÚreturnNc                 C   s   | dvrt ‚| t_dS )a�  
        Sets whether validation is enabled or disabled.

        The default behavior mimics Python's ``assert`` statement: validation
        is on by default, but is disabled if Python is run in optimized mode
        (via ``python -O``). Validation may be expensive, so you may want to
        disable it once a model is working.

        Args:
            value (bool): Whether to enable validation.
        )TFN)Ú
ValueErrorr	   Ú_validate_args)r
   © r   ús/var/www/html/academy/tools/voices/clone/cosy-venv/lib/python3.10/site-packages/torch/distributions/distribution.pyÚset_default_validate_args   s   
z&Distribution.set_default_validate_argsÚbatch_shapeÚevent_shapeÚvalidate_argsc           	         s  || _ || _|d ur|| _| jr{z| j}W n ty-   i }t | j› d�d d ¡ Y nw | ¡ D ]H\}}t	 
|¡r<q2|| jvrLttt| ƒ|ƒtƒrLq2t| |ƒ}| |¡}| ¡ sztd|› dt|ƒj› dt|jƒ› dt| ƒ› dt|ƒ› d	|› �ƒ‚q2tƒ  ¡  d S )
Nz$ does not define `arg_constraints`. zAPlease set `arg_constraints = {}` or initialize the distribution z2with `validate_args=False` to turn off validation.zExpected parameter z (ú
 of shape z) of distribution z to satisfy the constraint ú, but found invalid values:
)Ú_batch_shapeÚ_event_shaper   Úarg_constraintsÚNotImplementedErrorÚwarningsÚwarnÚ	__class__Úitemsr   Úis_dependentÚ__dict__Ú
isinstanceÚgetattrÚtyper   ÚcheckÚallr   Ú__name__ÚtupleÚshapeÚreprÚsuperÚ__init__)	Úselfr   r   r   r   ÚparamÚ
constraintr
   Úvalid©r   r   r   r*   &   sT   

ÿþÿþ
ÿ

ÿÿþýüÿÿzDistribution.__init__c                 C   ó   t ‚)a/  
        Returns a new distribution instance (or populates an existing instance
        provided by a derived class) with batch dimensions expanded to
        `batch_shape`. This method calls :class:`~torch.Tensor.expand` on
        the distribution's parameters. As such, this does not allocate new
        memory for the expanded distribution instance. Additionally,
        this does not repeat any args checking or parameter broadcasting in
        `__init__.py`, when an instance is first created.

        Args:
            batch_shape (torch.Size): the desired expanded size.
            _instance: new instance provided by subclasses that
                need to override `.expand`.

        Returns:
            New distribution instance with batch dimensions expanded to
            `batch_size`.
        ©r   )r+   r   Ú	_instancer   r   r   ÚexpandM   s   zDistribution.expandc                 C   ó   | j S )zF
        Returns the shape over which parameters are batched.
        )r   ©r+   r   r   r   r   b   ó   zDistribution.batch_shapec                 C   r4   )zJ
        Returns the shape of a single sample (without batching).
        )r   r5   r   r   r   r   i   r6   zDistribution.event_shapec                 C   r0   )a
  
        Returns a dictionary from argument names to
        :class:`~torch.distributions.constraints.Constraint` objects that
        should be satisfied by each argument of this distribution. Args that
        are not tensors need not appear in this dict.
        r1   r5   r   r   r   r   p   ó   zDistribution.arg_constraintsc                 C   r0   )z‰
        Returns a :class:`~torch.distributions.constraints.Constraint` object
        representing this distribution's support.
        r1   r5   r   r   r   Úsupportz   ó   zDistribution.supportc                 C   r0   )z7
        Returns the mean of the distribution.
        r1   r5   r   r   r   Úmean‚   ó   zDistribution.meanc                 C   s   t | j› d�ƒ‚)z7
        Returns the mode of the distribution.
        z does not implement mode)r   r   r5   r   r   r   Úmode‰   s   zDistribution.modec                 C   r0   )z;
        Returns the variance of the distribution.
        r1   r5   r   r   r   Úvariance�   r;   zDistribution.variancec                 C   s
   | j  ¡ S )zE
        Returns the standard deviation of the distribution.
        )r=   Úsqrtr5   r   r   r   Ústddev—   s   
zDistribution.stddevÚsample_shapec                 C   s6   t  ¡ � |  |¡W  d  ƒ S 1 sw   Y  dS )z”
        Generates a sample_shape shaped sample or sample_shape shaped batch of
        samples if the distribution parameters are batched.
        N)ÚtorchÚno_gradÚrsample©r+   r@   r   r   r   Úsamplež   s   
$ÿzDistribution.samplec                 C   r0   )z¼
        Generates a sample_shape shaped reparameterized sample or sample_shape
        shaped batch of reparameterized samples if the distribution parameters
        are batched.
        r1   rD   r   r   r   rC   ¦   r9   zDistribution.rsampleÚnc                 C   s   t  dt¡ |  t |f¡¡S )zq
        Generates n samples or n batches of samples if the distribution
        parameters are batched.
        z6sample_n will be deprecated. Use .sample((n,)) instead)r   r   ÚUserWarningrE   rA   ÚSize)r+   rF   r   r   r   Úsample_n®   s   ÿzDistribution.sample_nc                 C   r0   )z“
        Returns the log of the probability density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        r1   ©r+   r
   r   r   r   Úlog_prob¸   r7   zDistribution.log_probc                 C   r0   )z‡
        Returns the cumulative density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        r1   rJ   r   r   r   ÚcdfÂ   r7   zDistribution.cdfc                 C   r0   )z�
        Returns the inverse cumulative density/mass function evaluated at
        `value`.

        Args:
            value (Tensor):
        r1   rJ   r   r   r   ÚicdfÌ   r7   zDistribution.icdfr3   c                 C   r0   )ar  
        Returns tensor containing all values supported by a discrete
        distribution. The result will enumerate over dimension 0, so the shape
        of the result will be `(cardinality,) + batch_shape + event_shape`
        (where `event_shape = ()` for univariate distributions).

        Note that this enumerates over all batched tensors in lock-step
        `[[0, 0], [1, 1], ...]`. With `expand=False`, enumeration happens
        along dim 0, but with the remaining batch dimensions being
        singleton dimensions, `[[0], [1], ..`.

        To iterate over the full Cartesian product use
        `itertools.product(m.enumerate_support())`.

        Args:
            expand (bool): whether to expand the support over the
                batch dims to match the distribution's `batch_shape`.

        Returns:
            Tensor iterating over dimension 0.
        r1   )r+   r3   r   r   r   Úenumerate_supportÖ   s   zDistribution.enumerate_supportc                 C   r0   )z‡
        Returns entropy of distribution, batched over batch_shape.

        Returns:
            Tensor of shape batch_shape.
        r1   r5   r   r   r   Úentropyî   s   zDistribution.entropyc                 C   s   t  |  ¡ ¡S )zŠ
        Returns perplexity of distribution, batched over batch_shape.

        Returns:
            Tensor of shape batch_shape.
        )rA   ÚexprO   r5   r   r   r   Ú
perplexity÷   s   zDistribution.perplexity.c                 C   s,   t |tjƒst |¡}t || j | j ¡S )ax  
        Returns the size of the sample returned by the distribution, given
        a `sample_shape`. Note, that the batch and event shapes of a distribution
        instance are fixed at the time of construction. If this is empty, the
        returned shape is upcast to (1,).

        Args:
            sample_shape (torch.Size): the size of the sample to be drawn.
        )r    rA   rH   r   r   rD   r   r   r   Ú_extended_shape   s   

zDistribution._extended_shapec           	      C   sP  t |tjƒs
tdƒ‚t| ¡ ƒt| jƒ }| ¡ |d… | jkr.td| ¡ › d| j› d�ƒ‚| ¡ }| j| j }tt	|ƒt	|ƒƒD ]\}}|dkr\|dkr\||kr\td|› d|› d�ƒ‚qAz| j
}W n tyy   t | j› d�d	 d
 ¡ Y dS w |dus€J ‚| |¡}| ¡ s¦tdt|ƒj› dt|jƒ› dt|ƒ› dt| ƒ› d|› �
ƒ‚dS )a  
        Argument validation for distribution methods such as `log_prob`,
        `cdf` and `icdf`. The rightmost dimensions of a value to be
        scored via these methods must agree with the distribution's batch
        and event shapes.

        Args:
            value (Tensor): the tensor whose log probability is to be
                computed by the `log_prob` method.
        Raises
            ValueError: when the rightmost dimensions of `value` do not match the
                distribution's batch and event shapes.
        z/The value argument to log_prob must be a TensorNz5The right-most size of value must match event_shape: z vs Ú.é   z9Value is not broadcastable with batch_shape+event_shape: z% does not define `support` to enable z;sample validation. Please initialize the distribution with z-`validate_args=False` to turn off validation.zExpected value argument (r   z) to be within the support (z) of the distribution r   )r    rA   ÚTensorr   ÚlenÚsizer   r   ÚzipÚreversedr8   r   r   r   r   r#   r$   r"   r%   r&   r'   r(   )	r+   r
   Zevent_dim_startZactual_shapeÚexpected_shapeÚiÚjr8   r.   r   r   r   Ú_validate_sample  sX   ÿÿ€

ÿþÿú
ÿÿþýüÿÿzDistribution._validate_samplec                 C   sN   |d u rt | ƒj|jkrtd| jj› d|j› d�ƒ‚|d u r%|  t | ƒ¡S |S )Nz	Subclass z of zR that defines a custom __init__ method must also define a custom .expand() method.)r"   r*   r   r   r%   Ú__new__)r+   Úclsr2   r   r   r   Ú_get_checked_instance@  s
   ÿz"Distribution._get_checked_instancec                    sD   ‡ fdd„ˆ j  ¡ D ƒ}d ‡ fdd„|D ƒ¡}ˆ jjd | d S )Nc                    s   g | ]\}}|ˆ j v r|‘qS r   )r   )Ú.0ÚkÚ_r5   r   r   Ú
<listcomp>I  s    z)Distribution.__repr__.<locals>.<listcomp>z, c                    s@   g | ]}|› d ˆ j |  ¡ dkrˆ j | nˆ j |  ¡ › �‘qS )z: rT   )r   ÚnumelrW   )ra   Úpr5   r   r   rd   K  s    2ÿÿú(ú))r   r   Újoinr   r%   )r+   Úparam_namesÚargs_stringr   r5   r   Ú__repr__H  s   
þÿzDistribution.__repr__)N)T)/r%   Ú
__module__Ú__qualname__Ú__doc__Zhas_rsampleÚhas_enumerate_supportr   ÚstaticmethodÚboolr   rA   rH   r   r*   r3   Úpropertyr   r   r   Ústrr   Ú
Constraintr   r   r8   rU   r:   r<   r=   r?   rE   rC   ÚintrI   rK   rL   rM   rN   rO   rQ   r   r   rR   r]   r`   rl   Ú__classcell__r   r   r/   r   r	      s^    üþýü'	



	"	
2)r   Útypingr   r   r   r   rA   Útorch.distributionsr   Útorch.distributions.utilsr   Útorch.typesr   Ú__all__r	   r   r   r   r   Ú<module>   s    