blob: 474d5978c9eaf7d092efa559ae4156643ab6de23 [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
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000039TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
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
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000045/// ArmNN Opaque Delegate
46class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000047{
48 friend class ArmnnSubgraph;
49public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000050 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000051
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000052 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000053
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000054 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000055
56 /// Retrieve version in X.Y.Z form
57 static const std::string GetVersion();
58
59private:
60 /**
61 * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
62 */
63 armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
64 {
65 static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000066 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000067 return instance.get();
68 }
69
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000070 TfLiteOpaqueDelegateBuilder m_Builder =
71 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000072 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010073 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000074 nullptr, // .CopyFromBufferHandle
75 nullptr, // .CopyToBufferHandle
76 nullptr, // .FreeBufferHandle
77 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000078 };
79
80 /// ArmNN Runtime pointer
81 armnn::IRuntime* m_Runtime;
82 /// ArmNN Delegate Options
83 armnnDelegate::DelegateOptions m_Options;
84};
85
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000086static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
87
Matthew Sloyan65c21a12023-04-04 12:06:14 +010088/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000089const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
90
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010091using tflite::delegates::DelegatePluginInterface;
92using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr;
93
94class ArmnnDelegatePlugin : public DelegatePluginInterface
95{
96public:
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 Charlinad1b3d72023-03-14 12:10:28 +0000120/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
121class ArmnnSubgraph
122{
123public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000124 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
125 const TfLiteOpaqueDelegateParams* parameters,
126 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000127
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000128 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000129
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000130 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000131
132 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000133 TfLiteOpaqueContext* tfLiteContext,
134 TfLiteRegistrationExternal* tfLiteRegistration,
135 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000136 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000137private:
138 ArmnnSubgraph(armnn::NetworkId networkId,
139 armnn::IRuntime* runtime,
140 std::vector<armnn::BindingPointInfo>& inputBindings,
141 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000142 : m_NetworkId(networkId)
143 , m_Runtime(runtime)
144 , m_InputBindings(inputBindings)
145 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000146 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000147 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000148 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000149 const TfLiteIntArray* inputs,
150 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000151 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000152 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000153 const TfLiteIntArray* outputs,
154 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000155 /// The Network Id
156 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000157 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000158 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000159 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000160 std::vector<armnn::BindingPointInfo> m_InputBindings;
161 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000162};
163
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000164} // armnnOpaqueDelegate namespace