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