@@ -503,22 +503,32 @@ def get_numpy_bits(arr: numpy.ndarray) -> int:
503503def get_numpy_quantization_level (arr : numpy .ndarray ) -> int :
504504 """return the quantization precision of the array"""
505505 dtype_int_match = re .search (r"\d+" , str (arr .dtype ))
506- if dtype_int_match .group () and int (dtype_int_match .group ()) < 16 :
506+ if (
507+ dtype_int_match .group ()
508+ and int (dtype_int_match .group ()) < 16
509+ and numpy .unique (arr ).size > 1
510+ ):
507511 # log2 of the difference between the max and the min, convert float to int
508512 return int (numpy .ceil (numpy .log2 (numpy .max (arr ) - numpy .min (arr ))))
509513
510514 return int (dtype_int_match .group ())
511515
512516
513- def get_numpy_distribution_statistics (arr : numpy .ndarray ) -> Tuple [int , int ]:
517+ def get_numpy_distribution_statistics (
518+ arr : numpy .ndarray , epsilon : float = 1e-10
519+ ) -> Tuple [int , int ]:
514520 """Remove dimensions and compute the statistics"""
515521 flatten_arr = arr .flatten ()
516522 mean_val = numpy .mean (flatten_arr )
517523 std_dev = numpy .std (flatten_arr )
518524 n = len (flatten_arr )
519525
520- skewness = (numpy .sum ((flatten_arr - mean_val ) ** 3 ) / n ) / (std_dev ** 3 )
521- kurtosis = (numpy .sum ((flatten_arr - mean_val ) ** 4 ) / n ) / (std_dev ** 4 ) - 3
526+ skewness = (numpy .sum ((flatten_arr - mean_val ) ** 3 ) / n ) / max (
527+ std_dev ** 3 , epsilon
528+ )
529+ kurtosis = (numpy .sum ((flatten_arr - mean_val ) ** 4 ) / n ) / max (
530+ std_dev ** 4 , epsilon
531+ ) - 3
522532
523533 return skewness , kurtosis
524534
0 commit comments