You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2019/11/29 05:25:43 UTC

[GitHub] [incubator-tvm] FrozenGene commented on a change in pull request #4439: [CONTRIB] TFLite Runtime

FrozenGene commented on a change in pull request #4439: [CONTRIB] TFLite Runtime
URL: https://github.com/apache/incubator-tvm/pull/4439#discussion_r351984740
 
 

 ##########
 File path: src/runtime/contrib/tflite/tflite_runtime.cc
 ##########
 @@ -0,0 +1,189 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+
+/*!
+ * \file tflite_runtime.cc
+ */
+#include <tvm/runtime/registry.h>
+#include <tvm/dtype.h>
+#include <tensorflow/lite/interpreter.h>
+#include <tensorflow/lite/kernels/register.h>
+#include <tensorflow/lite/model.h>
+
+
+#include "tflite_runtime.h"
+
+namespace tvm {
+namespace runtime {
+
+#define TVM_DTYPE_DISPATCH(type, DType, ...)            \
+  if (type == Float(64)) {                              \
+    typedef double DType;                               \
+    {__VA_ARGS__}                                       \
+  } else if (type == Float(32)) {                       \
+    typedef float DType;                                \
+    {__VA_ARGS__}                                       \
+  } else if (type == Float(16)) {                       \
+    typedef uint16_t DType;                             \
+    {__VA_ARGS__}                                       \
+  } else if (type == Int(64)) {                         \
+    typedef int64_t DType;                              \
+    {__VA_ARGS__}                                       \
+  } else if (type == Int(32)) {                         \
+    typedef int32_t DType;                              \
+    {__VA_ARGS__}                                       \
+  } else if (type == Int(16)) {                         \
+    typedef int16_t DType;                              \
+    {__VA_ARGS__}                                       \
+  } else if (type == Int(8)) {                          \
+    typedef int8_t DType;                               \
+    {__VA_ARGS__}                                       \
+  } else if (type == UInt(64)) {                        \
+    typedef uint64_t DType;                             \
+    {__VA_ARGS__}                                       \
+  } else if (type == UInt(32)) {                        \
+    typedef uint32_t DType;                             \
+    {__VA_ARGS__}                                       \
+  } else if (type == UInt(16)) {                        \
+    typedef uint16_t DType;                             \
+    {__VA_ARGS__}                                       \
+  } else if (type == UInt(8)) {                         \
+    typedef uint8_t DType;                              \
+    {__VA_ARGS__}                                       \
+  } else {                                              \
+    LOG(FATAL) << "unknown data type " << type;         \
+  }
+
+DataType TfLiteDType2TVMDType(TfLiteType dtype) {
+  switch (dtype) {
+    case kTfLiteFloat32:
+      return Float(32);
+    case kTfLiteInt32:
+      return Int(32);
+    case kTfLiteInt64:
+      return Int(64);
+    case kTfLiteInt16:
+      return Int(16);
+    case kTfLiteInt8:
+      return Int(8);
+    case kTfLiteUInt8:
+      return UInt(8);
+    case kTfLiteFloat16:
+      return Float(16);
+    default:
+      LOG(FATAL) << "tflite data type not support yet: " << dtype;
+      return Float(32);
+  }
+}
+
+
+void TFLiteRuntime::Init(const std::string& tflite_fname,
+                         TVMContext ctx) {
+  std::unique_ptr<tflite::FlatBufferModel> model = tflite::FlatBufferModel::BuildFromFile(tflite_fname.c_str());
+  tflite::ops::builtin::BuiltinOpResolver resolver;
+  tflite::InterpreterBuilder(*model, resolver)(&interpreter_);
+  ctx_ = ctx;
+}
+
+void TFLiteRuntime::AllocateTensors() {
+  interpreter_->AllocateTensors();
+}
+
+void TFLiteRuntime::Invoke() {
+  interpreter_->Invoke();
+}
+
+void TFLiteRuntime::SetInput(int index, DLTensor* data_in) {
+  DataType dtype(data_in->dtype);
+  TVM_DTYPE_DISPATCH(dtype, DType, {
+      DType* dest = interpreter_->typed_input_tensor<DType>(index);
+      DType* src = static_cast<DType*>(data_in->data);
+      CHECK(data_in->strides == NULL);
+      int64_t size = 1;
+      for (int64_t i = 0; i < data_in->ndim; ++i) {
+        size *= data_in->shape[i];
+      }
+      for (int64_t i = 0; i < size; ++i) {
+        dest[i] = src[i];
+      }
+    });
+}
+
+NDArray TFLiteRuntime::GetOutput(int index) const {
+  TfLiteTensor* output = interpreter_->output_tensor(index);
+  DataType dtype = TfLiteDType2TVMDType(output->type);
+  TfLiteIntArray* dims = output->dims;
+  int64_t size = 1;
+  std::vector<int64_t> shape;
+  for (int i = 0; i < dims->size; ++i) {
+    shape.push_back(dims->data[i]);
+    size *= dims->data[i];
+  }
+  
+  NDArray ret = NDArray::Empty(shape, dtype, ctx_);
+  TVM_DTYPE_DISPATCH(dtype, DType, {
+      DType* dest = static_cast<DType*>(ret->data);
+      DType* src = interpreter_->typed_output_tensor<DType>(index);
+      for (int64_t i = 0; i < size; ++i) {
+        dest[i] = src[i];
+      }
+    });
+  return ret;
+}
+
+PackedFunc TFLiteRuntime::GetFunction(
+    const std::string& name,
+    const ObjectPtr<Object>& sptr_to_self) {
+  // Return member functions during query.
+  if (name == "set_input") {
+    return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+        int in_idx = args[0];
+        CHECK_GE(in_idx, 0);
+        this->SetInput(in_idx, args[1]);
+      });
+  } else if (name == "get_output") {
+    return PackedFunc([sptr_to_self, this](TVMArgs args, TVMRetValue* rv) {
+        *rv = this->GetOutput(args[0]);
+      });
+  } else if (name == "invoke") {
 
 Review comment:
   could we change the name from `invoke` into `run`? The api interface would be like our graphruntime, set_input, get_output, run and so on.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services