diff --git a/tensorflow/lite/core/c/BUILD b/tensorflow/lite/core/c/BUILD index 35fcea37f9d62d..70999f0b24cf7a 100644 --- a/tensorflow/lite/core/c/BUILD +++ b/tensorflow/lite/core/c/BUILD @@ -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", diff --git a/tensorflow/lite/core/c/c_api_test.cc b/tensorflow/lite/core/c/c_api_test.cc index abb0083e12578c..189cd9815f8ebf 100644 --- a/tensorflow/lite/core/c/c_api_test.cc +++ b/tensorflow/lite/core/c/c_api_test.cc @@ -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"); @@ -316,6 +317,7 @@ TEST(CApiSimple, Delegate) { EXPECT_EQ(TfLiteInterpreterInvoke(interpreter), kTfLiteOk); TfLiteInterpreterDelete(interpreter); } +#endif TEST(CApiSimple, DelegateExternal_GetExecutionPlan) { TfLiteModel* model = @@ -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"); @@ -428,6 +431,7 @@ TEST(CApiSimple, DelegateFails) { TfLiteInterpreterOptionsDelete(options); TfLiteModelDelete(model); } +#endif struct DelegateState { bool delegate_prepared; diff --git a/tensorflow/lite/core/interpreter.h b/tensorflow/lite/core/interpreter.h index 334de115286d93..ed9d798f34753b 100644 --- a/tensorflow/lite/core/interpreter.h +++ b/tensorflow/lite/core/interpreter.h @@ -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 = diff --git a/tensorflow/lite/core/interpreter_experimental.cc b/tensorflow/lite/core/interpreter_experimental.cc index e04b1d3e7c675d..016d45df977955 100644 --- a/tensorflow/lite/core/interpreter_experimental.cc +++ b/tensorflow/lite/core/interpreter_experimental.cc @@ -84,6 +84,12 @@ TfLiteStatus Interpreter::ModifyGraphWithDelegate(TfLiteDelegate* delegate) { return ModifyGraphWithDelegateImpl(delegate); } +TfLiteStatus Interpreter::ModifyGraphWithDelegate( + TfLiteOpaqueDelegateStruct* delegate) { + return ModifyGraphWithDelegateImpl( + reinterpret_cast(delegate)); +} + bool Interpreter::HasDelegates() { return primary_subgraph().HasDelegates(); } TfLiteStatus Interpreter::SetBufferHandle(int tensor_index, diff --git a/tensorflow/lite/delegates/delegate_test.cc b/tensorflow/lite/delegates/delegate_test.cc index dbfb3de9f4cfb7..560b2b4c65b940 100644 --- a/tensorflow/lite/delegates/delegate_test.cc +++ b/tensorflow/lite/delegates/delegate_test.cc @@ -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); } @@ -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);