
    &Vji	                         d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d d	lmZmZ d d
lmZ ddgZ G d de	          Z G d de          ZdS )    )OptionalN)Tensor)constraints)Categorical)Distribution)TransformedDistribution)ExpTransform)broadcast_allclamp_probs)_sizeExpRelaxedCategoricalRelaxedOneHotCategoricalc                   8    e Zd ZdZej        ej        dZej        ZdZ		 	 	 dde
dee
         dee
         dee         d	df
 fd
Zd fd	Zd Zed	ej        fd            Zed	e
fd            Zed	e
fd            Z ej                    fded	e
fdZd Z xZS )r   a  
    Creates a ExpRelaxedCategorical parameterized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits` (but not both).
    Returns the log of a point in the simplex. Based on the interface to
    :class:`OneHotCategorical`.

    Implementation based on [1].

    See also: :func:`torch.distributions.OneHotCategorical`

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event

    [1] The Concrete Distribution: A Continuous Relaxation of Discrete Random Variables
    (Maddison et al., 2017)

    [2] Categorical Reparametrization with Gumbel-Softmax
    (Jang et al., 2017)
    probslogitsTNtemperaturer   r   validate_argsreturnc                     t          ||          | _        || _        | j        j        }| j        j        dd          }t                                          |||           d S )Nr   )r   _categoricalr   batch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__s          a/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/distributions/relaxed_categorical.pyr   zExpRelaxedCategorical.__init__/   sc     (v66&'3'3BCC8kOOOOO    c                 4   |                      t          |          }t          j        |          }| j        |_        | j                            |          |_        t          t          |                              || j	        d           | j
        |_
        |S )NFr   )_get_checked_instancer   torchSizer   r   expandr   r   r   _validate_argsr   r   	_instancenewr    s       r!   r'   zExpRelaxedCategorical.expand<   s    (()>	JJj--*,33K@@#S))22) 	3 	
 	
 	
 "0
r"   c                 &     | j         j        |i |S N)r   _new)r   argskwargss      r!   r.   zExpRelaxedCategorical._newG   s    %t %t6v666r"   c                     | j         j        S r-   )r   r   r   s    r!   r   z!ExpRelaxedCategorical.param_shapeJ   s     ,,r"   c                     | j         j        S r-   )r   r   r2   s    r!   r   zExpRelaxedCategorical.logitsN   s     ''r"   c                     | j         j        S r-   )r   r   r2   s    r!   r   zExpRelaxedCategorical.probsR   s     &&r"   sample_shapec                 D   |                      |          }t          t          j        || j        j        | j        j                            }|                                                                  }| j        |z   | j        z  }||	                    dd          z
  S )N)dtypedevicer   Tdimkeepdim)
_extended_shaper   r%   randr   r7   r8   logr   	logsumexp)r   r5   shapeuniformsgumbelsscoress         r!   rsamplezExpRelaxedCategorical.rsampleV   s    $$\22JuDK$5dk>PQQQ
 
  ||~~&++--.+'4+;;((R(>>>>r"   c                    | j         j        }| j        r|                     |           t	          | j        |          \  }}t          j        | j        t          |                    
                                | j                                                            |dz
             z
  }||                    | j                  z
  }||                    dd          z
                      d          }||z   S )N   r   Tr9   )r   _num_eventsr(   _validate_sampler
   r   r%   	full_liker   floatlgammar>   mulr?   sum)r   valueKr   	log_scalescores         r!   log_probzExpRelaxedCategorical.log_prob_   s    ) 	)!!%(((%dk599OeAhh
 

&((T%))++//!a%99:	 4#3444R>>>CCBGGy  r"   NNNr-   )__name__
__module____qualname____doc__r   simplexreal_vectorarg_constraintssupporthas_rsampler   r   boolr   r'   r.   propertyr%   r&   r   r   r   r   rD   rR   __classcell__r    s   @r!   r   r      s        , !, 3{?VWWO  K
 #'#'(,P PP P  	P
  ~P 
P P P P P P	 	 	 	 	 	7 7 7 -UZ - - - X- ( ( ( ( X( 'v ' ' ' X' -7EJLL ? ?E ?V ? ? ? ?
! 
! 
! 
! 
! 
! 
!r"   c                        e Zd ZU dZej        ej        dZej        ZdZ	e
ed<   	 	 	 ddedee         dee         d	ee         d
df
 fdZd fd	Zed
efd            Zed
efd            Zed
efd            Z xZS )r   a  
    Creates a RelaxedOneHotCategorical distribution parametrized by
    :attr:`temperature`, and either :attr:`probs` or :attr:`logits`.
    This is a relaxed version of the :class:`OneHotCategorical` distribution, so
    its samples are on simplex, and are reparametrizable.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = RelaxedOneHotCategorical(torch.tensor([2.2]),
        ...                              torch.tensor([0.1, 0.2, 0.3, 0.4]))
        >>> m.sample()
        tensor([ 0.1294,  0.2324,  0.3859,  0.2523])

    Args:
        temperature (Tensor): relaxation temperature
        probs (Tensor): event probabilities
        logits (Tensor): unnormalized log probability for each event
    r   T	base_distNr   r   r   r   r   c                     t          ||||          }t                                          |t                      |           d S )Nr   )r   r   r   r	   )r   r   r   r   r   rb   r    s         r!   r   z!RelaxedOneHotCategorical.__init__   sM     *m
 
 
	 	LNN-PPPPPr"   c                     |                      t          |          }t                                          ||          S )N)r*   )r$   r   r   r'   r)   s       r!   r'   zRelaxedOneHotCategorical.expand   s3    (()A9MMww~~kS~999r"   c                     | j         j        S r-   )rb   r   r2   s    r!   r   z$RelaxedOneHotCategorical.temperature   s    ~))r"   c                     | j         j        S r-   )rb   r   r2   s    r!   r   zRelaxedOneHotCategorical.logits   s    ~$$r"   c                     | j         j        S r-   )rb   r   r2   s    r!   r   zRelaxedOneHotCategorical.probs   s    ~##r"   rS   r-   )rT   rU   rV   rW   r   rX   rY   rZ   r[   r\   r   __annotations__r   r   r]   r   r'   r^   r   r   r   r_   r`   s   @r!   r   r   l   sd         ( !, 3{?VWWO!GK$$$$
 #'#'(,
Q 
Q
Q 
Q  	
Q
  ~
Q 

Q 
Q 
Q 
Q 
Q 
Q: : : : : : *V * * * X* % % % % X% $v $ $ $ X$ $ $ $ $r"   )typingr   r%   r   torch.distributionsr   torch.distributions.categoricalr    torch.distributions.distributionr   ,torch.distributions.transformed_distributionr   torch.distributions.transformsr	   torch.distributions.utilsr
   r   torch.typesr   __all__r   r    r"   r!   <module>rs      s+                + + + + + + 7 7 7 7 7 7 9 9 9 9 9 9 P P P P P P 7 7 7 7 7 7 @ @ @ @ @ @ @ @       #$>
?W! W! W! W! W!L W! W! W!t4$ 4$ 4$ 4$ 4$6 4$ 4$ 4$ 4$ 4$r"   