1 /* Copyright 2019 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 #ifndef TENSORFLOW_LITE_PYTHON_OPTIMIZE_CALIBRATION_WRAPPER_H_
16 #define TENSORFLOW_LITE_PYTHON_OPTIMIZE_CALIBRATION_WRAPPER_H_
17 
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 // Place `<locale>` before <Python.h> to avoid build failures in macOS.
23 #include <locale>
24 
25 // The empty line above is on purpose as otherwise clang-format will
26 // automatically move <Python.h> before <locale>.
27 #include <Python.h>
28 
29 // We forward declare TFLite classes here to avoid exposing them to SWIG.
30 namespace tflite {
31 namespace ops {
32 namespace builtin {
33 class BuiltinOpResolver;
34 }  // namespace builtin
35 }  // namespace ops
36 
37 class FlatBufferModel;
38 class Interpreter;
39 
40 namespace interpreter_wrapper {
41 class PythonErrorReporter;
42 }  // namespace interpreter_wrapper
43 
44 namespace optimize {
45 namespace calibration {
46 class CalibrationReader;
47 }  // namespace calibration
48 }  // namespace optimize
49 
50 namespace calibration_wrapper {
51 
52 class CalibrationWrapper {
53  public:
54   // SWIG caller takes ownership of pointer.
55   static CalibrationWrapper* CreateWrapperCPPFromBuffer(PyObject* data);
56   ~CalibrationWrapper();
57 
58   PyObject* Prepare();
59 
60   PyObject* FeedTensor(PyObject* input_value);
61 
62   PyObject* QuantizeModel();
63 
64  private:
65   // CalibrationWrapper is not copyable or assignable. We avoid the use of
66   // CalibrationWrapper() = delete here for SWIG compatibility.
67   CalibrationWrapper(
68       std::unique_ptr<tflite::Interpreter> interpreter,
69       std::unique_ptr<tflite::ops::builtin::BuiltinOpResolver> resolver,
70       std::unique_ptr<tflite::interpreter_wrapper::PythonErrorReporter>
71           error_reporter,
72       std::unique_ptr<tflite::FlatBufferModel> model,
73       std::unique_ptr<tflite::optimize::calibration::CalibrationReader> reader);
74 
75   CalibrationWrapper(const CalibrationWrapper& rhs);
76 
77   PyObject* SetTensor(int index, PyObject* value);
78 
79   std::unique_ptr<tflite::Interpreter> interpreter_;
80   std::unique_ptr<tflite::interpreter_wrapper::PythonErrorReporter>
81       error_reporter_;
82   std::unique_ptr<tflite::ops::builtin::BuiltinOpResolver> resolver_;
83   std::unique_ptr<tflite::FlatBufferModel> model_;
84   std::unique_ptr<tflite::optimize::calibration::CalibrationReader> reader_;
85 };
86 
87 }  // namespace calibration_wrapper
88 }  // namespace tflite
89 
90 #endif  // TENSORFLOW_LITE_PYTHON_OPTIMIZE_CALIBRATION_WRAPPER_H_
91