File: p99.cc

package info (click to toggle)
pytorch 1.7.1-7
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 80,340 kB
  • sloc: cpp: 670,830; python: 343,991; ansic: 67,845; asm: 5,503; sh: 2,924; java: 2,888; xml: 266; makefile: 244; ruby: 148; yacc: 144; objc: 51; lex: 44
file content (63 lines) | stat: -rw-r--r-- 1,733 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "caffe2/core/logging.h"
#include "l2_minimization.h"

#include <cassert>
#include <cmath>

namespace dnnlowp {

TensorQuantizationParams P99::ChooseQuantizationParams(
    const Histogram& hist,
    bool preserve_sparsity,
    int precision) {
  float min, max;
  std::vector<float> bins_f(
      dnnlowp::adjust_hist_to_include_zero(hist, &min, &max));
  int nbins = bins_f.size();
  CAFFE_ENFORCE(min <= 0.f);
  CAFFE_ENFORCE(max >= 0.f);
  float org_max = max;
  float org_min = min;
  float bin_width = (max - min) / nbins;
  int zero_bin = round(-min / bin_width);

  int best_width = 0;
  double total_sum = 0;
  for (int i = 0; i < nbins; ++i) {
    total_sum += bins_f[i];
  }
  double sum = 0;
  std::vector<double> CDF(nbins, 0.f);
  for (int i = 0; i < nbins; ++i) {
    sum += bins_f[i];
    CDF[i] = (double)sum / total_sum;
  }
  CAFFE_ENFORCE(threshold_ > 0.5 && threshold_ < 1);
  double left_quantile = (1.0f - threshold_) / 2.0f;
  double right_quantile = 1.0f - left_quantile;
  int i_begin = 0;
  int i_end = nbins - 2;
  bool finished = false;
  while (i_begin <= i_end && !finished) {
    finished = true;
    if (CDF[i_begin] < left_quantile) {
      i_begin++;
      finished = false;
    }
    if (CDF[i_end] > right_quantile) {
      finished = false;
      i_end--;
    }
  }
  min = i_begin * bin_width + org_min;
  max = (i_end + 2) * bin_width + org_min;

  VLOG(2) << "Org min " << org_min << " org max " << org_max << " found min "
          << min << " max " << max;

  QuantizationFactory* qfactory = QuantizationFactory::GetDefaultInstance();
  return qfactory->ChooseQuantizationParams(
      min, max, precision, preserve_sparsity);
} // ChooseQuantizationParams

} // namespace dnnlowp