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