diff options
author | Eugene Brevdo <ebrevdo@google.com> | 2018-03-08 13:39:54 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2018-03-08 13:45:19 -0800 |
commit | 8c9a9b371d619ba35f7eae598a2eea045659738a (patch) | |
tree | 70ee7d729516141aea0abc693b199810143781b5 /tensorflow/contrib/lite/interpreter.cc | |
parent | ebf554ff77bc46bfdd9b424bc44b62f803100b33 (diff) |
[TF Lite] Provide a fast path for Interpreter::SetTensorParametersReadOnly.
The fast path kicks in if shape matches tensor.dims and type matches
tensor.type.
In this case, the interpreter is not invalidated and AllocateTensors need not
be called after a call to SetTensorParametersReadOnly.
PiperOrigin-RevId: 188380596
Diffstat (limited to 'tensorflow/contrib/lite/interpreter.cc')
-rw-r--r-- | tensorflow/contrib/lite/interpreter.cc | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/tensorflow/contrib/lite/interpreter.cc b/tensorflow/contrib/lite/interpreter.cc index 8fd1085544..2834dc49f9 100644 --- a/tensorflow/contrib/lite/interpreter.cc +++ b/tensorflow/contrib/lite/interpreter.cc @@ -569,10 +569,22 @@ TfLiteStatus Interpreter::SetTensorParametersReadOnly( &required_bytes)); TF_LITE_ENSURE_EQ(&context_, required_bytes, bytes); } - invokable_ = false; - TfLiteTensorReset(type, name, ConvertVectorToTfLiteIntArray(dims), - quantization, const_cast<char*>(buffer), bytes, - kTfLiteMmapRo, allocation, &context_.tensors[tensor_index]); + + TfLiteTensor& tensor = context_.tensors[tensor_index]; + if (type == tensor.type && EqualVectorAndTfLiteIntArray(tensor.dims, dims)) { + // Fast path which does not invalidate the invokable property. + TfLiteTensorDataFree(&tensor); + tensor.data.raw = const_cast<char*>(buffer); + if (!tensor.dims) tensor.dims = ConvertVectorToTfLiteIntArray(dims); + tensor.params = quantization; + tensor.allocation_type = kTfLiteMmapRo; + tensor.allocation = allocation; + } else { + invokable_ = false; + TfLiteTensorReset(type, name, ConvertVectorToTfLiteIntArray(dims), + quantization, const_cast<char*>(buffer), bytes, + kTfLiteMmapRo, allocation, &tensor); + } return kTfLiteOk; } |