From 46e574e8195ae2e8085086457a642210d9d3b8cd Mon Sep 17 00:00:00 2001 From: Narumol Prangnawarat Date: Fri, 5 May 2023 16:39:05 +0100 Subject: IVGCVSW-7626 Add Execute Network for Opaque Delegate Signed-off-by: Narumol Prangnawarat Change-Id: Ibdded86713368ecfdf31c4118dfe8a3404d1e3b8 --- delegate/CMakeLists.txt | 139 +++++++++++++-------- delegate/opaque/src/armnn_delegate.cpp | 2 +- .../opaque/src/test/ArmnnOpaqueDelegateTest.cpp | 4 +- tests/CMakeLists.txt | 9 +- tests/ExecuteNetwork/ExecuteNetwork.cpp | 8 +- tests/ExecuteNetwork/ExecuteNetworkParams.hpp | 3 +- .../ExecuteNetworkProgramOptions.cpp | 5 + tests/ExecuteNetwork/TfliteExecutor.cpp | 35 +++++- 8 files changed, 140 insertions(+), 65 deletions(-) diff --git a/delegate/CMakeLists.txt b/delegate/CMakeLists.txt index f0b0e97b71..055ffce1c3 100644 --- a/delegate/CMakeLists.txt +++ b/delegate/CMakeLists.txt @@ -351,65 +351,94 @@ if(BUILD_UNIT_TESTS) endif() endif() -if(BUILD_DELEGATE_JNI_INTERFACE) +if(BUILD_DELEGATE_JNI_INTERFACE AND BUILD_CLASSIC_DELEGATE) add_subdirectory(armnnDelegateJNI) endif() #################################################### ## Export targets -set(armnn_delegate_export_targets) -list(APPEND armnn_delegate_export_targets - armnnClassicDelegateObject - armnnDelegate - tflite_headers - flatbuffer_headers - profiling_library_headers - thirdparty_headers) - -install( - TARGETS ${armnn_delegate_export_targets} - EXPORT armnn-delegate-targets - LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} - ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} - RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) - -## Set export alias -set_target_properties(armnnDelegate - PROPERTIES - EXPORT_NAME ArmnnDelegate) - -## Export target scrips -install( - EXPORT armnn-delegate-targets - FILE ArmnnDelegateTargets.cmake - NAMESPACE ArmnnDelegate:: - DESTINATION ${CMAKE_INSTALL_LIBDIR}) - -## Create ArmnnDelegateConfig.cmake -include(CMakePackageConfigHelpers) -set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}) -message(STATUS "CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}" ) -message(STATUS "CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}" ) -SET(Armnn_DIR "${Armnn_DIR}") - -configure_package_config_file( - ${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/ArmnnDelegateConfig.cmake.in - ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake - INSTALL_DESTINATION ${INSTALL_CONFIGDIR} - PATH_VARS Armnn_DIR) - -## Install ArmNN Delegate config file -install( - FILES - ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake - DESTINATION ${INSTALL_CONFIGDIR}) - -## Export from build tree -export( - EXPORT armnn-delegate-targets - FILE ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateTargets.cmake - NAMESPACE ArmnnDelegate::) -add_library(ArmnnDelegate::ArmnnDelegate ALIAS armnnDelegate) - +if (BUILD_CLASSIC_DELEGATE) + set(armnn_delegate_export_targets) + list(APPEND armnn_delegate_export_targets + armnnClassicDelegateObject + armnnDelegate + tflite_headers + flatbuffer_headers + profiling_library_headers + thirdparty_headers) + + install( + TARGETS ${armnn_delegate_export_targets} + EXPORT armnn-delegate-targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + + ## Set export alias + set_target_properties(armnnDelegate + PROPERTIES + EXPORT_NAME ArmnnDelegate) + + ## Export target scrips + install( + EXPORT armnn-delegate-targets + FILE ArmnnDelegateTargets.cmake + NAMESPACE ArmnnDelegate:: + DESTINATION ${CMAKE_INSTALL_LIBDIR}) + + ## Create ArmnnDelegateConfig.cmake + include(CMakePackageConfigHelpers) + set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}) + message(STATUS "CMAKE_CURRENT_LIST_DIR ${CMAKE_CURRENT_LIST_DIR}" ) + message(STATUS "CMAKE_CURRENT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}" ) + SET(Armnn_DIR "${Armnn_DIR}") + + configure_package_config_file( + ${CMAKE_CURRENT_LIST_DIR}/cmake/Modules/ArmnnDelegateConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake + INSTALL_DESTINATION ${INSTALL_CONFIGDIR} + PATH_VARS Armnn_DIR) + + ## Install ArmNN Delegate config file + install( + FILES + ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateConfig.cmake + DESTINATION ${INSTALL_CONFIGDIR}) + + ## Export from build tree + export( + EXPORT armnn-delegate-targets + FILE ${CMAKE_CURRENT_BINARY_DIR}/ArmnnDelegateTargets.cmake + NAMESPACE ArmnnDelegate::) + add_library(ArmnnDelegate::ArmnnDelegate ALIAS armnnDelegate) +endif() #################################################### +## Export opaque delegate targets + +if(BUILD_OPAQUE_DELEGATE) + set(armnn_opaque_delegate_export_targets) + list(APPEND armnn_opaque_delegate_export_targets + armnnOpaqueDelegateObject + armnnOpaqueDelegate + tflite_headers + flatbuffer_headers + profiling_library_headers + thirdparty_headers) + + install( + TARGETS armnnOpaqueDelegate + EXPORT armnn-opaque-delegate-targets + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) + + ## Set export alias + set_target_properties(armnnOpaqueDelegate + PROPERTIES + EXPORT_NAME ArmnnOpaqueDelegate) + + add_library(ArmnnDelegate::ArmnnOpaqueDelegate ALIAS armnnOpaqueDelegate) +endif() + +#################################################### \ No newline at end of file diff --git a/delegate/opaque/src/armnn_delegate.cpp b/delegate/opaque/src/armnn_delegate.cpp index 8cdf01ffc3..1c9f2d973e 100644 --- a/delegate/opaque/src/armnn_delegate.cpp +++ b/delegate/opaque/src/armnn_delegate.cpp @@ -578,7 +578,7 @@ TfLiteStatus ArmnnSubgraph::Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpa armnn::TensorInfo inputTensorInfo = inputBinding.second; inputTensorInfo.SetConstant(true); const armnn::ConstTensor inputTensor(inputTensorInfo, TfLiteOpaqueTensorData(tensor)); - inputTensors.emplace_back(inputIdx, inputTensor); + inputTensors.emplace_back(inputIndexArray[inputIdx], inputTensor); ++inputIndex; } diff --git a/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp b/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp index 79f98a9e5e..2669bc8480 100644 --- a/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp +++ b/delegate/opaque/src/test/ArmnnOpaqueDelegateTest.cpp @@ -44,8 +44,8 @@ TEST_CASE ("DelegatePluginTest") { // Use default settings until options have been enabled. flatbuffers::FlatBufferBuilder flatBufferBuilder; - tflite::TFLiteSettingsBuilder tfliteSettingBuilder(flatBufferBuilder); - flatbuffers::Offset tfliteSettings = tfliteSettingBuilder.Finish(); + tflite::TFLiteSettingsBuilder tfliteSettingsBuilder(flatBufferBuilder); + flatbuffers::Offset tfliteSettings = tfliteSettingsBuilder.Finish(); flatBufferBuilder.Finish(tfliteSettings); const tflite::TFLiteSettings* settings = flatbuffers::GetRoot( flatBufferBuilder.GetBufferPointer()); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 102d744aa7..71374c4261 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -141,7 +141,8 @@ endif() if (BUILD_ARMNN_SERIALIZER OR BUILD_TF_LITE_PARSER OR BUILD_ONNX_PARSER - OR BUILD_CLASSIC_DELEGATE) + OR BUILD_CLASSIC_DELEGATE + OR BUILD_OPAQUE_DELEGATE) set(ExecuteNetwork_sources ExecuteNetwork/IExecutor.hpp ExecuteNetwork/ArmNNExecutor.cpp @@ -154,7 +155,7 @@ if (BUILD_ARMNN_SERIALIZER NetworkExecutionUtils/NetworkExecutionUtils.cpp NetworkExecutionUtils/NetworkExecutionUtils.hpp) - if(BUILD_CLASSIC_DELEGATE) + if(BUILD_CLASSIC_DELEGATE OR BUILD_OPAQUE_DELEGATE) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-comment") set(ExecuteNetwork_sources ${ExecuteNetwork_sources} @@ -167,6 +168,7 @@ if (BUILD_ARMNN_SERIALIZER target_include_directories(ExecuteNetwork PRIVATE ../src/armnnUtils) target_include_directories(ExecuteNetwork PRIVATE ../src/backends) target_include_directories(ExecuteNetwork PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) + if(EXECUTE_NETWORK_STATIC) target_link_libraries(ExecuteNetwork -Wl,--whole-archive @@ -189,6 +191,9 @@ if (BUILD_ARMNN_SERIALIZER if (BUILD_CLASSIC_DELEGATE) target_link_libraries(ExecuteNetwork ArmnnDelegate::ArmnnDelegate) endif() + if (BUILD_OPAQUE_DELEGATE) + target_link_libraries(ExecuteNetwork ArmnnDelegate::ArmnnOpaqueDelegate) + endif() target_link_libraries(ExecuteNetwork armnn) endif() diff --git a/tests/ExecuteNetwork/ExecuteNetwork.cpp b/tests/ExecuteNetwork/ExecuteNetwork.cpp index 14841ec1e9..f9f583a9c6 100644 --- a/tests/ExecuteNetwork/ExecuteNetwork.cpp +++ b/tests/ExecuteNetwork/ExecuteNetwork.cpp @@ -5,7 +5,7 @@ #include "ExecuteNetworkProgramOptions.hpp" #include "ArmNNExecutor.hpp" -#if defined(ARMNN_TFLITE_DELEGATE) +#if defined(ARMNN_TFLITE_DELEGATE) || defined(ARMNN_TFLITE_OPAQUE_DELEGATE) #include "TfliteExecutor.hpp" #endif #include @@ -13,10 +13,12 @@ std::unique_ptr BuildExecutor(ProgramOptions& programOptions) { - if (programOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate || + if (programOptions.m_ExNetParams.m_TfLiteExecutor == + ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteOpaqueDelegate || + programOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate || programOptions.m_ExNetParams.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::TfliteInterpreter) { -#if defined(ARMNN_TFLITE_DELEGATE) +#if defined(ARMNN_TFLITE_DELEGATE) || defined(ARMNN_TFLITE_OPAQUE_DELEGATE) return std::make_unique(programOptions.m_ExNetParams, programOptions.m_RuntimeOptions); #else ARMNN_LOG(fatal) << "Not built with Arm NN Tensorflow-Lite delegate support."; diff --git a/tests/ExecuteNetwork/ExecuteNetworkParams.hpp b/tests/ExecuteNetwork/ExecuteNetworkParams.hpp index 020dbdcced..ffcb4f482c 100644 --- a/tests/ExecuteNetwork/ExecuteNetworkParams.hpp +++ b/tests/ExecuteNetwork/ExecuteNetworkParams.hpp @@ -20,7 +20,8 @@ struct ExecuteNetworkParams { ArmNNTfLiteParser, ArmNNTfLiteDelegate, - TfliteInterpreter + TfliteInterpreter, + ArmNNTfLiteOpaqueDelegate, }; bool m_AllowExpandedDims; diff --git a/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp b/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp index 3a54b1ab19..8d5035e103 100644 --- a/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp +++ b/tests/ExecuteNetwork/ExecuteNetworkProgramOptions.cpp @@ -355,6 +355,7 @@ ProgramOptions::ProgramOptions() : m_CxxOptions{"ExecuteNetwork", "Set the executor for the tflite model: parser, delegate, tflite" "parser is the ArmNNTfLiteParser, " "delegate is the ArmNNTfLiteDelegate, " + "opaquedelegate is the ArmNNTfLiteOpaqueDelegate, " "tflite is the TfliteInterpreter", cxxopts::value()->default_value("parser")) @@ -539,6 +540,10 @@ void ProgramOptions::ParseOptions(int ac, const char* av[]) { m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteParser; } + else if (tfliteExecutor == "opaquedelegate") + { + m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteOpaqueDelegate; + } else if (tfliteExecutor == "delegate") { m_ExNetParams.m_TfLiteExecutor = ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate; diff --git a/tests/ExecuteNetwork/TfliteExecutor.cpp b/tests/ExecuteNetwork/TfliteExecutor.cpp index 87731c2f83..04f6ddb72a 100644 --- a/tests/ExecuteNetwork/TfliteExecutor.cpp +++ b/tests/ExecuteNetwork/TfliteExecutor.cpp @@ -3,6 +3,11 @@ // SPDX-License-Identifier: MIT // +#if defined(ARMNN_TFLITE_OPAQUE_DELEGATE) +#include <../delegate/opaque/include/armnn_delegate.hpp> +#endif + +#include #include "TfliteExecutor.hpp" #include "tensorflow/lite/kernels/kernel_util.h" @@ -26,8 +31,33 @@ TfLiteExecutor::TfLiteExecutor(const ExecuteNetworkParams& params, armnn::IRunti { LogAndThrow("Failed to allocate tensors in the TfLiteInterpreter."); } - if (m_Params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate) + + if (m_Params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteOpaqueDelegate) + { +#if defined(ARMNN_TFLITE_OPAQUE_DELEGATE) + // Use default settings until options have been enabled + flatbuffers::FlatBufferBuilder flatBufferBuilder; + TFLiteSettingsBuilder tfliteSettingsBuilder(flatBufferBuilder); + flatbuffers::Offset tfliteSettings = tfliteSettingsBuilder.Finish(); + flatBufferBuilder.Finish(tfliteSettings); + const TFLiteSettings* settings = + flatbuffers::GetRoot(flatBufferBuilder.GetBufferPointer()); + + std::unique_ptr delegatePlugIn = + delegates::DelegatePluginRegistry::CreateByName("armnn_delegate", *settings); + + // Create Armnn Opaque Delegate from Armnn Delegate Plugin + delegates::TfLiteDelegatePtr armnnDelegate = delegatePlugIn->Create(); + + // Add Delegate to the builder + builder.AddDelegate(armnnDelegate.get()); +#else + LogAndThrow("Not built with Arm NN Tensorflow-Lite opaque delegate support."); +#endif + } + else if (m_Params.m_TfLiteExecutor == ExecuteNetworkParams::TfLiteExecutor::ArmNNTfLiteDelegate) { +#if defined(ARMNN_TFLITE_DELEGATE) // Create the Armnn Delegate // Populate a DelegateOptions from the ExecuteNetworkParams. armnnDelegate::DelegateOptions delegateOptions = m_Params.ToDelegateOptions(); @@ -40,6 +70,9 @@ TfLiteExecutor::TfLiteExecutor(const ExecuteNetworkParams& params, armnn::IRunti { LogAndThrow("Could not register ArmNN TfLite Delegate to TfLiteInterpreter."); } +#else + LogAndThrow("Not built with Arm NN Tensorflow-Lite delegate support."); +#endif } else { -- cgit v1.2.1