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 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 8 | #include "armnnTfLiteParser/Version.hpp" |
| 9 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 10 | #include <armnn/BackendOptions.hpp> |
Matthew Bentham | 39ef3e5 | 2020-01-20 10:09:09 +0000 | [diff] [blame] | 11 | #include <armnn/Descriptors.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 12 | #include <armnn/Exceptions.hpp> |
Derek Lamberti | 0844697 | 2019-11-26 16:38:31 +0000 | [diff] [blame] | 13 | #include <armnn/Logging.hpp> |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 14 | #include <armnn/Tensor.hpp> |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 15 | #include <armnnUtils/TensorUtils.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 16 | #include <armnn/TypesUtils.hpp> |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 17 | #include <armnn/utility/Assert.hpp> |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 18 | #include <armnn/utility/IgnoreUnused.hpp> |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 19 | #include <armnn/utility/NumericCast.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 20 | |
| 21 | // armnnUtils: |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 22 | #include <armnnUtils/Permute.hpp> |
Rob Hughes | 9542f90 | 2021-07-14 09:48:54 +0100 | [diff] [blame] | 23 | #include <armnnUtils/Filesystem.hpp> |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 24 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 25 | #include <ParserHelper.hpp> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 26 | #include <VerificationHelpers.hpp> |
| 27 | |
| 28 | // The generated code based on the Tf Lite schema: |
| 29 | #include <schema_generated.h> |
| 30 | |
Matteo Martincigh | e011d20 | 2019-11-28 11:35:47 +0000 | [diff] [blame] | 31 | #include <flatbuffers/flexbuffers.h> |
| 32 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 33 | #include <fmt/format.h> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 34 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 35 | #include <algorithm> |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 36 | #include <fstream> |
| 37 | #include <iostream> |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 38 | #include <limits> |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 39 | #include <numeric> |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 40 | #include <sstream> |
| 41 | |
| 42 | #define ARMNN_THROW_PARSE_EXCEPTION(msg) \ |
| 43 | { \ |
| 44 | throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \ |
| 45 | << ": " \ |
| 46 | << CHECK_LOCATION().AsString()).str()); \ |
| 47 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 48 | |
| 49 | using namespace armnn; |
| 50 | using armnn::CheckLocation; |
| 51 | namespace armnnTfLiteParser |
| 52 | { |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 53 | |
| 54 | ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) : |
| 55 | pTfLiteParserImpl(new TfLiteParserImpl(options)) {} |
| 56 | |
| 57 | ITfLiteParser::~ITfLiteParser() = default; |
| 58 | |
| 59 | ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options) |
| 60 | { |
| 61 | return new ITfLiteParser(options); |
| 62 | } |
| 63 | |
| 64 | ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options) |
| 65 | { |
| 66 | return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy); |
| 67 | } |
| 68 | |
| 69 | void ITfLiteParser::Destroy(ITfLiteParser* parser) |
| 70 | { |
| 71 | delete parser; |
| 72 | } |
| 73 | |
| 74 | armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile) |
| 75 | { |
| 76 | return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile); |
| 77 | } |
| 78 | |
| 79 | armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) |
| 80 | { |
| 81 | return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent); |
| 82 | } |
| 83 | |
| 84 | BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId, |
| 85 | const std::string& name) const |
| 86 | { |
| 87 | return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name); |
| 88 | } |
| 89 | |
| 90 | BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId, |
| 91 | const std::string& name) const |
| 92 | { |
| 93 | return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name); |
| 94 | } |
| 95 | |
| 96 | size_t ITfLiteParser::GetSubgraphCount() const |
| 97 | { |
| 98 | return pTfLiteParserImpl->GetSubgraphCount(); |
| 99 | } |
| 100 | |
| 101 | std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const |
| 102 | { |
| 103 | return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId); |
| 104 | } |
| 105 | |
| 106 | std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const |
| 107 | { |
| 108 | return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId); |
| 109 | } |
| 110 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 111 | namespace |
| 112 | { |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 113 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 114 | const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max(); |
| 115 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 116 | void CheckSubgraph(const TfLiteParserImpl::ModelPtr & model, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 117 | size_t subgraphIndex, |
| 118 | const CheckLocation & location) |
| 119 | { |
| 120 | if (model.get() == nullptr) |
| 121 | { |
| 122 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 123 | fmt::format("{} was called with invalid (null) model. " |
| 124 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 125 | "subgraph:{} at {}", |
| 126 | location.m_Function, |
| 127 | subgraphIndex, |
| 128 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 129 | } |
| 130 | else if (subgraphIndex >= model->subgraphs.size()) |
| 131 | { |
| 132 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 133 | fmt::format("{} was called with an invalid subgraph index. " |
| 134 | "subgraph:{} at {}", |
| 135 | location.m_Function, |
| 136 | subgraphIndex, |
| 137 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \ |
| 142 | CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION()) |
| 143 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 144 | void CheckModel(const TfLiteParserImpl::ModelPtr & model, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 145 | size_t subgraphIndex, |
| 146 | size_t operatorIndex, |
| 147 | const CheckLocation & location) |
| 148 | { |
| 149 | if (model.get() == nullptr) |
| 150 | { |
| 151 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 152 | fmt::format("{} was called with invalid (null) model. " |
| 153 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 154 | "subgraph:{} operator:{} at {}", |
| 155 | location.m_Function, |
| 156 | subgraphIndex, |
| 157 | operatorIndex, |
| 158 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 159 | } |
| 160 | else if (subgraphIndex >= model->subgraphs.size()) |
| 161 | { |
| 162 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 163 | fmt::format("{} was called with an invalid subgraph index. " |
| 164 | "subgraph:{} operator:{} at {}", |
| 165 | location.m_Function, |
| 166 | subgraphIndex, |
| 167 | operatorIndex, |
| 168 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 169 | } |
| 170 | else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() && |
| 171 | operatorIndex != VIRTUAL_OPERATOR_ID) |
| 172 | { |
| 173 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 174 | fmt::format("{} was called with an invalid operator index. " |
| 175 | "subgraph:{} operator:{} at {}", |
| 176 | location.m_Function, |
| 177 | subgraphIndex, |
| 178 | operatorIndex, |
| 179 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 184 | CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION()) |
| 185 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 186 | void CheckTensor(const TfLiteParserImpl::ModelPtr & model, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 187 | size_t subgraphIndex, |
| 188 | size_t tensorIndex, |
| 189 | const CheckLocation & location) |
| 190 | { |
| 191 | // not checking model, because I assume CHECK_MODEL already run |
| 192 | // and checked that. An assert would do. |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 193 | ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 194 | |
| 195 | // also subgraph index should be checked by CHECK_MODEL so |
| 196 | // I only add an assert here |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 197 | ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 198 | |
| 199 | // the tensor index is the only one to check here |
| 200 | if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size()) |
| 201 | { |
| 202 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 203 | fmt::format("{} was called with an invalid tensor index. " |
| 204 | "subgraph:{} tensor:{} at {}", |
| 205 | location.m_Function, |
| 206 | subgraphIndex, |
| 207 | tensorIndex, |
| 208 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
| 212 | #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \ |
| 213 | CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION()) |
| 214 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 215 | void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 216 | const CheckLocation & location) |
| 217 | { |
| 218 | if (rawPtr == nullptr) |
| 219 | { |
| 220 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 221 | fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | #define CHECK_TENSOR_PTR(TENSOR_PTR) \ |
| 226 | CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION()) |
| 227 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 228 | void CheckBuffer(const TfLiteParserImpl::ModelPtr & model, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 229 | size_t bufferIndex, |
| 230 | const CheckLocation & location) |
| 231 | { |
| 232 | if (model.get() == nullptr) |
| 233 | { |
| 234 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 235 | fmt::format("{} was called with invalid (null) model. " |
| 236 | "Possible reason is that the model is not yet loaded and Unpack(ed). " |
| 237 | "buffer:{} at {}", |
| 238 | location.m_Function, |
| 239 | bufferIndex, |
| 240 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 241 | } |
| 242 | else if (bufferIndex >= model->buffers.size()) |
| 243 | { |
| 244 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 245 | fmt::format("{} was called with an invalid buffer index. " |
| 246 | "buffer index:{} at {}", |
| 247 | location.m_Function, |
| 248 | bufferIndex, |
| 249 | location.FileLine())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 250 | } |
| 251 | else if (model->buffers[bufferIndex].get() == nullptr) |
| 252 | { |
| 253 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 254 | fmt::format("The buffer #{} is null. {}", |
| 255 | bufferIndex, |
| 256 | location.AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \ |
| 261 | CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION()) |
| 262 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 263 | void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 264 | const armnn::TensorInfo & tensorInfo, |
| 265 | uint32_t bufferId, |
| 266 | const CheckLocation & location) |
| 267 | { |
| 268 | if (bufferPtr == nullptr) |
| 269 | { |
| 270 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 271 | fmt::format("BufferPtr is null for buffer:{}. {}", |
| 272 | bufferId, |
| 273 | location.AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 274 | } |
| 275 | else if(tensorInfo.GetNumElements() > bufferPtr->data.size() || |
| 276 | tensorInfo.GetNumBytes() > bufferPtr->data.size()) |
| 277 | { |
| 278 | std::stringstream ss; |
| 279 | ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. " |
| 280 | << "For tensor: " << tensorInfo.GetShape() |
| 281 | << " expecting: " << tensorInfo.GetNumBytes() << " bytes and " |
| 282 | << tensorInfo.GetNumElements() << " elements. " << location.AsString(); |
| 283 | throw ParseException(ss.str()); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \ |
| 288 | CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION()) |
| 289 | |
| 290 | bool IsActivationSupported(tflite::ActivationFunctionType activationType) |
| 291 | { |
| 292 | switch(activationType) |
| 293 | { |
| 294 | case tflite::ActivationFunctionType_NONE: |
| 295 | case tflite::ActivationFunctionType_RELU: |
| 296 | case tflite::ActivationFunctionType_RELU6: |
| 297 | case tflite::ActivationFunctionType_TANH: |
| 298 | { |
| 299 | return true; |
| 300 | } |
| 301 | default: |
| 302 | { |
| 303 | return false; |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \ |
| 309 | do { \ |
| 310 | if (IsActivationSupported(OPTION->fused_activation_function) == false) \ |
| 311 | { \ |
| 312 | throw ParseException( \ |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 313 | fmt::format("TfLite parser doesn't suppport fused activation: " \ |
| 314 | "{}/{} in {} subgraph:{} operator:{} at {}", \ |
| 315 | OPTION->fused_activation_function, \ |
| 316 | tflite::EnumNameActivationFunctionType(\ |
| 317 | OPTION->fused_activation_function), \ |
| 318 | __func__, \ |
| 319 | SUBGRAPH_INDEX, \ |
| 320 | OPERATOR_INDEX, \ |
| 321 | CHECK_LOCATION().FileLine())); \ |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 322 | } \ |
| 323 | } while(false) |
| 324 | |
| 325 | |
| 326 | std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in) |
| 327 | { |
| 328 | std::vector<unsigned int> result; |
| 329 | result.reserve(in.size()); |
| 330 | for (auto & i : in) |
| 331 | { |
mathad01 | c21025d | 2021-04-26 10:09:37 +0100 | [diff] [blame] | 332 | // If the location of the input data is -1 then the input should be ignored. |
| 333 | if (i == -1) |
| 334 | { |
| 335 | continue; |
| 336 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 337 | result.push_back(CHECKED_NON_NEGATIVE(i)); |
| 338 | } |
| 339 | return result; |
| 340 | } |
| 341 | |
| 342 | void CalcPadding(uint32_t inputSize, |
| 343 | uint32_t filterSize, |
| 344 | uint32_t stride, |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 345 | uint32_t dilation, |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 346 | uint32_t& paddingFront, |
| 347 | uint32_t& paddingBack, |
| 348 | tflite::Padding padding) |
| 349 | { |
| 350 | paddingFront = 0; |
| 351 | paddingBack = 0; |
| 352 | if (padding == tflite::Padding_SAME) |
| 353 | { |
| 354 | uint32_t outputSize = (inputSize + stride - 1) / stride; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 355 | uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1); |
| 356 | uint32_t temp = (outputSize - 1) * stride + dilatedSize; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 357 | if (temp > inputSize) |
| 358 | { |
| 359 | paddingFront = (temp - inputSize) / 2; |
| 360 | paddingBack = (temp - inputSize) - paddingFront; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 365 | armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr, |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 366 | const std::vector<unsigned int>& shape, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 367 | const bool outputTensor = false) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 368 | { |
| 369 | armnn::DataType type; |
| 370 | CHECK_TENSOR_PTR(tensorPtr); |
| 371 | |
| 372 | switch (tensorPtr->type) |
| 373 | { |
| 374 | case tflite::TensorType_UINT8: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 375 | type = armnn::DataType::QAsymmU8; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 376 | break; |
| 377 | case tflite::TensorType_FLOAT32: |
| 378 | type = armnn::DataType::Float32; |
| 379 | break; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 380 | case tflite::TensorType_INT8: |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 381 | if (tensorPtr->quantization->zero_point.size() == 1) |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 382 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 383 | // Per-tensor |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 384 | type = armnn::DataType::QAsymmS8; |
| 385 | } |
| 386 | else |
| 387 | { |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 388 | // Per-channel |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 389 | type = armnn::DataType::QSymmS8; |
| 390 | } |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 391 | break; |
| 392 | case tflite::TensorType_INT16: |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 393 | type = armnn::DataType::QSymmS16; |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 394 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 395 | case tflite::TensorType_INT32: |
| 396 | type = armnn::DataType::Signed32; |
| 397 | break; |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 398 | case tflite::TensorType_INT64: |
| 399 | type = armnn::DataType::Signed64; |
| 400 | break; |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 401 | case tflite::TensorType_BOOL: |
| 402 | type = armnn::DataType::Boolean; |
| 403 | break; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 404 | default: |
| 405 | { |
| 406 | CheckLocation location = CHECK_LOCATION(); |
| 407 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 408 | fmt::format("Unsupported data type {} = {} for tensor: {}. {}", |
| 409 | tensorPtr->type, |
| 410 | tflite::EnumNameTensorType(tensorPtr->type), |
| 411 | tensorPtr->name, |
| 412 | location.AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 413 | } |
| 414 | } |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 415 | TensorShape tensorShape; |
| 416 | |
| 417 | std::vector<unsigned int> safeShape = shape; |
| 418 | if (shape.size() == 0) |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 419 | { |
| 420 | safeShape.push_back(1); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | if (!outputTensor) |
| 424 | { |
| 425 | tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data()); |
| 426 | } |
| 427 | else |
| 428 | { |
Rob Hughes | d812a31 | 2021-08-06 13:10:53 +0100 | [diff] [blame] | 429 | size_t shapeSignatureSize = tensorPtr->shape_signature.size(); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 430 | |
| 431 | // If a shape signature exists we will use that to infer dynamic tensors |
| 432 | if (shapeSignatureSize != 0) |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 433 | { |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 434 | // If the shape is incompatible with the shape signature override the shape |
| 435 | if (shapeSignatureSize != shape.size()) |
| 436 | { |
| 437 | safeShape = {}; |
| 438 | |
| 439 | for (unsigned int i = 0; i < shapeSignatureSize; ++i) |
| 440 | { |
| 441 | unsigned int dim = tensorPtr->shape_signature[i] > -1 ? |
| 442 | static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0; |
| 443 | safeShape.push_back(dim); |
| 444 | } |
| 445 | } |
| 446 | |
Rob Hughes | d812a31 | 2021-08-06 13:10:53 +0100 | [diff] [blame] | 447 | std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size()); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 448 | for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i) |
| 449 | { |
| 450 | dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false : true; |
| 451 | } |
Rob Hughes | d812a31 | 2021-08-06 13:10:53 +0100 | [diff] [blame] | 452 | tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get()); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 453 | } |
| 454 | // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero |
| 455 | else if (shape.size() == 0) |
| 456 | { |
| 457 | tensorShape = TensorShape(1, false); |
| 458 | } |
| 459 | else |
| 460 | { |
| 461 | tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data()); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 462 | } |
Narumol Prangnawarat | 4818d46 | 2019-04-17 11:22:38 +0100 | [diff] [blame] | 463 | } |
| 464 | |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 465 | float quantizationScale = 0.0f; |
| 466 | int32_t quantizationOffset = 0; |
| 467 | |
| 468 | if (tensorPtr->quantization.get()) |
| 469 | { |
| 470 | if (tensorPtr->quantization->scale.size() <= 1) |
| 471 | { |
| 472 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 473 | CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1); |
| 474 | |
| 475 | if (tensorPtr->quantization->scale.size() == 1) |
| 476 | { |
| 477 | quantizationScale = tensorPtr->quantization->scale[0]; |
| 478 | } |
| 479 | if (tensorPtr->quantization->zero_point.size() == 1) |
| 480 | { |
| 481 | // NOTE: we lose precision here when converting from 64 bit to 32 |
Ryan OShea | 03181ff | 2020-02-07 17:22:22 +0000 | [diff] [blame] | 482 | // but this is what we support at the moment in ArmNN |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 483 | quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 486 | armnn::TensorInfo result(tensorShape, |
| 487 | type, |
| 488 | quantizationScale, |
| 489 | quantizationOffset); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 490 | return result; |
| 491 | } |
| 492 | else |
| 493 | { |
| 494 | std::vector<float> quantizationScales; |
| 495 | std::vector<int32_t> quantizationOffsets; |
| 496 | |
| 497 | // Scale |
| 498 | std::copy(tensorPtr->quantization->scale.begin(), |
| 499 | tensorPtr->quantization->scale.end(), |
| 500 | std::back_inserter(quantizationScales)); |
| 501 | |
Keith Davis | 0c2eeac | 2020-02-11 16:51:50 +0000 | [diff] [blame] | 502 | // QSymmS8 Per-axis |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 503 | armnn::TensorInfo result(tensorShape, |
| 504 | type, |
| 505 | quantizationScales, |
Jan Eilers | 7612bd6 | 2021-04-06 17:29:03 +0100 | [diff] [blame] | 506 | armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension)); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 507 | return result; |
| 508 | } |
| 509 | } |
| 510 | else |
| 511 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 512 | armnn::TensorInfo result(tensorShape, |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 513 | type, |
| 514 | quantizationScale, |
| 515 | quantizationOffset); |
| 516 | return result; |
| 517 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 518 | } |
| 519 | |
Jan Eilers | 7612bd6 | 2021-04-06 17:29:03 +0100 | [diff] [blame] | 520 | armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr) |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 521 | { |
| 522 | auto const & dimensions = AsUnsignedVector(tensorPtr->shape); |
Jan Eilers | 7612bd6 | 2021-04-06 17:29:03 +0100 | [diff] [blame] | 523 | return ToTensorInfo(tensorPtr, dimensions); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 526 | armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr, |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 527 | const bool outputTensor) |
| 528 | { |
| 529 | auto const & dimensions = AsUnsignedVector(tensorPtr->shape); |
Jan Eilers | 7612bd6 | 2021-04-06 17:29:03 +0100 | [diff] [blame] | 530 | return ToTensorInfo(tensorPtr, dimensions, outputTensor); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 531 | } |
| 532 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 533 | template<typename T> |
| 534 | std::pair<armnn::ConstTensor, std::unique_ptr<T[]>> |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 535 | CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr, |
| 536 | TfLiteParserImpl::TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 537 | armnn::TensorInfo& tensorInfo, |
| 538 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 539 | { |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 540 | IgnoreUnused(tensorPtr); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 541 | ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null"); |
| 542 | ARMNN_ASSERT_MSG(bufferPtr != nullptr, |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 543 | fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 544 | |
| 545 | std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 546 | |
| 547 | if (permutationVector.has_value() && permutationVector.value().GetSize() > 0) |
| 548 | { |
| 549 | tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value()); |
Matteo Martincigh | d5b9e64 | 2019-01-04 18:01:21 +0000 | [diff] [blame] | 550 | armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(), |
| 551 | reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T)); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 552 | } |
| 553 | else |
| 554 | { |
| 555 | ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes()); |
| 556 | } |
| 557 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 558 | // Make sure isConstant flag is set. |
| 559 | tensorInfo.SetConstant(); |
| 560 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 561 | return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data)); |
| 562 | } |
| 563 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 564 | armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex) |
| 565 | { |
| 566 | // generate the binding id by shifting the tensor id by 8 bit |
| 567 | // and add the subgraph id, which allows 256 subgraphs |
| 568 | return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex); |
| 569 | } |
| 570 | |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 571 | bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected) |
| 572 | { |
| 573 | const unsigned int actualSize = actual.GetNumDimensions(); |
| 574 | if (actualSize != expected.size()) |
| 575 | { |
| 576 | return false; |
| 577 | } |
| 578 | |
| 579 | for (unsigned int i = 0u; i < actualSize; i++) |
| 580 | { |
| 581 | if (expected[i] < 0 || |
| 582 | actual[i] != static_cast<unsigned int>(expected[i])) |
| 583 | { |
| 584 | return false; |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | return true; |
| 589 | } |
| 590 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 591 | void CheckMatchingQuantization(const TensorInfo& first, |
| 592 | const TensorInfo& second, |
| 593 | const std::string& descName, |
| 594 | std::string const& firstName, |
| 595 | std::string const& secondName) |
| 596 | { |
| 597 | if (!first.IsQuantized() || |
| 598 | !second.IsQuantized()) |
| 599 | { |
| 600 | // Not a quantized type, ignore the validation |
| 601 | return; |
| 602 | } |
| 603 | |
| 604 | DataType firstDataType = first.GetDataType(); |
| 605 | DataType secondDataType = second.GetDataType(); |
| 606 | |
| 607 | if (firstDataType != secondDataType) |
| 608 | { |
| 609 | throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName + |
| 610 | " must be of the same quantized type, " + |
| 611 | firstName + " is " + GetDataTypeName(firstDataType) + ", " + |
| 612 | secondName + " is " + GetDataTypeName(secondDataType)); |
| 613 | } |
| 614 | |
| 615 | if (!first.IsTypeSpaceMatch(second)) |
| 616 | { |
| 617 | throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName + |
| 618 | " must have the same quantization space, " + |
| 619 | firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) + |
| 620 | " and scale " + std::to_string(first.GetQuantizationScale()) + ", " + |
| 621 | secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) + |
| 622 | " and scale " + std::to_string(second.GetQuantizationScale())); |
| 623 | } |
| 624 | } |
| 625 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 626 | } // <anonymous> |
| 627 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 628 | TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options) |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 629 | : m_Options(options) |
| 630 | , m_Network(nullptr, nullptr) |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 631 | , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 632 | { |
| 633 | // register supported operators |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 634 | m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 635 | m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd; |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 636 | m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin; |
| 637 | m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 638 | m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D; |
| 639 | m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND; |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 640 | m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 641 | m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation; |
| 642 | m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D; |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 643 | // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed. |
| 644 | #if defined(ARMNN_POST_TFLITE_2_3) |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 645 | m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D; |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 646 | #endif |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 647 | m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator; |
| 648 | m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace; |
| 649 | m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D; |
| 650 | m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize; |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 651 | m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 652 | m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu; |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 653 | m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 654 | m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp; |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 655 | m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 656 | m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected; |
| 657 | m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather; |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 658 | m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater; |
| 659 | m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 660 | m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish; |
| 661 | m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu; |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 662 | m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess; |
| 663 | m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual; |
Mike Kelly | 31dce2b | 2021-09-01 21:22:37 +0100 | [diff] [blame] | 664 | m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION] |
| 665 | = &TfLiteParserImpl::ParseLocalResponseNormalization; |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 666 | m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 667 | m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic; |
| 668 | m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization; |
| 669 | m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D; |
| 670 | m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum; |
| 671 | m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean; |
| 672 | m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum; |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 673 | m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 674 | m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul; |
| 675 | m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg; |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 676 | m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 677 | m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack; |
| 678 | m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad; |
Narumol Prangnawarat | bfaee6b | 2021-05-24 18:50:24 +0100 | [diff] [blame] | 679 | m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 680 | m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize; |
| 681 | m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu; |
| 682 | m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6; |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 683 | m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax; |
| 684 | m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin; |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 685 | m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 686 | m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape; |
| 687 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear; |
| 688 | m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor; |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 689 | m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt; |
Keith Davis | 0176fd8 | 2021-06-01 17:36:32 +0100 | [diff] [blame] | 690 | m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape; |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 691 | m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice; |
| 692 | m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax; |
| 693 | m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND; |
| 694 | m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit; |
| 695 | m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV; |
| 696 | m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze; |
| 697 | m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice; |
| 698 | m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub; |
| 699 | m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum; |
| 700 | m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH; |
| 701 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose; |
| 702 | m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv; |
| 703 | m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack; |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 704 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 705 | // register supported custom operators |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 706 | m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 709 | void TfLiteParserImpl::ResetParser() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 710 | { |
| 711 | m_Network = armnn::INetworkPtr(nullptr, nullptr); |
| 712 | m_Model = nullptr; |
| 713 | m_SubgraphConnections.clear(); |
| 714 | } |
| 715 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 716 | INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 717 | { |
| 718 | ResetParser(); |
| 719 | m_Model = LoadModelFromFile(graphFile); |
| 720 | return CreateNetworkFromModel(); |
| 721 | } |
| 722 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 723 | INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 724 | { |
| 725 | ResetParser(); |
| 726 | m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size()); |
| 727 | return CreateNetworkFromModel(); |
| 728 | } |
| 729 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 730 | |
| 731 | armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model) |
| 732 | { |
| 733 | ResetParser(); |
| 734 | m_Model = std::move(model); |
| 735 | |
| 736 | return CreateNetworkFromModel(); |
| 737 | } |
| 738 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 739 | INetworkPtr TfLiteParserImpl::CreateNetworkFromModel() |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 740 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 741 | |
| 742 | using NetworkOptions = std::vector<BackendOptions>; |
| 743 | NetworkOptions networkOptions = {}; |
| 744 | if (m_Options && m_Options.value().m_InferAndValidate) |
| 745 | { |
| 746 | BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod", |
| 747 | { |
| 748 | { "InferAndValidate", true } |
| 749 | }); |
| 750 | |
| 751 | networkOptions.push_back(shapeInferenceMethodOption); |
| 752 | } |
| 753 | |
| 754 | m_Network = INetwork::Create(networkOptions); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 755 | ARMNN_ASSERT(m_Model.get() != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 756 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 757 | if (m_Model->subgraphs.size() != 1) |
| 758 | { |
| 759 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 760 | fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}", |
| 761 | m_Model->subgraphs.size(), |
| 762 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | size_t subgraphIndex = 0; |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 766 | size_t operatorIndex = 0; |
| 767 | try |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 768 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 769 | for (SubgraphPtr const& subgraph : m_Model->subgraphs) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 770 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 771 | m_SubgraphConnections.emplace_back(subgraph->tensors.size()); |
| 772 | for (OperatorPtr const& op : subgraph->operators) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 773 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 774 | auto const& opCodePtr = m_Model->operator_codes[op->opcode_index]; |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 775 | |
| 776 | // work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 777 | #if defined(ARMNN_POST_TFLITE_2_3) |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 778 | auto builtinCode = std::max(opCodePtr->builtin_code, |
| 779 | static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code)); |
| 780 | #else |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 781 | auto builtinCode = opCodePtr->builtin_code; |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 782 | #endif |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 783 | |
| 784 | if (builtinCode > tflite::BuiltinOperator_MAX) |
| 785 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 786 | throw ParseException(fmt::format("Operator code {} is out of range 0-{}. " |
| 787 | "subgraph:{} operator idx:{}. {}", |
| 788 | builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex, |
| 789 | operatorIndex, CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // lookup and call the parser function |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 793 | auto& parserFunction = m_ParserFunctions[builtinCode]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 794 | (this->*parserFunction)(subgraphIndex, operatorIndex); |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 795 | ++operatorIndex; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 796 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 797 | |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 798 | SetupInputLayers(subgraphIndex); |
| 799 | SetupOutputLayers(subgraphIndex); |
| 800 | SetupConstantLayers(subgraphIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 801 | |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 802 | ++subgraphIndex; |
| 803 | operatorIndex = 0; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 804 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 805 | } |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 806 | catch (const ParseException& e) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 807 | { |
Colm Donelan | 6350d27 | 2020-06-09 16:56:25 +0100 | [diff] [blame] | 808 | std::stringstream errorString; |
| 809 | errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #" |
| 810 | << subgraphIndex << " error: " << e.what(); |
| 811 | ARMNN_LOG(error) << errorString.str(); |
| 812 | std::stringstream errors; |
| 813 | errors << errorString.str() << "\n"; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 814 | throw ParseException(errors.str()); |
| 815 | } |
| 816 | |
| 817 | // 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] | 818 | for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 819 | { |
| 820 | for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 821 | { |
| 822 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr) |
| 823 | { |
| 824 | for (size_t inputSlotIdx = 0; |
| 825 | inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size(); |
| 826 | ++inputSlotIdx) |
| 827 | { |
| 828 | m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect( |
| 829 | *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx])); |
| 830 | } |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | return std::move(m_Network); |
| 836 | } |
| 837 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 838 | void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex, |
| 839 | size_t tensorIndex, |
| 840 | armnn::IOutputSlot* slot) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 841 | { |
| 842 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 843 | ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 844 | ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 845 | |
| 846 | TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
| 847 | |
| 848 | // assuming there is only one producer for that tensor |
| 849 | if (tensorSlots.outputSlot != nullptr) |
| 850 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 851 | throw ParseException(fmt::format("Another layer has already registered itself as the producer of " |
| 852 | "subgraph:{} tensor:{} {}", |
| 853 | subgraphIndex, |
| 854 | tensorIndex, |
| 855 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 856 | } |
| 857 | |
| 858 | tensorSlots.outputSlot = slot; |
| 859 | } |
| 860 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 861 | void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex, |
| 862 | size_t tensorIndex, |
| 863 | armnn::IInputSlot* slot) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 864 | { |
| 865 | CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 866 | ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex); |
| 867 | ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 868 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 869 | TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 870 | tensorSlots.inputSlots.push_back(slot); |
| 871 | } |
| 872 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 873 | void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex) |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 874 | { |
| 875 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 876 | |
| 877 | // NOTE: By default we presume the custom operator is not supported |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 878 | auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator; |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 879 | |
| 880 | // Identify custom code defined for custom operator |
| 881 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 882 | const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code; |
| 883 | |
| 884 | // Find parser function that correspondes to custom code (if any) |
| 885 | auto iterator = m_CustomParserFunctions.find(customCode); |
| 886 | if (iterator != m_CustomParserFunctions.end()) |
| 887 | { |
| 888 | customParserFunction = iterator->second; |
| 889 | } |
| 890 | |
| 891 | // Run parser function |
| 892 | (this->*customParserFunction)(subgraphIndex, operatorIndex); |
| 893 | } |
| 894 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 895 | void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 896 | { |
| 897 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 898 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 899 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 900 | |
| 901 | auto opcodeIndex = operatorPtr->opcode_index; |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 902 | |
| 903 | // work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 904 | #if defined(ARMNN_POST_TFLITE_2_3) |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 905 | auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code, |
| 906 | static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code)); |
| 907 | #else |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 908 | auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code; |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 909 | #endif |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 910 | |
| 911 | if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported) |
| 912 | { |
| 913 | // Do not add StandInLayer, throw ParseException instead |
| 914 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 915 | fmt::format("Operator not supported. " |
| 916 | "subgraph:{} operator:{} " |
| 917 | "opcode_index:{} opcode:{} / {} {}", |
| 918 | subgraphIndex, |
| 919 | operatorIndex, |
| 920 | opcodeIndex, |
| 921 | opcode, |
| 922 | tflite::EnumNameBuiltinOperator(opcode), |
| 923 | CHECK_LOCATION().AsString())); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 924 | } |
| 925 | |
| 926 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 927 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 928 | |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 929 | const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size()); |
| 930 | const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size()); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 931 | |
| 932 | StandInDescriptor descriptor(numInputs, numOutputs); |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 933 | auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 934 | |
| 935 | // Add a non-executable StandInLayer as a placeholder for any unsupported operator |
| 936 | IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 937 | ARMNN_ASSERT(layer != nullptr); |
| 938 | |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 939 | for (unsigned int i = 0u; i < numOutputs; ++i) |
| 940 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 941 | layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true)); |
Aron Virginas-Tar | c975f92 | 2019-10-23 17:38:17 +0100 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 945 | auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 946 | |
| 947 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds); |
| 948 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 949 | } |
| 950 | |
mathad01 | b392e98 | 2021-04-07 12:07:30 +0100 | [diff] [blame] | 951 | void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex) |
| 952 | { |
| 953 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 954 | |
| 955 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 956 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 957 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 958 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 959 | |
| 960 | auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex); |
| 961 | |
| 962 | IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str()); |
| 963 | ARMNN_ASSERT(layer != nullptr); |
| 964 | |
| 965 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 966 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 967 | |
| 968 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 969 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 970 | |
| 971 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 972 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 973 | } |
| 974 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 975 | void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 976 | { |
| 977 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 978 | |
| 979 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 980 | const auto * options = operatorPtr->builtin_options.AsConv2DOptions(); |
| 981 | |
| 982 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 983 | |
| 984 | Convolution2dDescriptor desc; |
| 985 | desc.m_BiasEnabled = false; |
| 986 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 987 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 988 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 989 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 990 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 991 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 992 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 993 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 994 | |
| 995 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 996 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 997 | |
| 998 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 999 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 1000 | |
| 1001 | // assuming input is NHWC |
| 1002 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1003 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1004 | |
| 1005 | // assuming the filter is OHWI : Output, H, W, Input |
| 1006 | // which is essentially the same as NHWC |
| 1007 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1008 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1009 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1010 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 1011 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1012 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 1013 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1014 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1015 | auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1016 | armnn::IConnectableLayer* layer = nullptr; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1017 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1018 | auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1019 | |
| 1020 | if (inputs.size() == 3) |
| 1021 | { |
| 1022 | desc.m_BiasEnabled = true; |
| 1023 | armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1024 | auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1025 | layer = m_Network->AddConvolution2dLayer(desc, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1026 | filterTensorAndData, |
| 1027 | Optional<ConstTensor>(biasTensorAndData), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1028 | layerName.c_str()); |
| 1029 | } |
| 1030 | else |
| 1031 | { |
| 1032 | layer = m_Network->AddConvolution2dLayer(desc, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1033 | filterTensorAndData, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1034 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1035 | layerName.c_str()); |
| 1036 | } |
| 1037 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1038 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1039 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1040 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1041 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1042 | |
| 1043 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1044 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1045 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1046 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1047 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1048 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1049 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1050 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1051 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1052 | } |
| 1053 | |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 1054 | // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed. |
| 1055 | #if defined(ARMNN_POST_TFLITE_2_3) |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1056 | void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex) |
| 1057 | { |
| 1058 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1059 | |
| 1060 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1061 | const auto* options = operatorPtr->builtin_options.AsConv3DOptions(); |
| 1062 | |
| 1063 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 1064 | |
| 1065 | Convolution3dDescriptor desc; |
| 1066 | desc.m_BiasEnabled = false; |
| 1067 | desc.m_DataLayout = armnn::DataLayout::NDHWC; |
| 1068 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1069 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1070 | desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d); |
| 1071 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 1072 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
| 1073 | desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor); |
| 1074 | |
| 1075 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1076 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 1077 | |
| 1078 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1079 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1080 | |
| 1081 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1082 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 1083 | |
| 1084 | // Assuming input is NDHWC |
| 1085 | unsigned int inputDepth = inputTensorInfo.GetShape()[1]; |
| 1086 | unsigned int inputHeight = inputTensorInfo.GetShape()[2]; |
| 1087 | unsigned int inputWidth = inputTensorInfo.GetShape()[3]; |
| 1088 | |
| 1089 | // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels |
| 1090 | unsigned int filterDepth = filterTensorInfo.GetShape()[0]; |
| 1091 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1092 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1093 | |
| 1094 | CalcPadding(inputDepth, filterDepth, desc.m_StrideZ, |
| 1095 | desc.m_DilationY, desc.m_PadFront, desc.m_PadBack, options->padding); |
| 1096 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 1097 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1098 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 1099 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
| 1100 | |
| 1101 | auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo); |
| 1102 | |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1103 | auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex); |
| 1104 | |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 1105 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1106 | // Add the first input and weights tensor to the registration list. |
| 1107 | // The constant weights will be added by SetupConstantLayers. |
| 1108 | std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]}; |
| 1109 | |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1110 | if (inputs.size() == 3) |
| 1111 | { |
| 1112 | desc.m_BiasEnabled = true; |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 1113 | |
| 1114 | // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers. |
| 1115 | tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]); |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 1118 | armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str()); |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1119 | ARMNN_ASSERT(layer != nullptr); |
| 1120 | |
| 1121 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 1122 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1123 | |
| 1124 | // Register the input connection slots for the layer, connections are made after all layers have been created |
Matthew Sloyan | 5d7b0a3 | 2021-10-18 13:07:49 +0100 | [diff] [blame] | 1125 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister); |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1126 | |
| 1127 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 1128 | // Register the output connection slots for the layer, connections are made after all layers have been created |
| 1129 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1130 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1131 | } |
Matthew Sloyan | 4d217c0 | 2021-10-07 11:48:58 +0100 | [diff] [blame] | 1132 | #endif |
Matthew Sloyan | eb5f810 | 2021-10-05 17:31:42 +0100 | [diff] [blame] | 1133 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1134 | void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1135 | { |
| 1136 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1137 | |
| 1138 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1139 | const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions(); |
| 1140 | |
| 1141 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 1142 | |
| 1143 | DepthwiseConvolution2dDescriptor desc; |
| 1144 | desc.m_BiasEnabled = false; |
| 1145 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1146 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1147 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Matthew Jackson | d6a9dee | 2019-07-22 13:53:24 +0100 | [diff] [blame] | 1148 | CHECKED_NON_NEGATIVE(options->depth_multiplier); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1149 | |
| 1150 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1151 | CHECK_VALID_SIZE(inputs.size(), 2, 3); |
| 1152 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1153 | CHECK_VALID_SIZE(outputs.size(), 1); |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1154 | desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor); |
| 1155 | desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor); |
Kevin May | 83add21 | 2019-03-26 11:39:19 +0000 | [diff] [blame] | 1156 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1157 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Jan Eilers | 7612bd6 | 2021-04-06 17:29:03 +0100 | [diff] [blame] | 1158 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1159 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1160 | // Assuming input is NHWC |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1161 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1162 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 1163 | |
| 1164 | // TensorflowLite weights come in the format [1, H, W, I * M] |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1165 | unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1166 | unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1167 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1168 | CalcPadding(inputHeight, filterHeight, desc.m_StrideY, |
| 1169 | desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1170 | CalcPadding(inputWidth, filterWidth, desc.m_StrideX, |
| 1171 | desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1172 | |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 1173 | // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation |
| 1174 | auto filterTensor = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1175 | armnn::IConnectableLayer* layer = nullptr; |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1176 | auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1177 | |
| 1178 | if (inputs.size() == 3) |
| 1179 | { |
| 1180 | desc.m_BiasEnabled = true; |
| 1181 | TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1182 | auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1183 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 1184 | filterTensor, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1185 | Optional<ConstTensor>(biasTensorAndData), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1186 | layerName.c_str()); |
| 1187 | } |
| 1188 | else |
| 1189 | { |
| 1190 | layer = m_Network->AddDepthwiseConvolution2dLayer(desc, |
Jan Eilers | 53ef795 | 2021-06-02 12:01:25 +0100 | [diff] [blame] | 1191 | filterTensor, |
Matteo Martincigh | fc598e1 | 2019-05-14 10:36:13 +0100 | [diff] [blame] | 1192 | EmptyOptional(), |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1193 | layerName.c_str()); |
| 1194 | } |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1195 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1196 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1197 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1198 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1199 | |
| 1200 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1201 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1202 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1203 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1204 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1205 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1206 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1207 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1208 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1209 | } |
| 1210 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1211 | void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex) |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1212 | { |
| 1213 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1214 | |
| 1215 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1216 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1217 | |
| 1218 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1219 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1220 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1221 | auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1222 | |
| 1223 | IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1224 | ARMNN_ASSERT(layer != nullptr); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1225 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1226 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Finn Williams | ed66d14 | 2019-12-06 09:55:55 +0000 | [diff] [blame] | 1227 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1228 | |
| 1229 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1230 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1231 | |
| 1232 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1233 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1234 | } |
| 1235 | |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 1236 | void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex) |
| 1237 | { |
| 1238 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1239 | |
| 1240 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1241 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1242 | |
| 1243 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1244 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1245 | |
| 1246 | auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex); |
| 1247 | |
| 1248 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1249 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 1250 | |
| 1251 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1252 | |
| 1253 | ReshapeDescriptor reshapeDesc; |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 1254 | |
| 1255 | if (outputTensorInfo.GetShape().AreAllDimensionsSpecified()) |
| 1256 | { |
| 1257 | reshapeDesc.m_TargetShape = outputTensorInfo.GetShape(); |
| 1258 | } |
| 1259 | else |
| 1260 | { |
| 1261 | int32_t axis = inputs[1]->shape[0]; |
| 1262 | |
| 1263 | int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()); |
| 1264 | |
| 1265 | if (axis > inputDimSize || axis < 0 - (inputDimSize + 1)) |
| 1266 | { |
| 1267 | throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive"); |
| 1268 | } |
| 1269 | |
| 1270 | if(axis < 0) |
| 1271 | { |
| 1272 | axis = inputDimSize + axis + 1; |
| 1273 | } |
| 1274 | |
Rob Hughes | d812a31 | 2021-08-06 13:10:53 +0100 | [diff] [blame] | 1275 | std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 1276 | unsigned int inputShapeIndex = 0; |
| 1277 | for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i) |
| 1278 | { |
| 1279 | if (i == static_cast<unsigned int>(axis)) |
| 1280 | { |
| 1281 | shape[i] = 1; |
| 1282 | } |
| 1283 | else |
| 1284 | { |
| 1285 | shape[i] = inputTensorInfo.GetShape()[inputShapeIndex]; |
| 1286 | ++inputShapeIndex; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Rob Hughes | d812a31 | 2021-08-06 13:10:53 +0100 | [diff] [blame] | 1290 | reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data()); |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 1291 | } |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 1292 | |
| 1293 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
| 1294 | ARMNN_ASSERT(layer != nullptr); |
| 1295 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1296 | |
| 1297 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1298 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1299 | |
| 1300 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1301 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1302 | } |
| 1303 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1304 | void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex) |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1305 | { |
| 1306 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1307 | |
| 1308 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1309 | CHECK_VALID_SIZE(inputs.size(), 1, 2); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1310 | |
| 1311 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1312 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1313 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1314 | auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex); |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1315 | TransposeDescriptor desc; |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1316 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1317 | if (inputs.size() == 2) |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1318 | { |
| 1319 | armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]); |
| 1320 | BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1321 | auto numPermVecElements = permuteTensorInfo.GetNumElements(); |
| 1322 | std::vector<unsigned int> permuteShape(numPermVecElements); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1323 | ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes()); |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1324 | PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements()); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1325 | |
Mike Kelly | 08759e2 | 2020-03-02 11:41:31 +0000 | [diff] [blame] | 1326 | desc = TransposeDescriptor(permutationVector); |
Kevin May | 85d9260 | 2019-09-27 17:21:06 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1329 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1330 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1331 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1332 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1333 | IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1334 | ARMNN_ASSERT(layer != nullptr); |
Keith Davis | 4cd29a0 | 2019-09-09 14:49:20 +0100 | [diff] [blame] | 1335 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1336 | |
| 1337 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1338 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1339 | |
| 1340 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1341 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1342 | } |
| 1343 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1344 | void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex) |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1345 | { |
| 1346 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1347 | |
| 1348 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1349 | const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions(); |
| 1350 | |
| 1351 | TransposeConvolution2dDescriptor desc; |
| 1352 | desc.m_BiasEnabled = false; |
| 1353 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1354 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1355 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1356 | |
| 1357 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
David Monahan | 6168380 | 2021-01-12 09:11:07 +0000 | [diff] [blame] | 1358 | if (inputs.size() == 4) |
| 1359 | { |
| 1360 | desc.m_BiasEnabled = true; |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1365 | } |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1366 | |
| 1367 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1368 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1369 | |
Colm Donelan | 0ad3ef1 | 2020-07-03 15:54:28 +0100 | [diff] [blame] | 1370 | if (inputs[0]) |
| 1371 | { |
| 1372 | armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]); |
| 1373 | std::vector<int> output_shape(tensorInfo.GetNumElements()); |
| 1374 | if (tensorInfo.GetDataType() == DataType::Signed32) |
| 1375 | { |
| 1376 | ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes()); |
| 1377 | } |
| 1378 | if (tensorInfo.GetDataType() == DataType::QAsymmU8) |
| 1379 | { |
| 1380 | for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++) |
| 1381 | { |
| 1382 | output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i]; |
| 1383 | } |
| 1384 | } |
| 1385 | // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor. |
| 1386 | for (int dimension : output_shape) |
| 1387 | { |
| 1388 | desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension)); |
| 1389 | } |
| 1390 | desc.m_OutputShapeEnabled = true; |
| 1391 | } |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1392 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1393 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 1394 | |
| 1395 | // TfLite uses NHWC tensors |
| 1396 | const unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1397 | const unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1398 | |
| 1399 | const unsigned int filterHeight = filterTensorInfo.GetShape()[1]; |
| 1400 | const unsigned int filterWidth = filterTensorInfo.GetShape()[2]; |
| 1401 | |
| 1402 | CalcPadding(inputHeight, |
| 1403 | filterHeight, |
| 1404 | desc.m_StrideY, |
| 1405 | 1, // DilationY |
| 1406 | desc.m_PadTop, |
| 1407 | desc.m_PadBottom, |
| 1408 | options->padding); |
| 1409 | |
| 1410 | CalcPadding(inputWidth, |
| 1411 | filterWidth, |
| 1412 | desc.m_StrideX, |
| 1413 | 1, // DilationX |
| 1414 | desc.m_PadLeft, |
| 1415 | desc.m_PadRight, |
| 1416 | options->padding); |
| 1417 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1418 | auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1419 | |
| 1420 | armnn::IConnectableLayer* layer = nullptr; |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1421 | auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1422 | |
David Monahan | 6168380 | 2021-01-12 09:11:07 +0000 | [diff] [blame] | 1423 | if (desc.m_BiasEnabled) |
| 1424 | { |
| 1425 | auto biasTensorInfo = ToTensorInfo(inputs[3]); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1426 | auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo); |
David Monahan | 6168380 | 2021-01-12 09:11:07 +0000 | [diff] [blame] | 1427 | layer = m_Network->AddTransposeConvolution2dLayer(desc, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1428 | filterTensorAndData, |
| 1429 | biasConstTensor, |
David Monahan | 6168380 | 2021-01-12 09:11:07 +0000 | [diff] [blame] | 1430 | layerName.c_str()); |
| 1431 | } |
| 1432 | else |
| 1433 | { |
| 1434 | layer = m_Network->AddTransposeConvolution2dLayer(desc, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 1435 | filterTensorAndData, |
David Monahan | 6168380 | 2021-01-12 09:11:07 +0000 | [diff] [blame] | 1436 | EmptyOptional(), |
| 1437 | layerName.c_str()); |
| 1438 | } |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1439 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1440 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1441 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1442 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1443 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1444 | |
| 1445 | // only the tensors for the inputs are relevant, exclude the const (filter) tensor |
| 1446 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Matthew Jackson | ccb25ea | 2019-08-20 17:18:33 +0100 | [diff] [blame] | 1447 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]}); |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 1448 | |
| 1449 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1450 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1451 | } |
| 1452 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1453 | void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex) |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1454 | { |
| 1455 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average); |
| 1456 | } |
| 1457 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1458 | void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1459 | { |
| 1460 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1461 | |
| 1462 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1463 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1464 | |
| 1465 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1466 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1467 | |
| 1468 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1469 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1470 | |
| 1471 | armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]); |
| 1472 | BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1473 | |
| 1474 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1475 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1476 | |
| 1477 | std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements()); |
| 1478 | ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes()); |
| 1479 | |
| 1480 | size_t step = 2; |
| 1481 | std::vector<std::pair<unsigned int, unsigned int>> crops; |
| 1482 | for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i) |
| 1483 | { |
| 1484 | crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]); |
| 1485 | } |
| 1486 | |
| 1487 | armnn::BatchToSpaceNdDescriptor desc; |
| 1488 | desc.m_BlockShape = blockShape; |
| 1489 | desc.m_Crops = crops; |
| 1490 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1491 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1492 | auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1493 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1494 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1495 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1496 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1497 | |
| 1498 | IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str()); |
| 1499 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | db947e2 | 2019-02-08 18:52:21 -0200 | [diff] [blame] | 1500 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1501 | |
| 1502 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1503 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1504 | |
| 1505 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1506 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1507 | } |
| 1508 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1509 | void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex) |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1510 | { |
| 1511 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1512 | |
| 1513 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1514 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1515 | |
| 1516 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1517 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1518 | |
| 1519 | L2NormalizationDescriptor desc; |
| 1520 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1521 | auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1522 | IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str()); |
| 1523 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1524 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1525 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1526 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | 28c9457 | 2019-07-18 10:47:03 +0100 | [diff] [blame] | 1527 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1528 | |
| 1529 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1530 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1531 | |
| 1532 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1533 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1534 | } |
| 1535 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1536 | void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex) |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1537 | { |
| 1538 | ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max); |
| 1539 | } |
| 1540 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1541 | void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1542 | { |
| 1543 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1544 | |
| 1545 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1546 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1547 | |
| 1548 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1549 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1550 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1551 | auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1552 | |
| 1553 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1554 | TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1555 | CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1"); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1556 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1557 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1558 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1559 | |
| 1560 | IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str()); |
| 1561 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1562 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1563 | |
| 1564 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 1565 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Bruno Goncalves | b8d805e | 2019-02-12 22:57:13 -0200 | [diff] [blame] | 1566 | |
| 1567 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1568 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1569 | } |
| 1570 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1571 | void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1572 | { |
| 1573 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1574 | |
| 1575 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1576 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 1577 | |
| 1578 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1579 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1580 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1581 | auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1582 | |
| 1583 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1584 | TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 1585 | CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1"); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1586 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1587 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1588 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1589 | |
| 1590 | IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str()); |
| 1591 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1592 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1593 | |
| 1594 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 1595 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Bruno Goncalves | 8f6d7a7 | 2019-02-12 22:58:18 -0200 | [diff] [blame] | 1596 | |
| 1597 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1598 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1599 | } |
| 1600 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1601 | void TfLiteParserImpl::ParsePool(size_t subgraphIndex, |
| 1602 | size_t operatorIndex, |
| 1603 | PoolingAlgorithm algorithm) |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1604 | { |
| 1605 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1606 | |
| 1607 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1608 | const auto * options = operatorPtr->builtin_options.AsPool2DOptions(); |
| 1609 | |
| 1610 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 1611 | |
| 1612 | std::string layerName; |
| 1613 | |
| 1614 | switch (algorithm) |
| 1615 | { |
| 1616 | case PoolingAlgorithm::Average: |
| 1617 | layerName = |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1618 | fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1619 | break; |
| 1620 | case PoolingAlgorithm::Max: |
| 1621 | layerName = |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1622 | fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1623 | break; |
| 1624 | default: |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 1625 | ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm"); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | Pooling2dDescriptor desc; |
| 1629 | |
| 1630 | desc.m_PoolType = algorithm; |
| 1631 | desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w); |
| 1632 | desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h); |
| 1633 | desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width); |
| 1634 | desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height); |
| 1635 | desc.m_PaddingMethod = PaddingMethod::Exclude; |
| 1636 | desc.m_OutputShapeRounding = OutputShapeRounding::Floor; |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1637 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1638 | |
| 1639 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1640 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1641 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1642 | |
| 1643 | // assuming input is NHWC |
| 1644 | unsigned int inputHeight = inputTensorInfo.GetShape()[1]; |
| 1645 | unsigned int inputWidth = inputTensorInfo.GetShape()[2]; |
| 1646 | |
Pablo Tello | f0bd683 | 2019-04-26 17:58:13 +0100 | [diff] [blame] | 1647 | CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u, |
| 1648 | desc.m_PadTop, desc.m_PadBottom, options->padding); |
| 1649 | CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u, |
| 1650 | desc.m_PadLeft, desc.m_PadRight, options->padding); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1651 | |
| 1652 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1653 | CHECK_VALID_SIZE(outputs.size(), 1); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1654 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1655 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1656 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1657 | |
| 1658 | IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str()); |
| 1659 | ARMNN_ASSERT(layer != nullptr); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1660 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1661 | |
| 1662 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1663 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1664 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1665 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1666 | |
jimfly01 | c25411c | 2018-11-14 17:47:22 +0000 | [diff] [blame] | 1667 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Nattapat Chaimanowong | b66504b | 2018-10-17 15:19:14 +0100 | [diff] [blame] | 1668 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1669 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1670 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1671 | } |
| 1672 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1673 | void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex) |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1674 | { |
| 1675 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1676 | |
| 1677 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1678 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1679 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1680 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1681 | |
| 1682 | SliceDescriptor desc; |
| 1683 | |
| 1684 | // set begin tensor info for slice descriptor |
| 1685 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1686 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1687 | |
| 1688 | std::vector<unsigned int> begin(beginTensorInfo.GetNumElements()); |
| 1689 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1690 | |
| 1691 | // set size tensor info for slice descriptor |
| 1692 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]); |
| 1693 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1694 | |
Mike Kelly | 7ba84d6 | 2021-09-10 15:27:19 +0100 | [diff] [blame] | 1695 | std::vector<int> signedSize(sizeTensorInfo.GetNumElements()); |
| 1696 | ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1697 | std::vector<unsigned int> size(sizeTensorInfo.GetNumElements()); |
Mike Kelly | 7ba84d6 | 2021-09-10 15:27:19 +0100 | [diff] [blame] | 1698 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 1699 | |
| 1700 | for (unsigned int i = 0; i < signedSize.size(); ++i) |
| 1701 | { |
| 1702 | int signedValue = signedSize[i]; |
Jim Flynn | fca233e | 2021-09-23 12:16:53 +0100 | [diff] [blame] | 1703 | |
Mike Kelly | 7ba84d6 | 2021-09-10 15:27:19 +0100 | [diff] [blame] | 1704 | if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i])) |
| 1705 | { |
| 1706 | throw ParseException(fmt::format("Invalid value for size {} size must be in range " |
| 1707 | "[-1, inputDimSize - begin] [-1, {}] inclusive {}", |
| 1708 | signedValue, |
| 1709 | inputTensorInfo.GetShape()[i] - begin[i], |
| 1710 | CHECK_LOCATION().AsString())); |
| 1711 | } |
| 1712 | |
| 1713 | if (signedValue == -1) |
| 1714 | { |
| 1715 | size[i] = inputTensorInfo.GetShape()[i] - begin[i]; |
| 1716 | } |
| 1717 | else |
| 1718 | { |
| 1719 | size[i] = static_cast<unsigned int>(signedValue); |
| 1720 | } |
| 1721 | } |
| 1722 | |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1723 | desc = SliceDescriptor(begin, size); |
| 1724 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1725 | auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1726 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1727 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1728 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1729 | |
| 1730 | IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str()); |
josh minor | ba424d2 | 2019-11-13 10:55:17 -0600 | [diff] [blame] | 1731 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1732 | |
| 1733 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1734 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1735 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1736 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1737 | |
| 1738 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1739 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1740 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1741 | } |
| 1742 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1743 | void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1744 | { |
| 1745 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1746 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1747 | const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions(); |
| 1748 | |
| 1749 | SoftmaxDescriptor desc; |
| 1750 | desc.m_Beta = options->beta; |
| 1751 | |
| 1752 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1753 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1754 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1755 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1756 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1757 | auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1758 | IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str()); |
| 1759 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1760 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1761 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1762 | |
| 1763 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 1764 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 1765 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1766 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1767 | |
| 1768 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 1769 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1770 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1771 | } |
| 1772 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1773 | void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1774 | { |
| 1775 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1776 | |
| 1777 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1778 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 1779 | |
| 1780 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1781 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1782 | |
| 1783 | armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]); |
| 1784 | BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1785 | |
| 1786 | armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]); |
| 1787 | BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1788 | |
| 1789 | std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements()); |
| 1790 | ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes()); |
| 1791 | |
| 1792 | std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements()); |
| 1793 | ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes()); |
| 1794 | |
| 1795 | size_t step = 2; |
| 1796 | std::vector<std::pair<unsigned int, unsigned int>> padList; |
| 1797 | for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i) |
| 1798 | { |
| 1799 | padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]); |
| 1800 | } |
| 1801 | |
| 1802 | armnn::SpaceToBatchNdDescriptor desc; |
| 1803 | desc.m_BlockShape = blockShape; |
| 1804 | desc.m_PadList = padList; |
| 1805 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1806 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1807 | auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1808 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1809 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 1810 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1811 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 1812 | |
| 1813 | IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str()); |
| 1814 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | baded14 | 2019-02-08 19:02:48 -0200 | [diff] [blame] | 1815 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1816 | |
| 1817 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1818 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1819 | |
| 1820 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1821 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1822 | } |
| 1823 | |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 1824 | armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims, |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1825 | const armnn::TensorInfo & inputTensorInfo) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1826 | { |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 1827 | CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1828 | static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 }; |
| 1829 | |
| 1830 | if (inputTensorInfo.GetNumDimensions() > 4) |
| 1831 | { |
| 1832 | std::stringstream ss; |
| 1833 | ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1834 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1835 | << CHECK_LOCATION().AsString(); |
| 1836 | throw ParseException(ss.str()); |
| 1837 | } |
| 1838 | |
| 1839 | if (squeezeDims.empty()) |
| 1840 | { |
| 1841 | squeezeDims.assign(dimensionSequence, |
| 1842 | dimensionSequence+inputTensorInfo.GetNumDimensions()); |
| 1843 | } |
| 1844 | |
| 1845 | std::vector<uint32_t> outputDims; |
| 1846 | for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++) |
| 1847 | { |
| 1848 | bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end()); |
| 1849 | auto currentDimension = inputTensorInfo.GetShape()[i]; |
| 1850 | if (skipSqueeze || currentDimension != 1) |
| 1851 | { |
| 1852 | outputDims.push_back(currentDimension); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | if (outputDims.size() > 4) |
| 1857 | { |
| 1858 | std::stringstream ss; |
| 1859 | ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions() |
| 1860 | << " shape:" << inputTensorInfo.GetShape() << " " |
| 1861 | << CHECK_LOCATION().AsString(); |
| 1862 | throw ParseException(ss.str()); |
| 1863 | } |
| 1864 | |
| 1865 | TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()), |
| 1866 | outputDims.data()); |
| 1867 | |
| 1868 | // we need to preserve the tensor type and the quantization data as well |
| 1869 | TensorInfo outTensorInfo = inputTensorInfo; |
| 1870 | outTensorInfo.SetShape(outShape); |
| 1871 | |
| 1872 | return outTensorInfo; |
| 1873 | } |
| 1874 | |
Keith Davis | 0176fd8 | 2021-06-01 17:36:32 +0100 | [diff] [blame] | 1875 | void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex) |
| 1876 | { |
| 1877 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1878 | |
| 1879 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1880 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1881 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1882 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1883 | |
| 1884 | auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex); |
| 1885 | |
| 1886 | IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str()); |
| 1887 | ARMNN_ASSERT(layer != nullptr); |
| 1888 | |
| 1889 | |
| 1890 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 1891 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1892 | |
| 1893 | // Check if output tensor type is Signed32 or Signed64 |
| 1894 | if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 && |
| 1895 | outputTensorInfo.GetDataType() != armnn::DataType::Signed64) |
| 1896 | { |
| 1897 | throw ParseException( |
| 1898 | fmt::format( |
| 1899 | "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}", |
| 1900 | CHECK_LOCATION().AsString())); |
| 1901 | } |
| 1902 | |
| 1903 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1904 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1905 | |
| 1906 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1907 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 1908 | } |
| 1909 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1910 | void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1911 | { |
| 1912 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1913 | |
| 1914 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1915 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 1916 | |
| 1917 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1918 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1919 | |
| 1920 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1921 | const auto * options = operatorPtr->builtin_options.AsSqueezeOptions(); |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 1922 | auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1923 | |
| 1924 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Teresa Charlin | 3ab8548 | 2021-06-08 16:59:29 +0100 | [diff] [blame] | 1925 | |
| 1926 | std::vector<uint32_t> squeezeDim; |
| 1927 | // A single negative dim index is interpreted as a negative index in python |
| 1928 | // Meaning the index will be the shape size plus the negative index value |
| 1929 | if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0) |
| 1930 | { |
| 1931 | int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0]; |
| 1932 | squeezeDim.push_back(static_cast<uint32_t>(dim)); |
| 1933 | } |
| 1934 | else |
| 1935 | { |
| 1936 | squeezeDim = AsUnsignedVector(options->squeeze_dims); |
| 1937 | } |
| 1938 | |
| 1939 | armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo); |
| 1940 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1941 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1942 | |
| 1943 | ReshapeDescriptor reshapeDesc; |
| 1944 | reshapeDesc.m_TargetShape = outputTensorInfo.GetShape(); |
| 1945 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1946 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 1947 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 1948 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 1949 | |
| 1950 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1951 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 1952 | |
| 1953 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 1954 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 1955 | } |
| 1956 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 1957 | void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 1958 | { |
| 1959 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 1960 | |
| 1961 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 1962 | CHECK_VALID_SIZE(inputs.size(), 4); |
| 1963 | |
| 1964 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 1965 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 1966 | |
| 1967 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 1968 | const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions(); |
| 1969 | |
| 1970 | StridedSliceDescriptor desc; |
| 1971 | desc.m_BeginMask = options->begin_mask; |
| 1972 | desc.m_EllipsisMask = options->ellipsis_mask; |
| 1973 | desc.m_EndMask = options->end_mask; |
| 1974 | desc.m_NewAxisMask = options->new_axis_mask; |
| 1975 | desc.m_ShrinkAxisMask = options->shrink_axis_mask; |
| 1976 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
| 1977 | |
| 1978 | armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]); |
| 1979 | BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 1980 | |
| 1981 | std::vector<int> begin(beginTensorInfo.GetNumElements()); |
| 1982 | ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes()); |
| 1983 | |
| 1984 | armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]); |
| 1985 | BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer); |
| 1986 | |
| 1987 | std::vector<int> end(endTensorInfo.GetNumElements()); |
| 1988 | ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes()); |
| 1989 | |
| 1990 | armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]); |
| 1991 | BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer); |
| 1992 | |
| 1993 | std::vector<int> stride(strideTensorInfo.GetNumElements()); |
| 1994 | ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes()); |
| 1995 | |
| 1996 | desc.m_Begin = begin; |
| 1997 | desc.m_End = end; |
| 1998 | desc.m_Stride = stride; |
| 1999 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2000 | auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 2001 | IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2002 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 2003 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2004 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | 451d95b | 2019-02-12 22:59:22 -0200 | [diff] [blame] | 2005 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2006 | |
| 2007 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2008 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2009 | |
| 2010 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2011 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2012 | } |
| 2013 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2014 | void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 2015 | { |
| 2016 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2017 | |
| 2018 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2019 | const auto * options = operatorPtr->builtin_options.AsSubOptions(); |
| 2020 | |
| 2021 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2022 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2023 | |
| 2024 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2025 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2026 | |
| 2027 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2028 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 2029 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2030 | auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 2031 | IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2032 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 2033 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2034 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 2035 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2036 | |
| 2037 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 2038 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Bruno Goncalves | bbeae26 | 2019-02-07 18:37:39 -0200 | [diff] [blame] | 2039 | |
| 2040 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 2041 | |
| 2042 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2043 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2044 | } |
| 2045 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2046 | void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex) |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 2047 | { |
| 2048 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2049 | |
| 2050 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2051 | const auto * options = operatorPtr->builtin_options.AsDivOptions(); |
| 2052 | |
| 2053 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2054 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2055 | |
| 2056 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2057 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2058 | |
| 2059 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2060 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 2061 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2062 | auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 2063 | IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2064 | ARMNN_ASSERT(layer != nullptr); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 2065 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2066 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 2067 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2068 | |
| 2069 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 2070 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Darshan Patel | 42b3d7d | 2020-05-25 22:30:07 +0530 | [diff] [blame] | 2071 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 2072 | |
| 2073 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2074 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2075 | } |
| 2076 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2077 | void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 2078 | { |
| 2079 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2080 | |
| 2081 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2082 | const auto * options = operatorPtr->builtin_options.AsAddOptions(); |
| 2083 | |
| 2084 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2085 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2086 | |
| 2087 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2088 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2089 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 2090 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2091 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 2092 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2093 | auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 2094 | IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2095 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 2096 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2097 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 2098 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2099 | |
| 2100 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 2101 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Bruno Goncalves | d4ac6a4 | 2018-12-18 12:56:22 -0200 | [diff] [blame] | 2102 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 2103 | |
| 2104 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2105 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2106 | } |
| 2107 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2108 | void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 2109 | { |
| 2110 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2111 | |
| 2112 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2113 | const auto * options = operatorPtr->builtin_options.AsMulOptions(); |
| 2114 | |
| 2115 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2116 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2117 | |
| 2118 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2119 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2120 | |
Bruno Goncalves | 9c761a6 | 2018-12-27 14:20:35 -0200 | [diff] [blame] | 2121 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2122 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 2123 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2124 | auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 2125 | IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2126 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 2127 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2128 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 2129 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2130 | |
| 2131 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 16f82f9 | 2020-09-14 16:12:44 +0100 | [diff] [blame] | 2132 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
Bruno Goncalves | f803f78 | 2018-12-18 13:40:30 -0200 | [diff] [blame] | 2133 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
| 2134 | |
| 2135 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2136 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2137 | } |
| 2138 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2139 | void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 2140 | { |
| 2141 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2142 | |
| 2143 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2144 | |
| 2145 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2146 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2147 | |
| 2148 | armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]); |
| 2149 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2150 | |
| 2151 | armnn::MeanDescriptor desc; |
| 2152 | std::vector<unsigned int> axis(dimTensorInfo.GetNumElements()); |
| 2153 | ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes()); |
| 2154 | desc.m_Axis = axis; |
| 2155 | |
| 2156 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2157 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 2158 | |
| 2159 | desc.m_KeepDims = |
| 2160 | inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? |
| 2161 | true : false; |
| 2162 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2163 | auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 2164 | IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2165 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 2235cee | 2018-12-19 12:51:45 -0200 | [diff] [blame] | 2166 | |
| 2167 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2168 | |
| 2169 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2170 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2171 | |
| 2172 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2173 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2174 | } |
| 2175 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2176 | void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2177 | { |
| 2178 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2179 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2180 | TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2181 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2182 | TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2183 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2184 | |
Narumol Prangnawarat | 8719d22 | 2020-11-27 16:57:56 +0000 | [diff] [blame] | 2185 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2186 | |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2187 | armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]); |
| 2188 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2189 | |
| 2190 | std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements()); |
| 2191 | ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes()); |
| 2192 | |
| 2193 | size_t step = 2; |
| 2194 | armnn::PadDescriptor desc; |
Narumol Prangnawarat | 8719d22 | 2020-11-27 16:57:56 +0000 | [diff] [blame] | 2195 | if (inputTensorInfo.IsQuantized()) |
| 2196 | { |
| 2197 | desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset()); |
| 2198 | } |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2199 | for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i) |
| 2200 | { |
| 2201 | desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]); |
| 2202 | } |
| 2203 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2204 | auto layerName = fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2205 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2206 | |
| 2207 | IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str()); |
| 2208 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 6c2355b | 2018-12-19 12:52:01 -0200 | [diff] [blame] | 2209 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2210 | |
| 2211 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2212 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2213 | |
| 2214 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2215 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2216 | } |
| 2217 | |
Matthew Sloyan | af3a4ef | 2021-10-22 15:48:12 +0100 | [diff] [blame] | 2218 | void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex) |
| 2219 | { |
| 2220 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2221 | |
| 2222 | TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2223 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2224 | |
| 2225 | TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2226 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2227 | |
| 2228 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2229 | |
| 2230 | armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]); |
| 2231 | BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2232 | |
| 2233 | std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements()); |
| 2234 | ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes()); |
| 2235 | |
| 2236 | size_t step = 2; |
| 2237 | armnn::PadDescriptor desc; |
| 2238 | for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i) |
| 2239 | { |
| 2240 | desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]); |
| 2241 | } |
| 2242 | |
| 2243 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2244 | const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions(); |
| 2245 | |
| 2246 | if (options->mode == tflite::MirrorPadMode_REFLECT) |
| 2247 | { |
| 2248 | desc.m_PaddingMode = PaddingMode::Reflect; |
| 2249 | } |
| 2250 | else if (options->mode == tflite::MirrorPadMode_SYMMETRIC) |
| 2251 | { |
| 2252 | desc.m_PaddingMode = PaddingMode::Symmetric; |
| 2253 | } |
| 2254 | else |
| 2255 | { |
| 2256 | ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC"); |
| 2257 | } |
| 2258 | |
| 2259 | // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1. |
| 2260 | // If padding mode is Symmetric then both paddings must be no greater than inputShape(i). |
| 2261 | auto inputShape = inputTensorInfo.GetShape(); |
| 2262 | auto padList = desc.m_PadList; |
| 2263 | |
| 2264 | const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect); |
| 2265 | for(unsigned int i = 0; i < padList.size(); ++i) |
| 2266 | { |
| 2267 | if(padList.at(i).first > (inputShape[i] - isReflect) || |
| 2268 | padList.at(i).second > (inputShape[i] - isReflect)) |
| 2269 | { |
| 2270 | ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or " |
| 2271 | "equal (Symmetric) to the dimension size."); |
| 2272 | } |
| 2273 | } |
| 2274 | |
| 2275 | auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex); |
| 2276 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 2277 | |
| 2278 | IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str()); |
| 2279 | ARMNN_ASSERT(layer != nullptr); |
| 2280 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2281 | |
| 2282 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2283 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2284 | |
| 2285 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2286 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2287 | } |
| 2288 | |
Narumol Prangnawarat | bfaee6b | 2021-05-24 18:50:24 +0100 | [diff] [blame] | 2289 | void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex) |
| 2290 | { |
| 2291 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2292 | |
| 2293 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2294 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2295 | |
| 2296 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2297 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2298 | |
| 2299 | auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex); |
| 2300 | |
| 2301 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2302 | armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]); |
| 2303 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 2304 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 2305 | |
| 2306 | IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str()); |
| 2307 | ARMNN_ASSERT(layer != nullptr); |
| 2308 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2309 | |
| 2310 | if (IsConstTensor(inputs[1])) |
| 2311 | { |
Narumol Prangnawarat | bfaee6b | 2021-05-24 18:50:24 +0100 | [diff] [blame] | 2312 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | bf99b5f | 2021-05-27 09:55:43 +0100 | [diff] [blame] | 2313 | armnn::IInputSlot* slot = &(layer->GetInputSlot(0)); |
| 2314 | RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot); |
Narumol Prangnawarat | bfaee6b | 2021-05-24 18:50:24 +0100 | [diff] [blame] | 2315 | |
| 2316 | auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo); |
| 2317 | std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name); |
| 2318 | IConnectableLayer* constLayer = |
| 2319 | m_Network->AddConstantLayer(alphaTensorAndData, constLayerName.c_str()); |
| 2320 | ARMNN_ASSERT(constLayer != nullptr); |
| 2321 | |
| 2322 | constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo); |
| 2323 | constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1)); |
| 2324 | RegisterOutputSlots(subgraphIndex, |
| 2325 | VIRTUAL_OPERATOR_ID, |
| 2326 | constLayer, |
| 2327 | { inputTensorIndexes[1] }); |
| 2328 | } |
| 2329 | else |
| 2330 | { |
| 2331 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2332 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes); |
| 2333 | } |
| 2334 | |
| 2335 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2336 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2337 | } |
| 2338 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2339 | void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2340 | { |
| 2341 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2342 | |
| 2343 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2344 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2345 | |
| 2346 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2347 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2348 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2349 | auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2350 | |
| 2351 | IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2352 | ARMNN_ASSERT(layer != nullptr); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2353 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2354 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 66dedc7 | 2019-12-10 16:32:07 +0000 | [diff] [blame] | 2355 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2356 | |
| 2357 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2358 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2359 | |
| 2360 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2361 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2362 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2363 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2364 | void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2365 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2366 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2369 | void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2370 | { |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2371 | ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu); |
| 2372 | } |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2373 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2374 | void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2375 | { |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2376 | ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu); |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2377 | } |
| 2378 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2379 | void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex) |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2380 | { |
| 2381 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid); |
| 2382 | } |
| 2383 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2384 | void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex) |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 2385 | { |
| 2386 | ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH); |
| 2387 | } |
| 2388 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2389 | void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex) |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 2390 | { |
| 2391 | ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu); |
| 2392 | } |
| 2393 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2394 | void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex) |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2395 | { |
| 2396 | ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish); |
| 2397 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2398 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2399 | void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType) |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2400 | { |
| 2401 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2402 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
Jan Eilers | 8eb2560 | 2020-03-09 12:13:48 +0000 | [diff] [blame] | 2403 | IgnoreUnused(operatorPtr); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2404 | |
| 2405 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2406 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 2407 | |
| 2408 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2409 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2410 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2411 | auto layerName = fmt::format("Activation:"); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2412 | ActivationDescriptor activationDesc; |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2413 | activationDesc.m_Function = activationType; |
| 2414 | |
| 2415 | switch (activationType) |
| 2416 | { |
| 2417 | case ActivationFunction::ReLu: |
| 2418 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2419 | layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2420 | break; |
| 2421 | } |
| 2422 | case ActivationFunction::BoundedReLu: |
| 2423 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2424 | layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2425 | activationDesc.m_A = 6.0f; |
| 2426 | activationDesc.m_B = 0.0f; |
| 2427 | break; |
| 2428 | } |
| 2429 | case ActivationFunction::Sigmoid: |
| 2430 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2431 | layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2432 | break; |
| 2433 | } |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 2434 | case ActivationFunction::TanH: |
| 2435 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2436 | layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex); |
Nina Drozd | 9985176 | 2019-04-09 09:37:38 +0100 | [diff] [blame] | 2437 | activationDesc.m_A = 1.0f; |
| 2438 | activationDesc.m_B = 1.0f; |
| 2439 | break; |
| 2440 | } |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2441 | case ActivationFunction::LeakyReLu: |
| 2442 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2443 | layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | 12239e7 | 2020-05-27 11:06:17 +0100 | [diff] [blame] | 2444 | const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions(); |
| 2445 | activationDesc.m_A = options->alpha; |
| 2446 | break; |
| 2447 | } |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 2448 | case ActivationFunction::Elu: |
| 2449 | { |
| 2450 | layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex); |
| 2451 | activationDesc.m_A = 1.0f; |
| 2452 | break; |
| 2453 | } |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2454 | case ActivationFunction::HardSwish: |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 2455 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2456 | layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex); |
Jan Eilers | 2f746b3 | 2020-07-28 14:00:06 +0100 | [diff] [blame] | 2457 | break; |
Matthew Sloyan | 7515d07 | 2020-12-16 12:50:01 +0000 | [diff] [blame] | 2458 | } |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2459 | default: |
| 2460 | { |
| 2461 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2462 | fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ", |
| 2463 | static_cast<int>(activationType), CHECK_LOCATION().AsString())); |
Finn Williams | c42c384 | 2019-01-22 14:18:11 +0000 | [diff] [blame] | 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2468 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2469 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 58f3919 | 2018-09-17 14:14:39 +0100 | [diff] [blame] | 2470 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2471 | |
| 2472 | // register the input connection slots for the layer, connections are made after all layers have been created |
| 2473 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2474 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2475 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2476 | |
| 2477 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 2478 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2479 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2480 | } |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2481 | armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo, |
| 2482 | const std::vector<int32_t> & targetDimsIn) |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2483 | { |
| 2484 | std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end()); |
| 2485 | const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1); |
| 2486 | |
| 2487 | if (stretchDim != targetDimsIn.end()) |
| 2488 | { |
| 2489 | if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end()) |
| 2490 | { |
| 2491 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2492 | fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString())); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | auto targetNumElements = |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 2496 | armnn::numeric_cast<unsigned int>( |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2497 | std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>())); |
| 2498 | |
| 2499 | auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim)); |
| 2500 | outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements; |
| 2501 | } |
| 2502 | |
| 2503 | TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data()); |
| 2504 | |
| 2505 | TensorInfo reshapeInfo = inputTensorInfo; |
| 2506 | reshapeInfo.SetShape(outputShape); |
| 2507 | |
| 2508 | return reshapeInfo; |
| 2509 | } |
| 2510 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2511 | void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex) |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2512 | { |
| 2513 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2514 | |
| 2515 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2516 | |
| 2517 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2518 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2519 | |
| 2520 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2521 | const auto * options = operatorPtr->builtin_options.AsReshapeOptions(); |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2522 | auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2523 | |
| 2524 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2525 | armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2526 | CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0"); |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2527 | |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2528 | // Extracting new shape for the output |
| 2529 | // There are two ways it can be passed |
| 2530 | // * First is to define the target shape in the operator built-in options |
| 2531 | // * Second is to pass it as a second input tensor |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2532 | std::vector<int32_t> targetShape; |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2533 | bool targetShapeFound = false; |
| 2534 | // Check if built-in options were given |
| 2535 | if (options != nullptr) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2536 | { |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2537 | // make sure the parameter is given |
| 2538 | if (options->new_shape.empty() == false) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2539 | { |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2540 | targetShape = options->new_shape; |
| 2541 | targetShapeFound = true; |
Derek Lamberti | f4a953f | 2020-03-17 14:25:57 +0000 | [diff] [blame] | 2542 | } |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2543 | } |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2544 | |
| 2545 | // If there is no built-in option given or if the built-in new_shape parameter was empty |
| 2546 | if (!targetShapeFound) |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2547 | { |
Teresa Charlin | 6a056a4 | 2021-12-01 10:25:43 +0000 | [diff] [blame] | 2548 | // Check for a second input tensor |
| 2549 | if (inputs.size() > 1 && inputs[1] != nullptr) |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2550 | { |
| 2551 | if (inputs[1]->is_variable) |
| 2552 | { |
| 2553 | ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported"); |
| 2554 | } |
| 2555 | |
| 2556 | if (inputs[1]->shape.size() != 1) |
| 2557 | { |
| 2558 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor"); |
| 2559 | } |
| 2560 | |
| 2561 | if (inputs[1]->type != tflite::TensorType_INT32) |
| 2562 | { |
| 2563 | ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type"); |
| 2564 | } |
| 2565 | |
Teresa Charlin | 6a056a4 | 2021-12-01 10:25:43 +0000 | [diff] [blame] | 2566 | // Extract target shape from input |
| 2567 | auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2568 | auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data()); |
Cathal Corbett | d2f7323 | 2021-12-10 13:38:52 +0000 | [diff] [blame] | 2569 | if (values) |
Sadik Armagan | 19a1c03 | 2021-01-20 12:17:00 +0000 | [diff] [blame] | 2570 | { |
Cathal Corbett | d2f7323 | 2021-12-10 13:38:52 +0000 | [diff] [blame] | 2571 | for (int i = 0; i < inputs[1]->shape[0]; ++i) |
| 2572 | { |
| 2573 | targetShape.push_back(values[i]); |
| 2574 | } |
Sadik Armagan | 19a1c03 | 2021-01-20 12:17:00 +0000 | [diff] [blame] | 2575 | } |
Cathal Corbett | d2f7323 | 2021-12-10 13:38:52 +0000 | [diff] [blame] | 2576 | else |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2577 | { |
Cathal Corbett | d2f7323 | 2021-12-10 13:38:52 +0000 | [diff] [blame] | 2578 | try |
| 2579 | { |
| 2580 | // We attempt to infer during Runtime. |
| 2581 | TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape(); |
| 2582 | // The parser only supports shape (batch, -1) or (-1) for non-constant shape input. |
| 2583 | if (reshapeShapes[0] > 2) |
| 2584 | { |
| 2585 | throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. " |
| 2586 | "When inferring during runtime, the parser only supports " |
| 2587 | "shape (batch, -1) or (-1) for target shape input.", |
| 2588 | reshapeShapes[0], |
| 2589 | layerName, |
| 2590 | CHECK_LOCATION().AsString())); |
| 2591 | } |
| 2592 | |
| 2593 | const int32_t numInputElements = inputTensorInfo.GetNumElements(); |
| 2594 | const int32_t inputTensorShape = inputTensorInfo.GetShape()[0]; |
| 2595 | if (reshapeShapes[0] == 1) |
| 2596 | { |
| 2597 | targetShape = {numInputElements}; |
| 2598 | } |
| 2599 | else if (reshapeShapes[0] == 2) |
| 2600 | { |
| 2601 | targetShape = {inputTensorShape, numInputElements / inputTensorShape}; |
| 2602 | } |
| 2603 | } |
| 2604 | catch (const std::exception& exc) |
| 2605 | { |
| 2606 | ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for " |
| 2607 | "Reshape operation. Reshape operator target shape input buffer data " |
| 2608 | "is null. " << exc.what()); |
| 2609 | } |
Jan Eilers | bac9b35 | 2020-07-13 13:40:24 +0100 | [diff] [blame] | 2610 | } |
| 2611 | } |
| 2612 | else |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2613 | { |
| 2614 | ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. " |
| 2615 | "At least one method required"); |
| 2616 | } |
Derek Lamberti | c9e5279 | 2020-03-11 11:42:26 +0000 | [diff] [blame] | 2617 | } |
| 2618 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2619 | armnn::TensorInfo reshapeOutputTensorInfo = |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2620 | TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2621 | |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2622 | // 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] | 2623 | const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape(); |
| 2624 | if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape)) |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2625 | { |
| 2626 | std::stringstream ss; |
| 2627 | ss << "New shape defined in reshape parameters " |
Aron Virginas-Tar | 70672f6 | 2019-01-23 14:00:00 +0000 | [diff] [blame] | 2628 | << reshapeOutputTensorShape |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2629 | << " does not equal output shape " |
| 2630 | << actualOutputTensorInfo.GetShape() |
| 2631 | << ": " |
| 2632 | << CHECK_LOCATION().AsString(); |
| 2633 | throw ParseException(ss.str()); |
| 2634 | } |
| 2635 | |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2636 | ReshapeDescriptor reshapeDesc; |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2637 | reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape(); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2638 | |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2639 | IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2640 | ARMNN_ASSERT(layer != nullptr); |
kevmay01 | 71972a8 | 2018-12-17 14:28:03 +0000 | [diff] [blame] | 2641 | layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo); |
Sadik | b94967b | 2018-09-19 15:30:00 +0100 | [diff] [blame] | 2642 | |
| 2643 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2644 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2645 | |
| 2646 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2647 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2648 | } |
| 2649 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2650 | void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex) |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2651 | { |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2652 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear); |
| 2653 | } |
| 2654 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2655 | void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2656 | { |
| 2657 | ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor); |
| 2658 | } |
| 2659 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2660 | void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod) |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2661 | { |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2662 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2663 | |
| 2664 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2665 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 2666 | |
| 2667 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2668 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2669 | |
| 2670 | armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]); |
| 2671 | |
| 2672 | // Data for the parsed tensor args (size) must be stored locally. |
| 2673 | std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements()); |
| 2674 | |
| 2675 | BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 2676 | ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes()); |
| 2677 | |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2678 | ResizeDescriptor desc; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2679 | desc.m_Method = resizeMethod; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2680 | desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]); |
Aron Virginas-Tar | 169d2f1 | 2019-07-01 19:01:44 +0100 | [diff] [blame] | 2681 | desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]); |
| 2682 | desc.m_DataLayout = armnn::DataLayout::NHWC; |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2683 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2684 | auto layerName = fmt::format("Resize:"); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2685 | |
| 2686 | switch (resizeMethod) |
| 2687 | { |
| 2688 | case ResizeMethod::Bilinear: |
| 2689 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2690 | layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex); |
Sang-Hoon Park | 820eb14 | 2020-01-08 10:25:24 +0000 | [diff] [blame] | 2691 | |
| 2692 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2693 | const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions(); |
| 2694 | |
David Monahan | 4a0c9b9 | 2020-05-30 09:48:39 +0100 | [diff] [blame] | 2695 | desc.m_AlignCorners = options->align_corners; |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2696 | break; |
| 2697 | } |
| 2698 | case ResizeMethod::NearestNeighbor: |
| 2699 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2700 | layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } |
| 2703 | default: |
| 2704 | { |
| 2705 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2706 | fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ", |
| 2707 | static_cast<int>(resizeMethod), CHECK_LOCATION().AsString())); |
Sadik Armagan | a3b31f0 | 2019-12-05 09:08:53 +0000 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2711 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2712 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2713 | CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0"); |
| 2714 | |
| 2715 | IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str()); |
| 2716 | ARMNN_ASSERT(layer != nullptr); |
Bruno Goncalves | 3f58ddb | 2019-02-07 18:40:11 -0200 | [diff] [blame] | 2717 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2718 | |
| 2719 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2720 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 2721 | |
| 2722 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2723 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 2724 | } |
| 2725 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2726 | void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2727 | { |
| 2728 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2729 | |
| 2730 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2731 | const auto * options = operatorPtr->builtin_options.AsConcatenationOptions(); |
| 2732 | |
| 2733 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2734 | |
| 2735 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2736 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2737 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2738 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2739 | unsigned int numConcatView = static_cast<unsigned int>(inputs.size()); |
| 2740 | uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions(); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2741 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2742 | const unsigned int concatDimInput = static_cast<unsigned int>( |
| 2743 | (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank)); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2744 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2745 | OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank); |
| 2746 | concatDescriptor.SetConcatAxis(concatDimInput); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2747 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2748 | unsigned int mergeDimOrigin = 0; |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2749 | |
| 2750 | for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex) |
| 2751 | { |
| 2752 | TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]); |
| 2753 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2754 | // This set up concatDescriptor view origin |
| 2755 | armnnUtils::ProcessConcatInputTensorInfo( |
| 2756 | inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2759 | auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2760 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2761 | |
Jim Flynn | 906f946 | 2019-05-10 13:55:21 +0100 | [diff] [blame] | 2762 | IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str()); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2763 | ARMNN_ASSERT(layer != nullptr); |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2764 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2765 | |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 2766 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2767 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2768 | |
Nattapat Chaimanowong | 5e9d298 | 2019-01-25 13:20:39 +0000 | [diff] [blame] | 2769 | // add fused activation layer |
| 2770 | layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function); |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2771 | |
Sadik Armagan | 479045b | 2018-10-01 11:51:37 +0100 | [diff] [blame] | 2772 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2773 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2774 | } |
| 2775 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2776 | void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2777 | { |
| 2778 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2779 | |
| 2780 | const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2781 | const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions(); |
| 2782 | |
| 2783 | CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex); |
| 2784 | |
| 2785 | FullyConnectedDescriptor desc; |
| 2786 | desc.m_BiasEnabled = false; |
Nattapat Chaimanowong | d8eee59 | 2018-10-26 10:24:14 +0100 | [diff] [blame] | 2787 | desc.m_TransposeWeightMatrix = true; |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2788 | |
| 2789 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2790 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2791 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2792 | |
| 2793 | armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]); |
| 2794 | |
| 2795 | // Fully Connected Layer accepts two dimensional weights input |
| 2796 | int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions()); |
| 2797 | if (weightsDimension != 2) |
| 2798 | { |
| 2799 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2800 | fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. " |
| 2801 | "Node {}", |
| 2802 | weightsDimension, |
| 2803 | CHECK_LOCATION().AsString())); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2804 | } |
| 2805 | |
Matthew Jackson | 74bf7da | 2019-08-16 16:51:42 +0100 | [diff] [blame] | 2806 | armnn::IConnectableLayer* layer = nullptr; |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2807 | auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2808 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2809 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2810 | // Add the first input tensor to the registration list |
| 2811 | std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]}; |
| 2812 | std::vector<unsigned int> ignoreInputWhenRegister = {}; |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2813 | |
| 2814 | desc.m_ConstantWeights = IsConstTensor(inputs[1]); |
| 2815 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2816 | // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant. |
| 2817 | tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2818 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2819 | if (inputs.size() == 3) |
| 2820 | { |
| 2821 | desc.m_BiasEnabled = true; |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2822 | |
| 2823 | // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers. |
| 2824 | tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2825 | } |
| 2826 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 2827 | // Filters and biases are always passed to fully connected as inputs |
| 2828 | layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str()); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2829 | |
| 2830 | ARMNN_ASSERT(layer != nullptr); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2831 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2832 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2833 | unsigned int startingSlotIndex = 0; |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2834 | if (inputTensorInfo.GetNumDimensions() > 2) |
| 2835 | { |
| 2836 | // Add reshape to flatten to 2D [batch_size, input_size], |
| 2837 | // where "input_size" corresponds to the number of inputs to the layer, |
| 2838 | // matching the second dimension of weights, |
| 2839 | // and "batch_size" is calculated by dividing the number of elements by "input_size". |
| 2840 | std::vector<unsigned int> reshapedDimensions(2); |
| 2841 | reshapedDimensions[1] = filterTensorInfo.GetShape()[1]; |
| 2842 | reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1]; |
| 2843 | |
| 2844 | if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0) |
| 2845 | { |
| 2846 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2847 | fmt::format("Failed to deduce input tensor shape from filter size {} {}", |
| 2848 | reshapedDimensions[1], |
| 2849 | CHECK_LOCATION().AsString())); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]); |
| 2853 | reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() }); |
| 2854 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2855 | std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName()); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2856 | armnn::ReshapeDescriptor reshapeDescriptor; |
| 2857 | reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape(); |
| 2858 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str()); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2859 | |
| 2860 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo); |
| 2861 | reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0)); |
| 2862 | |
| 2863 | RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]}); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2864 | // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots |
| 2865 | tensorIndexesToRegister.erase(tensorIndexesToRegister.begin()); |
| 2866 | startingSlotIndex = 1; |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2867 | } |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2868 | |
| 2869 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2870 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2871 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2872 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2873 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2874 | // we need to add the activation layer and fortunately we don't need to care about the data layout |
| 2875 | armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0, |
| 2876 | options->fused_activation_function); |
Narumol Prangnawarat | 501f4d4 | 2019-04-24 15:52:20 +0100 | [diff] [blame] | 2877 | |
Sadik Armagan | 8853c1f | 2018-10-22 09:04:18 +0100 | [diff] [blame] | 2878 | // register the output connection slots for the layer, connections are made after all layers have been created |
| 2879 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2880 | RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]}); |
| 2881 | } |
| 2882 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2883 | void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2884 | { |
| 2885 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2886 | |
| 2887 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2888 | |
| 2889 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2890 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2891 | CHECK_VALID_SIZE(outputs.size(), 4); |
| 2892 | |
| 2893 | // Obtain custom options from flexbuffers |
| 2894 | auto custom_options = operatorPtr->custom_options; |
| 2895 | const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap(); |
| 2896 | |
| 2897 | // Obtain descriptor information from tf lite |
| 2898 | DetectionPostProcessDescriptor desc; |
| 2899 | desc.m_MaxDetections = m["max_detections"].AsUInt32(); |
| 2900 | desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32(); |
| 2901 | desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat(); |
| 2902 | desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat(); |
| 2903 | desc.m_NumClasses = m["num_classes"].AsUInt32(); |
| 2904 | desc.m_ScaleH = m["h_scale"].AsFloat(); |
| 2905 | desc.m_ScaleW = m["w_scale"].AsFloat(); |
| 2906 | desc.m_ScaleX = m["x_scale"].AsFloat(); |
| 2907 | desc.m_ScaleY = m["y_scale"].AsFloat(); |
| 2908 | |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2909 | if (!(m["use_regular_nms"].IsNull())) |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2910 | { |
keidav01 | 07d58c7 | 2019-02-26 11:57:39 +0000 | [diff] [blame] | 2911 | desc.m_UseRegularNms = m["use_regular_nms"].AsBool(); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2912 | } |
| 2913 | if (!(m["detections_per_class"].IsNull())) |
| 2914 | { |
| 2915 | desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32(); |
| 2916 | } |
| 2917 | |
| 2918 | if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f) |
| 2919 | { |
| 2920 | throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold " |
| 2921 | "must be positive and less than or equal to 1."); |
| 2922 | } |
| 2923 | |
| 2924 | armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2925 | auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2926 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2927 | auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex); |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 2928 | IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData, |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2929 | layerName.c_str()); |
| 2930 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2931 | ARMNN_ASSERT(layer != nullptr); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2932 | |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2933 | // The model does not specify the output shapes. |
| 2934 | // The output shapes are calculated from the max_detection and max_classes_per_detection. |
| 2935 | unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection; |
| 2936 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 }); |
| 2937 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2938 | m_OverridenOutputShapes.push_back({ 1, numDetectedBox }); |
| 2939 | m_OverridenOutputShapes.push_back({ 1 }); |
| 2940 | |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2941 | for (unsigned int i = 0 ; i < outputs.size() ; ++i) |
| 2942 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 2943 | armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]); |
keidav01 | 1b3e2ea | 2019-02-21 10:07:37 +0000 | [diff] [blame] | 2944 | layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo); |
| 2945 | } |
| 2946 | |
| 2947 | // Register the input connection slots for the layer, connections are made after all layers have been created |
| 2948 | // only the tensors for the inputs are relevant, exclude the const tensors |
| 2949 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2950 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 2951 | |
| 2952 | // Register the output connection slots for the layer, connections are made after all layers have been created |
| 2953 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2954 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0], |
| 2955 | outputTensorIndexes[1], |
| 2956 | outputTensorIndexes[2], |
| 2957 | outputTensorIndexes[3]}); |
| 2958 | } |
| 2959 | |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2960 | /// The TfLite Pack operator is equivalent to the ArmNN Stack operator |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 2961 | void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex) |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2962 | { |
| 2963 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 2964 | |
| 2965 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 2966 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 2967 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 2968 | |
| 2969 | if (inputs.size() < 1) |
| 2970 | { |
| 2971 | throw ParseException("Pack must have at least one input."); |
| 2972 | } |
| 2973 | |
| 2974 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 2975 | const auto* options = operatorPtr->builtin_options.AsPackOptions(); |
| 2976 | |
| 2977 | StackDescriptor desc; |
| 2978 | desc.m_Axis = static_cast<uint32_t>(options->axis); |
| 2979 | desc.m_NumInputs = static_cast<uint32_t>(inputs.size()); |
| 2980 | |
| 2981 | // Use the tensor shape of the first input as the "correct" input shape in the descriptor |
| 2982 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 2983 | desc.m_InputShape = inputTensorInfo.GetShape(); |
| 2984 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 2985 | auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2986 | IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str()); |
| 2987 | |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 2988 | ARMNN_ASSERT(layer != nullptr); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2989 | |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 2990 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
Matthew Jackson | bcca1f4 | 2019-07-16 11:39:21 +0100 | [diff] [blame] | 2991 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 2992 | |
| 2993 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2994 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes}); |
| 2995 | |
| 2996 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 2997 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 2998 | } |
| 2999 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3000 | void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex) |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3001 | { |
| 3002 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3003 | |
| 3004 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3005 | const auto * options = operatorPtr->builtin_options.AsUnpackOptions(); |
| 3006 | |
| 3007 | // This unpackAxis indicates the axis to unpack |
| 3008 | const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis); |
| 3009 | |
| 3010 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3011 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 3012 | |
| 3013 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3014 | |
| 3015 | if (unpackAxis >= inputTensorInfo.GetNumDimensions()) |
| 3016 | { |
| 3017 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3018 | fmt::format("The unpack axis: {} cannot be greater than or equal to " |
| 3019 | "the number of input dimension {} {}", |
| 3020 | unpackAxis, |
| 3021 | inputTensorInfo.GetNumDimensions(), |
| 3022 | CHECK_LOCATION().AsString())); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3023 | } |
| 3024 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3025 | unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num); |
| 3026 | // If num is not defined, automatically infer from the length of the dimension axis. |
| 3027 | if(unpackNum == 0) |
| 3028 | { |
| 3029 | unpackNum = inputTensorInfo.GetShape()[unpackAxis]; |
| 3030 | } |
| 3031 | |
| 3032 | // If unpack number cannot be inferred and is still zero, throw ParseException. |
| 3033 | if(unpackNum == 0) |
| 3034 | { |
| 3035 | throw ParseException("Number to unpack must greater than zero."); |
| 3036 | } |
| 3037 | |
| 3038 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3039 | CHECK_VALID_SIZE(outputs.size(), unpackNum); |
| 3040 | |
| 3041 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
| 3042 | std::vector<unsigned int> unpackDimSizes(inputDimSize); |
| 3043 | |
| 3044 | // Add current input shape to unpackDimSizes |
| 3045 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 3046 | { |
| 3047 | unpackDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 3048 | } |
| 3049 | |
| 3050 | if (unpackDimSizes[unpackAxis] != unpackNum) |
| 3051 | { |
| 3052 | throw ParseException("Number to unpack must be the same as length of the dimension to " |
| 3053 | "unpack along."); |
| 3054 | } |
| 3055 | |
| 3056 | unpackDimSizes[unpackAxis] /= unpackNum; |
| 3057 | |
| 3058 | SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size())); |
| 3059 | for (unsigned int j = 0; j < unpackNum; ++j) |
| 3060 | { |
| 3061 | // Set the size of the views. |
| 3062 | for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx) |
| 3063 | { |
| 3064 | splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]); |
| 3065 | } |
| 3066 | splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j); |
| 3067 | } |
| 3068 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3069 | auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3070 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 3071 | ARMNN_ASSERT(layer != nullptr); |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3072 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3073 | TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()), |
| 3074 | unpackDimSizes.data()); |
| 3075 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3076 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3077 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3078 | |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 3079 | std::vector<unsigned int> reshapeDims; |
| 3080 | for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis) |
| 3081 | { |
| 3082 | if (axis != unpackAxis) |
| 3083 | { |
| 3084 | reshapeDims.push_back(splitOutShape[axis]); |
| 3085 | } |
| 3086 | } |
| 3087 | |
| 3088 | TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data()); |
| 3089 | |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3090 | // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter. |
| 3091 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 3092 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 3093 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true); |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3094 | std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName()); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3095 | armnn::ReshapeDescriptor desc; |
Finn Williams | b49ed18 | 2021-06-29 15:50:08 +0100 | [diff] [blame] | 3096 | desc.m_TargetShape = reshapeOutputShape; |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3097 | armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str()); |
| 3098 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 3099 | layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape, |
| 3100 | outputTensorInfo.GetDataType(), |
| 3101 | outputTensorInfo.GetQuantizationScale(), |
| 3102 | outputTensorInfo.GetQuantizationOffset())); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3103 | layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0)); |
| 3104 | |
Narumol Prangnawarat | 2c52646 | 2019-10-21 14:58:26 +0100 | [diff] [blame] | 3105 | reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
Narumol Prangnawarat | 672de57 | 2019-04-23 15:28:06 +0100 | [diff] [blame] | 3106 | |
| 3107 | uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]); |
| 3108 | armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0)); |
| 3109 | RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot); |
| 3110 | } |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3111 | } |
| 3112 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3113 | void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex) |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3114 | { |
| 3115 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3116 | |
| 3117 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3118 | const auto * options = operatorPtr->builtin_options.AsSplitOptions(); |
| 3119 | |
| 3120 | const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits); |
| 3121 | |
Nina Drozd | 200e380 | 2019-04-15 09:47:39 +0100 | [diff] [blame] | 3122 | // If number of splits cannot be inferred and is zero, throw ParseException. |
| 3123 | if(numSplits == 0) |
| 3124 | { |
| 3125 | throw ParseException("Number to splits must greater than zero."); |
| 3126 | } |
| 3127 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3128 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3129 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3130 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3131 | CHECK_VALID_SIZE(outputs.size(), numSplits); |
| 3132 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3133 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]); |
| 3134 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]); |
| 3135 | ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3136 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 3137 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3138 | if (axisBufferPtr == nullptr) |
| 3139 | { |
| 3140 | throw ParseException( |
| 3141 | fmt::format("Operation has invalid inputs. Failed to read axis. {}", |
| 3142 | CHECK_LOCATION().AsString())); |
| 3143 | } |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 3144 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3145 | std::vector<int32_t> axisData(axisTensorInfo.GetNumElements()); |
| 3146 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
| 3147 | int32_t axis = axisData[0]; |
| 3148 | |
| 3149 | auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); |
| 3150 | if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) |
| 3151 | { |
| 3152 | // Square bracket denotes inclusive n while parenthesis denotes exclusive n |
| 3153 | // E.g. Rank 4 tensor can have axis in range [-4, 3) |
| 3154 | // -1 == 3, -2 == 2, -3 == 1, -4 == 0 |
| 3155 | throw ParseException( |
| 3156 | fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}", |
| 3157 | axis, |
| 3158 | CHECK_LOCATION().AsString())); |
| 3159 | } |
| 3160 | |
| 3161 | const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3162 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3163 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 3164 | if (inputDimSize > MaxNumOfTensorDimensions) |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3165 | { |
| 3166 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3167 | fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}", |
| 3168 | inputTensorInfo.GetNumDimensions(), |
| 3169 | MaxNumOfTensorDimensions, |
| 3170 | CHECK_LOCATION().AsString())); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3171 | } |
| 3172 | |
| 3173 | std::vector<unsigned int> splitterDimSizes(inputDimSize); |
| 3174 | |
| 3175 | // Add current input shape to splitterDimSizes |
| 3176 | for (unsigned int i = 0; i < inputDimSize; ++i) |
| 3177 | { |
| 3178 | splitterDimSizes[i] = inputTensorInfo.GetShape()[i]; |
| 3179 | } |
| 3180 | |
| 3181 | if (splitterDimSizes[splitDim] % numSplits != 0) |
| 3182 | { |
| 3183 | throw ParseException("Number of splits must evenly divide the dimension"); |
| 3184 | } |
| 3185 | splitterDimSizes[splitDim] /= numSplits; |
| 3186 | |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 3187 | SplitterDescriptor splitDesc(numSplits, inputDimSize); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3188 | for (unsigned int j = 0; j < numSplits; ++j) |
| 3189 | { |
| 3190 | // Set the size of the views. |
| 3191 | for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx) |
| 3192 | { |
| 3193 | splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]); |
| 3194 | } |
| 3195 | splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j); |
| 3196 | } |
| 3197 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3198 | auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3199 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 3200 | ARMNN_ASSERT(layer != nullptr); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3201 | |
| 3202 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
Narumol Prangnawarat | 17660e6 | 2019-04-18 16:56:19 +0100 | [diff] [blame] | 3203 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]}); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3204 | |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3205 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 3206 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 3207 | armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true); |
Francis Murtagh | 98d6b3d | 2019-10-21 10:52:54 +0100 | [diff] [blame] | 3208 | layer->GetOutputSlot(k).SetTensorInfo(tensorInfo); |
Nina Drozd | 0324f48 | 2019-04-08 10:52:10 +0100 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3212 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 3213 | } |
| 3214 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3215 | unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn) |
| 3216 | { |
| 3217 | int numDims = armnn::numeric_cast<int>(numDimsIn); |
| 3218 | int v = idx < 0 ? numDims + idx : idx; |
| 3219 | ARMNN_ASSERT(v >= 0); |
| 3220 | ARMNN_ASSERT(v < numDims); |
| 3221 | |
| 3222 | return static_cast<unsigned int>(v); |
| 3223 | } |
| 3224 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3225 | void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex) |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3226 | { |
| 3227 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3228 | |
| 3229 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3230 | const auto * options = operatorPtr->builtin_options.AsSplitVOptions(); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3231 | |
| 3232 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3233 | CHECK_VALID_SIZE(inputs.size(), 3); |
| 3234 | |
| 3235 | auto& inputTensor = inputs[0]; |
| 3236 | auto& splitsTensor = inputs[1]; |
| 3237 | auto& axisTensor = inputs[2]; |
| 3238 | |
| 3239 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor); |
| 3240 | armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor); |
| 3241 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor); |
| 3242 | ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1); |
| 3243 | |
| 3244 | // Inputs |
| 3245 | auto inputDimSize = inputTensorInfo.GetNumDimensions(); |
| 3246 | if (inputDimSize > MaxNumOfTensorDimensions) |
| 3247 | { |
| 3248 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3249 | fmt::format("The number of dimensions: {} for input tensors of the " |
| 3250 | "SplitV op cannot be greater than {} {}", |
| 3251 | inputTensorInfo.GetNumDimensions(), |
| 3252 | MaxNumOfTensorDimensions, |
| 3253 | CHECK_LOCATION().AsString())); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3254 | } |
| 3255 | |
| 3256 | // Get split axis |
| 3257 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3258 | if (axisBufferPtr == nullptr) |
| 3259 | { |
| 3260 | throw ParseException( |
| 3261 | fmt::format("Operation has invalid inputs. Failed to read axis. {}", |
| 3262 | CHECK_LOCATION().AsString())); |
| 3263 | } |
| 3264 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3265 | std::vector<int> axisData(axisTensorInfo.GetNumElements()); |
| 3266 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3267 | int32_t axis = axisData[0]; |
| 3268 | |
| 3269 | auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); |
| 3270 | if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) |
| 3271 | { |
| 3272 | // Square bracket denotes inclusive n while parenthesis denotes exclusive n |
| 3273 | // E.g. Rank 4 tensor can have axis in range [-4, 3) |
| 3274 | // -1 == 3, -2 == 2, -3 == 1, -4 == 0 |
| 3275 | throw ParseException( |
| 3276 | fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}", |
| 3277 | axis, |
| 3278 | CHECK_LOCATION().AsString())); |
| 3279 | } |
| 3280 | const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions()); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3281 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3282 | // Set split sizes |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3283 | CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3284 | unsigned int numSplits{0}; |
| 3285 | |
| 3286 | if(options) |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3287 | { |
| 3288 | numSplits = CHECKED_NON_NEGATIVE(options->num_splits); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3289 | } |
| 3290 | else |
| 3291 | { |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3292 | numSplits = splitsInfo.GetNumElements(); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3293 | } |
| 3294 | |
| 3295 | if (numSplits <=0) |
| 3296 | { |
| 3297 | throw ParseException("SplitV has invalid number of splits"); |
| 3298 | } |
| 3299 | |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 3300 | std::vector<int> splitsData(numSplits); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3301 | BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer); |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 3302 | ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes()); |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3303 | |
Jan Eilers | c0761e9 | 2020-06-29 16:48:44 +0100 | [diff] [blame] | 3304 | unsigned int idx = 0; |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3305 | int numInferred{0}; |
| 3306 | unsigned int inferIdx{0}; |
| 3307 | int splitSum{0}; |
| 3308 | for (auto split : splitsData) |
| 3309 | { |
| 3310 | if (split < 0) |
| 3311 | { |
| 3312 | numInferred++; |
| 3313 | inferIdx = idx; |
| 3314 | } |
| 3315 | else |
| 3316 | { |
| 3317 | splitSum += split; |
| 3318 | } |
| 3319 | idx++; |
| 3320 | } |
| 3321 | // Check for inferred Axis |
| 3322 | if (numInferred == 0) |
| 3323 | { |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 3324 | if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim])) |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3325 | { |
| 3326 | throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim."); |
| 3327 | } |
| 3328 | } |
| 3329 | else if (numInferred == 1) |
| 3330 | { |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 3331 | splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum; |
Ryan OShea | 8670473 | 2020-05-26 11:41:04 +0100 | [diff] [blame] | 3332 | } |
| 3333 | else |
| 3334 | { |
| 3335 | throw ParseException("Cannot infer split size for more than one split"); |
| 3336 | } |
| 3337 | |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3338 | //Ouput size validation |
| 3339 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3340 | CHECK_VALID_SIZE(outputs.size(), numSplits); |
| 3341 | |
| 3342 | // Setup Armnn descriptor |
| 3343 | SplitterDescriptor splitDesc(numSplits, inputDimSize); |
| 3344 | unsigned int accumSplit = 0; |
| 3345 | for (unsigned int j = 0; j < numSplits; ++j) |
| 3346 | { |
Matthew Sloyan | 589e3e8 | 2020-09-11 16:17:48 +0100 | [diff] [blame] | 3347 | unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3348 | |
| 3349 | // Set the size of the views. |
| 3350 | for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx) |
| 3351 | { |
| 3352 | unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx]; |
| 3353 | if (dimIdx == splitDim) |
| 3354 | { |
| 3355 | dimSize = splitSize; |
| 3356 | } |
| 3357 | splitDesc.SetViewSize(j, dimIdx, dimSize); |
| 3358 | } |
| 3359 | |
| 3360 | splitDesc.SetViewOriginCoord(j, splitDim, accumSplit); |
| 3361 | accumSplit += splitSize; |
| 3362 | } |
| 3363 | |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3364 | auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3365 | IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str()); |
James Conroy | 0510239 | 2020-06-24 15:39:55 +0100 | [diff] [blame] | 3366 | ARMNN_ASSERT(layer != nullptr); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3367 | |
| 3368 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3369 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3370 | |
| 3371 | for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k) |
| 3372 | { |
Sadik Armagan | d109a4d | 2020-07-28 10:42:13 +0100 | [diff] [blame] | 3373 | armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true); |
Derek Lamberti | f017699 | 2020-04-28 13:37:49 +0100 | [diff] [blame] | 3374 | layer->GetOutputSlot(k).SetTensorInfo(tensorInfo); |
| 3375 | } |
| 3376 | |
| 3377 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3378 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 3379 | } |
| 3380 | |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 3381 | void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex) |
| 3382 | { |
| 3383 | ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min); |
| 3384 | } |
| 3385 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3386 | void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex) |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 3387 | { |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 3388 | ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max); |
| 3389 | } |
| 3390 | |
| 3391 | void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction) |
| 3392 | { |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 3393 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3394 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3395 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3396 | |
| 3397 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3398 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3399 | |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 3400 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 3401 | armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]); |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 3402 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3403 | ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1); |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 3404 | |
| 3405 | // Check if output tensor type is Signed32 or Signed64 |
Mike Kelly | 1f140f7 | 2021-04-06 12:25:55 +0100 | [diff] [blame] | 3406 | if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 && |
| 3407 | outputTensorInfo.GetDataType() != armnn::DataType::Signed64) |
| 3408 | { |
| 3409 | throw ParseException( |
| 3410 | fmt::format( |
| 3411 | "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}", |
| 3412 | CHECK_LOCATION().AsString())); |
| 3413 | } |
Matthew Sloyan | 28f177c | 2021-04-09 14:38:52 +0100 | [diff] [blame] | 3414 | |
| 3415 | // Get const axis value from model and set it to descriptor. |
| 3416 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 3417 | if (axisBufferPtr == nullptr) |
| 3418 | { |
| 3419 | throw ParseException( |
| 3420 | fmt::format("Operation has invalid inputs. Failed to read axis. {}", |
| 3421 | CHECK_LOCATION().AsString())); |
| 3422 | } |
| 3423 | |
| 3424 | std::vector<int32_t> axisData(axisTensorInfo.GetNumElements()); |
| 3425 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes()); |
| 3426 | int32_t axis = axisData.front(); |
| 3427 | |
| 3428 | auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); |
| 3429 | if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) |
| 3430 | { |
| 3431 | // Square bracket denotes inclusive n while parenthesis denotes exclusive n |
| 3432 | // E.g. Rank 4 tensor can have axis in range [-4, 3) |
| 3433 | // -1 == 3, -2 == 2, -3 == 1, -4 == 0 |
| 3434 | throw ParseException( |
| 3435 | fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}", |
| 3436 | axis, |
| 3437 | CHECK_LOCATION().AsString())); |
| 3438 | } |
| 3439 | |
| 3440 | ArgMinMaxDescriptor desc; |
| 3441 | desc.m_Axis = axis; |
| 3442 | desc.m_Function = argMinMaxFunction; |
| 3443 | |
| 3444 | // Register a ArgMin/ArgMax layer. |
| 3445 | auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}"; |
| 3446 | auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex); |
| 3447 | IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str()); |
| 3448 | ARMNN_ASSERT(layer != nullptr); |
Inki Dae | d4619e2 | 2020-09-10 15:33:54 +0900 | [diff] [blame] | 3449 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3450 | |
| 3451 | // Register input tensor to the layer. |
| 3452 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3453 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3454 | |
| 3455 | // Register output tensor to the layer. |
| 3456 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3457 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 3458 | } |
| 3459 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3460 | void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3461 | { |
| 3462 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3463 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3464 | TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3465 | CHECK_VALID_SIZE(inputs.size(), 2); |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3466 | TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3467 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3468 | |
| 3469 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 3470 | armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]); |
| 3471 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 3472 | |
| 3473 | armnn::GatherDescriptor gatherDescriptor; |
| 3474 | |
| 3475 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3476 | const auto * options = operatorPtr->builtin_options.AsGatherOptions(); |
| 3477 | auto axis = options->axis; |
| 3478 | |
| 3479 | auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions()); |
| 3480 | auto indicesDimensions = indicesTensorInfo.GetNumDimensions(); |
| 3481 | auto outputDimensions = outputTensorInfo.GetNumDimensions(); |
| 3482 | if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0))) |
| 3483 | { |
| 3484 | throw ParseException( |
| 3485 | fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}", |
| 3486 | axis, |
| 3487 | inputDimensions, inputDimensions, |
| 3488 | CHECK_LOCATION().AsString())); |
| 3489 | } |
| 3490 | if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1) |
| 3491 | { |
| 3492 | throw ParseException( |
| 3493 | fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}", |
| 3494 | outputDimensions, |
| 3495 | inputDimensions, indicesDimensions, |
| 3496 | CHECK_LOCATION().AsString())); |
| 3497 | } |
| 3498 | |
| 3499 | gatherDescriptor.m_Axis = axis; |
| 3500 | |
| 3501 | auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex); |
| 3502 | IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str()); |
| 3503 | ARMNN_ASSERT(layer != nullptr); |
| 3504 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3505 | |
| 3506 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3507 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 3508 | |
| 3509 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3510 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 3511 | } |
| 3512 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3513 | void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3514 | { |
| 3515 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3516 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3517 | TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3518 | CHECK_VALID_SIZE(inputs.size(), 1); |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3519 | TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
Sadik Armagan | 2686849 | 2021-01-22 14:25:31 +0000 | [diff] [blame] | 3520 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3521 | |
| 3522 | armnn::DepthToSpaceDescriptor descriptor; |
| 3523 | |
| 3524 | const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3525 | const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions(); |
| 3526 | auto blockSize = options->block_size; |
| 3527 | if (blockSize < 2) |
| 3528 | { |
| 3529 | throw ParseException( |
| 3530 | fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}", |
| 3531 | blockSize, |
| 3532 | CHECK_LOCATION().AsString())); |
| 3533 | } |
| 3534 | descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize); |
| 3535 | |
| 3536 | auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex); |
| 3537 | IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str()); |
| 3538 | ARMNN_ASSERT(layer != nullptr); |
| 3539 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 3540 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3541 | |
| 3542 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3543 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3544 | |
| 3545 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3546 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 3547 | } |
| 3548 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3549 | void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex) |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3550 | { |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 3551 | ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum); |
| 3552 | } |
| 3553 | |
Teresa Charlin | 4e3e831 | 2021-08-05 12:34:37 +0100 | [diff] [blame] | 3554 | void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex) |
| 3555 | { |
| 3556 | ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod); |
| 3557 | } |
| 3558 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 3559 | void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex) |
| 3560 | { |
| 3561 | ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max); |
| 3562 | } |
| 3563 | |
| 3564 | void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex) |
| 3565 | { |
| 3566 | ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min); |
| 3567 | } |
| 3568 | |
| 3569 | void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation) |
| 3570 | { |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3571 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3572 | |
| 3573 | const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3574 | const auto *options = operatorPtr->builtin_options.AsReducerOptions(); |
| 3575 | |
| 3576 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3577 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3578 | |
| 3579 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3580 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3581 | |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 3582 | auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3583 | |
| 3584 | armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]); |
| 3585 | armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3586 | |
| 3587 | ReduceDescriptor desc; |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3588 | BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer); |
| 3589 | // Get const axis value from model and set it to descriptor. |
| 3590 | if (axisBufferPtr != nullptr) |
| 3591 | { |
Sadik Armagan | 49bdb79 | 2021-02-11 13:57:07 +0000 | [diff] [blame] | 3592 | std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements()); |
| 3593 | ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes()); |
| 3594 | |
| 3595 | // Convert the axis to unsigned int and remove duplicates. |
| 3596 | auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions()); |
| 3597 | std::set<unsigned int> uniqueAxis; |
| 3598 | std::transform(axisData.begin(), |
| 3599 | axisData.end(), |
| 3600 | std::inserter(uniqueAxis, uniqueAxis.begin()), |
| 3601 | [rank](int i)->unsigned int{ |
| 3602 | return static_cast<uint32_t>(((i + rank) % rank)); }); |
| 3603 | desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end()); |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3604 | } |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 3605 | else |
| 3606 | { |
| 3607 | for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i) |
| 3608 | { |
| 3609 | desc.m_vAxis.push_back(i); |
| 3610 | } |
| 3611 | } |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3612 | |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3613 | desc.m_KeepDims = options->keep_dims; |
Sadik Armagan | a274748 | 2021-02-09 10:28:54 +0000 | [diff] [blame] | 3614 | desc.m_ReduceOperation = reduceOperation; |
Sadik Armagan | 0c3ea5b | 2021-02-03 09:29:30 +0000 | [diff] [blame] | 3615 | |
| 3616 | // Register a new layer object, Sum. |
| 3617 | IConnectableLayer *layer = m_Network->AddReduceLayer(desc, layerName.c_str()); |
| 3618 | |
| 3619 | armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]); |
| 3620 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3621 | |
| 3622 | // Register input tensor to the layer. |
| 3623 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3624 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3625 | |
| 3626 | // Register output tensor to the layer. |
| 3627 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3628 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 3629 | } |
| 3630 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3631 | void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex) |
| 3632 | { |
| 3633 | ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs); |
| 3634 | } |
| 3635 | |
| 3636 | void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex) |
| 3637 | { |
| 3638 | ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp); |
| 3639 | } |
| 3640 | |
Mike Kelly | 31dce2b | 2021-09-01 21:22:37 +0100 | [diff] [blame] | 3641 | void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex) |
| 3642 | { |
| 3643 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3644 | |
| 3645 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3646 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 3647 | |
| 3648 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3649 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3650 | |
| 3651 | auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex); |
| 3652 | std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex); |
| 3653 | |
| 3654 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 3655 | |
| 3656 | const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex]; |
| 3657 | const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions(); |
| 3658 | |
| 3659 | armnn::NormalizationDescriptor descriptor; |
| 3660 | descriptor.m_DataLayout = armnn::DataLayout::NHWC; |
| 3661 | descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across; |
| 3662 | descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness; |
| 3663 | descriptor.m_NormSize = static_cast<uint32_t>(options->radius); |
| 3664 | descriptor.m_K = options->bias; |
| 3665 | descriptor.m_Alpha = options->alpha; |
| 3666 | descriptor.m_Beta = options->beta; |
| 3667 | |
| 3668 | // ArmNN expects normSize to be the full size of the normalization |
| 3669 | // window rather than the radius as in TfLite. |
| 3670 | descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize); |
| 3671 | |
| 3672 | IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str()); |
| 3673 | ARMNN_ASSERT(layer != nullptr); |
| 3674 | |
| 3675 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 3676 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3677 | |
| 3678 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3679 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3680 | |
| 3681 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3682 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 3683 | } |
| 3684 | |
Matthew Sloyan | ed7fce4 | 2021-04-15 20:46:24 +0100 | [diff] [blame] | 3685 | void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex) |
| 3686 | { |
| 3687 | ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot); |
| 3688 | } |
| 3689 | |
| 3690 | void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex) |
| 3691 | { |
| 3692 | ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg); |
| 3693 | } |
| 3694 | |
| 3695 | void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex) |
| 3696 | { |
| 3697 | ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt); |
| 3698 | } |
| 3699 | |
| 3700 | void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation) |
| 3701 | { |
| 3702 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3703 | |
| 3704 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3705 | CHECK_VALID_SIZE(inputs.size(), 1); |
| 3706 | |
| 3707 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3708 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3709 | |
| 3710 | std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}"; |
| 3711 | std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex); |
| 3712 | |
| 3713 | ElementwiseUnaryDescriptor desc; |
| 3714 | desc.m_Operation = unaryOperation; |
| 3715 | IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str()); |
| 3716 | ARMNN_ASSERT(layer != nullptr); |
| 3717 | |
| 3718 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 3719 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3720 | |
| 3721 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3722 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]}); |
| 3723 | |
| 3724 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3725 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes); |
| 3726 | } |
| 3727 | |
Bruno Goncalves | 2d0eb86 | 2021-07-11 14:10:15 -0300 | [diff] [blame] | 3728 | void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex) |
| 3729 | { |
| 3730 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal); |
| 3731 | } |
| 3732 | |
| 3733 | void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex) |
| 3734 | { |
| 3735 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual); |
| 3736 | } |
| 3737 | |
| 3738 | void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex) |
| 3739 | { |
| 3740 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater); |
| 3741 | } |
| 3742 | |
| 3743 | void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex) |
| 3744 | { |
| 3745 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual); |
| 3746 | } |
| 3747 | |
| 3748 | void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex) |
| 3749 | { |
| 3750 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less); |
| 3751 | } |
| 3752 | |
| 3753 | void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex) |
| 3754 | { |
| 3755 | ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual); |
| 3756 | } |
| 3757 | |
| 3758 | void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex, |
| 3759 | ComparisonOperation comparisonOperation) |
| 3760 | { |
| 3761 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
| 3762 | |
| 3763 | auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex); |
| 3764 | CHECK_VALID_SIZE(inputs.size(), 2); |
| 3765 | |
| 3766 | auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex); |
| 3767 | CHECK_VALID_SIZE(outputs.size(), 1); |
| 3768 | |
| 3769 | auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}"; |
| 3770 | std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex); |
| 3771 | |
| 3772 | armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]); |
| 3773 | armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]); |
| 3774 | CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1"); |
| 3775 | |
| 3776 | ComparisonDescriptor desc; |
| 3777 | desc.m_Operation = comparisonOperation; |
| 3778 | IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str()); |
| 3779 | ARMNN_ASSERT(layer != nullptr); |
| 3780 | |
| 3781 | TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true); |
| 3782 | layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo); |
| 3783 | |
| 3784 | auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3785 | RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]}); |
| 3786 | |
| 3787 | auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex)); |
| 3788 | RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]}); |
| 3789 | } |
| 3790 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3791 | armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer, |
| 3792 | unsigned int outputSlot, |
| 3793 | tflite::ActivationFunctionType activationType) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3794 | { |
| 3795 | ActivationDescriptor activationDesc; |
| 3796 | std::string layerName = prevLayer->GetName(); |
| 3797 | |
| 3798 | switch(activationType) |
| 3799 | { |
| 3800 | case tflite::ActivationFunctionType_NONE: |
| 3801 | { |
| 3802 | // this is a no-op: return previous layer |
| 3803 | return prevLayer; |
| 3804 | } |
| 3805 | case tflite::ActivationFunctionType_RELU: |
| 3806 | { |
| 3807 | activationDesc.m_Function = ActivationFunction::ReLu; |
| 3808 | layerName += ":RELU"; |
| 3809 | break; |
| 3810 | } |
| 3811 | case tflite::ActivationFunctionType_RELU6: |
| 3812 | { |
| 3813 | activationDesc.m_Function = ActivationFunction::BoundedReLu; |
| 3814 | activationDesc.m_A = 6.0f; |
| 3815 | activationDesc.m_B = 0.0f; |
| 3816 | layerName += ":RELU6"; |
| 3817 | break; |
| 3818 | } |
| 3819 | case tflite::ActivationFunctionType_TANH: |
| 3820 | { |
| 3821 | activationDesc.m_Function = ActivationFunction::TanH; |
| 3822 | activationDesc.m_A = 1.0f; |
| 3823 | activationDesc.m_B = 1.0f; |
| 3824 | layerName += ":TANH"; |
| 3825 | break; |
| 3826 | } |
| 3827 | |
| 3828 | // I only put these here as a reminder what others we could support |
| 3829 | case tflite::ActivationFunctionType_RELU_N1_TO_1: |
| 3830 | case tflite::ActivationFunctionType_SIGN_BIT: |
| 3831 | default: |
| 3832 | { |
| 3833 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3834 | fmt::format("TfLite parser doesn't suppport fused activation: " |
| 3835 | "{}/{} {} ", |
| 3836 | activationType, |
| 3837 | tflite::EnumNameActivationFunctionType(activationType), |
| 3838 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3839 | |
| 3840 | } |
| 3841 | } |
| 3842 | |
| 3843 | IConnectableLayer* activationLayer = |
| 3844 | m_Network->AddActivationLayer(activationDesc, layerName.c_str()); |
| 3845 | |
| 3846 | auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot); |
| 3847 | prevOutputSlot.Connect(activationLayer->GetInputSlot(0)); |
| 3848 | activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo()); |
| 3849 | return activationLayer; |
| 3850 | } |
| 3851 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3852 | TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char * fileName) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3853 | { |
| 3854 | if (fileName == nullptr) |
| 3855 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3856 | throw InvalidArgumentException(fmt::format("Invalid (null) file name {}", |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3857 | CHECK_LOCATION().AsString())); |
| 3858 | } |
Francis Murtagh | 532a29d | 2020-06-29 11:50:01 +0100 | [diff] [blame] | 3859 | std::error_code errorCode; |
| 3860 | fs::path pathToFile(fileName); |
| 3861 | if (!fs::exists(pathToFile, errorCode)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3862 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3863 | //fmt::format() could not be used here (format error) |
| 3864 | std::stringstream msg; |
| 3865 | msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode |
| 3866 | << " " << CHECK_LOCATION().AsString(); |
| 3867 | |
| 3868 | throw FileNotFoundException(msg.str()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3869 | } |
| 3870 | std::ifstream file(fileName, std::ios::binary); |
| 3871 | std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>()); |
| 3872 | return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()), |
| 3873 | fileContent.size()); |
| 3874 | } |
| 3875 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3876 | TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t * binaryContent, size_t len) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3877 | { |
| 3878 | if (binaryContent == nullptr) |
| 3879 | { |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3880 | throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}", |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3881 | CHECK_LOCATION().AsString())); |
| 3882 | } |
| 3883 | flatbuffers::Verifier verifier(binaryContent, len); |
| 3884 | if (verifier.VerifyBuffer<tflite::Model>() == false) |
| 3885 | { |
| 3886 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 3887 | fmt::format("Buffer doesn't conform to the expected Tensorflow Lite " |
| 3888 | "flatbuffers format. size:{} {}", |
| 3889 | len, |
| 3890 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3891 | } |
| 3892 | return tflite::UnPackModel(binaryContent); |
| 3893 | } |
| 3894 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3895 | TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr & model, |
| 3896 | size_t subgraphIndex, |
| 3897 | size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3898 | { |
| 3899 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 3900 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3901 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3902 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3903 | |
| 3904 | size_t inputCount = operatorPtr->inputs.size(); |
mathad01 | c21025d | 2021-04-26 10:09:37 +0100 | [diff] [blame] | 3905 | TensorRawPtrVector result; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3906 | for (size_t i=0; i<inputCount; ++i) |
| 3907 | { |
mathad01 | c21025d | 2021-04-26 10:09:37 +0100 | [diff] [blame] | 3908 | // If the input location is -1 then assume input is turned off. |
| 3909 | if (operatorPtr->inputs[i] == -1) |
| 3910 | { |
| 3911 | continue; |
| 3912 | } |
| 3913 | else |
| 3914 | { |
| 3915 | uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]); |
| 3916 | result.push_back(subgraphPtr->tensors[inputId].get()); |
| 3917 | } |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3918 | } |
| 3919 | return result; |
| 3920 | } |
| 3921 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3922 | TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr & model, |
| 3923 | size_t subgraphIndex, |
| 3924 | size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3925 | { |
| 3926 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
| 3927 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3928 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3929 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3930 | |
| 3931 | size_t outputCount = operatorPtr->outputs.size(); |
| 3932 | TensorRawPtrVector result(outputCount); |
| 3933 | for (size_t i=0; i<outputCount; ++i) |
| 3934 | { |
| 3935 | uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]); |
| 3936 | CHECK_TENSOR(model, subgraphIndex, outputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3937 | result[i] = subgraphPtr->tensors[outputId].get(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3938 | } |
| 3939 | return result; |
| 3940 | } |
| 3941 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3942 | TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr & model, |
| 3943 | size_t subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3944 | { |
| 3945 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3946 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3947 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3948 | size_t inputCount = subgraphPtr->inputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3949 | TensorIdRawPtrVector result(inputCount); |
| 3950 | for (size_t i=0; i<inputCount; ++i) |
| 3951 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3952 | uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3953 | CHECK_TENSOR(model, subgraphIndex, inputId); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3954 | result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3955 | } |
| 3956 | return result; |
| 3957 | } |
| 3958 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3959 | TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr & model, |
| 3960 | size_t subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3961 | { |
| 3962 | CHECK_SUBGRAPH(model, subgraphIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3963 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3964 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3965 | size_t outputCount = subgraphPtr->outputs.size(); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3966 | TensorIdRawPtrVector result(outputCount); |
| 3967 | for (size_t i=0; i<outputCount; ++i) |
| 3968 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3969 | uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]); |
| 3970 | result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get()); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3971 | } |
| 3972 | return result; |
| 3973 | } |
| 3974 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3975 | std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model, |
| 3976 | size_t subgraphIndex, |
| 3977 | size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3978 | { |
| 3979 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3980 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3981 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3982 | return operatorPtr->inputs; |
| 3983 | } |
| 3984 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3985 | std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model, |
| 3986 | size_t subgraphIndex, |
| 3987 | size_t operatorIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3988 | { |
| 3989 | CHECK_MODEL(model, subgraphIndex, operatorIndex); |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 3990 | const auto & subgraphPtr = model->subgraphs[subgraphIndex]; |
| 3991 | const auto & operatorPtr = subgraphPtr->operators[operatorIndex]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 3992 | return operatorPtr->outputs; |
| 3993 | } |
| 3994 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 3995 | void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex, |
| 3996 | size_t operatorIndex, |
| 3997 | IConnectableLayer* layer, |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 3998 | const std::vector<unsigned int>& tensorIndexes, |
| 3999 | unsigned int startingSlotIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4000 | { |
| 4001 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 4002 | ARMNN_ASSERT(layer != nullptr); |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4003 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4004 | if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots()) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4005 | { |
| 4006 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 4007 | fmt::format("The number of tensor inputs ({}) does not match the number expected ({})" |
| 4008 | " for subgraph:{} operator index:{} {}", |
| 4009 | tensorIndexes.size(), |
| 4010 | layer->GetNumInputSlots(), |
| 4011 | subgraphIndex, |
| 4012 | operatorIndex, |
| 4013 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4014 | } |
| 4015 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4016 | for (unsigned int index = 0; index < tensorIndexes.size() ; ++index) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4017 | { |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4018 | unsigned int tensorIndex = tensorIndexes[index]; |
| 4019 | armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4020 | RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot); |
| 4021 | } |
| 4022 | } |
| 4023 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4024 | void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex, |
| 4025 | size_t operatorIndex, |
| 4026 | IConnectableLayer* layer, |
| 4027 | const std::vector<unsigned int>& tensorIndexes) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4028 | { |
| 4029 | CHECK_MODEL(m_Model, subgraphIndex, operatorIndex); |
Narumol Prangnawarat | ac2770a | 2020-04-01 16:51:23 +0100 | [diff] [blame] | 4030 | ARMNN_ASSERT(layer != nullptr); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4031 | if (tensorIndexes.size() != layer->GetNumOutputSlots()) |
| 4032 | { |
| 4033 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 4034 | fmt::format("The number of tensor outputs ({}) does not match the number expected ({})" |
| 4035 | " for subgraph:{} operator index:{} {}", |
| 4036 | tensorIndexes.size(), |
| 4037 | layer->GetNumOutputSlots(), |
| 4038 | subgraphIndex, |
| 4039 | operatorIndex, |
| 4040 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex) |
| 4044 | { |
| 4045 | unsigned int tensorIndex = tensorIndexes[slotIndex]; |
| 4046 | armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex)); |
| 4047 | RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot); |
| 4048 | } |
| 4049 | } |
| 4050 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4051 | void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4052 | { |
| 4053 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 4054 | |
| 4055 | auto inputs = GetSubgraphInputs(m_Model, subgraphIndex); |
| 4056 | for (auto const & tensorIdAndPtr : inputs) |
| 4057 | { |
| 4058 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 4059 | IConnectableLayer* layer = |
| 4060 | m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 4061 | |
| 4062 | auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second); |
| 4063 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 4064 | |
| 4065 | RegisterOutputSlots(subgraphIndex, |
| 4066 | VIRTUAL_OPERATOR_ID, |
| 4067 | layer, |
| 4068 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 4069 | } |
| 4070 | } |
| 4071 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4072 | void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4073 | { |
| 4074 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 4075 | |
| 4076 | auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex); |
| 4077 | for (auto const & tensorIdAndPtr : outputs) |
| 4078 | { |
| 4079 | auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first); |
| 4080 | IConnectableLayer* layer = |
| 4081 | m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str()); |
| 4082 | |
| 4083 | RegisterInputSlots(subgraphIndex, |
| 4084 | VIRTUAL_OPERATOR_ID, |
| 4085 | layer, |
| 4086 | { static_cast<uint32_t>(tensorIdAndPtr.first) }); |
| 4087 | } |
| 4088 | } |
| 4089 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4090 | void TfLiteParserImpl::SetupConstantLayers(size_t subgraphIndex) |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4091 | { |
| 4092 | CHECK_SUBGRAPH(m_Model, subgraphIndex); |
| 4093 | |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 4094 | const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex]; |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4095 | for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex) |
| 4096 | { |
| 4097 | for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex) |
| 4098 | { |
| 4099 | if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr && |
| 4100 | m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0) |
| 4101 | { |
Derek Lamberti | ff05cc5 | 2019-04-26 13:05:17 +0100 | [diff] [blame] | 4102 | TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get(); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4103 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4104 | if(IsConstTensor(tensorPtr)) |
| 4105 | { |
| 4106 | armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr); |
| 4107 | auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4108 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4109 | std::string layerName = fmt::format("Constant:{}", tensorPtr->name); |
| 4110 | IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str()); |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4111 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4112 | layer->GetOutputSlot(0).SetTensorInfo(tensorInfo); |
| 4113 | RegisterOutputSlots(subgraphIndex, |
| 4114 | VIRTUAL_OPERATOR_ID, |
| 4115 | layer, |
| 4116 | { tensorIndex }); |
| 4117 | } |
| 4118 | else |
| 4119 | { |
| 4120 | throw ParseException( |
| 4121 | fmt::format("Invalid Tensor: Tensor should be constant. {}", |
| 4122 | CHECK_LOCATION().AsString())); |
| 4123 | } |
Bruno Goncalves | 3d7efe9 | 2018-12-27 14:21:43 -0200 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | } |
| 4127 | } |
| 4128 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4129 | // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer); |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4130 | TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4131 | { |
| 4132 | CHECK_BUFFER(model, bufferIndex); |
| 4133 | return model->buffers[bufferIndex].get(); |
| 4134 | } |
| 4135 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4136 | template<typename T> |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4137 | std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage> |
| 4138 | TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr, |
| 4139 | TfLiteParserImpl::TensorRawPtr tensorPtr, |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4140 | armnn::TensorInfo& tensorInfo, |
| 4141 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
| 4142 | { |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4143 | // Make sure isConstant flag is set. |
| 4144 | tensorInfo.SetConstant(); |
| 4145 | |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4146 | auto constData = CreateConstTensorImpl<T>(bufferPtr, |
| 4147 | tensorPtr, |
| 4148 | tensorInfo, |
| 4149 | permutationVector); |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4150 | TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second)); |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4151 | return std::make_pair(constData.first, std::move(storage)); |
| 4152 | } |
| 4153 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4154 | bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr) |
| 4155 | { |
| 4156 | CHECK_TENSOR_PTR(tensorPtr); |
mathad01 | bf7edb6 | 2021-04-20 16:12:45 +0100 | [diff] [blame] | 4157 | bool isConst = true; |
| 4158 | |
| 4159 | auto buffer = GetBuffer(m_Model, tensorPtr->buffer); |
| 4160 | if (buffer->data.size() == 0) |
| 4161 | { |
| 4162 | isConst = false; |
| 4163 | } |
| 4164 | |
| 4165 | return isConst; |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4168 | std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage> |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4169 | TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr, |
| 4170 | armnn::TensorInfo& tensorInfo, |
| 4171 | armnn::Optional<armnn::PermutationVector&> permutationVector) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4172 | { |
| 4173 | CHECK_TENSOR_PTR(tensorPtr); |
| 4174 | auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer); |
| 4175 | CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer); |
| 4176 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4177 | // Make sure isConstant flag is set. |
| 4178 | tensorInfo.SetConstant(); |
| 4179 | |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4180 | switch (tensorInfo.GetDataType()) |
| 4181 | { |
| 4182 | case armnn::DataType::Float32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4183 | return CreateConstTensorAndStoreData<float>(bufferPtr, |
| 4184 | tensorPtr, |
| 4185 | tensorInfo, |
| 4186 | permutationVector); |
Derek Lamberti | f90c56d | 2020-01-10 17:14:08 +0000 | [diff] [blame] | 4187 | case armnn::DataType::QAsymmU8: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4188 | return CreateConstTensorAndStoreData<uint8_t>(bufferPtr, |
| 4189 | tensorPtr, |
| 4190 | tensorInfo, |
| 4191 | permutationVector); |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 4192 | case armnn::DataType::QSymmS8: |
| 4193 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 4194 | tensorPtr, |
| 4195 | tensorInfo, |
| 4196 | permutationVector); |
Keith Davis | 67e6c54 | 2020-02-19 10:08:33 +0000 | [diff] [blame] | 4197 | case armnn::DataType::QAsymmS8: |
| 4198 | return CreateConstTensorAndStoreData<int8_t>(bufferPtr, |
| 4199 | tensorPtr, |
| 4200 | tensorInfo, |
| 4201 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4202 | case armnn::DataType::Signed32: |
Matteo Martincigh | 747ef82 | 2018-12-18 09:26:39 +0000 | [diff] [blame] | 4203 | return CreateConstTensorAndStoreData<int32_t>(bufferPtr, |
| 4204 | tensorPtr, |
| 4205 | tensorInfo, |
| 4206 | permutationVector); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4207 | default: |
| 4208 | { |
| 4209 | std::stringstream errString; |
| 4210 | errString << "Unexpected datatype when creating const tensor: " |
| 4211 | << armnn::GetDataTypeName(tensorInfo.GetDataType()) |
| 4212 | << " shape:" << tensorInfo.GetShape() |
| 4213 | << CHECK_LOCATION().AsString(); |
| 4214 | throw ParseException(errString.str()); |
| 4215 | } |
| 4216 | } |
| 4217 | } |
| 4218 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4219 | armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr, |
| 4220 | armnn::TensorInfo& tensorInfo) |
| 4221 | { |
| 4222 | CHECK_TENSOR_PTR(tensorPtr); |
| 4223 | auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer); |
| 4224 | CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer); |
| 4225 | |
Matthew Sloyan | 81beae3 | 2021-07-13 19:46:11 +0100 | [diff] [blame] | 4226 | // Make sure isConstant flag is set. |
| 4227 | tensorInfo.SetConstant(); |
| 4228 | |
Finn Williams | d4fa545 | 2021-03-01 12:31:41 +0000 | [diff] [blame] | 4229 | return ConstTensor(tensorInfo, bufferPtr->data.data()); |
| 4230 | } |
| 4231 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4232 | BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId, |
| 4233 | const std::string& name) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4234 | { |
| 4235 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 4236 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 4237 | for (auto const & input : inputs) |
| 4238 | { |
| 4239 | if (input.second->name == name) |
| 4240 | { |
| 4241 | auto bindingId = GenerateLayerBindingId(subgraphId, input.first); |
Colm Donelan | 4bc993b | 2021-11-09 20:39:10 +0000 | [diff] [blame] | 4242 | auto inputTensorInfo = ToTensorInfo(input.second); |
| 4243 | // Input tensors are always treated as constant tensors during network execution. |
| 4244 | inputTensorInfo.SetConstant(true); |
| 4245 | return std::make_pair(bindingId, inputTensorInfo); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4246 | } |
| 4247 | } |
| 4248 | |
| 4249 | std::stringstream bindings; |
| 4250 | for (auto const & input : inputs) |
| 4251 | { |
| 4252 | bindings << "'" << input.second->name << "' "; |
| 4253 | } |
| 4254 | |
| 4255 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 4256 | fmt::format("No input binding found for subgraph:{} and name:{}. " |
| 4257 | "Possible inputs are: [{}] {}", |
| 4258 | subgraphId, |
| 4259 | name, |
| 4260 | bindings.str(), |
| 4261 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4262 | } |
| 4263 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4264 | BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId, |
| 4265 | const std::string& name) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4266 | { |
| 4267 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 4268 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 4269 | for (unsigned int i = 0; i < outputs.size(); ++i) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4270 | { |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 4271 | auto const output = outputs[i]; |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4272 | if (output.second->name == name) |
| 4273 | { |
| 4274 | auto bindingId = GenerateLayerBindingId(subgraphId, output.first); |
Narumol Prangnawarat | 4628d05 | 2019-02-25 17:26:05 +0000 | [diff] [blame] | 4275 | std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ? |
| 4276 | m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape); |
| 4277 | return std::make_pair(bindingId, ToTensorInfo(output.second, shape)); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | std::stringstream bindings; |
| 4282 | for (auto const & output : outputs) |
| 4283 | { |
| 4284 | bindings << "'" << output.second->name << "' "; |
| 4285 | } |
| 4286 | |
| 4287 | throw ParseException( |
James Ward | 58dec6b | 2020-09-11 17:32:44 +0100 | [diff] [blame] | 4288 | fmt::format("No output binding found for subgraph:{} and name:{}. " |
| 4289 | "Possible outputs are: [{}] {}", |
| 4290 | subgraphId, |
| 4291 | name, |
| 4292 | bindings.str(), |
| 4293 | CHECK_LOCATION().AsString())); |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4294 | } |
| 4295 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4296 | size_t TfLiteParserImpl::GetSubgraphCount() const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4297 | { |
| 4298 | return m_Model->subgraphs.size(); |
| 4299 | } |
| 4300 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4301 | std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4302 | { |
| 4303 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 4304 | auto inputs = GetSubgraphInputs(m_Model, subgraphId); |
| 4305 | std::vector<std::string> result; |
| 4306 | result.reserve(inputs.size()); |
| 4307 | for (auto const & input : inputs) |
| 4308 | { |
| 4309 | result.push_back(input.second->name); |
| 4310 | } |
| 4311 | return result; |
| 4312 | } |
| 4313 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4314 | std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4315 | { |
| 4316 | CHECK_SUBGRAPH(m_Model, subgraphId); |
| 4317 | auto outputs = GetSubgraphOutputs(m_Model, subgraphId); |
| 4318 | std::vector<std::string> result; |
| 4319 | result.reserve(outputs.size()); |
| 4320 | for (auto const & output : outputs) |
| 4321 | { |
| 4322 | result.push_back(output.second->name); |
| 4323 | } |
| 4324 | return result; |
| 4325 | } |
| 4326 | |
Matthew Sloyan | ac001ee | 2021-02-03 10:43:04 +0000 | [diff] [blame] | 4327 | const std::string TfLiteParserImpl::GetVersion() |
| 4328 | { |
| 4329 | return TFLITE_PARSER_VERSION; |
| 4330 | } |
| 4331 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4332 | TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4333 | : m_FloatData(std::move(data)) |
| 4334 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 4335 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4336 | , m_Int32Data(nullptr) |
| 4337 | { |
| 4338 | } |
| 4339 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4340 | TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4341 | : m_FloatData(nullptr) |
| 4342 | , m_Uint8Data(std::move(data)) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 4343 | , m_Int8Data(nullptr) |
| 4344 | , m_Int32Data(nullptr) |
| 4345 | { |
| 4346 | } |
| 4347 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4348 | TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 4349 | : m_FloatData(nullptr) |
| 4350 | , m_Uint8Data(nullptr) |
| 4351 | , m_Int8Data(std::move(data)) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4352 | , m_Int32Data(nullptr) |
| 4353 | { |
| 4354 | } |
| 4355 | |
Kevin May | 7d96b16 | 2021-02-03 17:38:41 +0000 | [diff] [blame] | 4356 | TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4357 | : m_FloatData(nullptr) |
| 4358 | , m_Uint8Data(nullptr) |
Keith Davis | d305e1a | 2020-01-22 11:57:54 +0000 | [diff] [blame] | 4359 | , m_Int8Data(nullptr) |
telsoa01 | c577f2c | 2018-08-31 09:22:23 +0100 | [diff] [blame] | 4360 | , m_Int32Data(std::move(data)) |
| 4361 | { |
| 4362 | } |
| 4363 | |
| 4364 | } // armnnTfLiteParser |