
    IЦi                         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/rS r " S	 S
\5      r " S S\5      rg)    N)Function)once_differentiable)constraints)ExponentialFamily)_size	Dirichletc                     UR                  SS5      R                  U5      n[        R                  " XU5      nXBX-  R                  SS5      -
  -  $ NT)sum	expand_astorch_dirichlet_grad)xconcentrationgrad_outputtotalgrads        \/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/distributions/dirichlet.py_Dirichlet_backwardr      sN    b$'11-@E  59D!/!6!6r4!@@AA    c                   >    \ rS rSr\S 5       r\\S 5       5       rSrg)
_Dirichlet   c                 T    [         R                  " U5      nU R                  X!5        U$ N)r   _sample_dirichletsave_for_backward)ctxr   r   s      r   forward_Dirichlet.forward   s%    ##M2a/r   c                 6    U R                   u  p#[        X#U5      $ r   )saved_tensorsr   )r   r   r   r   s       r   backward_Dirichlet.backward   s     ,,"1[AAr    N)	__name__
__module____qualname____firstlineno__staticmethodr    r   r$   __static_attributes__r&   r   r   r   r      s2     
 B  Br   r   c                     ^  \ rS rSrSrS\R                  " \R                  S5      0r\R                  r
SrSU 4S jjrSU 4S jjrSS\S	\R                   4S
 jjrS r\S 5       r\S 5       r\S 5       rS r\S 5       rS rSrU =r$ )r   "   a  
Creates a Dirichlet distribution parameterized by concentration :attr:`concentration`.

Example::

    >>> # xdoctest: +IGNORE_WANT("non-deterministic")
    >>> m = Dirichlet(torch.tensor([0.5, 0.5]))
    >>> m.sample()  # Dirichlet distributed with concentration [0.5, 0.5]
    tensor([ 0.1046,  0.8954])

Args:
    concentration (Tensor): concentration parameter of the distribution
        (often referred to as alpha)
r      Tc                    > UR                  5       S:  a  [        S5      eXl        UR                  S S UR                  SS  pC[        TU ]  X4US9  g )Nr/   z;`concentration` parameter must be at least one-dimensional.r   validate_args)dim
ValueErrorr   shapesuper__init__)selfr   r2   batch_shapeevent_shape	__class__s        r   r7   Dirichlet.__init__7   sa    "M  +#0#6#6s#;]=P=PQSQT=U[Or   c                   > U R                  [        U5      n[        R                  " U5      nU R                  R                  XR                  -   5      Ul        [        [        U]#  XR                  SS9  U R                  Ul	        U$ )NFr1   )
_get_checked_instancer   r   Sizer   expandr:   r6   r7   _validate_args)r8   r9   	_instancenewr;   s       r   r@   Dirichlet.expand@   sy    ((I>jj- ..55kDTDT6TUi&)) 	' 	
 "00
r   sample_shapereturnc                     U R                  U5      nU R                  R                  U5      n[        R	                  U5      $ r   )_extended_shaper   r@   r   apply)r8   rE   r5   r   s       r   rsampleDirichlet.rsampleJ   s9    $$\2**11%8..r   c                 l   U R                   (       a  U R                  U5        [        R                  " U R                  S-
  U5      R                  S5      [        R                  " U R                  R                  S5      5      -   [        R                  " U R                  5      R                  S5      -
  $ )N      ?r   )rA   _validate_sampler   xlogyr   r   lgamma)r8   values     r   log_probDirichlet.log_probO   s    !!%(KK**S0%8<<R@ll4--11"567ll4--.22267	
r   c                 T    U R                   U R                   R                  SS5      -  $ r
   )r   r   r8   s    r   meanDirichlet.meanX   s&    !!D$6$6$:$:2t$DDDr   c                 L   U R                   S-
  R                  SS9nXR                  SS5      -  nU R                   S:  R                  SS9n[        R
                  R                  R                  X#   R                  SS9UR                  S   5      R                  U5      X#'   U$ )Nr/   g        )minr   T)axis)r   clampr   allr   nn
functionalone_hotargmaxr5   to)r8   concentrationm1modemasks       r   rc   Dirichlet.mode\   s    --188S8A!4!4R!>>""Q&+++4XX((00J2&(=(=b(A

"T( 	
 r   c                     U R                   R                  SS5      nU R                   XR                   -
  -  UR                  S5      US-   -  -  $ )Nr   T   r/   )r   r   pow)r8   con0s     r   varianceDirichlet.variancef   sR    !!%%b$/(((*xx{dQh')	
r   c                    U R                   R                  S5      nU R                   R                  S5      n[        R                  " U R                   5      R                  S5      [        R                  " U5      -
  X-
  [        R
                  " U5      -  -
  U R                   S-
  [        R
                  " U R                   5      -  R                  S5      -
  $ )Nr   rM   )r   sizer   r   rP   digamma)r8   ka0s      r   entropyDirichlet.entropyo   s    ##B'##B'LL++,004ll2vr**+ ""S(EMM$:L:L,MMRRSUVW	
r   c                     U R                   4$ r   r   rU   s    r   _natural_paramsDirichlet._natural_paramsy   s    ""$$r   c                     UR                  5       R                  S5      [        R                   " UR                  S5      5      -
  $ )Nr   )rP   r   r   )r8   r   s     r   _log_normalizerDirichlet._log_normalizer}   s-    xxz~~b!ELLr$;;;r   rt   r   )r&   )r'   r(   r)   r*   __doc__r   independentpositivearg_constraintssimplexsupporthas_rsampler7   r@   r   r   TensorrJ   rR   propertyrV   rc   rj   rq   ru   rx   r,   __classcell__)r;   s   @r   r   r   "   s     	001E1EqIO !!GKP/E /5<< /

 E E   
 

 % %< <r   )r   torch.autogradr   torch.autograd.functionr   torch.distributionsr   torch.distributions.exp_familyr   torch.typesr   __all__r   r   r   r&   r   r   <module>r      sC     # 7 + <  -BB B\<! \<r   