ArmNN
 21.02
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 
15 namespace armnnDelegate
16 {
17 
19 {
20  DelegateData(const std::vector<armnn::BackendId>& backends)
21  : m_Backends(backends)
22  , m_Network(nullptr, nullptr)
23  {}
24 
25  const std::vector<armnn::BackendId> m_Backends;
27  std::vector<armnn::IOutputSlot*> m_OutputSlotForNode;
28 };
29 
30 // Forward decleration for functions initializing the ArmNN Delegate
32 
34 
35 void TfLiteArmnnDelegateDelete(TfLiteDelegate* tfLiteDelegate);
36 
37 TfLiteStatus DoPrepare(TfLiteContext* context, TfLiteDelegate* delegate);
38 
39 /// ArmNN Delegate
40 class Delegate
41 {
42  friend class ArmnnSubgraph;
43 public:
44  explicit Delegate(armnnDelegate::DelegateOptions options);
45 
46  TfLiteIntArray* IdentifyOperatorsToDelegate(TfLiteContext* context);
47 
48  TfLiteDelegate* GetDelegate();
49 
50  /// Retrieve version in X.Y.Z form
51  static const std::string GetVersion();
52 
53 private:
54  TfLiteDelegate m_Delegate = {
55  reinterpret_cast<void*>(this), // .data_
56  DoPrepare, // .Prepare
57  nullptr, // .CopyFromBufferHandle
58  nullptr, // .CopyToBufferHandle
59  nullptr, // .FreeBufferHandle
60  kTfLiteDelegateFlagsNone, // .flags
61  };
62 
63  /// ArmNN Runtime pointer
64  armnn::IRuntimePtr m_Runtime;
65  /// ArmNN Delegate Options
67 };
68 
69 /// ArmnnSubgraph class where parsing the nodes to ArmNN format and creating the ArmNN Graph
71 {
72 public:
73  static ArmnnSubgraph* Create(TfLiteContext* tfLiteContext,
74  const TfLiteDelegateParams* parameters,
75  const Delegate* delegate);
76 
77  TfLiteStatus Prepare(TfLiteContext* tfLiteContext);
78 
79  TfLiteStatus Invoke(TfLiteContext* tfLiteContext, TfLiteNode* tfLiteNode);
80 
81  static TfLiteStatus VisitNode(DelegateData& delegateData,
82  TfLiteContext* tfLiteContext,
83  TfLiteRegistration* tfLiteRegistration,
84  TfLiteNode* tfLiteNode,
85  int nodeIndex);
86 
87 private:
89  armnn::IRuntime* runtime,
90  std::vector<armnn::BindingPointInfo>& inputBindings,
91  std::vector<armnn::BindingPointInfo>& outputBindings)
92  : m_NetworkId(networkId), m_Runtime(runtime), m_InputBindings(inputBindings), m_OutputBindings(outputBindings)
93  {}
94 
95  static TfLiteStatus AddInputLayer(DelegateData& delegateData,
96  TfLiteContext* tfLiteContext,
97  const TfLiteIntArray* inputs,
98  std::vector<armnn::BindingPointInfo>& inputBindings);
99 
100  static TfLiteStatus AddOutputLayer(DelegateData& delegateData,
101  TfLiteContext* tfLiteContext,
102  const TfLiteIntArray* outputs,
103  std::vector<armnn::BindingPointInfo>& outputBindings);
104 
105 
106  /// The Network Id
107  armnn::NetworkId m_NetworkId;
108  /// ArmNN Rumtime
109  armnn::IRuntime* m_Runtime;
110 
111  // Binding information for inputs and outputs
112  std::vector<armnn::BindingPointInfo> m_InputBindings;
113  std::vector<armnn::BindingPointInfo> m_OutputBindings;
114 
115 };
116 
117 } // armnnDelegate namespace
118 
119 
TfLiteStatus DoPrepare(TfLiteContext *context, TfLiteDelegate *delegate)
DelegateData(const std::vector< armnn::BackendId > &backends)
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:26
std::vector< armnn::IOutputSlot * > m_OutputSlotForNode
int NetworkId
Definition: IRuntime.hpp:20
const std::string GetVersion()
Definition: Utils.cpp:77
armnn::INetworkPtr m_Network
const std::vector< armnn::BackendId > m_Backends
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:173