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