1 /* Copyright 2018 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_INTERPRETER_WRAPPER_INTERPRETER_WRAPPER_H_
16 #define TENSORFLOW_LITE_PYTHON_INTERPRETER_WRAPPER_INTERPRETER_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 
42 class PythonErrorReporter;
43 
44 class InterpreterWrapper {
45  public:
46   // SWIG caller takes ownership of pointer.
47   static InterpreterWrapper* CreateWrapperCPPFromFile(const char* model_path,
48                                                       std::string* error_msg);
49 
50   // SWIG caller takes ownership of pointer.
51   static InterpreterWrapper* CreateWrapperCPPFromBuffer(PyObject* data,
52                                                         std::string* error_msg);
53 
54   ~InterpreterWrapper();
55   PyObject* AllocateTensors();
56   PyObject* Invoke();
57 
58   PyObject* InputIndices() const;
59   PyObject* OutputIndices() const;
60   PyObject* ResizeInputTensor(int i, PyObject* value);
61 
62   int NumTensors() const;
63   std::string TensorName(int i) const;
64   PyObject* TensorType(int i) const;
65   PyObject* TensorSize(int i) const;
66   PyObject* TensorQuantization(int i) const;
67   PyObject* SetTensor(int i, PyObject* value);
68   PyObject* GetTensor(int i) const;
69   PyObject* ResetVariableTensors();
70 
71   // Returns a reference to tensor index i as a numpy array. The base_object
72   // should be the interpreter object providing the memory.
73   PyObject* tensor(PyObject* base_object, int i);
74 
75  private:
76   // Helper function to construct an `InterpreterWrapper` object.
77   // It only returns InterpreterWrapper if it can construct an `Interpreter`.
78   // Otherwise it returns `nullptr`.
79   static InterpreterWrapper* CreateInterpreterWrapper(
80       std::unique_ptr<tflite::FlatBufferModel> model,
81       std::unique_ptr<PythonErrorReporter> error_reporter,
82       std::string* error_msg);
83 
84   InterpreterWrapper(
85       std::unique_ptr<tflite::FlatBufferModel> model,
86       std::unique_ptr<PythonErrorReporter> error_reporter,
87       std::unique_ptr<tflite::ops::builtin::BuiltinOpResolver> resolver,
88       std::unique_ptr<tflite::Interpreter> interpreter);
89 
90   // InterpreterWrapper is not copyable or assignable. We avoid the use of
91   // InterpreterWrapper() = delete here for SWIG compatibility.
92   InterpreterWrapper();
93   InterpreterWrapper(const InterpreterWrapper& rhs);
94 
95   // The public functions which creates `InterpreterWrapper` should ensure all
96   // these member variables are initialized successfully. Otherwise it should
97   // report the error and return `nullptr`.
98   const std::unique_ptr<tflite::FlatBufferModel> model_;
99   const std::unique_ptr<PythonErrorReporter> error_reporter_;
100   const std::unique_ptr<tflite::ops::builtin::BuiltinOpResolver> resolver_;
101   const std::unique_ptr<tflite::Interpreter> interpreter_;
102 };
103 
104 }  // namespace interpreter_wrapper
105 }  // namespace tflite
106 
107 #endif  // TENSORFLOW_LITE_PYTHON_INTERPRETER_WRAPPER_INTERPRETER_WRAPPER_H_
108