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