ArmNN
 22.08
armnn_delegate.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2020 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
45 
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  TfLiteDelegate m_Delegate = {
68  reinterpret_cast<void*>(this), // .data_
69  DoPrepare, // .Prepare
70  nullptr, // .CopyFromBufferHandle
71  nullptr, // .CopyToBufferHandle
72  nullptr, // .FreeBufferHandle
73  kTfLiteDelegateFlagsNone, // .flags
74  };
75 
76  /// ArmNN Runtime pointer
77  armnn::IRuntimePtr m_Runtime;
78  /// ArmNN Delegate Options
80 };
81 
82 /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
84 {
85 public:
86  static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
87  const TfLiteDelegateParams* parameters,
88  const Delegate* delegate);
89 
90  TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
91 
92  TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
93 
94  static TfLiteStatus VisitNode(DelegateData& delegateData,
95  TfLiteContext* tfLiteContext,
96  TfLiteRegistration* tfLiteRegistration,
97  TfLiteNode* tfLiteNode,
98  int nodeIndex);
99 
100 private:
102  armnn::IRuntime* runtime,
103  std::vector<armnn::BindingPointInfo>& inputBindings,
104  std::vector<armnn::BindingPointInfo>& outputBindings)
105  : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
106  {}
107 
108  static TfLiteStatus AddInputLayer(DelegateData& delegateData,
109  TfLiteContext* tfLiteContext,
110  const TfLiteIntArray* inputs,
111  std::vector<armnn::BindingPointInfo>& inputBindings);
112 
113  static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
114  TfLiteContext* tfLiteContext,
115  const TfLiteIntArray* outputs,
116  std::vector<armnn::BindingPointInfo>& outputBindings);
117 
118 
119  /// The Network Id
120  armnn::NetworkId m_NetworkId;
121  /// ArmNN Rumtime
122  armnn::IRuntime* m_Runtime;
123 
124  // Binding information for inputs and outputs
125  std::vector<armnn::BindingPointInfo> m_InputBindings;
126  std::vector<armnn::BindingPointInfo> m_OutputBindings;
127 
128 };
129 
130 } // armnnDelegate namespace
131 
132 
TfLiteStatus DoPrepare(TfLiteContext *context, TfLiteDelegate *delegate)
DelegateData(const std::vector< armnn::BackendId > &backends)
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:33
std::vector< armnn::IOutputSlot * > m_OutputSlotForNode
const std::string GetVersion()
Definition: Utils.cpp:77
armnn::INetworkPtr m_Network
const std::vector< armnn::BackendId > m_Backends
int NetworkId
Definition: IRuntime.hpp:27
TfLiteDelegate * TfLiteArmnnDelegateCreate(armnnDelegate::DelegateOptions options)
void TfLiteArmnnDelegateDelete(TfLiteDelegate *tfLiteDelegate)
DelegateOptions TfLiteArmnnDelegateOptionsDefault()
ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph...
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:238