From 26654cb71db2b1e163527f52c3198d9434bb0e37 Mon Sep 17 00:00:00 2001 From: Narumol Prangnawarat Date: Wed, 3 May 2023 16:08:11 +0100 Subject: IVGCVSW-7423 Add ArmnnDelegatePlugin Signed-off-by: Narumol Prangnawarat Signed-off-by: Ryan OShea Change-Id: Ie02021ac56a512598760e4c6d05ef1a80f4aec8d --- delegate/opaque/CMakeLists.txt | 5 ++++ delegate/opaque/include/armnn_delegate.hpp | 30 ++++++++++++++++++++++ delegate/opaque/src/armnn_delegate.cpp | 12 ++++++--- .../opaque/src/test/ArmnnOpaqueDelegateTest.cpp | 25 ++++++++++++++++++ 4 files changed, 69 insertions(+), 3 deletions(-) (limited to 'delegate/opaque') diff --git a/delegate/opaque/CMakeLists.txt b/delegate/opaque/CMakeLists.txt index d7aed373ef..a33d3d8e90 100644 --- a/delegate/opaque/CMakeLists.txt +++ b/delegate/opaque/CMakeLists.txt @@ -92,6 +92,11 @@ target_compile_options(flatbuffer_headers_opaque INTERFACE -Wno-sign-conversion) target_link_libraries(armnnOpaqueDelegateObject PUBLIC flatbuffer_headers_opaque) +# Additional Absl Sync for Opaque Delegate +find_package(TfLiteAbsl REQUIRED MODULE) +target_include_directories(armnnOpaqueDelegateObject PUBLIC ${TfLite_ABSL_SYNC_HEADERS}) +target_link_libraries(armnnOpaqueDelegateObject PUBLIC ${TfLite_Extra_Absl_LIB}) + #################################################### ## Export targets install(TARGETS armnnOpaqueDelegateObject diff --git a/delegate/opaque/include/armnn_delegate.hpp b/delegate/opaque/include/armnn_delegate.hpp index 653015a63c..bb6451f649 100644 --- a/delegate/opaque/include/armnn_delegate.hpp +++ b/delegate/opaque/include/armnn_delegate.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5) #define ARMNN_POST_TFLITE_2_5 @@ -87,6 +88,35 @@ const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi(); extern const TfLiteStableDelegate TFL_TheStableDelegate; +using tflite::delegates::DelegatePluginInterface; +using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr; + +class ArmnnDelegatePlugin : public DelegatePluginInterface +{ +public: + static std::unique_ptr New(const tflite::TFLiteSettings& tflite_settings) + { + return std::make_unique(tflite_settings); + } + + tflite::delegates::TfLiteDelegatePtr Create() override + { + // Use default settings until options have been enabled. + return tflite::delegates::TfLiteDelegatePtr( + TfLiteArmnnOpaqueDelegateCreate(nullptr), TfLiteArmnnOpaqueDelegateDelete); + } + + int GetDelegateErrno(TfLiteOpaqueDelegate* from_delegate) override + { + return 0; + } + + explicit ArmnnDelegatePlugin(const tflite::TFLiteSettings& tfliteSettings) + { + // Use default settings until options have been enabled. + } +}; + /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph class ArmnnSubgraph { diff --git a/delegate/opaque/src/armnn_delegate.cpp b/delegate/opaque/src/armnn_delegate.cpp index fa64679efc..8cdf01ffc3 100644 --- a/delegate/opaque/src/armnn_delegate.cpp +++ b/delegate/opaque/src/armnn_delegate.cpp @@ -62,11 +62,15 @@ namespace armnnOpaqueDelegate const TfLiteStableDelegate TFL_TheStableDelegate = { /*delegate_abi_version=*/ TFL_STABLE_DELEGATE_ABI_VERSION, - /*delegate_name=*/ "ArmnnDelegatePlugin", - /*delegate_version=*/ "1.0.0", + /*delegate_name=*/ "armnn_delegate", + /*delegate_version=*/ OPAQUE_DELEGATE_VERSION, /*delegate_plugin=*/ GetArmnnDelegatePluginApi() }; +static auto* g_delegate_plugin_ArmnnDelegatePlugin_ = + new tflite::delegates::DelegatePluginRegistry::Register(TFL_TheStableDelegate.delegate_name, + ArmnnDelegatePlugin::New); + ArmnnOpaqueDelegate::ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options) : m_Options(std::move(options)) { @@ -121,7 +125,9 @@ TfLiteStatus DoPrepare(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueDelegate* // ArmNN Opaque Delegate Registration TfLiteRegistrationExternal* kernelRegistration = - TfLiteRegistrationExternalCreate(kTfLiteBuiltinDelegate, "TfLiteArmNNOpaqueDelegate", /*version=*/1); + TfLiteRegistrationExternalCreate(kTfLiteBuiltinDelegate, + TFL_TheStableDelegate.delegate_name, + /*version=*/OPAQUE_DELEGATE_MAJOR_VERSION); if(kernelRegistration == nullptr) { return kTfLiteError; diff --git a/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp b/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp index 1635b65809..79f98a9e5e 100644 --- a/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp +++ b/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp @@ -9,6 +9,9 @@ #include #include +#include +#include + namespace armnnOpaqueDelegate { @@ -37,6 +40,28 @@ TEST_CASE ("DelegateOptions_OpaqueDelegateDefault") armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateDelete(opaqueDelegate); } +TEST_CASE ("DelegatePluginTest") +{ + // Use default settings until options have been enabled. + flatbuffers::FlatBufferBuilder flatBufferBuilder; + tflite::TFLiteSettingsBuilder tfliteSettingBuilder(flatBufferBuilder); + flatbuffers::Offset tfliteSettings = tfliteSettingBuilder.Finish(); + flatBufferBuilder.Finish(tfliteSettings); + const tflite::TFLiteSettings* settings = flatbuffers::GetRoot( + flatBufferBuilder.GetBufferPointer()); + + std::unique_ptr delegatePlugin = + tflite::delegates::DelegatePluginRegistry::CreateByName("armnn_delegate", *settings); + + // Plugin is created correctly using armnn_delegate name. + CHECK((delegatePlugin != nullptr)); + + tflite::delegates::TfLiteDelegatePtr armnnDelegate = delegatePlugin->Create(); + + // Armnn Opaque Delegate is created correctly. + CHECK((armnnDelegate != nullptr)); + CHECK((armnnDelegate->opaque_delegate_builder != nullptr)); } +} } // namespace armnnDelegate -- cgit v1.2.1