aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/python/interpreter_wrapper/interpreter_wrapper.cc
blob: b283551c45d3d75aecb50043f1c7486b3345118d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/
#include "tensorflow/contrib/lite/python/interpreter_wrapper/interpreter_wrapper.h"

#include <string>

#include "absl/memory/memory.h"
#include "tensorflow/contrib/lite/interpreter.h"
#include "tensorflow/contrib/lite/kernels/register.h"
#include "tensorflow/contrib/lite/model.h"
#include "tensorflow/core/platform/logging.h"

// Disallow Numpy 1.7 deprecated symbols.
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <Python.h>

#include "numpy/arrayobject.h"
#include "numpy/ufuncobject.h"

#if PY_MAJOR_VERSION >= 3
#define PY_TO_CPPSTRING PyBytes_AsStringAndSize
#define CPP_TO_PYSTRING PyBytes_FromStringAndSize
#else
#define PY_TO_CPPSTRING PyString_AsStringAndSize
#define CPP_TO_PYSTRING PyString_FromStringAndSize
#endif

namespace tflite {
namespace interpreter_wrapper {

namespace {

// Calls PyArray's initialization to initialize all the API pointers. Note that
// this usage implies only this translation unit can use the pointers. See
// tensorflow/python/core/numpy.cc for a strategy if we ever need to extend
// this further.
void ImportNumpy() { import_array1(); }

std::unique_ptr<tflite::Interpreter> CreateInterpreter(
    const tflite::FlatBufferModel* model,
    const tflite::ops::builtin::BuiltinOpResolver& resolver) {
  if (!model) {
    return nullptr;
  }

  ImportNumpy();

  std::unique_ptr<tflite::Interpreter> interpreter;
  tflite::InterpreterBuilder(*model, resolver)(&interpreter);
  if (interpreter) {
    for (const int input_index : interpreter->inputs()) {
      const TfLiteTensor* tensor = interpreter->tensor(input_index);
      CHECK(tensor);
      const TfLiteIntArray* dims = tensor->dims;
      if (!dims) {
        continue;
      }

      std::vector<int> input_dims(dims->data, dims->data + dims->size);
      interpreter->ResizeInputTensor(input_index, input_dims);
    }
  }
  return interpreter;
}

int TfLiteTypeToPyArrayType(TfLiteType tf_lite_type) {
  switch (tf_lite_type) {
    case kTfLiteFloat32:
      return NPY_FLOAT32;
    case kTfLiteInt32:
      return NPY_INT32;
    case kTfLiteInt16:
      return NPY_INT16;
    case kTfLiteUInt8:
      return NPY_UINT8;
    case kTfLiteInt64:
      return NPY_INT64;
    case kTfLiteString:
      return NPY_OBJECT;
    case kTfLiteBool:
      return NPY_BOOL;
    case kTfLiteNoType:
      return -1;
  }
  LOG(ERROR) << "Unknown TfLiteType " << tf_lite_type;
  return -1;
}

TfLiteType TfLiteTypeFromPyArray(PyArrayObject* array) {
  int pyarray_type = PyArray_TYPE(array);
  switch (pyarray_type) {
    case NPY_FLOAT32:
      return kTfLiteFloat32;
    case NPY_INT32:
      return kTfLiteInt32;
    case NPY_INT16:
      return kTfLiteInt16;
    case NPY_UINT8:
      return kTfLiteUInt8;
    case NPY_INT64:
      return kTfLiteInt64;
    case NPY_BOOL:
      return kTfLiteBool;
    case NPY_OBJECT:
    case NPY_STRING:
    case NPY_UNICODE:
      return kTfLiteString;
  }
  LOG(ERROR) << "Unknown PyArray dtype " << pyarray_type;
  return kTfLiteNoType;
}

struct PyDecrefDeleter {
  void operator()(PyObject* p) const { Py_DECREF(p); }
};

PyObject* PyArrayFromIntVector(const int* data, npy_intp size) {
  void* pydata = malloc(size * sizeof(int));
  memcpy(pydata, data, size * sizeof(int));
  return PyArray_SimpleNewFromData(1, &size, NPY_INT32, pydata);
}

PyObject* PyTupleFromQuantizationParam(const TfLiteQuantizationParams& param) {
  PyObject* result = PyTuple_New(2);
  PyTuple_SET_ITEM(result, 0, PyFloat_FromDouble(param.scale));
  PyTuple_SET_ITEM(result, 1, PyLong_FromLong(param.zero_point));
  return result;
}

}  // namespace

InterpreterWrapper::InterpreterWrapper(
    std::unique_ptr<tflite::FlatBufferModel> model)
    : model_(std::move(model)),
      resolver_(absl::make_unique<tflite::ops::builtin::BuiltinOpResolver>()),
      interpreter_(CreateInterpreter(model_.get(), *resolver_)) {}

InterpreterWrapper::~InterpreterWrapper() {}

bool InterpreterWrapper::AllocateTensors() {
  if (!interpreter_) {
    LOG(ERROR) << "Cannot allocate tensors: invalid interpreter.";
    return false;
  }

  if (interpreter_->AllocateTensors() != kTfLiteOk) {
    LOG(ERROR) << "Unable to allocate tensors.";
    return false;
  }

  return true;
}

bool InterpreterWrapper::Invoke() {
  return interpreter_ ? (interpreter_->Invoke() == kTfLiteOk) : false;
}

PyObject* InterpreterWrapper::InputIndices() const {
  PyObject* np_array = PyArrayFromIntVector(interpreter_->inputs().data(),
                                            interpreter_->inputs().size());

  return PyArray_Return(reinterpret_cast<PyArrayObject*>(np_array));
}

PyObject* InterpreterWrapper::OutputIndices() const {
  PyObject* np_array = PyArrayFromIntVector(interpreter_->outputs().data(),
                                            interpreter_->outputs().size());

  return PyArray_Return(reinterpret_cast<PyArrayObject*>(np_array));
}

bool InterpreterWrapper::ResizeInputTensor(int i, PyObject* value) {
  if (!interpreter_) {
    LOG(ERROR) << "Invalid interpreter.";
    return false;
  }

  std::unique_ptr<PyObject, PyDecrefDeleter> array_safe(
      PyArray_FromAny(value, nullptr, 0, 0, NPY_ARRAY_CARRAY, nullptr));
  if (!array_safe) {
    LOG(ERROR) << "Failed to convert value into readable tensor.";
    return false;
  }

  PyArrayObject* array = reinterpret_cast<PyArrayObject*>(array_safe.get());

  if (PyArray_NDIM(array) != 1) {
    LOG(ERROR) << "Expected 1-D defining input shape.";
    return false;
  }

  if (PyArray_TYPE(array) != NPY_INT32) {
    LOG(ERROR) << "Shape must be an int32 array";
    return false;
  }

  std::vector<int> dims(PyArray_SHAPE(array)[0]);
  memcpy(dims.data(), PyArray_BYTES(array), dims.size() * sizeof(int));

  return (interpreter_->ResizeInputTensor(i, dims) == kTfLiteOk);
}

std::string InterpreterWrapper::TensorName(int i) const {
  if (!interpreter_ || i >= interpreter_->tensors_size() || i < 0) {
    return "";
  }

  const TfLiteTensor* tensor = interpreter_->tensor(i);
  return tensor->name;
}

PyObject* InterpreterWrapper::TensorType(int i) const {
  if (!interpreter_ || i >= interpreter_->tensors_size() || i < 0) {
    return nullptr;
  }

  const TfLiteTensor* tensor = interpreter_->tensor(i);
  int typenum = TfLiteTypeToPyArrayType(tensor->type);
  return PyArray_TypeObjectFromType(typenum);
}

PyObject* InterpreterWrapper::TensorSize(int i) const {
  if (!interpreter_ || i >= interpreter_->tensors_size() || i < 0) {
    Py_INCREF(Py_None);
    return Py_None;
  }

  const TfLiteTensor* tensor = interpreter_->tensor(i);
  PyObject* np_array =
      PyArrayFromIntVector(tensor->dims->data, tensor->dims->size);

  return PyArray_Return(reinterpret_cast<PyArrayObject*>(np_array));
}

PyObject* InterpreterWrapper::TensorQuantization(int i) const {
  if (!interpreter_ || i >= interpreter_->tensors_size() || i < 0) {
    Py_INCREF(Py_None);
    return Py_None;
  }

  const TfLiteTensor* tensor = interpreter_->tensor(i);
  return PyTupleFromQuantizationParam(tensor->params);
}

bool InterpreterWrapper::SetTensor(int i, PyObject* value) {
  if (!interpreter_) {
    LOG(ERROR) << "Invalid interpreter.";
    return false;
  }

  if (i >= interpreter_->tensors_size()) {
    LOG(ERROR) << "Invalid tensor index: " << i << " exceeds max tensor index "
               << interpreter_->tensors_size();
    return false;
  }

  std::unique_ptr<PyObject, PyDecrefDeleter> array_safe(
      PyArray_FromAny(value, nullptr, 0, 0, NPY_ARRAY_CARRAY, nullptr));
  if (!array_safe) {
    LOG(ERROR) << "Failed to convert value into readable tensor.";
    return false;
  }

  PyArrayObject* array = reinterpret_cast<PyArrayObject*>(array_safe.get());
  const TfLiteTensor* tensor = interpreter_->tensor(i);

  if (TfLiteTypeFromPyArray(array) != tensor->type) {
    LOG(ERROR) << "Cannot set tensor:"
               << " Got tensor of type " << TfLiteTypeFromPyArray(array)
               << " but expected type " << tensor->type << " for input " << i;
    return false;
  }

  if (PyArray_NDIM(array) != tensor->dims->size) {
    LOG(ERROR) << "Cannot set tensor: Dimension mismatch";
    return false;
  }

  for (int j = 0; j < PyArray_NDIM(array); j++) {
    if (tensor->dims->data[j] != PyArray_SHAPE(array)[j]) {
      LOG(ERROR) << "Cannot set tensor: Dimension mismatch";
      return false;
    }
  }

  size_t size = PyArray_NBYTES(array);
  DCHECK_EQ(size, tensor->bytes);
  memcpy(tensor->data.raw, PyArray_DATA(array), size);
  return true;
}

namespace {

PyObject* CheckGetTensorArgs(Interpreter* interpreter, int tensor_index,
                             TfLiteTensor** tensor, int* type_num) {
  if (!interpreter) {
    LOG(ERROR) << "Invalid interpreter.";
    Py_INCREF(Py_None);
    return Py_None;
  }

  if (tensor_index >= interpreter->tensors_size() || tensor_index < 0) {
    LOG(ERROR) << "Invalid tensor index: " << tensor_index
               << " exceeds max tensor index " << interpreter->inputs().size();
    Py_INCREF(Py_None);
    return Py_None;
  }

  *tensor = interpreter->tensor(tensor_index);
  if ((*tensor)->bytes == 0) {
    LOG(ERROR) << "Invalid tensor size";
    Py_INCREF(Py_None);
    return Py_None;
  }

  *type_num = TfLiteTypeToPyArrayType((*tensor)->type);
  if (*type_num == -1) {
    LOG(ERROR) << "Unknown tensor type " << (*tensor)->type;
    Py_INCREF(Py_None);
    return Py_None;
  }

  if (!(*tensor)->data.raw) {
    LOG(ERROR) << "Tensor data is null.";
    Py_INCREF(Py_None);
    return Py_None;
  }

  return nullptr;
}

}  // namespace

PyObject* InterpreterWrapper::GetTensor(int i) const {
  // Sanity check accessor
  TfLiteTensor* tensor = nullptr;
  int type_num = 0;
  if (PyObject* pynone_or_nullptr =
          CheckGetTensorArgs(interpreter_.get(), i, &tensor, &type_num)) {
    return pynone_or_nullptr;
  }
  std::vector<npy_intp> dims(tensor->dims->data,
                             tensor->dims->data + tensor->dims->size);
  // Make a buffer copy but we must tell Numpy It owns that data or else
  // it will leak.
  void* data = malloc(tensor->bytes);
  if (!data) {
    LOG(ERROR) << "Malloc to copy tensor failed.";
    Py_INCREF(Py_None);
    return Py_None;
  }
  memcpy(data, tensor->data.raw, tensor->bytes);
  PyObject* np_array =
      PyArray_SimpleNewFromData(dims.size(), dims.data(), type_num, data);
  PyArray_ENABLEFLAGS(reinterpret_cast<PyArrayObject*>(np_array),
                      NPY_ARRAY_OWNDATA);
  return PyArray_Return(reinterpret_cast<PyArrayObject*>(np_array));
}

PyObject* InterpreterWrapper::tensor(PyObject* base_object, int i) {
  // Sanity check accessor
  TfLiteTensor* tensor = nullptr;
  int type_num = 0;
  if (PyObject* pynone_or_nullptr =
          CheckGetTensorArgs(interpreter_.get(), i, &tensor, &type_num)) {
    return pynone_or_nullptr;
  }

  std::vector<npy_intp> dims(tensor->dims->data,
                             tensor->dims->data + tensor->dims->size);
  PyArrayObject* np_array =
      reinterpret_cast<PyArrayObject*>(PyArray_SimpleNewFromData(
          dims.size(), dims.data(), type_num, tensor->data.raw));
  Py_INCREF(base_object);  // SetBaseObject steals, so we need to add.
  PyArray_SetBaseObject(np_array, base_object);
  return PyArray_Return(np_array);
}

InterpreterWrapper* InterpreterWrapper::CreateWrapperCPPFromFile(
    const char* model_path) {
  std::unique_ptr<tflite::FlatBufferModel> model =
      tflite::FlatBufferModel::BuildFromFile(model_path);
  return model ? new InterpreterWrapper(std::move(model)) : nullptr;
}

InterpreterWrapper* InterpreterWrapper::CreateWrapperCPPFromBuffer(
    PyObject* data) {
  char * buf = nullptr;
  Py_ssize_t length;
  if (PY_TO_CPPSTRING(data, &buf, &length) == -1) {
    return nullptr;
  }
  std::unique_ptr<tflite::FlatBufferModel> model =
      tflite::FlatBufferModel::BuildFromBuffer(buf, length);
  return model ? new InterpreterWrapper(std::move(model)) : nullptr;
}

}  // namespace interpreter_wrapper
}  // namespace tflite