Fix issue where the Interpreter::ModifyGraphWithDelegate and

Subgraph::ModifyGraphWithDelegate methods did not specify
the behaviour when the delegate parameter is null.

PiperOrigin-RevId: 352655477
Change-Id: Ia3997ba325cb76b3c76c8672c4b6102343da8443
This commit is contained in:
Fergus Henderson 2021-01-19 14:28:03 -08:00 committed by TensorFlower Gardener
parent bd29ff22a7
commit d22d31bebd
4 changed files with 22 additions and 2 deletions

View File

@ -1488,6 +1488,11 @@ TfLiteStatus Subgraph::ModifyGraphWithDelegate(TfLiteDelegate* delegate) {
TFLITE_SCOPED_TAGGED_DEFAULT_PROFILE(profiler_.get(),
"ModifyGraphWithDelegate");
if (delegate == nullptr) {
ReportError("Null delegate.");
return kTfLiteDelegateError;
}
// Restore delegation state if applicable.
TF_LITE_ENSURE_STATUS(RedoAllDelegates());

View File

@ -552,7 +552,8 @@ class Subgraph {
// Returns one of the following status codes:
// 1. kTfLiteOk: Delegation succeeded
// 2. kTfLiteDelegateError: Delegation failed due to an error *in the
// delegate*. The Subgraph has been restored to its pre-delegation state.
// delegate*, or the delegate parameter was null. The Subgraph has been
// restored to its pre-delegation state.
// NOTE: This reverts all delegates previously applied to the Subgraph.
// 3. kTfLiteApplicationError : Delegation failed to be applied due to the
// incompatibility with the TfLite runtime, e.g., the model graph is already

View File

@ -42,6 +42,11 @@ using test_utils::TestFP16Delegation;
namespace {
TEST_F(TestDelegate, NullDelegate) {
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(nullptr),
kTfLiteDelegateError);
}
TEST_F(TestDelegate, BasicDelegate) {
delegate_ = std::unique_ptr<SimpleDelegate>(new SimpleDelegate({0, 1, 2}));
interpreter_->ModifyGraphWithDelegate(delegate_->get_tf_lite_delegate());
@ -916,6 +921,14 @@ TEST_P(TestFP16Delegation, NonDelegatedInterpreterWorks) {
VerifyInvoke();
}
TEST_F(TestFP16Delegation, NullDelegate) {
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(nullptr),
kTfLiteDelegateError);
// Verify that resulting interpreter still works, despite null delegate.
ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk);
VerifyInvoke();
}
TEST_P(TestFP16Delegation, DelegationWorks) {
delegate_ = std::unique_ptr<FP16Delegate>(
new FP16Delegate(/**num_delegated_subsets**/ GetParam()));

View File

@ -469,7 +469,8 @@ class Interpreter {
/// Returns one of the following four status codes:
/// 1. kTfLiteOk: Success.
/// 2. kTfLiteDelegateError: Delegation failed due to an error in the
/// delegate. The Interpreter has been restored to its pre-delegation state.
/// delegate, or the delegate parameter was null. The Interpreter has been
/// restored to its pre-delegation state.
/// NOTE: This undoes all delegates previously applied to the Interpreter.
/// 3. kTfLiteApplicationError : Delegation failed to be applied due to the
/// incompatibility with the TfLite runtime, e.g., the model graph is already