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