blob: 8aaf255a9d9f7b41183b94d91bed98ef6a7b7e69 [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>
Matthew Sloyan81ec9942021-10-12 10:26:30 +010014#include <tensorflow/lite/version.h>
15
16#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 3)
17#define ARMNN_POST_TFLITE_2_3
18#endif
19
20#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
21#define ARMNN_POST_TFLITE_2_5
22#endif
Sadik Armagan3c24f432020-10-19 17:35:30 +010023
24namespace armnnDelegate
25{
26
Sadik Armagan62483be2020-10-23 17:14:43 +010027struct DelegateData
28{
29 DelegateData(const std::vector<armnn::BackendId>& backends)
30 : m_Backends(backends)
31 , m_Network(nullptr, nullptr)
32 {}
Sadik Armagan3c24f432020-10-19 17:35:30 +010033
Sadik Armagan62483be2020-10-23 17:14:43 +010034 const std::vector<armnn::BackendId> m_Backends;
35 armnn::INetworkPtr m_Network;
36 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
37};
38
39// Forward decleration for functions initializing the ArmNN Delegate
40DelegateOptions TfLiteArmnnDelegateOptionsDefault();
41
42TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options);
43
44void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
45
46TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
47
48/// ArmNN Delegate
Sadik Armagan3c24f432020-10-19 17:35:30 +010049class Delegate
50{
51 friend class ArmnnSubgraph;
52public:
53 explicit Delegate(armnnDelegate::DelegateOptions options);
54
Sadik Armagan62483be2020-10-23 17:14:43 +010055 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
Sadik Armagan3c24f432020-10-19 17:35:30 +010056
57 TfLiteDelegate* GetDelegate();
58
Matthew Sloyanac001ee2021-02-03 10:43:04 +000059 /// Retrieve version in X.Y.Z form
60 static const std::string GetVersion();
61
Sadik Armagan3c24f432020-10-19 17:35:30 +010062private:
63 TfLiteDelegate m_Delegate = {
64 reinterpret_cast<void*>(this), // .data_
Sadik Armagan62483be2020-10-23 17:14:43 +010065 DoPrepare, // .Prepare
Sadik Armagan3c24f432020-10-19 17:35:30 +010066 nullptr, // .CopyFromBufferHandle
67 nullptr, // .CopyToBufferHandle
68 nullptr, // .FreeBufferHandle
69 kTfLiteDelegateFlagsNone, // .flags
70 };
71
Sadik Armagan62483be2020-10-23 17:14:43 +010072 /// ArmNN Runtime pointer
Sadik Armagan3c24f432020-10-19 17:35:30 +010073 armnn::IRuntimePtr m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +010074 /// ArmNN Delegate Options
Sadik Armagan3c24f432020-10-19 17:35:30 +010075 armnnDelegate::DelegateOptions m_Options;
76};
77
78/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
79class ArmnnSubgraph
80{
81public:
82 static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
83 const TfLiteDelegateParams* parameters,
84 const Delegate* delegate);
85
86 TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
87
Sadik Armagan62483be2020-10-23 17:14:43 +010088 TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
Sadik Armagan3c24f432020-10-19 17:35:30 +010089
Sadik Armagan62483be2020-10-23 17:14:43 +010090 static TfLiteStatus VisitNode(DelegateData& delegateData,
Sadik Armagan3c24f432020-10-19 17:35:30 +010091 TfLiteContext* tfLiteContext,
92 TfLiteRegistration* tfLiteRegistration,
93 TfLiteNode* tfLiteNode,
94 int nodeIndex);
95
96private:
Sadik Armagan62483be2020-10-23 17:14:43 +010097 ArmnnSubgraph(armnn::NetworkId networkId,
98 armnn::IRuntime* runtime,
99 std::vector<armnn::BindingPointInfo>& inputBindings,
100 std::vector<armnn::BindingPointInfo>& outputBindings)
101 : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100102 {}
103
Sadik Armagan62483be2020-10-23 17:14:43 +0100104 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
105 TfLiteContext* tfLiteContext,
106 const TfLiteIntArray* inputs,
107 std::vector<armnn::BindingPointInfo>& inputBindings);
108
109 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
110 TfLiteContext* tfLiteContext,
111 const TfLiteIntArray* outputs,
112 std::vector<armnn::BindingPointInfo>& outputBindings);
113
114
Sadik Armagan3c24f432020-10-19 17:35:30 +0100115 /// The Network Id
116 armnn::NetworkId m_NetworkId;
117 /// ArmNN Rumtime
118 armnn::IRuntime* m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +0100119
120 // Binding information for inputs and outputs
121 std::vector<armnn::BindingPointInfo> m_InputBindings;
122 std::vector<armnn::BindingPointInfo> m_OutputBindings;
123
Sadik Armagan3c24f432020-10-19 17:35:30 +0100124};
125
Sadik Armagan3c24f432020-10-19 17:35:30 +0100126} // armnnDelegate namespace
127
Sadik Armagan3c24f432020-10-19 17:35:30 +0100128