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