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