1 #include "caffe2/core/logging.h" 2 #include "l2_minimization.h" 9 TensorQuantizationParams P99::ChooseQuantizationParams(
10 const Histogram& hist,
11 bool preserve_sparsity,
13 assert(preserve_sparsity);
15 const std::vector<uint64_t> bins = *hist.GetHistogram();
16 int nbins = bins.size();
17 float min = hist.Min(), max = hist.Max();
20 float bin_width = (max - min) / nbins;
21 int zero_bin = ceil(-min / bin_width);
25 for (
int i = 0; i < nbins; ++i) {
29 for (
int width = 0; width < nbins; ++width) {
35 i_begin = std::max(0, zero_bin - width);
36 i_end = std::min(nbins - 1, zero_bin + width);
39 double selected_sum = 0;
40 for (
int i = i_begin; i <= i_end; ++i) {
41 selected_sum += bins[i];
44 if (selected_sum / total_sum >= 0.99) {
52 max = hist.Min() + bin_width * best_width;
54 min = hist.Min() + bin_width * (zero_bin - best_width);
55 max = hist.Min() + bin_width * (zero_bin + best_width + 1);
59 return qfactory->ChooseQuantizationParams(min, max);
static QuantizationFactory * GetDefaultInstance()
Get the default factory whose policy is determined by gflags.