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 | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 10 | #include <tensorflow/lite/c/c_api_opaque.h> |
| 11 | #include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h> |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 12 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 13 | namespace armnnOpaqueDelegate |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 14 | { |
| 15 | |
| 16 | struct DelegateData |
| 17 | { |
| 18 | DelegateData(const std::vector<armnn::BackendId>& backends) |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 19 | : m_Backends(backends) |
| 20 | , m_Network(nullptr, nullptr) |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 21 | {} |
| 22 | |
| 23 | const std::vector<armnn::BackendId> m_Backends; |
| 24 | armnn::INetworkPtr m_Network; |
| 25 | std::vector<armnn::IOutputSlot*> m_OutputSlotForNode; |
| 26 | }; |
| 27 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 28 | /// Forward declaration for functions initializing the ArmNN Delegate |
| 29 | ::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault(); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 30 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 31 | TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 32 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 33 | void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 34 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 35 | TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data); |
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 | /// ArmNN Opaque Delegate |
| 38 | class ArmnnOpaqueDelegate |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 39 | { |
| 40 | friend class ArmnnSubgraph; |
| 41 | public: |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 42 | explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options); |
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 | TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 45 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 46 | TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; } |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 47 | |
| 48 | /// Retrieve version in X.Y.Z form |
| 49 | static const std::string GetVersion(); |
| 50 | |
| 51 | private: |
| 52 | /** |
| 53 | * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates. |
| 54 | */ |
| 55 | armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options) |
| 56 | { |
| 57 | static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options); |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 58 | /// Instantiated on first use. |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 59 | return instance.get(); |
| 60 | } |
| 61 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 62 | TfLiteOpaqueDelegateBuilder m_Builder = |
| 63 | { |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 64 | reinterpret_cast<void*>(this), // .data_ |
Ryan OShea | a37ccb0 | 2023-04-11 10:54:07 +0100 | [diff] [blame] | 65 | DoPrepare, // .Prepare |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 66 | nullptr, // .CopyFromBufferHandle |
| 67 | nullptr, // .CopyToBufferHandle |
| 68 | nullptr, // .FreeBufferHandle |
| 69 | kTfLiteDelegateFlagsNone, // .flags |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | /// ArmNN Runtime pointer |
| 73 | armnn::IRuntime* m_Runtime; |
| 74 | /// ArmNN Delegate Options |
| 75 | armnnDelegate::DelegateOptions m_Options; |
| 76 | }; |
| 77 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 78 | static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; } |
| 79 | |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 80 | /// In order for the delegate to be loaded by TfLite |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 81 | const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi(); |
| 82 | |
Matthew Sloyan | 65c21a1 | 2023-04-04 12:06:14 +0100 | [diff] [blame] | 83 | extern const TfLiteStableDelegate TFL_TheStableDelegate; |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 84 | |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 85 | /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph |
| 86 | class ArmnnSubgraph |
| 87 | { |
| 88 | public: |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 89 | static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext, |
| 90 | const TfLiteOpaqueDelegateParams* parameters, |
| 91 | const ArmnnOpaqueDelegate* delegate); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 92 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 93 | TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 94 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 95 | TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 96 | |
| 97 | static TfLiteStatus VisitNode(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 98 | TfLiteOpaqueContext* tfLiteContext, |
| 99 | TfLiteRegistrationExternal* tfLiteRegistration, |
| 100 | TfLiteOpaqueNode* tfLiteNode, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 101 | int nodeIndex); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 102 | private: |
| 103 | ArmnnSubgraph(armnn::NetworkId networkId, |
| 104 | armnn::IRuntime* runtime, |
| 105 | std::vector<armnn::BindingPointInfo>& inputBindings, |
| 106 | std::vector<armnn::BindingPointInfo>& outputBindings) |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 107 | : m_NetworkId(networkId) |
| 108 | , m_Runtime(runtime) |
| 109 | , m_InputBindings(inputBindings) |
| 110 | , m_OutputBindings(outputBindings) |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 111 | {} |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 112 | static TfLiteStatus AddInputLayer(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 113 | TfLiteOpaqueContext* tfLiteContext, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 114 | const TfLiteIntArray* inputs, |
| 115 | std::vector<armnn::BindingPointInfo>& inputBindings); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 116 | static TfLiteStatus AddOutputLayer(DelegateData& delegateData, |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 117 | TfLiteOpaqueContext* tfLiteContext, |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 118 | const TfLiteIntArray* outputs, |
| 119 | std::vector<armnn::BindingPointInfo>& outputBindings); |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 120 | /// The Network Id |
| 121 | armnn::NetworkId m_NetworkId; |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 122 | /// ArmNN Runtime |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 123 | armnn::IRuntime* m_Runtime; |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 124 | /// Binding information for inputs and outputs |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 125 | std::vector<armnn::BindingPointInfo> m_InputBindings; |
| 126 | std::vector<armnn::BindingPointInfo> m_OutputBindings; |
Teresa Charlin | ad1b3d7 | 2023-03-14 12:10:28 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
Francis Murtagh | c4fb0dd | 2023-03-16 17:01:56 +0000 | [diff] [blame] | 129 | } // armnnOpaqueDelegate namespace |