Caffe2 - C++ API
A deep learning, cross platform ML framework
l2_minimization_approx_example.cc
1 #include "caffe2/core/logging.h"
2 #include "l2_minimization.h"
3 
4 #include <fstream>
5 #include <iostream>
6 #include <sstream>
7 
8 using namespace std;
9 using namespace dnnlowp;
10 
11 int main(int argc, const char* argv[]) {
12  if (argc < 3) {
13  cerr << "Usage: " << argv[0]
14  << " in_file out_file [preserve_sparsity] [precision]" << endl;
15  return -1;
16  }
17 
18  ifstream in(argv[1]);
19  ofstream out(argv[2]);
20  bool preserve_sparsity = argc >= 4 ? atoi(argv[3]) : false;
21  int precision = argc >= 5 ? atoi(argv[4]) : 8;
22 
23  vector<tuple<int, string, int, string>> infos;
24  vector<Histogram> hists;
25 
26  string line;
27  while (getline(in, line)) {
28  istringstream ist(line);
29 
30  int op_index, output_index;
31  string op_type, tensor_name;
32  float min, max;
33  int nbins;
34 
35  ist >> op_index >> op_type >> output_index >> tensor_name >> min >> max >>
36  nbins;
37  infos.push_back(tuple<int, string, int, string>(
38  op_index, op_type, output_index, tensor_name));
39 
40  vector<uint64_t> bins;
41  for (int i = 0; i < nbins; ++i) {
42  uint64_t cnt;
43  ist >> cnt;
44  bins.push_back(cnt);
45  }
46  assert(bins.size() == nbins);
47 
48  Histogram hist = Histogram(min, max, bins);
49  hists.emplace_back(min, max, bins);
50  }
51 
52  vector<TensorQuantizationParams> qparams(hists.size());
53 
54  for (int i = 0; i < hists.size(); ++i) {
56  hists[i], preserve_sparsity, precision);
57  }
58 
59  for (int i = 0; i < qparams.size(); ++i) {
60  VLOG(2) << std::get<2>(infos[i]);
61  out << std::get<0>(infos[i]) << " " << std::get<1>(infos[i]) << " "
62  << std::get<2>(infos[i]) << " " << std::get<3>(infos[i]) << " "
63  << qparams[i].Min() << " " << qparams[i].Max() << endl;
64  }
65 
66  return 0;
67 }
A quantization scheme that minimizes L2 norm of quantization error.
TensorQuantizationParams NonlinearQuantizationParamsSearch(const Histogram &hist, bool preserve_sparsity=false, int precision=8)
Faster approximate search.
bin_width = (max - min)/nbins ith bin (zero-based indexing) contains [i*bin_width, (i+1)*bin_width) with an exception that (nbins - 1)th bin contains [(nbins-1)*bin_width, nbins*bin_width]