
    9i	                         S SK Jr  S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	  S SK
Jr  S SKJr  S S	KJrJr  S S
KJr  SS/r " S S\	5      r " S S\5      rg)    )OptionalN)Tensor)constraints)Categorical)Distribution)TransformedDistribution)ExpTransform)broadcast_allclamp_probs)_sizeExpRelaxedCategoricalRelaxedOneHotCategoricalc                   \  ^  \ rS rSrSr\R                  \R                  S.r\R                  r	Sr
   SS\S\\   S\\   S	\\   S
S4
U 4S jjjrSU 4S jjrS r\S
\R&                  4S j5       r\S
\4S j5       r\S
\4S j5       r\R&                  " 5       4S\S
\4S jjrS rSrU =r$ )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                    > [        X#5      U l        Xl        U R                  R                  nU R                  R                  SS  n[
        TU ]  XVUS9  g )Nr   )r   _categoricalr   batch_shapeparam_shapesuper__init__)selfr   r   r   r   r   event_shape	__class__s          g/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/distributions/relaxed_categorical.pyr   ExpRelaxedCategorical.__init__/   sS     (6&''33''33BC8O    c                   > U R                  [        U5      n[        R                  " U5      nU R                  Ul        U R
                  R                  U5      Ul        [        [        U]#  XR                  SS9  U R                  Ul
        U$ )NFr   )_get_checked_instancer   torchSizer   r   expandr   r   r    _validate_argsr   r   	_instancenewr!   s       r"   r)   ExpRelaxedCategorical.expand<   s    (()>	Jjj-**,,33K@#S2)) 	3 	
 "00
r$   c                 :    U R                   R                  " U0 UD6$ N)r   _new)r   argskwargss      r"   r1   ExpRelaxedCategorical._newG   s      %%t6v66r$   c                 .    U R                   R                  $ r0   )r   r   r   s    r"   r   !ExpRelaxedCategorical.param_shapeJ   s      ,,,r$   c                 .    U R                   R                  $ r0   )r   r   r6   s    r"   r   ExpRelaxedCategorical.logitsN   s      '''r$   c                 .    U R                   R                  $ r0   )r   r   r6   s    r"   r   ExpRelaxedCategorical.probsR   s      &&&r$   sample_shapec                 L   U R                  U5      n[        [        R                  " X R                  R
                  U R                  R                  S95      nUR                  5       * R                  5       * nU R                  U-   U R                  -  nXUR                  SSS9-
  $ )N)dtypedevicer   Tdimkeepdim)
_extended_shaper   r'   randr   r>   r?   logr   	logsumexp)r   r<   shapeuniformsgumbelsscoress         r"   rsampleExpRelaxedCategorical.rsampleV   s    $$\2JJuKK$5$5dkk>P>PQ
  ||~&++-.++'4+;+;;((R(>>>r$   c                    U R                   R                  nU R                  (       a  U R                  U5        [	        U R
                  U5      u  p1[        R                  " U R                  [        U5      5      R                  5       U R                  R                  5       R                  US-
  * 5      -
  nX1R                  U R                  5      -
  nXUR                  SSS9-
  R                  S5      nXT-   $ )N   r   Tr@   )r   _num_eventsr*   _validate_sampler
   r   r'   	full_liker   floatlgammarE   mulrF   sum)r   valueKr   	log_scalescores         r"   log_probExpRelaxedCategorical.log_prob_   s    ))!!%(%dkk59OOeAh

&(T%%))+//!a%9:	 4#3#344R>>CCBG  r$   )r   r   NNNr0   )__name__
__module____qualname____firstlineno____doc__r   simplexreal_vectorarg_constraintssupporthas_rsampler   r   boolr   r)   r1   propertyr'   r(   r   r   r   r   rK   rZ   __static_attributes____classcell__r!   s   @r"   r   r      s   , !, 3 3{?V?VWO  K
 #'#'(,PP P  	P
  ~P 
P P	7 -UZZ - - ( ( ( 'v ' ' -2JJL ?E ?V ?
! 
!r$   c                     ^  \ rS rSr% Sr\R                  \R                  S.r\R                  r	Sr
\\S'      SS\S\\   S	\\   S
\\   SS4
U 4S jjjrSU 4S jjr\S\4S j5       r\S\4S j5       r\S\4S j5       rSrU =r$ )r   l   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                 H   > [        XX4S9n[        TU ]	  U[        5       US9  g )Nr   )r   r   r   r	   )r   r   r   r   r   rn   r!   s         r"   r   !RelaxedOneHotCategorical.__init__   s,     *
	 	LN-Pr$   c                 J   > U R                  [        U5      n[        TU ]  XS9$ )N)r,   )r&   r   r   r)   r+   s       r"   r)   RelaxedOneHotCategorical.expand   s'    (()A9Mw~k~99r$   c                 .    U R                   R                  $ r0   )rn   r   r6   s    r"   r   $RelaxedOneHotCategorical.temperature   s    ~~)))r$   c                 .    U R                   R                  $ r0   )rn   r   r6   s    r"   r   RelaxedOneHotCategorical.logits   s    ~~$$$r$   c                 .    U R                   R                  $ r0   )rn   r   r6   s    r"   r   RelaxedOneHotCategorical.probs   s    ~~###r$    r\   r0   )r]   r^   r_   r`   ra   r   rb   rc   rd   re   rf   r   __annotations__r   r   rg   r   r)   rh   r   r   r   ri   rj   rk   s   @r"   r   r   l   s    ( !, 3 3{?V?VWO!!GK$$
 #'#'(,
Q
Q 
Q  	
Q
  ~
Q 

Q 
Q: *V * * % % % $v $ $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   ry   r$   r"   <module>r      sL       + 7 9 P 7 @  #$>
?W!L W!t4$6 4$r$   