telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
| 2 | // Copyright © 2017 Arm Ltd. All rights reserved. |
David Beck | ecb56cd | 2018-09-05 12:52:57 +0100 | [diff] [blame] | 3 | // SPDX-License-Identifier: MIT |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4 | // |
| 5 | #include "OnnxParser.hpp" |
| 6 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 7 | #include "armnnOnnxParser/Version.hpp" |
| 8 | |
Matthew Bentham | 39ef3e5 | 2020-01-20 10:09:09 +0000 | [diff] [blame] | 9 | #include <armnn/Descriptors.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 10 | #include <armnn/utility/Assert.hpp> |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 11 | #include <armnn/utility/NumericCast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include <VerificationHelpers.hpp> |
| 13 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 14 | #include <fmt/format.h> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 15 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | #include <google/protobuf/text_format.h> |
| 17 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
| 18 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 19 | #include <iostream> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | #include <numeric> |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 21 | #include <armnnUtils/Permute.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 22 | |
| 23 | using namespace armnn; |
| 24 | |
| 25 | namespace armnnOnnxParser |
| 26 | { |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 27 | |
| 28 | IOnnxParser::IOnnxParser() : pOnnxParserImpl(new OnnxParserImpl()) {} |
| 29 | |
| 30 | IOnnxParser::~IOnnxParser() = default; |
| 31 | |
| 32 | IOnnxParser* IOnnxParser::CreateRaw() |
| 33 | { |
| 34 | return new IOnnxParser(); |
| 35 | } |
| 36 | |
| 37 | IOnnxParserPtr IOnnxParser::Create() |
| 38 | { |
| 39 | return IOnnxParserPtr(CreateRaw(), &IOnnxParser::Destroy); |
| 40 | } |
| 41 | |
| 42 | void IOnnxParser::Destroy(IOnnxParser* parser) |
| 43 | { |
| 44 | delete parser; |
| 45 | } |
| 46 | |
| 47 | armnn::INetworkPtr IOnnxParser::CreateNetworkFromBinaryFile(const char* graphFile) |
| 48 | { |
| 49 | return pOnnxParserImpl->CreateNetworkFromBinaryFile(graphFile); |
| 50 | } |
| 51 | |
| 52 | armnn::INetworkPtr IOnnxParser::CreateNetworkFromTextFile(const char* graphFile) |
| 53 | { |
| 54 | return pOnnxParserImpl->CreateNetworkFromTextFile(graphFile); |
| 55 | } |
| 56 | |
| 57 | armnn::INetworkPtr IOnnxParser::CreateNetworkFromString(const std::string& protoText) |
| 58 | { |
| 59 | return pOnnxParserImpl->CreateNetworkFromString(protoText); |
| 60 | } |
| 61 | |
| 62 | BindingPointInfo IOnnxParser::GetNetworkInputBindingInfo(const std::string& name) const |
| 63 | { |
| 64 | return pOnnxParserImpl->GetNetworkInputBindingInfo(name); |
| 65 | } |
| 66 | |
| 67 | BindingPointInfo IOnnxParser::GetNetworkOutputBindingInfo(const std::string& name) const |
| 68 | { |
| 69 | return pOnnxParserImpl->GetNetworkOutputBindingInfo(name); |
| 70 | } |
| 71 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 72 | namespace |
| 73 | { |
| 74 | void 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 87 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
| 96 | #define CHECK_VALID_DATATYPE(NODE, TENSOR, ACTUAL, ...) \ |
| 97 | CheckValidDataType({__VA_ARGS__}, ACTUAL, #__VA_ARGS__, NODE, TENSOR, CHECK_LOCATION()) |
| 98 | |
| 99 | using StrTypeListPair = std::pair<const char*, std::initializer_list<onnx::TensorProto::DataType>>; |
| 100 | #define STR_LIST(...) StrTypeListPair(#__VA_ARGS__, {__VA_ARGS__}) |
| 101 | |
| 102 | template <typename Callable> |
| 103 | void 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 120 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 127 | } |
| 128 | break; |
| 129 | } |
| 130 | ++attriNum; |
| 131 | } |
| 132 | if (attriNum == node.attribute_size()) |
| 133 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 134 | throw ParseException(fmt::format("Could not find required attribute {} in node {} {}", |
| 135 | attribName, node.name(), CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
| 139 | template <typename Callable> |
| 140 | void 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 156 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 169 | int64_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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 182 | std::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 | |
| 197 | uint32_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 | |
| 210 | std::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 | |
| 226 | float 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 | |
| 239 | std::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 Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 250 | armnn::TensorInfo ToTensorInfo(const std::string& name, std::vector<unsigned int>& shape, int data_type) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 251 | { |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 252 | DataType type; |
Tee Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 253 | switch(data_type) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 254 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 269 | 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() )); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 274 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 275 | } |
Tee Jung | caf2bdd | 2019-11-13 07:23:14 +0000 | [diff] [blame] | 276 | |
| 277 | // To avoid crashes by trivial tensors |
| 278 | if (shape.empty()) |
| 279 | { |
| 280 | return TensorInfo(TensorShape(), type); |
| 281 | } |
| 282 | |
Tee Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 283 | return TensorInfo(TensorShape(static_cast<unsigned int>(shape.size()), shape.data()), type); |
| 284 | } |
| 285 | |
| 286 | armnn::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 OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 295 | if (shapeDims.empty()) |
| 296 | { |
| 297 | shapeDims.push_back(1); |
| 298 | } |
| 299 | |
Tee Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 300 | return ToTensorInfo(info.name(), shapeDims, info.type().tensor_type().elem_type()); |
| 301 | } |
| 302 | |
| 303 | armnn::TensorInfo ToTensorInfo(const onnx::TensorProto& tensor) |
| 304 | { |
| 305 | std::vector<unsigned int> shapeDims; |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 306 | |
Tee Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 307 | for (auto dim: tensor.dims()) |
| 308 | { |
| 309 | shapeDims.push_back(CHECKED_NON_NEGATIVE(CHECKED_INT32(dim))); |
| 310 | } |
| 311 | |
Ryan OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 312 | if (shapeDims.empty()) |
| 313 | { |
| 314 | shapeDims.push_back(1); |
| 315 | } |
| 316 | |
Tee Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 317 | return ToTensorInfo(tensor.name(), shapeDims, tensor.data_type()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | std::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 Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 338 | void 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) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 345 | { |
| 346 | uint32_t outputSize = (inputSize + stride - 1) / stride; |
Sadik Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 347 | uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); |
| 348 | uint32_t temp = (outputSize - 1) * stride + dilatedSize; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 349 | *paddingFront = (temp - inputSize) / 2; |
| 350 | *paddingBack = *paddingFront; |
| 351 | if((temp - inputSize) % 2 == 1) |
| 352 | { |
| 353 | if (isUpper) |
| 354 | { |
Sadik Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 355 | *paddingBack += 1; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 356 | } |
| 357 | else |
| 358 | { |
Sadik Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 359 | *paddingFront += 1; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 364 | TensorInfo ComputeReshapeInfo(const TensorShape& targetShapeTensor, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 365 | const TensorShape& inShape, |
| 366 | const std::string& outName) |
| 367 | { |
| 368 | std::vector<int> targetDims; |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 369 | for(uint i = 0; i < targetShapeTensor.GetNumDimensions(); ++i) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 370 | { |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 371 | int val = CHECKED_INT32(targetShapeTensor[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 372 | 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 396 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 402 | } |
| 403 | |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 404 | auto targetNumElements = armnn::numeric_cast<unsigned int>(std::accumulate(targetDims.begin(), targetDims.end(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 405 | -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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 415 | const 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}, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 430 | }; |
| 431 | |
| 432 | template<typename TypePair, typename Location> |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 433 | void OnnxParserImpl::ValidateInputs(const onnx::NodeProto& node, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 434 | 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 449 | OnnxParserImpl::ValidateInputs(NODE, \ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 450 | VALID_INPUTS, \ |
| 451 | CHECK_LOCATION()) |
| 452 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 453 | std::vector<TensorInfo> OnnxParserImpl::ComputeOutputInfo(std::vector<std::string> outNames, |
| 454 | const IConnectableLayer* layer, |
| 455 | std::vector<TensorShape> inputShapes) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 456 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 457 | ARMNN_ASSERT(! outNames.empty()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 458 | 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 470 | ARMNN_ASSERT(inferredShapes.size() == outNames.size()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 471 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 485 | OnnxParserImpl::OnnxParserImpl() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 486 | : m_Network(nullptr, nullptr) |
| 487 | { |
| 488 | } |
| 489 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 490 | void OnnxParserImpl::ResetParser() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 491 | { |
| 492 | m_Network = armnn::INetworkPtr(nullptr, nullptr); |
| 493 | m_Graph = nullptr; |
| 494 | } |
| 495 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 496 | void OnnxParserImpl::Cleanup() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 497 | { |
| 498 | m_TensorConnections.clear(); |
| 499 | m_TensorsInfo.clear(); |
| 500 | m_OutputsMap.clear(); |
| 501 | m_OutputsFusedAndUsed.clear(); |
| 502 | } |
| 503 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 504 | template<typename T> |
| 505 | std::pair<armnn::ConstTensor, std::unique_ptr<T[]>> |
| 506 | CreateConstTensorImpl(const T* bufferPtr, |
| 507 | armnn::TensorInfo& tensorInfo, |
| 508 | const armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 509 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 510 | 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 | |
| 528 | std::pair<ConstTensor, std::unique_ptr<float[]>> |
| 529 | OnnxParserImpl::CreateConstTensor(const std::string name, |
| 530 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
| 531 | { |
| 532 | TensorInfo tensorInfo = *m_TensorsInfo[name].m_info; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 533 | onnx::TensorProto onnxTensor = *m_TensorsInfo[name].m_tensor; |
| 534 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 535 | // 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 543 | auto srcData = onnxTensor.float_data().data(); |
Pablo Tello | 3dcc1c6 | 2019-04-24 14:20:21 +0100 | [diff] [blame] | 544 | // Copy the value list entries into the destination |
| 545 | if (!onnxTensor.has_raw_data()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 546 | { |
Pablo Tello | 3dcc1c6 | 2019-04-24 14:20:21 +0100 | [diff] [blame] | 547 | if(tensorInfo.GetNumElements() != static_cast<uint>(onnxTensor.float_data_size())) |
| 548 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 549 | 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 Tello | 3dcc1c6 | 2019-04-24 14:20:21 +0100 | [diff] [blame] | 556 | } |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 557 | return CreateConstTensorImpl<float>(srcData, tensorInfo, permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 558 | } |
Pablo Tello | 3dcc1c6 | 2019-04-24 14:20:21 +0100 | [diff] [blame] | 559 | else |
| 560 | { |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 561 | return CreateConstTensorImpl<float>(reinterpret_cast<const float*>(onnxTensor.raw_data().c_str()), |
| 562 | tensorInfo, |
| 563 | permutationVector); |
Pablo Tello | 3dcc1c6 | 2019-04-24 14:20:21 +0100 | [diff] [blame] | 564 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 567 | ModelPtr OnnxParserImpl::LoadModelFromTextFile(const char* graphFile) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 568 | { |
| 569 | FILE* fd = fopen(graphFile, "r"); |
| 570 | |
| 571 | if (fd == nullptr) |
| 572 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 573 | throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 574 | } |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 587 | throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 588 | } |
| 589 | return modelProto; |
| 590 | } |
| 591 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 592 | INetworkPtr OnnxParserImpl::CreateNetworkFromTextFile(const char* graphFile) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 593 | { |
| 594 | ResetParser(); |
| 595 | ModelPtr modelProto = LoadModelFromTextFile(graphFile); |
| 596 | return CreateNetworkFromModel(*modelProto); |
| 597 | } |
| 598 | |
| 599 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 600 | ModelPtr OnnxParserImpl::LoadModelFromBinaryFile(const char* graphFile) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 601 | { |
| 602 | FILE* fd = fopen(graphFile, "rb"); |
| 603 | |
| 604 | if (fd == nullptr) |
| 605 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 606 | throw FileNotFoundException(fmt::format("Invalid (null) filename {}", CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 607 | } |
| 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 Raj | e518153 | 2020-10-09 14:52:25 +0100 | [diff] [blame] | 614 | codedStream.SetTotalBytesLimit(INT_MAX); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 615 | 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 622 | throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 623 | } |
| 624 | return modelProto; |
| 625 | |
| 626 | } |
| 627 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 628 | INetworkPtr OnnxParserImpl::CreateNetworkFromBinaryFile(const char* graphFile) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 629 | { |
| 630 | ResetParser(); |
| 631 | ModelPtr modelProto = LoadModelFromBinaryFile(graphFile); |
| 632 | return CreateNetworkFromModel(*modelProto); |
| 633 | } |
| 634 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 635 | ModelPtr OnnxParserImpl::LoadModelFromString(const std::string& protoText) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 636 | { |
| 637 | if (protoText == "") |
| 638 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 639 | throw InvalidArgumentException(fmt::format("Invalid (empty) string for model parameter {}", |
| 640 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 641 | } |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 649 | throw ParseException(fmt::format("{} {}", error.str(), CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 650 | } |
| 651 | return modelProto; |
| 652 | } |
| 653 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 654 | INetworkPtr OnnxParserImpl::CreateNetworkFromString(const std::string& protoText) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 655 | { |
| 656 | ResetParser(); |
| 657 | ModelPtr modelProto = LoadModelFromString(protoText); |
| 658 | return CreateNetworkFromModel(*modelProto); |
| 659 | } |
| 660 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 661 | INetworkPtr OnnxParserImpl::CreateNetworkFromModel(onnx::ModelProto& model) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 662 | { |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 678 | void OnnxParserImpl::LoadGraph() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 679 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 680 | ARMNN_ASSERT(m_Graph.get() != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 681 | |
| 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 Jung | fcf6fd5 | 2019-11-01 05:27:28 +0000 | [diff] [blame] | 690 | 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()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 693 | } |
| 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 OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 708 | if (operation == "MatMul" ) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 709 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 731 | throw ParseException(fmt::format("Unsupported operation {} for node '{}' {}", |
| 732 | operation, |
| 733 | node.name(), |
| 734 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 735 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 752 | void OnnxParserImpl::SetupInfo(const google::protobuf::RepeatedPtrField<onnx::ValueInfoProto >* list) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 753 | { |
| 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 Martincigh | e355dc2 | 2018-12-10 13:45:27 +0000 | [diff] [blame] | 758 | m_TensorsInfo[tensor.name()].m_dtype = |
| 759 | static_cast<onnx::TensorProto::DataType>(tensor.type().tensor_type().elem_type()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 760 | } |
| 761 | } |
| 762 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 763 | void OnnxParserImpl::DetectFullyConnected() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 764 | { |
| 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 | |
| 820 | template<typename Location> |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 821 | void OnnxParserImpl::GetInputAndParam(const onnx::NodeProto& node, |
| 822 | std::string* inputName, |
| 823 | std::string* constName, |
| 824 | const Location& location) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 825 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 837 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 842 | } |
| 843 | if(constName) |
| 844 | { |
| 845 | *constName = node.input(cstIndex); |
| 846 | } |
| 847 | if(inputName) |
| 848 | { |
| 849 | *inputName = node.input(!cstIndex); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | template<typename Location> |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 854 | void OnnxParserImpl::To1DTensor(const std::string& name, const Location& location) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 855 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 862 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 866 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 873 | void OnnxParserImpl::AddConvLayerWithDepthwiseConv(const onnx::NodeProto& node, const Convolution2dDescriptor& convDesc) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 874 | { |
| 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 Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 887 | |
| 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 OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 891 | |
| 892 | if (node.input_size() == 3) |
| 893 | { |
| 894 | if(!m_TensorsInfo[node.input(2)].isConstant()) |
| 895 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 896 | throw ParseException(fmt::format("Bias '{}' should be constant in Conv layer '{}' {}", |
| 897 | node.input(2), |
| 898 | node.name(), |
| 899 | CHECK_LOCATION().AsString())); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 900 | } |
| 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 Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 919 | weightTensor.first.GetInfo().GetShape() }); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 920 | |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 931 | void OnnxParserImpl::AddFullyConnected(const onnx::NodeProto& matmulNode, const onnx::NodeProto* addNode) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 932 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 964 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 974 | } |
| 975 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 976 | CreateConstTensor(weightName).first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 977 | Optional<ConstTensor>(CreateConstTensor(biasName).first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 978 | matmulNode.name().c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 979 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 980 | |
| 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 Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 992 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 993 | CreateConstTensor(weightName).first, |
| 994 | EmptyOptional(), |
| 995 | matmulNode.name().c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 996 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 997 | |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1008 | void OnnxParserImpl::AddPoolingLayer(const onnx::NodeProto& node, Pooling2dDescriptor& desc) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1009 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1053 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1058 | } |
| 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 Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 1062 | 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); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1076 | } |
| 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1087 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1088 | |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1100 | std::pair<std::string, std::string> OnnxParserImpl::AddPrepareBroadcast(const std::string& input0, |
| 1101 | const std::string& input1) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1102 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1110 | auto outputName = fmt::format("reshape_output_{}", input1); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1111 | PrependForBroadcast(outputName, input1, input0); |
| 1112 | inputs.second = outputName; |
| 1113 | } |
| 1114 | else if(input0Shape.GetNumDimensions() < input1Shape.GetNumDimensions()) |
| 1115 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1116 | auto outputName = fmt::format("reshape_output_{}", input0); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1117 | PrependForBroadcast(outputName, input0, input1); |
| 1118 | inputs.first = outputName; |
| 1119 | } |
| 1120 | return inputs; |
| 1121 | } |
| 1122 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1123 | void OnnxParserImpl::CreateConstantLayer(const std::string& tensorName, const std::string& layerName) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1124 | { |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1132 | void OnnxParserImpl::CreateReshapeLayer(const std::string& inputName, |
| 1133 | const std::string& outputName, |
| 1134 | const std::string& layerName) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1135 | { |
| 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 Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1141 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1142 | 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1152 | void OnnxParserImpl::ParseActivation(const onnx::NodeProto& node, const armnn::ActivationFunction func) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1153 | { |
Finn Williams | 7ee5d2c | 2020-03-27 11:11:50 +0000 | [diff] [blame] | 1154 | CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 1, 3); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1155 | 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 Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 1160 | desc.m_Function = func; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1161 | |
Finn Williams | 7ee5d2c | 2020-03-27 11:11:50 +0000 | [diff] [blame] | 1162 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1168 | IConnectableLayer* const layer = m_Network->AddActivationLayer(desc, node.name().c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1169 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1170 | |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1182 | void OnnxParserImpl::ParseClip(const onnx::NodeProto& node) |
Finn Williams | 7ee5d2c | 2020-03-27 11:11:50 +0000 | [diff] [blame] | 1183 | { |
| 1184 | ParseActivation(node, ActivationFunction::BoundedReLu); |
| 1185 | } |
| 1186 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1187 | void OnnxParserImpl::ParseSigmoid(const onnx::NodeProto& node) |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 1188 | { |
| 1189 | ParseActivation(node, ActivationFunction::Sigmoid); |
| 1190 | } |
| 1191 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1192 | void OnnxParserImpl::ParseTanh(const onnx::NodeProto& node) |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 1193 | { |
| 1194 | ParseActivation(node, ActivationFunction::TanH); |
| 1195 | } |
| 1196 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1197 | void OnnxParserImpl::ParseRelu(const onnx::NodeProto& node) |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 1198 | { |
| 1199 | ParseActivation(node, ActivationFunction::ReLu); |
| 1200 | } |
| 1201 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1202 | void OnnxParserImpl::ParseLeakyRelu(const onnx::NodeProto& node) |
Tee Jung | 7ff9a60 | 2019-11-01 07:04:42 +0000 | [diff] [blame] | 1203 | { |
| 1204 | ParseActivation(node, ActivationFunction::LeakyReLu); |
| 1205 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1206 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1207 | void OnnxParserImpl::ParseAdd(const onnx::NodeProto& node) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1208 | { |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1209 | CHECK_VALID_SIZE(static_cast<size_t>(node.input_size()), 2); |
| 1210 | CHECK_VALID_SIZE(static_cast<size_t>(node.output_size()), 1); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1211 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1212 | VALID_INPUTS(node, STR_LIST(onnx::TensorProto::FLOAT)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1213 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1214 | // TODO: unify broadcast validation code across layers |
| 1215 | // tracked by: IVGCVSW-1576 |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1216 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1217 | // 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++) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1225 | { |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1226 | unsigned int dim0 = input0.GetShape()[i]; |
| 1227 | unsigned int dim1 = input1.GetShape()[i]; |
| 1228 | if (dim0 != dim1 && dim0 != 1 && dim1 != 1) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1229 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1230 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1240 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1241 | } |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1242 | |
| 1243 | |
| 1244 | IConnectableLayer* layer = m_Network->AddAdditionLayer(node.name().c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1245 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1246 | |
| 1247 | auto outputInfo = ComputeOutputInfo({ node.output(0) }, layer, |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1248 | { m_TensorsInfo[inputs.first].m_info->GetShape(), |
| 1249 | m_TensorsInfo[inputs.second].m_info->GetShape() }); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1250 | layer->GetOutputSlot(0).SetTensorInfo(outputInfo[0]); |
| 1251 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1252 | // register the input connection -> for constant inputs, we need to make a newDim constant layer |
| 1253 | if(m_TensorsInfo[inputs.first].isConstant()) { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1254 | CreateConstantLayer(inputs.first, fmt::format("Add:constant_of_{}", node.input(0))); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1255 | } |
| 1256 | if(m_TensorsInfo[inputs.second].isConstant()) { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1257 | CreateConstantLayer(inputs.second, fmt::format("Add:constant_of_{}", node.input(1))); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1258 | } |
| 1259 | RegisterInputSlots(layer, {inputs.first, inputs.second}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1260 | |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1261 | // register the output connection |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1262 | RegisterOutputSlots(layer, {node.output(0)}); |
| 1263 | } |
| 1264 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1265 | void OnnxParserImpl::ParseAveragePool(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1266 | { |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1278 | void OnnxParserImpl::ParseBatchNormalization(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1279 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1291 | throw ParseException( |
| 1292 | fmt::format("Input tensor '{}' should be constant in BatchNormalization node '{}' {}", |
| 1293 | tensor, |
| 1294 | node.name(), |
| 1295 | CHECK_LOCATION().AsString())); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1296 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1325 | void OnnxParserImpl::ParseConstant(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1326 | { |
| 1327 | CHECK_VALID_SIZE(static_cast<size_t>(node.attribute_size()), 1); |
| 1328 | if (!node.attribute(0).has_t()) |
| 1329 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1330 | throw ParseException(fmt::format("Value not found for Constant node '{}' {}", |
| 1331 | node.name(), |
| 1332 | CHECK_LOCATION().AsString())); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1333 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1348 | void OnnxParserImpl::ParseConv(const onnx::NodeProto& node) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1349 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1357 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | if(!m_TensorsInfo[node.input(1)].isConstant()) |
| 1366 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1367 | throw ParseException( |
| 1368 | fmt::format("Weights '{}' should be constant in Conv layer '{}' {}", |
| 1369 | node.input(1), |
| 1370 | node.name(), |
| 1371 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | auto inputInfo = *m_TensorsInfo[node.input(0)].m_info; |
| 1375 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1376 | 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 Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 1391 | 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 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1398 | 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1417 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1423 | } |
| 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 Armagan | 60bb9d8 | 2021-01-11 15:15:01 +0000 | [diff] [blame] | 1441 | 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); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1455 | } |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1471 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1478 | } |
| 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 Flynn | e242f2d | 2019-05-22 14:24:13 +0100 | [diff] [blame] | 1489 | // and concatenate the results afterwards |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1490 | 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())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1497 | } |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1507 | throw ParseException(fmt::format("Bias '{}' should be constant in Conv layer '{}' {}", |
| 1508 | node.input(2), |
| 1509 | node.name(), |
| 1510 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1511 | } |
| 1512 | desc.m_BiasEnabled = true; |
| 1513 | auto biasTensor = CreateConstTensor(node.input(2)); |
| 1514 | layer = m_Network->AddConvolution2dLayer(desc, |
| 1515 | weightTensor.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1516 | Optional<ConstTensor>(biasTensor.first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1517 | node.name().c_str()); |
| 1518 | } |
| 1519 | else |
| 1520 | { |
| 1521 | layer = m_Network->AddConvolution2dLayer(desc, |
| 1522 | weightTensor.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1523 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1524 | node.name().c_str()); |
| 1525 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1526 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1527 | |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1541 | void OnnxParserImpl::ParseFlatten(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1542 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1562 | throw ParseException(fmt::format("Axis '{}' invalid. Tensor has '{}' dimensions in FlattenLayer '{}'", |
| 1563 | axis, inputShape.GetNumDimensions(), node.name())); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1564 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1588 | void OnnxParserImpl::ParseGlobalAveragePool(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1589 | { |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1612 | void OnnxParserImpl::ParseMaxPool(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1613 | { |
| 1614 | Pooling2dDescriptor desc; |
| 1615 | desc.m_PoolType = PoolingAlgorithm::Max; |
| 1616 | desc.m_PaddingMethod = PaddingMethod::Exclude; |
| 1617 | AddPoolingLayer(node, desc); |
| 1618 | } |
| 1619 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1620 | void OnnxParserImpl::ParseReshape(const onnx::NodeProto& node) |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1621 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1634 | throw ParseException(fmt::format("Shape '{}' should be constant in Reshape layer '{}' {}", |
| 1635 | node.input(1), |
| 1636 | node.name(), |
| 1637 | CHECK_LOCATION().AsString())); |
Ryan OShea | ed27ee7 | 2020-04-22 16:37:29 +0100 | [diff] [blame] | 1638 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1673 | void OnnxParserImpl::PrependForBroadcast(const std::string& outputName, |
| 1674 | const std::string& input0, |
| 1675 | const std::string& input1) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1676 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1703 | CreateReshapeLayer(input0, outputName, fmt::format("Add:reshapeOf{}", input0)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1704 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1712 | void OnnxParserImpl::SetupInputLayers() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1713 | { |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1730 | void OnnxParserImpl::SetupOutputLayers() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1731 | { |
| 1732 | if(m_Graph->output_size() == 0) |
| 1733 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1734 | throw ParseException(fmt::format("The given model does not have any outputs {}", CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1735 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1747 | void OnnxParserImpl::RegisterInputSlots(IConnectableLayer* layer, const std::vector<std::string>& tensorIds) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1748 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1749 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1750 | if (tensorIds.size() != layer->GetNumInputSlots()) |
| 1751 | { |
| 1752 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1753 | fmt::format("The number of tensor inputs ({}) does not match the number expected ({}) {}", |
| 1754 | tensorIds.size(), |
| 1755 | layer->GetNumInputSlots(), |
| 1756 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1757 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1774 | void OnnxParserImpl::RegisterOutputSlots(IConnectableLayer* layer, const std::vector<std::string>& tensorIds) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1775 | { |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1776 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1777 | if (tensorIds.size() != layer->GetNumOutputSlots()) |
| 1778 | { |
| 1779 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1780 | fmt::format("The number of tensor outputs ({}) does not match the number expected ({}) {} ", |
| 1781 | tensorIds.size(), |
| 1782 | layer->GetNumOutputSlots(), |
| 1783 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1784 | } |
| 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 OShea | 337c17f | 2020-02-21 12:33:17 +0000 | [diff] [blame] | 1799 | TensorSlots& tensorSlots = m_TensorConnections[tensorId]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1800 | |
| 1801 | // assuming there is only one producer for that tensor |
| 1802 | if (tensorSlots.outputSlot != nullptr) |
| 1803 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1804 | throw ParseException(fmt::format("Another layer has already registered itself as the producer of " |
| 1805 | "tensor:{} {}", |
| 1806 | tensorId, |
| 1807 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1808 | } |
| 1809 | tensorSlots.outputSlot = slot; |
| 1810 | } |
| 1811 | } |
| 1812 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1813 | BindingPointInfo OnnxParserImpl::GetNetworkInputBindingInfo(const std::string& name) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1814 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1823 | throw InvalidArgumentException(fmt::format("The input layer '{}' does not exist {}", |
| 1824 | name, CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1825 | } |
| 1826 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1827 | BindingPointInfo OnnxParserImpl::GetNetworkOutputBindingInfo(const std::string& name) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1828 | { |
| 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 Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1837 | throw InvalidArgumentException(fmt::format("The output layer '{}' does not exist {}", |
| 1838 | name, CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
Kevin May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1841 | std::vector<std::string> OnnxParserImpl::GetInputs(ModelPtr& model) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1842 | { |
| 1843 | if(model == nullptr) { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1844 | throw InvalidArgumentException(fmt::format("The given model cannot be null {}", |
| 1845 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1846 | } |
| 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 May | ef33cb1 | 2021-01-29 14:24:57 +0000 | [diff] [blame] | 1865 | std::vector<std::string> OnnxParserImpl::GetOutputs(ModelPtr& model) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1866 | { |
| 1867 | if(model == nullptr) { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1868 | throw InvalidArgumentException(fmt::format("The given model cannot be null {}", |
| 1869 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1870 | } |
| 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 Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 1880 | const std::string OnnxParserImpl::GetVersion() |
| 1881 | { |
| 1882 | return ONNX_PARSER_VERSION; |
| 1883 | } |
| 1884 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1885 | } // namespace armnnOnnxParser |