Ë
    P/æj›  ã                   óp   — 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gZ	 G d„ de«      Z
 G d	„ de
«      Zy)
é    N)Úconstraints)ÚCategorical)ÚDistribution)Ú_sizeÚOneHotCategoricalÚ OneHotCategoricalStraightThroughc                   ó0  ‡ — e Zd ZdZej
                  ej                  dœZej                  Z	dZ
dˆ fd„	Zdˆ fd„	Zd„ Zed„ «       Zed„ «       Zed	„ «       Zed
„ «       Zed„ «       Zed„ «       Zed„ «       Z ej.                  «       fd„Zd„ Zd„ Zdd„Zˆ xZS )r   aµ  
    Creates a one-hot categorical distribution parameterized by :attr:`probs` or
    :attr:`logits`.

    Samples are one-hot coded vectors of size ``probs.size(-1)``.

    .. note:: The `probs` argument must be non-negative, finite and have a non-zero sum,
              and it will be normalized to sum to 1 along the last dimension. :attr:`probs`
              will return this normalized value.
              The `logits` argument will be interpreted as unnormalized log probabilities
              and can therefore be any real number. It will likewise be normalized so that
              the resulting probabilities sum to 1 along the last dimension. :attr:`logits`
              will return this normalized value.

    See also: :func:`torch.distributions.Categorical` for specifications of
    :attr:`probs` and :attr:`logits`.

    Example::

        >>> # xdoctest: +IGNORE_WANT("non-deterministic")
        >>> m = OneHotCategorical(torch.tensor([ 0.25, 0.25, 0.25, 0.25 ]))
        >>> m.sample()  # equal probability of 0, 1, 2, 3
        tensor([ 0.,  0.,  0.,  1.])

    Args:
        probs (Tensor): event probabilities
        logits (Tensor): event log probabilities (unnormalized)
    )ÚprobsÚlogitsTc                 óª   •— t        ||«      | _        | j                  j                  }| j                  j                  dd  }t        ‰| �  |||¬«       y )Néÿÿÿÿ©Úvalidate_args)r   Ú_categoricalÚbatch_shapeÚparam_shapeÚsuperÚ__init__)Úselfr
   r   r   r   Úevent_shapeÚ	__class__s         €új/root/repos/pocs/video-evals/.venv/lib/python3.12/site-packages/torch/distributions/one_hot_categorical.pyr   zOneHotCategorical.__init__-   sO   ø€ Ü'¨¨vÓ6ˆÔØ×'Ñ'×3Ñ3ˆØ×'Ñ'×3Ñ3°B°CÐ8ˆÜ‰Ñ˜ kÀÐÕOó    c                 ó   •— | j                  t        |«      }t        j                  |«      }| j                  j                  |«      |_        t        t        |�  || j                  d¬«       | j                  |_	        |S )NFr   )
Ú_get_checked_instancer   ÚtorchÚSizer   Úexpandr   r   r   Ú_validate_args)r   r   Ú	_instanceÚnewr   s       €r   r   zOneHotCategorical.expand3   st   ø€ Ø×(Ñ(Ô):¸IÓFˆÜ—j‘j Ó-ˆØ×,Ñ,×3Ñ3°KÓ@ˆÔÜÔ Ñ.Ø˜×)Ñ)¸ð 	/ô 	
ð "×0Ñ0ˆÔØˆ
r   c                 ó:   —  | j                   j                  |i |¤ŽS ©N)r   Ú_new)r   ÚargsÚkwargss      r   r$   zOneHotCategorical._new=   s    € Ø%ˆt× Ñ ×%Ñ% tÐ6¨vÑ6Ð6r   c                 ó.   — | j                   j                  S r#   )r   Ú_param©r   s    r   r(   zOneHotCategorical._param@   ó   € à× Ñ ×'Ñ'Ð'r   c                 ó.   — | j                   j                  S r#   ©r   r
   r)   s    r   r
   zOneHotCategorical.probsD   ó   € à× Ñ ×&Ñ&Ð&r   c                 ó.   — | j                   j                  S r#   )r   r   r)   s    r   r   zOneHotCategorical.logitsH   r*   r   c                 ó.   — | j                   j                  S r#   r,   r)   s    r   ÚmeanzOneHotCategorical.meanL   r-   r   c                 óà   — | j                   j                  }|j                  d¬«      }t        j                  j
                  j                  ||j                  d   ¬«      j                  |«      S )Nr   )Úaxis)Únum_classes)	r   r
   Úargmaxr   ÚnnÚ
functionalÚone_hotÚshapeÚto)r   r
   Úmodes      r   r:   zOneHotCategorical.modeP   sW   € à×!Ñ!×'Ñ'ˆØ�|‰| ˆ|Ó$ˆÜ�x‰x×"Ñ"×*Ñ*¨4¸U¿[¹[È¹_Ð*ÓM×PÑPÐQVÓWÐWr   c                 ób   — | j                   j                  d| j                   j                  z
  z  S )Né   r,   r)   s    r   ÚvariancezOneHotCategorical.varianceV   s*   € à× Ñ ×&Ñ&¨!¨d×.?Ñ.?×.EÑ.EÑ*EÑFÐFr   c                 ó.   — | j                   j                  S r#   )r   r   r)   s    r   r   zOneHotCategorical.param_shapeZ   s   € à× Ñ ×,Ñ,Ð,r   c                 ó,  — t        j                  |«      }| j                  j                  }| j                  j                  }| j                  j                  |«      }t         j                  j                  j                  ||«      j                  |«      S r#   )
r   r   r   r
   Ú_num_eventsÚsampler5   r6   r7   r9   )r   Úsample_shaper
   Ú
num_eventsÚindicess        r   rA   zOneHotCategorical.sample^   sp   € Ü—z‘z ,Ó/ˆØ×!Ñ!×'Ñ'ˆØ×&Ñ&×2Ñ2ˆ
Ø×#Ñ#×*Ñ*¨<Ó8ˆÜ�x‰x×"Ñ"×*Ñ*¨7°JÓ?×BÑBÀ5ÓIÐIr   c                 óš   — | j                   r| j                  |«       |j                  d«      d   }| j                  j	                  |«      S )Nr   r<   )r   Ú_validate_sampleÚmaxr   Úlog_prob)r   ÚvaluerD   s      r   rH   zOneHotCategorical.log_probe   sB   € Ø×ÒØ×!Ñ! %Ô(Ø—)‘)˜B“- Ñ"ˆØ× Ñ ×)Ñ)¨'Ó2Ð2r   c                 ó6   — | j                   j                  «       S r#   )r   Úentropyr)   s    r   rK   zOneHotCategorical.entropyk   s   € Ø× Ñ ×(Ñ(Ó*Ð*r   c                 óL  — | j                   d   }t        j                  || j                  j                  | j                  j
                  ¬«      }|j                  |fdt        | j                  «      z  z   |fz   «      }|r#|j                  |f| j                  z   |fz   «      }|S )Nr   )ÚdtypeÚdevice)r<   )
r   r   Úeyer(   rM   rN   ÚviewÚlenr   r   )r   r   ÚnÚvaluess       r   Úenumerate_supportz#OneHotCategorical.enumerate_supportn   sŽ   € Ø×Ñ˜QÑˆÜ—‘˜1 D§K¡K×$5Ñ$5¸d¿k¹k×>PÑ>PÔQˆØ—‘˜a˜T D¬3¨t×/?Ñ/?Ó+@Ñ$@Ñ@ÀAÀ4ÑGÓHˆÙØ—]‘] A 4¨$×*:Ñ*:Ñ#:¸a¸TÑ#AÓBˆFØˆr   )NNNr#   )T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚsimplexÚreal_vectorÚarg_constraintsr7   ÚsupportÚhas_enumerate_supportr   r   r$   Úpropertyr(   r
   r   r0   r:   r=   r   r   r   rA   rH   rK   rT   Ú__classcell__)r   s   @r   r   r      só   ø„ ñð8 !,× 3Ñ 3¸{×?VÑ?VÑW€OØ×!Ñ!€GØ ÐõPõò7ð ñ(ó ð(ð ñ'ó ð'ð ñ(ó ð(ð ñ'ó ð'ð ñXó ðXð
 ñGó ðGð ñ-ó ð-ð #- %§*¡*£,ó Jò3ò+÷r   c                   óZ   — e Zd ZdZdZ ej                  «       fdedej                  fd„Z	y)r   a  
    Creates a reparameterizable :class:`OneHotCategorical` distribution based on the straight-
    through gradient estimator from [1].

    [1] Estimating or Propagating Gradients Through Stochastic Neurons for Conditional Computation
    (Bengio et al., 2013)
    TrB   Úreturnc                 ó|   — | j                  |«      }| j                  j                  }|||j                  «       z
  z   S r#   )rA   r   r
   Údetach)r   rB   Úsamplesr
   s       r   Úrsamplez(OneHotCategoricalStraightThrough.rsample�   s7   € Ø—+‘+˜lÓ+ˆØ×!Ñ!×'Ñ'ˆØ˜% %§,¡,£.Ñ0Ñ1Ð1r   N)
rU   rV   rW   rX   Úhas_rsampler   r   r   ÚTensorre   © r   r   r   r   w   s0   „ ñð €Kà,6¨E¯J©J«Lñ 2 Eð 2¸U¿\¹\ô 2r   )r   Útorch.distributionsr   Útorch.distributions.categoricalr   Ú torch.distributions.distributionr   Útorch.typesr   Ú__all__r   r   rh   r   r   ú<module>rn      s=   ðã Ý +Ý 7Ý 9Ý ð Ð BÐ
C€ôh˜ô hôV2Ð'8õ 2r   