Skip to content

Commit

Permalink
Add overload of ModifyGraphWithDelegate that takes a `TfLiteOpaqueD…
Browse files Browse the repository at this point in the history
…elegate*`.

This is similar to the existing overload of `InterpreterBuilder::AddDelegate`.
When building with TFLITE_USE_OPAQUE_DELEGATE, this overload is needed to use ModifyGraphWithDelegate with delegates created using TfLiteOpaqueDelegateCreate.

InterpreterBuilder::AddDelegate remains preferred over Interpreter::ModifyGraphWithDelegate, but this new overload is useful
because it reduces the burden for converting code to use the opaque delegate APIs.
PiperOrigin-RevId: 586663060
  • Loading branch information
fergushenderson authored and tensorflower-gardener committed Nov 30, 2023
1 parent 0d8a6a4 commit 45374d5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
27 changes: 27 additions & 0 deletions tensorflow/lite/core/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,33 @@ cc_test(
],
)

cc_test(
name = "c_api_test_with_opaque_delegate",
size = "small",
srcs = ["c_api_test.cc"],
copts = tflite_copts(),
data = [
"//tensorflow/lite:testdata/2_subgraphs.bin",
"//tensorflow/lite:testdata/add.bin",
"//tensorflow/lite:testdata/add_quantized.bin",
"//tensorflow/lite:testdata/custom_sinh.bin",
],
local_defines = ["TFLITE_USE_OPAQUE_DELEGATE"],
deps = [
":c_api",
":c_api_experimental",
":c_api_types",
":common",
"//tensorflow/lite:string_util",
"//tensorflow/lite/c:c_api_internal",
"//tensorflow/lite/core:subgraph",
"//tensorflow/lite/delegates:delegate_test_util",
"//tensorflow/lite/schema:schema_fbs",
"//tensorflow/lite/testing:util",
"@com_google_googletest//:gtest_main",
],
)

cc_test(
name = "selectively_built_c_api_test",
size = "small",
Expand Down
4 changes: 4 additions & 0 deletions tensorflow/lite/core/c/c_api_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ TEST(CApiSimple, TfLiteInterpreterGetTensor) {
TfLiteInterpreterDelete(interpreter);
}

#if !TFLITE_USE_OPAQUE_DELEGATE
TEST(CApiSimple, Delegate) {
TfLiteModel* model =
TfLiteModelCreateFromFile("tensorflow/lite/testdata/add.bin");
Expand All @@ -316,6 +317,7 @@ TEST(CApiSimple, Delegate) {
EXPECT_EQ(TfLiteInterpreterInvoke(interpreter), kTfLiteOk);
TfLiteInterpreterDelete(interpreter);
}
#endif

TEST(CApiSimple, DelegateExternal_GetExecutionPlan) {
TfLiteModel* model =
Expand Down Expand Up @@ -409,6 +411,7 @@ TEST(CApiSimple, DelegateExternal_MarkSubgraphAsDelegationSkippable) {
TfLiteOpaqueDelegateDelete(opaque_delegate);
}

#if !TFLITE_USE_OPAQUE_DELEGATE
TEST(CApiSimple, DelegateFails) {
TfLiteModel* model =
TfLiteModelCreateFromFile("tensorflow/lite/testdata/add.bin");
Expand All @@ -428,6 +431,7 @@ TEST(CApiSimple, DelegateFails) {
TfLiteInterpreterOptionsDelete(options);
TfLiteModelDelete(model);
}
#endif

struct DelegateState {
bool delegate_prepared;
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/core/interpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,7 @@ class Interpreter {
/// 5. kTfLiteError: Unexpected/runtime failure. \n
/// \warning This is an experimental API and subject to change. \n
TfLiteStatus ModifyGraphWithDelegate(TfLiteDelegate* delegate);
TfLiteStatus ModifyGraphWithDelegate(TfLiteOpaqueDelegateStruct* delegate);

// Owning handle to a TfLiteDelegate instance.
using TfLiteDelegatePtr =
Expand Down
6 changes: 6 additions & 0 deletions tensorflow/lite/core/interpreter_experimental.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ TfLiteStatus Interpreter::ModifyGraphWithDelegate(TfLiteDelegate* delegate) {
return ModifyGraphWithDelegateImpl(delegate);
}

TfLiteStatus Interpreter::ModifyGraphWithDelegate(
TfLiteOpaqueDelegateStruct* delegate) {
return ModifyGraphWithDelegateImpl(
reinterpret_cast<TfLiteDelegate*>(delegate));
}

bool Interpreter::HasDelegates() { return primary_subgraph().HasDelegates(); }

TfLiteStatus Interpreter::SetBufferHandle(int tensor_index,
Expand Down
6 changes: 4 additions & 2 deletions tensorflow/lite/delegates/delegate_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ using test_utils::TestTwoDelegates;
namespace {

TEST_F(TestDelegate, NullDelegate) {
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(nullptr),
TfLiteOpaqueDelegate* delegate = nullptr;
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(delegate),
kTfLiteDelegateError);
}

Expand Down Expand Up @@ -1488,7 +1489,8 @@ TEST_P(TestFP16Delegation, NonDelegatedInterpreterWorks) {
}

TEST_F(TestFP16Delegation, NullDelegate) {
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(nullptr),
TfLiteOpaqueDelegate* delegate = nullptr;
EXPECT_EQ(interpreter_->ModifyGraphWithDelegate(delegate),
kTfLiteDelegateError);
// Verify that resulting interpreter still works, despite null delegate.
ASSERT_EQ(interpreter_->AllocateTensors(), kTfLiteOk);
Expand Down

0 comments on commit 45374d5

Please sign in to comment.