Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 1 | // |
Colm Donelan | 253d1bb | 2024-03-04 22:19:26 +0000 | [diff] [blame] | 2 | // Copyright © 2023-2024 Arm Ltd and Contributors. All rights reserved. |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
| 4 | // |
| 5 | |
| 6 | #pragma once |
| 7 | |
| 8 | #include <DelegateOptions.hpp> |
Ryan OShea | 59f8f65 | 2023-05-11 20:37:53 +0100 | [diff] [blame] | 9 | #include <Version.hpp> |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 10 | |
Francis Murtagh | 3a9e7ba | 2023-04-26 15:58:39 +0100 | [diff] [blame] | 11 | #include <tensorflow/core/public/version.h> |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 12 | #include <tensorflow/lite/c/c_api_opaque.h> |
Matthew Sloyan | 76d0c4c | 2023-09-07 14:48:56 +0100 | [diff] [blame] | 13 | |
| 14 | #include <tensorflow/lite/acceleration/configuration/delegate_registry.h> |
| 15 | #include <tensorflow/lite/core/acceleration/configuration/c/stable_delegate.h> |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 16 | |
Francis Murtagh | 3a9e7ba | 2023-04-26 15:58:39 +0100 | [diff] [blame] | 17 | #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5) |
| 18 | #define ARMNN_POST_TFLITE_2_5 |
| 19 | #endif |
| 20 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 21 | namespace armnnOpaqueDelegate |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 22 | { |
| 23 | |
| 24 | struct DelegateData |
| 25 | { |
| 26 | DelegateData(const std::vector<armnn::BackendId>& backends) |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 27 | : m_Backends(backends) |
| 28 | , m_Network(nullptr, nullptr) |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 29 | {} |
| 30 | |
| 31 | const std::vector<armnn::BackendId> m_Backends; |
| 32 | armnn::INetworkPtr m_Network; |
| 33 | std::vector<armnn::IOutputSlot*> m_OutputSlotForNode; |
| 34 | }; |
| 35 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 36 | /// Forward declaration for functions initializing the ArmNN Delegate |
| 37 | ::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault(); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 38 | |
Teresa Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 39 | TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(armnnDelegate::DelegateOptions options); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 40 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 41 | void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 42 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 43 | TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 44 | |
Teresa Charlin | 19ad816 | 2023-10-04 11:17:03 +0100 | [diff] [blame] | 45 | armnnDelegate::DelegateOptions ParseArmNNSettings(const tflite::TFLiteSettings* tflite_settings); |
| 46 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 47 | /// ArmNN Opaque Delegate |
| 48 | class ArmnnOpaqueDelegate |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 49 | { |
| 50 | friend class ArmnnSubgraph; |
| 51 | public: |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 52 | explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 53 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 54 | TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 55 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 56 | TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; } |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 57 | |
| 58 | /// Retrieve version in X.Y.Z form |
| 59 | static const std::string GetVersion(); |
| 60 | |
| 61 | private: |
| 62 | /** |
| 63 | * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates. |
| 64 | */ |
| 65 | armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options) |
| 66 | { |
| 67 | static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options); |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 68 | /// Instantiated on first use. |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 69 | return instance.get(); |
| 70 | } |
| 71 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 72 | TfLiteOpaqueDelegateBuilder m_Builder = |
| 73 | { |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 74 | reinterpret_cast<void*>(this), // .data_ |
Ryan OShea | a37ccb0 | 2023-04-11 10:54:07 +0100 | [diff] [blame] | 75 | DoPrepare, // .Prepare |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 76 | nullptr, // .CopyFromBufferHandle |
| 77 | nullptr, // .CopyToBufferHandle |
| 78 | nullptr, // .FreeBufferHandle |
| 79 | kTfLiteDelegateFlagsNone, // .flags |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /// ArmNN Runtime pointer |
| 83 | armnn::IRuntime* m_Runtime; |
| 84 | /// ArmNN Delegate Options |
| 85 | armnnDelegate::DelegateOptions m_Options; |
| 86 | }; |
| 87 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 88 | static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; } |
| 89 | |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 90 | /// In order for the delegate to be loaded by TfLite |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 91 | const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi(); |
| 92 | |
Narumol Prangnawarat | 26654cb | 2023-05-03 16:08:11 +0100 | [diff] [blame] | 93 | using tflite::delegates::DelegatePluginInterface; |
| 94 | using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr; |
| 95 | |
| 96 | class ArmnnDelegatePlugin : public DelegatePluginInterface |
| 97 | { |
| 98 | public: |
Teresa Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 99 | static std::unique_ptr<ArmnnDelegatePlugin> New(const tflite::TFLiteSettings& tfliteSettings) |
Narumol Prangnawarat | 26654cb | 2023-05-03 16:08:11 +0100 | [diff] [blame] | 100 | { |
Teresa Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 101 | return std::make_unique<ArmnnDelegatePlugin>(tfliteSettings); |
Narumol Prangnawarat | 26654cb | 2023-05-03 16:08:11 +0100 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | tflite::delegates::TfLiteDelegatePtr Create() override |
| 105 | { |
Teresa Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 106 | return tflite::delegates::TfLiteDelegatePtr(TfLiteArmnnOpaqueDelegateCreate(m_delegateOptions), |
| 107 | TfLiteArmnnOpaqueDelegateDelete); |
Narumol Prangnawarat | 26654cb | 2023-05-03 16:08:11 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | int GetDelegateErrno(TfLiteOpaqueDelegate* from_delegate) override |
| 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | explicit ArmnnDelegatePlugin(const tflite::TFLiteSettings& tfliteSettings) |
Teresa Charlin | 3e4b608 | 2023-10-19 19:13:29 +0100 | [diff] [blame] | 116 | : m_delegateOptions(ParseArmNNSettings(&tfliteSettings)) |
| 117 | {} |
| 118 | |
| 119 | private: |
| 120 | armnnDelegate::DelegateOptions m_delegateOptions; |
Narumol Prangnawarat | 26654cb | 2023-05-03 16:08:11 +0100 | [diff] [blame] | 121 | }; |
| 122 | |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 123 | /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph |
| 124 | class ArmnnSubgraph |
| 125 | { |
| 126 | public: |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 127 | static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext, |
| 128 | const TfLiteOpaqueDelegateParams* parameters, |
| 129 | const ArmnnOpaqueDelegate* delegate); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 130 | |
Colm Donelan | 253d1bb | 2024-03-04 22:19:26 +0000 | [diff] [blame] | 131 | ~ArmnnSubgraph(); |
| 132 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 133 | TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 134 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 135 | TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 136 | |
| 137 | static TfLiteStatus VisitNode(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 138 | TfLiteOpaqueContext* tfLiteContext, |
| 139 | TfLiteRegistrationExternal* tfLiteRegistration, |
| 140 | TfLiteOpaqueNode* tfLiteNode, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 141 | int nodeIndex); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 142 | private: |
| 143 | ArmnnSubgraph(armnn::NetworkId networkId, |
| 144 | armnn::IRuntime* runtime, |
| 145 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 146 | std::vector<armnn::BindingPointInfo>& outputBindings) |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 147 | : m_NetworkId(networkId) |
| 148 | , m_Runtime(runtime) |
| 149 | , m_InputBindings(inputBindings) |
| 150 | , m_OutputBindings(outputBindings) |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 151 | {} |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 152 | static TfLiteStatus AddInputLayer(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 153 | TfLiteOpaqueContext* tfLiteContext, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 154 | const TfLiteIntArray* inputs, |
| 155 | std::vector<armnn::BindingPointInfo>& inputBindings); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 156 | static TfLiteStatus AddOutputLayer(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 157 | TfLiteOpaqueContext* tfLiteContext, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 158 | const TfLiteIntArray* outputs, |
| 159 | std::vector<armnn::BindingPointInfo>& outputBindings); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 160 | /// The Network Id |
| 161 | armnn::NetworkId m_NetworkId; |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 162 | /// ArmNN Runtime |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 163 | armnn::IRuntime* m_Runtime; |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 164 | /// Binding information for inputs and outputs |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 165 | std::vector<armnn::BindingPointInfo> m_InputBindings; |
| 166 | std::vector<armnn::BindingPointInfo> m_OutputBindings; |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 167 | }; |
| 168 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 169 | } // armnnOpaqueDelegate namespace |