
    ёi                     ^    S SK r S SKrS SKrSSKJr  \" \\ R                  SS9rS r	S r
S rg)	    N   )
get_loggerz&%(asctime)s-%(levelname)s: %(message)s)fmtc                    S/[        U5      -  n[        [        U5      [        U 5      -  5      nSnUn[        [        U 5      5       Hx  nXU R                  S5      nXT-
  nXs:X  a  SnOX   X7-
  S-   -  n[        XE5       H  n	X   S:X  a  SOUX)'   M     XC-  nXS-  nUS-   [        U 5      S-
  :X  d  Mm  [        U5      nMz     U$ )z
Expand hist bins.
r   g           )lenintrangecount)
quantized_binsreference_binsexpanded_quantized_binsnum_merged_binsj_startj_endidx
zero_countavg_bin_eleidx1s
             k/var/www/html/banglarbhumi/venv/lib/python3.13/site-packages/paddle/static/quantization/cal_kl_threshold.pyexpand_quantized_binsr      s      !cC$77#n-N0CCDOGES()#E288;
/(K(-,s2K ')D#)Q.K $) * 	" !GN+a//'E! *" #"    c                    [        U 5      [        U5      :X  d   eSnSn[        [        U 5      5       H  nX   nX&   nUS:X  a  US-  nUS-  nM  US:X  a0  [        R                  S[	        U5      -   S-   [	        U5      -   5        XG[
        R                  " X7-  5      -  -  nXW[
        R                  " X-  5      -  -  nM     XE-
  U-  $ )z
Calculate the entropy.
r   zFatal error!, idx = z qindex = 0! p_idx = )r   r
   _loggererrorstrmathlog)	reference_distr_PP_sumcandidate_distr_QQ_sumtmp_sum1tmp_sum2r   p_idxq_idxs	            r   safe_entropyr'   7   s      !S):%;;;;HHS*+,!&!&A:MHMHz*#h-. %j! %-!899H%-!899H -  5((r   c                 6   U R                   S:X  d   eU R                  S   n[        US-
  S-  5      nSUS-
  -  S-
  n[        R                  " [        R
                  " U 5      R                  5       5      nSnSnSn	[        XC5       H  n
U SU
 R                  5       n[	        X
S 5      nXS-
     S:X  a  M0  XS-
  ==   U-  ss'   USS nU SU
 R                  5       n[        X-  5      nS/U-  nSnUn[        U5       H.  n[	        UUU 5      UU'   UU-  nUU-  nUS-   US-
  :X  d  M,  U
nM0     [        UU5      n[	        U5      n[        XUU5      nU	(       d  UnU
nSn	M  UU:  a  UnU
nM  M     US:X  a  US:  a  X   S:X  a  US-  nM   UnUS-   U-  $ )z
Using the KL-divergence method to get the more precise threshold.

Args:
    hist(List): The hist of the tensor.
    bin_width(float): The bin width for the hist.
    bits(int): The quantization bits.
r   r   g      ?r   FNT)ndimshaper	   npsumarrayravelr
   tolistr   r'   )hist	bin_widthbits	hist_binsstarting_iterquant_ranger    min_kl_divergencemin_kl_index	kl_initedir   outliers_countreference_distr_binsr!   r   candidate_distr_Q_quantizedr   r   r   r"   kl_divergences                         r   cal_kl_thresholdr>   Q   s    99>>

1IQ#-.Mq/A%KFF288D>'')*ELI=, 1I,,.T"XU#q(a% N2 03 1I,,.ao.'(cK&7#%C/2!'%00', &G_$EaK!O+ & 2')=
 %&$&7
  -LI.. -LG -H qa"a'"$3)++r   )loggingr   numpyr+   
log_helperr   __name__INFOr   r   r'   r>    r   r   <module>rE      s8       #
gll H
#8)4?,r   