From c4fb0dd4145e05123c546458ba5d281abfcc2b28 Mon Sep 17 00:00:00 2001 From: Francis Murtagh Date: Thu, 16 Mar 2023 17:01:56 +0000 Subject: IVGCVSW-7556 Introduce Opaque Delegate API * Also added cmake for the new layers to reduce merge conflicts. Signed-off-by: Francis Murtagh Change-Id: Ieb59aa2b7e2a18c57c9357b8d5b5cd63d8211c85 --- delegate/CMakeLists.txt | 11 +- delegate/classic/include/armnn_delegate.hpp | 2 +- delegate/classic/src/Convolution.hpp | 2 +- delegate/classic/src/SharedFunctions.cpp | 8 +- delegate/common/include/DelegateOptions.hpp | 1 - delegate/opaque/CMakeLists.txt | 3 +- delegate/opaque/include/Version.hpp | 8 +- delegate/opaque/include/armnn_delegate.hpp | 107 ++++++++-------- delegate/opaque/src/Activation.hpp | 4 + delegate/opaque/src/ArgMinMax.hpp | 4 + delegate/opaque/src/BatchMatMul.hpp | 4 + delegate/opaque/src/BatchSpace.hpp | 4 + delegate/opaque/src/Comparison.hpp | 4 + delegate/opaque/src/Control.hpp | 4 + delegate/opaque/src/Convolution.hpp | 4 + delegate/opaque/src/ElementwiseBinary.hpp | 4 + delegate/opaque/src/ElementwiseUnary.hpp | 4 + delegate/opaque/src/Fill.hpp | 4 + delegate/opaque/src/FullyConnected.hpp | 4 + delegate/opaque/src/Gather.hpp | 4 + delegate/opaque/src/GatherNd.hpp | 4 + delegate/opaque/src/LogicalBinary.hpp | 4 + delegate/opaque/src/Lstm.hpp | 4 + delegate/opaque/src/MultiLayerFacade.hpp | 4 + delegate/opaque/src/Normalization.hpp | 4 + delegate/opaque/src/Pack.hpp | 4 + delegate/opaque/src/Pad.hpp | 4 + delegate/opaque/src/Pooling.hpp | 4 + delegate/opaque/src/Prelu.hpp | 4 + delegate/opaque/src/Quantization.hpp | 4 + delegate/opaque/src/Redefine.hpp | 4 + delegate/opaque/src/Reduce.hpp | 4 + delegate/opaque/src/Resize.hpp | 4 + delegate/opaque/src/Round.hpp | 4 + delegate/opaque/src/Shape.hpp | 4 + delegate/opaque/src/Slice.hpp | 4 + delegate/opaque/src/Softmax.hpp | 4 + delegate/opaque/src/SpaceDepth.hpp | 4 + delegate/opaque/src/Split.hpp | 4 + delegate/opaque/src/StridedSlice.hpp | 4 + delegate/opaque/src/Transpose.hpp | 4 + delegate/opaque/src/UnidirectionalSequenceLstm.hpp | 4 + delegate/opaque/src/Unpack.hpp | 4 + delegate/opaque/src/armnn_delegate.cpp | 136 +++++++++++++++++++++ delegate/opaque/src/armnn_external_delegate.cpp | 4 + delegate/test/ArmnnDelegateTest.cpp | 40 +++++- 46 files changed, 391 insertions(+), 71 deletions(-) create mode 100644 delegate/opaque/src/Activation.hpp create mode 100644 delegate/opaque/src/ArgMinMax.hpp create mode 100644 delegate/opaque/src/BatchMatMul.hpp create mode 100644 delegate/opaque/src/BatchSpace.hpp create mode 100644 delegate/opaque/src/Comparison.hpp create mode 100644 delegate/opaque/src/Control.hpp create mode 100644 delegate/opaque/src/Convolution.hpp create mode 100644 delegate/opaque/src/ElementwiseBinary.hpp create mode 100644 delegate/opaque/src/ElementwiseUnary.hpp create mode 100644 delegate/opaque/src/Fill.hpp create mode 100644 delegate/opaque/src/FullyConnected.hpp create mode 100644 delegate/opaque/src/Gather.hpp create mode 100644 delegate/opaque/src/GatherNd.hpp create mode 100644 delegate/opaque/src/LogicalBinary.hpp create mode 100644 delegate/opaque/src/Lstm.hpp create mode 100644 delegate/opaque/src/MultiLayerFacade.hpp create mode 100644 delegate/opaque/src/Normalization.hpp create mode 100644 delegate/opaque/src/Pack.hpp create mode 100644 delegate/opaque/src/Pad.hpp create mode 100644 delegate/opaque/src/Pooling.hpp create mode 100644 delegate/opaque/src/Prelu.hpp create mode 100644 delegate/opaque/src/Quantization.hpp create mode 100644 delegate/opaque/src/Redefine.hpp create mode 100644 delegate/opaque/src/Reduce.hpp create mode 100644 delegate/opaque/src/Resize.hpp create mode 100644 delegate/opaque/src/Round.hpp create mode 100644 delegate/opaque/src/Shape.hpp create mode 100644 delegate/opaque/src/Slice.hpp create mode 100644 delegate/opaque/src/Softmax.hpp create mode 100644 delegate/opaque/src/SpaceDepth.hpp create mode 100644 delegate/opaque/src/Split.hpp create mode 100644 delegate/opaque/src/StridedSlice.hpp create mode 100644 delegate/opaque/src/Transpose.hpp create mode 100644 delegate/opaque/src/UnidirectionalSequenceLstm.hpp create mode 100644 delegate/opaque/src/Unpack.hpp create mode 100644 delegate/opaque/src/armnn_delegate.cpp create mode 100644 delegate/opaque/src/armnn_external_delegate.cpp (limited to 'delegate') diff --git a/delegate/CMakeLists.txt b/delegate/CMakeLists.txt index f6a5e512fa..d35fd4e7b6 100644 --- a/delegate/CMakeLists.txt +++ b/delegate/CMakeLists.txt @@ -233,7 +233,7 @@ if(BUILD_UNIT_TESTS) else() endif() - if (BUILD_CLASSIC_DELEGATE) # For Opaque Delegate Unit Tests add OR BUILD_ARMNN_TFLITE_OPAQUE_DELEGATE here + if (BUILD_CLASSIC_DELEGATE OR BUILD_OPAQUE_DELEGATE) add_executable(DelegateUnitTests ${armnnDelegate_unittest_sources}) target_include_directories(DelegateUnitTests SYSTEM PRIVATE "${TF_LITE_SCHEMA_INCLUDE_PATH}") @@ -241,7 +241,14 @@ if(BUILD_UNIT_TESTS) # Add half library from armnn third-party libraries target_link_libraries(DelegateUnitTests PRIVATE thirdparty_headers) - target_link_libraries(DelegateUnitTests PRIVATE armnnDelegate) + if (BUILD_CLASSIC_DELEGATE) + target_link_libraries(DelegateUnitTests PRIVATE armnnDelegate) + target_include_directories(DelegateUnitTests PUBLIC ${PROJECT_SOURCE_DIR}) + endif() + if (BUILD_OPAQUE_DELEGATE) + target_link_libraries(DelegateUnitTests PRIVATE armnnOpaqueDelegate) + target_include_directories(DelegateUnitTests PUBLIC ${PROJECT_SOURCE_DIR}) + endif() target_link_libraries(DelegateUnitTests PRIVATE Armnn::armnnUtils) target_link_libraries(DelegateUnitTests PRIVATE profiling_library_headers) diff --git a/delegate/classic/include/armnn_delegate.hpp b/delegate/classic/include/armnn_delegate.hpp index 8957dc87d6..e94a6e2d4e 100644 --- a/delegate/classic/include/armnn_delegate.hpp +++ b/delegate/classic/include/armnn_delegate.hpp @@ -129,7 +129,7 @@ private: /// The Network Id armnn::NetworkId m_NetworkId; - /// ArmNN Rumtime + /// ArmNN Runtime armnn::IRuntime* m_Runtime; // Binding information for inputs and outputs diff --git a/delegate/classic/src/Convolution.hpp b/delegate/classic/src/Convolution.hpp index f6a50615fc..8963d2ead8 100644 --- a/delegate/classic/src/Convolution.hpp +++ b/delegate/classic/src/Convolution.hpp @@ -12,7 +12,7 @@ #include #include #include -#include "tensorflow/lite/kernels/internal/tensor.h" +#include namespace armnnDelegate { diff --git a/delegate/classic/src/SharedFunctions.cpp b/delegate/classic/src/SharedFunctions.cpp index 8de7d9c933..37e1c7fc64 100644 --- a/delegate/classic/src/SharedFunctions.cpp +++ b/delegate/classic/src/SharedFunctions.cpp @@ -8,10 +8,10 @@ #include -#include "tensorflow/lite/builtin_ops.h" -#include "tensorflow/lite/c/builtin_op_data.h" -#include "tensorflow/lite/c/common.h" -#include "tensorflow/lite/minimal_logging.h" +#include +#include +#include +#include namespace armnnDelegate { diff --git a/delegate/common/include/DelegateOptions.hpp b/delegate/common/include/DelegateOptions.hpp index 3737940d17..4d48451ef3 100644 --- a/delegate/common/include/DelegateOptions.hpp +++ b/delegate/common/include/DelegateOptions.hpp @@ -35,7 +35,6 @@ public: const armnn::Optional& logSeverityLevel = armnn::EmptyOptional(), const armnn::Optional& func = armnn::EmptyOptional()); - /** * This constructor processes delegate options in form of command line arguments. * It works in conjunction with the TfLite external delegate plugin. diff --git a/delegate/opaque/CMakeLists.txt b/delegate/opaque/CMakeLists.txt index 11b938bcff..6e345f5760 100644 --- a/delegate/opaque/CMakeLists.txt +++ b/delegate/opaque/CMakeLists.txt @@ -6,7 +6,8 @@ set(armnnOpaqueDelegateObject_sources) list(APPEND armnnOpaqueDelegateObject_sources include/armnn_delegate.hpp - include/Version.hpp) + include/Version.hpp + src/armnn_delegate.cpp) add_library(armnnOpaqueDelegateObject OBJECT ${armnnOpaqueDelegateObject_sources}) diff --git a/delegate/opaque/include/Version.hpp b/delegate/opaque/include/Version.hpp index 35c5928b6a..6b32cf482f 100644 --- a/delegate/opaque/include/Version.hpp +++ b/delegate/opaque/include/Version.hpp @@ -5,7 +5,7 @@ #pragma once -namespace armnnDelegate +namespace armnnOpaqueDelegate { /// Macro utils @@ -22,8 +22,8 @@ namespace armnnDelegate /// X = Major version number /// Y = Minor version number /// Z = Patch version number -#define DELEGATE_VERSION STRINGIFY_VALUE(OPAQUE_DELEGATE_MAJOR_VERSION) "." \ - STRINGIFY_VALUE(OPAQUE_DELEGATE_MINOR_VERSION) "." \ - STRINGIFY_VALUE(OPAQUE_DELEGATE_PATCH_VERSION) +#define OPAQUE_DELEGATE_VERSION STRINGIFY_VALUE(OPAQUE_DELEGATE_MAJOR_VERSION) "." \ + STRINGIFY_VALUE(OPAQUE_DELEGATE_MINOR_VERSION) "." \ + STRINGIFY_VALUE(OPAQUE_DELEGATE_PATCH_VERSION) } //namespace armnnDelegate \ No newline at end of file diff --git a/delegate/opaque/include/armnn_delegate.hpp b/delegate/opaque/include/armnn_delegate.hpp index 8957dc87d6..5522699e26 100644 --- a/delegate/opaque/include/armnn_delegate.hpp +++ b/delegate/opaque/include/armnn_delegate.hpp @@ -1,5 +1,5 @@ // -// Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved. +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. // SPDX-License-Identifier: MIT // @@ -7,32 +7,17 @@ #include -#include -#include -#include -#include -#include +#include +#include -#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 3) -#define ARMNN_POST_TFLITE_2_3 -#endif - -#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 4) -#define ARMNN_POST_TFLITE_2_4 -#endif - -#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5) -#define ARMNN_POST_TFLITE_2_5 -#endif - -namespace armnnDelegate +namespace armnnOpaqueDelegate { struct DelegateData { DelegateData(const std::vector& backends) - : m_Backends(backends) - , m_Network(nullptr, nullptr) + : m_Backends(backends) + , m_Network(nullptr, nullptr) {} const std::vector m_Backends; @@ -40,25 +25,25 @@ struct DelegateData std::vector m_OutputSlotForNode; }; -// Forward decleration for functions initializing the ArmNN Delegate -DelegateOptions TfLiteArmnnDelegateOptionsDefault(); +/// Forward declaration for functions initializing the ArmNN Delegate +::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault(); -TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options); +TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings); -void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate); +void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate); -TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate); +TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data); -/// ArmNN Delegate -class Delegate +/// ArmNN Opaque Delegate +class ArmnnOpaqueDelegate { friend class ArmnnSubgraph; public: - explicit Delegate(armnnDelegate::DelegateOptions options); + explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options); - TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context); + TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context); - TfLiteDelegate* GetDelegate(); + TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; } /// Retrieve version in X.Y.Z form static const std::string GetVersion(); @@ -70,18 +55,18 @@ private: armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options) { static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options); - // Instantiated on first use. + /// Instantiated on first use. return instance.get(); } - TfLiteDelegate m_Delegate = { + TfLiteOpaqueDelegateBuilder m_Builder = + { reinterpret_cast(this), // .data_ - DoPrepare, // .Prepare + nullptr, // .Prepare nullptr, // .CopyFromBufferHandle nullptr, // .CopyToBufferHandle nullptr, // .FreeBufferHandle kTfLiteDelegateFlagsNone, // .flags - nullptr, // .opaque_delegate_builder }; /// ArmNN Runtime pointer @@ -90,52 +75,62 @@ private: armnnDelegate::DelegateOptions m_Options; }; +static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; } + + + /// In order for the delegate to be loaded by TfLite +const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi(); + +extern const TfLiteStableDelegate TFL_TheStableDelegate = +{ + /*delegate_abi_version=*/ TFL_STABLE_DELEGATE_ABI_VERSION, + /*delegate_name=*/ "ArmnnDelegatePlugin", + /*delegate_version=*/ "1.0.0", + /*delegate_plugin=*/ GetArmnnDelegatePluginApi() +}; + /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph class ArmnnSubgraph { public: - static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext, - const TfLiteDelegateParams* parameters, - const Delegate* delegate); + static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext, + const TfLiteOpaqueDelegateParams* parameters, + const ArmnnOpaqueDelegate* delegate); - TfLiteStatus Prepare(TfLiteContext* tfLiteContext); + TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext); - TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode); + TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode); static TfLiteStatus VisitNode(DelegateData& delegateData, - TfLiteContext* tfLiteContext, - TfLiteRegistration* tfLiteRegistration, - TfLiteNode* tfLiteNode, + TfLiteOpaqueContext* tfLiteContext, + TfLiteRegistrationExternal* tfLiteRegistration, + TfLiteOpaqueNode* tfLiteNode, int nodeIndex); - private: ArmnnSubgraph(armnn::NetworkId networkId, armnn::IRuntime* runtime, std::vector& inputBindings, std::vector& outputBindings) - : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings) + : m_NetworkId(networkId) + , m_Runtime(runtime) + , m_InputBindings(inputBindings) + , m_OutputBindings(outputBindings) {} - static TfLiteStatus AddInputLayer(DelegateData& delegateData, - TfLiteContext* tfLiteContext, + TfLiteOpaqueContext* tfLiteContext, const TfLiteIntArray* inputs, std::vector& inputBindings); - static TfLiteStatus AddOutputLayer(DelegateData& delegateData, - TfLiteContext* tfLiteContext, + TfLiteOpaqueContext* tfLiteContext, const TfLiteIntArray* outputs, std::vector& outputBindings); - - /// The Network Id armnn::NetworkId m_NetworkId; - /// ArmNN Rumtime + /// ArmNN Runtime armnn::IRuntime* m_Runtime; - - // Binding information for inputs and outputs + /// Binding information for inputs and outputs std::vector m_InputBindings; std::vector m_OutputBindings; - }; -} // armnnDelegate namespace \ No newline at end of file +} // armnnOpaqueDelegate namespace \ No newline at end of file diff --git a/delegate/opaque/src/Activation.hpp b/delegate/opaque/src/Activation.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Activation.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/ArgMinMax.hpp b/delegate/opaque/src/ArgMinMax.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/ArgMinMax.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/BatchMatMul.hpp b/delegate/opaque/src/BatchMatMul.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/BatchMatMul.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/BatchSpace.hpp b/delegate/opaque/src/BatchSpace.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/BatchSpace.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Comparison.hpp b/delegate/opaque/src/Comparison.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Comparison.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Control.hpp b/delegate/opaque/src/Control.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Control.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Convolution.hpp b/delegate/opaque/src/Convolution.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Convolution.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/ElementwiseBinary.hpp b/delegate/opaque/src/ElementwiseBinary.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/ElementwiseBinary.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/ElementwiseUnary.hpp b/delegate/opaque/src/ElementwiseUnary.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/ElementwiseUnary.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Fill.hpp b/delegate/opaque/src/Fill.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Fill.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/FullyConnected.hpp b/delegate/opaque/src/FullyConnected.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/FullyConnected.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Gather.hpp b/delegate/opaque/src/Gather.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Gather.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/GatherNd.hpp b/delegate/opaque/src/GatherNd.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/GatherNd.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/LogicalBinary.hpp b/delegate/opaque/src/LogicalBinary.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/LogicalBinary.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Lstm.hpp b/delegate/opaque/src/Lstm.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Lstm.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/MultiLayerFacade.hpp b/delegate/opaque/src/MultiLayerFacade.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/MultiLayerFacade.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Normalization.hpp b/delegate/opaque/src/Normalization.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Normalization.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Pack.hpp b/delegate/opaque/src/Pack.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Pack.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Pad.hpp b/delegate/opaque/src/Pad.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Pad.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Pooling.hpp b/delegate/opaque/src/Pooling.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Pooling.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Prelu.hpp b/delegate/opaque/src/Prelu.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Prelu.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Quantization.hpp b/delegate/opaque/src/Quantization.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Quantization.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Redefine.hpp b/delegate/opaque/src/Redefine.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Redefine.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Reduce.hpp b/delegate/opaque/src/Reduce.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Reduce.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Resize.hpp b/delegate/opaque/src/Resize.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Resize.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Round.hpp b/delegate/opaque/src/Round.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Round.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Shape.hpp b/delegate/opaque/src/Shape.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Shape.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Slice.hpp b/delegate/opaque/src/Slice.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Slice.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Softmax.hpp b/delegate/opaque/src/Softmax.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Softmax.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/SpaceDepth.hpp b/delegate/opaque/src/SpaceDepth.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/SpaceDepth.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Split.hpp b/delegate/opaque/src/Split.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Split.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/StridedSlice.hpp b/delegate/opaque/src/StridedSlice.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/StridedSlice.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Transpose.hpp b/delegate/opaque/src/Transpose.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Transpose.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/UnidirectionalSequenceLstm.hpp b/delegate/opaque/src/UnidirectionalSequenceLstm.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/UnidirectionalSequenceLstm.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/Unpack.hpp b/delegate/opaque/src/Unpack.hpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/Unpack.hpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/opaque/src/armnn_delegate.cpp b/delegate/opaque/src/armnn_delegate.cpp new file mode 100644 index 0000000000..190c386666 --- /dev/null +++ b/delegate/opaque/src/armnn_delegate.cpp @@ -0,0 +1,136 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include + +#include + +#include "Activation.hpp" +#include "ArgMinMax.hpp" +#include "BatchMatMul.hpp" +#include "BatchSpace.hpp" +#include "Comparison.hpp" +#include "Convolution.hpp" +#include "Control.hpp" +#include "ElementwiseBinary.hpp" +#include "ElementwiseUnary.hpp" +#include "Fill.hpp" +#include "FullyConnected.hpp" +#include "Gather.hpp" +#include "GatherNd.hpp" +#include "LogicalBinary.hpp" +#include "Lstm.hpp" +#include "Normalization.hpp" +#include "Pack.hpp" +#include "Pad.hpp" +#include "Pooling.hpp" +#include "Prelu.hpp" +#include "Quantization.hpp" +#include "Redefine.hpp" +#include "Reduce.hpp" +#include "Resize.hpp" +#include "Round.hpp" +#include "Shape.hpp" +#include "Slice.hpp" +#include "StridedSlice.hpp" +#include "Softmax.hpp" +#include "SpaceDepth.hpp" +#include "Split.hpp" +#include "Transpose.hpp" +#include "UnidirectionalSequenceLstm.hpp" +#include "Unpack.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +namespace armnnOpaqueDelegate +{ + +ArmnnOpaqueDelegate::ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options) + : m_Options(std::move(options)) +{ + // Configures logging for ARMNN + if (m_Options.IsLoggingEnabled()) + { + armnn::ConfigureLogging(true, true, m_Options.GetLoggingSeverity()); + } + // Create/Get the static ArmNN Runtime. Note that the m_Runtime will be shared by all armnn_delegate + // instances so the RuntimeOptions cannot be altered for different armnn_delegate instances. + m_Runtime = GetRuntime(m_Options.GetRuntimeOptions()); + std::vector backends; + if (m_Runtime) + { + const armnn::BackendIdSet supportedDevices = m_Runtime->GetDeviceSpec().GetSupportedBackends(); + for (auto& backend : m_Options.GetBackends()) + { + if (std::find(supportedDevices.cbegin(), supportedDevices.cend(), backend) == supportedDevices.cend()) + { + TFLITE_LOG_PROD(tflite::TFLITE_LOG_INFO, + "TfLiteArmnnDelegate: Requested unknown backend %s", backend.Get().c_str()); + } + else + { + backends.push_back(backend); + } + } + } + + if (backends.empty()) + { + // No known backend specified + throw armnn::InvalidArgumentException("TfLiteArmnnOpaqueDelegate: No known backend specified."); + } + m_Options.SetBackends(backends); + + TFLITE_LOG_PROD_ONCE(tflite::TFLITE_LOG_INFO, "TfLiteArmnnOpaqueDelegate: Created TfLite ArmNN delegate."); +} + +TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings) +{ + // This method will always create Opaque Delegate with default settings until + // we have a DelegateOptions Constructor which can parse the void* settings + armnn::IgnoreUnused(settings); + auto options = TfLiteArmnnDelegateOptionsDefault(); + auto* armnnDelegate = new ::armnnOpaqueDelegate::ArmnnOpaqueDelegate(options); + return TfLiteOpaqueDelegateCreate(armnnDelegate->GetDelegateBuilder()); +} + +::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault() +{ + ::armnnDelegate::DelegateOptions options(armnn::Compute::CpuRef); + return options; +} + +void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate) +{ + if (tfLiteDelegate != nullptr) + { + delete static_cast<::armnnOpaqueDelegate::ArmnnOpaqueDelegate*>(TfLiteOpaqueDelegateGetData(tfLiteDelegate)); + TfLiteOpaqueDelegateDelete(tfLiteDelegate); + } +} + +const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi() +{ + static constexpr TfLiteOpaqueDelegatePlugin armnnPlugin{ + TfLiteArmnnOpaqueDelegateCreate, TfLiteArmnnOpaqueDelegateDelete, TfLiteArmnnOpaqueDelegateErrno}; + return &armnnPlugin; +} + +const std::string ArmnnOpaqueDelegate::GetVersion() { + return OPAQUE_DELEGATE_VERSION; +} + +} // armnnOpaqueDelegate namespace \ No newline at end of file diff --git a/delegate/opaque/src/armnn_external_delegate.cpp b/delegate/opaque/src/armnn_external_delegate.cpp new file mode 100644 index 0000000000..e16969768e --- /dev/null +++ b/delegate/opaque/src/armnn_external_delegate.cpp @@ -0,0 +1,4 @@ +// +// Copyright © 2023 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// diff --git a/delegate/test/ArmnnDelegateTest.cpp b/delegate/test/ArmnnDelegateTest.cpp index c23c702af8..4ef4ce9ed2 100644 --- a/delegate/test/ArmnnDelegateTest.cpp +++ b/delegate/test/ArmnnDelegateTest.cpp @@ -6,7 +6,9 @@ #define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN #include -#include +#include +#include +#include #include #include @@ -88,6 +90,42 @@ TEST_CASE ("ArmnnDelegateOptimizerOptionsRegistered") CHECK(tfLiteInterpreter != nullptr); } +TEST_CASE ("DelegateOptions_OpaqueDelegateDefault") +{ + // Check default options can be created + auto options = TfLiteArmnnDelegateOptionsDefault(); + armnnOpaqueDelegate::ArmnnOpaqueDelegate delegate(options); + + // Check version returns correctly + auto version = delegate.GetVersion(); + CHECK_EQ(version, OPAQUE_DELEGATE_VERSION); + + auto* builder = delegate.GetDelegateBuilder(); + CHECK(builder); + + // Check Opaque delegate created + auto opaqueDelegate = armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate(&options); + CHECK(opaqueDelegate); + + // Check Opaque Delegate can be deleted + CHECK(opaqueDelegate->opaque_delegate_builder->data); + armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateDelete(opaqueDelegate); +} + +TEST_CASE ("DelegateOptions_ClassicDelegateDefault") +{ + // Check default options can be created + auto options = TfLiteArmnnDelegateOptionsDefault(); + + // Check Classic delegate created + auto classicDelegate = armnnDelegate::TfLiteArmnnDelegateCreate(options); + CHECK(classicDelegate); + + // Check Classic Delegate can be deleted + CHECK(classicDelegate->data_); + armnnDelegate::TfLiteArmnnDelegateDelete(classicDelegate); +} + } } // namespace armnnDelegate -- cgit v1.2.1