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