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