blob: b213211ae94587cc0ce99c8369489808834ffeb4 [file] [log] [blame]
Sadik Armagan3c24f432020-10-19 17:35:30 +01001//
2// Copyright © 2020 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
Sadik Armagan5d03e312020-11-17 16:43:56 +00006#pragma once
Sadik Armagan3c24f432020-10-19 17:35:30 +01007
8#include "DelegateOptions.hpp"
9
10#include <tensorflow/lite/builtin_ops.h>
11#include <tensorflow/lite/c/builtin_op_data.h>
12#include <tensorflow/lite/c/common.h>
13#include <tensorflow/lite/minimal_logging.h>
14
15namespace armnnDelegate
16{
17
Sadik Armagan62483be2020-10-23 17:14:43 +010018struct DelegateData
19{
20 DelegateData(const std::vector<armnn::BackendId>& backends)
21 : m_Backends(backends)
22 , m_Network(nullptr, nullptr)
23 {}
Sadik Armagan3c24f432020-10-19 17:35:30 +010024
Sadik Armagan62483be2020-10-23 17:14:43 +010025 const std::vector<armnn::BackendId> m_Backends;
26 armnn::INetworkPtr m_Network;
27 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
28};
29
30// Forward decleration for functions initializing the ArmNN Delegate
31DelegateOptions TfLiteArmnnDelegateOptionsDefault();
32
33TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options);
34
35void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
36
37TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
38
39/// ArmNN Delegate
Sadik Armagan3c24f432020-10-19 17:35:30 +010040class Delegate
41{
42 friend class ArmnnSubgraph;
43public:
44 explicit Delegate(armnnDelegate::DelegateOptions options);
45
Sadik Armagan62483be2020-10-23 17:14:43 +010046 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
Sadik Armagan3c24f432020-10-19 17:35:30 +010047
48 TfLiteDelegate* GetDelegate();
49
Matthew Sloyanac001ee2021-02-03 10:43:04 +000050 /// Retrieve version in X.Y.Z form
51 static const std::string GetVersion();
52
Sadik Armagan3c24f432020-10-19 17:35:30 +010053private:
54 TfLiteDelegate m_Delegate = {
55 reinterpret_cast<void*>(this), // .data_
Sadik Armagan62483be2020-10-23 17:14:43 +010056 DoPrepare, // .Prepare
Sadik Armagan3c24f432020-10-19 17:35:30 +010057 nullptr, // .CopyFromBufferHandle
58 nullptr, // .CopyToBufferHandle
59 nullptr, // .FreeBufferHandle
60 kTfLiteDelegateFlagsNone, // .flags
61 };
62
Sadik Armagan62483be2020-10-23 17:14:43 +010063 /// ArmNN Runtime pointer
Sadik Armagan3c24f432020-10-19 17:35:30 +010064 armnn::IRuntimePtr m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +010065 /// ArmNN Delegate Options
Sadik Armagan3c24f432020-10-19 17:35:30 +010066 armnnDelegate::DelegateOptions m_Options;
67};
68
69/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
70class ArmnnSubgraph
71{
72public:
73 static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
74 const TfLiteDelegateParams* parameters,
75 const Delegate* delegate);
76
77 TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
78
Sadik Armagan62483be2020-10-23 17:14:43 +010079 TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
Sadik Armagan3c24f432020-10-19 17:35:30 +010080
Sadik Armagan62483be2020-10-23 17:14:43 +010081 static TfLiteStatus VisitNode(DelegateData& delegateData,
Sadik Armagan3c24f432020-10-19 17:35:30 +010082 TfLiteContext* tfLiteContext,
83 TfLiteRegistration* tfLiteRegistration,
84 TfLiteNode* tfLiteNode,
85 int nodeIndex);
86
87private:
Sadik Armagan62483be2020-10-23 17:14:43 +010088 ArmnnSubgraph(armnn::NetworkId networkId,
89 armnn::IRuntime* runtime,
90 std::vector<armnn::BindingPointInfo>& inputBindings,
91 std::vector<armnn::BindingPointInfo>& outputBindings)
92 : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
Sadik Armagan3c24f432020-10-19 17:35:30 +010093 {}
94
Sadik Armagan62483be2020-10-23 17:14:43 +010095 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
96 TfLiteContext* tfLiteContext,
97 const TfLiteIntArray* inputs,
98 std::vector<armnn::BindingPointInfo>& inputBindings);
99
100 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
101 TfLiteContext* tfLiteContext,
102 const TfLiteIntArray* outputs,
103 std::vector<armnn::BindingPointInfo>& outputBindings);
104
105
Sadik Armagan3c24f432020-10-19 17:35:30 +0100106 /// The Network Id
107 armnn::NetworkId m_NetworkId;
108 /// ArmNN Rumtime
109 armnn::IRuntime* m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +0100110
111 // Binding information for inputs and outputs
112 std::vector<armnn::BindingPointInfo> m_InputBindings;
113 std::vector<armnn::BindingPointInfo> m_OutputBindings;
114
Sadik Armagan3c24f432020-10-19 17:35:30 +0100115};
116
Sadik Armagan3c24f432020-10-19 17:35:30 +0100117} // armnnDelegate namespace
118
Sadik Armagan3c24f432020-10-19 17:35:30 +0100119