ArmNN
 24.08
armnn_delegate.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020-2024 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
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 #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 > 4)
21 #define ARMNN_POST_TFLITE_2_4
22 #endif
23 
24 #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
25 #define ARMNN_POST_TFLITE_2_5
26 #endif
27 
28 namespace armnnDelegate
29 {
30 
32 {
33  DelegateData(const std::vector<armnn::BackendId>& backends)
34  : m_Backends(backends)
35  , m_Network(nullptr, nullptr)
36  {}
37 
38  const std::vector<armnn::BackendId> m_Backends;
40  std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
41 };
42 
43 // Forward decleration for functions initializing the ArmNN Delegate
44 DelegateOptions TfLiteArmnnDelegateOptionsDefault();
45 
46 TfLiteDelegate* TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options);
47 
48 void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
49 
50 TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
51 
52 /// ArmNN Delegate
53 class Delegate
54 {
55  friend class ArmnnSubgraph;
56 public:
57  explicit Delegate(armnnDelegate::DelegateOptions options);
58 
59  TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
60 
61  TfLiteDelegate* GetDelegate();
62 
63  /// Retrieve version in X.Y.Z form
64  static const std::string GetVersion();
65 
66 private:
67  /**
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 
77  TfLiteDelegate m_Delegate = {
78  reinterpret_cast<void*>(this), // .data_
79  DoPrepare, // .Prepare
80  nullptr, // .CopyFromBufferHandle
81  nullptr, // .CopyToBufferHandle
82  nullptr, // .FreeBufferHandle
83  kTfLiteDelegateFlagsNone, // .flags
84  nullptr, // .opaque_delegate_builder
85  };
86 
87  /// ArmNN Runtime pointer
88  armnn::IRuntime* m_Runtime;
89  /// ArmNN Delegate Options
90  armnnDelegate::DelegateOptions m_Options;
91 };
92 
93 /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
95 {
96 public:
97  static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
98  const TfLiteDelegateParams* parameters,
99  const Delegate* delegate);
100 
101  ~ArmnnSubgraph();
102 
103  TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
104 
105  TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
106 
107  static TfLiteStatus VisitNode(DelegateData& delegateData,
108  TfLiteContext* tfLiteContext,
109  TfLiteRegistration* tfLiteRegistration,
110  TfLiteNode* tfLiteNode,
111  int nodeIndex);
112 
113 private:
115  armnn::IRuntime* runtime,
116  std::vector<armnn::BindingPointInfo>& inputBindings,
117  std::vector<armnn::BindingPointInfo>& outputBindings)
118  : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
119  {}
120 
121  static TfLiteStatus AddInputLayer(DelegateData& delegateData,
122  TfLiteContext* tfLiteContext,
123  const TfLiteIntArray* inputs,
124  std::vector<armnn::BindingPointInfo>& inputBindings);
125 
126  static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
127  TfLiteContext* tfLiteContext,
128  const TfLiteIntArray* outputs,
129  std::vector<armnn::BindingPointInfo>& outputBindings);
130 
131 
132  /// The Network Id
133  armnn::NetworkId m_NetworkId;
134  /// ArmNN Runtime
135  armnn::IRuntime* m_Runtime;
136 
137  // Binding information for inputs and outputs
138  std::vector<armnn::BindingPointInfo> m_InputBindings;
139  std::vector<armnn::BindingPointInfo> m_OutputBindings;
140 
141 };
142 
143 } // armnnDelegate namespace
armnn::INetworkPtr
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:339
armnnDelegate::ArmnnSubgraph::Create
static ArmnnSubgraph * Create(TfLiteContext *tfLiteContext, const TfLiteDelegateParams *parameters, const Delegate *delegate)
armnnDelegate::TfLiteArmnnDelegateDelete
void TfLiteArmnnDelegateDelete(TfLiteDelegate *tfLiteDelegate)
armnnDelegate::ArmnnSubgraph
ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph.
Definition: armnn_delegate.hpp:94
armnnDelegate::ArmnnSubgraph::Invoke
TfLiteStatus Invoke(TfLiteContext *tfLiteContext, TfLiteNode *tfLiteNode)
armnnDelegate::ArmnnSubgraph::Prepare
TfLiteStatus Prepare(TfLiteContext *tfLiteContext)
armnnDelegate::ArmnnSubgraph::~ArmnnSubgraph
~ArmnnSubgraph()
armnnDelegate::DelegateData::DelegateData
DelegateData(const std::vector< armnn::BackendId > &backends)
Definition: armnn_delegate.hpp:33
armnn::IRuntime
Definition: IRuntime.hpp:75
armnnDelegate::DoPrepare
TfLiteStatus DoPrepare(TfLiteContext *context, TfLiteDelegate *delegate)
armnnDelegate::Delegate::Delegate
Delegate(armnnDelegate::DelegateOptions options)
armnnDelegate::DelegateData::m_OutputSlotForNode
std::vector< armnn::IOutputSlot * > m_OutputSlotForNode
Definition: armnn_delegate.hpp:40
armnn::NetworkId
int NetworkId
Definition: IRuntime.hpp:35
armnn::IRuntimePtr
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:41
armnnDelegate::TfLiteArmnnDelegateCreate
TfLiteDelegate * TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options)
armnnDelegate::TfLiteArmnnDelegateOptionsDefault
DelegateOptions TfLiteArmnnDelegateOptionsDefault()
armnnDelegate::Delegate::GetDelegate
TfLiteDelegate * GetDelegate()
armnnDelegate::DelegateData::m_Network
armnn::INetworkPtr m_Network
Definition: armnn_delegate.hpp:39
armnnDelegate::ArmnnSubgraph::VisitNode
static TfLiteStatus VisitNode(DelegateData &delegateData, TfLiteContext *tfLiteContext, TfLiteRegistration *tfLiteRegistration, TfLiteNode *tfLiteNode, int nodeIndex)
armnnDelegate::DelegateData
Definition: armnn_delegate.hpp:31
armnn::IRuntime::CreationOptions
Definition: IRuntime.hpp:78
armnn::IRuntime::Create
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:52
armnnDelegate::Delegate::IdentifyOperatorsToDelegate
TfLiteIntArray * IdentifyOperatorsToDelegate(TfLiteContext *context)
armnnDelegate::Delegate
ArmNN Delegate.
Definition: armnn_delegate.hpp:53
armnnDelegate
Definition: armnn_delegate.hpp:28
armnnDelegate::DelegateData::m_Backends
const std::vector< armnn::BackendId > m_Backends
Definition: armnn_delegate.hpp:38
armnnDelegate::Delegate::GetVersion
static const std::string GetVersion()
Retrieve version in X.Y.Z form.