blob: 5522699e2617e4a9569b25a5f9d4aac01142206f [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 Murtaghc4fb0dd2023-03-16 17:01:56 +000010#include <tensorflow/lite/c/c_api_opaque.h>
11#include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h>
Teresa Charlinad1b3d72023-03-14 12:10:28 +000012
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000013namespace armnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000014{
15
16struct DelegateData
17{
18 DelegateData(const std::vector<armnn::BackendId>& backends)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000019 : m_Backends(backends)
20 , m_Network(nullptr, nullptr)
Teresa Charlinad1b3d72023-03-14 12:10:28 +000021 {}
22
23 const std::vector<armnn::BackendId> m_Backends;
24 armnn::INetworkPtr m_Network;
25 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
26};
27
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000028/// Forward declaration for functions initializing the ArmNN Delegate
29::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
Teresa Charlinad1b3d72023-03-14 12:10:28 +000030
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000031TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000032
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000033void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000034
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000035TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000036
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000037/// ArmNN Opaque Delegate
38class ArmnnOpaqueDelegate
Teresa Charlinad1b3d72023-03-14 12:10:28 +000039{
40 friend class ArmnnSubgraph;
41public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000042 explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000043
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000044 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000045
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000046 TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
Teresa Charlinad1b3d72023-03-14 12:10:28 +000047
48 /// Retrieve version in X.Y.Z form
49 static const std::string GetVersion();
50
51private:
52 /**
53 * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
54 */
55 armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
56 {
57 static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000058 /// Instantiated on first use.
Teresa Charlinad1b3d72023-03-14 12:10:28 +000059 return instance.get();
60 }
61
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000062 TfLiteOpaqueDelegateBuilder m_Builder =
63 {
Teresa Charlinad1b3d72023-03-14 12:10:28 +000064 reinterpret_cast<void*>(this), // .data_
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000065 nullptr, // .Prepare
Teresa Charlinad1b3d72023-03-14 12:10:28 +000066 nullptr, // .CopyFromBufferHandle
67 nullptr, // .CopyToBufferHandle
68 nullptr, // .FreeBufferHandle
69 kTfLiteDelegateFlagsNone, // .flags
Teresa Charlinad1b3d72023-03-14 12:10:28 +000070 };
71
72 /// ArmNN Runtime pointer
73 armnn::IRuntime* m_Runtime;
74 /// ArmNN Delegate Options
75 armnnDelegate::DelegateOptions m_Options;
76};
77
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000078static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
79
80
81 /// In order for the delegate to be loaded by TfLite
82const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
83
84extern const TfLiteStableDelegate TFL_TheStableDelegate =
85{
86 /*delegate_abi_version=*/ TFL_STABLE_DELEGATE_ABI_VERSION,
87 /*delegate_name=*/ "ArmnnDelegatePlugin",
88 /*delegate_version=*/ "1.0.0",
89 /*delegate_plugin=*/ GetArmnnDelegatePluginApi()
90};
91
Teresa Charlinad1b3d72023-03-14 12:10:28 +000092/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
93class ArmnnSubgraph
94{
95public:
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +000096 static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
97 const TfLiteOpaqueDelegateParams* parameters,
98 const ArmnnOpaqueDelegate* delegate);
Teresa Charlinad1b3d72023-03-14 12:10:28 +000099
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000100 TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000101
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000102 TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000103
104 static TfLiteStatus VisitNode(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000105 TfLiteOpaqueContext* tfLiteContext,
106 TfLiteRegistrationExternal* tfLiteRegistration,
107 TfLiteOpaqueNode* tfLiteNode,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000108 int nodeIndex);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000109private:
110 ArmnnSubgraph(armnn::NetworkId networkId,
111 armnn::IRuntime* runtime,
112 std::vector<armnn::BindingPointInfo>& inputBindings,
113 std::vector<armnn::BindingPointInfo>& outputBindings)
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000114 : m_NetworkId(networkId)
115 , m_Runtime(runtime)
116 , m_InputBindings(inputBindings)
117 , m_OutputBindings(outputBindings)
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000118 {}
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000119 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000120 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000121 const TfLiteIntArray* inputs,
122 std::vector<armnn::BindingPointInfo>& inputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000123 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000124 TfLiteOpaqueContext* tfLiteContext,
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000125 const TfLiteIntArray* outputs,
126 std::vector<armnn::BindingPointInfo>& outputBindings);
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000127 /// The Network Id
128 armnn::NetworkId m_NetworkId;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000129 /// ArmNN Runtime
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000130 armnn::IRuntime* m_Runtime;
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000131 /// Binding information for inputs and outputs
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000132 std::vector<armnn::BindingPointInfo> m_InputBindings;
133 std::vector<armnn::BindingPointInfo> m_OutputBindings;
Teresa Charlinad1b3d72023-03-14 12:10:28 +0000134};
135
Francis Murtaghc4fb0dd2023-03-16 17:01:56 +0000136} // armnnOpaqueDelegate namespace