
    zj$                    n   d dl mZ d dlZd dlZd dlmZmZ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 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$ 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. erd dlm/Z/  ed          Z0ddgZ1i Z2d;d Z3d<d%Z4d& Z5ej6         G d' d(                      Z7 e4e
e
          d)             Z8 e4ee          d*             Z9 e4ee          d+             Z: e4ee          d,             Z; e4ee          d-             Z< e4ee          d.             Z= e4ee          d/             Z> e4e(e(          d0             Z? e4e&e&          d1             Z@ e4e,e,          d2             ZA e4e"e"          d3             ZB e4e e           d4             ZC e4ee          d5             ZD e4ee          d6             ZE e4ee          d7             ZF e4e$e$          d8             ZG e4e*e*          d9             ZHd: ZIdS )=    )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_divergencepr   qreturnr   c                j     t          t          |           t          |                    | |          S )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r   r   s     f/lsinfo/ai/hellotax_ai/data_center/backend/venv/lib/python3.11/site-packages/paddle/distribution/kl.pyr   r   4   s,    : '9T!WWd1gg&&q!,,,    cls_ptype[Distribution]cls_qCallable[[_T], _T]c                     t           t                    rt          t                    st          d           fd}|S )a  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                     | t           f<   | S N)_REGISTER_TABLE)fr'   r)   s    r%   	decoratorzregister_kl.<locals>.decoratorq   s    ()u%r&   )
issubclassr   	TypeError)r'   r)   r0   s   `` r%   r   r   T   sg    0 e\** L*|3 3 L JKKK      r&   c                     fdt           D             }|st          t          d |D                       j        \  }}t          d |D                       j        \  }}t           ||f         t           ||f         ur;t	          j        d j         dj         d|j         d|j         d	t                     t           ||f         S )z3Multiple dispatch into concrete implement function.c                ^    g | ])\  }}t          |          t          |          %||f*S  )r1   ).0super_psuper_qr'   r)   s      r%   
<listcomp>z_dispatch.<locals>.<listcomp>|   sV       GWeW%% +5UG*D*D	'  r&   c              3  (   K   | ]}t          | V  d S r-   )_Comparer6   ms     r%   	<genexpr>z_dispatch.<locals>.<genexpr>   s&      77!1777777r&   c              3  B   K   | ]}t          t          |           V  d S r-   )r;   reversedr<   s     r%   r>   z_dispatch.<locals>.<genexpr>   s-      CCa8Xa[[1CCCCCCr&   zAmbiguous kl_divergence(z, z). Please register_kl())r.   NotImplementedErrorminclasseswarningswarn__name__RuntimeWarning)r'   r)   matchesleft_pleft_qright_pright_qs   ``     r%   r"   r"   x   s"        /  G
  "!!77w77777?NFFCC7CCCCCKGWvv~&ogw>N.OOO Fu~  F  F  F  F_e_n  F  Fry  sC  F  F  F	
 	
 	

 66>**r&   c                       e Zd Zd Zd Zd ZdS )r;   c                    || _         d S r-   rD   )selfrD   s     r%   __init__z_Compare.__init__   s    r&   c                "    | j         |j         k    S r-   rP   )rQ   others     r%   __eq__z_Compare.__eq__   s    |u},,r&   c                x    t          | j        |j                  D ]\  }}t          ||          s dS ||ur ndS )NFT)ziprD   r1   )rQ   rT   cls_xcls_ys       r%   __le__z_Compare.__le__   sV    em<< 	 	LE5eU++ uuE!! "tr&   N)rG   
__module____qualname__rR   rU   rZ   r5   r&   r%   r;   r;      sA          - - -    r&   r;   c                ,    |                      |          S r-   r   r$   s     r%   _kl_bernoulli_bernoullir_          ??1r&   c                   |j                                         |j                                        z   | j         | j        z                                   z   | j                                         | j                                        z   |j         |j        z                                   z   z
  | j         |j         z
  | j                                         z  z   | j        |j        z
  | j                                        z  z   |j         |j        z   | j         | j        z   z
  | j         | j        z                                   z  z   S r-   )alphalgammabetadigammar$   s     r%   _kl_beta_betarf      s    
		AFMMOO	+qw/?.G.G.I.I	I7>>afmmoo-161A0I0I0K0KK	MGag!2!22	4 FQVOqv~~///	1
 g17QV#34w((**+	
	r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_binomial_binomialrh      r`   r&   c                <   | j                             d                                          |j                             d                                          z
  | j                                         |j                                         z
                      d          z
  | j         |j         z
  | j                                         | j                             d                                                              d          z
  z                      d          z   S )N)concentrationsumrc   re   	unsqueezer$   s     r%   _kl_dirichlet_dirichletrn      s     
		R	 	 	'	'	)	)AO,?,?,C,C,J,J,L,L	LO""$$q'='='?'??DDRHH	J 1?2O++--o))"--5577AA"EEF
 c"gg
	
r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_categorical_categoricalrp      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_cauchy_cauchyrr      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   +_kl_continuousbernoulli_continuousbernoullirt      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_normal_normalrv      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_mvn_mvnrx      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_uniform_uniformrz      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_laplace_laplacer|      r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_geometric_geometricr~      r`   r&   c           	        t          |           t          |          k    st          g }| j        D ]2}|                                }d|_        |                    |           3|j        } | j        | }	 t                      rt          j	        ||d          }n t          j
                            ||          }n[# t          $ rN}t          d                    t          |           j        t          |          j                            |d}~ww xY w |j        | |z
  }t!          |||          D ]3\  }	}
}|
|	z
  |z  }|t#          |t%          |j                            z  }4|S )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#   rB   _natural_parametersdetachstop_gradientappend_log_normalizerr   paddlegradstatic	gradientsRuntimeErrorr2   formatrG   rW   _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    77d1gg!!& ' '#&&&&,""$45J 	Lk,4  GG m--j:JKKG   y  A  A1gg&d1gg.> A  
 
 		 
	,	-
	:B$'*G% % 7 7 & '!V+
nT3q}#5#5666Is   6AB= =
DA	DDc                ,    |                      |          S r-   r^   r$   s     r%   _kl_exponential_exponentialr     r`   r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_gamma_gammar     r`   r&   c                @    | j                             |j                   S r-   )_baser   r$   s     r%   _kl_lognormal_lognormalr   $  s    7  )))r&   c                ,    |                      |          S r-   r^   r$   s     r%   _kl_poisson_poissonr   )  r`   r&   c                t    |dk    r1|                      t          t          | d                              n| S )Nr   )rl   listrange)valuens     r%   r   r   .  s3    ,-EE599T%A,,''(((u<r&   )r   r   r   r   r    r   )r'   r(   r)   r(   r    r*   )J
__future__r   	functoolsrE   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_orderingr;   r_   rf   rh   rn   rp   rr   rt   rv   rx   rz   r|   r~   r   r   r   r   r   r   r5   r&   r%   <module>r      s   # " " " " "      3 3 3 3 3 3 3 3 3 3  3 3 3 3 3 3 ) ) ) ) ) ) 1 1 1 1 1 1 7 7 7 7 7 7 - - - - - - H H H H H H 3 3 3 3 3 3 9 9 9 9 9 9 7 7 7 7 7 7 D D D D D D + + + + + + 3 3 3 3 3 3 / / / / / / 3 3 3 3 3 3 F F F F F F - - - - - - / / / / / / / / / / / / , , , , , , 	B/
*- - - -@! ! ! !H+ + +0          Y	""  #" T4
 
 
 Xx    !  Y	""  #"  [+&&  '& VV    "566  76 VV   !344  54 Wg   Wg   Y	""  #"  122$ $ 32$N [+&&  '& UE   Y	""* * #"* Wg  = = = = =r&   