blob: bb6451f649ee3a964797a2bc823be471059b159c [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>
9
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010010#include <tensorflow/core/public/version.h>
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000011#include <tensorflow/lite/c/c_api_opaque.h>
12#include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h>
Narumol Prangnawarat26654cb2023-05-03 16:08:11 +010013#include <tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000014
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010015#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
16#define ARMNN_POST_TFLITE_2_5
17#endif
18
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000019namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000020{
21
22struct DelegateData
23{
24 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000025 : m_Backends(backends)
26 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000027 {}
28
29 const std::vector<armnn::BackendId> m_Backends;
30 armnn::INetworkPtr m_Network;
31 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
32};
33
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000034/// Forward declaration for functions initializing the ArmNN Delegate
35::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000036
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000037TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000038
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000039void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000040
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000041TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000042
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000043/// ArmNN Opaque Delegate
44class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000045{
46 friend class ArmnnSubgraph;
47public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000048 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000049
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000050 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000051
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000052 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000053
54 /// Retrieve version in X.Y.Z form
55 static const std::string GetVersion();
56
57private:
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 Murtaghc4fb0dd2023-03-16 17:01:56 +000064 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000065 return instance.get();
66 }
67
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000068 TfLiteOpaqueDelegateBuilder m_Builder =
69 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000070 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010071 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000072 nullptr, // .CopyFromBufferHandle
73 nullptr, // .CopyToBufferHandle
74 nullptr, // .FreeBufferHandle
75 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000076 };
77
78 /// ArmNN Runtime pointer
79 armnn::IRuntime* m_Runtime;
80 /// ArmNN Delegate Options
81 armnnDelegate::DelegateOptions m_Options;
82};
83
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000084static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
85
Matthew Sloyan65c21a12023-04-04 12:06:14 +010086/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000087const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
88
Matthew Sloyan65c21a12023-04-04 12:06:14 +010089extern const TfLiteStableDelegate TFL_TheStableDelegate;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000090
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