blob: 2b809419de8d9afb1b925edff1dc14c4d66adba7 [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
34using BindingPointInfo = std::pair<armnn::LayerBindingId, armnn::TensorInfo>;
35
36class ParsedTfOperation;
37using ParsedTfOperationPtr = std::unique_ptr<ParsedTfOperation>;
38
39///
40/// WithOutputTensorIndex wraps a value and an index. The purpose of
telsoa01c577f2c2018-08-31 09:22:23 +010041/// this template is to signify that, in Tensorflow, the input name of
42/// a layer has the convention of 'inputTensorName:#index', where the
43/// #index can be omitted and it implicitly means the 0 output of
surmeh01bceff2f2018-03-29 16:29:27 +010044/// the referenced layer. By supporting this notation we can handle
45/// layers with multiple outputs, such as Split.
46///
47template <typename T>
48struct WithOutputTensorIndex
49{
50 T m_IndexedValue;
51 unsigned int m_Index;
52
53 WithOutputTensorIndex(const T & value, unsigned int index)
54 : m_IndexedValue{value}
55 , m_Index{index} {}
56
57 WithOutputTensorIndex(T && value, unsigned int index)
58 : m_IndexedValue{value}
59 , m_Index{index} {}
60};
61
62using OutputOfParsedTfOperation = WithOutputTensorIndex<ParsedTfOperation *>;
63using OutputOfConstNodeDef = WithOutputTensorIndex<const tensorflow::NodeDef*>;
64using OutputId = WithOutputTensorIndex<std::string>;
65
66class TfParser : public ITfParser
67{
68public:
telsoa01c577f2c2018-08-31 09:22:23 +010069 /// Creates the network from a protobuf text file on the disk.
surmeh01bceff2f2018-03-29 16:29:27 +010070 virtual armnn::INetworkPtr CreateNetworkFromTextFile(
71 const char* graphFile,
72 const std::map<std::string, armnn::TensorShape>& inputShapes,
73 const std::vector<std::string>& requestedOutputs) override;
74
telsoa01c577f2c2018-08-31 09:22:23 +010075 /// Creates the network from a protobuf binary file on the disk.
surmeh01bceff2f2018-03-29 16:29:27 +010076 virtual armnn::INetworkPtr CreateNetworkFromBinaryFile(
77 const char* graphFile,
78 const std::map<std::string, armnn::TensorShape>& inputShapes,
79 const std::vector<std::string>& requestedOutputs) override;
80
telsoa01c577f2c2018-08-31 09:22:23 +010081 /// Creates the network directly from protobuf text in a string. Useful for debugging/testing.
surmeh01bceff2f2018-03-29 16:29:27 +010082 virtual armnn::INetworkPtr CreateNetworkFromString(
83 const char* protoText,
84 const std::map<std::string, armnn::TensorShape>& inputShapes,
85 const std::vector<std::string>& requestedOutputs) override;
86
telsoa01c577f2c2018-08-31 09:22:23 +010087 /// Retrieves binding info (layer id and tensor info) for the network input identified by the given layer name.
surmeh01bceff2f2018-03-29 16:29:27 +010088 virtual BindingPointInfo GetNetworkInputBindingInfo(const std::string& name) const override;
89
telsoa01c577f2c2018-08-31 09:22:23 +010090 /// Retrieves binding info (layer id and tensor info) for the network output identified by the given layer name.
surmeh01bceff2f2018-03-29 16:29:27 +010091 virtual BindingPointInfo GetNetworkOutputBindingInfo(const std::string& name) const override;
92
93public:
94 TfParser();
95
96private:
97 template <typename T>
98 friend class ParsedConstTfOperation;
99 friend class ParsedMatMulTfOperation;
telsoa01c577f2c2018-08-31 09:22:23 +0100100 friend class ParsedMulTfOperation;
surmeh01bceff2f2018-03-29 16:29:27 +0100101
telsoa01c577f2c2018-08-31 09:22:23 +0100102 /// Parses a GraphDef loaded into memory from one of the other CreateNetwork*.
surmeh01bceff2f2018-03-29 16:29:27 +0100103 armnn::INetworkPtr CreateNetworkFromGraphDef(const tensorflow::GraphDef& graphDef,
104 const std::map<std::string, armnn::TensorShape>& inputShapes,
105 const std::vector<std::string>& requestedOutputs);
106
telsoa01c577f2c2018-08-31 09:22:23 +0100107 /// Sets up variables and then performs BFS to parse all nodes.
surmeh01bceff2f2018-03-29 16:29:27 +0100108 void LoadGraphDef(const tensorflow::GraphDef& graphDef);
109
telsoa01c577f2c2018-08-31 09:22:23 +0100110 /// Parses a given node, assuming nodes before it in the graph have been done.
surmeh01bceff2f2018-03-29 16:29:27 +0100111 void LoadNodeDef(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
112
telsoa01c577f2c2018-08-31 09:22:23 +0100113 /// Handling identity layers as the input for Conv2D layer.
surmeh01bceff2f2018-03-29 16:29:27 +0100114 const tensorflow::NodeDef* ResolveIdentityNode(const tensorflow::NodeDef* nodeDef);
115 /// Finds the nodes connected as inputs of the given node in the graph.
116 std::vector<OutputOfConstNodeDef> GetTfInputNodes(const tensorflow::NodeDef& nodeDef) const;
117 /// Finds the IParsedTfOperations for the nodes connected as inputs of the given node in the graph,
118 /// and throws an exception if the number of inputs does not match the expected one.
119 /// This will automatically resolve any identity nodes. The result vector contains the parsed operation
120 /// together with the output tensor index to make the connection unambiguous.
121 std::vector<OutputOfParsedTfOperation> GetInputParsedTfOperationsChecked(const tensorflow::NodeDef& nodeDef,
122 std::size_t expectedNumInputs);
123
124 ParsedTfOperationPtr ParseConst(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
125
telsoa01c577f2c2018-08-31 09:22:23 +0100126 /// Checks if there is a pre-parsed const tensor available with the given name and Type.
surmeh01bceff2f2018-03-29 16:29:27 +0100127 template<typename Type>
128 bool HasParsedConstTensor(const std::string & nodeName) const;
jimfly01f6ba7472018-12-04 10:09:52 +0000129 template<typename Type>
130 bool HasParsedConstTensor(ParsedTfOperation* parsedTfOpPtr) const;
surmeh01bceff2f2018-03-29 16:29:27 +0100131
132 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);
136 ParsedTfOperationPtr ParseDepthwiseConv2D(const tensorflow::NodeDef& nodeDef,const tensorflow::GraphDef& graphDef);
Conor Kennedyc2130a02018-12-05 11:05:54 +0000137 ParsedTfOperationPtr ParseExpandDims(const tensorflow::NodeDef& nodeDef,const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100138 ParsedTfOperationPtr ParseFusedBatchNorm(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
139 ParsedTfOperationPtr ParseConcat(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
140 ParsedTfOperationPtr ParseIdentity(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
141 ParsedTfOperationPtr ParseLrn(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
142 ParsedTfOperationPtr ParseMatMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000143 ParsedTfOperationPtr ParseMean(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100144 ParsedTfOperationPtr ParseMul(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
145 ParsedTfOperationPtr ParsePlaceholder(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
saoste01bbd40612018-08-28 15:41:51 +0100146 ParsedTfOperationPtr ParseRealDiv(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100147 ParsedTfOperationPtr ParseRelu(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
148 ParsedTfOperationPtr ParseRelu6(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
149 ParsedTfOperationPtr ParseReshape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
150 ParsedTfOperationPtr ParseResizeBilinear(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Mohamed Nour Abouelseoud7a8892f2019-01-09 14:19:58 +0000151 ParsedTfOperationPtr ParseRsqrt(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100152 ParsedTfOperationPtr ParseShape(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
153 ParsedTfOperationPtr ParseSqueeze(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
154 ParsedTfOperationPtr ParseSigmoid(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
155 ParsedTfOperationPtr ParseSoftmax(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
156 ParsedTfOperationPtr ParseSoftplus(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Sadik Armagan2ad6cb42018-12-27 11:23:44 +0000157 ParsedTfOperationPtr ParseSplit(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100158 ParsedTfOperationPtr ParseTanh(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
159 ParsedTfOperationPtr ParseMaxPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
160 ParsedTfOperationPtr ParseAvgPool(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
161 ParsedTfOperationPtr ParsePooling2d(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef,
162 armnn::PoolingAlgorithm pooltype);
jimfly0184c70e62018-12-19 13:14:46 +0000163 ParsedTfOperationPtr ParseEqual(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
telsoa01c577f2c2018-08-31 09:22:23 +0100164 ParsedTfOperationPtr ParseMaximum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
Nattapat Chaimanowong24df8222018-12-04 13:47:02 +0000165 ParsedTfOperationPtr ParseMinimum(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
FrancisMurtagh94412af2019-01-24 10:53:39 +0000166 ParsedTfOperationPtr ParseGather(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly01a06bf312018-12-18 16:24:51 +0000167 ParsedTfOperationPtr ParseGreater(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly01f6ba7472018-12-04 10:09:52 +0000168 ParsedTfOperationPtr ParsePad(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
jimfly0123be07e2018-12-04 17:47:22 +0000169 ParsedTfOperationPtr ParseSub(const tensorflow::NodeDef& nodeDef, const tensorflow::GraphDef& graphDef);
surmeh01bceff2f2018-03-29 16:29:27 +0100170 ParsedTfOperationPtr AddActivationLayer(const tensorflow::NodeDef& nodeDef, armnn::ActivationDescriptor& desc);
171 ParsedTfOperationPtr AddAdditionLayer(const tensorflow::NodeDef& nodeDef, bool isBiasAdd = false);
saoste01bbd40612018-08-28 15:41:51 +0100172 ParsedTfOperationPtr AddRealDivLayer(const tensorflow::NodeDef& nodeDef);
Sadik Armagan975c09a2018-12-04 10:02:08 +0000173 ParsedTfOperationPtr AddMaximumLayer(const tensorflow::NodeDef& nodeDef);
telsoa01c577f2c2018-08-31 09:22:23 +0100174
175private:
176 armnn::IConnectableLayer* AddMultiplicationLayer(const tensorflow::NodeDef& nodeDef);
177
surmeh01bceff2f2018-03-29 16:29:27 +0100178 armnn::IConnectableLayer* AddFullyConnectedLayer(const tensorflow::NodeDef& matMulNodeDef,
179 const tensorflow::NodeDef* addNodeDef, const char* armnnLayerName);
180
telsoa01c577f2c2018-08-31 09:22:23 +0100181 bool IsSupportedLeakyReluPattern(const tensorflow::NodeDef& mulNodeDef,
182 size_t alphaLayerIndex,
183 const OutputOfParsedTfOperation& otherOp,
184 armnn::IOutputSlot** outputOfLeakyRelu,
185 armnn::ActivationDescriptor & desc);
186
jimfly0184c70e62018-12-19 13:14:46 +0000187 std::pair<armnn::IOutputSlot*, armnn::IOutputSlot*> ProcessElementwiseInputSlots(
188 const tensorflow::NodeDef& nodeDef, const std::string& layerName);
189
kevmay012b4d88e2019-01-24 14:05:09 +0000190 ParsedTfOperationPtr ProcessComparisonLayer(
191 armnn::IOutputSlot* input0Slot,
192 armnn::IOutputSlot* input1Slot,
193 armnn::IConnectableLayer* const layer,
194 const tensorflow::NodeDef& nodeDef);
195
jimfly0184c70e62018-12-19 13:14:46 +0000196 ParsedTfOperationPtr ProcessElementwiseLayer(
197 armnn::IOutputSlot* input0Slot,
198 armnn::IOutputSlot* input1Slot,
199 armnn::IConnectableLayer* const layer,
200 const tensorflow::NodeDef& nodeDef);
201
Ferran Balaguerfbdad032018-12-28 18:15:24 +0000202 armnn::IConnectableLayer* CreateAdditionLayer(
203 const tensorflow::NodeDef& nodeDef,
204 armnn::IOutputSlot* input0Slot,
205 armnn::IOutputSlot* input1Slot,
206 const std::string& layerName);
207
208 armnn::IConnectableLayer* CreateAdditionLayer(
209 const tensorflow::NodeDef& nodeDef,
210 const OutputOfParsedTfOperation& opOne,
211 const OutputOfParsedTfOperation& opTwo,
212 unsigned int numberOfAddition);
213
214 armnn::IConnectableLayer* CreateAdditionLayer(
215 const tensorflow::NodeDef& nodeDef,
216 armnn::IConnectableLayer* layerOne,
217 armnn::IConnectableLayer* layerTwo,
218 unsigned int numberOfAddition,
219 unsigned long numberOfLayersToConnect,
220 bool isOdd);
221
222 armnn::IConnectableLayer* CreateAdditionLayer(
223 const tensorflow::NodeDef& nodeDef,
224 const OutputOfParsedTfOperation& op,
225 armnn::IConnectableLayer* layer);
226
surmeh01bceff2f2018-03-29 16:29:27 +0100227 static std::pair<armnn::LayerBindingId, armnn::TensorInfo> GetBindingInfo(const std::string& layerName,
228 const char* bindingPointDesc,
229 const std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo);
230
231 void TrackInputBinding(armnn::IConnectableLayer* layer,
232 armnn::LayerBindingId id,
233 const armnn::TensorInfo& tensorInfo);
234
235 void TrackOutputBinding(armnn::IConnectableLayer* layer,
236 armnn::LayerBindingId id,
237 const armnn::TensorInfo& tensorInfo);
238
239 static void TrackBindingPoint(armnn::IConnectableLayer* layer, armnn::LayerBindingId id,
240 const armnn::TensorInfo& tensorInfo,
241 const char* bindingPointDesc,
242 std::unordered_map<std::string, BindingPointInfo>& nameToBindingInfo);
243
244 void Cleanup();
245
telsoa01c577f2c2018-08-31 09:22:23 +0100246 /// The network we're building. Gets cleared after it is passed to the user.
surmeh01bceff2f2018-03-29 16:29:27 +0100247 armnn::INetworkPtr m_Network;
248
249 using OperationParsingFunction = ParsedTfOperationPtr(TfParser::*)(const tensorflow::NodeDef& nodeDef,
250 const tensorflow::GraphDef& graphDef);
251
telsoa01c577f2c2018-08-31 09:22:23 +0100252 /// Map of TensorFlow operation names to parsing member functions.
surmeh01bceff2f2018-03-29 16:29:27 +0100253 static const std::map<std::string, OperationParsingFunction> ms_OperationNameToParsingFunctions;
254
narpra016f37f832018-12-21 18:30:00 +0000255 static const std::list<std::string> m_ControlInputs;
256
surmeh01bceff2f2018-03-29 16:29:27 +0100257 std::map<std::string, armnn::TensorShape> m_InputShapes;
258 std::vector<std::string> m_RequestedOutputs;
259
telsoa01c577f2c2018-08-31 09:22:23 +0100260 /// Map of nodes extracted from the GraphDef to speed up parsing.
surmeh01bceff2f2018-03-29 16:29:27 +0100261 std::unordered_map<std::string, const tensorflow::NodeDef*> m_NodesByName;
262
263 std::unordered_map<std::string, ParsedTfOperationPtr> m_ParsedTfOperations;
264
telsoa01c577f2c2018-08-31 09:22:23 +0100265 /// Maps input layer names to their corresponding ids and tensor info.
surmeh01bceff2f2018-03-29 16:29:27 +0100266 std::unordered_map<std::string, BindingPointInfo> m_NetworkInputsBindingInfo;
267
telsoa01c577f2c2018-08-31 09:22:23 +0100268 /// Maps output layer names to their corresponding ids and tensor info.
surmeh01bceff2f2018-03-29 16:29:27 +0100269 std::unordered_map<std::string, BindingPointInfo> m_NetworkOutputsBindingInfo;
270};
Ferran Balaguer51dd62f2019-01-11 19:29:18 +0000271
surmeh01bceff2f2018-03-29 16:29:27 +0100272}