Set subgraph state correctly after Undoing delegates, and refactor ResizeInputTensor to be no-op if applicable.

PiperOrigin-RevId: 256432644
This commit is contained in:
Sachin Joglekar 2019-07-03 13:53:55 -07:00 committed by TensorFlower Gardener
parent 107a35a551
commit 4cca406165
3 changed files with 15 additions and 5 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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!";
}