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