From 4cca4061659ec087cd995045aa38b6aed24e063e Mon Sep 17 00:00:00 2001 From: Sachin Joglekar Date: Wed, 3 Jul 2019 13:53:55 -0700 Subject: [PATCH] Set subgraph state correctly after Undoing delegates, and refactor ResizeInputTensor to be no-op if applicable. PiperOrigin-RevId: 256432644 --- tensorflow/lite/core/subgraph.cc | 9 ++++++--- tensorflow/lite/interpreter_test.cc | 8 ++++++++ .../lite/tools/benchmark/benchmark_tflite_model.cc | 3 +-- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/tensorflow/lite/core/subgraph.cc b/tensorflow/lite/core/subgraph.cc index e4b103638d3..17d09efba1b 100644 --- a/tensorflow/lite/core/subgraph.cc +++ b/tensorflow/lite/core/subgraph.cc @@ -601,9 +601,6 @@ TfLiteStatus Subgraph::ResizeInputTensor(int tensor_index, if (graph_is_immutable && !delegates_applied) { ReportError("ResizeInputTensor is disallowed when graph is immutable."); return kTfLiteError; - } else if (graph_is_immutable) { - // Undo delegation if it resulted in the graph being immutable. - TF_LITE_ENSURE_STATUS(UndoAllDelegates()); } // TODO(aselle): All bounds checks can be implemented as one-sided bounds @@ -623,6 +620,10 @@ TfLiteStatus Subgraph::ResizeInputTensor(int tensor_index, return kTfLiteOk; } + if (graph_is_immutable) { + // Undo delegation if it resulted in the graph being immutable. + TF_LITE_ENSURE_STATUS(UndoAllDelegates()); + } state_ = kStateUninvokable; return ResizeTensorImpl(tensor, ConvertVectorToTfLiteIntArray(dims)); } @@ -1076,6 +1077,8 @@ TfLiteStatus Subgraph::UndoAllDelegates() { execution_plan_[execution_plan_index]); } nodes_and_registration_.resize(max_retained_node_index + 1); + // After undoing delegates, the graph is uninvokable, but mutable. + state_ = kStateUninvokable; delegates_undone_ = true; return kTfLiteOk; diff --git a/tensorflow/lite/interpreter_test.cc b/tensorflow/lite/interpreter_test.cc index 2272a49b172..9ca31d544e6 100644 --- a/tensorflow/lite/interpreter_test.cc +++ b/tensorflow/lite/interpreter_test.cc @@ -1394,6 +1394,10 @@ TEST_F(TestDelegate, TestResizeInputWithNonDynamicDelegate) { interpreter_->ModifyGraphWithDelegate(delegate_->get_tf_lite_delegate()), kTfLiteOk); + // Try resizing input to same shape as before (which should be a No-op). + ASSERT_EQ(interpreter_->ResizeInputTensor(0, {3}), kTfLiteOk); + ASSERT_EQ(interpreter_->execution_plan().size(), 1); + ASSERT_EQ(interpreter_->ResizeInputTensor(0, {1, 3}), kTfLiteOk); ASSERT_EQ(interpreter_->ResizeInputTensor(1, {1, 3}), kTfLiteOk); ASSERT_EQ(interpreter_->execution_plan().size(), 3); @@ -1450,6 +1454,10 @@ TEST_F(TestDelegate, TestResizeInputWithMultipleDelegates) { // Should be two delegates nodes. ASSERT_EQ(interpreter_->execution_plan().size(), 2); + // Try resizing input to same shape as before (which should be a No-op). + ASSERT_EQ(interpreter_->ResizeInputTensor(0, {3}), kTfLiteOk); + ASSERT_EQ(interpreter_->execution_plan().size(), 2); + // Resizing input tensors should temporarily restore original execution plan // of 3 nodes. ASSERT_EQ(interpreter_->ResizeInputTensor(0, {1, 3}), kTfLiteOk); diff --git a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc index 0e7c35c923e..301108539a2 100644 --- a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc +++ b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc @@ -482,8 +482,7 @@ void BenchmarkTfLiteModel::Init() { } } - // Don't allocate tensors if we have delegates. - if (delegates_.empty() && interpreter->AllocateTensors() != kTfLiteOk) { + if (interpreter->AllocateTensors() != kTfLiteOk) { TFLITE_LOG(FATAL) << "Failed to allocate tensors!"; }