blob: ae8555688412e87ff139dcba49206444440a151f [file] [log] [blame]
Teresa Charlinad1b3d72023-03-14 12:10:28 +00001//
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +00002// Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
Teresa Charlinad1b3d72023-03-14 12:10:28 +00003// SPDX-License-Identifier: MIT
4//
5
6#pragma once
7
8#include <DelegateOptions.hpp>
Ryan OShea59f8f652023-05-11 20:37:53 +01009#include <Version.hpp>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000010
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010011#include <tensorflow/core/public/version.h>
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000012#include <tensorflow/lite/c/c_api_opaque.h>
Matthew Sloyan76d0c4c2023-09-07 14:48:56 +010013
14#include <tensorflow/lite/acceleration/configuration/delegate_registry.h>
15#include <tensorflow/lite/core/acceleration/configuration/c/stable_delegate.h>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000016
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010017#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
18#define ARMNN_POST_TFLITE_2_5
19#endif
20
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000021namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000022{
23
24struct DelegateData
25{
26 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000027 : m_Backends(backends)
28 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000029 {}
30
31 const std::vector<armnn::BackendId> m_Backends;
32 armnn::INetworkPtr m_Network;
33 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
34};
35
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000036/// Forward declaration for functions initializing the ArmNN Delegate
37::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000038
Teresa Charlin3e4b6082023-10-19 19:13:29 +010039TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000040
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000041void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000042
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000043TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000044
Teresa Charlin19ad8162023-10-04 11:17:03 +010045armnnDelegate::DelegateOptions ParseArmNNSettings(const tflite::TFLiteSettings* tflite_settings);
46
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000047/// ArmNN Opaque Delegate
48class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000049{
50 friend class ArmnnSubgraph;
51public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000052 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000053
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000054 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000055
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000056 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000057
58 /// Retrieve version in X.Y.Z form
59 static const std::string GetVersion();
60
61private:
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 Murtaghc4fb0dd2023-03-16 17:01:56 +000068 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000069 return instance.get();
70 }
71
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000072 TfLiteOpaqueDelegateBuilder m_Builder =
73 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000074 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010075 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000076 nullptr, // .CopyFromBufferHandle
77 nullptr, // .CopyToBufferHandle
78 nullptr, // .FreeBufferHandle
79 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000080 };
81
82 /// ArmNN Runtime pointer
83 armnn::IRuntime* m_Runtime;
84 /// ArmNN Delegate Options
85 armnnDelegate::DelegateOptions m_Options;
86};
87
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000088static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
89
Matthew Sloyan65c21a12023-04-04 12:06:14 +010090/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000091const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
92
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010093using tflite::delegates::DelegatePluginInterface;
94using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr;
95
96class ArmnnDelegatePlugin : public DelegatePluginInterface
97{
98public:
Teresa Charlin3e4b6082023-10-19 19:13:29 +010099 static std::unique_ptr<ArmnnDelegatePlugin> New(const tflite::TFLiteSettings& tfliteSettings)
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100100 {
Teresa Charlin3e4b6082023-10-19 19:13:29 +0100101 return std::make_unique<ArmnnDelegatePlugin>(tfliteSettings);
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100102 }
103
104 tflite::delegates::TfLiteDelegatePtr Create() override
105 {
Teresa Charlin3e4b6082023-10-19 19:13:29 +0100106 return tflite::delegates::TfLiteDelegatePtr(TfLiteArmnnOpaqueDelegateCreate(m_delegateOptions),
107 TfLiteArmnnOpaqueDelegateDelete);
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100108 }
109
110 int GetDelegateErrno(TfLiteOpaqueDelegate* from_delegate) override
111 {
112 return 0;
113 }
114
115 explicit ArmnnDelegatePlugin(const tflite::TFLiteSettings& tfliteSettings)
Teresa Charlin3e4b6082023-10-19 19:13:29 +0100116 : m_delegateOptions(ParseArmNNSettings(&tfliteSettings))
117 {}
118
119private:
120 armnnDelegate::DelegateOptions m_delegateOptions;
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +0100121};
122
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000123/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
124class ArmnnSubgraph
125{
126public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000127 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
128 const TfLiteOpaqueDelegateParams* parameters,
129 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000130
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000131 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000132
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000133 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000134
135 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000136 TfLiteOpaqueContext* tfLiteContext,
137 TfLiteRegistrationExternal* tfLiteRegistration,
138 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000139 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000140private:
141 ArmnnSubgraph(armnn::NetworkId networkId,
142 armnn::IRuntime* runtime,
143 std::vector<armnn::BindingPointInfo>& inputBindings,
144 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000145 : m_NetworkId(networkId)
146 , m_Runtime(runtime)
147 , m_InputBindings(inputBindings)
148 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000149 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000150 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000151 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000152 const TfLiteIntArray* inputs,
153 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000154 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000155 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000156 const TfLiteIntArray* outputs,
157 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000158 /// The Network Id
159 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000160 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000161 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000162 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000163 std::vector<armnn::BindingPointInfo> m_InputBindings;
164 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000165};
166
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000167} // armnnOpaqueDelegate namespace