ArmNN
 21.11
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 > 5)
21 #define ARMNN_POST_TFLITE_2_5
22 #endif
23 
24 namespace armnnDelegate
25 {
26 
28 {
29  DelegateData(const std::vector<armnn::BackendId>& backends)
30  : m_Backends(backends)
31  , m_Network(nullptr, nullptr)
32  {}
33 
34  const std::vector<armnn::BackendId> m_Backends;
36  std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
37 };
38 
39 // Forward decleration for functions initializing the ArmNN Delegate
41 
43 
44 void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
45 
46 TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
47 
48 /// ArmNN Delegate
49 class Delegate
50 {
51  friend class ArmnnSubgraph;
52 public:
53  explicit Delegate(armnnDelegate::DelegateOptions options);
54 
55  TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
56 
57  TfLiteDelegate* GetDelegate();
58 
59  /// Retrieve version in X.Y.Z form
60  static const std::string GetVersion();
61 
62 private:
63  TfLiteDelegate m_Delegate = {
64  reinterpret_cast<void*>(this), // .data_
65  DoPrepare, // .Prepare
66  nullptr, // .CopyFromBufferHandle
67  nullptr, // .CopyToBufferHandle
68  nullptr, // .FreeBufferHandle
69  kTfLiteDelegateFlagsNone, // .flags
70  };
71 
72  /// ArmNN Runtime pointer
73  armnn::IRuntimePtr m_Runtime;
74  /// ArmNN Delegate Options
76 };
77 
78 /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
80 {
81 public:
82  static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
83  const TfLiteDelegateParams* parameters,
84  const Delegate* delegate);
85 
86  TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
87 
88  TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
89 
90  static TfLiteStatus VisitNode(DelegateData& delegateData,
91  TfLiteContext* tfLiteContext,
92  TfLiteRegistration* tfLiteRegistration,
93  TfLiteNode* tfLiteNode,
94  int nodeIndex);
95 
96 private:
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)
102  {}
103 
104  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 
115  /// The Network Id
116  armnn::NetworkId m_NetworkId;
117  /// ArmNN Rumtime
118  armnn::IRuntime* m_Runtime;
119 
120  // Binding information for inputs and outputs
121  std::vector<armnn::BindingPointInfo> m_InputBindings;
122  std::vector<armnn::BindingPointInfo> m_OutputBindings;
123 
124 };
125 
126 } // armnnDelegate namespace
127 
128 
TfLiteStatus DoPrepare(TfLiteContext *context, TfLiteDelegate *delegate)
DelegateData(const std::vector< armnn::BackendId > &backends)
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:31
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:25
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:197