blob: 1fb5b96b8fd7927bc0f96ce531a05c8fe003a931 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
2// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#include "OnnxParser.hpp"
6
Matthew Sloyanac001ee2021-02-03 10:43:04 +00007#include "armnnOnnxParser/Version.hpp"
8
Matthew Bentham39ef3e52020-01-20 10:09:09 +00009#include <armnn/Descriptors.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010010#include <armnn/utility/Assert.hpp>
Matthew Sloyan589e3e82020-09-11 16:17:48 +010011#include <armnn/utility/NumericCast.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010012#include <VerificationHelpers.hpp>
13
James Ward58dec6b2020-09-11 17:32:44 +010014#include <fmt/format.h>
Aron Virginas-Tard4f0fea2019-04-09 14:08:06 +010015
telsoa01c577f2c2018-08-31 09:22:23 +010016#include <google/protobuf/text_format.h>
17#include <google/protobuf/io/zero_copy_stream_impl.h>
18
Matthew Sloyanac001ee2021-02-03 10:43:04 +000019#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010020#include <numeric>
Jan Eilers53ef7952021-06-02 12:01:25 +010021#include <armnnUtils/Permute.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010022
23using namespace armnn;
24
25namespace armnnOnnxParser
26{
Kevin Mayef33cb12021-01-29 14:24:57 +000027
28IOnnxParser::IOnnxParser() : pOnnxParserImpl(new OnnxParserImpl()) {}
29
30IOnnxParser::~IOnnxParser() = default;
31
32IOnnxParser* IOnnxParser::CreateRaw()
33{
34 return new IOnnxParser();
35}
36
37IOnnxParserPtr IOnnxParser::Create()
38{
39 return IOnnxParserPtr(CreateRaw(), &IOnnxParser::Destroy);
40}
41
42void IOnnxParser::Destroy(IOnnxParser* parser)
43{
44 delete parser;
45}
46
47armnn::INetworkPtr IOnnxParser::CreateNetworkFromBinaryFile(const char* graphFile)
48{
49 return pOnnxParserImpl->CreateNetworkFromBinaryFile(graphFile);
50}
51
52armnn::INetworkPtr IOnnxParser::CreateNetworkFromTextFile(const char* graphFile)
53{
54 return pOnnxParserImpl->CreateNetworkFromTextFile(graphFile);
55}
56
57armnn::INetworkPtr IOnnxParser::CreateNetworkFromString(const std::string& protoText)
58{
59 return pOnnxParserImpl->CreateNetworkFromString(protoText);
60}
61
62BindingPointInfo IOnnxParser::GetNetworkInputBindingInfo(const std::string& name) const
63{
64 return pOnnxParserImpl->GetNetworkInputBindingInfo(name);
65}
66
67BindingPointInfo IOnnxParser::GetNetworkOutputBindingInfo(const std::string& name) const
68{
69 return pOnnxParserImpl->GetNetworkOutputBindingInfo(name);
70}
71
telsoa01c577f2c2018-08-31 09:22:23 +010072namespace
73{
74void CheckValidDataType(std::initializer_list<onnx::TensorProto::DataType> validInputTypes,
75 const onnx::TensorProto::DataType actualValue,
76 const char* validExpr,
77 std::string nodeName,
78 std::string tensorName,
79 const armnn::CheckLocation& location)
80{
81 bool isValid = std::any_of(validInputTypes.begin(),
82 validInputTypes.end(),
83 [&actualValue](onnx::TensorProto::DataType x) { return x == actualValue; } );
84 if (!isValid)
85 {
86 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +010087 fmt::format("Datatype {} is not valid for tensor '{}' of node '{}', not in {{{}}}. {}",
88 onnx::TensorProto::DataType_Name(actualValue),
89 tensorName,
90 nodeName,
91 validExpr,
92 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +010093 }
94}
95
96#define CHECK_VALID_DATATYPE(NODE, TENSOR, ACTUAL, ...) \
97CheckValidDataType({__VA_ARGS__}, ACTUAL, #__VA_ARGS__, NODE, TENSOR, CHECK_LOCATION())
98
99using StrTypeListPair = std::pair<const char*, std::initializer_list<onnx::TensorProto::DataType>>;
100#define STR_LIST(...) StrTypeListPair(#__VA_ARGS__, {__VA_ARGS__})
101
102template <typename Callable>
103void ReadMandatoryNodeAttributeImpl(const onnx::NodeProto& node,
104 const std::string& attribName,
105 onnx::AttributeProto::AttributeType expectedType,
106 Callable callable)
107{
108 auto attribs = node.attribute();
109 int attriNum = 0;
110 while (attriNum < node.attribute_size())
111 {
112 if (attribs.Get(attriNum).name() == attribName)
113 {
114 if (attribs.Get(attriNum).type() == expectedType)
115 {
116 callable(attribs.Get(attriNum));
117 }
118 else
119 {
James Ward58dec6b2020-09-11 17:32:44 +0100120 throw ParseException(fmt::format("Attribute {} of node {} expected to have {} as "
121 "onnx::AttributeProto::AttributeType, but found {} instead {}",
122 attribName,
123 node.name(),
124 onnx::AttributeProto::AttributeType_Name(expectedType),
125 onnx::AttributeProto::AttributeType_Name(attribs.Get(attriNum).type()),
126 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100127 }
128 break;
129 }
130 ++attriNum;
131 }
132 if (attriNum == node.attribute_size())
133 {
James Ward58dec6b2020-09-11 17:32:44 +0100134 throw ParseException(fmt::format("Could not find required attribute {} in node {} {}",
135 attribName, node.name(), CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100136 }
137}
138
139template <typename Callable>
140void ReadOptionalNodeAttributeImpl(const onnx::NodeProto& node,
141 const std::string& attribName,
142 onnx::AttributeProto::AttributeType expectedType,
143 Callable callable)
144{
145 auto attribs = node.attribute();
146 for (int attriNum = 0; attriNum < node.attribute_size(); ++attriNum)
147 {
148 if (attribs.Get(attriNum).name() == attribName)
149 {
150 if (attribs.Get(attriNum).type() == expectedType)
151 {
152 callable(attribs.Get(attriNum));
153 }
154 else
155 {
James Ward58dec6b2020-09-11 17:32:44 +0100156 throw ParseException(
157 fmt::format("Attribute {} of node {} expected to have {} as onnx::AttributeProto::AttributeType, "
158 "but found {} instead {}",
159 attribName,
160 node.name(),
161 onnx::AttributeProto::AttributeType_Name(expectedType),
162 onnx::AttributeProto::AttributeType_Name(attribs.Get(attriNum).type()),
163 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100164 }
165 }
166 }
167}
168
Ryan OSheaed27ee72020-04-22 16:37:29 +0100169int64_t ReadOptionalNodeInt64Attribute(const onnx::NodeProto& node,
170 const std::string& name,
171 const int64_t defaultValue = 0)
172{
173 int64_t attribValue = defaultValue;
174 ReadOptionalNodeAttributeImpl(node, name, onnx::AttributeProto::INT,
175 [&attribValue](const onnx::AttributeProto& attrValue)
176 {
177 attribValue = attrValue.i();
178 });
179 return attribValue;
180}
181
telsoa01c577f2c2018-08-31 09:22:23 +0100182std::vector<uint32_t> ReadMandatoryNodeUint32ListAttribute(const onnx::NodeProto& node,
183 const std::string& name)
184{
185 std::vector<uint32_t> attriList;
186 ReadMandatoryNodeAttributeImpl(node, name, onnx::AttributeProto::INTS,
187 [&attriList](const onnx::AttributeProto& attrValue)
188 {
189 for (int attriNum = 0; attriNum < attrValue.ints_size(); ++attriNum)
190 {
191 attriList.push_back(CHECKED_NON_NEGATIVE(CHECKED_INT32(attrValue.ints().Get(attriNum))));
192 }
193 });
194 return attriList;
195}
196
197uint32_t ReadOptionalNodeUint32Attribute(const onnx::NodeProto& node,
198 const std::string& name,
199 const uint32_t defaultVal = 0u)
200{
201 uint32_t attribValue = defaultVal;
202 ReadOptionalNodeAttributeImpl(node, name, onnx::AttributeProto::INT,
203 [&attribValue](const onnx::AttributeProto& attrValue)
204 {
205 attribValue = CHECKED_NON_NEGATIVE(CHECKED_INT32((attrValue.i())));
206 });
207 return attribValue;
208}
209
210std::vector<uint32_t> ReadOptionalNodeUint32ListAttribute(const onnx::NodeProto& node,
211 const std::string& name)
212{
213 std::vector<uint32_t> attriList;
214 ReadOptionalNodeAttributeImpl(node, name, onnx::AttributeProto::INTS,
215 [&attriList](const onnx::AttributeProto& attrValue)
216 {
217 for (int attriNum = 0; attriNum < attrValue.ints_size(); ++attriNum)
218 {
219 attriList.push_back(CHECKED_NON_NEGATIVE(CHECKED_INT32(attrValue.ints().Get(attriNum))));
220 }
221 });
222
223 return attriList;
224}
225
226float ReadOptionalNodeFloatAttribute(const onnx::NodeProto& node,
227 const std::string& name,
228 const float defaultValue = 0.0f)
229{
230 float attribValue = defaultValue;
231 ReadOptionalNodeAttributeImpl(node, name, onnx::AttributeProto::FLOAT,
232 [&attribValue](const onnx::AttributeProto& attrValue)
233 {
234 attribValue = attrValue.f();
235 });
236 return attribValue;
237}
238
239std::string ReadOptionalNodeStringAttribute(const onnx::NodeProto& node, const std::string& name)
240{
241 std::string attribValue = "";
242 ReadOptionalNodeAttributeImpl(node, name, onnx::AttributeProto::STRING,
243 [&attribValue](const onnx::AttributeProto& attrValue)
244 {
245 attribValue = attrValue.s();
246 });
247 return attribValue;
248}
249
Tee Jungfcf6fd52019-11-01 05:27:28 +0000250armnn::TensorInfo ToTensorInfo(const std::string& name, std::vector<unsigned int>& shape, int data_type)
telsoa01c577f2c2018-08-31 09:22:23 +0100251{
telsoa01c577f2c2018-08-31 09:22:23 +0100252 DataType type;
Tee Jungfcf6fd52019-11-01 05:27:28 +0000253 switch(data_type)
telsoa01c577f2c2018-08-31 09:22:23 +0100254 {
255 case onnx::TensorProto::FLOAT:
256 {
257 type = DataType::Float32;
258 break;
259 }
260 case onnx::TensorProto::INT32:
261 case onnx::TensorProto::INT64:
262 {
263 type = DataType::Signed32;
264 break;
265 }
266 default:
267 {
268 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100269 fmt::format("'{}' is not a currently supported datatype for tensor {}."
270 " Supported dataTypes are FLOAT, INT32 and INT64. {}",
271 onnx::TensorProto::DataType_Name(static_cast<onnx::TensorProto::DataType>(data_type)),
272 name,
273 CHECK_LOCATION().AsString() ));
telsoa01c577f2c2018-08-31 09:22:23 +0100274 }
telsoa01c577f2c2018-08-31 09:22:23 +0100275 }
Tee Jungcaf2bdd2019-11-13 07:23:14 +0000276
277 // To avoid crashes by trivial tensors
278 if (shape.empty())
279 {
280 return TensorInfo(TensorShape(), type);
281 }
282
Tee Jungfcf6fd52019-11-01 05:27:28 +0000283 return TensorInfo(TensorShape(static_cast<unsigned int>(shape.size()), shape.data()), type);
284}
285
286armnn::TensorInfo ToTensorInfo(const onnx::ValueInfoProto& info)
287{
288 const onnx::TensorShapeProto onnxShape = info.type().tensor_type().shape();
289 std::vector<unsigned int> shapeDims;
290 for (int i = 0; i < onnxShape.dim_size(); ++i)
291 {
292 shapeDims.push_back(CHECKED_NON_NEGATIVE(CHECKED_INT32(onnxShape.dim(i).dim_value())));
293 }
294
Ryan OShea337c17f2020-02-21 12:33:17 +0000295 if (shapeDims.empty())
296 {
297 shapeDims.push_back(1);
298 }
299
Tee Jungfcf6fd52019-11-01 05:27:28 +0000300 return ToTensorInfo(info.name(), shapeDims, info.type().tensor_type().elem_type());
301}
302
303armnn::TensorInfo ToTensorInfo(const onnx::TensorProto& tensor)
304{
305 std::vector<unsigned int> shapeDims;
Ryan OShea337c17f2020-02-21 12:33:17 +0000306
Tee Jungfcf6fd52019-11-01 05:27:28 +0000307 for (auto dim: tensor.dims())
308 {
309 shapeDims.push_back(CHECKED_NON_NEGATIVE(CHECKED_INT32(dim)));
310 }
311
Ryan OShea337c17f2020-02-21 12:33:17 +0000312 if (shapeDims.empty())
313 {
314 shapeDims.push_back(1);
315 }
316
Tee Jungfcf6fd52019-11-01 05:27:28 +0000317 return ToTensorInfo(tensor.name(), shapeDims, tensor.data_type());
telsoa01c577f2c2018-08-31 09:22:23 +0100318}
319
320std::string TensorInfoAsString(const TensorInfo& info,
321 const std::string& name,
322 const onnx::TensorProto::DataType& type)
323{
324 const TensorShape shape = info.GetShape();
325 std::stringstream ss;
326 ss << "tensor '" << name << "' contains "
327 << onnx::TensorProto::DataType_Name(type)
328 << " and has shape [";
329
330 for (uint32_t i = 0; i < shape.GetNumDimensions() - 1; ++i)
331 {
332 ss << shape[i] << ", ";
333 }
334 ss << shape[shape.GetNumDimensions() - 1] << "]";
335 return ss.str();
336}
337
Sadik Armagan60bb9d82021-01-11 15:15:01 +0000338void CalcPadding(uint32_t inputSize,
339 uint32_t filterSize,
340 uint32_t stride,
341 uint32_t dilation,
342 uint32_t* paddingFront,
343 uint32_t* paddingBack,
344 bool isUpper)
telsoa01c577f2c2018-08-31 09:22:23 +0100345{
346 uint32_t outputSize = (inputSize + stride - 1) / stride;
Sadik Armagan60bb9d82021-01-11 15:15:01 +0000347 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
348 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100349 *paddingFront = (temp - inputSize) / 2;
350 *paddingBack = *paddingFront;
351 if((temp - inputSize) % 2 == 1)
352 {
353 if (isUpper)
354 {
Sadik Armagan60bb9d82021-01-11 15:15:01 +0000355 *paddingBack += 1;
telsoa01c577f2c2018-08-31 09:22:23 +0100356 }
357 else
358 {
Sadik Armagan60bb9d82021-01-11 15:15:01 +0000359 *paddingFront += 1;
telsoa01c577f2c2018-08-31 09:22:23 +0100360 }
361 }
362}
363
Ryan OSheaed27ee72020-04-22 16:37:29 +0100364TensorInfo ComputeReshapeInfo(const TensorShape& targetShapeTensor,
telsoa01c577f2c2018-08-31 09:22:23 +0100365 const TensorShape& inShape,
366 const std::string& outName)
367{
368 std::vector<int> targetDims;
Ryan OSheaed27ee72020-04-22 16:37:29 +0100369 for(uint i = 0; i < targetShapeTensor.GetNumDimensions(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +0100370 {
Ryan OSheaed27ee72020-04-22 16:37:29 +0100371 int val = CHECKED_INT32(targetShapeTensor[i]);
telsoa01c577f2c2018-08-31 09:22:23 +0100372 if(val == 0)
373 {
374 targetDims.push_back(static_cast<int>(inShape[static_cast<uint>(i)]));
375 }
376 else
377 {
378 targetDims.push_back(val);
379 }
380 }
381
382 std::vector<unsigned int> outDims(targetDims.begin(), targetDims.end());
383 const auto stretchDim = std::find(targetDims.begin(), targetDims.end(), -1);
384 if (stretchDim != targetDims.end())
385 {
386 if (std::find(std::next(stretchDim), targetDims.end(), -1) != targetDims.end())
387 {
388 std::stringstream ss;
389 ss << "[ ";
390 for(uint i = 0; i < targetDims.size() - 1; ++i)
391 {
392 ss << targetDims[i] << ", ";
393 }
394 ss << targetDims[targetDims.size() - 1] << " ]";
395
James Ward58dec6b2020-09-11 17:32:44 +0100396 throw ParseException(
397 fmt::format("Error during creation of reshaped tensor '{}'. At most one component of shape can be "
398 " -1 and here, shape is {} {}",
399 outName,
400 ss.str(),
401 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100402 }
403
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100404 auto targetNumElements = armnn::numeric_cast<unsigned int>(std::accumulate(targetDims.begin(), targetDims.end(),
telsoa01c577f2c2018-08-31 09:22:23 +0100405 -1, std::multiplies<int32_t>()));
406 auto stretchIndex = static_cast<size_t>(std::distance(targetDims.begin(), stretchDim));
407 outDims[stretchIndex] = inShape.GetNumElements() / targetNumElements;
408 }
409 TensorShape outShape = TensorShape{static_cast<unsigned int>(outDims.size()), outDims.data()};
410 return TensorInfo(outShape, DataType::Float32);
411}
412
413} //namespace
414
Kevin Mayef33cb12021-01-29 14:24:57 +0000415const std::map<std::string, OnnxParserImpl::OperationParsingFunction> OnnxParserImpl::m_ParserFunctions = {
416 { "BatchNormalization", &OnnxParserImpl::ParseBatchNormalization},
417 { "GlobalAveragePool", &OnnxParserImpl::ParseGlobalAveragePool},
418 { "AveragePool", &OnnxParserImpl::ParseAveragePool },
419 { "Clip", &OnnxParserImpl::ParseClip },
420 { "Constant", &OnnxParserImpl::ParseConstant },
421 { "MaxPool", &OnnxParserImpl::ParseMaxPool },
422 { "Reshape", &OnnxParserImpl::ParseReshape },
423 { "Sigmoid", &OnnxParserImpl::ParseSigmoid },
424 { "Tanh", &OnnxParserImpl::ParseTanh },
425 { "Relu", &OnnxParserImpl::ParseRelu },
426 { "LeakyRelu", &OnnxParserImpl::ParseLeakyRelu },
427 { "Conv", &OnnxParserImpl::ParseConv },
428 { "Add", &OnnxParserImpl::ParseAdd },
429 { "Flatten", &OnnxParserImpl::ParseFlatten},
telsoa01c577f2c2018-08-31 09:22:23 +0100430};
431
432template<typename TypePair, typename Location>
Kevin Mayef33cb12021-01-29 14:24:57 +0000433void OnnxParserImpl::ValidateInputs(const onnx::NodeProto& node,
telsoa01c577f2c2018-08-31 09:22:23 +0100434 TypePair validInputs,
435 const Location& location)
436{
437 for(auto input : node.input())
438 {
439 CheckValidDataType(validInputs.second,
440 m_TensorsInfo[input].m_dtype,
441 validInputs.first,
442 node.name(),
443 input,
444 location);
445 }
446}
447
448#define VALID_INPUTS(NODE, VALID_INPUTS) \
Kevin Mayef33cb12021-01-29 14:24:57 +0000449 OnnxParserImpl::ValidateInputs(NODE, \
telsoa01c577f2c2018-08-31 09:22:23 +0100450 VALID_INPUTS, \
451 CHECK_LOCATION())
452
Kevin Mayef33cb12021-01-29 14:24:57 +0000453std::vector<TensorInfo> OnnxParserImpl::ComputeOutputInfo(std::vector<std::string> outNames,
454 const IConnectableLayer* layer,
455 std::vector<TensorShape> inputShapes)
telsoa01c577f2c2018-08-31 09:22:23 +0100456{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100457 ARMNN_ASSERT(! outNames.empty());
telsoa01c577f2c2018-08-31 09:22:23 +0100458 bool needCompute = std::any_of(outNames.begin(),
459 outNames.end(),
460 [this](std::string name)
461 {
462 return (m_TensorsInfo.count(name) == 0 || m_TensorsInfo[name].m_info == nullptr);
463 });
464 std::vector<TensorInfo> outInfo;
465 //if the output info(s) are not here, we need to compute them
466 std::vector<TensorShape> inferredShapes;
467 if(needCompute)
468 {
469 inferredShapes = layer->InferOutputShapes(inputShapes);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100470 ARMNN_ASSERT(inferredShapes.size() == outNames.size());
telsoa01c577f2c2018-08-31 09:22:23 +0100471 }
472 for (uint i = 0; i < outNames.size(); ++i)
473 {
474 if(needCompute)
475 {
476 m_TensorsInfo[outNames[i]] = OnnxTensor();
477 m_TensorsInfo[outNames[i]].m_info = std::make_unique<TensorInfo>(
478 TensorInfo(inferredShapes[i], DataType::Float32));
479 }
480 outInfo.push_back(*m_TensorsInfo[outNames[i]].m_info);
481 }
482 return outInfo;
483}
484
Kevin Mayef33cb12021-01-29 14:24:57 +0000485OnnxParserImpl::OnnxParserImpl()
telsoa01c577f2c2018-08-31 09:22:23 +0100486 : m_Network(nullptr, nullptr)
487{
488}
489
Kevin Mayef33cb12021-01-29 14:24:57 +0000490void OnnxParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100491{
492 m_Network = armnn::INetworkPtr(nullptr, nullptr);
493 m_Graph = nullptr;
494}
495
Kevin Mayef33cb12021-01-29 14:24:57 +0000496void OnnxParserImpl::Cleanup()
telsoa01c577f2c2018-08-31 09:22:23 +0100497{
498 m_TensorConnections.clear();
499 m_TensorsInfo.clear();
500 m_OutputsMap.clear();
501 m_OutputsFusedAndUsed.clear();
502}
503
Jan Eilers53ef7952021-06-02 12:01:25 +0100504template<typename T>
505std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
506CreateConstTensorImpl(const T* bufferPtr,
507 armnn::TensorInfo& tensorInfo,
508 const armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100509{
Jan Eilers53ef7952021-06-02 12:01:25 +0100510 ARMNN_ASSERT_MSG(bufferPtr != nullptr, fmt::format("Buffer for permutation is null").c_str());
511
512 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
513
514 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
515 {
516 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
517 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
518 reinterpret_cast<const T*>(bufferPtr), data.get(), sizeof(T));
519 }
520 else
521 {
522 ::memcpy(data.get(), bufferPtr, tensorInfo.GetNumBytes());
523 }
524
525 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
526}
527
528std::pair<ConstTensor, std::unique_ptr<float[]>>
529OnnxParserImpl::CreateConstTensor(const std::string name,
530 armnn::Optional<armnn::PermutationVector&> permutationVector)
531{
532 TensorInfo tensorInfo = *m_TensorsInfo[name].m_info;
telsoa01c577f2c2018-08-31 09:22:23 +0100533 onnx::TensorProto onnxTensor = *m_TensorsInfo[name].m_tensor;
534
Jan Eilers53ef7952021-06-02 12:01:25 +0100535 // Const tensors requires at least a list of values
536 if (tensorInfo.GetNumElements() == 0)
537 {
538 throw ParseException(fmt::format("No tensor data found for Const tensor '{}' {}",
539 name,
540 CHECK_LOCATION().AsString()));
541 }
542
telsoa01c577f2c2018-08-31 09:22:23 +0100543 auto srcData = onnxTensor.float_data().data();
Pablo Tello3dcc1c62019-04-24 14:20:21 +0100544 // Copy the value list entries into the destination
545 if (!onnxTensor.has_raw_data())
telsoa01c577f2c2018-08-31 09:22:23 +0100546 {
Pablo Tello3dcc1c62019-04-24 14:20:21 +0100547 if(tensorInfo.GetNumElements() != static_cast<uint>(onnxTensor.float_data_size()))
548 {
James Ward58dec6b2020-09-11 17:32:44 +0100549 throw ParseException(
550 fmt::format("The number of data provided ({}) does not match the tensor '{}' number of "
551 "elements ({}) {}",
552 onnxTensor.float_data_size(),
553 name,
554 tensorInfo.GetNumElements(),
555 CHECK_LOCATION().AsString()));
Pablo Tello3dcc1c62019-04-24 14:20:21 +0100556 }
Jan Eilers53ef7952021-06-02 12:01:25 +0100557 return CreateConstTensorImpl<float>(srcData, tensorInfo, permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +0100558 }
Pablo Tello3dcc1c62019-04-24 14:20:21 +0100559 else
560 {
Jan Eilers53ef7952021-06-02 12:01:25 +0100561 return CreateConstTensorImpl<float>(reinterpret_cast<const float*>(onnxTensor.raw_data().c_str()),
562 tensorInfo,
563 permutationVector);
Pablo Tello3dcc1c62019-04-24 14:20:21 +0100564 }
telsoa01c577f2c2018-08-31 09:22:23 +0100565}
566
Kevin Mayef33cb12021-01-29 14:24:57 +0000567ModelPtr OnnxParserImpl::LoadModelFromTextFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100568{
569 FILE* fd = fopen(graphFile, "r");
570
571 if (fd == nullptr)
572 {
James Ward58dec6b2020-09-11 17:32:44 +0100573 throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100574 }
575
576 // Parse the file into a message
577 ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
578 using google::protobuf::io::FileInputStream;
579 std::unique_ptr<FileInputStream> input = std::make_unique<FileInputStream>(fileno(fd));
580 bool success = google::protobuf::TextFormat::Parse(input.get(), modelProto.get());
581 fclose(fd);
582
583 if (!success)
584 {
585 std::stringstream error;
586 error << "Failed to parse graph file";
James Ward58dec6b2020-09-11 17:32:44 +0100587 throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100588 }
589 return modelProto;
590}
591
Kevin Mayef33cb12021-01-29 14:24:57 +0000592INetworkPtr OnnxParserImpl::CreateNetworkFromTextFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100593{
594 ResetParser();
595 ModelPtr modelProto = LoadModelFromTextFile(graphFile);
596 return CreateNetworkFromModel(*modelProto);
597}
598
599
Kevin Mayef33cb12021-01-29 14:24:57 +0000600ModelPtr OnnxParserImpl::LoadModelFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100601{
602 FILE* fd = fopen(graphFile, "rb");
603
604 if (fd == nullptr)
605 {
James Ward58dec6b2020-09-11 17:32:44 +0100606 throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100607 }
608
609 // Parse the file into a message
610 ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
611
612 google::protobuf::io::FileInputStream inStream(fileno(fd));
613 google::protobuf::io::CodedInputStream codedStream(&inStream);
Nikhil Raje5181532020-10-09 14:52:25 +0100614 codedStream.SetTotalBytesLimit(INT_MAX);
telsoa01c577f2c2018-08-31 09:22:23 +0100615 bool success = modelProto.get()->ParseFromCodedStream(&codedStream);
616 fclose(fd);
617
618 if (!success)
619 {
620 std::stringstream error;
621 error << "Failed to parse graph file";
James Ward58dec6b2020-09-11 17:32:44 +0100622 throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100623 }
624 return modelProto;
625
626}
627
Kevin Mayef33cb12021-01-29 14:24:57 +0000628INetworkPtr OnnxParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100629{
630 ResetParser();
631 ModelPtr modelProto = LoadModelFromBinaryFile(graphFile);
632 return CreateNetworkFromModel(*modelProto);
633}
634
Kevin Mayef33cb12021-01-29 14:24:57 +0000635ModelPtr OnnxParserImpl::LoadModelFromString(const std::string& protoText)
telsoa01c577f2c2018-08-31 09:22:23 +0100636{
637 if (protoText == "")
638 {
James Ward58dec6b2020-09-11 17:32:44 +0100639 throw InvalidArgumentException(fmt::format("Invalid (empty) string for model parameter {}",
640 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100641 }
642 // Parse the string into a message
643 ModelPtr modelProto = std::make_unique<onnx::ModelProto>();
644 bool success = google::protobuf::TextFormat::ParseFromString(protoText, modelProto.get());
645 if (!success)
646 {
647 std::stringstream error;
648 error << "Failed to parse graph file";
James Ward58dec6b2020-09-11 17:32:44 +0100649 throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100650 }
651 return modelProto;
652}
653
Kevin Mayef33cb12021-01-29 14:24:57 +0000654INetworkPtr OnnxParserImpl::CreateNetworkFromString(const std::string& protoText)
telsoa01c577f2c2018-08-31 09:22:23 +0100655{
656 ResetParser();
657 ModelPtr modelProto = LoadModelFromString(protoText);
658 return CreateNetworkFromModel(*modelProto);
659}
660
Kevin Mayef33cb12021-01-29 14:24:57 +0000661INetworkPtr OnnxParserImpl::CreateNetworkFromModel(onnx::ModelProto& model)
telsoa01c577f2c2018-08-31 09:22:23 +0100662{
663 m_Network = INetwork::Create();
664 try
665 {
666 m_Graph = std::make_unique<onnx::GraphProto>(*model.mutable_graph());
667 LoadGraph();
668 }
669 catch (const ParseException& e)
670 {
671 Cleanup();
672 throw e;
673 }
674 Cleanup();
675 return std::move(m_Network);
676}
677
Kevin Mayef33cb12021-01-29 14:24:57 +0000678void OnnxParserImpl::LoadGraph()
telsoa01c577f2c2018-08-31 09:22:23 +0100679{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100680 ARMNN_ASSERT(m_Graph.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100681
682 //Fill m_TensorsInfo with the shapes and value of every tensor
683 SetupInfo(m_Graph->mutable_output());
684 SetupInfo(m_Graph->mutable_input());
685 SetupInfo(m_Graph->mutable_value_info());
686
687 for (auto tensor : m_Graph->initializer())
688 {
689 m_TensorsInfo[tensor.name()].m_tensor = std::make_unique<const onnx::TensorProto>(tensor);
Tee Jungfcf6fd52019-11-01 05:27:28 +0000690 m_TensorsInfo[tensor.name()].m_info = std::make_unique<TensorInfo>(ToTensorInfo(tensor));
691 m_TensorsInfo[tensor.name()].m_dtype =
692 static_cast<onnx::TensorProto::DataType>(tensor.data_type());
telsoa01c577f2c2018-08-31 09:22:23 +0100693 }
694
695 SetupInputLayers();
696 SetupOutputLayers();
697
698 //Detect FullyConnected layers with bias and update the FusedAndUsed map acccordingly
699 DetectFullyConnected();
700
701 //Parsing the graph
702 for(size_t nodeIndex = 0; nodeIndex < static_cast<size_t>(m_Graph->node_size()); nodeIndex++)
703 {
704 auto node = m_Graph->node(static_cast<int>(nodeIndex));
705 const std::string& operation = node.op_type();
706
707 // check which layers we handled already (add and matmul fused as FC)
Ryan OShea337c17f2020-02-21 12:33:17 +0000708 if (operation == "MatMul" )
telsoa01c577f2c2018-08-31 09:22:23 +0100709 {
710 if(m_OutputsFusedAndUsed[nodeIndex].inputForNodes != m_OutputsFusedAndUsed[nodeIndex].fusedWithNodes.size())
711 {
712 //Node which can not be fused as a FullyConnected layer (used in layers as a simple matmul output)
713 AddFullyConnected(node);
714 }
715 }
716 else if (!(m_OutputsFusedAndUsed[nodeIndex].fusedWithNodes.empty()) && operation == "Add")
717 {
718 int matmulIndex = static_cast<int> (m_OutputsFusedAndUsed[nodeIndex].fusedWithNodes[0]);
719 AddFullyConnected(m_Graph->node(matmulIndex), &node);
720 }
721 else if (m_OutputsFusedAndUsed[nodeIndex].fusedWithNodes.empty()) //node is not part of a fused layer
722 {
723 auto it = m_ParserFunctions.find(operation);
724 if (it != m_ParserFunctions.end())
725 {
726 auto func = it->second;
727 (this->*func)(node);
728 }
729 else
730 {
James Ward58dec6b2020-09-11 17:32:44 +0100731 throw ParseException(fmt::format("Unsupported operation {} for node '{}' {}",
732 operation,
733 node.name(),
734 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100735 }
736 }
737 }
738
739 //Making the connections between outputs and inputs of each layers
740 for (const auto& tensorCon : m_TensorConnections)
741 {
742 if (tensorCon.second.outputSlot != nullptr)
743 {
744 for (size_t inputSlotIdx = 0; inputSlotIdx < tensorCon.second.inputSlots.size(); ++inputSlotIdx)
745 {
746 tensorCon.second.outputSlot->Connect(*(tensorCon.second.inputSlots[inputSlotIdx]));
747 }
748 }
749 }
750}
751
Kevin Mayef33cb12021-01-29 14:24:57 +0000752void OnnxParserImpl::SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list)
telsoa01c577f2c2018-08-31 09:22:23 +0100753{
754 for (auto tensor : *list)
755 {
756 m_TensorsInfo[tensor.name()] = OnnxTensor();
757 m_TensorsInfo[tensor.name()].m_info = std::make_unique<TensorInfo>(ToTensorInfo(tensor));
Matteo Martincighe355dc22018-12-10 13:45:27 +0000758 m_TensorsInfo[tensor.name()].m_dtype =
759 static_cast<onnx::TensorProto::DataType>(tensor.type().tensor_type().elem_type());
telsoa01c577f2c2018-08-31 09:22:23 +0100760 }
761}
762
Kevin Mayef33cb12021-01-29 14:24:57 +0000763void OnnxParserImpl::DetectFullyConnected()
telsoa01c577f2c2018-08-31 09:22:23 +0100764{
765 m_OutputsFusedAndUsed = std::vector<UsageSummary> (static_cast<size_t>(m_Graph->node_size()), UsageSummary());
766 auto matmulAndConstant = [&](const std::string& constInput,
767 const std::string& matmulInput,
768 int& nodeIndex)
769 {
770 auto matmulIt = m_OutputsMap.find(matmulInput);
771 if(matmulIt != m_OutputsMap.end() && matmulIt->second.first->op_type() == "MatMul"
772 && m_TensorsInfo[constInput].isConstant())
773 {
774 nodeIndex = matmulIt->second.second;
775 return true;
776 }
777 return false;
778 };
779
780 for(int nodeIndex = 0; nodeIndex < m_Graph->node_size(); nodeIndex++)
781 {
782 const onnx::NodeProto* node = &m_Graph->node(nodeIndex);
783 for (const std::string& output : node->output())
784 {
785 m_OutputsMap[output] = std::make_pair(node, nodeIndex);
786 }
787
788 for (const std::string& input : node->input()) //count how many time a node is used as input
789 {
790 auto matmulIt = m_OutputsMap.find(input);
791 if(matmulIt != m_OutputsMap.end()){
792 ++m_OutputsFusedAndUsed[static_cast<size_t>(matmulIt->second.second)].inputForNodes; //node used
793 }
794 }
795
796 if (node->op_type() == "Add")
797 {
798 int matmulIndex = 0;
799 if (matmulAndConstant(node->input(0), node->input(1), matmulIndex) ||
800 matmulAndConstant(node->input(1), node->input(0), matmulIndex))
801 {
802 //matmul and add were fused
803 m_OutputsFusedAndUsed[static_cast<size_t>(matmulIndex)].fusedWithNodes
804 .push_back(static_cast<size_t>(nodeIndex));
805
806 m_OutputsFusedAndUsed[static_cast<size_t>(nodeIndex)].fusedWithNodes
807 .push_back(static_cast<size_t>(matmulIndex));
808 }
809 }
810 }
811
812 for (auto output: m_Graph->output()) { //Add usages as output of the graph in count of usages
813 auto matmulIt = m_OutputsMap.find(output.name());
814 if(matmulIt != m_OutputsMap.end()){
815 ++m_OutputsFusedAndUsed[static_cast<size_t>(matmulIt->second.second)].inputForNodes;
816 }
817 }
818}
819
820template<typename Location>
Kevin Mayef33cb12021-01-29 14:24:57 +0000821void OnnxParserImpl::GetInputAndParam(const onnx::NodeProto& node,
822 std::string* inputName,
823 std::string* constName,
824 const Location& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100825{
826 int cstIndex;
827 if (m_TensorsInfo[node.input(0)].isConstant())
828 {
829 cstIndex = 0;
830 }
831 else if (m_TensorsInfo[node.input(1)].isConstant())
832 {
833 cstIndex = 1;
834 }
835 else
836 {
James Ward58dec6b2020-09-11 17:32:44 +0100837 throw ParseException(fmt::format("One of the input tensors ('{}' or '{}') should be constant in node '{}' {}",
838 node.input(0),
839 node.input(1),
840 node.name(),
841 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100842 }
843 if(constName)
844 {
845 *constName = node.input(cstIndex);
846 }
847 if(inputName)
848 {
849 *inputName = node.input(!cstIndex);
850 }
851}
852
853template<typename Location>
Kevin Mayef33cb12021-01-29 14:24:57 +0000854void OnnxParserImpl::To1DTensor(const std::string& name, const Location& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100855{
856 TensorShape shape = m_TensorsInfo[name].m_info->GetShape();
857 std::vector<uint32_t> newShape;
858 for(uint i = 0; i < shape.GetNumDimensions() - 1; ++i)
859 {
860 if(shape[i] != 1)
861 {
James Ward58dec6b2020-09-11 17:32:44 +0100862 throw ParseException(
863 fmt::format("Only tensors with shape [1, ..., 1, X] can be converted to 1D and {} {}",
864 TensorInfoAsString(*m_TensorsInfo[name].m_info, name, m_TensorsInfo[name].m_dtype),
865 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100866 }
867 }
868 newShape.push_back(shape[shape.GetNumDimensions() - 1]);
869
870 m_TensorsInfo[name].m_info->SetShape(TensorShape(static_cast<unsigned int>(newShape.size()), newShape.data()));
871}
872
Kevin Mayef33cb12021-01-29 14:24:57 +0000873void OnnxParserImpl::AddConvLayerWithDepthwiseConv(const onnx::NodeProto& node, const Convolution2dDescriptor& convDesc)
Ryan OSheaed27ee72020-04-22 16:37:29 +0100874{
875 ARMNN_ASSERT(node.op_type() == "Conv");
876
877 DepthwiseConvolution2dDescriptor desc;
878 desc.m_PadLeft = convDesc.m_PadLeft;
879 desc.m_PadRight = convDesc.m_PadRight;
880 desc.m_PadTop = convDesc.m_PadTop;
881 desc.m_PadBottom = convDesc.m_PadBottom;
882 desc.m_StrideX = convDesc.m_StrideX;
883 desc.m_StrideY = convDesc.m_StrideY;
884 desc.m_BiasEnabled = convDesc.m_BiasEnabled;
885
886 armnn::IConnectableLayer* layer;
Jan Eilers53ef7952021-06-02 12:01:25 +0100887
888 // weights come in as [O,1,H,W] from ONNX and need to be converted to ArmNNs dephtwise weights layout [1,H,W,O]
889 armnn::PermutationVector perVec {3,0,1,2};
890 auto weightTensor = CreateConstTensor(node.input(1), perVec);
Ryan OSheaed27ee72020-04-22 16:37:29 +0100891
892 if (node.input_size() == 3)
893 {
894 if(!m_TensorsInfo[node.input(2)].isConstant())
895 {
James Ward58dec6b2020-09-11 17:32:44 +0100896 throw ParseException(fmt::format("Bias '{}' should be constant in Conv layer '{}' {}",
897 node.input(2),
898 node.name(),
899 CHECK_LOCATION().AsString()));
Ryan OSheaed27ee72020-04-22 16:37:29 +0100900 }
901 desc.m_BiasEnabled = true;
902 auto biasTensor = CreateConstTensor(node.input(2));
903 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
904 weightTensor.first,
905 Optional<ConstTensor>(biasTensor.first),
906 node.name().c_str());
907 }
908 else
909 {
910 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
911 weightTensor.first,
912 EmptyOptional(),
913 node.name().c_str());
914 }
915 ARMNN_ASSERT(layer != nullptr);
916
917 auto outputInfo = ComputeOutputInfo({ node.output(0) }, layer,
918 { m_TensorsInfo[node.input(0)].m_info->GetShape(),
Jan Eilers53ef7952021-06-02 12:01:25 +0100919 weightTensor.first.GetInfo().GetShape() });
Ryan OSheaed27ee72020-04-22 16:37:29 +0100920
921 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
922
923 // register the input connection slots for the layer, connections are made after all layers have been created
924 // only the tensors for the inputs are relevant, exclude the const tensors
925 RegisterInputSlots(layer, {node.input(0)});
926
927 // register the output connection slots for the layer, connections are made after all layers have been created
928 RegisterOutputSlots(layer, {node.output(0)});
929}
930
Kevin Mayef33cb12021-01-29 14:24:57 +0000931void OnnxParserImpl::AddFullyConnected(const onnx::NodeProto& matmulNode, const onnx::NodeProto* addNode)
telsoa01c577f2c2018-08-31 09:22:23 +0100932{
933
934 // find matmul inputs
935 std::string weightName;
936 std::string inputName;
937 CHECK_VALID_SIZE(static_cast<size_t>(matmulNode.input_size()), 2);
938 CHECK_VALID_SIZE(static_cast<size_t>(matmulNode.output_size()), 1);
939 VALID_INPUTS(matmulNode, STR_LIST(onnx::TensorProto::FLOAT));
940
941 GetInputAndParam(matmulNode, &inputName, &weightName, CHECK_LOCATION());
942
943 FullyConnectedDescriptor desc;
944 desc.m_BiasEnabled = addNode != nullptr;
945
946 IConnectableLayer* layer = nullptr;
947 if(desc.m_BiasEnabled)
948 {
949 // find bias const
950 std::string biasName;
951 CHECK_VALID_SIZE(static_cast<size_t>(addNode->input_size()), 2);
952 CHECK_VALID_SIZE(static_cast<size_t>(addNode->output_size()), 1);
953 VALID_INPUTS(*addNode, STR_LIST(onnx::TensorProto::FLOAT));
954
955 GetInputAndParam(*addNode, nullptr, &biasName, CHECK_LOCATION());
956
957 //Output shape is [1, weights[1]] and 1d vec in ONNX can be [1,X] so we convert biases to "armnn" 1D
958 To1DTensor(biasName, CHECK_LOCATION());
959 TensorInfo weightInfo = *m_TensorsInfo[weightName].m_info;
960 TensorInfo biasInfo = *m_TensorsInfo[biasName].m_info;
961
962 if (weightInfo.GetShape()[1] != biasInfo.GetShape()[0])
963 {
James Ward58dec6b2020-09-11 17:32:44 +0100964 throw ParseException(
965 fmt::format("Shape of weights '{}' and bias of following Add node '{}' do not match : {}"
966 " and {} ( /!\\ bias should be a 1D tensor) {}",
967 weightName,
968 addNode->name(),
969 TensorInfoAsString(*m_TensorsInfo[weightName].m_info, weightName,
970 m_TensorsInfo[weightName].m_dtype),
971 TensorInfoAsString(*m_TensorsInfo[biasName].m_info, biasName,
972 m_TensorsInfo[biasName].m_dtype ),
973 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100974 }
975 layer = m_Network->AddFullyConnectedLayer(desc,
976 CreateConstTensor(weightName).first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100977 Optional<ConstTensor>(CreateConstTensor(biasName).first),
telsoa01c577f2c2018-08-31 09:22:23 +0100978 matmulNode.name().c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100979 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100980
981 auto outputInfo = ComputeOutputInfo({addNode->output(0)}, layer,
982 {m_TensorsInfo[inputName].m_info->GetShape(),
983 m_TensorsInfo[weightName].m_info->GetShape()});
984
985 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
986
987 RegisterInputSlots(layer, {inputName});
988 RegisterOutputSlots(layer, {addNode->output(0)});
989 }
990 else
991 {
Matteo Martincighfc598e12019-05-14 10:36:13 +0100992 layer = m_Network->AddFullyConnectedLayer(desc,
993 CreateConstTensor(weightName).first,
994 EmptyOptional(),
995 matmulNode.name().c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100996 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100997
998 auto outputInfo = ComputeOutputInfo({matmulNode.output(0)}, layer,
999 {m_TensorsInfo[inputName].m_info->GetShape(),
1000 m_TensorsInfo[weightName].m_info->GetShape()});
1001 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1002
1003 RegisterInputSlots(layer, {inputName});
1004 RegisterOutputSlots(layer, {matmulNode.output(0)});
1005 }
1006}
1007
Kevin Mayef33cb12021-01-29 14:24:57 +00001008void OnnxParserImpl::AddPoolingLayer(const onnx::NodeProto& node, Pooling2dDescriptor& desc)
telsoa01c577f2c2018-08-31 09:22:23 +01001009{
1010
1011 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 1);
1012 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1013
1014 VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT));
1015
1016 std::vector<uint32_t> kernel_shape = ReadMandatoryNodeUint32ListAttribute(node, "kernel_shape"); //size of pool win
1017 std::vector<uint32_t> strides = ReadOptionalNodeUint32ListAttribute(node, "strides");
1018 std::vector<uint32_t> pads = ReadOptionalNodeUint32ListAttribute(node, "pads");
1019
1020 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
1021 desc.m_PoolWidth = kernel_shape[1];
1022 desc.m_PoolHeight = kernel_shape[0];
1023
1024 if(strides.empty())
1025 {
1026 desc.m_StrideX = 1;
1027 desc.m_StrideY = 1;
1028 }
1029 else
1030 {
1031 desc.m_StrideX = strides[1];
1032 desc.m_StrideY = strides[0];
1033 }
1034
1035 //Check new padding version first
1036 if(pads.empty())
1037 {
1038 //Check deprecated version
1039 std::string paddingString = ReadOptionalNodeStringAttribute(node, "auto_pad");
1040 if(paddingString != "VALID" && paddingString != "" && paddingString != "NOTSET")
1041 {
1042 bool isUpper;
1043 if( paddingString == "SAME_LOWER")
1044 {
1045 isUpper = false;
1046 }
1047 else if (paddingString == "SAME_UPPER")
1048 {
1049 isUpper = true;
1050 }
1051 else
1052 {
James Ward58dec6b2020-09-11 17:32:44 +01001053 throw ParseException(fmt::format("Invalid auto_pad attribute for node {}. "
1054 "Only SAME_UPPER, SAME_LOWER or VALID supported and found {} {}",
1055 node.name(),
1056 paddingString,
1057 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001058 }
1059 auto inputInfo = *m_TensorsInfo[node.input(0)].m_info;
1060 uint32_t inputHeight = inputInfo.GetShape()[2];
1061 uint32_t inputWidth = inputInfo.GetShape()[3];
Sadik Armagan60bb9d82021-01-11 15:15:01 +00001062 CalcPadding(inputHeight,
1063 desc.m_PoolHeight,
1064 desc.m_StrideY,
1065 1u,
1066 &desc.m_PadTop,
1067 &desc.m_PadBottom,
1068 isUpper);
1069 CalcPadding(inputWidth,
1070 desc.m_PoolWidth,
1071 desc.m_StrideX,
1072 1u,
1073 &desc.m_PadLeft,
1074 &desc.m_PadRight,
1075 isUpper);
telsoa01c577f2c2018-08-31 09:22:23 +01001076 }
1077 }
1078 else
1079 {
1080 desc.m_PadTop = pads[0];
1081 desc.m_PadLeft = pads[1];
1082 desc.m_PadBottom = pads[2];
1083 desc.m_PadRight = pads[3];
1084 }
1085
1086 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, node.name().c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001087 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001088
1089 auto outputInfo = ComputeOutputInfo({node.output(0)}, layer, {m_TensorsInfo[node.input(0)].m_info->GetShape()});
1090 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1091
1092 // register the input connection slots for the layer, connections are made after all layers have been created
1093 // only the tensors for the inputs are relevant, exclude the const tensors
1094 RegisterInputSlots(layer, {node.input(0)});
1095
1096 // register the output connection slots for the layer, connections are made after all layers have been created
1097 RegisterOutputSlots(layer, {node.output(0)});
1098}
1099
Kevin Mayef33cb12021-01-29 14:24:57 +00001100std::pair<std::string, std::string> OnnxParserImpl::AddPrepareBroadcast(const std::string& input0,
1101 const std::string& input1)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001102{
1103 std::pair<std::string, std::string> inputs = std::make_pair(input0, input1);
1104
1105 TensorShape input0Shape = m_TensorsInfo[input0].m_info->GetShape();
1106 TensorShape input1Shape = m_TensorsInfo[input1].m_info->GetShape();
1107
1108 if(input1Shape.GetNumDimensions() < input0Shape.GetNumDimensions())
1109 {
James Ward58dec6b2020-09-11 17:32:44 +01001110 auto outputName = fmt::format("reshape_output_{}", input1);
Ryan OSheaed27ee72020-04-22 16:37:29 +01001111 PrependForBroadcast(outputName, input1, input0);
1112 inputs.second = outputName;
1113 }
1114 else if(input0Shape.GetNumDimensions() < input1Shape.GetNumDimensions())
1115 {
James Ward58dec6b2020-09-11 17:32:44 +01001116 auto outputName = fmt::format("reshape_output_{}", input0);
Ryan OSheaed27ee72020-04-22 16:37:29 +01001117 PrependForBroadcast(outputName, input0, input1);
1118 inputs.first = outputName;
1119 }
1120 return inputs;
1121}
1122
Kevin Mayef33cb12021-01-29 14:24:57 +00001123void OnnxParserImpl::CreateConstantLayer(const std::string& tensorName, const std::string& layerName)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001124{
1125 auto armnnTensor = CreateConstTensor(tensorName);
1126
1127 IConnectableLayer* layer = m_Network->AddConstantLayer(armnnTensor.first, layerName.c_str());
1128 layer->GetOutputSlot(0).SetTensorInfo(armnnTensor.first.GetInfo());
1129 RegisterOutputSlots(layer, {tensorName});
1130}
1131
Kevin Mayef33cb12021-01-29 14:24:57 +00001132void OnnxParserImpl::CreateReshapeLayer(const std::string& inputName,
1133 const std::string& outputName,
1134 const std::string& layerName)
telsoa01c577f2c2018-08-31 09:22:23 +01001135{
1136 const TensorInfo outputTensorInfo = *m_TensorsInfo[outputName].m_info;
1137 ReshapeDescriptor reshapeDesc;
1138 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1139
1140 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001141 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001142 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1143
1144 // register the input connection slots for the layer, connections are made after all layers have been created
1145 // only the tensors for the inputs are relevant, exclude the const tensors
1146 RegisterInputSlots(layer, {inputName});
1147
1148 // register the output connection slots for the layer, connections are made after all layers have been created
1149 RegisterOutputSlots(layer, {outputName});
1150}
1151
Kevin Mayef33cb12021-01-29 14:24:57 +00001152void OnnxParserImpl::ParseActivation(const onnx::NodeProto& node, const armnn::ActivationFunction func)
telsoa01c577f2c2018-08-31 09:22:23 +01001153{
Finn Williams7ee5d2c2020-03-27 11:11:50 +00001154 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 1, 3);
telsoa01c577f2c2018-08-31 09:22:23 +01001155 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1156
1157 VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT));
1158
1159 ActivationDescriptor desc;
Tee Jung7ff9a602019-11-01 07:04:42 +00001160 desc.m_Function = func;
telsoa01c577f2c2018-08-31 09:22:23 +01001161
Finn Williams7ee5d2c2020-03-27 11:11:50 +00001162 if (func == ActivationFunction::BoundedReLu)
1163 {
1164 desc.m_A = node.input(2).empty() ? std::numeric_limits<float>::max() : std::stof(node.input(2));
1165 desc.m_B = node.input(1).empty() ? std::numeric_limits<float>::lowest() : std::stof(node.input(1));
1166 }
1167
telsoa01c577f2c2018-08-31 09:22:23 +01001168 IConnectableLayer* const layer = m_Network->AddActivationLayer(desc, node.name().c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001169 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001170
1171 auto outputInfo = ComputeOutputInfo({ node.output(0)}, layer, {m_TensorsInfo[node.input(0)].m_info->GetShape()});
1172 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1173
1174 // register the input connection slots for the layer, connections are made after all layers have been created
1175 // only the tensors for the inputs are relevant, exclude the const tensors
1176 RegisterInputSlots(layer, {node.input(0)});
1177
1178 // register the output connection slots for the layer, connections are made after all layers have been created
1179 RegisterOutputSlots(layer, {node.output(0)});
1180}
1181
Kevin Mayef33cb12021-01-29 14:24:57 +00001182void OnnxParserImpl::ParseClip(const onnx::NodeProto& node)
Finn Williams7ee5d2c2020-03-27 11:11:50 +00001183{
1184 ParseActivation(node, ActivationFunction::BoundedReLu);
1185}
1186
Kevin Mayef33cb12021-01-29 14:24:57 +00001187void OnnxParserImpl::ParseSigmoid(const onnx::NodeProto& node)
Tee Jung7ff9a602019-11-01 07:04:42 +00001188{
1189 ParseActivation(node, ActivationFunction::Sigmoid);
1190}
1191
Kevin Mayef33cb12021-01-29 14:24:57 +00001192void OnnxParserImpl::ParseTanh(const onnx::NodeProto& node)
Tee Jung7ff9a602019-11-01 07:04:42 +00001193{
1194 ParseActivation(node, ActivationFunction::TanH);
1195}
1196
Kevin Mayef33cb12021-01-29 14:24:57 +00001197void OnnxParserImpl::ParseRelu(const onnx::NodeProto& node)
Tee Jung7ff9a602019-11-01 07:04:42 +00001198{
1199 ParseActivation(node, ActivationFunction::ReLu);
1200}
1201
Kevin Mayef33cb12021-01-29 14:24:57 +00001202void OnnxParserImpl::ParseLeakyRelu(const onnx::NodeProto& node)
Tee Jung7ff9a602019-11-01 07:04:42 +00001203{
1204 ParseActivation(node, ActivationFunction::LeakyReLu);
1205}
telsoa01c577f2c2018-08-31 09:22:23 +01001206
Kevin Mayef33cb12021-01-29 14:24:57 +00001207void OnnxParserImpl::ParseAdd(const onnx::NodeProto& node)
telsoa01c577f2c2018-08-31 09:22:23 +01001208{
Ryan OSheaed27ee72020-04-22 16:37:29 +01001209 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 2);
1210 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001211
Ryan OSheaed27ee72020-04-22 16:37:29 +01001212 VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT));
telsoa01c577f2c2018-08-31 09:22:23 +01001213
Ryan OSheaed27ee72020-04-22 16:37:29 +01001214 // TODO: unify broadcast validation code across layers
1215 // tracked by: IVGCVSW-1576
telsoa01c577f2c2018-08-31 09:22:23 +01001216
Ryan OSheaed27ee72020-04-22 16:37:29 +01001217 // Checking broadcast compatibility : only scalar or 1D tensors
1218 auto inputs = AddPrepareBroadcast(node.input(0), node.input(1));
1219 auto input0 = *m_TensorsInfo[inputs.first].m_info;
1220 auto input1 = *m_TensorsInfo[inputs.second].m_info;
1221 ARMNN_ASSERT(input0.GetNumDimensions() == input1.GetNumDimensions());
1222
1223 unsigned int numDims = input0.GetNumDimensions();
1224 for (unsigned int i = 0; i < numDims; i++)
telsoa01c577f2c2018-08-31 09:22:23 +01001225 {
Ryan OSheaed27ee72020-04-22 16:37:29 +01001226 unsigned int dim0 = input0.GetShape()[i];
1227 unsigned int dim1 = input1.GetShape()[i];
1228 if (dim0 != dim1 && dim0 != 1 && dim1 != 1)
telsoa01c577f2c2018-08-31 09:22:23 +01001229 {
James Ward58dec6b2020-09-11 17:32:44 +01001230 throw ParseException(
1231 fmt::format("Broadcast is only supported for scalar or 1D tensors in Add node '{}'. "
1232 "Input dimensions should either match or one should be of size 1 and here, "
1233 "{} and {} {}",
1234 node.name(),
1235 TensorInfoAsString(*m_TensorsInfo[inputs.first].m_info, inputs.first,
1236 m_TensorsInfo[inputs.first].m_dtype),
1237 TensorInfoAsString(*m_TensorsInfo[inputs.second].m_info, inputs.second,
1238 m_TensorsInfo[inputs.second].m_dtype),
1239 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001240 }
telsoa01c577f2c2018-08-31 09:22:23 +01001241 }
Ryan OSheaed27ee72020-04-22 16:37:29 +01001242
1243
1244 IConnectableLayer* layer = m_Network->AddAdditionLayer(node.name().c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001245 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001246
1247 auto outputInfo = ComputeOutputInfo({ node.output(0) }, layer,
Ryan OSheaed27ee72020-04-22 16:37:29 +01001248 { m_TensorsInfo[inputs.first].m_info->GetShape(),
1249 m_TensorsInfo[inputs.second].m_info->GetShape() });
telsoa01c577f2c2018-08-31 09:22:23 +01001250 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1251
Ryan OSheaed27ee72020-04-22 16:37:29 +01001252 // register the input connection -> for constant inputs, we need to make a newDim constant layer
1253 if(m_TensorsInfo[inputs.first].isConstant()) {
James Ward58dec6b2020-09-11 17:32:44 +01001254 CreateConstantLayer(inputs.first, fmt::format("Add:constant_of_{}", node.input(0)));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001255 }
1256 if(m_TensorsInfo[inputs.second].isConstant()) {
James Ward58dec6b2020-09-11 17:32:44 +01001257 CreateConstantLayer(inputs.second, fmt::format("Add:constant_of_{}", node.input(1)));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001258 }
1259 RegisterInputSlots(layer, {inputs.first, inputs.second});
telsoa01c577f2c2018-08-31 09:22:23 +01001260
Ryan OSheaed27ee72020-04-22 16:37:29 +01001261 // register the output connection
telsoa01c577f2c2018-08-31 09:22:23 +01001262 RegisterOutputSlots(layer, {node.output(0)});
1263}
1264
Kevin Mayef33cb12021-01-29 14:24:57 +00001265void OnnxParserImpl::ParseAveragePool(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001266{
1267 Pooling2dDescriptor desc;
1268 desc.m_PoolType = PoolingAlgorithm::Average;
1269
1270 uint32_t count_include_pad = 0;
1271 count_include_pad = ReadOptionalNodeUint32Attribute(node, "count_include_pad");
1272 if(count_include_pad) {
1273 desc.m_PaddingMethod = PaddingMethod::IgnoreValue;
1274 }
1275 AddPoolingLayer(node, desc);
1276}
1277
Kevin Mayef33cb12021-01-29 14:24:57 +00001278void OnnxParserImpl::ParseBatchNormalization(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001279{
1280 //IGNORE momentum parameter and spatial parameters
1281
1282 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 5);
1283 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1284
1285 VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT));
1286 for(int ind = 1; ind < node.input_size(); ++ind)
1287 {
1288 auto tensor = node.input(ind);
1289 if(! m_TensorsInfo[tensor].isConstant())
1290 {
James Ward58dec6b2020-09-11 17:32:44 +01001291 throw ParseException(
1292 fmt::format("Input tensor '{}' should be constant in BatchNormalization node '{}' {}",
1293 tensor,
1294 node.name(),
1295 CHECK_LOCATION().AsString()));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001296 }
1297 }
1298
1299 float epsilon = ReadOptionalNodeFloatAttribute(node, "epsilon", 1e-5f);
1300 BatchNormalizationDescriptor desc;
1301 desc.m_Eps = epsilon;
1302
1303 auto scaleTensor = CreateConstTensor(node.input(1));
1304 auto biasTensor = CreateConstTensor(node.input(2));
1305 auto meanTensor = CreateConstTensor(node.input(3));
1306 auto varTensor = CreateConstTensor(node.input(4));
1307
1308 IConnectableLayer* layer = m_Network->AddBatchNormalizationLayer(desc,
1309 meanTensor.first,
1310 varTensor.first,
1311 biasTensor.first,
1312 scaleTensor.first,
1313 node.name().c_str());
1314 ARMNN_ASSERT(layer != nullptr);
1315
1316 auto outputInfo = ComputeOutputInfo({node.output(0)}, layer, {m_TensorsInfo[node.input(0)].m_info->GetShape()});
1317 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1318
1319 RegisterInputSlots(layer, {node.input(0)}); //don't register constant inputs
1320
1321 // register the output connection
1322 RegisterOutputSlots(layer, {node.output(0)});
1323}
1324
Kevin Mayef33cb12021-01-29 14:24:57 +00001325void OnnxParserImpl::ParseConstant(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001326{
1327 CHECK_VALID_SIZE(static_cast<size_t>(node.attribute_size()), 1);
1328 if (!node.attribute(0).has_t())
1329 {
James Ward58dec6b2020-09-11 17:32:44 +01001330 throw ParseException(fmt::format("Value not found for Constant node '{}' {}",
1331 node.name(),
1332 CHECK_LOCATION().AsString()));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001333 }
1334 const onnx::TensorProto& onnxTensor = node.attribute(0).t();
1335
1336 //ONNX can have Float16 and double constant nodes but ArmNN only supports float32
1337 CHECK_VALID_DATATYPE(node.name(), onnxTensor.name(),
1338 static_cast<onnx::TensorProto::DataType>(onnxTensor.data_type()), onnx::TensorProto::FLOAT);
1339
1340 //Register this as a m_ConstParam so we know we can use it as a constant param in future layers.
1341 m_TensorsInfo[node.output(0)].m_tensor = std::make_unique<const onnx::TensorProto>(onnxTensor);
1342 m_TensorsInfo[node.output(0)].m_info = std::make_unique<TensorInfo>(ToTensorInfo(onnxTensor));
1343 m_TensorsInfo[node.output(0)].m_dtype = static_cast<onnx::TensorProto::DataType>(onnxTensor.data_type());
1344
1345 CreateConstantLayer(node.output(0), node.name());
1346}
1347
Kevin Mayef33cb12021-01-29 14:24:57 +00001348void OnnxParserImpl::ParseConv(const onnx::NodeProto& node)
telsoa01c577f2c2018-08-31 09:22:23 +01001349{
1350 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 2, 3); //input, weight, (bias)
1351 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1352
1353 VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT));
1354
1355 if(m_TensorsInfo[node.input(0)].m_info->GetNumDimensions() != 4)
1356 {
James Ward58dec6b2020-09-11 17:32:44 +01001357 throw ParseException(
1358 fmt::format("ArmNN only supports 2D convolution and Conv layer '{}' input {} {}",
1359 node.name(),
1360 TensorInfoAsString(*m_TensorsInfo[node.input(0)].m_info, node.input(0),
1361 m_TensorsInfo[node.input(0)].m_dtype),
1362 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001363 }
1364
1365 if(!m_TensorsInfo[node.input(1)].isConstant())
1366 {
James Ward58dec6b2020-09-11 17:32:44 +01001367 throw ParseException(
1368 fmt::format("Weights '{}' should be constant in Conv layer '{}' {}",
1369 node.input(1),
1370 node.name(),
1371 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001372 }
1373
1374 auto inputInfo = *m_TensorsInfo[node.input(0)].m_info;
1375
telsoa01c577f2c2018-08-31 09:22:23 +01001376 Convolution2dDescriptor desc;
1377 desc.m_BiasEnabled = false;
1378
1379 std::vector<uint32_t> strides = ReadOptionalNodeUint32ListAttribute(node, "strides");
1380 if(strides.empty())
1381 {
1382 desc.m_StrideX = 1;
1383 desc.m_StrideY = 1;
1384 }
1385 else
1386 {
1387 desc.m_StrideX = strides[1];
1388 desc.m_StrideY = strides[0];
1389 }
1390
Sadik Armagan60bb9d82021-01-11 15:15:01 +00001391 std::vector<uint32_t> dilations = ReadOptionalNodeUint32ListAttribute(node, "dilations");
1392 if(!dilations.empty())
1393 {
1394 desc.m_DilationX = dilations[1];
1395 desc.m_DilationY = dilations[0];
1396 }
1397
telsoa01c577f2c2018-08-31 09:22:23 +01001398 std::vector<uint32_t> pads = ReadOptionalNodeUint32ListAttribute(node, "pads");
1399 //Check new padding version first
1400 if(pads.empty())
1401 {
1402 //Check deprecated version
1403 std::string paddingString = ReadOptionalNodeStringAttribute(node, "auto_pad");
1404 if(paddingString != "VALID" && paddingString != "" && paddingString != "NOTSET")
1405 {
1406 bool isUpper;
1407 if( paddingString == "SAME_LOWER")
1408 {
1409 isUpper = false;
1410 }
1411 else if (paddingString == "SAME_UPPER")
1412 {
1413 isUpper = true;
1414 }
1415 else
1416 {
James Ward58dec6b2020-09-11 17:32:44 +01001417 throw ParseException(
1418 fmt::format("Invalid auto_pad attribute for node {}. Only SAME_UPPER, SAME_LOWER or VALID "
1419 "supported and found {} {}",
1420 node.name(),
1421 paddingString,
1422 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001423 }
1424 uint32_t inputHeight = inputInfo.GetShape()[2];
1425 uint32_t inputWidth = inputInfo.GetShape()[3];
1426
1427 uint32_t weightHeight;
1428 uint32_t weightWidth;
1429 std::vector<uint32_t> kernel_shape = ReadOptionalNodeUint32ListAttribute(node, "kernel_shape");
1430 if (kernel_shape.empty())
1431 {
1432 const TensorInfo weightTensorInfo = *m_TensorsInfo[node.input(1)].m_info;
1433 weightHeight = weightTensorInfo.GetShape()[2];
1434 weightWidth = weightTensorInfo.GetShape()[3];
1435 }
1436 else
1437 {
1438 weightHeight = kernel_shape[0];
1439 weightWidth = kernel_shape[1];
1440 }
Sadik Armagan60bb9d82021-01-11 15:15:01 +00001441 CalcPadding(inputHeight,
1442 weightHeight,
1443 desc.m_StrideY,
1444 desc.m_DilationY,
1445 &desc.m_PadTop,
1446 &desc.m_PadBottom,
1447 isUpper);
1448 CalcPadding(inputWidth,
1449 weightWidth,
1450 desc.m_StrideX,
1451 desc.m_DilationX,
1452 &desc.m_PadLeft,
1453 &desc.m_PadRight,
1454 isUpper);
telsoa01c577f2c2018-08-31 09:22:23 +01001455 }
1456 }
1457 else
1458 {
1459 desc.m_PadTop = pads[0];
1460 desc.m_PadLeft = pads[1];
1461 desc.m_PadBottom = pads[2];
1462 desc.m_PadRight = pads[3];
1463 }
1464
1465 uint32_t group = ReadOptionalNodeUint32Attribute(node, "group", 1);
1466 if(group > 1)
1467 {
1468 if (group > inputInfo.GetShape()[1])
1469 {
1470 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001471 fmt::format("Error parsing Convolution node: {}. "
1472 "The 'group'={} parameter cannot be larger than the "
1473 "channel of the input shape={} (in NCHW format). {}",
1474 node.name(),
1475 group,
1476 inputInfo.GetShape()[1],
1477 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001478 }
1479 else if (group == inputInfo.GetShape()[1])
1480 {
1481 // we use a depthwise convolution here, because the number of groups equals to the
1482 // input channels
1483 AddConvLayerWithDepthwiseConv(node, desc);
1484 return;
1485 }
1486 else
1487 {
1488 // TODO: split the input by channels into channels/groups separate convolutions
Jim Flynne242f2d2019-05-22 14:24:13 +01001489 // and concatenate the results afterwards
James Ward58dec6b2020-09-11 17:32:44 +01001490 throw ParseException(fmt::format("Error parsing Convolution node: {}. "
1491 "The 'group'={} parameter should be 1 or be equal to the "
1492 "channel of the input shape={} (in NCHW format). {}",
1493 node.name(),
1494 group,
1495 inputInfo.GetShape()[1],
1496 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001497 }
1498 }
1499
1500 armnn::IConnectableLayer* layer;
1501 auto weightTensor = CreateConstTensor(node.input(1));
1502
1503 if (node.input_size() == 3)
1504 {
1505 if(!m_TensorsInfo[node.input(2)].isConstant())
1506 {
James Ward58dec6b2020-09-11 17:32:44 +01001507 throw ParseException(fmt::format("Bias '{}' should be constant in Conv layer '{}' {}",
1508 node.input(2),
1509 node.name(),
1510 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001511 }
1512 desc.m_BiasEnabled = true;
1513 auto biasTensor = CreateConstTensor(node.input(2));
1514 layer = m_Network->AddConvolution2dLayer(desc,
1515 weightTensor.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001516 Optional<ConstTensor>(biasTensor.first),
telsoa01c577f2c2018-08-31 09:22:23 +01001517 node.name().c_str());
1518 }
1519 else
1520 {
1521 layer = m_Network->AddConvolution2dLayer(desc,
1522 weightTensor.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001523 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001524 node.name().c_str());
1525 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001526 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001527
1528 auto outputInfo = ComputeOutputInfo({ node.output(0) }, layer,
1529 { m_TensorsInfo[node.input(0)].m_info->GetShape(),
1530 m_TensorsInfo[node.input(1)].m_info->GetShape() });
1531 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1532
1533 // register the input connection slots for the layer, connections are made after all layers have been created
1534 // only the tensors for the inputs are relevant, exclude the const tensors
1535 RegisterInputSlots(layer, {node.input(0)});
1536
1537 // register the output connection slots for the layer, connections are made after all layers have been created
1538 RegisterOutputSlots(layer, {node.output(0)});
1539}
1540
Kevin Mayef33cb12021-01-29 14:24:57 +00001541void OnnxParserImpl::ParseFlatten(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001542{
1543 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 1);
1544 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1545
1546 CHECK_VALID_DATATYPE(node.name(), node.input(0),
1547 m_TensorsInfo[node.input(0)].m_dtype,
1548 onnx::TensorProto::FLOAT);
1549
1550 int64_t axis = ReadOptionalNodeInt64Attribute(node, "axis", 1);
1551 TensorShape inputShape = m_TensorsInfo[node.input(0)].m_info->GetShape();
1552
1553 /// Negative axis conversion
1554 if (axis < 0)
1555 {
1556 axis += inputShape.GetNumDimensions();
1557 }
1558
1559 /// Check Axis is within dimensions
1560 if (axis < 0 || axis >= inputShape.GetNumDimensions())
1561 {
James Ward58dec6b2020-09-11 17:32:44 +01001562 throw ParseException(fmt::format("Axis '{}' invalid. Tensor has '{}' dimensions in FlattenLayer '{}'",
1563 axis, inputShape.GetNumDimensions(), node.name()));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001564 }
1565
1566 /// If axis chosen is 0 dimension1 will always be 1 in output , default dimension2 to 1 because 0 is invalid
1567 uint dimension1{1};
1568 uint dimension2{1};
1569 uint i{0};
1570
1571 /// dimension1 = (d_0 * d_1 ... d_(axis-1))
1572 for (i = 0; i < axis; i++){
1573 dimension1 *= inputShape[i];
1574 }
1575
1576 /// dimension2 = (d_axis * d_(axis+1) ... d_n)
1577 for (i = static_cast<uint>(axis); i < inputShape.GetNumDimensions(); i++){
1578 dimension2 *= inputShape[i];
1579 }
1580
1581 TensorShape outputShape{dimension1, dimension2};
1582
1583 auto outInfo = ComputeReshapeInfo(outputShape, inputShape, node.output(0));
1584 m_TensorsInfo[node.output(0)].m_info = std::make_unique<TensorInfo>(outInfo);
1585 CreateReshapeLayer(node.input(0), node.output(0), node.name());
1586}
1587
Kevin Mayef33cb12021-01-29 14:24:57 +00001588void OnnxParserImpl::ParseGlobalAveragePool(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001589{
1590 Pooling2dDescriptor desc = Pooling2dDescriptor();
1591 desc.m_PoolType = PoolingAlgorithm::Average;
1592
1593 //kernel size is the same as input
1594 TensorShape inputShape = m_TensorsInfo[node.input(0)].m_info->GetShape();
1595 desc.m_PoolWidth = inputShape[3];
1596 desc.m_PoolHeight = inputShape[2];
1597
1598 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, node.name().c_str());
1599 ARMNN_ASSERT(layer != nullptr);
1600
1601 auto outputInfo = ComputeOutputInfo({node.output(0)}, layer, {inputShape});
1602 layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]);
1603
1604 // register the input connection slots for the layer, connections are made after all layers have been created
1605 // only the tensors for the inputs are relevant, exclude the const tensors
1606 RegisterInputSlots(layer, {node.input(0)});
1607
1608 // register the output connection slots for the layer, connections are made after all layers have been created
1609 RegisterOutputSlots(layer, {node.output(0)});
1610}
1611
Kevin Mayef33cb12021-01-29 14:24:57 +00001612void OnnxParserImpl::ParseMaxPool(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001613{
1614 Pooling2dDescriptor desc;
1615 desc.m_PoolType = PoolingAlgorithm::Max;
1616 desc.m_PaddingMethod = PaddingMethod::Exclude;
1617 AddPoolingLayer(node, desc);
1618}
1619
Kevin Mayef33cb12021-01-29 14:24:57 +00001620void OnnxParserImpl::ParseReshape(const onnx::NodeProto& node)
Ryan OSheaed27ee72020-04-22 16:37:29 +01001621{
1622 CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 2);
1623 CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1);
1624
1625 CHECK_VALID_DATATYPE(node.name(), node.input(0),
1626 m_TensorsInfo[node.input(0)].m_dtype,
1627 onnx::TensorProto::FLOAT); //input
1628 CHECK_VALID_DATATYPE(node.name(), node.input(1),
1629 m_TensorsInfo[node.input(1)].m_dtype,
1630 onnx::TensorProto::INT64); //shape
1631
1632 if(!m_TensorsInfo[node.input(1)].isConstant())
1633 {
James Ward58dec6b2020-09-11 17:32:44 +01001634 throw ParseException(fmt::format("Shape '{}' should be constant in Reshape layer '{}' {}",
1635 node.input(1),
1636 node.name(),
1637 CHECK_LOCATION().AsString()));
Ryan OSheaed27ee72020-04-22 16:37:29 +01001638 }
1639
1640 if(m_TensorsInfo[node.input(0)].isConstant())
1641 {
1642 //make a new cst tensor -> move the data to the output tensor (the shape is already good in the output tensor)
1643 if(m_TensorsInfo.count(node.output(0)) == 0)
1644 {
1645 m_TensorsInfo[node.output(0)] = OnnxTensor();
1646 }
1647 m_TensorsInfo[node.output(0)].m_tensor =
1648 std::make_unique<onnx::TensorProto>(*m_TensorsInfo[node.input(0)].m_tensor);
1649 }
1650 else
1651 {
1652 TensorShape inputShape = m_TensorsInfo[node.input(0)].m_info->GetShape();
1653
1654 if(m_TensorsInfo.count(node.output(0)) == 0 || m_TensorsInfo[node.output(0)].m_info == nullptr)
1655 {
1656 uint64_t dims = static_cast<uint64_t>(m_TensorsInfo[node.input(1)].m_tensor->int64_data_size());
1657 TensorShape targetShape{static_cast<unsigned int>(dims), 1};
1658
1659 for(uint i = 0; i < dims; i++)
1660 {
1661 int val = CHECKED_INT32(m_TensorsInfo[node.input(1)].m_tensor->int64_data(static_cast<int>(i)));
1662 targetShape[i]= static_cast<unsigned int>(val);
1663 }
1664
1665 auto outInfo = ComputeReshapeInfo(targetShape, inputShape, node.output(0));
1666 m_TensorsInfo[node.output(0)].m_info = std::make_unique<TensorInfo>(outInfo);
1667 }
1668
1669 CreateReshapeLayer(node.input(0), node.output(0), node.name());
1670 }
1671}
1672
Kevin Mayef33cb12021-01-29 14:24:57 +00001673void OnnxParserImpl::PrependForBroadcast(const std::string& outputName,
1674 const std::string& input0,
1675 const std::string& input1)
telsoa01c577f2c2018-08-31 09:22:23 +01001676{
1677 //input0 should be reshaped to have same number of dim as input1
1678 TensorInfo outputTensorInfo = TensorInfo(*m_TensorsInfo[input0].m_info);
1679
1680 TensorShape input0Shape = m_TensorsInfo[input0].m_info->GetShape();
1681 TensorShape input1Shape = m_TensorsInfo[input1].m_info->GetShape();
1682
1683 uint32_t diff = input1Shape.GetNumDimensions() - input0Shape.GetNumDimensions();
1684 std::vector<uint32_t> newShape;
1685 while(diff > 0)
1686 {
1687 newShape.push_back(1);
1688 diff--;
1689 }
1690 for (uint dim = 0; dim < input0Shape.GetNumDimensions(); ++dim)
1691 {
1692 newShape.push_back(input0Shape[dim]);
1693 }
1694 outputTensorInfo.SetShape(TensorShape(static_cast<unsigned int>(newShape.size()), newShape.data()));
1695
1696 //add the new tensor to m_TensorsInfo
1697 m_TensorsInfo[outputName] = OnnxTensor();
1698 m_TensorsInfo[outputName].m_info = std::make_unique<TensorInfo>(outputTensorInfo);
1699
1700 //add reshape layer if the parent was not constant...
1701 if( ! m_TensorsInfo[input0].isConstant())
1702 {
James Ward58dec6b2020-09-11 17:32:44 +01001703 CreateReshapeLayer(input0, outputName, fmt::format("Add:reshapeOf{}", input0));
telsoa01c577f2c2018-08-31 09:22:23 +01001704 }
1705 else //make it constant and it will be create in Add
1706 {
1707 m_TensorsInfo[outputName].m_tensor = std::make_unique<onnx::TensorProto>(*m_TensorsInfo[input0].m_tensor);
1708
1709 }
1710}
1711
Kevin Mayef33cb12021-01-29 14:24:57 +00001712void OnnxParserImpl::SetupInputLayers()
telsoa01c577f2c2018-08-31 09:22:23 +01001713{
1714 //Find user input and add their layers
1715 for(int inputIndex = 0; inputIndex < m_Graph->input_size(); ++inputIndex)
1716 {
1717 auto input = m_Graph->input(inputIndex);
1718 if (! m_TensorsInfo[input.name()].isConstant())
1719 {
1720 IConnectableLayer* layer =
1721 m_Network->AddInputLayer(static_cast<armnn::LayerBindingId>(inputIndex), input.name().c_str());
1722 auto tensorInfo = ToTensorInfo(input);
1723 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
1724
1725 RegisterOutputSlots(layer,{ input.name() });
1726 }
1727 }
1728}
1729
Kevin Mayef33cb12021-01-29 14:24:57 +00001730void OnnxParserImpl::SetupOutputLayers()
telsoa01c577f2c2018-08-31 09:22:23 +01001731{
1732 if(m_Graph->output_size() == 0)
1733 {
James Ward58dec6b2020-09-11 17:32:44 +01001734 throw ParseException(fmt::format("The given model does not have any outputs {}", CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001735 }
1736
1737 for(int outputIndex = 0; outputIndex < m_Graph->output_size(); ++outputIndex)
1738 {
1739 IConnectableLayer* layer =
1740 m_Network->AddOutputLayer(static_cast<armnn::LayerBindingId>(outputIndex),
1741 m_Graph->output(outputIndex).name().c_str());
1742
1743 RegisterInputSlots(layer, { m_Graph->output(outputIndex).name() });
1744 }
1745}
1746
Kevin Mayef33cb12021-01-29 14:24:57 +00001747void OnnxParserImpl::RegisterInputSlots(IConnectableLayer* layer, const std::vector<std::string>& tensorIds)
telsoa01c577f2c2018-08-31 09:22:23 +01001748{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001749 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001750 if (tensorIds.size() != layer->GetNumInputSlots())
1751 {
1752 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001753 fmt::format("The number of tensor inputs ({}) does not match the number expected ({}) {}",
1754 tensorIds.size(),
1755 layer->GetNumInputSlots(),
1756 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001757 }
1758 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex)
1759 {
1760 std::string tensorId = tensorIds[slotIndex];
1761 armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex));
1762
1763 auto it = m_TensorConnections.find(tensorId);
1764
1765 if (it == m_TensorConnections.end())
1766 {
1767 //First time seing this tensor, we need to map it
1768 m_TensorConnections[tensorId] = TensorSlots();
1769 }
1770 m_TensorConnections[tensorId].inputSlots.push_back(slot);
1771 }
1772}
1773
Kevin Mayef33cb12021-01-29 14:24:57 +00001774void OnnxParserImpl::RegisterOutputSlots(IConnectableLayer* layer, const std::vector<std::string>& tensorIds)
telsoa01c577f2c2018-08-31 09:22:23 +01001775{
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001776 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001777 if (tensorIds.size() != layer->GetNumOutputSlots())
1778 {
1779 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001780 fmt::format("The number of tensor outputs ({}) does not match the number expected ({}) {} ",
1781 tensorIds.size(),
1782 layer->GetNumOutputSlots(),
1783 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001784 }
1785
1786 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
1787 {
1788 std::string tensorId = tensorIds[slotIndex];
1789 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
1790
1791 auto it = m_TensorConnections.find(tensorId);
1792
1793 if (it == m_TensorConnections.end())
1794 {
1795 //First time seing this tensor, we need to map it
1796 m_TensorConnections[tensorId] = TensorSlots();
1797 }
1798
Ryan OShea337c17f2020-02-21 12:33:17 +00001799 TensorSlots& tensorSlots = m_TensorConnections[tensorId];
telsoa01c577f2c2018-08-31 09:22:23 +01001800
1801 // assuming there is only one producer for that tensor
1802 if (tensorSlots.outputSlot != nullptr)
1803 {
James Ward58dec6b2020-09-11 17:32:44 +01001804 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
1805 "tensor:{} {}",
1806 tensorId,
1807 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001808 }
1809 tensorSlots.outputSlot = slot;
1810 }
1811}
1812
Kevin Mayef33cb12021-01-29 14:24:57 +00001813BindingPointInfo OnnxParserImpl::GetNetworkInputBindingInfo(const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01001814{
1815 for(int i = 0; i < m_Graph->input_size(); ++i)
1816 {
1817 auto input = m_Graph->input(i);
1818 if(input.name() == name)
1819 {
1820 return std::make_pair(static_cast<armnn::LayerBindingId>(i), ToTensorInfo(input));
1821 }
1822 }
James Ward58dec6b2020-09-11 17:32:44 +01001823 throw InvalidArgumentException(fmt::format("The input layer '{}' does not exist {}",
1824 name, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001825}
1826
Kevin Mayef33cb12021-01-29 14:24:57 +00001827BindingPointInfo OnnxParserImpl::GetNetworkOutputBindingInfo(const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01001828{
1829 for(int i = 0; i < m_Graph->output_size(); ++i)
1830 {
1831 auto output = m_Graph->output(i);
1832 if(output.name() == name)
1833 {
1834 return std::make_pair(static_cast<armnn::LayerBindingId>(i), ToTensorInfo(output));
1835 }
1836 }
James Ward58dec6b2020-09-11 17:32:44 +01001837 throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}",
1838 name, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001839}
1840
Kevin Mayef33cb12021-01-29 14:24:57 +00001841std::vector<std::string> OnnxParserImpl::GetInputs(ModelPtr& model)
telsoa01c577f2c2018-08-31 09:22:23 +01001842{
1843 if(model == nullptr) {
James Ward58dec6b2020-09-11 17:32:44 +01001844 throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
1845 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001846 }
1847
1848 std::vector<std::string> inputNames;
1849 std::map<std::string, bool> isConstant;
1850 for(auto tensor : model->graph().initializer())
1851 {
1852 isConstant[tensor.name()] = true;
1853 }
1854 for(auto input : model->graph().input())
1855 {
1856 auto it = isConstant.find(input.name());
1857 if(it == isConstant.end())
1858 {
1859 inputNames.push_back(input.name());
1860 }
1861 }
1862 return inputNames;
1863}
1864
Kevin Mayef33cb12021-01-29 14:24:57 +00001865std::vector<std::string> OnnxParserImpl::GetOutputs(ModelPtr& model)
telsoa01c577f2c2018-08-31 09:22:23 +01001866{
1867 if(model == nullptr) {
James Ward58dec6b2020-09-11 17:32:44 +01001868 throw InvalidArgumentException(fmt::format("The given model cannot be null {}",
1869 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001870 }
1871
1872 std::vector<std::string> outputNames;
1873 for(auto output : model->graph().output())
1874 {
1875 outputNames.push_back(output.name());
1876 }
1877 return outputNames;
1878}
1879
Matthew Sloyanac001ee2021-02-03 10:43:04 +00001880const std::string OnnxParserImpl::GetVersion()
1881{
1882 return ONNX_PARSER_VERSION;
1883}
1884
telsoa01c577f2c2018-08-31 09:22:23 +01001885} // namespace armnnOnnxParser