blob: 94499ea52dceeca8d208d6f8ba19725065d8f9d4 [file] [log] [blame]
surmeh01bceff2f2018-03-29 16:29:27 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
surmeh01bceff2f2018-03-29 16:29:27 +01004//
5#pragma once
6
7#include "armnnTfParser/ITfParser.hpp"
8
9#include "armnn/Types.hpp"
10#include "armnn/Tensor.hpp"
11#include "armnn/INetwork.hpp"
12
narpra016f37f832018-12-21 18:30:00 +000013#include <list>
surmeh01bceff2f2018-03-29 16:29:27 +010014#include <map>
15#include <memory>
16#include <unordered_map>
jimfly0184c70e62018-12-19 13:14:46 +000017#include <utility>
surmeh01bceff2f2018-03-29 16:29:27 +010018#include <vector>
19
20namespace armnn
21{
22class TensorInfo;
23}
24
25namespace tensorflow
26{
27class GraphDef;
28class NodeDef;
29}
30
31namespace armnnTfParser
32{
33
surmeh01bceff2f2018-03-29 16:29:27 +010034class ParsedTfOperation;
35using ParsedTfOperationPtr = std::unique_ptr<ParsedTfOperation>;
36
37///
38/// WithOutputTensorIndex wraps a value and an index. The purpose of
telsoa01c577f2c2018-08-31 09:22:23 +010039/// this template is to signify that, in Tensorflow, the input name of
40/// a layer has the convention of 'inputTensorName:#index', where the
41/// #index can be omitted and it implicitly means the 0 output of
surmeh01bceff2f2018-03-29 16:29:27 +010042/// the referenced layer. By supporting this notation we can handle
43/// layers with multiple outputs, such as Split.
44///
45template <typename T>
46struct WithOutputTensorIndex
47{
48 T m_IndexedValue;
49 unsigned int m_Index;
50
51 WithOutputTensorIndex(const T & value, unsigned int index)
52 : m_IndexedValue{value}
53 , m_Index{index} {}
54
55 WithOutputTensorIndex(T && value, unsigned int index)
56 : m_IndexedValue{value}
57 , m_Index{index} {}
58};
59
60using OutputOfParsedTfOperation = WithOutputTensorIndex<ParsedTfOperation *>;
61using OutputOfConstNodeDef = WithOutputTensorIndex<const tensorflow::NodeDef*>;
62using OutputId = WithOutputTensorIndex<std::string>;
63
64class TfParser : public ITfParser
65{
66public:
telsoa01c577f2c2018-08-31 09:22:23 +010067 /// Creates the network from a protobuf text file on the disk.
surmeh01bceff2f2018-03-29 16:29:27 +010068 virtual armnn::INetworkPtr CreateNetworkFromTextFile(
69 const char* graphFile,
70 const std::map<std::string, armnn::TensorShape>& inputShapes,
71 const std::vector<std::string>& requestedOutputs) override;
72
telsoa01c577f2c2018-08-31 09:22:23 +010073 /// Creates the network from a protobuf binary file on the disk.
surmeh01bceff2f2018-03-29 16:29:27 +010074 virtual armnn::INetworkPtr CreateNetworkFromBinaryFile(
75 const char* graphFile,
76 const std::map<std::string, armnn::TensorShape>& inputShapes,
77 const std::vector<std::string>& requestedOutputs) override;
78
telsoa01c577f2c2018-08-31 09:22:23 +010079 /// Creates the network directly from protobuf text in a string. Useful for debugging/testing.
surmeh01bceff2f2018-03-29 16:29:27 +010080 virtual armnn::INetworkPtr CreateNetworkFromString(
81 const char* protoText,
82 const std::map<std::string, armnn::TensorShape>& inputShapes,
83 const std::vector<std::string>& requestedOutputs) override;
84
telsoa01c577f2c2018-08-31 09:22:23 +010085 /// Retrieves binding info (layer id and tensor info) for the network input identified by the given layer name.
surmeh01bceff2f2018-03-29 16:29:27 +010086 virtual BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const override;
87
telsoa01c577f2c2018-08-31 09:22:23 +010088 /// Retrieves binding info (layer id and tensor info) for the network output identified by the given layer name.
surmeh01bceff2f2018-03-29 16:29:27 +010089 virtual BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const override;
90
91public:
92 TfParser();
93
94private:
95 template <typename T>
96 friend class ParsedConstTfOperation;
97 friend class ParsedMatMulTfOperation;
telsoa01c577f2c2018-08-31 09:22:23 +010098 friend class ParsedMulTfOperation;
surmeh01bceff2f2018-03-29 16:29:27 +010099
telsoa01c577f2c2018-08-31 09:22:23 +0100100 /// Parses a GraphDef loaded into memory from one of the other CreateNetwork*.
surmeh01bceff2f2018-03-29 16:29:27 +0100101 armnn::INetworkPtr CreateNetworkFromGraphDef(const tensorflow::GraphDef& graphDef,
102 const std::map<std::string, armnn::TensorShape>& inputShapes,
103 const std::vector<std::string>& requestedOutputs);
104
telsoa01c577f2c2018-08-31 09:22:23 +0100105 /// Sets up variables and then performs BFS to parse all nodes.
surmeh01bceff2f2018-03-29 16:29:27 +0100106 void LoadGraphDef(const tensorflow::GraphDef& graphDef);
107
telsoa01c577f2c2018-08-31 09:22:23 +0100108 /// Parses a given node, assuming nodes before it in the graph have been done.
surmeh01bceff2f2018-03-29 16:29:27 +0100109 void LoadNodeDef(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
110
telsoa01c577f2c2018-08-31 09:22:23 +0100111 /// Handling identity layers as the input for Conv2D layer.
surmeh01bceff2f2018-03-29 16:29:27 +0100112 const tensorflow::NodeDef* ResolveIdentityNode(const tensorflow::NodeDef* nodeDef);
113 /// Finds the nodes connected as inputs of the given node in the graph.
114 std::vector<OutputOfConstNodeDef> GetTfInputNodes(const tensorflow::NodeDef& nodeDef) const;
115 /// Finds the IParsedTfOperations for the nodes connected as inputs of the given node in the graph,
116 /// and throws an exception if the number of inputs does not match the expected one.
117 /// This will automatically resolve any identity nodes. The result vector contains the parsed operation
118 /// together with the output tensor index to make the connection unambiguous.
119 std::vector<OutputOfParsedTfOperation> GetInputParsedTfOperationsChecked(const tensorflow::NodeDef& nodeDef,
120 std::size_t expectedNumInputs);
121
122 ParsedTfOperationPtr ParseConst(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
123
telsoa01c577f2c2018-08-31 09:22:23 +0100124 /// Checks if there is a pre-parsed const tensor available with the given name and Type.
surmeh01bceff2f2018-03-29 16:29:27 +0100125 template<typename Type>
126 bool HasParsedConstTensor(const std::string & nodeName) const;
jimfly01f6ba7472018-12-04 10:09:52 +0000127 template<typename Type>
128 bool HasParsedConstTensor(ParsedTfOperation* parsedTfOpPtr) const;
surmeh01bceff2f2018-03-29 16:29:27 +0100129
Saoirse Stewart91c0eff2019-02-27 11:07:57 +0000130 unsigned int GetConstInputIndex(const std::vector<OutputOfParsedTfOperation>& inputs);
131
surmeh01bceff2f2018-03-29 16:29:27 +0100132 ParsedTfOperationPtr ParseAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Ferran Balaguerfbdad032018-12-28 18:15:24 +0000133 ParsedTfOperationPtr ParseAddN(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100134 ParsedTfOperationPtr ParseBiasAdd(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
135 ParsedTfOperationPtr ParseConv2D(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Jim Flynn6cde7ed2019-02-20 14:25:11 +0000136 ParsedTfOperationPtr ParseDepthwiseConv2D(const tensorflow::NodeDef& nodeDef,
137 const tensorflow::GraphDef& graphDef);
138 ParsedTfOperationPtr ParseExpandDims(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100139 ParsedTfOperationPtr ParseFusedBatchNorm(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
140 ParsedTfOperationPtr ParseConcat(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
141 ParsedTfOperationPtr ParseIdentity(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
142 ParsedTfOperationPtr ParseLrn(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
143 ParsedTfOperationPtr ParseMatMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000144 ParsedTfOperationPtr ParseMean(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100145 ParsedTfOperationPtr ParseMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
146 ParsedTfOperationPtr ParsePlaceholder(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
saoste01bbd40612018-08-28 15:41:51 +0100147 ParsedTfOperationPtr ParseRealDiv(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100148 ParsedTfOperationPtr ParseRelu(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
149 ParsedTfOperationPtr ParseRelu6(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
150 ParsedTfOperationPtr ParseReshape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
151 ParsedTfOperationPtr ParseResizeBilinear(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Mohamed Nour Abouelseoud7a8892f2019-01-09 14:19:58 +0000152 ParsedTfOperationPtr ParseRsqrt(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100153 ParsedTfOperationPtr ParseShape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
154 ParsedTfOperationPtr ParseSqueeze(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
155 ParsedTfOperationPtr ParseSigmoid(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
156 ParsedTfOperationPtr ParseSoftmax(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
157 ParsedTfOperationPtr ParseSoftplus(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Sadik Armagan2ad6cb42018-12-27 11:23:44 +0000158 ParsedTfOperationPtr ParseSplit(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Georgios Pinitas5e90aab2020-02-14 14:46:51 +0000159 ParsedTfOperationPtr ParseStridedSlice(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100160 ParsedTfOperationPtr ParseTanh(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
161 ParsedTfOperationPtr ParseMaxPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
162 ParsedTfOperationPtr ParseAvgPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Jim Flynn6cde7ed2019-02-20 14:25:11 +0000163 ParsedTfOperationPtr ParsePooling2d(const tensorflow::NodeDef& nodeDef,
164 const tensorflow::GraphDef& graphDef,
165 armnn::PoolingAlgorithm pooltype);
jimfly0184c70e62018-12-19 13:14:46 +0000166 ParsedTfOperationPtr ParseEqual(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
telsoa01c577f2c2018-08-31 09:22:23 +0100167 ParsedTfOperationPtr ParseMaximum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Nattapat Chaimanowong24df8222018-12-04 13:47:02 +0000168 ParsedTfOperationPtr ParseMinimum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
FrancisMurtagh94412af2019-01-24 10:53:39 +0000169 ParsedTfOperationPtr ParseGather(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly01a06bf312018-12-18 16:24:51 +0000170 ParsedTfOperationPtr ParseGreater(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly01f6ba7472018-12-04 10:09:52 +0000171 ParsedTfOperationPtr ParsePad(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly0123be07e2018-12-04 17:47:22 +0000172 ParsedTfOperationPtr ParseSub(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Sadik Armagan48d70932020-02-18 15:18:27 +0000173 ParsedTfOperationPtr ParseStack(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Sang-Hoon Parkdd3f71b2020-02-18 11:27:35 +0000174 ParsedTfOperationPtr ParseTranspose(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100175 ParsedTfOperationPtr AddActivationLayer(const tensorflow::NodeDef& nodeDef, armnn::ActivationDescriptor& desc);
176 ParsedTfOperationPtr AddAdditionLayer(const tensorflow::NodeDef& nodeDef, bool isBiasAdd = false);
saoste01bbd40612018-08-28 15:41:51 +0100177 ParsedTfOperationPtr AddRealDivLayer(const tensorflow::NodeDef& nodeDef);
Sadik Armagan975c09a2018-12-04 10:02:08 +0000178 ParsedTfOperationPtr AddMaximumLayer(const tensorflow::NodeDef& nodeDef);
telsoa01c577f2c2018-08-31 09:22:23 +0100179
180private:
181 armnn::IConnectableLayer* AddMultiplicationLayer(const tensorflow::NodeDef& nodeDef);
182
surmeh01bceff2f2018-03-29 16:29:27 +0100183 armnn::IConnectableLayer* AddFullyConnectedLayer(const tensorflow::NodeDef& matMulNodeDef,
184 const tensorflow::NodeDef* addNodeDef, const char* armnnLayerName);
185
telsoa01c577f2c2018-08-31 09:22:23 +0100186 bool IsSupportedLeakyReluPattern(const tensorflow::NodeDef& mulNodeDef,
187 size_t alphaLayerIndex,
188 const OutputOfParsedTfOperation& otherOp,
189 armnn::IOutputSlot** outputOfLeakyRelu,
190 armnn::ActivationDescriptor & desc);
191
jimfly0184c70e62018-12-19 13:14:46 +0000192 std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> ProcessElementwiseInputSlots(
193 const tensorflow::NodeDef& nodeDef, const std::string& layerName);
194
kevmay012b4d88e2019-01-24 14:05:09 +0000195 ParsedTfOperationPtr ProcessComparisonLayer(
196 armnn::IOutputSlot* input0Slot,
197 armnn::IOutputSlot* input1Slot,
198 armnn::IConnectableLayer* const layer,
199 const tensorflow::NodeDef& nodeDef);
200
jimfly0184c70e62018-12-19 13:14:46 +0000201 ParsedTfOperationPtr ProcessElementwiseLayer(
202 armnn::IOutputSlot* input0Slot,
203 armnn::IOutputSlot* input1Slot,
204 armnn::IConnectableLayer* const layer,
205 const tensorflow::NodeDef& nodeDef);
206
Ferran Balaguerfbdad032018-12-28 18:15:24 +0000207 armnn::IConnectableLayer* CreateAdditionLayer(
208 const tensorflow::NodeDef& nodeDef,
209 armnn::IOutputSlot* input0Slot,
210 armnn::IOutputSlot* input1Slot,
211 const std::string& layerName);
212
213 armnn::IConnectableLayer* CreateAdditionLayer(
214 const tensorflow::NodeDef& nodeDef,
215 const OutputOfParsedTfOperation& opOne,
216 const OutputOfParsedTfOperation& opTwo,
217 unsigned int numberOfAddition);
218
219 armnn::IConnectableLayer* CreateAdditionLayer(
220 const tensorflow::NodeDef& nodeDef,
221 armnn::IConnectableLayer* layerOne,
222 armnn::IConnectableLayer* layerTwo,
223 unsigned int numberOfAddition,
224 unsigned long numberOfLayersToConnect,
225 bool isOdd);
226
227 armnn::IConnectableLayer* CreateAdditionLayer(
228 const tensorflow::NodeDef& nodeDef,
229 const OutputOfParsedTfOperation& op,
230 armnn::IConnectableLayer* layer);
231
surmeh01bceff2f2018-03-29 16:29:27 +0100232 static std::pair<armnn::LayerBindingId, armnn::TensorInfo> GetBindingInfo(const std::string& layerName,
233 const char* bindingPointDesc,
234 const std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo);
235
236 void TrackInputBinding(armnn::IConnectableLayer* layer,
237 armnn::LayerBindingId id,
238 const armnn::TensorInfo& tensorInfo);
239
240 void TrackOutputBinding(armnn::IConnectableLayer* layer,
241 armnn::LayerBindingId id,
242 const armnn::TensorInfo& tensorInfo);
243
244 static void TrackBindingPoint(armnn::IConnectableLayer* layer, armnn::LayerBindingId id,
245 const armnn::TensorInfo& tensorInfo,
246 const char* bindingPointDesc,
247 std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo);
248
249 void Cleanup();
250
telsoa01c577f2c2018-08-31 09:22:23 +0100251 /// The network we're building. Gets cleared after it is passed to the user.
surmeh01bceff2f2018-03-29 16:29:27 +0100252 armnn::INetworkPtr m_Network;
253
254 using OperationParsingFunction = ParsedTfOperationPtr(TfParser::*)(const tensorflow::NodeDef& nodeDef,
255 const tensorflow::GraphDef& graphDef);
256
telsoa01c577f2c2018-08-31 09:22:23 +0100257 /// Map of TensorFlow operation names to parsing member functions.
surmeh01bceff2f2018-03-29 16:29:27 +0100258 static const std::map<std::string, OperationParsingFunction> ms_OperationNameToParsingFunctions;
259
narpra016f37f832018-12-21 18:30:00 +0000260 static const std::list<std::string> m_ControlInputs;
261
surmeh01bceff2f2018-03-29 16:29:27 +0100262 std::map<std::string, armnn::TensorShape> m_InputShapes;
263 std::vector<std::string> m_RequestedOutputs;
264
telsoa01c577f2c2018-08-31 09:22:23 +0100265 /// Map of nodes extracted from the GraphDef to speed up parsing.
surmeh01bceff2f2018-03-29 16:29:27 +0100266 std::unordered_map<std::string, const tensorflow::NodeDef*> m_NodesByName;
267
268 std::unordered_map<std::string, ParsedTfOperationPtr> m_ParsedTfOperations;
269
telsoa01c577f2c2018-08-31 09:22:23 +0100270 /// Maps input layer names to their corresponding ids and tensor info.
surmeh01bceff2f2018-03-29 16:29:27 +0100271 std::unordered_map<std::string, BindingPointInfo> m_NetworkInputsBindingInfo;
272
telsoa01c577f2c2018-08-31 09:22:23 +0100273 /// Maps output layer names to their corresponding ids and tensor info.
surmeh01bceff2f2018-03-29 16:29:27 +0100274 std::unordered_map<std::string, BindingPointInfo> m_NetworkOutputsBindingInfo;
275};
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000276
surmeh01bceff2f2018-03-29 16:29:27 +0100277}