ArmNN
 23.08
armnn_delegate.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <DelegateOptions.hpp>
9 #include <Version.hpp>
10 
11 #include <tensorflow/core/public/version.h>
12 #include <tensorflow/lite/c/c_api_opaque.h>
13 #include <tensorflow/lite/core/experimental/acceleration/configuration/c/stable_delegate.h>
14 #include <tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h>
15 
16 #if TF_MAJOR_VERSION > 2 || (TF_MAJOR_VERSION == 2 && TF_MINOR_VERSION > 5)
17 #define ARMNN_POST_TFLITE_2_5
18 #endif
19 
21 {
22 
24 {
25  DelegateData(const std::vector<armnn::BackendId>& backends)
26  : m_Backends(backends)
27  , m_Network(nullptr, nullptr)
28  {}
29 
30  const std::vector<armnn::BackendId> m_Backends;
32  std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
33 };
34 
35 /// Forward declaration for functions initializing the ArmNN Delegate
36 ::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault();
37 
38 TfLiteOpaqueDelegate* TfLiteArmnnOpaqueDelegateCreate(const void* settings);
39 
40 void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate* tfLiteDelegate);
41 
42 TfLiteStatus DoPrepare(TfLiteOpaqueContext* context, TfLiteOpaqueDelegate* delegate, void* data);
43 
44 /// ArmNN Opaque Delegate
46 {
47  friend class ArmnnSubgraph;
48 public:
49  explicit ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options);
50 
51  TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteOpaqueContext* context);
52 
53  TfLiteOpaqueDelegateBuilder* GetDelegateBuilder() { return &m_Builder; }
54 
55  /// Retrieve version in X.Y.Z form
56  static const std::string GetVersion();
57 
58 private:
59  /**
60  * Returns a pointer to the armnn::IRuntime* this will be shared by all armnn_delegates.
61  */
62  armnn::IRuntime* GetRuntime(const armnn::IRuntime::CreationOptions& options)
63  {
64  static armnn::IRuntimePtr instance = armnn::IRuntime::Create(options);
65  /// Instantiated on first use.
66  return instance.get();
67  }
68 
69  TfLiteOpaqueDelegateBuilder m_Builder =
70  {
71  reinterpret_cast<void*>(this), // .data_
72  DoPrepare, // .Prepare
73  nullptr, // .CopyFromBufferHandle
74  nullptr, // .CopyToBufferHandle
75  nullptr, // .FreeBufferHandle
76  kTfLiteDelegateFlagsNone, // .flags
77  };
78 
79  /// ArmNN Runtime pointer
80  armnn::IRuntime* m_Runtime;
81  /// ArmNN Delegate Options
82  armnnDelegate::DelegateOptions m_Options;
83 };
84 
85 static int TfLiteArmnnOpaqueDelegateErrno(TfLiteOpaqueDelegate* delegate) { return 0; }
86 
87 /// In order for the delegate to be loaded by TfLite
88 const TfLiteOpaqueDelegatePlugin* GetArmnnDelegatePluginApi();
89 
90 using tflite::delegates::DelegatePluginInterface;
91 using TfLiteOpaqueDelegatePtr = tflite::delegates::TfLiteDelegatePtr;
92 
93 class ArmnnDelegatePlugin : public DelegatePluginInterface
94 {
95 public:
96  static std::unique_ptr<ArmnnDelegatePlugin> New(const tflite::TFLiteSettings& tflite_settings)
97  {
98  return std::make_unique<ArmnnDelegatePlugin>(tflite_settings);
99  }
100 
101  tflite::delegates::TfLiteDelegatePtr Create() override
102  {
103  // Use default settings until options have been enabled.
104  return tflite::delegates::TfLiteDelegatePtr(
106  }
107 
108  int GetDelegateErrno(TfLiteOpaqueDelegate* from_delegate) override
109  {
110  return 0;
111  }
112 
113  explicit ArmnnDelegatePlugin(const tflite::TFLiteSettings& tfliteSettings)
114  {
115  // Use default settings until options have been enabled.
116  }
117 };
118 
119 /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
121 {
122 public:
123  static ArmnnSubgraph* Create(TfLiteOpaqueContext* tfLiteContext,
124  const TfLiteOpaqueDelegateParams* parameters,
125  const ArmnnOpaqueDelegate* delegate);
126 
127  TfLiteStatus Prepare(TfLiteOpaqueContext* tfLiteContext);
128 
129  TfLiteStatus Invoke(TfLiteOpaqueContext* tfLiteContext, TfLiteOpaqueNode* tfLiteNode);
130 
131  static TfLiteStatus VisitNode(DelegateData& delegateData,
132  TfLiteOpaqueContext* tfLiteContext,
133  TfLiteRegistrationExternal* tfLiteRegistration,
134  TfLiteOpaqueNode* tfLiteNode,
135  int nodeIndex);
136 private:
138  armnn::IRuntime* runtime,
139  std::vector<armnn::BindingPointInfo>& inputBindings,
140  std::vector<armnn::BindingPointInfo>& outputBindings)
141  : m_NetworkId(networkId)
142  , m_Runtime(runtime)
143  , m_InputBindings(inputBindings)
144  , m_OutputBindings(outputBindings)
145  {}
146  static TfLiteStatus AddInputLayer(DelegateData& delegateData,
147  TfLiteOpaqueContext* tfLiteContext,
148  const TfLiteIntArray* inputs,
149  std::vector<armnn::BindingPointInfo>& inputBindings);
150  static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
151  TfLiteOpaqueContext* tfLiteContext,
152  const TfLiteIntArray* outputs,
153  std::vector<armnn::BindingPointInfo>& outputBindings);
154  /// The Network Id
155  armnn::NetworkId m_NetworkId;
156  /// ArmNN Runtime
157  armnn::IRuntime* m_Runtime;
158  /// Binding information for inputs and outputs
159  std::vector<armnn::BindingPointInfo> m_InputBindings;
160  std::vector<armnn::BindingPointInfo> m_OutputBindings;
161 };
162 
163 } // armnnOpaqueDelegate namespace
armnn::INetworkPtr
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:339
armnnOpaqueDelegate::TfLiteOpaqueDelegatePtr
tflite::delegates::TfLiteDelegatePtr TfLiteOpaqueDelegatePtr
Definition: armnn_delegate.hpp:91
armnnOpaqueDelegate::ArmnnDelegatePlugin::New
static std::unique_ptr< ArmnnDelegatePlugin > New(const tflite::TFLiteSettings &tflite_settings)
Definition: armnn_delegate.hpp:96
Version.hpp
armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateDelete
void TfLiteArmnnOpaqueDelegateDelete(TfLiteOpaqueDelegate *tfLiteDelegate)
armnnOpaqueDelegate::ArmnnSubgraph::VisitNode
static TfLiteStatus VisitNode(DelegateData &delegateData, TfLiteOpaqueContext *tfLiteContext, TfLiteRegistrationExternal *tfLiteRegistration, TfLiteOpaqueNode *tfLiteNode, int nodeIndex)
armnnOpaqueDelegate::ArmnnOpaqueDelegate::GetDelegateBuilder
TfLiteOpaqueDelegateBuilder * GetDelegateBuilder()
Definition: armnn_delegate.hpp:53
armnnOpaqueDelegate::ArmnnDelegatePlugin
Definition: armnn_delegate.hpp:93
armnnOpaqueDelegate::ArmnnOpaqueDelegate::IdentifyOperatorsToDelegate
TfLiteIntArray * IdentifyOperatorsToDelegate(TfLiteOpaqueContext *context)
armnnOpaqueDelegate::ArmnnSubgraph::Prepare
TfLiteStatus Prepare(TfLiteOpaqueContext *tfLiteContext)
armnnOpaqueDelegate::DoPrepare
TfLiteStatus DoPrepare(TfLiteOpaqueContext *context, TfLiteOpaqueDelegate *delegate, void *data)
armnn::IRuntime
Definition: IRuntime.hpp:75
armnnOpaqueDelegate::ArmnnDelegatePlugin::ArmnnDelegatePlugin
ArmnnDelegatePlugin(const tflite::TFLiteSettings &tfliteSettings)
Definition: armnn_delegate.hpp:113
armnnOpaqueDelegate::DelegateData::DelegateData
DelegateData(const std::vector< armnn::BackendId > &backends)
Definition: armnn_delegate.hpp:25
armnn::NetworkId
int NetworkId
Definition: IRuntime.hpp:35
armnnOpaqueDelegate::GetArmnnDelegatePluginApi
const TfLiteOpaqueDelegatePlugin * GetArmnnDelegatePluginApi()
In order for the delegate to be loaded by TfLite.
armnnOpaqueDelegate
Definition: armnn_delegate.hpp:20
armnnOpaqueDelegate::ArmnnOpaqueDelegate
ArmNN Opaque Delegate.
Definition: armnn_delegate.hpp:45
armnn::IRuntimePtr
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:41
armnnOpaqueDelegate::ArmnnDelegatePlugin::GetDelegateErrno
int GetDelegateErrno(TfLiteOpaqueDelegate *from_delegate) override
Definition: armnn_delegate.hpp:108
armnnOpaqueDelegate::ArmnnSubgraph::Invoke
TfLiteStatus Invoke(TfLiteOpaqueContext *tfLiteContext, TfLiteOpaqueNode *tfLiteNode)
armnnOpaqueDelegate::ArmnnSubgraph::Create
static ArmnnSubgraph * Create(TfLiteOpaqueContext *tfLiteContext, const TfLiteOpaqueDelegateParams *parameters, const ArmnnOpaqueDelegate *delegate)
armnnOpaqueDelegate::DelegateData::m_Network
armnn::INetworkPtr m_Network
Definition: armnn_delegate.hpp:31
armnn::IRuntime::CreationOptions
Definition: IRuntime.hpp:78
armnnOpaqueDelegate::DelegateData
Definition: armnn_delegate.hpp:23
armnn::IRuntime::Create
static IRuntimePtr Create(const CreationOptions &options)
Definition: Runtime.cpp:52
armnnOpaqueDelegate::DelegateData::m_OutputSlotForNode
std::vector< armnn::IOutputSlot * > m_OutputSlotForNode
Definition: armnn_delegate.hpp:32
armnnOpaqueDelegate::TfLiteArmnnDelegateOptionsDefault
::armnnDelegate::DelegateOptions TfLiteArmnnDelegateOptionsDefault()
Forward declaration for functions initializing the ArmNN Delegate.
armnnOpaqueDelegate::ArmnnOpaqueDelegate::ArmnnOpaqueDelegate
ArmnnOpaqueDelegate(armnnDelegate::DelegateOptions options)
armnnOpaqueDelegate::ArmnnSubgraph
ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph.
Definition: armnn_delegate.hpp:120
armnnOpaqueDelegate::ArmnnOpaqueDelegate::GetVersion
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
armnnOpaqueDelegate::DelegateData::m_Backends
const std::vector< armnn::BackendId > m_Backends
Definition: armnn_delegate.hpp:30
armnnOpaqueDelegate::TfLiteArmnnOpaqueDelegateCreate
TfLiteOpaqueDelegate * TfLiteArmnnOpaqueDelegateCreate(const void *settings)
armnnOpaqueDelegate::ArmnnDelegatePlugin::Create
tflite::delegates::TfLiteDelegatePtr Create() override
Definition: armnn_delegate.hpp:101