
    Αi$                       S SK Jr  S SKrS SKrS SKJrJr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  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$  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.  \(       a  S SKJ/r/  \" S5      r0SS/r10 r2S2S jr3      S3S jr4S r5\Rl                   " S S5      5       r7\4" \
\
5      S  5       r8\4" \\5      S! 5       r9\4" \\5      S" 5       r:\4" \\5      S# 5       r;\4" \\5      S$ 5       r<\4" \\5      S% 5       r=\4" \\5      S& 5       r>\4" \(\(5      S' 5       r?\4" \&\&5      S( 5       r@\4" \,\,5      S) 5       rA\4" \"\"5      S* 5       rB\4" \ \ 5      S+ 5       rC\4" \\5      S, 5       rD\4" \\5      S- 5       rE\4" \\5      S. 5       rF\4" \$\$5      S/ 5       rG\4" \*\*5      S0 5       rHS1 rIg)4    )annotationsN)TYPE_CHECKINGCallableTypeVar)	Bernoulli)Beta)Binomial)Categorical)Cauchy)ContinuousBernoulli)	Dirichlet)Distribution)Exponential)ExponentialFamily)Gamma)	Geometric)Laplace)	LogNormal)MultivariateNormal)Normal)Poisson)Uniform)in_dynamic_mode)Tensor_Tregister_klkl_divergencec                J    [        [        U 5      [        U5      5      " X5      $ )a  
Kullback-Leibler divergence between distribution p and q.

.. math::

    KL(p||q) = \int p(x)log\frac{p(x)}{q(x)} \mathrm{d}x

Args:
    p (Distribution): ``Distribution`` object. Inherits from the Distribution Base class.
    q (Distribution): ``Distribution`` object. Inherits from the Distribution Base class.

Returns:
    Tensor, Batchwise KL-divergence between distribution p and q.

Examples:

    .. code-block:: python

        >>> import paddle

        >>> p = paddle.distribution.Beta(alpha=0.5, beta=0.5)
        >>> q = paddle.distribution.Beta(alpha=0.3, beta=0.7)

        >>> print(paddle.distribution.kl_divergence(p, q))
        Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
            0.21193528)

)	_dispatchtypepqs     V/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/distribution/kl.pyr   r   4   s    : T!Wd1g&q,,    c                   ^ ^ [        T [        5      (       a  [        T[        5      (       d  [        S5      eU U4S jnU$ )ah  Decorator for register a KL divergence implementation function.

The ``kl_divergence(p, q)`` function will search concrete implementation
functions registered by ``register_kl``, according to multi-dispatch pattern.
If an implementation function is found, it will return the result, otherwise,
it will raise ``NotImplementError`` exception. Users can register
implementation function by the decorator.

Args:
    cls_p (type[Distribution]): The Distribution type of Instance p. Subclass derived from ``Distribution``.
    cls_q (type[Distribution]): The Distribution type of Instance q. Subclass derived from ``Distribution``.

Examples:
    .. code-block:: python

        >>> import paddle

        >>> @paddle.distribution.register_kl(paddle.distribution.Beta, paddle.distribution.Beta)
        >>> def kl_beta_beta():
        ...     pass # insert implementation here
z0cls_p and cls_q must be subclass of Distributionc                   > U [         TT4'   U $ N)_REGISTER_TABLE)fcls_pcls_qs    r$   	decoratorregister_kl.<locals>.decoratorq   s    ()u%r%   )
issubclassr   	TypeError)r+   r,   r-   s   `` r$   r   r   T   s>    0 e\***|3 3 JKK r%   c                   [          VVs/ s H,  u  p#[        X5      (       d  M  [        X5      (       d  M)  X#4PM.     nnnU(       d  [        e[        S U 5       5      R                  u  pV[        S U 5       5      R                  u  px[         XV4   [         Xx4   LaP  [
        R                  " SU R                   SUR                   SUR                   SUR                   S3	[        5        [         XV4   $ s  snnf )z3Multiple dispatch into concrete implement function.c              3  2   #    U  H  n[        U6 v   M     g 7fr(   )_Compare.0ms     r$   	<genexpr>_dispatch.<locals>.<genexpr>   s     7w!1ws   c              3  D   #    U  H  n[        [        U5      6 v   M     g 7fr(   )r3   reversedr4   s     r$   r7   r8      s     C7a8Xa[17s    zAmbiguous kl_divergence(z, z). Please register_kl())	r)   r/   NotImplementedErrorminclasseswarningswarn__name__RuntimeWarning)	r+   r,   super_psuper_qmatchesleft_pleft_qright_pright_qs	            r$   r   r   x   s    !0 /Ge% 	*4U*D 	 /  
 !!7w77??NFC7CCKKGv~&og>N.OO&u~~&6b8HH^_e_n_n^ooqry  sC  sC  rD  DE  F	

 6>**#s   C8C8C8c                  &    \ rS rSrS rS rS rSrg)r3      c                    Xl         g r(   r>   )selfr>   s     r$   __init___Compare.__init__   s    r%   c                4    U R                   UR                   :H  $ r(   rM   )rN   others     r$   __eq___Compare.__eq__   s    ||u}},,r%   c                    [        U R                  UR                  5       H  u  p#[        X#5      (       d    gX#Ld  M    g   g)NFT)zipr>   r/   )rN   rR   cls_xcls_ys       r$   __le___Compare.__le__   s<    emm<LEe++! =
 r%   rM   N)rA   
__module____qualname____firstlineno__rO   rS   rY   __static_attributes__ r%   r$   r3   r3      s    -r%   r3   c                $    U R                  U5      $ r(   r   r!   s     r$   _kl_bernoulli_bernoullirb          ??1r%   c                   UR                   R                  5       UR                  R                  5       -   U R                   U R                  -   R                  5       -   U R                   R                  5       U R                  R                  5       -   UR                   UR                  -   R                  5       -   -
  U R                   UR                   -
  U R                   R                  5       -  -   U R                  UR                  -
  U R                  R                  5       -  -   UR                   UR                  -   U R                   U R                  -   -
  U R                   U R                  -   R                  5       -  -   $ r(   )alphalgammabetadigammar!   s     r$   _kl_beta_betari      s    
	AFFMMO	+qww/?.G.G.I	I77>>affmmo-1661A0I0I0KK	MGGagg!22	4 FFQVVOqvv~~//	1
 gg177QVV#34ww((*+	
	r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_binomial_binomialrk      rc   r%   c                ,   U R                   R                  S5      R                  5       UR                   R                  S5      R                  5       -
  U R                   R                  5       UR                   R                  5       -
  R                  S5      -
  U R                   UR                   -
  U R                   R                  5       U R                   R                  S5      R                  5       R	                  S5      -
  -  R                  S5      -   $ )N)concentrationsumrf   rh   	unsqueezer!   s     r$   _kl_dirichlet_dirichletrq      s     
		R	 	'	'	)AOO,?,?,C,J,J,L	LOO""$q'='='??DDRH	J 1??2OO++-oo))"-557AA"EF
 c"g
	
r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_categorical_categoricalrs      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_cauchy_cauchyru      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   +_kl_continuousbernoulli_continuousbernoullirw      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_normal_normalry      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_mvn_mvnr{      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_uniform_uniformr}      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_laplace_laplacer      rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_geometric_geometricr      rc   r%   c           	        [        U 5      [        U5      :X  d  [        e/ nU R                   H+  nUR                  5       nSUl        UR                  U5        M-     UR                  nU R                  " U6 n [        5       (       a  [        R                  " XRSS9nO [        R                  R                  XR5      n UR                  " U6 U-
  n[!        X$U5       H/  u  pnX-
  U-  nU[#        U[%        UR&                  5      5      -  nM1     U$ ! [         aE  n[        SR                  [        U 5      R                  [        U5      R                  S95      UeSnAff = f)zuCompute kl-divergence using `Bregman divergences <https://www.lix.polytechnique.fr/~nielsen/EntropyEF-ICIP2010.pdf>`_FT)create_graphzkCan't compute kl_divergence({cls_p}, {cls_q}) use bregman divergence. Please register_kl({cls_p}, {cls_q}).)r+   r,   N)r    r<   _natural_parametersdetachstop_gradientappend_log_normalizerr   paddlegradstatic	gradientsRuntimeErrorr0   formatrA   rV   _sum_rightmostlenevent_shape)r"   r#   p_natural_paramsparamq_natural_params
p_log_normp_gradseklp_paramq_paramp_gradterms                r$   _kl_expfamily_expfamilyr      s_    7d1g!!&&#& '
 ,,""$45Jkk4G mm--jKG 
		,	-
	:B$'G% & !V+
nT3q}}#566	% I  y  A  A1g&&d1g.>.> A 
 		s   8$D D 
EA EEc                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_exponential_exponentialr     rc   r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_gamma_gammar     rc   r%   c                L    U R                   R                  UR                   5      $ r(   )_baser   r!   s     r$   _kl_lognormal_lognormalr   $  s    77  ))r%   c                $    U R                  U5      $ r(   ra   r!   s     r$   _kl_poisson_poissonr   )  rc   r%   c                \    US:  a%  U R                  [        [        U* S5      5      5      $ U $ )Nr   )ro   listrange)valuens     r$   r   r   .  s)    ,-E599T%A,'(<u<r%   )r"   r   r#   r   returnr   )r+   type[Distribution]r,   r   r   zCallable[[_T], _T])J
__future__r   	functoolsr?   typingr   r   r   r   paddle.distribution.bernoullir   paddle.distribution.betar   paddle.distribution.binomialr	   paddle.distribution.categoricalr
   paddle.distribution.cauchyr   (paddle.distribution.continuous_bernoullir   paddle.distribution.dirichletr    paddle.distribution.distributionr   paddle.distribution.exponentialr   &paddle.distribution.exponential_familyr   paddle.distribution.gammar   paddle.distribution.geometricr   paddle.distribution.laplacer   paddle.distribution.lognormalr   'paddle.distribution.multivariate_normalr   paddle.distribution.normalr   paddle.distribution.poissonr   paddle.distribution.uniformr   paddle.frameworkr   r   r   __all__r)   r   r   r   total_orderingr3   rb   ri   rk   rq   rs   ru   rw   ry   r{   r}   r   r   r   r   r   r   r   r   r_   r%   r$   <module>r      s   #   3 3  3 ) 1 7 - H 3 9 7 D + 3 / 3 F - / / ,	B/
*-@!!&8!!H+0     Y	" # T4
 
 Xx  ! Y	" #  [+& ' VV   "56 7 VV  !34 5 Wg  Wg  Y	" #  12$ 3$N [+& ' UE  Y	"* #* Wg =r%   