Skip to content

Commit

Permalink
[Fix][Runtime] Use flatBuffersBuffer_ in EdgeTPURuntime::Init() (#8034)
Browse files Browse the repository at this point in the history
* Use flatBuffersBuffer_ in EdgeTPURuntime::Init()

* Specify target_runtime for tflite_runtime.create()

* Add a comment for describing the dependent TF version
  • Loading branch information
tk1012 authored May 14, 2021
1 parent 76ccd8e commit 3bf65b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/runtime/contrib/edgetpu/edgetpu_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ void EdgeTPURuntime::Init(const std::string& tflite_model_bytes, Device dev) {
const char* buffer = tflite_model_bytes.c_str();
size_t buffer_size = tflite_model_bytes.size();
// Load compiled model as a FlatBufferModel

// According to tflite_runtime.cc, the buffer for tflite::FlatBufferModel
// should be allocated on flatBuffersBuffer_ to make share it must be kept alive
// for interpreters.
flatBuffersBuffer_ = std::unique_ptr<char[]>(new char[buffer_size]);
std::memcpy(flatBuffersBuffer_.get(), buffer, buffer_size);
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromBuffer(buffer, buffer_size);
tflite::FlatBufferModel::BuildFromBuffer(flatBuffersBuffer_.get(), buffer_size);
// Build resolver
tflite::ops::builtin::BuiltinOpResolver resolver;
// Init EdgeTPUContext object
Expand Down
8 changes: 7 additions & 1 deletion tests/python/contrib/test_edgetpu_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

# import tflite_runtime.interpreter as tflite

# NOTE: This script was tested on tensorflow/tflite (v2.4.1)


def skipped_test_tflite_runtime():
def get_tflite_model_path(target_edgetpu):
Expand Down Expand Up @@ -68,9 +70,13 @@ def check_remote(target_edgetpu=False):
server = rpc.Server("127.0.0.1")
remote = rpc.connect(server.host, server.port)
dev = remote.cpu(0)
if target_edgetpu:
runtime_target = "edge_tpu"
else:
runtime_target = "cpu"

with open(tflite_model_path, "rb") as model_fin:
runtime = tflite_runtime.create(model_fin.read(), dev)
runtime = tflite_runtime.create(model_fin.read(), dev, runtime_target)
runtime.set_input(0, tvm.nd.array(tflite_input, dev))
runtime.invoke()
out = runtime.get_output(0)
Expand Down

0 comments on commit 3bf65b7

Please sign in to comment.