
    [Th{              	          % S SK r S SKrS SKJr  S SKJr  S SKrS SKJr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*J+r+J,r,  SSK-J.r.J/r/  SSK0J1r1  SSK2J3r3  SSK4J5r5  SSK6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=J>r?  0 r@\A\B\C\C4   \4   \DS'   0 rE\A\B\C\C4   \4   \DS '   S!S"/rFS# rG\ " S$ S%5      5       rHS& rIS' rJS( rKS) rLS*\S+\S,\4S- jrM\G" \
\
5      S. 5       rN\G" \\5      S/ 5       rO\G" \\5      S0 5       rP\G" \\5      S1 5       rQ\G" \\5      S2 5       rR\G" \\5      S3 5       rS\G" \\5      S4 5       rT\G" \\5      S5 5       rU\G" \\5      S6 5       rV\G" \"\"5      S7 5       rW\G" \ \ 5      S8 5       rX\G" \$\$5      S9 5       rY\G" \(\(5      S: 5       rZ\G" \,\,5      S; 5       r[\G" \/\,5      S< 5       r\\G" \,\/5      S= 5       r]\G" \/\/5      S> 5       r^\G" \1\15      S? 5       r_\G" \3\35      S@ 5       r`\G" \5\55      SA 5       ra\G" \7\75      SB 5       rb\G" \9\95      SC 5       rc\G" \;\;5      SD 5       rd\G" \
\75      SE 5       re\G" \\5      SF 5       rf\G" \\55      SG 5       rg\G" \\5      SH 5       rh\G" \\5      SI 5       ri\G" \\15      SJ 5       rj\G" \\;5      SK 5       rk\G" \\55      SL 5       rl\G" \\5      SM 5       rm\G" \\15      SN 5       rn\G" \\;5      SO 5       ro\G" \\5      \G" \\5      \G" \\55      \G" \\;5      SP 5       5       5       5       rp\G" \\5      SQ 5       rq\G" \\"5      SR 5       rr\G" \\15      SS 5       rs\G" \\5      \G" \\5      \G" \\55      \G" \\;5      ST 5       5       5       5       rt\G" \\5      SU 5       ru\G" \\"5      SV 5       rv\G" \\15      SW 5       rw\G" \"\5      \G" \"\5      \G" \"\5      \G" \"\5      \G" \"\55      \G" \"\;5      SX 5       5       5       5       5       5       rx\G" \"\15      SY 5       ry\G" \(\5      \G" \(\5      \G" \(\5      \G" \(\5      \G" \(\55      \G" \(\;5      SZ 5       5       5       5       5       5       rz\G" \(\15      S[ 5       r{\G" \1\5      \G" \1\5      \G" \1\5      \G" \1\5      \G" \1\55      \G" \1\;5      S\ 5       5       5       5       5       5       r|\G" \1\"5      S] 5       r}\G" \1\(5      S^ 5       r~\G" \5\5      \G" \5\5      \G" \5\;5      S_ 5       5       5       r\G" \5\5      S` 5       r\G" \5\5      Sa 5       r\G" \5\15      Sb 5       r\G" \7\
5      \G" \7\5      Sc 5       5       r\G" \;\5      Sd 5       r\G" \;\5      Se 5       r\G" \;\5      Sf 5       r\G" \;\5      Sg 5       r\G" \;\"5      Sh 5       r\G" \;\15      Si 5       r\G" \;\55      Sj 5       r\G" \&\&5      Sk 5       r\G" \\5      Sl 5       rSm rg)n    N)total_ordering)Callable)infTensor   )	Bernoulli)Beta)Binomial)Categorical)Cauchy)ContinuousBernoulli)	Dirichlet)Distribution)ExponentialFamily)Exponential)Gamma)	Geometric)Gumbel)
HalfNormal)Independent)Laplace)_batch_lowrank_logdet_batch_lowrank_mahalanobisLowRankMultivariateNormal)_batch_mahalanobisMultivariateNormal)Normal)OneHotCategorical)Pareto)Poisson)TransformedDistribution)Uniform)_sum_rightmosteuler_constant_KL_REGISTRY_KL_MEMOIZEregister_klkl_divergencec                    ^ ^ [        T [        5      (       d#  [        T [        5      (       a  [	        ST  35      e[        T[        5      (       d#  [        T[        5      (       a  [	        ST 35      eU U4S jnU$ )a  
Decorator to register a pairwise function with :meth:`kl_divergence`.
Usage::

    @register_kl(Normal, Normal)
    def kl_normal_normal(p, q):
        # insert implementation here

Lookup returns the most specific (type,type) match ordered by subclass. If
the match is ambiguous, a `RuntimeWarning` is raised. For example to
resolve the ambiguous situation::

    @register_kl(BaseP, DerivedQ)
    def kl_version1(p, q): ...
    @register_kl(DerivedP, BaseQ)
    def kl_version2(p, q): ...

you should register a third most-specific implementation, e.g.::

    register_kl(DerivedP, DerivedQ)(kl_version1)  # Break the tie.

Args:
    type_p (type): A subclass of :class:`~torch.distributions.Distribution`.
    type_q (type): A subclass of :class:`~torch.distributions.Distribution`.
z6Expected type_p to be a Distribution subclass but got z6Expected type_q to be a Distribution subclass but got c                 F   > U [         TT4'   [        R                  5         U $ N)r%   r&   clear)funtype_ptype_qs    N/var/www/auris/envauris/lib/python3.13/site-packages/torch/distributions/kl.py	decoratorregister_kl.<locals>.decoratorV   s"    '*VV^$
    )
isinstancetype
issubclassr   	TypeError)r.   r/   r1   s   `` r0   r'   r'   3   st    4 fd##
6<(H(HDVHM
 	
 fd##
6<(H(HDVHM
 	

 r3   c                   ,    \ rS rSrS/rS rS rS rSrg)_Match^   typesc                     Xl         g r+   r;   )selfr;   s     r0   __init___Match.__init__b   s    
r3   c                 4    U R                   UR                   :H  $ r+   r=   )r>   others     r0   __eq___Match.__eq__e   s    zzU[[((r3   c                     [        U R                  UR                  5       H  u  p#[        X#5      (       d    gX#Ld  M    g   g)NFT)zipr;   r6   )r>   rB   xys       r0   __le___Match.__le__h   s;    

EKK0DAa##z 1
 r3   r=   N)	__name__
__module____qualname____firstlineno__	__slots__r?   rC   rI   __static_attributes__ r3   r0   r9   r9   ^   s    	I)r3   r9   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  [        $ [        S U 5       5      R                  u  pV[        S U 5       5      R                  u  px[         XV4   n	[         X4   n
XLaP  [
        R                  " SU R                   SUR                   SUR                   SUR                   S3	[        5        U	$ s  snnf )zH
Find the most specific approximate match, assuming single inheritance.
c              3   2   #    U  H  n[        U6 v   M     g 7fr+   )r9   .0ms     r0   	<genexpr>_dispatch_kl.<locals>.<genexpr>   s     5WWs   c              3   D   #    U  H  n[        [        U5      6 v   M     g 7fr+   )r9   reversedrT   s     r0   rW   rX      s     AA68A;/s    zAmbiguous kl_divergence(z, z). Please register_kl())	r%   r6   NotImplementedminr;   warningswarnrK   RuntimeWarning)r.   r/   super_psuper_qmatchesleft_pleft_qright_qright_pleft_fun	right_funs              r0   _dispatch_klrj   q   s     !- ,Gf& 	+5f+F 	 ,  
  5W55;;NFAAAGGGFN+HW-.I &v&7r&//9J K""(//!2"W5E5E4FaI	

 O)s   C3C3C3c                 8    [         R                  " U [        5      $ )zA
Helper function for obtaining infinite KL Divergence throughout
)torch	full_liker   tensors    r0   _infinite_likerp      s     ??63''r3   c                 @    [         R                  R                  X 5      $ )z*
Utility function for calculating x log x
)rl   specialxlogyrn   s    r0   _x_log_xrt      s     ==v..r3   c                     U R                  S5      nU R                  S5      nU R                  SX!-  5      R                  S5      R                  S5      nUR                  U R                  SS 5      $ )zh
Utility function for calculating the trace of XX^{T} with X having arbitrary trailing batch dimensions
   N)sizereshapepowsumshape)bmatnrV   
flat_traces       r0   _batch_trace_XXTr      sa     			"A		"Ab!%(,,Q/33B7Jdjj"o..r3   pqreturnc                 h    [         [        U 5      [        U5      4   nU[        L a9  [        SU R                  R                   SUR                  R                   35      eU" X5      $ ! [         a>    [        [        U 5      [        U5      5      nU[         [        U 5      [        U5      4'    Nf = f)a  
Compute Kullback-Leibler divergence :math:`KL(p \| q)` between two distributions.

.. math::

    KL(p \| q) = \int p(x) \log\frac {p(x)} {q(x)} \,dx

Args:
    p (Distribution): A :class:`~torch.distributions.Distribution` object.
    q (Distribution): A :class:`~torch.distributions.Distribution` object.

Returns:
    Tensor: A batch of KL divergences of shape `batch_shape`.

Raises:
    NotImplementedError: If the distribution types have not been registered via
        :meth:`register_kl`.
z(No KL(p || q) is implemented for p type z and q type )r&   r5   KeyErrorrj   r\   NotImplementedError	__class__rK   )r   r   r-   s      r0   r(   r(      s    &,$q'47*+ n!6q{{7K7K6LLYZYdYdYmYmXno
 	
 q9  ,47DG,(+DGT!W$%,s   A) )AB10B1c                 |   U R                   [        R                  R                  R	                  UR
                  * 5      [        R                  R                  R	                  U R
                  * 5      -
  -  n[        X!R                   S:H  '   SX R                   S:H  '   SU R                   -
  [        R                  R                  R	                  UR
                  5      [        R                  R                  R	                  U R
                  5      -
  -  n[        X1R                   S:H  '   SX0R                   S:H  '   X#-   $ Nr   r   )probsrl   nn
functionalsoftpluslogitsr   r   r   t1t2s       r0   _kl_bernoulli_bernoullir      s    	
$$ahhY/
((


&
&y
1	2
B Bww!|Bww!|
agg+$$QXX.1D1D1M1Mahh1WW
B Bww!|Bww!|7Nr3   c                    U R                   U R                  -   nUR                   UR                  -   nUR                   R                  5       UR                  R                  5       -   UR                  5       -   nU R                   R                  5       U R                  R                  5       -   UR                  5       -   nU R                   UR                   -
  [        R                  " U R                   5      -  nU R                  UR                  -
  [        R                  " U R                  5      -  nX2-
  [        R                  " U5      -  nXE-
  U-   U-   U-   $ r+   )concentration1concentration0lgammarl   digamma)	r   r   sum_params_psum_params_qr   r   t3t4t5s	            r0   _kl_beta_betar      s   ##a&6&66L##a&6&66L	
			 	 	"Q%5%5%<%<%>	>,AVAVAX	XB	
			 	 	"Q%5%5%<%<%>	>,AVAVAX	XB


Q--
-q?O?O1P	PB


Q--
-q?O?O1P	PB

%|)D	DB7R<"r!!r3   c                    U R                   UR                   :  R                  5       (       a  [        S5      eU R                   U R                  U R                  UR                  -
  -  U R                  * R                  5       -   UR                  * R                  5       -
  -  nU R                   UR                   :  n[        X#   5      X#'   U$ )NzKKL between Binomials where q.total_count > p.total_count is not implemented)total_countanyr   r   r   log1prp   )r   r   klinf_idxss       r0   _kl_binomial_binomialr      s     	
%**,,!Y
 	
 
	188ahh&'AGG8*:*:*<<?O?O?QQ
B }}q}},H!",/BLIr3   c                     U R                   U R                  UR                  -
  -  n[        X!R                   S:H  R                  U5      '   SX R                   S:H  R                  U5      '   UR	                  S5      $ )Nr   rv   )r   r   r   	expand_asr|   )r   r   ts      r0   _kl_categorical_categoricalr      sa    	188ahh&'A%(Aww!|q!"%&Aww!|q!"559r3   c                 &   U R                   U R                  UR                  -
  -  nU R                  5       [        R                  " U R
                  * 5      -   nUR                  5       * [        R                  " UR
                  * 5      -
  nX#-   U-   $ r+   )meanr   _cont_bern_log_normrl   r   r   r   r   r   r   r   s        r0   -_kl_continuous_bernoulli_continuous_bernoullir      sp    	
188ahh&	'B	
			 5;;x#8	8B



!	!EKK$9	9B7R<r3   c                    U R                   R                  S5      nUR                   R                  S5      nUR                  5       UR                  5       -
  nU R                   R                  5       UR                   R                  5       -
  R                  S5      nU R                   UR                   -
  nU R                   R                  5       UR                  5       R	                  S5      -
  nXE-
  Xg-  R                  S5      -   $ )Nrv   )concentrationr|   r   r   	unsqueeze)r   r   sum_p_concentrationsum_q_concentrationr   r   r   r   s           r0   _kl_dirichlet_dirichletr     s     //--b1//--b1		#	#	%(;(B(B(D	DB
//
 
 
"Q__%;%;%=
=	B	B2	FB	
1??	*B	
	 	 	"%8%@%@%B%L%LR%P	PB7bg]]2&&&r3   c                 d    UR                   U R                   -  nUR                  5       * nX2-   S-
  $ Nr   ratelog)r   r   
rate_ratior   s       r0   _kl_exponential_exponentialr     s/    !&&J
..
	B?Qr3   c                    [        U 5      [        U5      :X  d  [        S5      eU R                   Vs/ s H   o"R                  5       R	                  5       PM"     nnUR                  nU R
                  " U6 n[        R                  R                  UR                  5       USS9nUR
                  " U6 U-
  n[        X4U5       H/  u  pn
X-
  U
-  nU[        U[        UR                  5      5      -  nM1     U$ s  snf )NzThe cross KL-divergence between different exponential families cannot                             be computed using Bregman divergencesT)create_graph)r5   r   _natural_paramsdetachrequires_grad__log_normalizerrl   autogradgradr|   rF   r#   lenevent_shape)r   r   np	p_nparams	q_nparams	lg_normal	gradientsresultpnpqnpgterms               r0   _kl_expfamily_expfamilyr     s    7d1g!C
 	
 9:8I8IJ8I"++-8IIJ!!I!!9-I##IMMOYT#RI	*Y6F9;!	Q.s1=='9:: < M Ks   'C6c                    UR                   U R                  UR                  -  R                  5       -  n[        R                  " UR                   5      [        R                  " U R                   5      -
  nU R                   UR                   -
  [        R
                  " U R                   5      -  nUR                  U R                  -
  U R                   U R                  -  -  nX#-   U-   U-   $ r+   )r   r   r   rl   r   r   r   r   r   r   r   r   s         r0   _kl_gamma_gammar   +  s    	
AFFQVVO002	2B	aoo	&aoo)F	FB
//AOO
+u}}Q__/M	MB
&&166/aoo6	7B7R<"r3   c                 T   U R                   UR                   -  nUR                  UR                   -  nU R                  UR                   -  nUR                  5       * U-
  U-   nU[        -  n[        R
                  " USU-   R                  5       -   U-
  5      nXV-   U-   S[        -   -
  $ r   )scalelocr   _euler_gammarl   expr   )r   r   ct1ct2ct3r   r   r   s           r0   _kl_gumbel_gumbelr   4  s    
''AGG
C
%%!''/C
%%!''/C
'')c	C	B	|	B	3!c'))++c1	2B7R<1|+,,r3   c                     U R                  5       * [        R                  " UR                  * 5      U R                  -  -
  UR                  -
  $ r+   )entropyrl   r   r   r   r   r   s     r0   _kl_geometric_geometricr   ?  s6    IIK<%++qwwh/!''99AHHDDr3   c                 B    [        U R                  UR                  5      $ r+   )_kl_normal_normal	base_distr   s     r0   _kl_halfnormal_halfnormalr   D  s    Q[[!++66r3   c                 "   U R                   UR                   -  nU R                  UR                  -
  R                  5       nUR                  5       * nX1R                   -  nU[        R
                  " U* U R                   -  5      -  nXE-   U-   S-
  $ r   )r   r   absr   rl   r   )r   r   scale_ratioloc_abs_diffr   r   r   s          r0   _kl_laplace_laplacer   I  sz     ''AGG#KEEAEEM&&(L
//
	B		B	uyy,!89	9B7R<!r3   c                    U R                   UR                   :w  a  [        S5      e[        UR                  UR                  UR
                  5      [        U R                  U R                  U R
                  5      -
  n[        UR                  UR                  UR                  U R                  -
  UR
                  5      nUR                  R                  UR                  R                  S5      -  n[        R                  R                  UR
                  USS9nU R                  UR                  -  R                  S5      n[        U R                  UR                  R                  5       R                  S5      -  5      n[        XPR                  R!                  5       R                  S5      -  5      n[        UR#                  U R                  5      5      n	Xg-   U-
  U	-
  n
SX*-   U-   U R                   S   -
  -  $ )NzKL-divergence between two Low Rank Multivariate Normals with                          different event shapes cannot be computedrw   Fupperrv         ?r   )r   
ValueErrorr   _unbroadcasted_cov_factor_unbroadcasted_cov_diag_capacitance_trilr   r   mTr   rl   linalgsolve_triangularr|   r   rsqrtsqrtmatmul)r   r   term1term3	qWt_qDinvAterm21term22term23term24term2s              r0   7_kl_lowrankmultivariatenormal_lowrankmultivariatenormalr  T  s   }}%E
 	

 "	##Q%>%>@S@S	##Q%>%>@S@S	E
 '	##	!!			E ++..1J1J1T1TUW1XXI%%a&9&99E%RA''!*C*CCHHLF	##a&?&?&E&E&G&Q&QRT&UUF a";";"@"@"B"L"LR"PPQFahhq'B'BCDFOf$v-E%-%'!--*::;;r3   c                 V   U R                   UR                   :w  a  [        S5      e[        UR                  UR                  UR
                  5      SU R                  R                  SSS9R                  5       R                  S5      -  -
  n[        UR                  UR                  UR                  U R                  -
  UR
                  5      nUR                  R                  UR                  R                  S5      -  n[        R                  R!                  UR
                  USS9n[#        U R                  UR                  R%                  5       R                  S5      -  5      n[#        UR'                  U R                  5      5      nXg-
  nSX(-   U-   U R                   S	   -
  -  $ )
NKL-divergence between two (Low Rank) Multivariate Normals with                          different event shapes cannot be computedrx   rw   rv   dim1dim2Fr   r   r   )r   r   r   r   r   r   _unbroadcasted_scale_trildiagonalr   r|   r   r   r   r   rl   r   r   r   r   r   )	r   r   r   r   r   r   r   r   r   s	            r0   0_kl_multivariatenormal_lowrankmultivariatenormalr	  v  sx   }}%E
 	

 "	##Q%>%>@S@S	A''00br0BFFHLLRPPQE '	##	!!			E ++..1J1J1T1TUW1XXI%%a&9&99E%RA	##a&?&?&E&E&G&Q&QRT&UUF ahhq'B'BCDFOE%-%'!--*::;;r3   c                 4   U R                   UR                   :w  a  [        S5      eSUR                  R                  SSS9R	                  5       R                  S5      -  [        U R                  U R                  U R                  5      -
  n[        UR                  UR                  U R                  -
  5      n[        R                  R                  UR                  R                  S S U R                  R                  S S 5      nU R                   S   nUR                  R!                  XEU4-   5      nU R                  R!                  XEU R"                  R%                  S5      4-   5      n[        R&                  " U R                  R)                  5       5      R!                  XEU4-   5      n[+        [        R,                  R/                  XgSS95      n	[+        [        R,                  R/                  XhSS95      n
X-   nS	X+-   U-   U R                   S   -
  -  $ )
Nr  rx   rw   rv   r  r   Fr   r   )r   r   r  r  r   r|   r   r   r   r   r   r   rl   _C_infer_sizer}   expand
cov_factorry   
diag_embedr   r   r   r   )r   r   r   r   combined_batch_shaper   q_scale_trilp_cov_factor
p_cov_diagr   r   r   s               r0   0_kl_lowrankmultivariatenormal_multivariatenormalr    s   }}%E
 	

 ++44"24FJJLPP
 	##Q%>%>@S@S	E
 q::QUUQUU]LE !88//	##))#2.0K0K0Q0QRUSU0V 	
aA..556JQRV6STL..551<<#4#4R#899L !!!";";"@"@"BCJJ1v%J %%l%NF %%le%LF OE%-%'!--*::;;r3   c                 "   U R                   UR                   :w  a  [        S5      eUR                  R                  SSS9R	                  5       R                  S5      U R                  R                  SSS9R	                  5       R                  S5      -
  n[        R                  R                  UR                  R                  S S U R                  R                  S S 5      nU R                   S   nUR                  R                  X4U4-   5      nU R                  R                  X4U4-   5      n[        [        R                  R                  XVSS95      n[        UR                  UR                  U R                  -
  5      nUSXx-   U-
  -  -   $ )	NzvKL-divergence between two Multivariate Normals with                          different event shapes cannot be computedrw   rv   r  r   Fr   r   )r   r   r  r  r   r|   rl   r  r  r}   r  r   r   r   r   r   )	r   r   
half_term1r  r   r  p_scale_trilr   r   s	            r0   )_kl_multivariatenormal_multivariatenormalr    sx    	}}%E
 	

 ,,552B5GKKMQQ
	##,,"2,>BBDHHLMJ !88//	##))#2.0K0K0Q0QRUSU0V 	
aA..556JQRV6STL..556JQRV6STL%%l%NE q::QUUQUU]LEu}q0111r3   c                     U R                   UR                   -  R                  S5      nU R                  UR                  -
  UR                   -  R                  S5      nSX#-   S-
  UR                  5       -
  -  $ Nrx   r   r   r   r{   r   r   )r   r   	var_ratior   s       r0   r   r     sa    177"''*I55155=AGG
#	(	(	+B).1$y}}677r3   c                 B    [        U R                  UR                  5      $ r+   )r   _categoricalr   s     r0   '_kl_onehotcategorical_onehotcategoricalr    s    &q~~q~~FFr3   c                 @   U R                   UR                   -  nUR                  U R                  -  nUR                  UR                  5       -  nUR                  5       * nXE-   U-   S-
  n[        X`R                  R
                  UR                  R
                  :  '   U$ r   )r   alphar   r   supportlower_bound)r   r   r   alpha_ratior   r   r   s          r0   _kl_pareto_paretor%    s     ''AGG#K''AGG#K	
;??$	$B
//
	BW{"Q&F<?F99  199#8#889Mr3   c                     U R                   U R                   R                  5       UR                   R                  5       -
  -  U R                   UR                   -
  -
  $ r+   r   r   s     r0   _kl_poisson_poissonr'    s;    66QVVZZ\AFFJJL01QVVaff_EEr3   c                     U R                   UR                   :w  a  [        eU R                  UR                  :w  a  [        e[        U R                  UR                  5      $ r+   )
transformsr   r   r(   r   r   s     r0   _kl_transformed_transformedr*    sC    ||q||#!!}}%!!akk22r3   c                     UR                   UR                  -
  U R                   U R                  -
  -  R                  5       n[        X!R                  U R                  :  UR                   U R                   :  -  '   U$ r+   )highlowr   r   r   r   r   s      r0   _kl_uniform_uniformr/    sW    vv~!&&155.1668F25FEEAEEMaffqvvo./Mr3   c                     U R                  5       * U R                  UR                  R                  5       -  UR                  -
  -
  $ r+   )r   r   r   r   r   s     r0   _kl_bernoulli_poissonr1     s1    IIK<177QVVZZ\1AFF:;;r3   c                     U R                  5       * U R                  UR                  -  -
  [        R                  " UR
                  * 5      -
  UR                  5       -
  $ r+   )r   r   r   rl   r   r   r   r   s     r0   _kl_beta_continuous_bernoullir3    sS     

&&188
	
++qwwh
	  


!	"r3   c                 ,    [        U R                  5      $ r+   )rp   r   r   s     r0   _kl_beta_infinityr5    s    !**++r3   c                     U R                  5       * UR                  R                  5       -
  UR                  U R                  U R                  U R                  -   -  -  -   $ r+   )r   r   r   r   r   r   s     r0   _kl_beta_exponentialr7    sT     

&&**,	
&&A$$(8(81;K;K(KL
M	Nr3   c                    U R                  5       * nUR                  R                  5       UR                  UR                  R	                  5       -  -
  nUR                  S-
  U R
                  R                  5       U R
                  U R                  -   R                  5       -
  -  nUR                  U R
                  -  U R
                  U R                  -   -  nX#-   U-
  U-   $ r   )r   r   r   r   r   r   r   r   r   s         r0   _kl_beta_gammar9    s    
))+B	
			!AOOaffjjl$B	BB
//A
	  "a&6&69I9I&I%R%R%TT
B 
!""	"a&6&69I9I&I	JB7R<"r3   c                    U R                   U R                   U R                  -   -  nUR                  R                  S5      nU R	                  5       * nSUS-  [
        R                  -  R                  5       -  nUSU-
  -  U R                   U R                  -   S-   -  UR                  S5      -   S-  nUR                  U-  nUR                  R                  S5      S-  nXE-   Xg-
  U-   U-  -   $ r  )	r   r   r   r{   r   mathpir   r   )	r   r   E_beta
var_normalr   r   r   r   r   s	            r0   _kl_beta_normalr?  +  s    !1!1A4D4D!DEFQJ
))+B	
Q(--/	/B!f*!1!1A4D4D!Dq!HI
**Q-	
B 
B	
1	B7bglj000r3   c                    U R                  5       * UR                  UR                  -
  R                  5       -   n[        X!R                  U R
                  R                  :  UR                  U R
                  R                  :  -  '   U$ r+   )r   r,  r-  r   r   r"  r#  upper_boundr.  s      r0   _kl_beta_uniformrB  :  sa    iik\QVVaee^0022FQTFEEAII)))affqyy7L7L.LMNMr3   c                 ,    [        U R                  5      $ r+   )rp   r   r   s     r0   !_kl_continuous_bernoulli_infinityrD  D  s    !''""r3   c                     U R                  5       * [        R                  " UR                  5      -
  UR                  U R                  -  -   $ r+   )r   rl   r   r   r   r   s     r0   $_kl_continuous_bernoulli_exponentialrF  I  s3    IIK<%))AFF++affqvvo==r3   c                     U R                  5       * nS[        R                  " S[        R                  -  5      [        R
                  " UR                  UR                  -  5      -   -  [        R                  " UR                  5      -   nU R                  [        R
                  " U R                  5      -   SUR                  -  U R                  -  -
  S[        R
                  " UR                  5      -  -  nX#-   U-   $ )Nr   g       @)
r   r;  r   r<  rl   squarer   r   variancer   r   s        r0   _kl_continuous_bernoulli_normalrJ  R  s    
))+B	tww'%,,quuqww*GG	H599	L 
B **u||AFF+
+cAEEkAFF.B
Bell177##
B 7R<r3   c           	         U R                  5       * UR                  UR                  -
  R                  5       -   n[        R
                  " [        R                  " [        R                  " UR                  U R                  R                  5      [        R                  " UR                  U R                  R                  5      5      [        R                  " U5      [        -  U5      $ r+   )r   r,  r-  r   rl   wheremaxger"  r#  lerA  	ones_liker   r.  s      r0    _kl_continuous_bernoulli_uniformrQ  ^  s    iik\QVVaee^0022F;;		HHQUUAII112HHQVVQYY223	
 	#% r3   c                 ,    [        U R                  5      $ r+   rp   r   r   s     r0   _kl_exponential_infinityrT  k  s    
 !&&!!r3   c                     UR                   U R                   -  nUR                  * [        R                  " U5      -  nUU-   UR                  R	                  5       -   UR                  [
        -  -   S[
        -   -
  $ r   )r   r   rl   r   r   r   )r   r   ratior   s       r0   _kl_exponential_gammarW  s  st    FFQVVOE
//	EIIe,	,B

	
//
 
 
"	# //L
(	) |		r3   c                     U R                   UR                  -  nUR                  UR                  -  nUR                  5       S-
  n[        R
                  " U5      U-  US-   -  nUR                  5       nXC-
  U-   U-   $ r   )r   r   r   r   rl   r   
reciprocal)r   r   scale_rate_prodloc_scale_ratior   r   r   s          r0   _kl_exponential_gumbelr\    sv    ffqww&OeeaggoO					"B	?	#o	519L	MB		#	#	%B"$r))r3   c                 z   UR                   R                  S5      nU R                  R                  S5      nS[        R                  " X2-  S-  [
        R                  -  5      -  nUR                  5       nUR                  U R                  -  nUR                  R                  S5      S-  nUS-
  XV-
  U-   U-  -   $ r  )	r   r{   r   rl   r   r;  r<  rY  r   )r   r   r>  rate_sqrr   r   r   r   s           r0   _kl_exponential_normalr_    s    QJvvzz!}H	uyy.2TWW<=	=B				B	
B	
1	B6RWr\Z///r3   c                 ,    [        U R                  5      $ r+   )rp   r   r   s     r0   _kl_gamma_infinityra    s    
 !//**r3   c                     U R                  5       * UR                  R                  5       -
  UR                  U R                  -  U R                  -  -   $ r+   )r   r   r   r   r   s     r0   _kl_gamma_exponentialrc    s:    IIK<!&&**,&!//)AAFF)JJJr3   c                    U R                   UR                  -  nUR                  UR                  -  nU R                  S-
  U R                  R	                  5       -  U R                  R                  5       -
  U R                  -
  nUR                  5       U R                  U-  -   n[        R                  " U5      SUR                  5       -   R                  U R                  * 5      -  U-
  nXE-   U-   $ r   )r   r   r   r   r   r   r   rl   r   rY  r{   )r   r   beta_scale_prodr[  r   r   r   s          r0   _kl_gamma_gumbelrf    s    ffqww&OeeaggoO	
1	 7 7 99
//
 
 
"	#
//	 
 
			?!B	BB		/"))++
0
0!//1A
B	C
	 
 7R<r3   c                 p   UR                   R                  S5      nU R                  R                  S5      nS[        R                  " X2-  S-  [
        R                  -  5      -  U R                  -
  U R                  R                  5       -
  nSU R                  R                  S5      U R                  -   -  U-  nUR                  U R                  -  U R                  -  nSUR                  R                  S5      -  nUU R                  S-
  U R                  R                  5       -  -   XV-
  U-   U-  -   $ r  )r   r{   r   rl   r   r;  r<  r   r   r   r   )r   r   r>  beta_sqrr   r   r   r   s           r0   _kl_gamma_normalri    s   QJvvzz!}Heii-1DGG;<<
//	
//
 
 
"	# 
 
##A&8	9H	DB	
	 166	)B	quuyy|	B
??Q!//"9"9";
;	<7R<:
%	&r3   c                 ,    [        U R                  5      $ r+   rp   r   r   s     r0   _kl_gumbel_infinityrl         !%%  r3   c                    U R                   UR                   -  nU[        R                  " S[        R                  -  5      -  R	                  5       n[        R                  U-  S-  R                  S5      S-  nU R                  U R                   [        -  -   UR                  -
  UR                   -  R                  S5      S-  nU* U-   U-   [        S-   -
  $ )Nrx   r      r   )r   r;  r   r<  r   r{   r   r   )r   r   param_ratior   r   r   s         r0   _kl_gumbel_normalrq    s    ''AGG#K
		!dgg+.
.	3	3	5B
''K
#
%	*	*1	-	1B55177\))AEE1QWW
<	A	A!	Ds	JB38b=L1,--r3   c                 ,    [        U R                  5      $ r+   rk  r   s     r0   _kl_laplace_infinityrs    rm  r3   c                    UR                   R                  S5      nU R                   R                  S5      U-  nS[        R                  " SU-  [        R
                  -  5      -  nSU R                  R                  S5      -  nU R                  UR                  -  nSUR                  R                  S5      -  nU* U-   XV-
  U-   U-  -   S-
  $ r  )r   r{   rl   r   r;  r<  r   )r   r   r>  scale_sqr_var_ratior   r   r   r   s           r0   _kl_laplace_normalrv    s    QJ''++a.:5	uyy00477:;	;B	quuyy|	B	
B	quuyy|	B3$$"
'BBQFFr3   c                 ,    [        U R                  5      $ r+   rk  r   s     r0   _kl_normal_infinityrx    rm  r3   c                    U R                   UR                  -  nU R                  UR                  -  R                  S5      nUR                   UR                  -  nUR                  5       S-  nX$-
  n[        R
                  " U* SU-  -   U-   5      nU* U-   U-   SS[        R                  " S[        R                  -  5      -   -  -
  $ r  )r   r   r{   r   rl   r   r;  r<  )r   r   mean_scale_ratiovar_scale_sqr_ratior[  r   r   r   s           r0   _kl_normal_gumbelr|    s    uuqww77QWW,11!4eeaggoO		 	 	"S	(B		+B	$$s-@'@@?R	SB38b=C1txxDGG'<#<=>>r3   c                 N   U R                   UR                   -
  nU R                  UR                  -  nX R                  -  n[        R                  " U5      n[        R
                  " S[        R                  -  5      U R                  -  [        R                  " SUR                  S5      -  5      -  nU[        R                  " [        R
                  " S5      U-  5      -  nU* Xg-   UR                  -  -   SS[        R                  " S[        R                  -  5      -   -  -
  $ )Nrx   g      r   r   )
r   r   rl   r   r;  r   r<  r   r{   erf)r   r   loc_diffr   loc_diff_scale_ratior   r   r   s           r0   _kl_normal_laplacer    s    uuquu}H''AGG#K#gg-	;	B		!dgg+(599T<P<T<TUV<W5W+XX  
EIIdiin/CCD	DB3"'QWW$$q488C$''M3J/J(KLLr3   c                 ,    [        U R                  5      $ r+   )rp   r   r   s     r0   _kl_pareto_infinityr    s     !''""r3   c                 "   U R                   UR                  -  nU R                  U-  R                  5       nU R                  R	                  5       nU R                  U-  U R                  S-
  -  nX4-
  U-   S-
  n[
        X`R                  S:*  '   U$ r   )r   r   r!  r   rY  r   )r   r   rZ  r   r   r   r   s          r0   _kl_pareto_exponentialr  !  s}    gg&O
''O
#	(	(	*B	
			B	
?	"aggk	2BWr\AFF77a<Mr3   c                    U R                   R                  5       U R                  R                  5       -   nU R                  R                  5       U-
  nUR                  R                  5       UR                  UR                  R                  5       -  -
  nSUR                  -
  U-  nUR                  U R                  -  U R                   -  U R                  S-
  -  nX4-   U-   U-   S-
  n[        XpR                  S:*  '   U$ r   )r   r   r!  rY  r   r   r   r   r   r   common_termr   r   r   r   r   s           r0   _kl_pareto_gammar  ,  s    ''++-!''"4"4"66K	
	$B	
			!AOOaffjjl$B	BB
aoo
	,B	
!''	AGG	#qww{	3BWr\B"FF77a<Mr3   c                 `   SUR                   R                  S5      -  nU R                   U R                  S-
  -  n[        R                  " S[        R
                  -  5      UR                   -  U R                  -  U R                   -  R                  5       nU R                  R                  5       nU R                  UR                  S5      -  U R                  S-
  -  nU R                  U-  UR                  -
  R                  S5      nXE-
  Xg-   U-  -   S-
  n[        XR                  S:*  '   U$ )Nrx   r   )
r   r{   r!  r;  r   r<  r   rY  r   r   )	r   r   r>  r  r   r   r   r   r   s	            r0   _kl_pareto_normalr  ;  s    QWW[[^#J''QWWq[)K
))AK
 177
*QWW
4qww
>	C	C	EB	
			B	
;??1%	%1	5B
''K
!%%
'	,	,Q	/BW:--1FF77a<Mr3   c                 ,    [        U R                  5      $ r+   rS  r   s     r0   _kl_poisson_infinityr  H  s     !&&!!r3   c                    U R                   U R                  -
  n[        R                  " U5      nUR                  S-
  [        U R                   5      [        U R                  5      -
  U-
  -  U-  nUR                  S-
  [        SU R                   -
  5      [        SU R                  -
  5      -
  U-   -  U-  nUR                  R                  5       UR                  R                  5       -   UR                  UR                  -   R                  5       -
  nXV-   U-
  U-
  n[        XpR                   UR                  R                  :  U R                  UR                  R                  :  -  '   U$ r   )r,  r-  rl   r   r   rt   r   r   r   r"  rA  r#  r  s           r0   _kl_uniform_betar  N  sE   &&155.K	;	B	
		A	AFFhquuo-;	=
	  
		A	AJ(1quu9"55C	E
	  	
!



!
!
#	$a...
6
6
8	9 
 Wr\BFQTFFFQYY***quuqyy7L7L/LMNMr3   c           	         U R                  5       * U R                  UR                  -  -
  [        R                  " UR
                  * 5      -
  UR                  5       -
  n[        R                  " [        R                  " [        R                  " U R                  UR                  R                  5      [        R                  " U R                  UR                  R                  5      5      [        R                   " U5      ["        -  U5      $ r+   )r   r   r   rl   r   r   r   rL  rM  rN  r,  r"  rA  rO  r-  r#  rP  r   r.  s      r0    _kl_uniform_continuous_bernoullir  f  s     

&&188
	
++qwwh
	  


!	"  ;;		HHQVVQYY223HHQUUAII112	
 	#% r3   c                    UR                   U R                  U R                  -   -  S-  U R                  U R                  -
  UR                   -  R                  5       -
  n[        X R                  UR
                  R                  :  '   U$ )Nrx   )r   r,  r-  r   r   r"  r#  r.  s      r0   _kl_uniform_exponetialr  x  sd    VVqvv~&*qvv~.G-L-L-NNF,/F55199((()Mr3   c                    U R                   U R                  -
  nUR                  5       nUR                  R	                  5       UR                  UR
                  R                  5       -  -
  nSUR                  -
  [        U R                   5      [        U R                  5      -
  U-
  -  U-  nUR
                  U R                   U R                  -   -  S-  nU* U-   U-   U-   n[        XpR                  UR                  R                  :  '   U$ )Nr   rx   )
r,  r-  r   r   r   r   rt   r   r"  r#  r  s           r0   _kl_uniform_gammar    s    &&155.K		B	
			!AOOaffjjl$B	BB	
Q__	AFFhquuo-;	=
	 
 
166AEE>	"Q	&BS2X]RF,/F55199((()Mr3   c                    UR                   U R                  U R                  -
  -  nU R                  UR                  -
  UR                   -  nU R                  UR                  -
  UR                   -  nUR	                  5       SX4-   -  -   nU[
        R                  " U* 5      [
        R                  " U* 5      -
  -  nXV-
  $ )Nr   )r   r,  r-  r   r   rl   r   )r   r   r  high_loc_difflow_loc_diffr   r   s          r0   _kl_uniform_gumbelr    s    ''QVVaee^,KVVaee^qww.MEEAEEMQWW,L		SM$@A	AB			=.1EII|m4LL	MB7Nr3   c                    U R                   U R                  -
  n[        R                  " [        R                  S-  5      UR
                  -  U-  R                  5       nUR                  S5      S-  nU R                   U R                  -   SUR                  -  -
  S-  R                  S5      nUSXE-   -  UR
                  R                  S5      -  -   $ )Nrx      r   )	r,  r-  r;  r   r<  r   r   r{   r   )r   r   r  r   r   r   s         r0   _kl_uniform_normalr    s    &&155.K
))DGGaK
 177
*[
8	=	=	?B
		1		"B66AEE>AI%
*	/	/	2Brw!''++a.000r3   c                    U R                   U R                  -
  nUR                  UR                  R	                  UR                  5      -  U-  R                  5       n[        U R                   5      [        U R                  5      -
  U-
  U-  nXAR                  S-   -  U-
  n[        XPR                  UR                  R                  :  '   U$ r   )
r,  r-  r!  r   r{   r   rt   r   r"  r#  )r   r   support_uniformr   r   r   s         r0   _kl_uniform_paretor    s    ffquunO
''AGGKK(
(O
<	A	A	CB
166
Xaee_
,
>/	QB77Q;"$F,/F55199((()Mr3   c                     U R                   UR                   :w  a  [        e[        U R                  UR                  5      n[	        X R                   5      $ r+   )reinterpreted_batch_ndimsr   r(   r   r#   r.  s      r0   _kl_independent_independentr    sA    ""a&A&AA!!1;;4F&"="=>>r3   c                    U R                   UR                   -   R                  S5      U R                  UR                  -
  R                  S5      -   R                  5       nSU R                   -  UR                   -  R                  5       nX#-
  $ )Nrx      r  r   s       r0   _kl_cauchy_cauchyr    sl     77QWW
!
!!
$':':1'=
=	B	B	DB
agg+
	$	$	&B7Nr3   c                     S/n [        [        S S9 H1  u  pU R                  SUR                   SUR                   S35        M3     SR	                  U 5      n[
        R                  (       a  [
        =R                  U-  sl        gg)	zHAppends a list of implemented KL functions to the doc for kl_divergence.zLKL divergence is currently implemented for the following distribution pairs:c                 >    U S   R                   U S   R                   4$ r   )rK   )p_qs    r0   <lambda>_add_kl_info.<locals>.<lambda>  s    s1vA&Hr3   )keyz* :class:`~torch.distributions.z#` and :class:`~torch.distributions.`z
	N)sortedr%   appendrK   joinr(   __doc__)rowsr   r   kl_infos       r0   _add_kl_infor    s     	WD H 	-ajj\9\]^]g]g\hhij	
 kk$G( r3   )r;  r^   	functoolsr   typingr   rl   r   r   	bernoullir   betar	   binomialr
   categoricalr   cauchyr   continuous_bernoullir   	dirichletr   distributionr   
exp_familyr   exponentialr   gammar   	geometricr   gumbelr   half_normalr   independentr   laplacer   lowrank_multivariate_normalr   r   r   multivariate_normalr   r   normalr   one_hot_categoricalr   paretor   poissonr    transformed_distributionr!   uniformr"   utilsr#   r$   r   r%   dicttupler5   __annotations__r&   __all__r'   r9   rj   rp   rt   r   r(   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r	  r  r  r   r  r%  r'  r*  r/  r1  r3  r5  r7  r9  r?  rB  rD  rF  rJ  rQ  rT  rW  r\  r_  ra  rc  rf  ri  rl  rq  rs  rv  rx  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  rQ   r3   r0   <module>r     sQ	     $        $  5   & ) $     # $  
 H  2   =  A
  d	$*x 
  T	$*x  /
*(V   $6(//\ l v L Y	" # T4" " Xx  ! [+& '  "56 7 Y	"' #' [+& '  12 3" UE  VV- - Y	"E #E Z$7 %7 Wg  &(AB< C<B !:;< <<: &(:;!< <!<H !342 520 VV8 8  12G 3G VV  WgF F $&=>3 ?3 Wg  Y < !< T&' ( T6, , T;   T5  T61 1 T7   &)# *#  +.> />  &) *  '*	 +	 [$[-.[&!['"" # " /  " [% 	 !	 [&!* "* [&!0 "0 UDU'(UFUG+   ) + UK K !K UF ( UF $ VTV()V[!VUVVVW!    " * ! VV. . WdW)*Wk"WeWfWg!    # + ! WfG G VTV()V[!VUVVVW!    " * ! VV? ? VW	M 	M VTV()VW#  * # V[! " VU  VV	 	 Wi Wh"   !" Wd . W)* +" Wk" # We  Wf  Wf1 1 Wf  [+&? '? VV )r3   