blob: ff860c4b1ccc48726d2972dc18e7fd310a4b7816 [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>
13#include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h>
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010014#include <tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000015
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010016#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
17#define ARMNN_POST_TFLITE_2_5
18#endif
19
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000020namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000021{
22
23struct DelegateData
24{
25 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000026 : m_Backends(backends)
27 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000028 {}
29
30 const std::vector<armnn::BackendId> m_Backends;
31 armnn::INetworkPtr m_Network;
32 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
33};
34
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000035/// Forward declaration for functions initializing the ArmNN Delegate
36::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000037
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000038TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000039
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000040void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000041
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000042TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000043
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000044/// ArmNN Opaque Delegate
45class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000046{
47 friend class ArmnnSubgraph;
48public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000049 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000050
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000051 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000052
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000053 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000054
55 /// Retrieve version in X.Y.Z form
56 static const std::string GetVersion();
57
58private:
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 Murtaghc4fb0dd2023-03-16 17:01:56 +000065 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000066 return instance.get();
67 }
68
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000069 TfLiteOpaqueDelegateBuilder m_Builder =
70 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000071 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010072 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000073 nullptr, // .CopyFromBufferHandle
74 nullptr, // .CopyToBufferHandle
75 nullptr, // .FreeBufferHandle
76 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000077 };
78
79 /// ArmNN Runtime pointer
80 armnn::IRuntime* m_Runtime;
81 /// ArmNN Delegate Options
82 armnnDelegate::DelegateOptions m_Options;
83};
84
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000085static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
86
Matthew Sloyan65c21a12023-04-04 12:06:14 +010087/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000088const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
89
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010090using tflite::delegates::DelegatePluginInterface;
91using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr;
92
93class ArmnnDelegatePlugin : public DelegatePluginInterface
94{
95public:
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 Charlinad1b3d72023-03-14 12:10:28 +0000119/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
120class ArmnnSubgraph
121{
122public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000123 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
124 const TfLiteOpaqueDelegateParams* parameters,
125 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000126
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000127 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000128
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000129 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000130
131 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000132 TfLiteOpaqueContext* tfLiteContext,
133 TfLiteRegistrationExternal* tfLiteRegistration,
134 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000135 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000136private:
137 ArmnnSubgraph(armnn::NetworkId networkId,
138 armnn::IRuntime* runtime,
139 std::vector<armnn::BindingPointInfo>& inputBindings,
140 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000141 : m_NetworkId(networkId)
142 , m_Runtime(runtime)
143 , m_InputBindings(inputBindings)
144 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000145 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000146 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000147 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000148 const TfLiteIntArray* inputs,
149 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000150 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000151 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000152 const TfLiteIntArray* outputs,
153 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000154 /// The Network Id
155 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000156 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000157 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000158 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000159 std::vector<armnn::BindingPointInfo> m_InputBindings;
160 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000161};
162
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000163} // armnnOpaqueDelegate namespace