• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include <functional>
17 #include <string>
18 
19 #include "pybind11/functional.h"
20 #include "pybind11/pybind11.h"
21 #include "pybind11/pytypes.h"
22 #include "pybind11/stl.h"
23 #include "tensorflow/lite/python/optimize/calibration_wrapper.h"
24 #include "tensorflow/python/lib/core/pybind11_lib.h"
25 
26 namespace py = pybind11;
27 using tflite::calibration_wrapper::AddIntermediateTensors;
28 using tflite::calibration_wrapper::CalibrationWrapper;
29 
PYBIND11_MODULE(_pywrap_tensorflow_lite_calibration_wrapper,m)30 PYBIND11_MODULE(_pywrap_tensorflow_lite_calibration_wrapper, m) {
31   m.doc() = R"pbdoc(
32     _pywrap_tensorflow_lite_calibration_wrapper
33     -----
34   )pbdoc";
35   m.def("AddIntermediateTensors", [](py::handle& data) {
36     return tensorflow::PyoOrThrow(AddIntermediateTensors(data.ptr()));
37   });
38   py::class_<CalibrationWrapper>(m, "CalibrationWrapper")
39       .def(py::init([](py::handle& data,
40                        const std::vector<std::string>& registerers_by_name,
41                        const std::vector<std::function<void(uintptr_t)>>&
42                            registerers_by_func) {
43         std::string error;
44         auto* wrapper = ::CalibrationWrapper::CreateWrapperCPPFromBuffer(
45             data.ptr(), registerers_by_name, registerers_by_func, &error);
46         if (!wrapper) {
47           throw std::invalid_argument(error);  // throws ValueError in Python
48         }
49         return wrapper;
50       }))
51       .def("Prepare",
52            [](CalibrationWrapper& self, py::handle& input_shapes,
53               std::string signature_key) {
54              return tensorflow::PyoOrThrow(
55                  self.Prepare(input_shapes.ptr(), signature_key));
56            })
57       .def("Prepare",
58            [](CalibrationWrapper& self, py::handle& input_shapes) {
59              return tensorflow::PyoOrThrow(self.Prepare(input_shapes.ptr()));
60            })
61       .def("Prepare",
62            [](CalibrationWrapper& self, std::string signature_key) {
63              return tensorflow::PyoOrThrow(self.Prepare(signature_key));
64            })
65       .def("Prepare",
66            [](CalibrationWrapper& self) {
67              return tensorflow::PyoOrThrow(self.Prepare());
68            })
69       .def("FeedTensor",
70            [](CalibrationWrapper& self, py::handle& input_value,
71               std::string signature_key) {
72              return tensorflow::PyoOrThrow(
73                  self.FeedTensor(input_value.ptr(), signature_key));
74            })
75       .def("FeedTensor",
76            [](CalibrationWrapper& self, py::handle& input_value) {
77              return tensorflow::PyoOrThrow(self.FeedTensor(input_value.ptr()));
78            })
79       .def("QuantizeModel",
80            [](CalibrationWrapper& self, int input_py_type, int output_py_type,
81               bool allow_float, int activations_py_type, int bias_py_type,
82               bool disable_per_channel) {
83              return tensorflow::PyoOrThrow(self.QuantizeModel(
84                  input_py_type, output_py_type, allow_float,
85                  activations_py_type, bias_py_type, disable_per_channel));
86            })
87       .def("QuantizeModel",
88            [](CalibrationWrapper& self, int input_py_type, int output_py_type,
89               bool allow_float, const char* operator_output_name) {
90              return tensorflow::PyoOrThrow(
91                  self.QuantizeModel(input_py_type, output_py_type, allow_float,
92                                     operator_output_name));
93            })
94       .def("Calibrate", [](CalibrationWrapper& self) {
95         return tensorflow::PyoOrThrow(self.Calibrate());
96       });
97 }
98