blob: 653015a63c8cccb8dfd63254ee18d4c71295d44b [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>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000013
Francis Murtagh3a9e7ba2023-04-26 15:58:39 +010014#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
15#define ARMNN_POST_TFLITE_2_5
16#endif
17
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000018namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000019{
20
21struct DelegateData
22{
23 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000024 : m_Backends(backends)
25 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000026 {}
27
28 const std::vector<armnn::BackendId> m_Backends;
29 armnn::INetworkPtr m_Network;
30 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
31};
32
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000033/// Forward declaration for functions initializing the ArmNN Delegate
34::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000035
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000036TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000037
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000038void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000039
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000040TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000041
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000042/// ArmNN Opaque Delegate
43class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000044{
45 friend class ArmnnSubgraph;
46public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000047 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000048
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000049 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000050
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000051 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000052
53 /// Retrieve version in X.Y.Z form
54 static const std::string GetVersion();
55
56private:
57 /**
58 * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
59 */
60 armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
61 {
62 static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000063 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000064 return instance.get();
65 }
66
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000067 TfLiteOpaqueDelegateBuilder m_Builder =
68 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000069 reinterpret_cast<void*>(this), // .data_
Ryan OSheaa37ccb02023-04-11 10:54:07 +010070 DoPrepare, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000071 nullptr, // .CopyFromBufferHandle
72 nullptr, // .CopyToBufferHandle
73 nullptr, // .FreeBufferHandle
74 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000075 };
76
77 /// ArmNN Runtime pointer
78 armnn::IRuntime* m_Runtime;
79 /// ArmNN Delegate Options
80 armnnDelegate::DelegateOptions m_Options;
81};
82
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000083static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
84
Matthew Sloyan65c21a12023-04-04 12:06:14 +010085/// In order for the delegate to be loaded by TfLite
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000086const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
87
Matthew Sloyan65c21a12023-04-04 12:06:14 +010088extern const TfLiteStableDelegate TFL_TheStableDelegate;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000089
Teresa Charlinad1b3d72023-03-14 12:10:28 +000090/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
91class ArmnnSubgraph
92{
93public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000094 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
95 const TfLiteOpaqueDelegateParams* parameters,
96 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000097
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000098 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000099
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000100 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000101
102 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000103 TfLiteOpaqueContext* tfLiteContext,
104 TfLiteRegistrationExternal* tfLiteRegistration,
105 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000106 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000107private:
108 ArmnnSubgraph(armnn::NetworkId networkId,
109 armnn::IRuntime* runtime,
110 std::vector<armnn::BindingPointInfo>& inputBindings,
111 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000112 : m_NetworkId(networkId)
113 , m_Runtime(runtime)
114 , m_InputBindings(inputBindings)
115 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000116 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000117 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000118 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000119 const TfLiteIntArray* inputs,
120 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000121 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000122 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000123 const TfLiteIntArray* outputs,
124 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000125 /// The Network Id
126 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000127 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000128 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000129 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000130 std::vector<armnn::BindingPointInfo> m_InputBindings;
131 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000132};
133
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000134} // armnnOpaqueDelegate namespace