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