blob: 159d5904237ca77b9c6b9616326edbf01cdc491d [file] [log] [blame]
Sadik Armagan3c24f432020-10-19 17:35:30 +01001//
Mike Kelly5446a4d2023-01-20 15:51:05 +00002// Copyright © 2020-2023 Arm Ltd and Contributors. All rights reserved.
Sadik Armagan3c24f432020-10-19 17:35:30 +01003// 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
Cathal Corbett80b4ef02022-05-25 11:21:11 +010020#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 4)
21#define ARMNN_POST_TFLITE_2_4
22#endif
23
Matthew Sloyan81ec9942021-10-12 10:26:30 +010024#if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
25#define ARMNN_POST_TFLITE_2_5
26#endif
Sadik Armagan3c24f432020-10-19 17:35:30 +010027
28namespace armnnDelegate
29{
30
Sadik Armagan62483be2020-10-23 17:14:43 +010031struct DelegateData
32{
33 DelegateData(const std::vector<armnn::BackendId>& backends)
34 : m_Backends(backends)
35 , m_Network(nullptr, nullptr)
36 {}
Sadik Armagan3c24f432020-10-19 17:35:30 +010037
Sadik Armagan62483be2020-10-23 17:14:43 +010038 const std::vector<armnn::BackendId> m_Backends;
39 armnn::INetworkPtr m_Network;
40 std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
41};
42
43// Forward decleration for functions initializing the ArmNN Delegate
44DelegateOptions TfLiteArmnnDelegateOptionsDefault();
45
46TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options);
47
48void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
49
50TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
51
52/// ArmNN Delegate
Sadik Armagan3c24f432020-10-19 17:35:30 +010053class Delegate
54{
55 friend class ArmnnSubgraph;
56public:
57 explicit Delegate(armnnDelegate::DelegateOptions options);
58
Sadik Armagan62483be2020-10-23 17:14:43 +010059 TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
Sadik Armagan3c24f432020-10-19 17:35:30 +010060
61 TfLiteDelegate* GetDelegate();
62
Matthew Sloyanac001ee2021-02-03 10:43:04 +000063 /// Retrieve version in X.Y.Z form
64 static const std::string GetVersion();
65
Sadik Armagan3c24f432020-10-19 17:35:30 +010066private:
Mike Kelly5446a4d2023-01-20 15:51:05 +000067 /**
68 * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
69 */
70 armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
71 {
72 static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
73 // Instantiated on first use.
74 return instance.get();
75 }
76
Sadik Armagan3c24f432020-10-19 17:35:30 +010077 TfLiteDelegate m_Delegate = {
78 reinterpret_cast<void*>(this), // .data_
Sadik Armagan62483be2020-10-23 17:14:43 +010079 DoPrepare, // .Prepare
Sadik Armagan3c24f432020-10-19 17:35:30 +010080 nullptr, // .CopyFromBufferHandle
81 nullptr, // .CopyToBufferHandle
82 nullptr, // .FreeBufferHandle
83 kTfLiteDelegateFlagsNone, // .flags
84 };
85
Sadik Armagan62483be2020-10-23 17:14:43 +010086 /// ArmNN Runtime pointer
Mike Kelly5446a4d2023-01-20 15:51:05 +000087 armnn::IRuntime* m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +010088 /// ArmNN Delegate Options
Sadik Armagan3c24f432020-10-19 17:35:30 +010089 armnnDelegate::DelegateOptions m_Options;
90};
91
92/// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
93class ArmnnSubgraph
94{
95public:
96 static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
97 const TfLiteDelegateParams* parameters,
98 const Delegate* delegate);
99
100 TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
101
Sadik Armagan62483be2020-10-23 17:14:43 +0100102 TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
Sadik Armagan3c24f432020-10-19 17:35:30 +0100103
Sadik Armagan62483be2020-10-23 17:14:43 +0100104 static TfLiteStatus VisitNode(DelegateData& delegateData,
Sadik Armagan3c24f432020-10-19 17:35:30 +0100105 TfLiteContext* tfLiteContext,
106 TfLiteRegistration* tfLiteRegistration,
107 TfLiteNode* tfLiteNode,
108 int nodeIndex);
109
110private:
Sadik Armagan62483be2020-10-23 17:14:43 +0100111 ArmnnSubgraph(armnn::NetworkId networkId,
112 armnn::IRuntime* runtime,
113 std::vector<armnn::BindingPointInfo>& inputBindings,
114 std::vector<armnn::BindingPointInfo>& outputBindings)
115 : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
Sadik Armagan3c24f432020-10-19 17:35:30 +0100116 {}
117
Sadik Armagan62483be2020-10-23 17:14:43 +0100118 static TfLiteStatus AddInputLayer(DelegateData& delegateData,
119 TfLiteContext* tfLiteContext,
120 const TfLiteIntArray* inputs,
121 std::vector<armnn::BindingPointInfo>& inputBindings);
122
123 static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
124 TfLiteContext* tfLiteContext,
125 const TfLiteIntArray* outputs,
126 std::vector<armnn::BindingPointInfo>& outputBindings);
127
128
Sadik Armagan3c24f432020-10-19 17:35:30 +0100129 /// The Network Id
130 armnn::NetworkId m_NetworkId;
131 /// ArmNN Rumtime
132 armnn::IRuntime* m_Runtime;
Sadik Armagan62483be2020-10-23 17:14:43 +0100133
134 // Binding information for inputs and outputs
135 std::vector<armnn::BindingPointInfo> m_InputBindings;
136 std::vector<armnn::BindingPointInfo> m_OutputBindings;
137
Sadik Armagan3c24f432020-10-19 17:35:30 +0100138};
139
Sadik Armagan3c24f432020-10-19 17:35:30 +0100140} // armnnDelegate namespace
141
Sadik Armagan3c24f432020-10-19 17:35:30 +0100142