telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1 | // |
Mike Kelly | c5789ca | 2020-07-06 19:24:15 +0100 | [diff] [blame] | 2 | // Copyright © 2017 Arm Ltd and Contributors. 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 | // |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 5 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 6 | #include "TfLiteParser.hpp" |
| 7 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 8 | #include <armnn/BackendOptions.hpp> |
Matthew Bentham | 39ef3e5 | 2020-01-20 10:09:09 +0000 | [diff] [blame] | 9 | #include <armnn/Descriptors.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 10 | #include <armnn/Exceptions.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 11 | #include <armnn/Logging.hpp> |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 12 | #include <armnn/Tensor.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 13 | #include <armnn/TypesUtils.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 14 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 15 | #include <armnn/utility/IgnoreUnused.hpp> |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 16 | #include <armnn/utility/NumericCast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 17 | |
| 18 | // armnnUtils: |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 19 | #include <armnnUtils/Permute.hpp> |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 20 | #include <Filesystem.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 21 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 22 | #include <ParserHelper.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 23 | #include <VerificationHelpers.hpp> |
| 24 | |
| 25 | // The generated code based on the Tf Lite schema: |
| 26 | #include <schema_generated.h> |
| 27 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 28 | #include <flatbuffers/flexbuffers.h> |
| 29 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 30 | #include <boost/format.hpp> |
Aron Virginas-Tar | d4f0fea | 2019-04-09 14:08:06 +0100 | [diff] [blame] | 31 | #include <boost/numeric/conversion/cast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 32 | |
| 33 | #include <fstream> |
| 34 | #include <algorithm> |
| 35 | #include <limits> |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 36 | #include <numeric> |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 37 | #include <sstream> |
| 38 | |
| 39 | #define ARMNN_THROW_PARSE_EXCEPTION(msg) \ |
| 40 | { \ |
| 41 | throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \ |
| 42 | << ": " \ |
| 43 | << CHECK_LOCATION().AsString()).str()); \ |
| 44 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 45 | |
| 46 | using namespace armnn; |
| 47 | using armnn::CheckLocation; |
| 48 | namespace armnnTfLiteParser |
| 49 | { |
| 50 | namespace |
| 51 | { |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 52 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 53 | const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max(); |
| 54 | |
| 55 | void CheckSubgraph(const TfLiteParser::ModelPtr & model, |
| 56 | size_t subgraphIndex, |
| 57 | const CheckLocation & location) |
| 58 | { |
| 59 | if (model.get() == nullptr) |
| 60 | { |
| 61 | throw ParseException( |
| 62 | boost::str( |
| 63 | boost::format("%1% was called with invalid (null) model. " |
| 64 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 65 | "subgraph:%2% at %3%") % |
| 66 | location.m_Function % |
| 67 | subgraphIndex % |
| 68 | location.FileLine())); |
| 69 | } |
| 70 | else if (subgraphIndex >= model->subgraphs.size()) |
| 71 | { |
| 72 | throw ParseException( |
| 73 | boost::str( |
| 74 | boost::format("%1% was called with an invalid subgraph index. " |
| 75 | "subgraph:%2% at %3%") % |
| 76 | location.m_Function % |
| 77 | subgraphIndex % |
| 78 | location.FileLine())); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \ |
| 83 | CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION()) |
| 84 | |
| 85 | void CheckModel(const TfLiteParser::ModelPtr & model, |
| 86 | size_t subgraphIndex, |
| 87 | size_t operatorIndex, |
| 88 | const CheckLocation & location) |
| 89 | { |
| 90 | if (model.get() == nullptr) |
| 91 | { |
| 92 | throw ParseException( |
| 93 | boost::str( |
| 94 | boost::format("%1% was called with invalid (null) model. " |
| 95 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 96 | "subgraph:%2% operator:%3% at %4%") % |
| 97 | location.m_Function % |
| 98 | subgraphIndex % |
| 99 | operatorIndex % |
| 100 | location.FileLine())); |
| 101 | } |
| 102 | else if (subgraphIndex >= model->subgraphs.size()) |
| 103 | { |
| 104 | throw ParseException( |
| 105 | boost::str( |
| 106 | boost::format("%1% was called with an invalid subgraph index. " |
| 107 | "subgraph:%2% operator:%3% at %4%") % |
| 108 | location.m_Function % |
| 109 | subgraphIndex % |
| 110 | operatorIndex % |
| 111 | location.FileLine())); |
| 112 | } |
| 113 | else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() && |
| 114 | operatorIndex != VIRTUAL_OPERATOR_ID) |
| 115 | { |
| 116 | throw ParseException( |
| 117 | boost::str( |
| 118 | boost::format("%1% was called with an invalid operator index. " |
| 119 | "subgraph:%2% operator:%3% at %4%") % |
| 120 | location.m_Function % |
| 121 | subgraphIndex % |
| 122 | operatorIndex % |
| 123 | location.FileLine())); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 128 | CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION()) |
| 129 | |
| 130 | void CheckTensor(const TfLiteParser::ModelPtr & model, |
| 131 | size_t subgraphIndex, |
| 132 | size_t tensorIndex, |
| 133 | const CheckLocation & location) |
| 134 | { |
| 135 | // not checking model, because I assume CHECK_MODEL already run |
| 136 | // and checked that. An assert would do. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 137 | ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | |
| 139 | // also subgraph index should be checked by CHECK_MODEL so |
| 140 | // I only add an assert here |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 141 | ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 142 | |
| 143 | // the tensor index is the only one to check here |
| 144 | if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size()) |
| 145 | { |
| 146 | throw ParseException( |
| 147 | boost::str( |
| 148 | boost::format("%1% was called with an invalid tensor index. " |
| 149 | "subgraph:%2% tensor:%3% at %4%") % |
| 150 | location.m_Function % |
| 151 | subgraphIndex % |
| 152 | tensorIndex % |
| 153 | location.FileLine())); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \ |
| 158 | CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION()) |
| 159 | |
| 160 | void CheckTensorPtr(TfLiteParser::TensorRawPtr rawPtr, |
| 161 | const CheckLocation & location) |
| 162 | { |
| 163 | if (rawPtr == nullptr) |
| 164 | { |
| 165 | throw ParseException( |
| 166 | boost::str( |
| 167 | boost::format("%1% was called with a null tensor pointer. " |
| 168 | "at %2%") % |
| 169 | location.m_Function % |
| 170 | location.FileLine())); |
| 171 | |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | #define CHECK_TENSOR_PTR(TENSOR_PTR) \ |
| 176 | CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) |
| 177 | |
| 178 | void CheckBuffer(const TfLiteParser::ModelPtr & model, |
| 179 | size_t bufferIndex, |
| 180 | const CheckLocation & location) |
| 181 | { |
| 182 | if (model.get() == nullptr) |
| 183 | { |
| 184 | throw ParseException( |
| 185 | boost::str( |
| 186 | boost::format("%1% was called with invalid (null) model. " |
| 187 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 188 | "buffer:%2% at %3%") % |
| 189 | location.m_Function % |
| 190 | bufferIndex % |
| 191 | location.FileLine())); |
| 192 | } |
| 193 | else if (bufferIndex >= model->buffers.size()) |
| 194 | { |
| 195 | throw ParseException( |
| 196 | boost::str( |
| 197 | boost::format("%1% was called with an invalid buffer index. " |
| 198 | "buffer index:%2% at %3%") % |
| 199 | location.m_Function % |
| 200 | bufferIndex % |
| 201 | location.FileLine())); |
| 202 | } |
| 203 | else if (model->buffers[bufferIndex].get() == nullptr) |
| 204 | { |
| 205 | throw ParseException( |
| 206 | boost::str( |
| 207 | boost::format("The buffer #%1% is null. %3%") % |
| 208 | bufferIndex % |
| 209 | location.AsString())); |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \ |
| 214 | CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION()) |
| 215 | |
| 216 | void CheckBufferSize(TfLiteParser::BufferRawPtr bufferPtr, |
| 217 | const armnn::TensorInfo & tensorInfo, |
| 218 | uint32_t bufferId, |
| 219 | const CheckLocation & location) |
| 220 | { |
| 221 | if (bufferPtr == nullptr) |
| 222 | { |
| 223 | throw ParseException( |
| 224 | boost::str( |
| 225 | boost::format("BufferPtr is null for buffer:%1%. %2%") % |
| 226 | bufferId % |
| 227 | location.AsString())); |
| 228 | } |
| 229 | else if(tensorInfo.GetNumElements() > bufferPtr->data.size() || |
| 230 | tensorInfo.GetNumBytes() > bufferPtr->data.size()) |
| 231 | { |
| 232 | std::stringstream ss; |
| 233 | ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. " |
| 234 | << "For tensor: " << tensorInfo.GetShape() |
| 235 | << " expecting: " << tensorInfo.GetNumBytes() << " bytes and " |
| 236 | << tensorInfo.GetNumElements() << " elements. " << location.AsString(); |
| 237 | throw ParseException(ss.str()); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \ |
| 242 | CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION()) |
| 243 | |
| 244 | bool IsActivationSupported(tflite::ActivationFunctionType activationType) |
| 245 | { |
| 246 | switch(activationType) |
| 247 | { |
| 248 | case tflite::ActivationFunctionType_NONE: |
| 249 | case tflite::ActivationFunctionType_RELU: |
| 250 | case tflite::ActivationFunctionType_RELU6: |
| 251 | case tflite::ActivationFunctionType_TANH: |
| 252 | { |
| 253 | return true; |
| 254 | } |
| 255 | default: |
| 256 | { |
| 257 | return false; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 263 | do { \ |
| 264 | if (IsActivationSupported(OPTION->fused_activation_function) == false) \ |
| 265 | { \ |
| 266 | throw ParseException( \ |
| 267 | boost::str( \ |
| 268 | boost::format("TfLite parser doesn't suppport fused activation: " \ |
| 269 | "%1%/%2% in %3% subgraph:%4% operator:%5% at %6%") % \ |
| 270 | OPTION->fused_activation_function % \ |
| 271 | tflite::EnumNameActivationFunctionType(\ |
| 272 | OPTION->fused_activation_function) % \ |
| 273 | __func__ % \ |
| 274 | SUBGRAPH_INDEX % \ |
| 275 | OPERATOR_INDEX % \ |
| 276 | CHECK_LOCATION().FileLine())); \ |
| 277 | } \ |
| 278 | } while(false) |
| 279 | |
| 280 | |
| 281 | std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in) |
| 282 | { |
| 283 | std::vector<unsigned int> result; |
| 284 | result.reserve(in.size()); |
| 285 | for (auto & i : in) |
| 286 | { |
| 287 | result.push_back(CHECKED_NON_NEGATIVE(i)); |
| 288 | } |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | void CalcPadding(uint32_t inputSize, |
| 293 | uint32_t filterSize, |
| 294 | uint32_t stride, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 295 | uint32_t dilation, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 296 | uint32_t& paddingFront, |
| 297 | uint32_t& paddingBack, |
| 298 | tflite::Padding padding) |
| 299 | { |
| 300 | paddingFront = 0; |
| 301 | paddingBack = 0; |
| 302 | if (padding == tflite::Padding_SAME) |
| 303 | { |
| 304 | uint32_t outputSize = (inputSize + stride - 1) / stride; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 305 | uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); |
| 306 | uint32_t temp = (outputSize - 1) * stride + dilatedSize; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 307 | if (temp > inputSize) |
| 308 | { |
| 309 | paddingFront = (temp - inputSize) / 2; |
| 310 | paddingBack = (temp - inputSize) - paddingFront; |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 315 | armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr, |
| 316 | const std::vector<unsigned int>& shapes, |
| 317 | const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3}, |
| 318 | const bool outputTensor = false) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 319 | { |
| 320 | armnn::DataType type; |
| 321 | CHECK_TENSOR_PTR(tensorPtr); |
| 322 | |
| 323 | switch (tensorPtr->type) |
| 324 | { |
| 325 | case tflite::TensorType_UINT8: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 326 | type = armnn::DataType::QAsymmU8; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 327 | break; |
| 328 | case tflite::TensorType_FLOAT32: |
| 329 | type = armnn::DataType::Float32; |
| 330 | break; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 331 | case tflite::TensorType_INT8: |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 332 | if (tensorPtr->quantization->zero_point.size() == 1) |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 333 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 334 | // Per-tensor |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 335 | type = armnn::DataType::QAsymmS8; |
| 336 | } |
| 337 | else |
| 338 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 339 | // Per-channel |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 340 | type = armnn::DataType::QSymmS8; |
| 341 | } |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 342 | break; |
| 343 | case tflite::TensorType_INT16: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 344 | type = armnn::DataType::QSymmS16; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 345 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 346 | case tflite::TensorType_INT32: |
| 347 | type = armnn::DataType::Signed32; |
| 348 | break; |
| 349 | |
| 350 | default: |
| 351 | { |
| 352 | CheckLocation location = CHECK_LOCATION(); |
| 353 | throw ParseException( |
| 354 | boost::str( |
| 355 | boost::format("Unsupported data type %1% = %2% for tensor: %3%. %4%") % |
| 356 | tensorPtr->type % |
| 357 | tflite::EnumNameTensorType(tensorPtr->type) % |
| 358 | tensorPtr->name % |
| 359 | location.AsString())); |
| 360 | } |
| 361 | } |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 362 | std::vector<unsigned int> safeShape = shapes; |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 363 | bool isDynamic = false; |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 364 | if (safeShape.size() == 0) |
| 365 | { |
| 366 | safeShape.push_back(1); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 367 | if (outputTensor) |
| 368 | { |
| 369 | isDynamic = true; |
| 370 | } |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 371 | } |
| 372 | |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 373 | float quantizationScale = 0.0f; |
| 374 | int32_t quantizationOffset = 0; |
| 375 | |
| 376 | if (tensorPtr->quantization.get()) |
| 377 | { |
| 378 | if (tensorPtr->quantization->scale.size() <= 1) |
| 379 | { |
| 380 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 381 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 382 | |
| 383 | if (tensorPtr->quantization->scale.size() == 1) |
| 384 | { |
| 385 | quantizationScale = tensorPtr->quantization->scale[0]; |
| 386 | } |
| 387 | if (tensorPtr->quantization->zero_point.size() == 1) |
| 388 | { |
| 389 | // NOTE: we lose precision here when converting from 64 bit to 32 |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 390 | // but this is what we support at the moment in ArmNN |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 391 | quantizationOffset = boost::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]); |
| 392 | } |
| 393 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 394 | TensorShape tensorShape(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 395 | safeShape.data()); |
| 396 | if (isDynamic) |
| 397 | { |
| 398 | tensorShape = TensorShape(1, false); |
| 399 | } |
| 400 | armnn::TensorInfo result(tensorShape, |
| 401 | type, |
| 402 | quantizationScale, |
| 403 | quantizationOffset); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 404 | return result; |
| 405 | } |
| 406 | else |
| 407 | { |
| 408 | std::vector<float> quantizationScales; |
| 409 | std::vector<int32_t> quantizationOffsets; |
| 410 | |
| 411 | // Scale |
| 412 | std::copy(tensorPtr->quantization->scale.begin(), |
| 413 | tensorPtr->quantization->scale.end(), |
| 414 | std::back_inserter(quantizationScales)); |
| 415 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 416 | // QSymmS8 Per-axis |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 417 | TensorShape tensorShape(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 418 | safeShape.data()); |
| 419 | if (isDynamic) |
| 420 | { |
| 421 | tensorShape = TensorShape(1, false); |
| 422 | } |
| 423 | armnn::TensorInfo result(tensorShape, |
| 424 | type, |
| 425 | quantizationScales, |
| 426 | dimensionMappings[boost::numeric_cast<unsigned int>( |
| 427 | tensorPtr->quantization->quantized_dimension)]); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 428 | return result; |
| 429 | } |
| 430 | } |
| 431 | else |
| 432 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 433 | TensorShape tensorShape(boost::numeric_cast<unsigned int>(safeShape.size()), |
| 434 | safeShape.data()); |
| 435 | if (isDynamic) |
| 436 | { |
| 437 | tensorShape = TensorShape(1, false); |
| 438 | } |
| 439 | armnn::TensorInfo result(tensorShape, |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 440 | type, |
| 441 | quantizationScale, |
| 442 | quantizationOffset); |
| 443 | return result; |
| 444 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 445 | } |
| 446 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 447 | armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr, |
| 448 | const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3}) |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 449 | { |
| 450 | auto const & dimensions = AsUnsignedVector(tensorPtr->shape); |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 451 | return ToTensorInfo(tensorPtr, dimensions, dimensionMappings); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 454 | armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr, |
| 455 | const bool outputTensor) |
| 456 | { |
| 457 | auto const & dimensions = AsUnsignedVector(tensorPtr->shape); |
| 458 | const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3}; |
| 459 | return ToTensorInfo(tensorPtr, dimensions, dimensionMappings, outputTensor); |
| 460 | } |
| 461 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 462 | template<typename T> |
| 463 | std::pair<armnn::ConstTensor, std::unique_ptr<T[]>> |
| 464 | CreateConstTensorImpl(TfLiteParser::BufferRawPtr bufferPtr, |
| 465 | TfLiteParser::TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 466 | armnn::TensorInfo& tensorInfo, |
| 467 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 468 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 469 | IgnoreUnused(tensorPtr); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 470 | ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null"); |
| 471 | ARMNN_ASSERT_MSG(bufferPtr != nullptr, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 472 | boost::str( |
| 473 | boost::format("Buffer for buffer:%1% is null") % tensorPtr->buffer).c_str()); |
| 474 | |
| 475 | std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 476 | |
| 477 | if (permutationVector.has_value() && permutationVector.value().GetSize() > 0) |
| 478 | { |
| 479 | tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 480 | armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(), |
| 481 | reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T)); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 482 | } |
| 483 | else |
| 484 | { |
| 485 | ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes()); |
| 486 | } |
| 487 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 488 | return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data)); |
| 489 | } |
| 490 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 491 | armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex) |
| 492 | { |
| 493 | // generate the binding id by shifting the tensor id by 8 bit |
| 494 | // and add the subgraph id, which allows 256 subgraphs |
| 495 | return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex); |
| 496 | } |
| 497 | |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 498 | bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected) |
| 499 | { |
| 500 | const unsigned int actualSize = actual.GetNumDimensions(); |
| 501 | if (actualSize != expected.size()) |
| 502 | { |
| 503 | return false; |
| 504 | } |
| 505 | |
| 506 | for (unsigned int i = 0u; i < actualSize; i++) |
| 507 | { |
| 508 | if (expected[i] < 0 || |
| 509 | actual[i] != static_cast<unsigned int>(expected[i])) |
| 510 | { |
| 511 | return false; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | return true; |
| 516 | } |
| 517 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 518 | void CheckMatchingQuantization(const TensorInfo& first, |
| 519 | const TensorInfo& second, |
| 520 | const std::string& descName, |
| 521 | std::string const& firstName, |
| 522 | std::string const& secondName) |
| 523 | { |
| 524 | if (!first.IsQuantized() || |
| 525 | !second.IsQuantized()) |
| 526 | { |
| 527 | // Not a quantized type, ignore the validation |
| 528 | return; |
| 529 | } |
| 530 | |
| 531 | DataType firstDataType = first.GetDataType(); |
| 532 | DataType secondDataType = second.GetDataType(); |
| 533 | |
| 534 | if (firstDataType != secondDataType) |
| 535 | { |
| 536 | throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName + |
| 537 | " must be of the same quantized type, " + |
| 538 | firstName + " is " + GetDataTypeName(firstDataType) + ", " + |
| 539 | secondName + " is " + GetDataTypeName(secondDataType)); |
| 540 | } |
| 541 | |
| 542 | if (!first.IsTypeSpaceMatch(second)) |
| 543 | { |
| 544 | throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName + |
| 545 | " must have the same quantization space, " + |
| 546 | firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) + |
| 547 | " and scale " + std::to_string(first.GetQuantizationScale()) + ", " + |
| 548 | secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) + |
| 549 | " and scale " + std::to_string(second.GetQuantizationScale())); |
| 550 | } |
| 551 | } |
| 552 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 553 | } // <anonymous> |
| 554 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 555 | TfLiteParser::TfLiteParser(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
| 556 | : m_Options(options) |
| 557 | , m_Network(nullptr, nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 558 | , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParser::ParseUnsupportedOperator) |
| 559 | { |
| 560 | // register supported operators |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 561 | m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParser::ParseAdd; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 562 | m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParser::ParseAveragePool2D; |
| 563 | m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParser::ParseBatchToSpaceND; |
| 564 | m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParser::ParseConcatenation; |
| 565 | m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParser::ParseConv2D; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 566 | m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParser::ParseCustomOperator; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 567 | m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParser::ParseDepthwiseConv2D; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 568 | m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParser::ParseDequantize; |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 569 | m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParser::ParseExp; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 570 | m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParser::ParseFullyConnected; |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 571 | m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParser::ParseHardSwish; |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 572 | m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParser::ParseLeakyRelu; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 573 | m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParser::ParseLogistic; |
| 574 | m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParser::ParseL2Normalization; |
| 575 | m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParser::ParseMaxPool2D; |
| 576 | m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParser::ParseMaximum; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 577 | m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParser::ParseMean; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 578 | m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParser::ParseMinimum; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 579 | m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParser::ParseMul; |
Darshan Patel | 83fcf98 | 2020-05-26 22:22:42 +0530 | [diff] [blame] | 580 | m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParser::ParseNeg; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 581 | m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParser::ParsePack; |
| 582 | m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParser::ParsePad; |
| 583 | m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParser::ParseQuantize; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 584 | m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParser::ParseRelu; |
| 585 | m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParser::ParseRelu6; |
| 586 | m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParser::ParseReshape; |
| 587 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParser::ParseResizeBilinear; |
| 588 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParser::ParseResizeNearestNeighbor; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 589 | m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParser::ParseSlice; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 590 | m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParser::ParseSoftmax; |
| 591 | m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParser::ParseSpaceToBatchND; |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 592 | m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParser::ParseSplit; |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 593 | m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParser::ParseSplitV; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 594 | m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParser::ParseSqueeze; |
| 595 | m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParser::ParseStridedSlice; |
| 596 | m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParser::ParseSub; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 597 | m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParser::ParseTanH; |
| 598 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParser::ParseTranspose; |
| 599 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParser::ParseTransposeConv; |
| 600 | m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParser::ParseUnpack; |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 601 | m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParser::ParseDiv; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 602 | // register supported custom operators |
| 603 | m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParser::ParseDetectionPostProcess; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | void TfLiteParser::ResetParser() |
| 607 | { |
| 608 | m_Network = armnn::INetworkPtr(nullptr, nullptr); |
| 609 | m_Model = nullptr; |
| 610 | m_SubgraphConnections.clear(); |
| 611 | } |
| 612 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 613 | void TfLiteParser::AddBroadcastReshapeLayer(size_t subgraphIndex, |
| 614 | size_t operatorIndex, |
| 615 | IConnectableLayer *layer) |
| 616 | { |
| 617 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 618 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 619 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 620 | const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex]; |
| 621 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 622 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 623 | ARMNN_ASSERT(operatorPtr->inputs.size() > 1); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 624 | |
| 625 | uint32_t reshapedInputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[0]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 626 | TensorRawPtr tensorPtr = subgraphPtr->tensors[reshapedInputId].get(); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 627 | uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[1]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 628 | TensorRawPtr tensorPtr1 = subgraphPtr->tensors[inputId].get(); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 629 | |
| 630 | armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(tensorPtr); |
| 631 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(tensorPtr1); |
| 632 | |
Mike Kelly | c5789ca | 2020-07-06 19:24:15 +0100 | [diff] [blame] | 633 | uint32_t inputSlotId = 1; |
| 634 | uint32_t reshapeSlotId = 0; |
| 635 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 636 | if (inputTensorInfo.GetNumDimensions() < reshapedTensorInfo.GetNumDimensions()) |
| 637 | { |
| 638 | uint32_t id = reshapedInputId; |
| 639 | reshapedInputId = inputId; |
| 640 | inputId = id; |
| 641 | |
| 642 | reshapedTensorInfo = ToTensorInfo(tensorPtr1); |
| 643 | inputTensorInfo = ToTensorInfo(tensorPtr); |
Mike Kelly | c5789ca | 2020-07-06 19:24:15 +0100 | [diff] [blame] | 644 | |
| 645 | inputSlotId = 0; |
| 646 | reshapeSlotId = 1; |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 647 | } |
| 648 | |
| 649 | uint32_t numDimensions = inputTensorInfo.GetNumDimensions(); |
| 650 | |
| 651 | std::vector<unsigned> reshapedDim; |
| 652 | for (unsigned int i = 0; i < reshapedTensorInfo.GetNumDimensions(); ++i) |
| 653 | { |
| 654 | reshapedDim.push_back(reshapedTensorInfo.GetShape()[i]); |
| 655 | } |
| 656 | |
| 657 | std::vector<unsigned int> reshapedDimensions(numDimensions, 1); |
| 658 | std::copy_backward (reshapedDim.begin(), reshapedDim.end(), reshapedDimensions.end()); |
| 659 | |
| 660 | reshapedTensorInfo.SetShape(armnn::TensorShape{ numDimensions, reshapedDimensions.data() }); |
| 661 | |
| 662 | std::string layerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 663 | armnn::ReshapeDescriptor desc; |
| 664 | desc.m_TargetShape = reshapedTensorInfo.GetShape(); |
| 665 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 666 | |
| 667 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo); |
Mike Kelly | c5789ca | 2020-07-06 19:24:15 +0100 | [diff] [blame] | 668 | reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(reshapeSlotId)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 669 | |
| 670 | RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {reshapedInputId}); |
| 671 | |
Mike Kelly | c5789ca | 2020-07-06 19:24:15 +0100 | [diff] [blame] | 672 | armnn::IInputSlot* input1Slot = &(layer->GetInputSlot(inputSlotId)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 673 | RegisterConsumerOfTensor(subgraphIndex, inputId, input1Slot); |
| 674 | } |
| 675 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 676 | INetworkPtr TfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile) |
| 677 | { |
| 678 | ResetParser(); |
| 679 | m_Model = LoadModelFromFile(graphFile); |
| 680 | return CreateNetworkFromModel(); |
| 681 | } |
| 682 | |
| 683 | INetworkPtr TfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) |
| 684 | { |
| 685 | ResetParser(); |
| 686 | m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size()); |
| 687 | return CreateNetworkFromModel(); |
| 688 | } |
| 689 | |
| 690 | INetworkPtr TfLiteParser::CreateNetworkFromModel() |
| 691 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 692 | |
| 693 | using NetworkOptions = std::vector<BackendOptions>; |
| 694 | NetworkOptions networkOptions = {}; |
| 695 | if (m_Options && m_Options.value().m_InferAndValidate) |
| 696 | { |
| 697 | BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod", |
| 698 | { |
| 699 | { "InferAndValidate", true } |
| 700 | }); |
| 701 | |
| 702 | networkOptions.push_back(shapeInferenceMethodOption); |
| 703 | } |
| 704 | |
| 705 | m_Network = INetwork::Create(networkOptions); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 706 | ARMNN_ASSERT(m_Model.get() != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 707 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 708 | if (m_Model->subgraphs.size() != 1) |
| 709 | { |
| 710 | throw ParseException( |
| 711 | boost::str( |
| 712 | boost::format("Current TfLite parser only supports 1 subgraph. Current one has: %1% %2%") % |
| 713 | m_Model->subgraphs.size() % |
| 714 | CHECK_LOCATION().AsString())); |
| 715 | } |
| 716 | |
| 717 | size_t subgraphIndex = 0; |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 718 | size_t operatorIndex = 0; |
| 719 | try |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 720 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 721 | for (SubgraphPtr const& subgraph : m_Model->subgraphs) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 722 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 723 | m_SubgraphConnections.emplace_back(subgraph->tensors.size()); |
| 724 | for (OperatorPtr const& op : subgraph->operators) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 725 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 726 | auto const& opCodePtr = m_Model->operator_codes[op->opcode_index]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 727 | auto builtinCode = opCodePtr->builtin_code; |
| 728 | |
| 729 | if (builtinCode > tflite::BuiltinOperator_MAX) |
| 730 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 731 | throw ParseException(boost::str(boost::format("Operator code %1% is out of range 0-%2%. " |
| 732 | "subgraph:%3% operator idx:%4%. %5%") % |
| 733 | builtinCode % tflite::BuiltinOperator_MAX % subgraphIndex % |
| 734 | operatorIndex % CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | // lookup and call the parser function |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 738 | auto& parserFunction = m_ParserFunctions[builtinCode]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 739 | (this->*parserFunction)(subgraphIndex, operatorIndex); |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 740 | ++operatorIndex; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 741 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 742 | |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 743 | SetupInputLayers(subgraphIndex); |
| 744 | SetupOutputLayers(subgraphIndex); |
| 745 | SetupConstantLayers(subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 746 | |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 747 | ++subgraphIndex; |
| 748 | operatorIndex = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 749 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 750 | } |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 751 | catch (const ParseException& e) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 752 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 753 | std::stringstream errorString; |
| 754 | errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #" |
| 755 | << subgraphIndex << " error: " << e.what(); |
| 756 | ARMNN_LOG(error) << errorString.str(); |
| 757 | std::stringstream errors; |
| 758 | errors << errorString.str() << "\n"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 759 | throw ParseException(errors.str()); |
| 760 | } |
| 761 | |
| 762 | // establish the connections from the layer outputs to the inputs of the subsequent layers |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 763 | for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 764 | { |
| 765 | for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 766 | { |
| 767 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr) |
| 768 | { |
| 769 | for (size_t inputSlotIdx = 0; |
| 770 | inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size(); |
| 771 | ++inputSlotIdx) |
| 772 | { |
| 773 | m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect( |
| 774 | *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx])); |
| 775 | } |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return std::move(m_Network); |
| 781 | } |
| 782 | |
| 783 | void TfLiteParser::RegisterProducerOfTensor(size_t subgraphIndex, |
| 784 | size_t tensorIndex, |
| 785 | armnn::IOutputSlot* slot) |
| 786 | { |
| 787 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 788 | ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 789 | ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 790 | |
| 791 | TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
| 792 | |
| 793 | // assuming there is only one producer for that tensor |
| 794 | if (tensorSlots.outputSlot != nullptr) |
| 795 | { |
| 796 | throw ParseException(boost::str( |
| 797 | boost::format("Another layer has already registered itself as the producer of " |
| 798 | "subgraph:%1% tensor:%2% %3%") % |
| 799 | subgraphIndex % |
| 800 | tensorIndex % |
| 801 | CHECK_LOCATION().AsString())); |
| 802 | } |
| 803 | |
| 804 | tensorSlots.outputSlot = slot; |
| 805 | } |
| 806 | |
| 807 | void TfLiteParser::RegisterConsumerOfTensor(size_t subgraphIndex, |
| 808 | size_t tensorIndex, |
| 809 | armnn::IInputSlot* slot) |
| 810 | { |
| 811 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 812 | ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 813 | ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 814 | |
| 815 | TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
| 816 | tensorSlots.inputSlots.push_back(slot); |
| 817 | } |
| 818 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 819 | void TfLiteParser::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex) |
| 820 | { |
| 821 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 822 | |
| 823 | // NOTE: By default we presume the custom operator is not supported |
| 824 | auto customParserFunction = &TfLiteParser::ParseUnsupportedOperator; |
| 825 | |
| 826 | // Identify custom code defined for custom operator |
| 827 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 828 | const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code; |
| 829 | |
| 830 | // Find parser function that correspondes to custom code (if any) |
| 831 | auto iterator = m_CustomParserFunctions.find(customCode); |
| 832 | if (iterator != m_CustomParserFunctions.end()) |
| 833 | { |
| 834 | customParserFunction = iterator->second; |
| 835 | } |
| 836 | |
| 837 | // Run parser function |
| 838 | (this->*customParserFunction)(subgraphIndex, operatorIndex); |
| 839 | } |
| 840 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 841 | void TfLiteParser::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex) |
| 842 | { |
| 843 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 844 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 845 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 846 | |
| 847 | auto opcodeIndex = operatorPtr->opcode_index; |
| 848 | auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code; |
| 849 | |
| 850 | if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported) |
| 851 | { |
| 852 | // Do not add StandInLayer, throw ParseException instead |
| 853 | throw ParseException( |
| 854 | boost::str( |
| 855 | boost::format("Operator not supported. " |
| 856 | "subgraph:%1% operator:%2% " |
| 857 | "opcode_index:%3% opcode:%4% / %5% %6%") % |
| 858 | subgraphIndex % |
| 859 | operatorIndex % |
| 860 | opcodeIndex % |
| 861 | opcode % |
| 862 | tflite::EnumNameBuiltinOperator(opcode) % |
| 863 | CHECK_LOCATION().AsString())); |
| 864 | } |
| 865 | |
| 866 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 867 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 868 | |
| 869 | const unsigned int numInputs = boost::numeric_cast<unsigned int>(inputs.size()); |
| 870 | const unsigned int numOutputs = boost::numeric_cast<unsigned int>(outputs.size()); |
| 871 | |
| 872 | StandInDescriptor descriptor(numInputs, numOutputs); |
| 873 | auto layerName = boost::str(boost::format("StandIn:%1%:%2%:%3%") % subgraphIndex % operatorIndex % opcode); |
| 874 | |
| 875 | // Add a non-executable StandInLayer as a placeholder for any unsupported operator |
| 876 | IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 877 | ARMNN_ASSERT(layer != nullptr); |
| 878 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 879 | for (unsigned int i = 0u; i < numOutputs; ++i) |
| 880 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 881 | layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true)); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 885 | auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 886 | |
| 887 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds); |
| 888 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 889 | } |
| 890 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 891 | void TfLiteParser::ParseConv2D(size_t subgraphIndex, size_t operatorIndex) |
| 892 | { |
| 893 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 894 | |
| 895 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 896 | const auto * options = operatorPtr->builtin_options.AsConv2DOptions(); |
| 897 | |
| 898 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 899 | |
| 900 | Convolution2dDescriptor desc; |
| 901 | desc.m_BiasEnabled = false; |
| 902 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 903 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 904 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 905 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 906 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 907 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 908 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 909 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 910 | |
| 911 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 912 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 913 | |
| 914 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 915 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 916 | |
| 917 | // assuming input is NHWC |
| 918 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 919 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 920 | |
| 921 | // assuming the filter is OHWI : Output, H, W, Input |
| 922 | // which is essentially the same as NHWC |
| 923 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 924 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 925 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 926 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 927 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 928 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 929 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 930 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 931 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 932 | filterTensorInfo, |
| 933 | armnn::Optional<armnn::PermutationVector&>()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 934 | armnn::IConnectableLayer* layer = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 935 | |
| 936 | auto layerName = boost::str(boost::format("Conv2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 937 | |
| 938 | if (inputs.size() == 3) |
| 939 | { |
| 940 | desc.m_BiasEnabled = true; |
| 941 | armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 942 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 943 | biasTensorInfo, |
| 944 | armnn::Optional<armnn::PermutationVector&>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 945 | layer = m_Network->AddConvolution2dLayer(desc, |
| 946 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 947 | Optional<ConstTensor>(biasTensorAndData.first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 948 | layerName.c_str()); |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | layer = m_Network->AddConvolution2dLayer(desc, |
| 953 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 954 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 955 | layerName.c_str()); |
| 956 | } |
| 957 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 958 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 959 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 960 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 961 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 962 | |
| 963 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 964 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 965 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 966 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 967 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 968 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 969 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 970 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 971 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 972 | } |
| 973 | |
| 974 | void TfLiteParser::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex) |
| 975 | { |
| 976 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 977 | |
| 978 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 979 | const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions(); |
| 980 | |
| 981 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 982 | |
| 983 | DepthwiseConvolution2dDescriptor desc; |
| 984 | desc.m_BiasEnabled = false; |
| 985 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 986 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 987 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Matthew Jackson | d6a9dee | 2019-07-22 13:53:24 +0100 | [diff] [blame] | 988 | CHECKED_NON_NEGATIVE(options->depth_multiplier); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 989 | |
| 990 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 991 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 992 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 993 | CHECK_VALID_SIZE(outputs.size(), 1); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 994 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 995 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 996 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 997 | // Mappings from TensorflowLite filter tensors to the ArmNN filter tensors (ArmNN weights have to be [M, I, H, W]) |
| 998 | PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, H, W] |
| 999 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1000 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 1001 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1], permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1002 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1003 | // Assuming input is NHWC |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1004 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1005 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1006 | |
| 1007 | // TensorflowLite weights come in the format [1, H, W, I * M] |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1008 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1009 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1010 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1011 | // Reshape weights as [ H, W, I, M ] |
| 1012 | filterTensorInfo.SetShape({ filterHeight, |
| 1013 | filterWidth, |
| 1014 | inputTensorInfo.GetShape()[3], |
| 1015 | filterTensorInfo.GetShape()[3] / inputTensorInfo.GetShape()[3] }); |
| 1016 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1017 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 1018 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1019 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 1020 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1021 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1022 | auto filterTensorAndData = CreateConstTensor(inputs[1], filterTensorInfo, permutationVector); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1023 | armnn::IConnectableLayer* layer = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1024 | auto layerName = boost::str(boost::format("DepthwiseConv2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1025 | |
| 1026 | if (inputs.size() == 3) |
| 1027 | { |
| 1028 | desc.m_BiasEnabled = true; |
| 1029 | TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1030 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 1031 | biasTensorInfo, |
| 1032 | armnn::Optional<armnn::PermutationVector&>()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1033 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
| 1034 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1035 | Optional<ConstTensor>(biasTensorAndData.first), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1036 | layerName.c_str()); |
| 1037 | } |
| 1038 | else |
| 1039 | { |
| 1040 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
| 1041 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1042 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1043 | layerName.c_str()); |
| 1044 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1045 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1046 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1047 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1048 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1049 | |
| 1050 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1051 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1052 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1053 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1054 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1055 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1056 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1057 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1058 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1059 | } |
| 1060 | |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1061 | void TfLiteParser::ParseDequantize(size_t subgraphIndex, size_t operatorIndex) |
| 1062 | { |
| 1063 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1064 | |
| 1065 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1066 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1067 | |
| 1068 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1069 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1070 | |
| 1071 | auto layerName = boost::str(boost::format("Dequantize:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1072 | |
| 1073 | IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1074 | ARMNN_ASSERT(layer != nullptr); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1075 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1076 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1077 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1078 | |
| 1079 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1080 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1081 | |
| 1082 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1083 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1084 | } |
| 1085 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 1086 | void TfLiteParser::ParseExp(size_t subgraphIndex, size_t operatorIndex) |
| 1087 | { |
| 1088 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1089 | |
| 1090 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1091 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1092 | |
| 1093 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1094 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1095 | |
| 1096 | auto layerName = boost::str(boost::format("Exp:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1097 | |
| 1098 | ElementwiseUnaryDescriptor desc; |
| 1099 | desc.m_Operation = UnaryOperation::Exp; |
| 1100 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerName.c_str()); |
| 1101 | ARMNN_ASSERT(layer != nullptr); |
| 1102 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1103 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 1104 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1105 | |
| 1106 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1107 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1108 | |
| 1109 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1110 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1111 | } |
| 1112 | |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1113 | void TfLiteParser::ParseTranspose(size_t subgraphIndex, size_t operatorIndex) |
| 1114 | { |
| 1115 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1116 | |
| 1117 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1118 | CHECK_VALID_SIZE(inputs.size(), 1, 2); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1119 | |
| 1120 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1121 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1122 | |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1123 | auto layerName = boost::str(boost::format("Transpose:%1%:%2%") % subgraphIndex % operatorIndex); |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1124 | TransposeDescriptor desc; |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1125 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1126 | if (inputs.size() == 2) |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1127 | { |
| 1128 | armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]); |
| 1129 | BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1130 | auto numPermVecElements = permuteTensorInfo.GetNumElements(); |
| 1131 | std::vector<unsigned int> permuteShape(numPermVecElements); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1132 | ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes()); |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1133 | PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements()); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1134 | |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1135 | desc = TransposeDescriptor(permutationVector); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1136 | } |
| 1137 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1138 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1139 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1140 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1141 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1142 | IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1143 | ARMNN_ASSERT(layer != nullptr); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1144 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1145 | |
| 1146 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1147 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1148 | |
| 1149 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1150 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1151 | } |
| 1152 | |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1153 | void TfLiteParser::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex) |
| 1154 | { |
| 1155 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1156 | |
| 1157 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1158 | const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions(); |
| 1159 | |
| 1160 | TransposeConvolution2dDescriptor desc; |
| 1161 | desc.m_BiasEnabled = false; |
| 1162 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1163 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1164 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1165 | |
| 1166 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1167 | CHECK_VALID_SIZE(inputs.size(), 3); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1168 | |
| 1169 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1170 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1171 | |
Colm Donelan | 0ad3ef1 | 2020-07-03 15:54:28 +0100 | [diff] [blame] | 1172 | if (inputs[0]) |
| 1173 | { |
| 1174 | armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]); |
| 1175 | std::vector<int> output_shape(tensorInfo.GetNumElements()); |
| 1176 | if (tensorInfo.GetDataType() == DataType::Signed32) |
| 1177 | { |
| 1178 | ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes()); |
| 1179 | } |
| 1180 | if (tensorInfo.GetDataType() == DataType::QAsymmU8) |
| 1181 | { |
| 1182 | for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++) |
| 1183 | { |
| 1184 | output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i]; |
| 1185 | } |
| 1186 | } |
| 1187 | // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor. |
| 1188 | for (int dimension : output_shape) |
| 1189 | { |
| 1190 | desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension)); |
| 1191 | } |
| 1192 | desc.m_OutputShapeEnabled = true; |
| 1193 | } |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1194 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1195 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 1196 | |
| 1197 | // TfLite uses NHWC tensors |
| 1198 | const unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1199 | const unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1200 | |
| 1201 | const unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1202 | const unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1203 | |
| 1204 | CalcPadding(inputHeight, |
| 1205 | filterHeight, |
| 1206 | desc.m_StrideY, |
| 1207 | 1, // DilationY |
| 1208 | desc.m_PadTop, |
| 1209 | desc.m_PadBottom, |
| 1210 | options->padding); |
| 1211 | |
| 1212 | CalcPadding(inputWidth, |
| 1213 | filterWidth, |
| 1214 | desc.m_StrideX, |
| 1215 | 1, // DilationX |
| 1216 | desc.m_PadLeft, |
| 1217 | desc.m_PadRight, |
| 1218 | options->padding); |
| 1219 | |
| 1220 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 1221 | filterTensorInfo, |
| 1222 | armnn::Optional<armnn::PermutationVector&>()); |
| 1223 | |
| 1224 | armnn::IConnectableLayer* layer = nullptr; |
| 1225 | auto layerName = boost::str(boost::format("TransposeConv:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1226 | |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1227 | layer = m_Network->AddTransposeConvolution2dLayer(desc, |
| 1228 | filterTensorAndData.first, |
| 1229 | EmptyOptional(), |
| 1230 | layerName.c_str()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1231 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1232 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1233 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1234 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1235 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1236 | |
| 1237 | // only the tensors for the inputs are relevant, exclude the const (filter) tensor |
| 1238 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1239 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]}); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1240 | |
| 1241 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1242 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1243 | } |
| 1244 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1245 | void TfLiteParser::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex) |
| 1246 | { |
| 1247 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average); |
| 1248 | } |
| 1249 | |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1250 | void TfLiteParser::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex) |
| 1251 | { |
| 1252 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1253 | |
| 1254 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1255 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1256 | |
| 1257 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1258 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1259 | |
| 1260 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1261 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1262 | |
| 1263 | armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]); |
| 1264 | BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1265 | |
| 1266 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1267 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1268 | |
| 1269 | std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements()); |
| 1270 | ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes()); |
| 1271 | |
| 1272 | size_t step = 2; |
| 1273 | std::vector<std::pair<unsigned int, unsigned int>> crops; |
| 1274 | for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i) |
| 1275 | { |
| 1276 | crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]); |
| 1277 | } |
| 1278 | |
| 1279 | armnn::BatchToSpaceNdDescriptor desc; |
| 1280 | desc.m_BlockShape = blockShape; |
| 1281 | desc.m_Crops = crops; |
| 1282 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1283 | |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1284 | auto layerName = boost::str(boost::format("BatchToSpaceND:%1%:%2%") % subgraphIndex % operatorIndex); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1285 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1286 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1287 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1288 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1289 | |
| 1290 | IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str()); |
| 1291 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1292 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1293 | |
| 1294 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1295 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1296 | |
| 1297 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1298 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1299 | } |
| 1300 | |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1301 | void TfLiteParser::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex) |
| 1302 | { |
| 1303 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1304 | |
| 1305 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1306 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1307 | |
| 1308 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1309 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1310 | |
| 1311 | L2NormalizationDescriptor desc; |
| 1312 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1313 | auto layerName = boost::str(boost::format("L2Normalization:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1314 | IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str()); |
| 1315 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1316 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1317 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1318 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1319 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1320 | |
| 1321 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1322 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1323 | |
| 1324 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1325 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1326 | } |
| 1327 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1328 | void TfLiteParser::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex) |
| 1329 | { |
| 1330 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max); |
| 1331 | } |
| 1332 | |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1333 | void TfLiteParser::ParseMaximum(size_t subgraphIndex, size_t operatorIndex) |
| 1334 | { |
| 1335 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1336 | |
| 1337 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1338 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1339 | |
| 1340 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1341 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1342 | |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1343 | auto layerName = boost::str(boost::format("Maximum:%1%:%2%") % subgraphIndex % operatorIndex); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1344 | |
| 1345 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1346 | TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1347 | CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1"); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1348 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1349 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1350 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1351 | |
| 1352 | IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str()); |
| 1353 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1354 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1355 | |
| 1356 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1357 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1358 | { |
| 1359 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1360 | } |
| 1361 | else |
| 1362 | { |
| 1363 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1364 | } |
| 1365 | |
| 1366 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1367 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1368 | } |
| 1369 | |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1370 | void TfLiteParser::ParseMinimum(size_t subgraphIndex, size_t operatorIndex) |
| 1371 | { |
| 1372 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1373 | |
| 1374 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1375 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1376 | |
| 1377 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1378 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1379 | |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1380 | auto layerName = boost::str(boost::format("Minimum:%1%:%2%") % subgraphIndex % operatorIndex); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1381 | |
| 1382 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1383 | TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1384 | CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1"); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1385 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1386 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1387 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1388 | |
| 1389 | IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str()); |
| 1390 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1391 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1392 | |
| 1393 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1394 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1395 | { |
| 1396 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1397 | } |
| 1398 | else |
| 1399 | { |
| 1400 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1401 | } |
| 1402 | |
| 1403 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1404 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1405 | } |
| 1406 | |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1407 | void TfLiteParser::ParsePool(size_t subgraphIndex, |
| 1408 | size_t operatorIndex, |
| 1409 | PoolingAlgorithm algorithm) |
| 1410 | { |
| 1411 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1412 | |
| 1413 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1414 | const auto * options = operatorPtr->builtin_options.AsPool2DOptions(); |
| 1415 | |
| 1416 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 1417 | |
| 1418 | std::string layerName; |
| 1419 | |
| 1420 | switch (algorithm) |
| 1421 | { |
| 1422 | case PoolingAlgorithm::Average: |
| 1423 | layerName = |
| 1424 | boost::str(boost::format("AveragePool2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1425 | break; |
| 1426 | case PoolingAlgorithm::Max: |
| 1427 | layerName = |
| 1428 | boost::str(boost::format("MaxPool2D:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1429 | break; |
| 1430 | default: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1431 | ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm"); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | Pooling2dDescriptor desc; |
| 1435 | |
| 1436 | desc.m_PoolType = algorithm; |
| 1437 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1438 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1439 | desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width); |
| 1440 | desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height); |
| 1441 | desc.m_PaddingMethod = PaddingMethod::Exclude; |
| 1442 | desc.m_OutputShapeRounding = OutputShapeRounding::Floor; |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1443 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1444 | |
| 1445 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1446 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1447 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1448 | |
| 1449 | // assuming input is NHWC |
| 1450 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1451 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1452 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1453 | CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u, |
| 1454 | desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1455 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u, |
| 1456 | desc.m_PadLeft, desc.m_PadRight, options->padding); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1457 | |
| 1458 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1459 | CHECK_VALID_SIZE(outputs.size(), 1); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1460 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1461 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1462 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1463 | |
| 1464 | IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str()); |
| 1465 | ARMNN_ASSERT(layer != nullptr); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1466 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1467 | |
| 1468 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1469 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1470 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1471 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1472 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1473 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1474 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1475 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1476 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1477 | } |
| 1478 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1479 | void TfLiteParser::ParseSlice(size_t subgraphIndex, size_t operatorIndex) |
| 1480 | { |
| 1481 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1482 | |
| 1483 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1484 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1485 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1486 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1487 | |
| 1488 | SliceDescriptor desc; |
| 1489 | |
| 1490 | // set begin tensor info for slice descriptor |
| 1491 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1492 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1493 | |
| 1494 | std::vector<unsigned int> begin(beginTensorInfo.GetNumElements()); |
| 1495 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1496 | |
| 1497 | // set size tensor info for slice descriptor |
| 1498 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]); |
| 1499 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1500 | |
| 1501 | std::vector<unsigned int> size(sizeTensorInfo.GetNumElements()); |
| 1502 | ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
| 1503 | desc = SliceDescriptor(begin, size); |
| 1504 | |
| 1505 | auto layerName = boost::str(boost::format("Slice:%1%:%2%") % subgraphIndex % operatorIndex); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1506 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1507 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1508 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1509 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1510 | |
| 1511 | IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str()); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1512 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1513 | |
| 1514 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1515 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1516 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1517 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1518 | |
| 1519 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1520 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1521 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1522 | } |
| 1523 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1524 | void TfLiteParser::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex) |
| 1525 | { |
| 1526 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1527 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1528 | const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions(); |
| 1529 | |
| 1530 | SoftmaxDescriptor desc; |
| 1531 | desc.m_Beta = options->beta; |
| 1532 | |
| 1533 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1534 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1535 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1536 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1537 | |
| 1538 | auto layerName = boost::str(boost::format("Softmax:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1539 | IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str()); |
| 1540 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1541 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1542 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1543 | |
| 1544 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1545 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1546 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1547 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1548 | |
| 1549 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1550 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1551 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1552 | } |
| 1553 | |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1554 | void TfLiteParser::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex) |
| 1555 | { |
| 1556 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1557 | |
| 1558 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1559 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1560 | |
| 1561 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1562 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1563 | |
| 1564 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1565 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1566 | |
| 1567 | armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]); |
| 1568 | BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1569 | |
| 1570 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1571 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1572 | |
| 1573 | std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements()); |
| 1574 | ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes()); |
| 1575 | |
| 1576 | size_t step = 2; |
| 1577 | std::vector<std::pair<unsigned int, unsigned int>> padList; |
| 1578 | for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i) |
| 1579 | { |
| 1580 | padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]); |
| 1581 | } |
| 1582 | |
| 1583 | armnn::SpaceToBatchNdDescriptor desc; |
| 1584 | desc.m_BlockShape = blockShape; |
| 1585 | desc.m_PadList = padList; |
| 1586 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1587 | |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1588 | auto layerName = boost::str(boost::format("SpaceToBatchND:%1%:%2%") % subgraphIndex % operatorIndex); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1589 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1590 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1591 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1592 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1593 | |
| 1594 | IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str()); |
| 1595 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1596 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1597 | |
| 1598 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1599 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1600 | |
| 1601 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1602 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1603 | } |
| 1604 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1605 | armnn::TensorInfo TfLiteParser::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn, |
| 1606 | const armnn::TensorInfo & inputTensorInfo) |
| 1607 | { |
| 1608 | CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4); |
| 1609 | std::vector<uint32_t> squeezeDims = squeezeDimsIn; |
| 1610 | static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 1611 | |
| 1612 | if (inputTensorInfo.GetNumDimensions() > 4) |
| 1613 | { |
| 1614 | std::stringstream ss; |
| 1615 | ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1616 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1617 | << CHECK_LOCATION().AsString(); |
| 1618 | throw ParseException(ss.str()); |
| 1619 | } |
| 1620 | |
| 1621 | if (squeezeDims.empty()) |
| 1622 | { |
| 1623 | squeezeDims.assign(dimensionSequence, |
| 1624 | dimensionSequence+inputTensorInfo.GetNumDimensions()); |
| 1625 | } |
| 1626 | |
| 1627 | std::vector<uint32_t> outputDims; |
| 1628 | for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++) |
| 1629 | { |
| 1630 | bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end()); |
| 1631 | auto currentDimension = inputTensorInfo.GetShape()[i]; |
| 1632 | if (skipSqueeze || currentDimension != 1) |
| 1633 | { |
| 1634 | outputDims.push_back(currentDimension); |
| 1635 | } |
| 1636 | } |
| 1637 | |
| 1638 | if (outputDims.size() > 4) |
| 1639 | { |
| 1640 | std::stringstream ss; |
| 1641 | ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1642 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1643 | << CHECK_LOCATION().AsString(); |
| 1644 | throw ParseException(ss.str()); |
| 1645 | } |
| 1646 | |
| 1647 | TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()), |
| 1648 | outputDims.data()); |
| 1649 | |
| 1650 | // we need to preserve the tensor type and the quantization data as well |
| 1651 | TensorInfo outTensorInfo = inputTensorInfo; |
| 1652 | outTensorInfo.SetShape(outShape); |
| 1653 | |
| 1654 | return outTensorInfo; |
| 1655 | } |
| 1656 | |
| 1657 | void TfLiteParser::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex) |
| 1658 | { |
| 1659 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1660 | |
| 1661 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1662 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1663 | |
| 1664 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1665 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1666 | |
| 1667 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1668 | const auto * options = operatorPtr->builtin_options.AsSqueezeOptions(); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1669 | auto layerName = boost::str(boost::format("Squeeze:%1%:%2%") % subgraphIndex % operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1670 | |
| 1671 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1672 | armnn::TensorInfo outputTensorInfo = |
| 1673 | TfLiteParser::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims), |
| 1674 | inputTensorInfo); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1675 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1676 | |
| 1677 | ReshapeDescriptor reshapeDesc; |
| 1678 | reshapeDesc.m_TargetShape = outputTensorInfo.GetShape(); |
| 1679 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1680 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1681 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1682 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1683 | |
| 1684 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1685 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1686 | |
| 1687 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1688 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1689 | } |
| 1690 | |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 1691 | void TfLiteParser::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex) |
| 1692 | { |
| 1693 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1694 | |
| 1695 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1696 | CHECK_VALID_SIZE(inputs.size(), 4); |
| 1697 | |
| 1698 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1699 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1700 | |
| 1701 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1702 | const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions(); |
| 1703 | |
| 1704 | StridedSliceDescriptor desc; |
| 1705 | desc.m_BeginMask = options->begin_mask; |
| 1706 | desc.m_EllipsisMask = options->ellipsis_mask; |
| 1707 | desc.m_EndMask = options->end_mask; |
| 1708 | desc.m_NewAxisMask = options->new_axis_mask; |
| 1709 | desc.m_ShrinkAxisMask = options->shrink_axis_mask; |
| 1710 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1711 | |
| 1712 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1713 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1714 | |
| 1715 | std::vector<int> begin(beginTensorInfo.GetNumElements()); |
| 1716 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1717 | |
| 1718 | armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]); |
| 1719 | BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1720 | |
| 1721 | std::vector<int> end(endTensorInfo.GetNumElements()); |
| 1722 | ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes()); |
| 1723 | |
| 1724 | armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]); |
| 1725 | BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer); |
| 1726 | |
| 1727 | std::vector<int> stride(strideTensorInfo.GetNumElements()); |
| 1728 | ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes()); |
| 1729 | |
| 1730 | desc.m_Begin = begin; |
| 1731 | desc.m_End = end; |
| 1732 | desc.m_Stride = stride; |
| 1733 | |
| 1734 | auto layerName = boost::str(boost::format("StridedSlice:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1735 | IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1736 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 1737 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1738 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 1739 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1740 | |
| 1741 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1742 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1743 | |
| 1744 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1745 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1746 | } |
| 1747 | |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 1748 | void TfLiteParser::ParseSub(size_t subgraphIndex, size_t operatorIndex) |
| 1749 | { |
| 1750 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1751 | |
| 1752 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1753 | const auto * options = operatorPtr->builtin_options.AsSubOptions(); |
| 1754 | |
| 1755 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1756 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1757 | |
| 1758 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1759 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1760 | |
| 1761 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1762 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1763 | |
| 1764 | auto layerName = boost::str(boost::format("Sub:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1765 | IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1766 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 1767 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1768 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 1769 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1770 | |
| 1771 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1772 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1773 | { |
| 1774 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1775 | } |
| 1776 | else |
| 1777 | { |
| 1778 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1779 | } |
| 1780 | |
| 1781 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1782 | |
| 1783 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1784 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1785 | } |
| 1786 | |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 1787 | void TfLiteParser::ParseDiv(size_t subgraphIndex, size_t operatorIndex) |
| 1788 | { |
| 1789 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1790 | |
| 1791 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1792 | const auto * options = operatorPtr->builtin_options.AsDivOptions(); |
| 1793 | |
| 1794 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1795 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1796 | |
| 1797 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1798 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1799 | |
| 1800 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1801 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1802 | |
| 1803 | auto layerName = boost::str(boost::format("Div:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1804 | IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1805 | ARMNN_ASSERT(layer != nullptr); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 1806 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1807 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 1808 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1809 | |
| 1810 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1811 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1812 | { |
| 1813 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1814 | } |
| 1815 | else |
| 1816 | { |
| 1817 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1818 | } |
| 1819 | |
| 1820 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1821 | |
| 1822 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1823 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1824 | } |
| 1825 | |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1826 | void TfLiteParser::ParseAdd(size_t subgraphIndex, size_t operatorIndex) |
| 1827 | { |
| 1828 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1829 | |
| 1830 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1831 | const auto * options = operatorPtr->builtin_options.AsAddOptions(); |
| 1832 | |
| 1833 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1834 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1835 | |
| 1836 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1837 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1838 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1839 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1840 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1841 | |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1842 | auto layerName = boost::str(boost::format("Add:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1843 | IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1844 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1845 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1846 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1847 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1848 | |
| 1849 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1850 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1851 | { |
| 1852 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1853 | } |
| 1854 | else |
| 1855 | { |
| 1856 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1857 | } |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 1858 | |
| 1859 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1860 | |
| 1861 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1862 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1863 | } |
| 1864 | |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1865 | void TfLiteParser::ParseMul(size_t subgraphIndex, size_t operatorIndex) |
| 1866 | { |
| 1867 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1868 | |
| 1869 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1870 | const auto * options = operatorPtr->builtin_options.AsMulOptions(); |
| 1871 | |
| 1872 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1873 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1874 | |
| 1875 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1876 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1877 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1878 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1879 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1880 | |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1881 | auto layerName = boost::str(boost::format("Mul:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1882 | IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1883 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1884 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1885 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1886 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1887 | |
| 1888 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 1889 | if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions()) |
| 1890 | { |
| 1891 | AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer); |
| 1892 | } |
| 1893 | else |
| 1894 | { |
| 1895 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 1896 | } |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 1897 | |
| 1898 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1899 | |
| 1900 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1901 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1902 | } |
| 1903 | |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 1904 | void TfLiteParser::ParseMean(size_t subgraphIndex, size_t operatorIndex) |
| 1905 | { |
| 1906 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1907 | |
| 1908 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1909 | |
| 1910 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1911 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1912 | |
| 1913 | armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]); |
| 1914 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1915 | |
| 1916 | armnn::MeanDescriptor desc; |
| 1917 | std::vector<unsigned int> axis(dimTensorInfo.GetNumElements()); |
| 1918 | ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes()); |
| 1919 | desc.m_Axis = axis; |
| 1920 | |
| 1921 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1922 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 1923 | |
| 1924 | desc.m_KeepDims = |
| 1925 | inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? |
| 1926 | true : false; |
| 1927 | |
| 1928 | auto layerName = boost::str(boost::format("Mean:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1929 | IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1930 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 1931 | |
| 1932 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1933 | |
| 1934 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1935 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1936 | |
| 1937 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1938 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1939 | } |
| 1940 | |
Darshan Patel | 83fcf98 | 2020-05-26 22:22:42 +0530 | [diff] [blame] | 1941 | void TfLiteParser::ParseNeg(size_t subgraphIndex, size_t operatorIndex) |
| 1942 | { |
| 1943 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1944 | |
| 1945 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1946 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1947 | |
| 1948 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1949 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1950 | |
| 1951 | auto layerName = boost::str(boost::format("Neg:%1%:%2%") % subgraphIndex % operatorIndex); |
| 1952 | armnn::ElementwiseUnaryDescriptor descriptor(armnn::UnaryOperation::Neg); |
| 1953 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str()); |
| 1954 | ARMNN_ASSERT(layer != nullptr); |
| 1955 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1956 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Darshan Patel | 83fcf98 | 2020-05-26 22:22:42 +0530 | [diff] [blame] | 1957 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1958 | |
| 1959 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1960 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1961 | |
| 1962 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1963 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1964 | } |
| 1965 | |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 1966 | void TfLiteParser::ParsePad(size_t subgraphIndex, size_t operatorIndex) |
| 1967 | { |
| 1968 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1969 | |
| 1970 | TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1971 | |
| 1972 | TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1973 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1974 | |
| 1975 | armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]); |
| 1976 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1977 | |
| 1978 | std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements()); |
| 1979 | ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes()); |
| 1980 | |
| 1981 | size_t step = 2; |
| 1982 | armnn::PadDescriptor desc; |
| 1983 | for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i) |
| 1984 | { |
| 1985 | desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]); |
| 1986 | } |
| 1987 | |
| 1988 | auto layerName = boost::str(boost::format("Pad:%1%:%2%") % subgraphIndex % operatorIndex); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1989 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1990 | |
| 1991 | IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str()); |
| 1992 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 1993 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1994 | |
| 1995 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1996 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1997 | |
| 1998 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1999 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2000 | } |
| 2001 | |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2002 | void TfLiteParser::ParseQuantize(size_t subgraphIndex, size_t operatorIndex) |
| 2003 | { |
| 2004 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2005 | |
| 2006 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2007 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2008 | |
| 2009 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2010 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2011 | |
| 2012 | auto layerName = boost::str(boost::format("Quantize:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2013 | |
| 2014 | IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2015 | ARMNN_ASSERT(layer != nullptr); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2016 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2017 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2018 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2019 | |
| 2020 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2021 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2022 | |
| 2023 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2024 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2025 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2026 | |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2027 | void TfLiteParser::ParseRelu(size_t subgraphIndex, size_t operatorIndex) |
| 2028 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2029 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | void TfLiteParser::ParseRelu6(size_t subgraphIndex, size_t operatorIndex) |
| 2033 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2034 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu); |
| 2035 | } |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2036 | |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2037 | void TfLiteParser::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex) |
| 2038 | { |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2039 | ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu); |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2040 | } |
| 2041 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2042 | void TfLiteParser::ParseLogistic(size_t subgraphIndex, size_t operatorIndex) |
| 2043 | { |
| 2044 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid); |
| 2045 | } |
| 2046 | |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 2047 | void TfLiteParser::ParseTanH(size_t subgraphIndex, size_t operatorIndex) |
| 2048 | { |
| 2049 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH); |
| 2050 | } |
| 2051 | |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2052 | void TfLiteParser::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex) |
| 2053 | { |
| 2054 | ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish); |
| 2055 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2056 | |
| 2057 | void TfLiteParser::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType) |
| 2058 | { |
| 2059 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2060 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 2061 | IgnoreUnused(operatorPtr); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2062 | |
| 2063 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2064 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2065 | |
| 2066 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2067 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2068 | |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2069 | auto layerName = str(boost::format("Activation:")); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2070 | ActivationDescriptor activationDesc; |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2071 | activationDesc.m_Function = activationType; |
| 2072 | |
| 2073 | switch (activationType) |
| 2074 | { |
| 2075 | case ActivationFunction::ReLu: |
| 2076 | { |
| 2077 | layerName += str(boost::format("RELU:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2078 | break; |
| 2079 | } |
| 2080 | case ActivationFunction::BoundedReLu: |
| 2081 | { |
| 2082 | layerName += str(boost::format("RELU6:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2083 | activationDesc.m_A = 6.0f; |
| 2084 | activationDesc.m_B = 0.0f; |
| 2085 | break; |
| 2086 | } |
| 2087 | case ActivationFunction::Sigmoid: |
| 2088 | { |
| 2089 | layerName += str(boost::format("SIGMOID:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2090 | break; |
| 2091 | } |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 2092 | case ActivationFunction::TanH: |
| 2093 | { |
| 2094 | layerName += str(boost::format("TANH:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2095 | activationDesc.m_A = 1.0f; |
| 2096 | activationDesc.m_B = 1.0f; |
| 2097 | break; |
| 2098 | } |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2099 | case ActivationFunction::LeakyReLu: |
| 2100 | { |
| 2101 | layerName += str(boost::format("LEAKYRELU:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2102 | const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions(); |
| 2103 | activationDesc.m_A = options->alpha; |
| 2104 | break; |
| 2105 | } |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2106 | case ActivationFunction::HardSwish: |
| 2107 | layerName += str(boost::format("HARDSWISH:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2108 | break; |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2109 | default: |
| 2110 | { |
| 2111 | throw ParseException( |
| 2112 | boost::str(boost::format("Unexpected ActivationFunction[%1%] when creating layerName " |
| 2113 | " %2% ") %static_cast<int>(activationType)% CHECK_LOCATION().AsString())); |
| 2114 | } |
| 2115 | } |
| 2116 | |
| 2117 | IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2118 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2119 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2120 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2121 | |
| 2122 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 2123 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2124 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2125 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2126 | |
| 2127 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 2128 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2129 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2130 | } |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2131 | armnn::TensorInfo TfLiteParser::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 2132 | const std::vector<int32_t> & targetDimsIn) |
| 2133 | { |
| 2134 | std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end()); |
| 2135 | const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1); |
| 2136 | |
| 2137 | if (stretchDim != targetDimsIn.end()) |
| 2138 | { |
| 2139 | if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end()) |
| 2140 | { |
| 2141 | throw ParseException( |
| 2142 | boost::str( |
| 2143 | boost::format("At most one component of shape can be -1 %1%") % CHECK_LOCATION().AsString())); |
| 2144 | } |
| 2145 | |
| 2146 | auto targetNumElements = |
| 2147 | boost::numeric_cast<unsigned int>( |
| 2148 | std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>())); |
| 2149 | |
| 2150 | auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim)); |
| 2151 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 2152 | } |
| 2153 | |
| 2154 | TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data()); |
| 2155 | |
| 2156 | TensorInfo reshapeInfo = inputTensorInfo; |
| 2157 | reshapeInfo.SetShape(outputShape); |
| 2158 | |
| 2159 | return reshapeInfo; |
| 2160 | } |
| 2161 | |
| 2162 | void TfLiteParser::ParseReshape(size_t subgraphIndex, size_t operatorIndex) |
| 2163 | { |
| 2164 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2165 | |
| 2166 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2167 | |
| 2168 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2169 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2170 | |
| 2171 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2172 | const auto * options = operatorPtr->builtin_options.AsReshapeOptions(); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2173 | auto layerName = boost::str(boost::format("Reshape:%1%:%2%") % subgraphIndex % operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2174 | |
| 2175 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2176 | armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2177 | CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0"); |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2178 | |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2179 | // Extracting new shape for the output |
| 2180 | // There are two ways it can be passed |
| 2181 | // * First is to define the target shape in the operator built-in options |
| 2182 | // * Second is to pass it as a second input tensor |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2183 | std::vector<int32_t> targetShape; |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2184 | bool targetShapeFound = false; |
| 2185 | // Check if built-in options were given |
| 2186 | if (options != nullptr) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2187 | { |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2188 | // make sure the parameter is given |
| 2189 | if (options->new_shape.empty() == false) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2190 | { |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2191 | targetShape = options->new_shape; |
| 2192 | targetShapeFound = true; |
Derek Lamberti | f4a953f | 2020-03-17 14:25:57 +0000 | [diff] [blame] | 2193 | } |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2194 | } |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2195 | |
| 2196 | // If there is no built-in option given or if the built-in new_shape parameter was empty |
| 2197 | if (!targetShapeFound) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2198 | { |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2199 | // Check for a second input tensor |
| 2200 | if (inputs.size() > 1 && inputs[1] != nullptr) |
| 2201 | { |
| 2202 | if (inputs[1]->is_variable) |
| 2203 | { |
| 2204 | ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported"); |
| 2205 | } |
| 2206 | |
| 2207 | if (inputs[1]->shape.size() != 1) |
| 2208 | { |
| 2209 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor"); |
| 2210 | } |
| 2211 | |
| 2212 | if (inputs[1]->type != tflite::TensorType_INT32) |
| 2213 | { |
| 2214 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type"); |
| 2215 | } |
| 2216 | |
| 2217 | // Extract target shape from input |
| 2218 | auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2219 | auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data()); |
| 2220 | for (int i=0; i < inputs[1]->shape[0]; ++i) |
| 2221 | { |
| 2222 | targetShape.push_back(values[i]); |
| 2223 | } |
| 2224 | } |
| 2225 | else |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2226 | { |
| 2227 | ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. " |
| 2228 | "At least one method required"); |
| 2229 | } |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2230 | } |
| 2231 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2232 | armnn::TensorInfo reshapeOutputTensorInfo = |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2233 | TfLiteParser::OutputShapeOfReshape(inputTensorInfo, targetShape); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2234 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2235 | // Check for valid input size and that reshape parameters equal output shape |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 2236 | const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape(); |
| 2237 | if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape)) |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2238 | { |
| 2239 | std::stringstream ss; |
| 2240 | ss << "New shape defined in reshape parameters " |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 2241 | << reshapeOutputTensorShape |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2242 | << " does not equal output shape " |
| 2243 | << actualOutputTensorInfo.GetShape() |
| 2244 | << ": " |
| 2245 | << CHECK_LOCATION().AsString(); |
| 2246 | throw ParseException(ss.str()); |
| 2247 | } |
| 2248 | |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2249 | ReshapeDescriptor reshapeDesc; |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2250 | reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape(); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2251 | |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2252 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2253 | ARMNN_ASSERT(layer != nullptr); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2254 | layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2255 | |
| 2256 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2257 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2258 | |
| 2259 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2260 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2261 | } |
| 2262 | |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2263 | void TfLiteParser::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex) |
| 2264 | { |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2265 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear); |
| 2266 | } |
| 2267 | |
| 2268 | void TfLiteParser::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex) |
| 2269 | { |
| 2270 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor); |
| 2271 | } |
| 2272 | |
| 2273 | void TfLiteParser::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod) |
| 2274 | { |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2275 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2276 | |
| 2277 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2278 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2279 | |
| 2280 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2281 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2282 | |
| 2283 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]); |
| 2284 | |
| 2285 | // Data for the parsed tensor args (size) must be stored locally. |
| 2286 | std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements()); |
| 2287 | |
| 2288 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2289 | ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
| 2290 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2291 | ResizeDescriptor desc; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2292 | desc.m_Method = resizeMethod; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2293 | desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]); |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2294 | desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]); |
| 2295 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2296 | |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2297 | auto layerName = str(boost::format("Resize:")); |
| 2298 | |
| 2299 | switch (resizeMethod) |
| 2300 | { |
| 2301 | case ResizeMethod::Bilinear: |
| 2302 | { |
| 2303 | layerName += str(boost::format("BILINEAR:%1%:%2%") % subgraphIndex % operatorIndex); |
Sang-Hoon Park | 820eb14 | 2020-01-08 10:25:24 +0000 | [diff] [blame] | 2304 | |
| 2305 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2306 | const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions(); |
| 2307 | |
David Monahan | 4a0c9b9 | 2020-05-30 09:48:39 +0100 | [diff] [blame] | 2308 | desc.m_AlignCorners = options->align_corners; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2309 | break; |
| 2310 | } |
| 2311 | case ResizeMethod::NearestNeighbor: |
| 2312 | { |
| 2313 | layerName += str(boost::format("NEARESTNEIGHBOR:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2314 | break; |
| 2315 | } |
| 2316 | default: |
| 2317 | { |
| 2318 | throw ParseException( |
| 2319 | boost::str(boost::format("Unexpected ResizeMethod[%1%] when creating layerName " |
| 2320 | " %2% ") %static_cast<int>(resizeMethod)% CHECK_LOCATION().AsString())); |
| 2321 | } |
| 2322 | } |
| 2323 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2324 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2325 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2326 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 2327 | |
| 2328 | IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str()); |
| 2329 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2330 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2331 | |
| 2332 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2333 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2334 | |
| 2335 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2336 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2337 | } |
| 2338 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2339 | void TfLiteParser::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex) |
| 2340 | { |
| 2341 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2342 | |
| 2343 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2344 | const auto * options = operatorPtr->builtin_options.AsConcatenationOptions(); |
| 2345 | |
| 2346 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2347 | |
| 2348 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2349 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2350 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2351 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2352 | unsigned int numConcatView = static_cast<unsigned int>(inputs.size()); |
| 2353 | uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions(); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2354 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2355 | const unsigned int concatDimInput = static_cast<unsigned int>( |
| 2356 | (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank)); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2357 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2358 | OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank); |
| 2359 | concatDescriptor.SetConcatAxis(concatDimInput); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2360 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2361 | unsigned int mergeDimOrigin = 0; |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2362 | |
| 2363 | for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex) |
| 2364 | { |
| 2365 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]); |
| 2366 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2367 | // This set up concatDescriptor view origin |
| 2368 | armnnUtils::ProcessConcatInputTensorInfo( |
| 2369 | inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | auto layerName = boost::str(boost::format("Concatenation:%1%:%2%") % subgraphIndex % operatorIndex); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2373 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2374 | |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 2375 | IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2376 | ARMNN_ASSERT(layer != nullptr); |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2377 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2378 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2379 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2380 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2381 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2382 | // add fused activation layer |
| 2383 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2384 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2385 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2386 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2387 | } |
| 2388 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2389 | void TfLiteParser::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex) |
| 2390 | { |
| 2391 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2392 | |
| 2393 | const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2394 | const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions(); |
| 2395 | |
| 2396 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2397 | |
| 2398 | FullyConnectedDescriptor desc; |
| 2399 | desc.m_BiasEnabled = false; |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 2400 | desc.m_TransposeWeightMatrix = true; |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2401 | |
| 2402 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2403 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2404 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2405 | |
| 2406 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 2407 | |
| 2408 | // Fully Connected Layer accepts two dimensional weights input |
| 2409 | int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions()); |
| 2410 | if (weightsDimension != 2) |
| 2411 | { |
| 2412 | throw ParseException( |
| 2413 | boost::str( |
| 2414 | boost::format( |
| 2415 | "Dimension %1% for Fully Connected weights is not supported by Armnn. " |
| 2416 | "Node %2%") |
| 2417 | % weightsDimension |
| 2418 | % CHECK_LOCATION().AsString())); |
| 2419 | } |
| 2420 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2421 | auto filterTensorAndData = CreateConstTensor(inputs[1], |
| 2422 | filterTensorInfo, |
| 2423 | armnn::Optional<armnn::PermutationVector&>()); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 2424 | armnn::IConnectableLayer* layer = nullptr; |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2425 | auto layerName = boost::str(boost::format("FullyConnected:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2426 | |
| 2427 | if (inputs.size() == 3) |
| 2428 | { |
| 2429 | desc.m_BiasEnabled = true; |
| 2430 | TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 2431 | auto biasTensorAndData = CreateConstTensor(inputs[2], |
| 2432 | biasTensorInfo, |
| 2433 | armnn::Optional<armnn::PermutationVector&>()); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2434 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 2435 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 2436 | Optional<ConstTensor>(biasTensorAndData.first), |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2437 | layerName.c_str()); |
| 2438 | } |
| 2439 | else |
| 2440 | { |
| 2441 | layer = m_Network->AddFullyConnectedLayer(desc, |
| 2442 | filterTensorAndData.first, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 2443 | EmptyOptional(), |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2444 | layerName.c_str()); |
| 2445 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2446 | ARMNN_ASSERT(layer != nullptr); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2447 | |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2448 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2449 | |
| 2450 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2451 | |
| 2452 | if (inputTensorInfo.GetNumDimensions() > 2) |
| 2453 | { |
| 2454 | // Add reshape to flatten to 2D [batch_size, input_size], |
| 2455 | // where "input_size" corresponds to the number of inputs to the layer, |
| 2456 | // matching the second dimension of weights, |
| 2457 | // and "batch_size" is calculated by dividing the number of elements by "input_size". |
| 2458 | std::vector<unsigned int> reshapedDimensions(2); |
| 2459 | reshapedDimensions[1] = filterTensorInfo.GetShape()[1]; |
| 2460 | reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1]; |
| 2461 | |
| 2462 | if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0) |
| 2463 | { |
| 2464 | throw ParseException( |
| 2465 | boost::str( |
| 2466 | boost::format( |
| 2467 | "Failed to deduce input tensor shape from filter size %1%") |
| 2468 | % reshapedDimensions[1] |
| 2469 | % CHECK_LOCATION().AsString())); |
| 2470 | } |
| 2471 | |
| 2472 | armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]); |
| 2473 | reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() }); |
| 2474 | |
| 2475 | std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 2476 | armnn::ReshapeDescriptor desc; |
| 2477 | desc.m_TargetShape = reshapedTensorInfo.GetShape(); |
| 2478 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 2479 | |
| 2480 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo); |
| 2481 | reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 2482 | |
| 2483 | RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]}); |
| 2484 | } |
| 2485 | else |
| 2486 | { |
| 2487 | // register the input connection slot for the layer |
| 2488 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2489 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2490 | } |
| 2491 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2492 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2493 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2494 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2495 | // we need to add the activation layer and fortunately we don't need to care about the data layout |
| 2496 | armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0, |
| 2497 | options->fused_activation_function); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2498 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2499 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 2500 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2501 | RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]}); |
| 2502 | } |
| 2503 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2504 | void TfLiteParser::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex) |
| 2505 | { |
| 2506 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2507 | |
| 2508 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2509 | |
| 2510 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2511 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2512 | CHECK_VALID_SIZE(outputs.size(), 4); |
| 2513 | |
| 2514 | // Obtain custom options from flexbuffers |
| 2515 | auto custom_options = operatorPtr->custom_options; |
| 2516 | const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap(); |
| 2517 | |
| 2518 | // Obtain descriptor information from tf lite |
| 2519 | DetectionPostProcessDescriptor desc; |
| 2520 | desc.m_MaxDetections = m["max_detections"].AsUInt32(); |
| 2521 | desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32(); |
| 2522 | desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat(); |
| 2523 | desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat(); |
| 2524 | desc.m_NumClasses = m["num_classes"].AsUInt32(); |
| 2525 | desc.m_ScaleH = m["h_scale"].AsFloat(); |
| 2526 | desc.m_ScaleW = m["w_scale"].AsFloat(); |
| 2527 | desc.m_ScaleX = m["x_scale"].AsFloat(); |
| 2528 | desc.m_ScaleY = m["y_scale"].AsFloat(); |
| 2529 | |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2530 | if (!(m["use_regular_nms"].IsNull())) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2531 | { |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2532 | desc.m_UseRegularNms = m["use_regular_nms"].AsBool(); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2533 | } |
| 2534 | if (!(m["detections_per_class"].IsNull())) |
| 2535 | { |
| 2536 | desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32(); |
| 2537 | } |
| 2538 | |
| 2539 | if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f) |
| 2540 | { |
| 2541 | throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold " |
| 2542 | "must be positive and less than or equal to 1."); |
| 2543 | } |
| 2544 | |
| 2545 | armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]); |
| 2546 | auto anchorTensorAndData = CreateConstTensor(inputs[2], anchorTensorInfo, |
| 2547 | armnn::Optional<armnn::PermutationVector&>()); |
| 2548 | |
| 2549 | auto layerName = boost::str(boost::format("DetectionPostProcess:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2550 | IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData.first, |
| 2551 | layerName.c_str()); |
| 2552 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2553 | ARMNN_ASSERT(layer != nullptr); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2554 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2555 | // The model does not specify the output shapes. |
| 2556 | // The output shapes are calculated from the max_detection and max_classes_per_detection. |
| 2557 | unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection; |
| 2558 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 }); |
| 2559 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2560 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2561 | m_OverridenOutputShapes.push_back({ 1 }); |
| 2562 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2563 | for (unsigned int i = 0 ; i < outputs.size() ; ++i) |
| 2564 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2565 | armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2566 | layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo); |
| 2567 | } |
| 2568 | |
| 2569 | // Register the input connection slots for the layer, connections are made after all layers have been created |
| 2570 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2571 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2572 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 2573 | |
| 2574 | // Register the output connection slots for the layer, connections are made after all layers have been created |
| 2575 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2576 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0], |
| 2577 | outputTensorIndexes[1], |
| 2578 | outputTensorIndexes[2], |
| 2579 | outputTensorIndexes[3]}); |
| 2580 | } |
| 2581 | |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2582 | /// The TfLite Pack operator is equivalent to the ArmNN Stack operator |
| 2583 | void TfLiteParser::ParsePack(size_t subgraphIndex, size_t operatorIndex) |
| 2584 | { |
| 2585 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2586 | |
| 2587 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2588 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2589 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2590 | |
| 2591 | if (inputs.size() < 1) |
| 2592 | { |
| 2593 | throw ParseException("Pack must have at least one input."); |
| 2594 | } |
| 2595 | |
| 2596 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2597 | const auto* options = operatorPtr->builtin_options.AsPackOptions(); |
| 2598 | |
| 2599 | StackDescriptor desc; |
| 2600 | desc.m_Axis = static_cast<uint32_t>(options->axis); |
| 2601 | desc.m_NumInputs = static_cast<uint32_t>(inputs.size()); |
| 2602 | |
| 2603 | // Use the tensor shape of the first input as the "correct" input shape in the descriptor |
| 2604 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2605 | desc.m_InputShape = inputTensorInfo.GetShape(); |
| 2606 | |
| 2607 | auto layerName = boost::str(boost::format("Pack:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2608 | IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str()); |
| 2609 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2610 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2611 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2612 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2613 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2614 | |
| 2615 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2616 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
| 2617 | |
| 2618 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2619 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2620 | } |
| 2621 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2622 | void TfLiteParser::ParseUnpack(size_t subgraphIndex, size_t operatorIndex) |
| 2623 | { |
| 2624 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2625 | |
| 2626 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2627 | const auto * options = operatorPtr->builtin_options.AsUnpackOptions(); |
| 2628 | |
| 2629 | // This unpackAxis indicates the axis to unpack |
| 2630 | const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis); |
| 2631 | |
| 2632 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2633 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2634 | |
| 2635 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2636 | |
| 2637 | if (unpackAxis >= inputTensorInfo.GetNumDimensions()) |
| 2638 | { |
| 2639 | throw ParseException( |
| 2640 | boost::str( |
| 2641 | boost::format( |
| 2642 | "The unpack axis: %1% cannot be greater than or equal to " |
| 2643 | "the number of input dimension %2% %3%") |
| 2644 | % unpackAxis |
| 2645 | % inputTensorInfo.GetNumDimensions() |
| 2646 | % CHECK_LOCATION().AsString())); |
| 2647 | } |
| 2648 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2649 | unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num); |
| 2650 | // If num is not defined, automatically infer from the length of the dimension axis. |
| 2651 | if(unpackNum == 0) |
| 2652 | { |
| 2653 | unpackNum = inputTensorInfo.GetShape()[unpackAxis]; |
| 2654 | } |
| 2655 | |
| 2656 | // If unpack number cannot be inferred and is still zero, throw ParseException. |
| 2657 | if(unpackNum == 0) |
| 2658 | { |
| 2659 | throw ParseException("Number to unpack must greater than zero."); |
| 2660 | } |
| 2661 | |
| 2662 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2663 | CHECK_VALID_SIZE(outputs.size(), unpackNum); |
| 2664 | |
| 2665 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
| 2666 | std::vector<unsigned int> unpackDimSizes(inputDimSize); |
| 2667 | |
| 2668 | // Add current input shape to unpackDimSizes |
| 2669 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 2670 | { |
| 2671 | unpackDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 2672 | } |
| 2673 | |
| 2674 | if (unpackDimSizes[unpackAxis] != unpackNum) |
| 2675 | { |
| 2676 | throw ParseException("Number to unpack must be the same as length of the dimension to " |
| 2677 | "unpack along."); |
| 2678 | } |
| 2679 | |
| 2680 | unpackDimSizes[unpackAxis] /= unpackNum; |
| 2681 | |
| 2682 | SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size())); |
| 2683 | for (unsigned int j = 0; j < unpackNum; ++j) |
| 2684 | { |
| 2685 | // Set the size of the views. |
| 2686 | for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx) |
| 2687 | { |
| 2688 | splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]); |
| 2689 | } |
| 2690 | splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j); |
| 2691 | } |
| 2692 | |
| 2693 | auto layerName = boost::str(boost::format("Unpack:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2694 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2695 | ARMNN_ASSERT(layer != nullptr); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2696 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2697 | TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()), |
| 2698 | unpackDimSizes.data()); |
| 2699 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2700 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2701 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2702 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2703 | // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter. |
| 2704 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 2705 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2706 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2707 | std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName()); |
| 2708 | armnn::ReshapeDescriptor desc; |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2709 | desc.m_TargetShape = outputTensorInfo.GetShape(); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2710 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 2711 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2712 | layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape, |
| 2713 | outputTensorInfo.GetDataType(), |
| 2714 | outputTensorInfo.GetQuantizationScale(), |
| 2715 | outputTensorInfo.GetQuantizationOffset())); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2716 | layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0)); |
| 2717 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 2718 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 2719 | |
| 2720 | uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]); |
| 2721 | armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0)); |
| 2722 | RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot); |
| 2723 | } |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2726 | void TfLiteParser::ParseSplit(size_t subgraphIndex, size_t operatorIndex) |
| 2727 | { |
| 2728 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2729 | |
| 2730 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2731 | const auto * options = operatorPtr->builtin_options.AsSplitOptions(); |
| 2732 | |
| 2733 | const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits); |
| 2734 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 2735 | // If number of splits cannot be inferred and is zero, throw ParseException. |
| 2736 | if(numSplits == 0) |
| 2737 | { |
| 2738 | throw ParseException("Number to splits must greater than zero."); |
| 2739 | } |
| 2740 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2741 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2742 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2743 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2744 | CHECK_VALID_SIZE(outputs.size(), numSplits); |
| 2745 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2746 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]); |
| 2747 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2748 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2749 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
| 2750 | std::vector<unsigned int> axisData(axisTensorInfo.GetNumElements()); |
| 2751 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
| 2752 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2753 | ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2754 | const unsigned int splitDim = axisData[0]; |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2755 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2756 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2757 | if (inputDimSize > MaxNumOfTensorDimensions) |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2758 | { |
| 2759 | throw ParseException( |
| 2760 | boost::str( |
| 2761 | boost::format( |
| 2762 | "The number of dimensions: %1% for input tensors of the " |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2763 | "split op cannot be greater than %2% %3%") |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2764 | % inputTensorInfo.GetNumDimensions() |
| 2765 | % MaxNumOfTensorDimensions |
| 2766 | % CHECK_LOCATION().AsString())); |
| 2767 | } |
| 2768 | |
| 2769 | std::vector<unsigned int> splitterDimSizes(inputDimSize); |
| 2770 | |
| 2771 | // Add current input shape to splitterDimSizes |
| 2772 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 2773 | { |
| 2774 | splitterDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 2775 | } |
| 2776 | |
| 2777 | if (splitterDimSizes[splitDim] % numSplits != 0) |
| 2778 | { |
| 2779 | throw ParseException("Number of splits must evenly divide the dimension"); |
| 2780 | } |
| 2781 | splitterDimSizes[splitDim] /= numSplits; |
| 2782 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2783 | SplitterDescriptor splitDesc(numSplits, inputDimSize); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2784 | for (unsigned int j = 0; j < numSplits; ++j) |
| 2785 | { |
| 2786 | // Set the size of the views. |
| 2787 | for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx) |
| 2788 | { |
| 2789 | splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]); |
| 2790 | } |
| 2791 | splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j); |
| 2792 | } |
| 2793 | |
| 2794 | auto layerName = boost::str(boost::format("Split:%1%:%2%") % subgraphIndex % operatorIndex); |
| 2795 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2796 | ARMNN_ASSERT(layer != nullptr); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2797 | |
| 2798 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 2799 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]}); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2800 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2801 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 2802 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2803 | armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true); |
Francis Murtagh | 98d6b3d | 2019-10-21 10:52:54 +0100 | [diff] [blame] | 2804 | layer->GetOutputSlot(k).SetTensorInfo(tensorInfo); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2808 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2809 | } |
| 2810 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2811 | unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn) |
| 2812 | { |
| 2813 | int numDims = armnn::numeric_cast<int>(numDimsIn); |
| 2814 | int v = idx < 0 ? numDims + idx : idx; |
| 2815 | ARMNN_ASSERT(v >= 0); |
| 2816 | ARMNN_ASSERT(v < numDims); |
| 2817 | |
| 2818 | return static_cast<unsigned int>(v); |
| 2819 | } |
| 2820 | |
| 2821 | void TfLiteParser::ParseSplitV(size_t subgraphIndex, size_t operatorIndex) |
| 2822 | { |
| 2823 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2824 | |
| 2825 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2826 | const auto * options = operatorPtr->builtin_options.AsSplitVOptions(); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2827 | |
| 2828 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2829 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 2830 | |
| 2831 | auto& inputTensor = inputs[0]; |
| 2832 | auto& splitsTensor = inputs[1]; |
| 2833 | auto& axisTensor = inputs[2]; |
| 2834 | |
| 2835 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor); |
| 2836 | armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor); |
| 2837 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor); |
| 2838 | ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1); |
| 2839 | |
| 2840 | // Inputs |
| 2841 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
| 2842 | if (inputDimSize > MaxNumOfTensorDimensions) |
| 2843 | { |
| 2844 | throw ParseException( |
| 2845 | boost::str( |
| 2846 | boost::format( |
| 2847 | "The number of dimensions: %1% for input tensors of the " |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 2848 | "SplitV op cannot be greater than %2% %3%") |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2849 | % inputTensorInfo.GetNumDimensions() |
| 2850 | % MaxNumOfTensorDimensions |
| 2851 | % CHECK_LOCATION().AsString())); |
| 2852 | } |
| 2853 | |
| 2854 | // Get split axis |
| 2855 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer); |
| 2856 | std::vector<int> axisData(axisTensorInfo.GetNumElements()); |
| 2857 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
| 2858 | const unsigned int splitDim = ComputeWrappedIndex(axisData[0], inputTensorInfo.GetNumDimensions()); |
| 2859 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2860 | // Set split sizes |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2861 | CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2862 | unsigned int numSplits{0}; |
| 2863 | |
| 2864 | if(options) |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2865 | { |
| 2866 | numSplits = CHECKED_NON_NEGATIVE(options->num_splits); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2867 | } |
| 2868 | else |
| 2869 | { |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2870 | numSplits = splitsInfo.GetNumElements(); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | if (numSplits <=0) |
| 2874 | { |
| 2875 | throw ParseException("SplitV has invalid number of splits"); |
| 2876 | } |
| 2877 | |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 2878 | std::vector<int> splitsData(numSplits); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2879 | BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer); |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 2880 | ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes()); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2881 | |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 2882 | unsigned int idx = 0; |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2883 | int numInferred{0}; |
| 2884 | unsigned int inferIdx{0}; |
| 2885 | int splitSum{0}; |
| 2886 | for (auto split : splitsData) |
| 2887 | { |
| 2888 | if (split < 0) |
| 2889 | { |
| 2890 | numInferred++; |
| 2891 | inferIdx = idx; |
| 2892 | } |
| 2893 | else |
| 2894 | { |
| 2895 | splitSum += split; |
| 2896 | } |
| 2897 | idx++; |
| 2898 | } |
| 2899 | // Check for inferred Axis |
| 2900 | if (numInferred == 0) |
| 2901 | { |
| 2902 | if (splitSum != numeric_cast<int>(inputTensorInfo.GetShape()[splitDim])) |
| 2903 | { |
| 2904 | throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim."); |
| 2905 | } |
| 2906 | } |
| 2907 | else if (numInferred == 1) |
| 2908 | { |
| 2909 | splitsData[inferIdx] = numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum; |
| 2910 | } |
| 2911 | else |
| 2912 | { |
| 2913 | throw ParseException("Cannot infer split size for more than one split"); |
| 2914 | } |
| 2915 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2916 | //Ouput size validation |
| 2917 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2918 | CHECK_VALID_SIZE(outputs.size(), numSplits); |
| 2919 | |
| 2920 | // Setup Armnn descriptor |
| 2921 | SplitterDescriptor splitDesc(numSplits, inputDimSize); |
| 2922 | unsigned int accumSplit = 0; |
| 2923 | for (unsigned int j = 0; j < numSplits; ++j) |
| 2924 | { |
| 2925 | unsigned int splitSize = numeric_cast<unsigned int>(splitsData[j]); |
| 2926 | |
| 2927 | // Set the size of the views. |
| 2928 | for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx) |
| 2929 | { |
| 2930 | unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx]; |
| 2931 | if (dimIdx == splitDim) |
| 2932 | { |
| 2933 | dimSize = splitSize; |
| 2934 | } |
| 2935 | splitDesc.SetViewSize(j, dimIdx, dimSize); |
| 2936 | } |
| 2937 | |
| 2938 | splitDesc.SetViewOriginCoord(j, splitDim, accumSplit); |
| 2939 | accumSplit += splitSize; |
| 2940 | } |
| 2941 | |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 2942 | auto layerName = boost::str(boost::format("SplitV:%1%:%2%") % subgraphIndex % operatorIndex); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2943 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2944 | ARMNN_ASSERT(layer != nullptr); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2945 | |
| 2946 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2947 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2948 | |
| 2949 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 2950 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2951 | armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 2952 | layer->GetOutputSlot(k).SetTensorInfo(tensorInfo); |
| 2953 | } |
| 2954 | |
| 2955 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2956 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2957 | } |
| 2958 | |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2959 | armnn::IConnectableLayer* TfLiteParser::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer, |
| 2960 | unsigned int outputSlot, |
| 2961 | tflite::ActivationFunctionType activationType) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 2962 | { |
| 2963 | ActivationDescriptor activationDesc; |
| 2964 | std::string layerName = prevLayer->GetName(); |
| 2965 | |
| 2966 | switch(activationType) |
| 2967 | { |
| 2968 | case tflite::ActivationFunctionType_NONE: |
| 2969 | { |
| 2970 | // this is a no-op: return previous layer |
| 2971 | return prevLayer; |
| 2972 | } |
| 2973 | case tflite::ActivationFunctionType_RELU: |
| 2974 | { |
| 2975 | activationDesc.m_Function = ActivationFunction::ReLu; |
| 2976 | layerName += ":RELU"; |
| 2977 | break; |
| 2978 | } |
| 2979 | case tflite::ActivationFunctionType_RELU6: |
| 2980 | { |
| 2981 | activationDesc.m_Function = ActivationFunction::BoundedReLu; |
| 2982 | activationDesc.m_A = 6.0f; |
| 2983 | activationDesc.m_B = 0.0f; |
| 2984 | layerName += ":RELU6"; |
| 2985 | break; |
| 2986 | } |
| 2987 | case tflite::ActivationFunctionType_TANH: |
| 2988 | { |
| 2989 | activationDesc.m_Function = ActivationFunction::TanH; |
| 2990 | activationDesc.m_A = 1.0f; |
| 2991 | activationDesc.m_B = 1.0f; |
| 2992 | layerName += ":TANH"; |
| 2993 | break; |
| 2994 | } |
| 2995 | |
| 2996 | // I only put these here as a reminder what others we could support |
| 2997 | case tflite::ActivationFunctionType_RELU_N1_TO_1: |
| 2998 | case tflite::ActivationFunctionType_SIGN_BIT: |
| 2999 | default: |
| 3000 | { |
| 3001 | throw ParseException( |
| 3002 | boost::str( |
| 3003 | boost::format("TfLite parser doesn't suppport fused activation: " |
| 3004 | "%1%/%2% %3% ") % |
| 3005 | activationType % |
| 3006 | tflite::EnumNameActivationFunctionType(activationType) % |
| 3007 | CHECK_LOCATION().AsString())); |
| 3008 | |
| 3009 | } |
| 3010 | } |
| 3011 | |
| 3012 | IConnectableLayer* activationLayer = |
| 3013 | m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
| 3014 | |
| 3015 | auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot); |
| 3016 | prevOutputSlot.Connect(activationLayer->GetInputSlot(0)); |
| 3017 | activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo()); |
| 3018 | return activationLayer; |
| 3019 | } |
| 3020 | |
| 3021 | TfLiteParser::ModelPtr TfLiteParser::LoadModelFromFile(const char * fileName) |
| 3022 | { |
| 3023 | if (fileName == nullptr) |
| 3024 | { |
| 3025 | throw InvalidArgumentException(boost::str(boost::format("Invalid (null) file name %1%") % |
| 3026 | CHECK_LOCATION().AsString())); |
| 3027 | } |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 3028 | std::error_code errorCode; |
| 3029 | fs::path pathToFile(fileName); |
| 3030 | if (!fs::exists(pathToFile, errorCode)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3031 | { |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 3032 | std::string locationString = CHECK_LOCATION().AsString(); |
| 3033 | std::string msg = boost::str(boost::format("Cannot find the file (%1%) errorCode: %2% %3%") % |
| 3034 | fileName % |
| 3035 | errorCode % |
| 3036 | locationString); |
| 3037 | throw FileNotFoundException(msg); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3038 | } |
| 3039 | std::ifstream file(fileName, std::ios::binary); |
| 3040 | std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
| 3041 | return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()), |
| 3042 | fileContent.size()); |
| 3043 | } |
| 3044 | |
| 3045 | TfLiteParser::ModelPtr TfLiteParser::LoadModelFromBinary(const uint8_t * binaryContent, size_t len) |
| 3046 | { |
| 3047 | if (binaryContent == nullptr) |
| 3048 | { |
| 3049 | throw InvalidArgumentException(boost::str(boost::format("Invalid (null) binary content %1%") % |
| 3050 | CHECK_LOCATION().AsString())); |
| 3051 | } |
| 3052 | flatbuffers::Verifier verifier(binaryContent, len); |
| 3053 | if (verifier.VerifyBuffer<tflite::Model>() == false) |
| 3054 | { |
| 3055 | throw ParseException( |
| 3056 | boost::str(boost::format("Buffer doesn't conform to the expected Tensorflow Lite " |
| 3057 | "flatbuffers format. size:%1% %2%") % |
| 3058 | len % |
| 3059 | CHECK_LOCATION().AsString())); |
| 3060 | } |
| 3061 | return tflite::UnPackModel(binaryContent); |
| 3062 | } |
| 3063 | |
| 3064 | TfLiteParser::TensorRawPtrVector TfLiteParser::GetInputs(const ModelPtr & model, |
| 3065 | size_t subgraphIndex, |
| 3066 | size_t operatorIndex) |
| 3067 | { |
| 3068 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 3069 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3070 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3071 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3072 | |
| 3073 | size_t inputCount = operatorPtr->inputs.size(); |
| 3074 | TensorRawPtrVector result(inputCount); |
| 3075 | for (size_t i=0; i<inputCount; ++i) |
| 3076 | { |
| 3077 | uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3078 | result[i] = subgraphPtr->tensors[inputId].get(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3079 | } |
| 3080 | return result; |
| 3081 | } |
| 3082 | |
| 3083 | TfLiteParser::TensorRawPtrVector TfLiteParser::GetOutputs(const ModelPtr & model, |
| 3084 | size_t subgraphIndex, |
| 3085 | size_t operatorIndex) |
| 3086 | { |
| 3087 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 3088 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3089 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3090 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3091 | |
| 3092 | size_t outputCount = operatorPtr->outputs.size(); |
| 3093 | TensorRawPtrVector result(outputCount); |
| 3094 | for (size_t i=0; i<outputCount; ++i) |
| 3095 | { |
| 3096 | uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]); |
| 3097 | CHECK_TENSOR(model, subgraphIndex, outputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3098 | result[i] = subgraphPtr->tensors[outputId].get(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3099 | } |
| 3100 | return result; |
| 3101 | } |
| 3102 | |
| 3103 | TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphInputs(const ModelPtr & model, |
| 3104 | size_t subgraphIndex) |
| 3105 | { |
| 3106 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3107 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3108 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3109 | size_t inputCount = subgraphPtr->inputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3110 | TensorIdRawPtrVector result(inputCount); |
| 3111 | for (size_t i=0; i<inputCount; ++i) |
| 3112 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3113 | uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3114 | CHECK_TENSOR(model, subgraphIndex, inputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3115 | result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3116 | } |
| 3117 | return result; |
| 3118 | } |
| 3119 | |
| 3120 | TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphOutputs(const ModelPtr & model, |
| 3121 | size_t subgraphIndex) |
| 3122 | { |
| 3123 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3124 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3125 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3126 | size_t outputCount = subgraphPtr->outputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3127 | TensorIdRawPtrVector result(outputCount); |
| 3128 | for (size_t i=0; i<outputCount; ++i) |
| 3129 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3130 | uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]); |
| 3131 | result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3132 | } |
| 3133 | return result; |
| 3134 | } |
| 3135 | |
| 3136 | std::vector<int32_t>& TfLiteParser::GetInputTensorIds(const ModelPtr& model, |
| 3137 | size_t subgraphIndex, |
| 3138 | size_t operatorIndex) |
| 3139 | { |
| 3140 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3141 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3142 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3143 | return operatorPtr->inputs; |
| 3144 | } |
| 3145 | |
| 3146 | std::vector<int32_t>& TfLiteParser::GetOutputTensorIds(const ModelPtr& model, |
| 3147 | size_t subgraphIndex, |
| 3148 | size_t operatorIndex) |
| 3149 | { |
| 3150 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3151 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3152 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3153 | return operatorPtr->outputs; |
| 3154 | } |
| 3155 | |
| 3156 | void TfLiteParser::RegisterInputSlots(size_t subgraphIndex, |
| 3157 | size_t operatorIndex, |
| 3158 | IConnectableLayer* layer, |
| 3159 | const std::vector<unsigned int>& tensorIndexes) |
| 3160 | { |
| 3161 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 3162 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3163 | if (tensorIndexes.size() != layer->GetNumInputSlots()) |
| 3164 | { |
| 3165 | throw ParseException( |
| 3166 | boost::str(boost::format("The number of tensor inputs (%1%) does not match the number expected (%2%)" |
| 3167 | " for subgraph:%3% operator index:%4% %5%") % |
| 3168 | tensorIndexes.size() % |
| 3169 | layer->GetNumInputSlots() % |
| 3170 | subgraphIndex % |
| 3171 | operatorIndex % |
| 3172 | CHECK_LOCATION().AsString())); |
| 3173 | } |
| 3174 | |
| 3175 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex) |
| 3176 | { |
| 3177 | unsigned int tensorIndex = tensorIndexes[slotIndex]; |
| 3178 | armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex)); |
| 3179 | RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot); |
| 3180 | } |
| 3181 | } |
| 3182 | |
| 3183 | void TfLiteParser::RegisterOutputSlots(size_t subgraphIndex, |
| 3184 | size_t operatorIndex, |
| 3185 | IConnectableLayer* layer, |
| 3186 | const std::vector<unsigned int>& tensorIndexes) |
| 3187 | { |
| 3188 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 3189 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3190 | if (tensorIndexes.size() != layer->GetNumOutputSlots()) |
| 3191 | { |
| 3192 | throw ParseException( |
| 3193 | boost::str(boost::format("The number of tensor outputs (%1%) does not match the number expected (%2%)" |
| 3194 | " for subgraph:%3% operator index:%4% %5%") % |
| 3195 | tensorIndexes.size() % |
| 3196 | layer->GetNumOutputSlots() % |
| 3197 | subgraphIndex % |
| 3198 | operatorIndex % |
| 3199 | CHECK_LOCATION().AsString())); |
| 3200 | } |
| 3201 | |
| 3202 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex) |
| 3203 | { |
| 3204 | unsigned int tensorIndex = tensorIndexes[slotIndex]; |
| 3205 | armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex)); |
| 3206 | RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot); |
| 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | void TfLiteParser::SetupInputLayers(size_t subgraphIndex) |
| 3211 | { |
| 3212 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 3213 | |
| 3214 | auto inputs = GetSubgraphInputs(m_Model, subgraphIndex); |
| 3215 | for (auto const & tensorIdAndPtr : inputs) |
| 3216 | { |
| 3217 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 3218 | IConnectableLayer* layer = |
| 3219 | m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 3220 | |
| 3221 | auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second); |
| 3222 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 3223 | |
| 3224 | RegisterOutputSlots(subgraphIndex, |
| 3225 | VIRTUAL_OPERATOR_ID, |
| 3226 | layer, |
| 3227 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 3228 | } |
| 3229 | } |
| 3230 | |
| 3231 | void TfLiteParser::SetupOutputLayers(size_t subgraphIndex) |
| 3232 | { |
| 3233 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 3234 | |
| 3235 | auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex); |
| 3236 | for (auto const & tensorIdAndPtr : outputs) |
| 3237 | { |
| 3238 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 3239 | IConnectableLayer* layer = |
| 3240 | m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 3241 | |
| 3242 | RegisterInputSlots(subgraphIndex, |
| 3243 | VIRTUAL_OPERATOR_ID, |
| 3244 | layer, |
| 3245 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 3246 | } |
| 3247 | } |
| 3248 | |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 3249 | void TfLiteParser::SetupConstantLayers(size_t subgraphIndex) |
| 3250 | { |
| 3251 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 3252 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3253 | const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex]; |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 3254 | for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
| 3255 | { |
| 3256 | for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 3257 | { |
| 3258 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr && |
| 3259 | m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0) |
| 3260 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3261 | TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get(); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 3262 | armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr); |
| 3263 | auto tensorAndData = CreateConstTensor(tensorPtr, |
| 3264 | tensorInfo, |
| 3265 | armnn::Optional<armnn::PermutationVector&>()); |
| 3266 | |
| 3267 | std::string layerName = boost::str(boost::format("Constant:%1%") % tensorPtr->name); |
| 3268 | IConnectableLayer *layer = |
| 3269 | m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str()); |
| 3270 | |
| 3271 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 3272 | RegisterOutputSlots(subgraphIndex, |
| 3273 | VIRTUAL_OPERATOR_ID, |
| 3274 | layer, |
| 3275 | { tensorIndex }); |
| 3276 | |
| 3277 | } |
| 3278 | } |
| 3279 | } |
| 3280 | } |
| 3281 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3282 | // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
| 3283 | TfLiteParser::BufferRawPtr TfLiteParser::GetBuffer(const ModelPtr& model, size_t bufferIndex) |
| 3284 | { |
| 3285 | CHECK_BUFFER(model, bufferIndex); |
| 3286 | return model->buffers[bufferIndex].get(); |
| 3287 | } |
| 3288 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 3289 | template<typename T> |
| 3290 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 3291 | TfLiteParser::CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr, |
| 3292 | TfLiteParser::TensorRawPtr tensorPtr, |
| 3293 | armnn::TensorInfo& tensorInfo, |
| 3294 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
| 3295 | { |
| 3296 | auto constData = CreateConstTensorImpl<T>(bufferPtr, |
| 3297 | tensorPtr, |
| 3298 | tensorInfo, |
| 3299 | permutationVector); |
| 3300 | TfLiteParser::SupportedDataStorage storage(std::move(constData.second)); |
| 3301 | return std::make_pair(constData.first, std::move(storage)); |
| 3302 | } |
| 3303 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3304 | std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage> |
| 3305 | TfLiteParser::CreateConstTensor(TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 3306 | armnn::TensorInfo& tensorInfo, |
| 3307 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3308 | { |
| 3309 | CHECK_TENSOR_PTR(tensorPtr); |
| 3310 | auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer); |
| 3311 | CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer); |
| 3312 | |
| 3313 | switch (tensorInfo.GetDataType()) |
| 3314 | { |
| 3315 | case armnn::DataType::Float32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 3316 | return CreateConstTensorAndStoreData<float>(bufferPtr, |
| 3317 | tensorPtr, |
| 3318 | tensorInfo, |
| 3319 | permutationVector); |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 3320 | case armnn::DataType::QAsymmU8: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 3321 | return CreateConstTensorAndStoreData<uint8_t>(bufferPtr, |
| 3322 | tensorPtr, |
| 3323 | tensorInfo, |
| 3324 | permutationVector); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3325 | case armnn::DataType::QSymmS8: |
| 3326 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 3327 | tensorPtr, |
| 3328 | tensorInfo, |
| 3329 | permutationVector); |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 3330 | case armnn::DataType::QAsymmS8: |
| 3331 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 3332 | tensorPtr, |
| 3333 | tensorInfo, |
| 3334 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3335 | case armnn::DataType::Signed32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 3336 | return CreateConstTensorAndStoreData<int32_t>(bufferPtr, |
| 3337 | tensorPtr, |
| 3338 | tensorInfo, |
| 3339 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3340 | default: |
| 3341 | { |
| 3342 | std::stringstream errString; |
| 3343 | errString << "Unexpected datatype when creating const tensor: " |
| 3344 | << armnn::GetDataTypeName(tensorInfo.GetDataType()) |
| 3345 | << " shape:" << tensorInfo.GetShape() |
| 3346 | << CHECK_LOCATION().AsString(); |
| 3347 | throw ParseException(errString.str()); |
| 3348 | } |
| 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | BindingPointInfo TfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId, |
| 3353 | const std::string& name) const |
| 3354 | { |
| 3355 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3356 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 3357 | for (auto const & input : inputs) |
| 3358 | { |
| 3359 | if (input.second->name == name) |
| 3360 | { |
| 3361 | auto bindingId = GenerateLayerBindingId(subgraphId, input.first); |
| 3362 | return std::make_pair(bindingId, ToTensorInfo(input.second)); |
| 3363 | } |
| 3364 | } |
| 3365 | |
| 3366 | std::stringstream bindings; |
| 3367 | for (auto const & input : inputs) |
| 3368 | { |
| 3369 | bindings << "'" << input.second->name << "' "; |
| 3370 | } |
| 3371 | |
| 3372 | throw ParseException( |
| 3373 | boost::str( |
| 3374 | boost::format("No input binding found for subgraph:%1% and name:%2%. " |
| 3375 | "Possible inputs are: [%3%] %4%") % |
| 3376 | subgraphId % |
| 3377 | name % |
| 3378 | bindings.str() % |
| 3379 | CHECK_LOCATION().AsString())); |
| 3380 | } |
| 3381 | |
| 3382 | BindingPointInfo TfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId, |
| 3383 | const std::string& name) const |
| 3384 | { |
| 3385 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3386 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 3387 | for (unsigned int i = 0; i < outputs.size(); ++i) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3388 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 3389 | auto const output = outputs[i]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3390 | if (output.second->name == name) |
| 3391 | { |
| 3392 | auto bindingId = GenerateLayerBindingId(subgraphId, output.first); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 3393 | std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ? |
| 3394 | m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape); |
| 3395 | return std::make_pair(bindingId, ToTensorInfo(output.second, shape)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | |
| 3399 | std::stringstream bindings; |
| 3400 | for (auto const & output : outputs) |
| 3401 | { |
| 3402 | bindings << "'" << output.second->name << "' "; |
| 3403 | } |
| 3404 | |
| 3405 | throw ParseException( |
| 3406 | boost::str( |
| 3407 | boost::format("No output binding found for subgraph:%1% and name:%2%. " |
| 3408 | "Possible outputs are: [%3%] %4%") % |
| 3409 | subgraphId % |
| 3410 | name % |
| 3411 | bindings.str() % |
| 3412 | CHECK_LOCATION().AsString())); |
| 3413 | } |
| 3414 | |
| 3415 | size_t TfLiteParser::GetSubgraphCount() const |
| 3416 | { |
| 3417 | return m_Model->subgraphs.size(); |
| 3418 | } |
| 3419 | |
| 3420 | std::vector<std::string> TfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const |
| 3421 | { |
| 3422 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3423 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 3424 | std::vector<std::string> result; |
| 3425 | result.reserve(inputs.size()); |
| 3426 | for (auto const & input : inputs) |
| 3427 | { |
| 3428 | result.push_back(input.second->name); |
| 3429 | } |
| 3430 | return result; |
| 3431 | } |
| 3432 | |
| 3433 | std::vector<std::string> TfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const |
| 3434 | { |
| 3435 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 3436 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
| 3437 | std::vector<std::string> result; |
| 3438 | result.reserve(outputs.size()); |
| 3439 | for (auto const & output : outputs) |
| 3440 | { |
| 3441 | result.push_back(output.second->name); |
| 3442 | } |
| 3443 | return result; |
| 3444 | } |
| 3445 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3446 | ITfLiteParser* ITfLiteParser::CreateRaw(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3447 | { |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3448 | return new TfLiteParser(options); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3449 | } |
| 3450 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3451 | ITfLiteParserPtr ITfLiteParser::Create(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3452 | { |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 3453 | return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | void ITfLiteParser::Destroy(ITfLiteParser* parser) |
| 3457 | { |
| 3458 | delete parser; |
| 3459 | } |
| 3460 | |
| 3461 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data) |
| 3462 | : m_FloatData(std::move(data)) |
| 3463 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3464 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3465 | , m_Int32Data(nullptr) |
| 3466 | { |
| 3467 | } |
| 3468 | |
| 3469 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data) |
| 3470 | : m_FloatData(nullptr) |
| 3471 | , m_Uint8Data(std::move(data)) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3472 | , m_Int8Data(nullptr) |
| 3473 | , m_Int32Data(nullptr) |
| 3474 | { |
| 3475 | } |
| 3476 | |
| 3477 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data) |
| 3478 | : m_FloatData(nullptr) |
| 3479 | , m_Uint8Data(nullptr) |
| 3480 | , m_Int8Data(std::move(data)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3481 | , m_Int32Data(nullptr) |
| 3482 | { |
| 3483 | } |
| 3484 | |
| 3485 | TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data) |
| 3486 | : m_FloatData(nullptr) |
| 3487 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 3488 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3489 | , m_Int32Data(std::move(data)) |
| 3490 | { |
| 3491 | } |
| 3492 | |
| 3493 | } // armnnTfLiteParser |