Caffe2 - C++ API
A deep learning, cross platform ML framework
pybind.cc
1 #include <pybind11/pybind11.h>
2 #include "activation_distribution_observer.h"
3 #include "caffe2_dnnlowp_utils.h"
4 
5 PYBIND11_MODULE(dnnlowp_pybind11, m) {
6  using namespace std;
7  using namespace caffe2;
8 
9  m.def("ClearNetObservers", []() { ClearGlobalNetObservers(); });
10 
11  m.def(
12  "ObserveMinMaxOfOutput",
13  [](const string& min_max_file_name, int dump_freq) {
14  AddGlobalNetObserverCreator(
15  [dump_freq, min_max_file_name](NetBase* net) {
16  return make_unique<OutputMinMaxNetObserver>(
17  net, min_max_file_name, dump_freq);
18  });
19  },
20  pybind11::arg("min_max_file_name"),
21  pybind11::arg("dump_freq") = -1);
22 
23  m.def(
24  "ObserveHistogramOfOutput",
25  [](const string& out_file_name, int dump_freq, bool mul_nets) {
26  AddGlobalNetObserverCreator(
27  [out_file_name, dump_freq, mul_nets](NetBase* net) {
28  return make_unique<HistogramNetObserver>(
29  net, out_file_name, 2048, dump_freq, mul_nets);
30  });
31  },
32  pybind11::arg("out_file_name"),
33  pybind11::arg("dump_freq") = -1,
34  pybind11::arg("mul_nets") = false);
35 
36  m.def(
37  "RegisterQuantizationParams",
38  [](const string& min_max_file_name,
39  bool is_weight,
40  const string& qparams_output_file_name) {
41  AddGlobalNetObserverCreator([min_max_file_name,
42  is_weight,
43  qparams_output_file_name](NetBase* net) {
44  return make_unique<RegisterQuantizationParamsNetObserver>(
45  net, min_max_file_name, is_weight, qparams_output_file_name);
46  });
47  },
48  pybind11::arg("min_max_file_name"),
49  pybind11::arg("is_weight") = false,
50  pybind11::arg("qparams_output_file_name") = "");
51 
52  m.def(
53  "RegisterQuantizationParamsWithHistogram",
54  [](const string& histogram_file_name,
55  bool is_weight,
56  const string& qparams_output_file_name) {
57  AddGlobalNetObserverCreator([histogram_file_name,
58  is_weight,
59  qparams_output_file_name](NetBase* net) {
60  return make_unique<
62  net, histogram_file_name, is_weight, qparams_output_file_name);
63  });
64  },
65  pybind11::arg("histogram_file_name"),
66  pybind11::arg("is_weight") = false,
67  pybind11::arg("qparams_output_file_name") = "");
68 
69  m.def(
70  "AddScaleZeroOffsetArgumentsWithHistogram",
71  [](const pybind11::bytes& net_def_bytes,
72  const string& histogram_file_name) {
73  NetDef def;
74  CAFFE_ENFORCE(
75  ParseProtoFromLargeString(net_def_bytes.cast<string>(), &def));
76  pybind11::gil_scoped_release g;
77 
78  string protob;
79  auto transformed_net =
80  dnnlowp::AddScaleZeroOffsetArgumentsWithHistogram(
81  def, histogram_file_name);
82 
83  CAFFE_ENFORCE(transformed_net.SerializeToString(&protob));
84  return pybind11::bytes(protob);
85  });
86 }
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
Definition: blob.h:13
Set quantization parameters of operators based on min/max collected from OutputMinMaxObserver.