blob: f44a747815f54dc24dadb44782ce65d18e3afbd5 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Mike Kelly04d82292023-01-19 18:29:40 +00002// Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
Matteo Martincighe011d202019-11-28 11:35:47 +00005
telsoa01c577f2c2018-08-31 09:22:23 +01006#include "TfLiteParser.hpp"
7
Matthew Sloyanac001ee2021-02-03 10:43:04 +00008#include "armnnTfLiteParser/Version.hpp"
Mike Kelly5880b912022-01-28 16:18:54 +00009#include "armnn/LstmParams.hpp"
Matthew Sloyanac001ee2021-02-03 10:43:04 +000010
Sadik Armagand109a4d2020-07-28 10:42:13 +010011#include <armnn/BackendOptions.hpp>
Matthew Bentham39ef3e52020-01-20 10:09:09 +000012#include <armnn/Descriptors.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010013#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000014#include <armnn/Logging.hpp>
James Conroy05102392020-06-24 15:39:55 +010015#include <armnn/Tensor.hpp>
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +000016#include <armnnUtils/TensorUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010017#include <armnn/TypesUtils.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010018#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000019#include <armnn/utility/IgnoreUnused.hpp>
Derek Lambertif0176992020-04-28 13:37:49 +010020#include <armnn/utility/NumericCast.hpp>
Mike Kelly377fb212023-01-10 15:55:28 +000021#include <armnn/LayerSupport.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010022
23// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000024#include <armnnUtils/Permute.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010025#include <armnnUtils/Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000026
Sadik Armagan479045b2018-10-01 11:51:37 +010027#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010028#include <VerificationHelpers.hpp>
29
30// The generated code based on the Tf Lite schema:
31#include <schema_generated.h>
32
Matteo Martincighe011d202019-11-28 11:35:47 +000033#include <flatbuffers/flexbuffers.h>
34
James Ward58dec6b2020-09-11 17:32:44 +010035#include <fmt/format.h>
telsoa01c577f2c2018-08-31 09:22:23 +010036
telsoa01c577f2c2018-08-31 09:22:23 +010037#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000038#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010039#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010040#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000041
42#define ARMNN_THROW_PARSE_EXCEPTION(msg) \
43 { \
44 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
45 << ": " \
46 << CHECK_LOCATION().AsString()).str()); \
47 }
telsoa01c577f2c2018-08-31 09:22:23 +010048
49using namespace armnn;
50using armnn::CheckLocation;
51namespace armnnTfLiteParser
52{
Kevin May7d96b162021-02-03 17:38:41 +000053
54ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
55 pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
56
57ITfLiteParser::~ITfLiteParser() = default;
58
59ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
60{
61 return new ITfLiteParser(options);
62}
63
64ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
65{
66 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
67}
68
69void ITfLiteParser::Destroy(ITfLiteParser* parser)
70{
71 delete parser;
72}
73
74armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
75{
76 return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
77}
78
Mike Kelly0d77ae12022-01-07 17:42:27 +000079armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
Kevin May7d96b162021-02-03 17:38:41 +000080{
81 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
82}
83
84BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
85 const std::string& name) const
86{
87 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
88}
89
90BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
91 const std::string& name) const
92{
93 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
94}
95
96size_t ITfLiteParser::GetSubgraphCount() const
97{
98 return pTfLiteParserImpl->GetSubgraphCount();
99}
100
101std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
102{
103 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
104}
105
106std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
107{
108 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
109}
110
telsoa01c577f2c2018-08-31 09:22:23 +0100111namespace
112{
jimfly01c25411c2018-11-14 17:47:22 +0000113
telsoa01c577f2c2018-08-31 09:22:23 +0100114const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
115
Mike Kelly0d77ae12022-01-07 17:42:27 +0000116void CheckSubgraph(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100117 size_t subgraphIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000118 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100119{
120 if (model.get() == nullptr)
121 {
122 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100123 fmt::format("{} was called with invalid (null) model. "
124 "Possible reason is that the model is not yet loaded and Unpack(ed). "
125 "subgraph:{} at {}",
126 location.m_Function,
127 subgraphIndex,
128 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100129 }
130 else if (subgraphIndex >= model->subgraphs.size())
131 {
132 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100133 fmt::format("{} was called with an invalid subgraph index. "
134 "subgraph:{} at {}",
135 location.m_Function,
136 subgraphIndex,
137 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100138 }
139}
140
141#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
142 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
143
Mike Kelly0d77ae12022-01-07 17:42:27 +0000144void CheckModel(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100145 size_t subgraphIndex,
146 size_t operatorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000147 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100148{
149 if (model.get() == nullptr)
150 {
151 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100152 fmt::format("{} was called with invalid (null) model. "
153 "Possible reason is that the model is not yet loaded and Unpack(ed). "
154 "subgraph:{} operator:{} at {}",
155 location.m_Function,
156 subgraphIndex,
157 operatorIndex,
158 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100159 }
160 else if (subgraphIndex >= model->subgraphs.size())
161 {
162 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100163 fmt::format("{} was called with an invalid subgraph index. "
164 "subgraph:{} operator:{} at {}",
165 location.m_Function,
166 subgraphIndex,
167 operatorIndex,
168 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100169 }
170 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
171 operatorIndex != VIRTUAL_OPERATOR_ID)
172 {
173 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100174 fmt::format("{} was called with an invalid operator index. "
175 "subgraph:{} operator:{} at {}",
176 location.m_Function,
177 subgraphIndex,
178 operatorIndex,
179 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100180 }
181}
182
183#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
184 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
185
Mike Kelly0d77ae12022-01-07 17:42:27 +0000186void CheckTensor(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100187 size_t subgraphIndex,
188 size_t tensorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000189 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100190{
191 // not checking model, because I assume CHECK_MODEL already run
192 // and checked that. An assert would do.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100193 ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
telsoa01c577f2c2018-08-31 09:22:23 +0100194
195 // also subgraph index should be checked by CHECK_MODEL so
196 // I only add an assert here
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100197 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
telsoa01c577f2c2018-08-31 09:22:23 +0100198
199 // the tensor index is the only one to check here
200 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
201 {
202 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100203 fmt::format("{} was called with an invalid tensor index. "
204 "subgraph:{} tensor:{} at {}",
205 location.m_Function,
206 subgraphIndex,
207 tensorIndex,
208 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100209 }
210}
211
212#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
213 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
214
Kevin May7d96b162021-02-03 17:38:41 +0000215void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000216 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100217{
218 if (rawPtr == nullptr)
219 {
220 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100221 fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100222 }
223}
224
225#define CHECK_TENSOR_PTR(TENSOR_PTR) \
226 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
227
Mike Kelly0d77ae12022-01-07 17:42:27 +0000228void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100229 size_t bufferIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000230 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100231{
232 if (model.get() == nullptr)
233 {
234 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100235 fmt::format("{} was called with invalid (null) model. "
236 "Possible reason is that the model is not yet loaded and Unpack(ed). "
237 "buffer:{} at {}",
238 location.m_Function,
239 bufferIndex,
240 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100241 }
242 else if (bufferIndex >= model->buffers.size())
243 {
244 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100245 fmt::format("{} was called with an invalid buffer index. "
246 "buffer index:{} at {}",
247 location.m_Function,
248 bufferIndex,
249 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100250 }
251 else if (model->buffers[bufferIndex].get() == nullptr)
252 {
253 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100254 fmt::format("The buffer #{} is null. {}",
255 bufferIndex,
256 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100257 }
258}
259
260#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
261 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
262
Kevin May7d96b162021-02-03 17:38:41 +0000263void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000264 const armnn::TensorInfo& tensorInfo,
telsoa01c577f2c2018-08-31 09:22:23 +0100265 uint32_t bufferId,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000266 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100267{
268 if (bufferPtr == nullptr)
269 {
270 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100271 fmt::format("BufferPtr is null for buffer:{}. {}",
272 bufferId,
273 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100274 }
275 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
276 tensorInfo.GetNumBytes() > bufferPtr->data.size())
277 {
278 std::stringstream ss;
279 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
280 << "For tensor: " << tensorInfo.GetShape()
281 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
282 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
283 throw ParseException(ss.str());
284 }
285}
286
Mike Kelly0d77ae12022-01-07 17:42:27 +0000287
288tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
289{
290 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
291 auto opcodeIndex = operatorPtr->opcode_index;
292
293// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
294#if defined(ARMNN_POST_TFLITE_2_3)
295 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
296 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
297#else
298 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
299#endif
300 return opcode;
301}
302
303std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
304 const TfLiteParserImpl::ModelPtr& model,
305 size_t bufferIndex)
306{
307 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
308 std::vector<unsigned int> buffer(info.GetNumElements());
309
310 if (info.GetDataType() == DataType::Signed32)
311 {
312 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
313 }
314 else if (info.GetDataType() == DataType::Signed64)
315 {
316 std::vector<uint64_t> uint64Buffer(info.GetNumElements());
317 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
318 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
319 }
Mike Kelly0506ef02023-01-03 16:29:44 +0000320 else
321 {
322 CheckLocation location = CHECK_LOCATION();
323 throw ParseException(
324 fmt::format("Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
325 GetDataTypeName(info.GetDataType()),
326 location.AsString()));
327 }
Mike Kelly0d77ae12022-01-07 17:42:27 +0000328 return buffer;
329}
330
telsoa01c577f2c2018-08-31 09:22:23 +0100331#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
332 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
333
334bool IsActivationSupported(tflite::ActivationFunctionType activationType)
335{
336 switch(activationType)
337 {
338 case tflite::ActivationFunctionType_NONE:
339 case tflite::ActivationFunctionType_RELU:
340 case tflite::ActivationFunctionType_RELU6:
341 case tflite::ActivationFunctionType_TANH:
342 {
343 return true;
344 }
345 default:
346 {
347 return false;
348 }
349 }
350}
351
352#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
353 do { \
354 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
355 { \
356 throw ParseException( \
Mike Kelly377fb212023-01-10 15:55:28 +0000357 fmt::format("TfLite parser doesn't support fused activation: " \
James Ward58dec6b2020-09-11 17:32:44 +0100358 "{}/{} in {} subgraph:{} operator:{} at {}", \
359 OPTION->fused_activation_function, \
360 tflite::EnumNameActivationFunctionType(\
361 OPTION->fused_activation_function), \
362 __func__, \
363 SUBGRAPH_INDEX, \
364 OPERATOR_INDEX, \
365 CHECK_LOCATION().FileLine())); \
telsoa01c577f2c2018-08-31 09:22:23 +0100366 } \
367 } while(false)
368
369
Mike Kelly0d77ae12022-01-07 17:42:27 +0000370std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t>& in)
telsoa01c577f2c2018-08-31 09:22:23 +0100371{
372 std::vector<unsigned int> result;
373 result.reserve(in.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +0000374 for (auto& i : in)
telsoa01c577f2c2018-08-31 09:22:23 +0100375 {
mathad01c21025d2021-04-26 10:09:37 +0100376 // If the location of the input data is -1 then the input should be ignored.
377 if (i == -1)
378 {
379 continue;
380 }
telsoa01c577f2c2018-08-31 09:22:23 +0100381 result.push_back(CHECKED_NON_NEGATIVE(i));
382 }
383 return result;
384}
385
Mike Kelly5880b912022-01-28 16:18:54 +0000386bool IsOptionalOperandPresent(int input)
387{
388 return (input >= 0);
389}
390
telsoa01c577f2c2018-08-31 09:22:23 +0100391void CalcPadding(uint32_t inputSize,
392 uint32_t filterSize,
393 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100394 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100395 uint32_t& paddingFront,
396 uint32_t& paddingBack,
397 tflite::Padding padding)
398{
399 paddingFront = 0;
400 paddingBack = 0;
401 if (padding == tflite::Padding_SAME)
402 {
403 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100404 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
405 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100406 if (temp > inputSize)
407 {
408 paddingFront = (temp - inputSize) / 2;
409 paddingBack = (temp - inputSize) - paddingFront;
410 }
411 }
412}
413
Teresa Charlin024ef0b2023-04-26 11:19:03 +0100414// Function that calculates explicit padding when the output shape is known.
415// At the moment the output is only given as an input parameter in Transpose Convolution,
416// not in Convolution and Depthwise Convolution
417void CalcPadding(uint32_t inputSize,
418 uint32_t filterSize,
419 uint32_t stride,
420 uint32_t dilation,
421 uint32_t& paddingFront,
422 uint32_t& paddingBack,
423 tflite::Padding padding,
424 uint32_t outputSize)
425{
426 IgnoreUnused(dilation);
427 paddingFront = 0;
428 paddingBack = 0;
429 if (padding == tflite::Padding_SAME)
430 {
431 uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize;
432 paddingFront = totalPadding / 2;
433 paddingBack = totalPadding - paddingFront;
434 }
435}
436
Kevin May7d96b162021-02-03 17:38:41 +0000437armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Finn Williamsb49ed182021-06-29 15:50:08 +0100438 const std::vector<unsigned int>& shape,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100439 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100440{
441 armnn::DataType type;
442 CHECK_TENSOR_PTR(tensorPtr);
443
444 switch (tensorPtr->type)
445 {
446 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000447 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100448 break;
449 case tflite::TensorType_FLOAT32:
450 type = armnn::DataType::Float32;
451 break;
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100452 case tflite::TensorType_FLOAT16:
453 type = armnn::DataType::Float16;
454 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000455 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000456 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000457 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000458 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000459 type = armnn::DataType::QAsymmS8;
460 }
461 else
462 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000463 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000464 type = armnn::DataType::QSymmS8;
465 }
Finn Williamsed66d142019-12-06 09:55:55 +0000466 break;
467 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000468 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000469 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100470 case tflite::TensorType_INT32:
471 type = armnn::DataType::Signed32;
472 break;
Inki Daed4619e22020-09-10 15:33:54 +0900473 case tflite::TensorType_INT64:
474 type = armnn::DataType::Signed64;
475 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100476 case tflite::TensorType_BOOL:
477 type = armnn::DataType::Boolean;
478 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100479 default:
480 {
481 CheckLocation location = CHECK_LOCATION();
482 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100483 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
484 tensorPtr->type,
485 tflite::EnumNameTensorType(tensorPtr->type),
486 tensorPtr->name,
487 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100488 }
489 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100490 TensorShape tensorShape;
491
492 std::vector<unsigned int> safeShape = shape;
493 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100494 {
495 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100496 }
497
498 if (!outputTensor)
499 {
500 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
501 }
502 else
503 {
Rob Hughesd812a312021-08-06 13:10:53 +0100504 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100505
506 // If a shape signature exists we will use that to infer dynamic tensors
507 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100508 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100509 // If the shape is incompatible with the shape signature override the shape
510 if (shapeSignatureSize != shape.size())
511 {
512 safeShape = {};
513
514 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
515 {
516 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
517 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
518 safeShape.push_back(dim);
519 }
520 }
521
Rob Hughesd812a312021-08-06 13:10:53 +0100522 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Mike Kelly04d82292023-01-19 18:29:40 +0000523 bool batchOnly = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100524 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
525 {
Mike Kelly04d82292023-01-19 18:29:40 +0000526 dimMask[i] = tensorPtr->shape_signature[i] != -1;
527
528 if (i > 0 && !dimMask[i])
529 {
530 batchOnly = false;
531 }
532 }
533 if (batchOnly)
534 {
535 dimMask[0] = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100536 }
Rob Hughesd812a312021-08-06 13:10:53 +0100537 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100538 }
539 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
540 else if (shape.size() == 0)
541 {
542 tensorShape = TensorShape(1, false);
543 }
544 else
545 {
546 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100547 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100548 }
549
Teresa Charlinacb3ec52023-04-03 19:57:00 +0100550 float quantizationScale = 1.0f;
Keith Davisd305e1a2020-01-22 11:57:54 +0000551 int32_t quantizationOffset = 0;
552
553 if (tensorPtr->quantization.get())
554 {
555 if (tensorPtr->quantization->scale.size() <= 1)
556 {
557 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
558 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
559
560 if (tensorPtr->quantization->scale.size() == 1)
561 {
562 quantizationScale = tensorPtr->quantization->scale[0];
563 }
564 if (tensorPtr->quantization->zero_point.size() == 1)
565 {
566 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000567 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100568 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000569 }
570
Sadik Armagand109a4d2020-07-28 10:42:13 +0100571 armnn::TensorInfo result(tensorShape,
572 type,
573 quantizationScale,
574 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000575 return result;
576 }
577 else
578 {
579 std::vector<float> quantizationScales;
580 std::vector<int32_t> quantizationOffsets;
581
582 // Scale
583 std::copy(tensorPtr->quantization->scale.begin(),
584 tensorPtr->quantization->scale.end(),
585 std::back_inserter(quantizationScales));
586
Keith Davis0c2eeac2020-02-11 16:51:50 +0000587 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100588 armnn::TensorInfo result(tensorShape,
589 type,
590 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100591 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000592 return result;
593 }
594 }
595 else
596 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100597 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000598 type,
599 quantizationScale,
600 quantizationOffset);
601 return result;
602 }
telsoa01c577f2c2018-08-31 09:22:23 +0100603}
604
Kevin May7d96b162021-02-03 17:38:41 +0000605armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Mike Kelly377fb212023-01-10 15:55:28 +0000606 const bool outputTensor = false)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100607{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000608 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100609 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100610}
611
telsoa01c577f2c2018-08-31 09:22:23 +0100612template<typename T>
613std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000614CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
615 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000616 armnn::TensorInfo& tensorInfo,
617 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100618{
Jan Eilers8eb25602020-03-09 12:13:48 +0000619 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100620 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
621 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100622 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100623
624 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000625
626 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
627 {
628 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000629 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
630 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000631 }
632 else
633 {
634 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
635 }
636
Matthew Sloyan81beae32021-07-13 19:46:11 +0100637 // Make sure isConstant flag is set.
638 tensorInfo.SetConstant();
639
telsoa01c577f2c2018-08-31 09:22:23 +0100640 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
641}
642
telsoa01c577f2c2018-08-31 09:22:23 +0100643armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
644{
645 // generate the binding id by shifting the tensor id by 8 bit
646 // and add the subgraph id, which allows 256 subgraphs
647 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
648}
649
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000650bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
651{
652 const unsigned int actualSize = actual.GetNumDimensions();
653 if (actualSize != expected.size())
654 {
655 return false;
656 }
657
658 for (unsigned int i = 0u; i < actualSize; i++)
659 {
660 if (expected[i] < 0 ||
661 actual[i] != static_cast<unsigned int>(expected[i]))
662 {
663 return false;
664 }
665 }
666
667 return true;
668}
669
Cathal Corbett2b922e22022-09-23 15:49:24 +0100670bool CheckShape(const armnn::TensorShape& actual, const armnn::TensorShape& expected)
671{
672 std::vector<int32_t> expectedVec;
673 for (uint32_t i = 0; i < expected.GetNumDimensions(); i++)
674 {
675 expectedVec.push_back(expected[i]);
676 }
677 return CheckShape(actual, expectedVec);
678}
679
James Conroy05102392020-06-24 15:39:55 +0100680void CheckMatchingQuantization(const TensorInfo& first,
681 const TensorInfo& second,
682 const std::string& descName,
683 std::string const& firstName,
684 std::string const& secondName)
685{
686 if (!first.IsQuantized() ||
687 !second.IsQuantized())
688 {
689 // Not a quantized type, ignore the validation
690 return;
691 }
692
693 DataType firstDataType = first.GetDataType();
694 DataType secondDataType = second.GetDataType();
695
696 if (firstDataType != secondDataType)
697 {
698 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
699 " must be of the same quantized type, " +
700 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
701 secondName + " is " + GetDataTypeName(secondDataType));
702 }
703
704 if (!first.IsTypeSpaceMatch(second))
705 {
706 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
707 " must have the same quantization space, " +
708 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
709 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
710 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
711 " and scale " + std::to_string(second.GetQuantizationScale()));
712 }
713}
714
Mike Kelly377fb212023-01-10 15:55:28 +0000715bool IsDynamic(TfLiteParserImpl::TensorRawPtr tensorPtr)
716{
717 auto shape = tensorPtr->shape;
718
719 if (shape.empty())
720 {
721 return true;
722 }
723 auto shapeSig = tensorPtr->shape_signature;
724
725 if (shapeSig.empty())
726 {
727 return false;
728 }
729
730 for (unsigned int i = 0; i < shapeSig.size() ; ++i)
731 {
732 if (shapeSig[i] == -1)
733 {
734 return true;
735 }
736 }
737 return false;
738}
739
telsoa01c577f2c2018-08-31 09:22:23 +0100740} // <anonymous>
741
Kevin May7d96b162021-02-03 17:38:41 +0000742TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100743: m_Options(options)
744, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000745, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100746{
747 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100748 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000749 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100750 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
751 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000752 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
753 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
Samuel Yapfd3ba5a2022-08-24 17:04:34 +0100754 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
Teresa Charlin93f0ad02023-03-23 15:28:02 +0000755 m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
mathad01b392e982021-04-07 12:07:30 +0100756 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000757 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
758 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100759 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +0100760 #if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100761 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100762 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000763 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
764 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
765 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
766 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100767 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000768 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300769 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000770 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100771 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000772 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000773 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
774 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin91a53ea2022-04-25 15:47:29 +0100775 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300776 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
777 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000778 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
779 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300780 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
781 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Mike Kelly31dce2b2021-09-01 21:22:37 +0100782 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
783 = &TfLiteParserImpl::ParseLocalResponseNormalization;
Teresa Charlin28aa6692022-07-12 11:18:44 +0100784 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100785 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000786 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
Teresa Charlinfd33a692022-06-29 15:35:57 +0100787 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
Kevin May7d96b162021-02-03 17:38:41 +0000788 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
789 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
790 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
791 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
792 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100793 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000794 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
795 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300796 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000797 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
798 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000799 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
John Mcloughlin0ec00872023-05-15 17:03:49 +0100800 m_ParserFunctions[tflite::BuiltinOperator_POW] = &TfLiteParserImpl::ParsePower;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100801 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000802 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
803 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
804 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000805 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
806 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100807 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000808 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
809 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
810 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Tianle Chenge5a30ff2023-07-03 11:24:12 +0100811 m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100812 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100813 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100814 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Teresa Charlin28aa6692022-07-12 11:18:44 +0100815 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
Kevin May7d96b162021-02-03 17:38:41 +0000816 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
817 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
818 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
Teresa Charlin2a764ad2023-02-24 18:17:31 +0000819 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
Kevin May7d96b162021-02-03 17:38:41 +0000820 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
821 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
822 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
John Mcloughlin0ec00872023-05-15 17:03:49 +0100823 m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
Kevin May7d96b162021-02-03 17:38:41 +0000824 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
825 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
826 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
827 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
828 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
829 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000830 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
831 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000832 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100833
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100834 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000835 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100836}
837
Mike Kelly377fb212023-01-10 15:55:28 +0000838armnn::TensorInfo TfLiteParserImpl::InputTensorInfo(size_t subgraphIndex,
839 size_t operatorIndex,
840 int input)
841{
842 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
843 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
844
845 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[input]);
846 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
847
848 if (search != m_TensorInfos.end())
849 {
850 return m_TensorInfos[inputId];
851 }
852 else
853 {
854 auto tensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
855 m_TensorInfos.insert({ inputId, tensorInfo });
856 return tensorInfo;
857 }
858}
859
860armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(size_t subgraphIndex,
861 size_t operatorIndex,
862 armnn::IConnectableLayer* layer,
863 int output,
864 std::vector<int> inputs)
865{
866 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
867 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
868
869 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
870
871 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
872
873 if (outputSearch != m_TensorInfos.end())
874 {
875 return m_TensorInfos[outputId];
876 }
877
878 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
879 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
880
881 if (IsDynamic(outputTensorPtr))
882 {
883 if (inputs.empty())
884 {
885 for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
886 {
887 inputs.emplace_back(i);
888 }
889 }
890 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
891 std::vector<armnn::TensorShape> inputShapes;
892
893 for (unsigned int i = 0; i < inputs.size(); ++i)
894 {
895 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[inputs[i]]);
896 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
897
898 if (search != m_TensorInfos.end())
899 {
900 auto &inputTensorInfo = m_TensorInfos[inputId];
901 inputShapes.push_back(inputTensorInfo.GetShape());
902 }
903 else
904 {
Mike Kelly377fb212023-01-10 15:55:28 +0000905 auto inputTensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
906 m_TensorInfos.insert({ inputId, inputTensorInfo});
907 inputShapes.push_back(inputTensorInfo.GetShape());
908 }
909 }
910 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
911 tensor.SetShape(outputShape);
912 }
913 m_TensorInfos.insert({ outputId, tensor});
914 return tensor;
915}
916
917armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(size_t subgraphIndex,
918 size_t operatorIndex,
919 armnn::IConnectableLayer* layer,
920 int output,
921 std::vector<armnn::TensorShape> inputShapes)
922{
923 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
924 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
925
926 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
927 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
928 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
929
930 if (IsDynamic(outputTensorPtr))
931 {
932 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
933 tensor.SetShape(outputShape);
934 }
935 m_TensorInfos.insert({ outputId, tensor});
936 return tensor;
937}
938
Kevin May7d96b162021-02-03 17:38:41 +0000939void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100940{
941 m_Network = armnn::INetworkPtr(nullptr, nullptr);
942 m_Model = nullptr;
943 m_SubgraphConnections.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000944 m_OverriddenOutputShapes.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000945 m_ConstantsToDequantize.clear();
946 m_ConstantsToBeCreated.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000947 m_TensorInfos.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100948}
949
Kevin May7d96b162021-02-03 17:38:41 +0000950INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100951{
952 ResetParser();
953 m_Model = LoadModelFromFile(graphFile);
954 return CreateNetworkFromModel();
955}
956
Mike Kelly0d77ae12022-01-07 17:42:27 +0000957INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100958{
959 ResetParser();
960 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
961 return CreateNetworkFromModel();
962}
963
Finn Williamsb49ed182021-06-29 15:50:08 +0100964
965armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
966{
967 ResetParser();
968 m_Model = std::move(model);
969
970 return CreateNetworkFromModel();
971}
972
Kevin May7d96b162021-02-03 17:38:41 +0000973INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100974{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100975
976 using NetworkOptions = std::vector<BackendOptions>;
977 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100978 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100979 {
Mike Kelly80512b02022-05-16 23:10:42 +0100980 if (m_Options.value().m_InferAndValidate)
981 {
982 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
983 {
984 { "InferAndValidate", true }
985 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100986
Mike Kelly80512b02022-05-16 23:10:42 +0100987 networkOptions.push_back(shapeInferenceMethodOption);
988 }
989 if (m_Options.value().m_AllowExpandedDims)
990 {
991 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
992 {
993 { "AllowExpandedDims", true }
994 });
995
996 networkOptions.push_back(shapeInferenceMethodOption);
997 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100998 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100999 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001000 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001001
telsoa01c577f2c2018-08-31 09:22:23 +01001002 if (m_Model->subgraphs.size() != 1)
1003 {
1004 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001005 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
1006 m_Model->subgraphs.size(),
1007 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001008 }
1009
1010 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +01001011 size_t operatorIndex = 0;
1012 try
telsoa01c577f2c2018-08-31 09:22:23 +01001013 {
Colm Donelan6350d272020-06-09 16:56:25 +01001014 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +01001015 {
Mike Kelly377fb212023-01-10 15:55:28 +00001016 SetupInputLayerTensorInfos(subgraphIndex);
1017 SetupConstantLayerTensorInfos(subgraphIndex);
1018
Colm Donelan6350d272020-06-09 16:56:25 +01001019 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
1020 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +01001021 {
Colm Donelan6350d272020-06-09 16:56:25 +01001022 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +01001023
1024// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001025#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001026 auto builtinCode = std::max(opCodePtr->builtin_code,
1027 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
1028#else
telsoa01c577f2c2018-08-31 09:22:23 +01001029 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001030#endif
telsoa01c577f2c2018-08-31 09:22:23 +01001031
1032 if (builtinCode > tflite::BuiltinOperator_MAX)
1033 {
James Ward58dec6b2020-09-11 17:32:44 +01001034 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
1035 "subgraph:{} operator idx:{}. {}",
1036 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1037 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001038 }
1039
1040 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +01001041 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +01001042 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +01001043 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +01001044 }
telsoa01c577f2c2018-08-31 09:22:23 +01001045
Colm Donelan6350d272020-06-09 16:56:25 +01001046 SetupInputLayers(subgraphIndex);
1047 SetupOutputLayers(subgraphIndex);
1048 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001049
Colm Donelan6350d272020-06-09 16:56:25 +01001050 ++subgraphIndex;
1051 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +01001052 }
telsoa01c577f2c2018-08-31 09:22:23 +01001053 }
Colm Donelan6350d272020-06-09 16:56:25 +01001054 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +01001055 {
Colm Donelan6350d272020-06-09 16:56:25 +01001056 std::stringstream errorString;
1057 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
1058 << subgraphIndex << " error: " << e.what();
1059 ARMNN_LOG(error) << errorString.str();
1060 std::stringstream errors;
1061 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +01001062 throw ParseException(errors.str());
1063 }
1064
1065 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +01001066 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001067 {
1068 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1069 {
1070 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
1071 {
1072 for (size_t inputSlotIdx = 0;
1073 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1074 ++inputSlotIdx)
1075 {
1076 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1077 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1078 }
1079 }
1080 }
1081 }
telsoa01c577f2c2018-08-31 09:22:23 +01001082 return std::move(m_Network);
1083}
1084
Mike Kelly0506ef02023-01-03 16:29:44 +00001085bool TfLiteParserImpl::ShouldConstantTensorBeConverted(TfLiteParserImpl::TensorRawPtr tensorPtr,
1086 armnn::DataType inputDataType,
1087 armnn::DataType tensorDataType)
Mike Kelly5880b912022-01-28 16:18:54 +00001088{
Mike Kelly0506ef02023-01-03 16:29:44 +00001089 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1090 (tensorDataType == DataType::QAsymmU8 ||
1091 tensorDataType == DataType::QAsymmS8 ||
1092 tensorDataType == DataType::QSymmS8 ||
1093 tensorDataType == DataType::Signed32 ||
1094 tensorDataType == DataType::Signed64));
Mike Kelly5880b912022-01-28 16:18:54 +00001095}
1096
Kevin May7d96b162021-02-03 17:38:41 +00001097void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
1098 size_t tensorIndex,
1099 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001100{
1101 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001102 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1103 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001104
1105 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1106
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001107 if (slot->GetOwningIConnectableLayer().GetType() != LayerType::Constant)
telsoa01c577f2c2018-08-31 09:22:23 +01001108 {
telsoa01c577f2c2018-08-31 09:22:23 +01001109
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001110 // assuming there is only one producer for that tensor
1111 if (tensorSlots.outputSlot != nullptr)
1112 {
1113 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
1114 "subgraph:{} tensor:{} {}",
1115 subgraphIndex,
1116 tensorIndex,
1117 CHECK_LOCATION().AsString()));
1118 }
1119 }
telsoa01c577f2c2018-08-31 09:22:23 +01001120 tensorSlots.outputSlot = slot;
1121}
1122
Kevin May7d96b162021-02-03 17:38:41 +00001123void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
1124 size_t tensorIndex,
1125 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001126{
1127 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001128 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1129 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001130
Finn Williamsd4fa5452021-03-01 12:31:41 +00001131 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01001132 tensorSlots.inputSlots.push_back(slot);
1133}
1134
Kevin May7d96b162021-02-03 17:38:41 +00001135void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001136{
1137 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1138
1139 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +00001140 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001141
1142 // Identify custom code defined for custom operator
1143 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1144 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1145
Mike Kelly377fb212023-01-10 15:55:28 +00001146 // Find parser function that corresponds to custom code (if any)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001147 auto iterator = m_CustomParserFunctions.find(customCode);
1148 if (iterator != m_CustomParserFunctions.end())
1149 {
1150 customParserFunction = iterator->second;
1151 }
1152
1153 // Run parser function
1154 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1155}
1156
Kevin May7d96b162021-02-03 17:38:41 +00001157void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001158{
1159 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001160
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001161 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1162
1163 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001164
1165// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001166#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001167 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1168 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1169#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001170 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001171#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001172
1173 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1174 {
1175 // Do not add StandInLayer, throw ParseException instead
1176 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001177 fmt::format("Operator not supported. "
1178 "subgraph:{} operator:{} "
1179 "opcode_index:{} opcode:{} / {} {}",
1180 subgraphIndex,
1181 operatorIndex,
1182 opcodeIndex,
1183 opcode,
1184 tflite::EnumNameBuiltinOperator(opcode),
1185 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001186 }
1187
1188 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1189 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1190
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001191 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1192 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001193
1194 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001195 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001196
1197 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1198 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001199 ARMNN_ASSERT(layer != nullptr);
1200
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001201 for (unsigned int i = 0u; i < numOutputs; ++i)
1202 {
Mike Kelly04d82292023-01-19 18:29:40 +00001203 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[0], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001204 }
1205
1206 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1207 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1208
1209 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1210 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001211}
1212
mathad01b392e982021-04-07 12:07:30 +01001213void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1214{
1215 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1216
1217 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1218 CHECK_VALID_SIZE(inputs.size(), 1);
1219 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1220 CHECK_VALID_SIZE(outputs.size(), 1);
1221
1222 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1223
1224 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1225 ARMNN_ASSERT(layer != nullptr);
1226
Mike Kelly377fb212023-01-10 15:55:28 +00001227 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
mathad01b392e982021-04-07 12:07:30 +01001228 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1229
1230 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1231 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1232
1233 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1234 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1235}
1236
Kevin May7d96b162021-02-03 17:38:41 +00001237void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001238{
1239 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1240
Mike Kelly0d77ae12022-01-07 17:42:27 +00001241 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1242 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001243
1244 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1245
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001246 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1247 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1248 CHECK_VALID_SIZE(outputs.size(), 1);
1249
telsoa01c577f2c2018-08-31 09:22:23 +01001250 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001251 inputs.size() == 3 ?
1252 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001253 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1254 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001255 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001256 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1257 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001258
Mike Kelly377fb212023-01-10 15:55:28 +00001259 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1260 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001261
1262 // assuming input is NHWC
1263 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001264 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001265
1266 // assuming the filter is OHWI : Output, H, W, Input
1267 // which is essentially the same as NHWC
1268 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001269 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001270
Pablo Tellof0bd6832019-04-26 17:58:13 +01001271 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1272 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1273 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1274 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001275
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001276 // Add the first input and weights tensor to the registration list.
1277 // The constant weights will be added by SetupConstantLayers.
1278 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1279 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001280
James Ward58dec6b2020-09-11 17:32:44 +01001281 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001282 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001283
Mike Kelly0506ef02023-01-03 16:29:44 +00001284 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
telsoa01c577f2c2018-08-31 09:22:23 +01001285 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001286 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001287 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001288
1289 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001290 {
Mike Kelly377fb212023-01-10 15:55:28 +00001291 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001292
1293 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1294 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1295
Mike Kelly0506ef02023-01-03 16:29:44 +00001296 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001297 {
1298 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1299 }
telsoa01c577f2c2018-08-31 09:22:23 +01001300 }
1301
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001302 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001303
Mike Kelly377fb212023-01-10 15:55:28 +00001304 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001305 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001306
1307 // register the input connection slots for the layer, connections are made after all layers have been created
1308 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001309 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001310
jimfly01c25411c2018-11-14 17:47:22 +00001311 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001312 // register the output connection slots for the layer, connections are made after all layers have been created
1313 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001314 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001315}
1316
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001317// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +01001318#if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001319void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1320{
1321 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1322
1323 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1324 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1325
1326 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1327
1328 Convolution3dDescriptor desc;
1329 desc.m_BiasEnabled = false;
1330 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1331 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1332 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1333 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1334 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1335 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1336 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1337
1338 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1339 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1340
1341 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1342 CHECK_VALID_SIZE(outputs.size(), 1);
1343
Mike Kelly377fb212023-01-10 15:55:28 +00001344 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1345 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001346
1347 // Assuming input is NDHWC
1348 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1349 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1350 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1351
1352 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1353 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1354 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1355 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1356
1357 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001358 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001359 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1360 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1361 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1362 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1363
Mike Kelly5880b912022-01-28 16:18:54 +00001364 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001365
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001366 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1367
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001368 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1369 // Add the first input and weights tensor to the registration list.
1370 // The constant weights will be added by SetupConstantLayers.
1371 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1372
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001373 if (inputs.size() == 3)
1374 {
1375 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001376
1377 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1378 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001379 }
1380
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001381 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001382 ARMNN_ASSERT(layer != nullptr);
1383
Mike Kelly377fb212023-01-10 15:55:28 +00001384 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001385 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1386
1387 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001388 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001389
1390 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1391 // Register the output connection slots for the layer, connections are made after all layers have been created
1392 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1393 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1394}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001395#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001396
Kevin May7d96b162021-02-03 17:38:41 +00001397void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001398{
1399 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1400
Mike Kelly0d77ae12022-01-07 17:42:27 +00001401 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1402 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001403
1404 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1405
1406 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001407 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1408 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001409 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001410 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001411
1412 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1413 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001414 if (inputs.size() == 3)
1415 {
1416 desc.m_BiasEnabled = true;
1417 }
1418
telsoa01c577f2c2018-08-31 09:22:23 +01001419 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1420 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001421 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1422 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001423
Mike Kelly377fb212023-01-10 15:55:28 +00001424 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1425 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001426
Matteo Martincigh747ef822018-12-18 09:26:39 +00001427 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001428 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1429 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001430
1431 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001432 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1433 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1434
Pablo Tellof0bd6832019-04-26 17:58:13 +01001435 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1436 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1437 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1438 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001439
Jan Eilers53ef7952021-06-02 12:01:25 +01001440 // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
James Ward58dec6b2020-09-11 17:32:44 +01001441 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001442
Cathal Corbett06902652022-04-14 17:55:11 +01001443 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1444 // Add the first input and weights tensor to the registration list.
1445 // The constant weights will be added by SetupConstantLayers.
1446 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1447
1448 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1449
1450 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001451 {
1452 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00001453 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Cathal Corbett06902652022-04-14 17:55:11 +01001454
1455 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1456 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001457 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001458 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001459
Mike Kelly377fb212023-01-10 15:55:28 +00001460 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001461 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001462
1463 // register the input connection slots for the layer, connections are made after all layers have been created
1464 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001466
jimfly01c25411c2018-11-14 17:47:22 +00001467 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001468 // register the output connection slots for the layer, connections are made after all layers have been created
1469 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1470 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1471}
1472
Kevin May7d96b162021-02-03 17:38:41 +00001473void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001474{
1475 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1476
1477 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1478 CHECK_VALID_SIZE(inputs.size(), 1);
1479
1480 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1481 CHECK_VALID_SIZE(outputs.size(), 1);
1482
James Ward58dec6b2020-09-11 17:32:44 +01001483 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001484
1485 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001486 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001487
Mike Kelly377fb212023-01-10 15:55:28 +00001488 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Finn Williamsed66d142019-12-06 09:55:55 +00001489 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1490
1491 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1492 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1493
1494 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1495 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1496}
1497
Teresa Charlin3ab85482021-06-08 16:59:29 +01001498void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1499{
1500 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1501
1502 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1503 CHECK_VALID_SIZE(inputs.size(), 2);
1504
1505 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1506 CHECK_VALID_SIZE(outputs.size(), 1);
1507
1508 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1509
Mike Kelly377fb212023-01-10 15:55:28 +00001510 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001511 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001512 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1513
Teresa Charlina7a605a2023-06-14 14:51:17 +01001514 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1515
1516 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1517 if (axisBufferPtr == nullptr)
1518 {
1519 throw ParseException(fmt::format("{}: Operation has invalid inputs. Failed to read axis.",
1520 CHECK_LOCATION().AsString()));
1521 }
1522
1523 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
1524 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
1525 int32_t axis = axisData[0];
1526
1527 auto inputRank = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1528 auto outputRank = inputRank + 1;
1529 if((axis < -1 * outputRank) || (outputRank <= axis))
1530 {
1531 throw ParseException(fmt::format("{}: Axis {} is not within [-{}, {}) range.",
1532 CHECK_LOCATION().AsString(), axis, outputRank, outputRank));
1533 }
1534
1535 axis = axis < 0 ? (axis + outputRank) : axis;
1536
1537 std::vector<unsigned int> shape(static_cast<unsigned int>(outputRank));
1538 unsigned int inputShapeIndex = 0;
1539 for (unsigned int i = 0; i < static_cast<unsigned int>(outputRank); ++i)
1540 {
1541 if (i == static_cast<unsigned int>(axis))
1542 {
1543 shape[i] = 1;
1544 }
1545 else
1546 {
1547 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1548 ++inputShapeIndex;
1549 }
1550 }
1551
Teresa Charlin3ab85482021-06-08 16:59:29 +01001552 ReshapeDescriptor reshapeDesc;
Teresa Charlina7a605a2023-06-14 14:51:17 +01001553 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(outputRank), shape.data());
1554 outputTensorInfo.SetShape(reshapeDesc.m_TargetShape);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001555
1556 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1557 ARMNN_ASSERT(layer != nullptr);
1558 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1559
Teresa Charlina7a605a2023-06-14 14:51:17 +01001560 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
1561 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
1562
Teresa Charlin3ab85482021-06-08 16:59:29 +01001563 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1564 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1565
1566 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1567 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1568}
1569
Kevin May7d96b162021-02-03 17:38:41 +00001570void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001571{
1572 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1573
1574 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001575 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001576
1577 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1578 CHECK_VALID_SIZE(outputs.size(), 1);
1579
James Ward58dec6b2020-09-11 17:32:44 +01001580 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001581 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001582
josh minorba424d22019-11-13 10:55:17 -06001583 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001584 {
Mike Kelly377fb212023-01-10 15:55:28 +00001585 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Kevin May85d92602019-09-27 17:21:06 +01001586 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001587 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1588 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001589 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001590 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001591
Mike Kelly08759e22020-03-02 11:41:31 +00001592 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001593 }
Mike Kelly377fb212023-01-10 15:55:28 +00001594 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Keith Davis4cd29a02019-09-09 14:49:20 +01001595
James Conroy05102392020-06-24 15:39:55 +01001596 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001597 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001598
1599 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1600 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001601 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1602
1603 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1604 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1605
1606 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1607 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1608}
1609
Kevin May7d96b162021-02-03 17:38:41 +00001610void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001611{
1612 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1613
Mike Kelly0d77ae12022-01-07 17:42:27 +00001614 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1615 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001616
1617 TransposeConvolution2dDescriptor desc;
1618 desc.m_BiasEnabled = false;
1619 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1620 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1621 desc.m_DataLayout = armnn::DataLayout::NHWC;
1622
1623 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001624 if (inputs.size() == 4)
1625 {
1626 desc.m_BiasEnabled = true;
1627 }
1628 else
1629 {
1630 CHECK_VALID_SIZE(inputs.size(), 3);
1631 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001632
1633 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1634 CHECK_VALID_SIZE(outputs.size(), 1);
1635
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001636
1637 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1638 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1639
1640 // TfLite uses NHWC tensors
1641 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1642 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1643
1644 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1645 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1646
Ryan OSheaf0a35b82023-02-21 18:32:30 +00001647 // This block determines the output shape of the transpose convolution. If the output shape tensor ptr is not null
1648 // And the tensor is a constant, we can access the data at load time and set the output shape of the
1649 // layer. If this is not constant, We do not have access to the shape data, so we have to use
1650 // infer output shape and skip this code block.
1651 if (inputs[0] && IsConstTensor(inputs[0]))
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001652 {
Mike Kelly377fb212023-01-10 15:55:28 +00001653 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001654 std::vector<int> output_shape(tensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00001655
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001656 if (tensorInfo.GetDataType() == DataType::Signed32)
1657 {
1658 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1659 }
1660 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1661 {
1662 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1663 {
1664 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1665 }
1666 }
1667 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1668 for (int dimension : output_shape)
1669 {
1670 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1671 }
1672 desc.m_OutputShapeEnabled = true;
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001673
1674 // TfLite uses NHWC tensors
1675 const unsigned int outputHeight = desc.m_OutputShape[1];
1676 const unsigned int outputWidth = desc.m_OutputShape[2];
1677
1678 CalcPadding(inputHeight,
1679 filterHeight,
1680 desc.m_StrideY,
1681 1, // DilationY
1682 desc.m_PadTop,
1683 desc.m_PadBottom,
1684 options->padding,
1685 outputHeight);
1686
1687 CalcPadding(inputWidth,
1688 filterWidth,
1689 desc.m_StrideX,
1690 1, // DilationX
1691 desc.m_PadLeft,
1692 desc.m_PadRight,
1693 options->padding,
1694 outputWidth);
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001695 }
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001696 else
1697 {
1698 CalcPadding(inputHeight,
1699 filterHeight,
1700 desc.m_StrideY,
1701 1, // DilationY
1702 desc.m_PadTop,
1703 desc.m_PadBottom,
1704 options->padding);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001705
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001706 CalcPadding(inputWidth,
1707 filterWidth,
1708 desc.m_StrideX,
1709 1, // DilationX
1710 desc.m_PadLeft,
1711 desc.m_PadRight,
1712 options->padding);
1713 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001714
Mike Kelly5880b912022-01-28 16:18:54 +00001715 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001716
1717 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001718 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001719
David Monahan61683802021-01-12 09:11:07 +00001720 if (desc.m_BiasEnabled)
1721 {
Mike Kelly377fb212023-01-10 15:55:28 +00001722 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Mike Kelly5880b912022-01-28 16:18:54 +00001723 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001724 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001725 filterTensorAndData.first,
1726 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001727 layerName.c_str());
1728 }
1729 else
1730 {
1731 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001732 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001733 EmptyOptional(),
1734 layerName.c_str());
1735 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001736
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001737 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001738
Mike Kelly377fb212023-01-10 15:55:28 +00001739 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001740 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1741
1742 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1743 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001744 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001745
1746 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1747 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1748}
1749
Kevin May7d96b162021-02-03 17:38:41 +00001750void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001751{
1752 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1753}
1754
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001755void TfLiteParserImpl::ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex)
1756{
1757 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1758
1759 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1760 CHECK_VALID_SIZE(inputs.size(), 2);
1761
1762 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1763 CHECK_VALID_SIZE(outputs.size(), 1);
1764
1765 auto layerName = fmt::format("BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1766
Mike Kelly377fb212023-01-10 15:55:28 +00001767 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1768 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001769
1770 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1771 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1772
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001773 // Adjoint in tensorflow lite performs transpose operation
1774 BatchMatMulDescriptor descriptor(options->adj_x,
1775 options->adj_y,
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001776 false,
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001777 false);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001778 // Arbitrary DataLayout
1779
1780 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1781 ARMNN_ASSERT(layer != nullptr);
1782
Mike Kelly377fb212023-01-10 15:55:28 +00001783 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001784 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1785
1786 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1787 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1788
1789 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1790 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1791}
1792
Kevin May7d96b162021-02-03 17:38:41 +00001793void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001794{
1795 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1796
1797 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1798 CHECK_VALID_SIZE(inputs.size(), 3);
1799
1800 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1801 CHECK_VALID_SIZE(outputs.size(), 1);
1802
Mike Kelly377fb212023-01-10 15:55:28 +00001803 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001804 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1805
Mike Kelly377fb212023-01-10 15:55:28 +00001806 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001807 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1808
1809 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1810 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1811
1812 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1813 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1814
1815 size_t step = 2;
1816 std::vector<std::pair<unsigned int, unsigned int>> crops;
1817 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1818 {
1819 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1820 }
1821
1822 armnn::BatchToSpaceNdDescriptor desc;
1823 desc.m_BlockShape = blockShape;
1824 desc.m_Crops = crops;
1825 desc.m_DataLayout = armnn::DataLayout::NHWC;
1826
James Ward58dec6b2020-09-11 17:32:44 +01001827 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001828
Mike Kelly377fb212023-01-10 15:55:28 +00001829 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01001830
1831 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1832 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001833
1834 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1835 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001836 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1837
1838 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1839 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1840
1841 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1842 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1843}
1844
Kevin May7d96b162021-02-03 17:38:41 +00001845void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001846{
1847 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1848
1849 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1850 CHECK_VALID_SIZE(inputs.size(), 1);
1851
1852 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1853 CHECK_VALID_SIZE(outputs.size(), 1);
1854
1855 L2NormalizationDescriptor desc;
1856 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001857 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001858 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1859
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001860 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001861
Mike Kelly377fb212023-01-10 15:55:28 +00001862 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Jackson28c94572019-07-18 10:47:03 +01001863 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1864
1865 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1866 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1867
1868 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1869 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1870}
1871
Kevin May7d96b162021-02-03 17:38:41 +00001872void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001873{
1874 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1875}
1876
Kevin May7d96b162021-02-03 17:38:41 +00001877void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001878{
1879 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1880
1881 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1882 CHECK_VALID_SIZE(inputs.size(), 2);
1883
1884 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1885 CHECK_VALID_SIZE(outputs.size(), 1);
1886
James Ward58dec6b2020-09-11 17:32:44 +01001887 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001888
Mike Kelly377fb212023-01-10 15:55:28 +00001889 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1890 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01001891 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001892
Mike Kelly3ec30772023-03-08 13:47:17 +00001893 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Maximum, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001894 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001895
1896 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1897 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001898 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1899
1900 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001901 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001902
1903 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1904 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1905}
1906
Kevin May7d96b162021-02-03 17:38:41 +00001907void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001908{
1909 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1910
1911 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1912 CHECK_VALID_SIZE(inputs.size(), 2);
1913
1914 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1915 CHECK_VALID_SIZE(outputs.size(), 1);
1916
James Ward58dec6b2020-09-11 17:32:44 +01001917 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001918
Mike Kelly377fb212023-01-10 15:55:28 +00001919 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1920 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01001921 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001922
Mike Kelly3ec30772023-03-08 13:47:17 +00001923 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Minimum, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001924 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001925
1926 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1927 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001928 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1929
1930 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001931 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001932
1933 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1934 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1935}
1936
Kevin May7d96b162021-02-03 17:38:41 +00001937void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1938 size_t operatorIndex,
1939 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001940{
1941 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1942
Mike Kelly0d77ae12022-01-07 17:42:27 +00001943 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1944 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001945
1946 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1947
1948 std::string layerName;
1949
1950 switch (algorithm)
1951 {
1952 case PoolingAlgorithm::Average:
1953 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001954 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001955 break;
1956 case PoolingAlgorithm::Max:
1957 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001958 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001959 break;
1960 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001961 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001962 }
1963
1964 Pooling2dDescriptor desc;
1965
1966 desc.m_PoolType = algorithm;
1967 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1968 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1969 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1970 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1971 desc.m_PaddingMethod = PaddingMethod::Exclude;
1972 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001973 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001974
1975 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1976 CHECK_VALID_SIZE(inputs.size(), 1);
Mike Kelly377fb212023-01-10 15:55:28 +00001977 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001978
1979 // assuming input is NHWC
1980 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1981 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1982
Pablo Tellof0bd6832019-04-26 17:58:13 +01001983 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1984 desc.m_PadTop, desc.m_PadBottom, options->padding);
1985 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1986 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001987
1988 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1989 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001990
James Conroy05102392020-06-24 15:39:55 +01001991 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1992 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001993
1994 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1995 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
jimfly01c25411c2018-11-14 17:47:22 +00001996 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001997
1998 // register the input connection slots for the layer, connections are made after all layers have been created
1999 // only the tensors for the inputs are relevant, exclude the const tensors
2000 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00002001 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002002
jimfly01c25411c2018-11-14 17:47:22 +00002003 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002004 // register the output connection slots for the layer, connections are made after all layers have been created
2005 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2006 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2007}
2008
Kevin May7d96b162021-02-03 17:38:41 +00002009void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06002010{
2011 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2012
2013 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2014 CHECK_VALID_SIZE(inputs.size(), 3);
2015 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2016 CHECK_VALID_SIZE(outputs.size(), 1);
2017
2018 SliceDescriptor desc;
2019
2020 // set begin tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00002021 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
josh minorba424d22019-11-13 10:55:17 -06002022 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2023
2024 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
2025 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2026
2027 // set size tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00002028 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
josh minorba424d22019-11-13 10:55:17 -06002029 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2030
Cathal Corbettde33dda2022-09-20 16:40:09 +01002031 std::vector<int> signedSize(sizeTensorInfo.GetNumElements(), 1);
2032
2033 // if size buffer data is not specified, all contents of size vector remain as values of 1
2034 if (sizeBufferPtr->data.data())
2035 {
2036 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2037 }
2038
josh minorba424d22019-11-13 10:55:17 -06002039 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00002040 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly7ba84d62021-09-10 15:27:19 +01002041
2042 for (unsigned int i = 0; i < signedSize.size(); ++i)
2043 {
2044 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01002045
Mike Kelly7ba84d62021-09-10 15:27:19 +01002046 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
2047 {
2048 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
2049 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
2050 signedValue,
2051 inputTensorInfo.GetShape()[i] - begin[i],
2052 CHECK_LOCATION().AsString()));
2053 }
2054
2055 if (signedValue == -1)
2056 {
2057 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
2058 }
2059 else
2060 {
2061 size[i] = static_cast<unsigned int>(signedValue);
2062 }
2063 }
2064
josh minorba424d22019-11-13 10:55:17 -06002065 desc = SliceDescriptor(begin, size);
2066
James Ward58dec6b2020-09-11 17:32:44 +01002067 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06002068
James Conroy05102392020-06-24 15:39:55 +01002069 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
Mike Kelly377fb212023-01-10 15:55:28 +00002070
2071 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2072 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
josh minorba424d22019-11-13 10:55:17 -06002073 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2074
2075 // register the input connection slots for the layer, connections are made after all layers have been created
2076 // only the tensors for the inputs are relevant, exclude the const tensors
2077 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2078 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2079
2080 // register the output connection slots for the layer, connections are made after all layers have been created
2081 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2082 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2083}
2084
Kevin May7d96b162021-02-03 17:38:41 +00002085void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002086{
2087 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002088 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2089 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01002090
2091 SoftmaxDescriptor desc;
2092 desc.m_Beta = options->beta;
2093
2094 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2095 CHECK_VALID_SIZE(inputs.size(), 1);
2096 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2097 CHECK_VALID_SIZE(outputs.size(), 1);
2098
James Ward58dec6b2020-09-11 17:32:44 +01002099 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002100 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2101
Mike Kelly377fb212023-01-10 15:55:28 +00002102 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
telsoa01c577f2c2018-08-31 09:22:23 +01002103 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2104
2105 // register the input connection slots for the layer, connections are made after all layers have been created
2106 // only the tensors for the inputs are relevant, exclude the const tensors
2107 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2108 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2109
2110 // register the output connection slots for the layer, connections are made after all layers have been created
2111 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2112 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2113}
2114
Teresa Charlinfd33a692022-06-29 15:35:57 +01002115void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
2116{
2117 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2118
2119 LogSoftmaxDescriptor desc;
2120
2121 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2122 CHECK_VALID_SIZE(inputs.size(), 1);
2123 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2124 CHECK_VALID_SIZE(outputs.size(), 1);
2125
2126 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2127 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2128
Mike Kelly377fb212023-01-10 15:55:28 +00002129 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Teresa Charlinfd33a692022-06-29 15:35:57 +01002130 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2131
2132 // register the input connection slots for the layer, connections are made after all layers have been created
2133 // only the tensors for the inputs are relevant, exclude the const tensors
2134 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2135 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2136
2137 // register the output connection slots for the layer, connections are made after all layers have been created
2138 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2139 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2140}
2141
Kevin May7d96b162021-02-03 17:38:41 +00002142void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002143{
2144 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2145
2146 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2147 CHECK_VALID_SIZE(inputs.size(), 3);
2148
2149 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2150 CHECK_VALID_SIZE(outputs.size(), 1);
2151
Mike Kelly377fb212023-01-10 15:55:28 +00002152 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002153 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2154
Mike Kelly377fb212023-01-10 15:55:28 +00002155 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002156 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2157
2158 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
2159 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
2160
2161 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
2162 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
2163
2164 size_t step = 2;
2165 std::vector<std::pair<unsigned int, unsigned int>> padList;
2166 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
2167 {
2168 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2169 }
2170
2171 armnn::SpaceToBatchNdDescriptor desc;
2172 desc.m_BlockShape = blockShape;
2173 desc.m_PadList = padList;
2174 desc.m_DataLayout = armnn::DataLayout::NHWC;
2175
James Ward58dec6b2020-09-11 17:32:44 +01002176 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002177
Mike Kelly377fb212023-01-10 15:55:28 +00002178 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01002179
2180 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2181 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002182
2183 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2184 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002185 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2186
2187 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2188 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2189
2190 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2191 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2192}
2193
Teresa Charlin2a764ad2023-02-24 18:17:31 +00002194void TfLiteParserImpl::ParseSpaceToDepth(size_t subgraphIndex, size_t operatorIndex)
2195{
2196 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2197
2198 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2199 CHECK_VALID_SIZE(inputs.size(), 1);
2200 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2201 CHECK_VALID_SIZE(outputs.size(), 1);
2202
2203 armnn::SpaceToDepthDescriptor descriptor;
2204
2205 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2206 const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2207 auto blockSize = options->block_size;
2208 if (blockSize < 2)
2209 {
2210 throw ParseException(
2211 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
2212 blockSize,
2213 CHECK_LOCATION().AsString()));
2214 }
2215 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2216
2217 auto layerName = fmt::format("SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2218 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2219 ARMNN_ASSERT(layer != nullptr);
2220 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2221 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2222
2223 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2224 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2225
2226 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2227 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2228}
2229
Teresa Charlin3ab85482021-06-08 16:59:29 +01002230armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00002231 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01002232{
Teresa Charlin3ab85482021-06-08 16:59:29 +01002233 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01002234 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2235
2236 if (inputTensorInfo.GetNumDimensions() > 4)
2237 {
2238 std::stringstream ss;
2239 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2240 << " shape:" << inputTensorInfo.GetShape() << " "
2241 << CHECK_LOCATION().AsString();
2242 throw ParseException(ss.str());
2243 }
2244
2245 if (squeezeDims.empty())
2246 {
2247 squeezeDims.assign(dimensionSequence,
2248 dimensionSequence+inputTensorInfo.GetNumDimensions());
2249 }
2250
2251 std::vector<uint32_t> outputDims;
2252 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2253 {
2254 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2255 auto currentDimension = inputTensorInfo.GetShape()[i];
2256 if (skipSqueeze || currentDimension != 1)
2257 {
2258 outputDims.push_back(currentDimension);
2259 }
2260 }
2261
2262 if (outputDims.size() > 4)
2263 {
2264 std::stringstream ss;
2265 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2266 << " shape:" << inputTensorInfo.GetShape() << " "
2267 << CHECK_LOCATION().AsString();
2268 throw ParseException(ss.str());
2269 }
2270
2271 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2272 outputDims.data());
2273
2274 // we need to preserve the tensor type and the quantization data as well
2275 TensorInfo outTensorInfo = inputTensorInfo;
2276 outTensorInfo.SetShape(outShape);
2277
2278 return outTensorInfo;
2279}
2280
Keith Davis0176fd82021-06-01 17:36:32 +01002281void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2282{
2283 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2284
2285 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2286 CHECK_VALID_SIZE(inputs.size(), 1);
2287 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2288 CHECK_VALID_SIZE(outputs.size(), 1);
2289
2290 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2291
2292 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2293 ARMNN_ASSERT(layer != nullptr);
2294
Mike Kelly377fb212023-01-10 15:55:28 +00002295 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Keith Davis0176fd82021-06-01 17:36:32 +01002296 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2297
2298 // Check if output tensor type is Signed32 or Signed64
2299 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2300 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2301 {
2302 throw ParseException(
2303 fmt::format(
2304 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2305 CHECK_LOCATION().AsString()));
2306 }
2307
2308 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2309 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2310
2311 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2312 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2313}
2314
Kevin May7d96b162021-02-03 17:38:41 +00002315void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002316{
2317 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2318
2319 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2320 CHECK_VALID_SIZE(inputs.size(), 1);
2321
2322 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2323 CHECK_VALID_SIZE(outputs.size(), 1);
2324
2325 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2326 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002327 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002328
Mike Kelly377fb212023-01-10 15:55:28 +00002329 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002330
2331 std::vector<uint32_t> squeezeDim;
2332 // A single negative dim index is interpreted as a negative index in python
2333 // Meaning the index will be the shape size plus the negative index value
2334 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2335 {
2336 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2337 squeezeDim.push_back(static_cast<uint32_t>(dim));
2338 }
2339 else
2340 {
2341 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2342 }
2343
2344 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2345
James Conroy05102392020-06-24 15:39:55 +01002346 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002347
2348 ReshapeDescriptor reshapeDesc;
2349 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2350
Mike Kellyb2293702023-02-14 17:16:12 +00002351 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
2352 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2353
telsoa01c577f2c2018-08-31 09:22:23 +01002354 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002355 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002356 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2357
2358 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2359 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2360
2361 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2362 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2363}
2364
Kevin May7d96b162021-02-03 17:38:41 +00002365void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002366{
2367 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2368
2369 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2370 CHECK_VALID_SIZE(inputs.size(), 4);
2371
2372 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2373 CHECK_VALID_SIZE(outputs.size(), 1);
2374
Mike Kelly0d77ae12022-01-07 17:42:27 +00002375 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2376 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002377
2378 StridedSliceDescriptor desc;
2379 desc.m_BeginMask = options->begin_mask;
2380 desc.m_EllipsisMask = options->ellipsis_mask;
2381 desc.m_EndMask = options->end_mask;
2382 desc.m_NewAxisMask = options->new_axis_mask;
2383 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2384 desc.m_DataLayout = armnn::DataLayout::NHWC;
2385
Mike Kelly377fb212023-01-10 15:55:28 +00002386 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002387 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2388
2389 std::vector<int> begin(beginTensorInfo.GetNumElements());
2390 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2391
Mike Kelly377fb212023-01-10 15:55:28 +00002392 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002393 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2394
2395 std::vector<int> end(endTensorInfo.GetNumElements());
2396 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2397
Mike Kelly377fb212023-01-10 15:55:28 +00002398 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002399 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2400
2401 std::vector<int> stride(strideTensorInfo.GetNumElements());
2402 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2403
2404 desc.m_Begin = begin;
2405 desc.m_End = end;
2406 desc.m_Stride = stride;
2407
James Ward58dec6b2020-09-11 17:32:44 +01002408 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002409 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002410 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002411
Mike Kelly377fb212023-01-10 15:55:28 +00002412 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002413 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2414
2415 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2416 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2417
2418 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2419 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2420}
2421
Kevin May7d96b162021-02-03 17:38:41 +00002422void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002423{
2424 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2425
Mike Kelly0d77ae12022-01-07 17:42:27 +00002426 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2427 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002428
2429 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2430 CHECK_VALID_SIZE(inputs.size(), 2);
2431
2432 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2433 CHECK_VALID_SIZE(outputs.size(), 1);
2434
Mike Kelly377fb212023-01-10 15:55:28 +00002435 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2436 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002437
James Ward58dec6b2020-09-11 17:32:44 +01002438 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002439 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Sub, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002440 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002441
Mike Kelly377fb212023-01-10 15:55:28 +00002442 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002443 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2444
2445 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002446 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002447 if (options)
2448 {
2449 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2450 }
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002451
2452 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2453 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2454}
2455
Kevin May7d96b162021-02-03 17:38:41 +00002456void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302457{
2458 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2459
Mike Kelly0d77ae12022-01-07 17:42:27 +00002460 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2461 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302462
2463 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2464 CHECK_VALID_SIZE(inputs.size(), 2);
2465
2466 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2467 CHECK_VALID_SIZE(outputs.size(), 1);
2468
Mike Kelly377fb212023-01-10 15:55:28 +00002469 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2470 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302471
James Ward58dec6b2020-09-11 17:32:44 +01002472 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002473 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002474 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302475
Mike Kelly377fb212023-01-10 15:55:28 +00002476 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302477 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2478
2479 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002480 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002481 if (options)
2482 {
2483 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2484 }
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302485
2486 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2487 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2488}
2489
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002490void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2491{
2492 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2493
2494 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2495 CHECK_VALID_SIZE(inputs.size(), 2);
2496
2497 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2498 CHECK_VALID_SIZE(outputs.size(), 1);
2499
Mike Kelly377fb212023-01-10 15:55:28 +00002500 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2501 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002502
2503 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002504 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002505 ARMNN_ASSERT(layer != nullptr);
2506
Mike Kelly377fb212023-01-10 15:55:28 +00002507 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002508 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2509
2510 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2511 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2512 layer = AddFusedFloorLayer(layer, 0);
2513
2514 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2515 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2516}
2517
Kevin May7d96b162021-02-03 17:38:41 +00002518void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002519{
2520 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2521
Mike Kelly0d77ae12022-01-07 17:42:27 +00002522 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2523 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002524
2525 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2526 CHECK_VALID_SIZE(inputs.size(), 2);
2527
2528 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2529 CHECK_VALID_SIZE(outputs.size(), 1);
2530
Mike Kelly377fb212023-01-10 15:55:28 +00002531 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2532 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002533
James Ward58dec6b2020-09-11 17:32:44 +01002534 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002535 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Add, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002536 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002537
Mike Kelly377fb212023-01-10 15:55:28 +00002538 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002539 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2540
2541 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002542 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002543 if (options)
2544 {
2545 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2546 }
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002547
2548 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2549 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2550}
2551
Kevin May7d96b162021-02-03 17:38:41 +00002552void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002553{
2554 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2555
Mike Kelly0d77ae12022-01-07 17:42:27 +00002556 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2557 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002558
2559 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2560 CHECK_VALID_SIZE(inputs.size(), 2);
2561
2562 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2563 CHECK_VALID_SIZE(outputs.size(), 1);
2564
Mike Kelly377fb212023-01-10 15:55:28 +00002565 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2566 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002567
James Ward58dec6b2020-09-11 17:32:44 +01002568 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002569 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002570 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002571
Mike Kelly377fb212023-01-10 15:55:28 +00002572 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002573 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2574
2575 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002576 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002577 if (options)
2578 {
2579 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2580 }
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002581
2582 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2583 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2584}
2585
Kevin May7d96b162021-02-03 17:38:41 +00002586void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002587{
2588 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2589
2590 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2591
2592 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2593 CHECK_VALID_SIZE(outputs.size(), 1);
2594
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002595 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2596 TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002597
2598 armnn::MeanDescriptor desc;
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002599 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2600 // Get const axis value from model and set it to descriptor.
2601 if (axisBufferPtr != nullptr)
2602 {
2603 std::vector<int32_t> axisData(dimTensorInfo.GetNumElements());
2604 ::memcpy(axisData.data(), axisBufferPtr->data.data(), dimTensorInfo.GetNumBytes());
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002605
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002606 // Convert the axis to unsigned int and remove duplicates.
2607 auto rank = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2608 std::set<unsigned int> uniqueAxis;
2609 std::transform(axisData.begin(),
2610 axisData.end(),
2611 std::inserter(uniqueAxis, uniqueAxis.begin()),
2612 [rank](int i)->unsigned int{
2613 return static_cast<uint32_t>(((i + rank) % rank)); });
2614 desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2615 }
2616 else
2617 {
2618 for (uint32_t i = 0; i < inputTensorInfo.GetNumDimensions(); ++i)
2619 {
2620 desc.m_Axis.push_back(i);
2621 }
2622 }
2623
Sadik Armagand109a4d2020-07-28 10:42:13 +01002624 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002625
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002626 desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002627
James Ward58dec6b2020-09-11 17:32:44 +01002628 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002629 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002630 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002631
Mike Kelly377fb212023-01-10 15:55:28 +00002632 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002633 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2634
2635 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2636 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2637
2638 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2639 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2640}
2641
Kevin May7d96b162021-02-03 17:38:41 +00002642void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002643{
2644 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2645
Kevin May7d96b162021-02-03 17:38:41 +00002646 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002647
Kevin May7d96b162021-02-03 17:38:41 +00002648 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002649 CHECK_VALID_SIZE(outputs.size(), 1);
2650
Mike Kelly377fb212023-01-10 15:55:28 +00002651 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2652 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002653
Mike Kelly0d77ae12022-01-07 17:42:27 +00002654 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002655
2656 size_t step = 2;
2657 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002658 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2659
2660 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002661 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002662 CHECK_VALID_SIZE(inputs.size(), 2);
2663
2664 if (inputTensorInfo.IsQuantized())
2665 {
2666 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2667 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002668 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002669 else if (opcode == tflite::BuiltinOperator_PADV2)
2670 {
2671 CHECK_VALID_SIZE(inputs.size(), 3);
2672
Mike Kelly377fb212023-01-10 15:55:28 +00002673 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002674
2675 if (padValueTensorInfo.GetNumElements() != 1)
2676 {
2677 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2678 }
2679 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2680
2681 // Get the pad value from the input tensor
2682 if (padValueBufferPtr->data.size() > 0)
2683 {
2684 switch (padValueTensorInfo.GetDataType())
2685 {
2686 case armnn::DataType::Float32:
2687 {
2688 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2689 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2690 desc.m_PadValue = padValueBuffer[0];
2691 break;
2692 }
2693 case armnn::DataType::QAsymmU8:
2694 {
2695 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2696 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2697 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2698 padValueTensorInfo.GetQuantizationScale(),
2699 padValueTensorInfo.GetQuantizationOffset());
2700 break;
2701 }
2702 case armnn::DataType::QAsymmS8:
2703 case armnn::DataType::QSymmS8:
2704 {
2705 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2706 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2707 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2708 padValueTensorInfo.GetQuantizationScale(),
2709 padValueTensorInfo.GetQuantizationOffset());
2710 break;
2711 }
2712 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2713 }
2714 }
2715 else if (inputTensorInfo.IsQuantized())
2716 {
2717 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2718 }
2719 }
2720
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002721 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2722 {
2723 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2724 }
2725
Mike Kelly0d77ae12022-01-07 17:42:27 +00002726 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2727 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01002728
2729 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2730 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002731 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002732 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2733
2734 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2735 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2736
2737 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2738 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2739}
2740
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002741void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2742{
2743 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2744
2745 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2746 CHECK_VALID_SIZE(inputs.size(), 2);
2747
2748 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2749 CHECK_VALID_SIZE(outputs.size(), 1);
2750
Mike Kelly377fb212023-01-10 15:55:28 +00002751 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002752
Mike Kelly377fb212023-01-10 15:55:28 +00002753 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002754 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2755
2756 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2757 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2758
2759 size_t step = 2;
2760 armnn::PadDescriptor desc;
2761 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2762 {
2763 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2764 }
2765
2766 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2767 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2768
2769 if (options->mode == tflite::MirrorPadMode_REFLECT)
2770 {
2771 desc.m_PaddingMode = PaddingMode::Reflect;
2772 }
2773 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2774 {
2775 desc.m_PaddingMode = PaddingMode::Symmetric;
2776 }
2777 else
2778 {
2779 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2780 }
2781
2782 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2783 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2784 auto inputShape = inputTensorInfo.GetShape();
2785 auto padList = desc.m_PadList;
2786
2787 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2788 for(unsigned int i = 0; i < padList.size(); ++i)
2789 {
2790 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2791 padList.at(i).second > (inputShape[i] - isReflect))
2792 {
2793 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2794 "equal (Symmetric) to the dimension size.");
2795 }
2796 }
2797
2798 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002799
2800 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2801 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002802 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002803 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2804
2805 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2806 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2807
2808 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2809 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2810}
2811
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002812void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2813{
2814 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2815
2816 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2817 CHECK_VALID_SIZE(inputs.size(), 2);
2818
2819 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2820 CHECK_VALID_SIZE(outputs.size(), 1);
2821
2822 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2823
Mike Kelly377fb212023-01-10 15:55:28 +00002824 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2825 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002826
2827 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2828 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002829
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002830
2831 if (IsConstTensor(inputs[1]))
2832 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002833 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002834 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2835 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002836
Mike Kelly5880b912022-01-28 16:18:54 +00002837 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2838 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002839 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2840 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002841 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002842 ARMNN_ASSERT(constLayer != nullptr);
2843
2844 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2845 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2846 RegisterOutputSlots(subgraphIndex,
2847 VIRTUAL_OPERATOR_ID,
2848 constLayer,
2849 { inputTensorIndexes[1] });
2850 }
2851 else
2852 {
2853 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2854 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2855 }
2856
Mike Kelly377fb212023-01-10 15:55:28 +00002857 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2858 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2859 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2860
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002861 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2862 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2863}
2864
Kevin May7d96b162021-02-03 17:38:41 +00002865void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002866{
2867 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2868
2869 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2870 CHECK_VALID_SIZE(inputs.size(), 1);
2871
2872 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2873 CHECK_VALID_SIZE(outputs.size(), 1);
2874
James Ward58dec6b2020-09-11 17:32:44 +01002875 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002876
2877 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002878 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002879
Mike Kelly377fb212023-01-10 15:55:28 +00002880 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan66dedc72019-12-10 16:32:07 +00002881 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2882
2883 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2884 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2885
2886 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2887 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2888}
Finn Williamsc42c3842019-01-22 14:18:11 +00002889
Kevin May7d96b162021-02-03 17:38:41 +00002890void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002891{
Finn Williamsc42c3842019-01-22 14:18:11 +00002892 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002893}
2894
Kevin May7d96b162021-02-03 17:38:41 +00002895void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002896{
Finn Williamsc42c3842019-01-22 14:18:11 +00002897 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2898}
Sadik Armagan58f39192018-09-17 14:14:39 +01002899
Kevin May7d96b162021-02-03 17:38:41 +00002900void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002901{
Jan Eilers2f746b32020-07-28 14:00:06 +01002902 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002903}
2904
Kevin May7d96b162021-02-03 17:38:41 +00002905void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002906{
2907 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2908}
2909
Kevin May7d96b162021-02-03 17:38:41 +00002910void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002911{
2912 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2913}
2914
Kevin May7d96b162021-02-03 17:38:41 +00002915void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002916{
2917 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2918}
2919
Kevin May7d96b162021-02-03 17:38:41 +00002920void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002921{
2922 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2923}
Finn Williamsc42c3842019-01-22 14:18:11 +00002924
Kevin May7d96b162021-02-03 17:38:41 +00002925void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002926{
2927 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002928 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002929 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002930
2931 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2932 CHECK_VALID_SIZE(inputs.size(), 1);
2933
2934 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2935 CHECK_VALID_SIZE(outputs.size(), 1);
2936
James Ward58dec6b2020-09-11 17:32:44 +01002937 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002938 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002939 activationDesc.m_Function = activationType;
2940
2941 switch (activationType)
2942 {
2943 case ActivationFunction::ReLu:
2944 {
James Ward58dec6b2020-09-11 17:32:44 +01002945 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002946 break;
2947 }
2948 case ActivationFunction::BoundedReLu:
2949 {
James Ward58dec6b2020-09-11 17:32:44 +01002950 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002951 activationDesc.m_A = 6.0f;
2952 activationDesc.m_B = 0.0f;
2953 break;
2954 }
2955 case ActivationFunction::Sigmoid:
2956 {
James Ward58dec6b2020-09-11 17:32:44 +01002957 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002958 break;
2959 }
Nina Drozd99851762019-04-09 09:37:38 +01002960 case ActivationFunction::TanH:
2961 {
James Ward58dec6b2020-09-11 17:32:44 +01002962 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002963 activationDesc.m_A = 1.0f;
2964 activationDesc.m_B = 1.0f;
2965 break;
2966 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002967 case ActivationFunction::LeakyReLu:
2968 {
James Ward58dec6b2020-09-11 17:32:44 +01002969 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002970 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002971 activationDesc.m_A = options->alpha;
2972 break;
2973 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002974 case ActivationFunction::Elu:
2975 {
2976 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2977 activationDesc.m_A = 1.0f;
2978 break;
2979 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002980 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002981 {
James Ward58dec6b2020-09-11 17:32:44 +01002982 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002983 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002984 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002985 default:
2986 {
2987 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002988 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2989 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002990 }
2991 }
2992
2993 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002994
Mike Kelly377fb212023-01-10 15:55:28 +00002995 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan58f39192018-09-17 14:14:39 +01002996 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2997
2998 // register the input connection slots for the layer, connections are made after all layers have been created
2999 // only the tensors for the inputs are relevant, exclude the const tensors
3000 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3001 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3002
3003 // register the output connection slots for the layer, connections are made after all layers have been created
3004 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3005 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3006}
Mike Kelly0d77ae12022-01-07 17:42:27 +00003007armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
3008 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01003009{
3010 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3011 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3012
3013 if (stretchDim != targetDimsIn.end())
3014 {
3015 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3016 {
3017 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003018 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01003019 }
3020
3021 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003022 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01003023 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3024
3025 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3026 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
3027 }
3028
3029 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
3030
3031 TensorInfo reshapeInfo = inputTensorInfo;
3032 reshapeInfo.SetShape(outputShape);
3033
3034 return reshapeInfo;
3035}
3036
Kevin May7d96b162021-02-03 17:38:41 +00003037void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01003038{
3039 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3040
3041 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01003042
3043 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3044 CHECK_VALID_SIZE(outputs.size(), 1);
3045
Mike Kelly0d77ae12022-01-07 17:42:27 +00003046 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3047 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01003048 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01003049
Mike Kelly377fb212023-01-10 15:55:28 +00003050 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
kevmay0171972a82018-12-17 14:28:03 +00003051 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01003052 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00003053
Jan Eilersbac9b352020-07-13 13:40:24 +01003054 // Extracting new shape for the output
3055 // There are two ways it can be passed
3056 // * First is to define the target shape in the operator built-in options
3057 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00003058 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01003059 bool targetShapeFound = false;
3060 // Check if built-in options were given
3061 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00003062 {
Jan Eilersbac9b352020-07-13 13:40:24 +01003063 // make sure the parameter is given
3064 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00003065 {
Jan Eilersbac9b352020-07-13 13:40:24 +01003066 targetShape = options->new_shape;
3067 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00003068 }
Derek Lambertic9e52792020-03-11 11:42:26 +00003069 }
Jan Eilersbac9b352020-07-13 13:40:24 +01003070
3071 // If there is no built-in option given or if the built-in new_shape parameter was empty
3072 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00003073 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00003074 // Check for a second input tensor
3075 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01003076 {
3077 if (inputs[1]->is_variable)
3078 {
3079 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
3080 }
3081
3082 if (inputs[1]->shape.size() != 1)
3083 {
3084 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
3085 }
3086
3087 if (inputs[1]->type != tflite::TensorType_INT32)
3088 {
3089 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
3090 }
3091
Teresa Charlin6a056a42021-12-01 10:25:43 +00003092 // Extract target shape from input
3093 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3094 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00003095 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00003096 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003097 for (int i = 0; i < inputs[1]->shape[0]; ++i)
3098 {
3099 targetShape.push_back(values[i]);
3100 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00003101 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003102 else
Jan Eilersbac9b352020-07-13 13:40:24 +01003103 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003104 try
3105 {
3106 // We attempt to infer during Runtime.
Mike Kelly04d82292023-01-19 18:29:40 +00003107 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
3108
3109 if (reshapeShapes[0] == actualOutputTensorInfo.GetNumDimensions())
3110 {
3111 for (unsigned int i = 0; i < actualOutputTensorInfo.GetShape().GetNumDimensions(); ++i)
3112 {
3113 targetShape.push_back(actualOutputTensorInfo.GetShape()[i]);
3114 }
3115 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003116 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
Mike Kelly04d82292023-01-19 18:29:40 +00003117 else if (reshapeShapes[0] > 2)
Cathal Corbettd2f73232021-12-10 13:38:52 +00003118 {
3119 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
3120 "When inferring during runtime, the parser only supports "
3121 "shape (batch, -1) or (-1) for target shape input.",
3122 reshapeShapes[0],
3123 layerName,
3124 CHECK_LOCATION().AsString()));
3125 }
Mike Kelly04d82292023-01-19 18:29:40 +00003126 else
Cathal Corbettd2f73232021-12-10 13:38:52 +00003127 {
Mike Kelly04d82292023-01-19 18:29:40 +00003128 const int32_t numInputElements = inputTensorInfo.GetNumElements();
3129 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
3130 if (reshapeShapes[0] == 1)
3131 {
3132 targetShape = {numInputElements};
3133 }
3134 else if (reshapeShapes[0] == 2)
3135 {
3136 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3137 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003138 }
3139 }
3140 catch (const std::exception& exc)
3141 {
3142 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
3143 "Reshape operation. Reshape operator target shape input buffer data "
3144 "is null. " << exc.what());
3145 }
Jan Eilersbac9b352020-07-13 13:40:24 +01003146 }
3147 }
3148 else
Derek Lambertic9e52792020-03-11 11:42:26 +00003149 {
3150 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
3151 "At least one method required");
3152 }
Derek Lambertic9e52792020-03-11 11:42:26 +00003153 }
3154
kevmay0171972a82018-12-17 14:28:03 +00003155 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00003156 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01003157
kevmay0171972a82018-12-17 14:28:03 +00003158 // Check for valid input size and that reshape parameters equal output shape
Cathal Corbett2b922e22022-09-23 15:49:24 +01003159 // The output shape can be provided to us in 2 ways:
3160 // 1. through the normal 'shape' parameter given by outputs[indx]->shape
3161 // 2. through additional parameter 'shape_signature' given by outputs[indx]->buffer.
3162 // This parameter can sometimes contain -1 value not visible in the 'shape' parameter.
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00003163 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
3164 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00003165 {
Cathal Corbett2b922e22022-09-23 15:49:24 +01003166 // Attempt to extract output shape from secondary 'shape_signature'
3167 // parameter and try to CheckShape() with this param.
3168 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3169
3170 // if outputs[0]->shape_signature contain a -1 value, we need to compute its actual value
3171 // from reshape input in order to correctly verify reshape parameters equal output shape
3172 armnn::TensorInfo secondaryReshapeOutputTensorInfo =
3173 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, secondaryOutputTargetShape);
3174
3175 if (!CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.GetShape()))
3176 {
3177 std::stringstream ss;
3178 ss << "New shape defined in reshape parameters "
3179 << reshapeOutputTensorShape
3180 << " does not equal output shape "
3181 << actualOutputTensorInfo.GetShape()
3182 << ": "
3183 << CHECK_LOCATION().AsString();
3184 throw ParseException(ss.str());
3185 }
kevmay0171972a82018-12-17 14:28:03 +00003186 }
Mike Kelly377fb212023-01-10 15:55:28 +00003187 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
kevmay0171972a82018-12-17 14:28:03 +00003188
Sadikb94967b2018-09-19 15:30:00 +01003189 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00003190 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Mike Kelly377fb212023-01-10 15:55:28 +00003191 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
Sadikb94967b2018-09-19 15:30:00 +01003192
Sadikb94967b2018-09-19 15:30:00 +01003193 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003194 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00003195 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01003196
3197 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3198 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3199
3200 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3201 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3202}
3203
Kevin May7d96b162021-02-03 17:38:41 +00003204void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003205{
Sadik Armagana3b31f02019-12-05 09:08:53 +00003206 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3207}
3208
Kevin May7d96b162021-02-03 17:38:41 +00003209void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003210{
3211 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3212}
3213
Kevin May7d96b162021-02-03 17:38:41 +00003214void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003215{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003216 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3217
3218 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3219 CHECK_VALID_SIZE(inputs.size(), 2);
3220
3221 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3222 CHECK_VALID_SIZE(outputs.size(), 1);
3223
Mike Kelly377fb212023-01-10 15:55:28 +00003224 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003225
3226 // Data for the parsed tensor args (size) must be stored locally.
3227 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
3228
3229 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3230 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
3231
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003232 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003233 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003234 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003235 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
3236 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003237
James Ward58dec6b2020-09-11 17:32:44 +01003238 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00003239
3240 switch (resizeMethod)
3241 {
3242 case ResizeMethod::Bilinear:
3243 {
James Ward58dec6b2020-09-11 17:32:44 +01003244 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00003245
3246 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3247 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3248
David Monahan4a0c9b92020-05-30 09:48:39 +01003249 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003250 break;
3251 }
3252 case ResizeMethod::NearestNeighbor:
3253 {
James Ward58dec6b2020-09-11 17:32:44 +01003254 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00003255 break;
3256 }
3257 default:
3258 {
3259 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003260 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
3261 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00003262 }
3263 }
3264
Mike Kelly377fb212023-01-10 15:55:28 +00003265 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01003266
3267 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
3268 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00003269 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3270 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003271 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3272
3273 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3274 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3275
3276 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3277 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3278}
3279
Tianle Chenge5a30ff2023-07-03 11:24:12 +01003280void TfLiteParserImpl::ParseReverseV2(size_t subgraphIndex, size_t operatorIndex)
3281{
3282 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3283
3284 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3285 CHECK_VALID_SIZE(inputs.size(), 2);
3286
3287 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3288 CHECK_VALID_SIZE(outputs.size(), 1);
3289
3290 auto layerName = fmt::format("ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3291
3292 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3293 TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
3294 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3295
3296 std::vector<int32_t> axisTensorData(axisTensorInfo.GetNumElements());
3297
3298 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3299 ::memcpy(axisTensorData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3300
3301 ReverseV2Descriptor descriptor(axisTensorData);
3302
3303 auto inputRank = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3304 std::vector<bool> dimFlag(inputRank, false);
3305
3306 for (auto axis : axisTensorData)
3307 {
3308 if (axis < -inputRank || axis >= inputRank)
3309 {
3310 throw ParseException(
3311 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3312 axis,
3313 inputRank, inputRank,
3314 CHECK_LOCATION().AsString()));
3315 }
3316
3317 auto posAxis = axis < 0 ? axis + inputRank : axis;
3318
3319 if (dimFlag[posAxis])
3320 {
3321 throw ParseException(
3322 fmt::format("Operation has repeated axis: {} {}",
3323 axis,
3324 CHECK_LOCATION().AsString()));
3325 }
3326 dimFlag[posAxis] = true;
3327 }
3328
3329 IConnectableLayer* layer = m_Network->AddReverseV2Layer(descriptor, layerName.c_str());
3330 ARMNN_ASSERT(layer != nullptr);
3331
3332 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3333
3334 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3335 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3336
3337 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3338 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3339}
3340
Kevin May7d96b162021-02-03 17:38:41 +00003341void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01003342{
3343 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3344
Mike Kelly0d77ae12022-01-07 17:42:27 +00003345 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3346 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003347
3348 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3349
3350 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3351 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003352 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
3353
Sadik Armagan479045b2018-10-01 11:51:37 +01003354 CHECK_VALID_SIZE(outputs.size(), 1);
3355
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003356 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
Mike Kelly377fb212023-01-10 15:55:28 +00003357 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003358
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003359 const unsigned int concatDimInput = static_cast<unsigned int>(
Mike Kelly377fb212023-01-10 15:55:28 +00003360 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01003361
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003362 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3363 concatDescriptor.SetConcatAxis(concatDimInput);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003364 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01003365
3366 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3367 {
Mike Kelly377fb212023-01-10 15:55:28 +00003368 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
Sadik Armagan479045b2018-10-01 11:51:37 +01003369
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003370 // This set up concatDescriptor view origin
3371 armnnUtils::ProcessConcatInputTensorInfo(
Mike Kelly377fb212023-01-10 15:55:28 +00003372 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01003373 }
3374
James Ward58dec6b2020-09-11 17:32:44 +01003375 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01003376
Jim Flynn906f9462019-05-10 13:55:21 +01003377 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003378 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00003379 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003380 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01003381
James Conroy05102392020-06-24 15:39:55 +01003382 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003383 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01003384
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003385 // add fused activation layer
3386 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01003387
Sadik Armagan479045b2018-10-01 11:51:37 +01003388 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3389 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3390}
3391
Kevin May7d96b162021-02-03 17:38:41 +00003392void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003393{
3394 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3395
Mike Kelly0d77ae12022-01-07 17:42:27 +00003396 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003397 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3398
3399 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3400
3401 FullyConnectedDescriptor desc;
3402 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003403 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003404
3405 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3406 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3407 CHECK_VALID_SIZE(outputs.size(), 1);
3408
Mike Kelly377fb212023-01-10 15:55:28 +00003409 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003410
3411 // Fully Connected Layer accepts two dimensional weights input
3412 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3413 if (weightsDimension != 2)
3414 {
3415 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003416 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3417 "Node {}",
3418 weightsDimension,
3419 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003420 }
3421
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003422 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003423 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003424
Matthew Sloyan81beae32021-07-13 19:46:11 +01003425 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3426 // Add the first input tensor to the registration list
3427 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
Mike Kelly377fb212023-01-10 15:55:28 +00003428 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003429
3430 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3431
Matthew Sloyan81beae32021-07-13 19:46:11 +01003432 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3433 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003434
Mike Kelly0506ef02023-01-03 16:29:44 +00003435 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003436 {
3437 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3438 }
3439
Finn Williamsd4fa5452021-03-01 12:31:41 +00003440 if (inputs.size() == 3)
3441 {
3442 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00003443 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003444
3445 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3446 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003447
Mike Kelly0506ef02023-01-03 16:29:44 +00003448 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003449 {
3450 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3451 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003452 }
3453
Matthew Sloyan81beae32021-07-13 19:46:11 +01003454 // Filters and biases are always passed to fully connected as inputs
3455 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003456
3457 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003458
Finn Williamsd4fa5452021-03-01 12:31:41 +00003459 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003460 if (inputTensorInfo.GetNumDimensions() > 2)
3461 {
3462 // Add reshape to flatten to 2D [batch_size, input_size],
3463 // where "input_size" corresponds to the number of inputs to the layer,
3464 // matching the second dimension of weights,
3465 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3466 std::vector<unsigned int> reshapedDimensions(2);
3467 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3468 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3469
3470 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3471 {
3472 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003473 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3474 reshapedDimensions[1],
3475 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003476 }
3477
Mike Kelly377fb212023-01-10 15:55:28 +00003478 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003479 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
Mike Kelly377fb212023-01-10 15:55:28 +00003480 inputTensorInfo = reshapedTensorInfo;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003481
James Ward58dec6b2020-09-11 17:32:44 +01003482 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003483 armnn::ReshapeDescriptor reshapeDescriptor;
3484 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
Mike Kelly04d82292023-01-19 18:29:40 +00003485 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor,
3486 reshapeLayerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003487
3488 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3489 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3490
3491 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003492 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3493 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3494 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003495 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003496
3497 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003498
Mike Kelly377fb212023-01-10 15:55:28 +00003499 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3500 { inputTensorInfo.GetShape(),
3501 filterTensorInfo.GetShape() });
Mike Kelly04d82292023-01-19 18:29:40 +00003502
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003503 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3504
Mike Kelly04d82292023-01-19 18:29:40 +00003505 if (outputTensorInfo.GetNumDimensions() > 2)
3506 {
3507 // Calculate reshape to flatten to 2D [batch_size, input_size]
3508 std::vector<unsigned int> reshapedDimensions(2);
3509 reshapedDimensions[1] = filterTensorInfo.GetShape()[0];
3510 reshapedDimensions[0] = outputTensorInfo.GetNumElements() / reshapedDimensions[1];
3511 armnn::TensorInfo reshapedOutputTensorInfo = outputTensorInfo;
3512 if (outputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3513 {
3514 throw ParseException(
3515 fmt::format("Failed to deduce output tensor shape from filter size {} {}",
3516 reshapedDimensions[1],
3517 CHECK_LOCATION().AsString()));
3518 }
3519 reshapedOutputTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3520 layer->GetOutputSlot(0).SetTensorInfo(reshapedOutputTensorInfo);
3521
3522 std::string reshapeLayerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3523 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3524 }
3525
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003526 // we need to add the activation layer and fortunately we don't need to care about the data layout
3527 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3528 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003529
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003530 // register the output connection slots for the layer, connections are made after all layers have been created
3531 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3532 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
Mike Kelly04d82292023-01-19 18:29:40 +00003533
3534 m_TensorInfos[outputTensorIndexes[0]] = layer->GetOutputSlot(0).GetTensorInfo();
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003535}
3536
Kevin May7d96b162021-02-03 17:38:41 +00003537void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003538{
3539 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3540
Mike Kelly0d77ae12022-01-07 17:42:27 +00003541 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003542
3543 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3544 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3545 CHECK_VALID_SIZE(outputs.size(), 4);
3546
3547 // Obtain custom options from flexbuffers
3548 auto custom_options = operatorPtr->custom_options;
3549 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3550
3551 // Obtain descriptor information from tf lite
3552 DetectionPostProcessDescriptor desc;
3553 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3554 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3555 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3556 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3557 desc.m_NumClasses = m["num_classes"].AsUInt32();
3558 desc.m_ScaleH = m["h_scale"].AsFloat();
3559 desc.m_ScaleW = m["w_scale"].AsFloat();
3560 desc.m_ScaleX = m["x_scale"].AsFloat();
3561 desc.m_ScaleY = m["y_scale"].AsFloat();
3562
keidav0107d58c72019-02-26 11:57:39 +00003563 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003564 {
keidav0107d58c72019-02-26 11:57:39 +00003565 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003566 }
3567 if (!(m["detections_per_class"].IsNull()))
3568 {
3569 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3570 }
3571
3572 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3573 {
3574 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3575 "must be positive and less than or equal to 1.");
3576 }
3577
Mike Kelly377fb212023-01-10 15:55:28 +00003578 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003579 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003580
James Ward58dec6b2020-09-11 17:32:44 +01003581 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003582 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003583 layerName.c_str());
3584
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003585 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003586
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003587 // The model does not specify the output shapes.
3588 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3589 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
Mike Kelly377fb212023-01-10 15:55:28 +00003590 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3591 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3592 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3593 m_OverriddenOutputShapes.push_back({ 1 });
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003594
keidav011b3e2ea2019-02-21 10:07:37 +00003595 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3596 {
Mike Kelly377fb212023-01-10 15:55:28 +00003597 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverriddenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003598 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3599 }
3600
3601 // Register the input connection slots for the layer, connections are made after all layers have been created
3602 // only the tensors for the inputs are relevant, exclude the const tensors
3603 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3604 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3605
3606 // Register the output connection slots for the layer, connections are made after all layers have been created
3607 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3608 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3609 outputTensorIndexes[1],
3610 outputTensorIndexes[2],
3611 outputTensorIndexes[3]});
3612}
3613
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003614/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003615void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003616{
3617 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3618
3619 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3620 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3621 CHECK_VALID_SIZE(outputs.size(), 1);
3622
3623 if (inputs.size() < 1)
3624 {
3625 throw ParseException("Pack must have at least one input.");
3626 }
3627
3628 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3629 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3630
3631 StackDescriptor desc;
3632 desc.m_Axis = static_cast<uint32_t>(options->axis);
3633 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3634
3635 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00003636 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003637 desc.m_InputShape = inputTensorInfo.GetShape();
3638
James Ward58dec6b2020-09-11 17:32:44 +01003639 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003640 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3641
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003642 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003643
Mike Kelly377fb212023-01-10 15:55:28 +00003644 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003645 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3646
3647 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3648 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3649
3650 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3651 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3652}
3653
Mike Kelly5880b912022-01-28 16:18:54 +00003654void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3655{
3656 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3657
3658 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3659 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3660
3661 if (inputs.size() < 2)
3662 {
3663 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3664 }
3665
3666 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3667 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3668 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3669 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003670 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly5880b912022-01-28 16:18:54 +00003671 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3672
3673 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3674 // Please refer to each operand at
3675 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3676 armnn::LstmInputParams params;
3677
3678 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3679 {
3680 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3681 inputTensorInfo).first;
3682 }
3683
3684 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3685 inputTensorInfo).first;
3686 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3687 inputTensorInfo).first;
3688 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3689 inputTensorInfo).first;
3690
3691 // Recurrent weight tensors of size {n_cell, n_output}
3692 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3693 {
3694 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3695 inputTensorInfo).first;
3696 }
3697
3698 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3699 inputTensorInfo).first;
3700 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3701 inputTensorInfo).first;
3702 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3703 inputTensorInfo).first;
3704
3705 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3706 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3707 {
3708 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3709 inputTensorInfo).first;
3710 }
3711
3712 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3713 {
3714 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3715 inputTensorInfo).first;
3716 }
3717
3718 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3719 {
3720 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3721 inputTensorInfo).first;
3722 }
3723
3724 // Gates bias tensors of size {n_cell}
3725 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3726 {
3727 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3728 inputTensorInfo).first;
3729 }
3730
3731 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3732 inputTensorInfo).first;
3733 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3734 inputTensorInfo).first;
3735 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3736 inputTensorInfo).first;
3737
3738 // Projection weight tensor of size {n_output, n_cell}
3739 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3740 {
3741 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3742 inputTensorInfo).first;
3743 }
3744 // Projection bias tensor of size {n_output}
3745 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3746 {
3747 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3748 inputTensorInfo).first;
3749 }
3750
3751 // These state tensors are defined as variable tensors, and will be modified by this op.
3752 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3753 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3754 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3755 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3756
3757 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3758 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3759 {
3760 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3761 inputTensorInfo).first;
3762 }
3763
3764 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3765 {
3766 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3767 inputTensorInfo).first;
3768 }
3769
3770 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3771 {
3772 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3773 inputTensorInfo).first;
3774 }
3775
3776 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3777 {
3778 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3779 inputTensorInfo).first;
3780 }
3781
3782 // set the layer descriptor
3783 armnn::UnidirectionalSequenceLstmDescriptor desc;
3784 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3785 desc.m_ClippingThresCell = nodeParams->cell_clip;
3786 desc.m_ClippingThresProj = nodeParams->proj_clip;
3787 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3788 || params.m_RecurrentToInputWeights == nullptr
3789 || params.m_InputGateBias == nullptr);
3790 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3791 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3792 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3793 || params.m_ForgetLayerNormWeights != nullptr
3794 || params.m_CellLayerNormWeights != nullptr
3795 || params.m_OutputLayerNormWeights != nullptr);
3796 desc.m_TimeMajor = nodeParams->time_major;
3797
Mike Kellyc0800a32022-06-15 10:57:52 +01003798 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003799 {
3800 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3801 inputTensorInfo).first;
3802 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3803 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3804
3805 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3806 inputTensorInfo).first;
3807 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3808 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3809
3810 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3811 inputTensorInfo).first;
3812 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3813 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3814
3815 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3816 inputTensorInfo).first;
3817 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3818 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3819 }
3820 else
3821 {
3822 float defaultIntermediate = std::pow(2, -12);
3823 desc.m_InputIntermediateScale = defaultIntermediate;
3824 desc.m_ForgetIntermediateScale = defaultIntermediate;
3825 desc.m_CellIntermediateScale = defaultIntermediate;
3826 desc.m_OutputIntermediateScale = defaultIntermediate;
3827 }
3828
Mike Kellyc0800a32022-06-15 10:57:52 +01003829 if (operatorPtr->intermediates.size() > 4)
3830 {
3831 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3832 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003833
Mike Kellyc0800a32022-06-15 10:57:52 +01003834 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3835 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3836 }
Mike Kelly5880b912022-01-28 16:18:54 +00003837 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3838 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3839 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3840
3841 armnn::DataType dataType = inputTensorInfo.GetDataType();
3842 float qScale = inputTensorInfo.GetQuantizationScale();
3843 float qOffset = inputTensorInfo.GetQuantizationOffset();
3844
3845 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3846 if (!desc.m_CifgEnabled)
3847 {
3848 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3849 }
3850 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3851 cellStateInInfo.GetDataType(),
3852 cellStateInInfo.GetQuantizationScale(),
3853 cellStateInInfo.GetQuantizationOffset());
3854 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3855
3856 armnn::LstmInputParamsInfo paramsInfo;
3857 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3858 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3859 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3860 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3861 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3862 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3863 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3864 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3865 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3866
3867 if (!desc.m_CifgEnabled)
3868 {
3869 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3870 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3871 if (params.m_CellToInputWeights != nullptr)
3872 {
3873 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3874 }
3875 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3876 }
3877
3878 if (desc.m_ProjectionEnabled)
3879 {
3880 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3881 if (params.m_ProjectionBias != nullptr)
3882 {
3883 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3884 }
3885 }
3886
3887 if (desc.m_PeepholeEnabled)
3888 {
3889 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3890 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3891 }
3892
3893 if (desc.m_LayerNormEnabled)
3894 {
3895 if(!desc.m_CifgEnabled)
3896 {
3897 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3898 }
3899 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3900 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3901 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3902 }
3903
3904 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3905 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3906 ARMNN_ASSERT(layer != nullptr);
3907
3908 // register the input connection slots for the layer, connections are made after all layers have been created
3909 // only the tensors for the inputs are relevant, exclude the const tensors
3910 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3911 operatorPtr->inputs[18],
3912 operatorPtr->inputs[19]});
3913 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3914 inputTensorIndexes[1],
3915 inputTensorIndexes[2]});
3916
3917 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3918
3919 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3920 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3921 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3922
3923 unsigned int tensorIndex = outputTensorIndexes[0];
3924 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3925 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3926}
3927
Kevin May7d96b162021-02-03 17:38:41 +00003928void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003929{
3930 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3931
Mike Kelly0d77ae12022-01-07 17:42:27 +00003932 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3933 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003934
3935 // This unpackAxis indicates the axis to unpack
3936 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3937
3938 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3939 CHECK_VALID_SIZE(inputs.size(), 1);
3940
Mike Kelly377fb212023-01-10 15:55:28 +00003941 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003942
3943 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3944 {
3945 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003946 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3947 "the number of input dimension {} {}",
3948 unpackAxis,
3949 inputTensorInfo.GetNumDimensions(),
3950 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003951 }
3952
Nina Drozd200e3802019-04-15 09:47:39 +01003953 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3954 // If num is not defined, automatically infer from the length of the dimension axis.
3955 if(unpackNum == 0)
3956 {
3957 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3958 }
3959
3960 // If unpack number cannot be inferred and is still zero, throw ParseException.
3961 if(unpackNum == 0)
3962 {
3963 throw ParseException("Number to unpack must greater than zero.");
3964 }
3965
3966 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3967 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3968
3969 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3970 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3971
3972 // Add current input shape to unpackDimSizes
3973 for (unsigned int i = 0; i < inputDimSize; ++i)
3974 {
3975 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3976 }
3977
3978 if (unpackDimSizes[unpackAxis] != unpackNum)
3979 {
3980 throw ParseException("Number to unpack must be the same as length of the dimension to "
3981 "unpack along.");
3982 }
3983
3984 unpackDimSizes[unpackAxis] /= unpackNum;
3985
3986 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3987 for (unsigned int j = 0; j < unpackNum; ++j)
3988 {
3989 // Set the size of the views.
3990 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3991 {
3992 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3993 }
3994 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3995 }
3996
James Ward58dec6b2020-09-11 17:32:44 +01003997 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003998 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003999 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01004000
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004001 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
4002 unpackDimSizes.data());
4003
Nina Drozd200e3802019-04-15 09:47:39 +01004004 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4005 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4006
Finn Williamsb49ed182021-06-29 15:50:08 +01004007 std::vector<unsigned int> reshapeDims;
4008 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
4009 {
4010 if (axis != unpackAxis)
4011 {
4012 reshapeDims.push_back(splitOutShape[axis]);
4013 }
4014 }
4015
4016 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
4017
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004018 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
4019 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4020 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004021 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01004022 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004023 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01004024 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004025 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
4026
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01004027 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
4028 outputTensorInfo.GetDataType(),
4029 outputTensorInfo.GetQuantizationScale(),
4030 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004031 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
4032
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01004033 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004034
4035 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
4036 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
4037 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4038 }
Nina Drozd200e3802019-04-15 09:47:39 +01004039}
4040
Kevin May7d96b162021-02-03 17:38:41 +00004041void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01004042{
4043 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4044
Mike Kelly0d77ae12022-01-07 17:42:27 +00004045 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4046 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01004047
4048 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4049
Nina Drozd200e3802019-04-15 09:47:39 +01004050 // If number of splits cannot be inferred and is zero, throw ParseException.
4051 if(numSplits == 0)
4052 {
4053 throw ParseException("Number to splits must greater than zero.");
4054 }
4055
Nina Drozd0324f482019-04-08 10:52:10 +01004056 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4057 CHECK_VALID_SIZE(inputs.size(), 2);
4058 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4059 CHECK_VALID_SIZE(outputs.size(), numSplits);
4060
Mike Kelly377fb212023-01-10 15:55:28 +00004061 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4062 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004063 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01004064
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004065 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004066 if (axisBufferPtr == nullptr)
4067 {
4068 throw ParseException(
4069 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4070 CHECK_LOCATION().AsString()));
4071 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004072
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004073 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4074 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4075 int32_t axis = axisData[0];
4076
4077 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4078 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4079 {
4080 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4081 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4082 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4083 throw ParseException(
4084 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4085 axis,
4086 CHECK_LOCATION().AsString()));
4087 }
4088
4089 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01004090
Nina Drozd0324f482019-04-08 10:52:10 +01004091 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004092 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01004093 {
4094 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004095 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4096 inputTensorInfo.GetNumDimensions(),
4097 MaxNumOfTensorDimensions,
4098 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01004099 }
4100
4101 std::vector<unsigned int> splitterDimSizes(inputDimSize);
4102
4103 // Add current input shape to splitterDimSizes
4104 for (unsigned int i = 0; i < inputDimSize; ++i)
4105 {
4106 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
4107 }
4108
4109 if (splitterDimSizes[splitDim] % numSplits != 0)
4110 {
4111 throw ParseException("Number of splits must evenly divide the dimension");
4112 }
4113 splitterDimSizes[splitDim] /= numSplits;
4114
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004115 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01004116 for (unsigned int j = 0; j < numSplits; ++j)
4117 {
4118 // Set the size of the views.
4119 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4120 {
4121 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4122 }
4123 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4124 }
4125
James Ward58dec6b2020-09-11 17:32:44 +01004126 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01004127 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01004128 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01004129
4130 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004131 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01004132
Nina Drozd0324f482019-04-08 10:52:10 +01004133 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4134 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004135 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01004136 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01004137 }
4138
4139 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4140 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4141}
4142
Derek Lambertif0176992020-04-28 13:37:49 +01004143unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
4144{
4145 int numDims = armnn::numeric_cast<int>(numDimsIn);
4146 int v = idx < 0 ? numDims + idx : idx;
4147 ARMNN_ASSERT(v >= 0);
4148 ARMNN_ASSERT(v < numDims);
4149
4150 return static_cast<unsigned int>(v);
4151}
4152
Kevin May7d96b162021-02-03 17:38:41 +00004153void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01004154{
4155 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4156
Mike Kelly0d77ae12022-01-07 17:42:27 +00004157 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4158 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01004159
4160 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4161 CHECK_VALID_SIZE(inputs.size(), 3);
4162
4163 auto& inputTensor = inputs[0];
4164 auto& splitsTensor = inputs[1];
4165 auto& axisTensor = inputs[2];
4166
4167 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
4168 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
4169 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
4170 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
4171
4172 // Inputs
4173 auto inputDimSize = inputTensorInfo.GetNumDimensions();
4174 if (inputDimSize > MaxNumOfTensorDimensions)
4175 {
4176 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004177 fmt::format("The number of dimensions: {} for input tensors of the "
4178 "SplitV op cannot be greater than {} {}",
4179 inputTensorInfo.GetNumDimensions(),
4180 MaxNumOfTensorDimensions,
4181 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01004182 }
4183
4184 // Get split axis
4185 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004186 if (axisBufferPtr == nullptr)
4187 {
4188 throw ParseException(
4189 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4190 CHECK_LOCATION().AsString()));
4191 }
4192
Derek Lambertif0176992020-04-28 13:37:49 +01004193 std::vector<int> axisData(axisTensorInfo.GetNumElements());
4194 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004195 int32_t axis = axisData[0];
4196
4197 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4198 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4199 {
4200 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4201 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4202 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4203 throw ParseException(
4204 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4205 axis,
4206 CHECK_LOCATION().AsString()));
4207 }
4208 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01004209
Derek Lambertif0176992020-04-28 13:37:49 +01004210 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01004211 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01004212 unsigned int numSplits{0};
4213
4214 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01004215 {
4216 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01004217 }
4218 else
4219 {
Ryan OShea86704732020-05-26 11:41:04 +01004220 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01004221 }
4222
4223 if (numSplits <=0)
4224 {
4225 throw ParseException("SplitV has invalid number of splits");
4226 }
4227
Jan Eilersc0761e92020-06-29 16:48:44 +01004228 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01004229 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01004230 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01004231
Jan Eilersc0761e92020-06-29 16:48:44 +01004232 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01004233 int numInferred{0};
4234 unsigned int inferIdx{0};
4235 int splitSum{0};
4236 for (auto split : splitsData)
4237 {
4238 if (split < 0)
4239 {
4240 numInferred++;
4241 inferIdx = idx;
4242 }
4243 else
4244 {
4245 splitSum += split;
4246 }
4247 idx++;
4248 }
4249 // Check for inferred Axis
4250 if (numInferred == 0)
4251 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004252 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01004253 {
4254 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
4255 }
4256 }
4257 else if (numInferred == 1)
4258 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004259 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01004260 }
4261 else
4262 {
4263 throw ParseException("Cannot infer split size for more than one split");
4264 }
4265
Derek Lambertif0176992020-04-28 13:37:49 +01004266 //Ouput size validation
4267 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4268 CHECK_VALID_SIZE(outputs.size(), numSplits);
4269
4270 // Setup Armnn descriptor
4271 SplitterDescriptor splitDesc(numSplits, inputDimSize);
4272 unsigned int accumSplit = 0;
4273 for (unsigned int j = 0; j < numSplits; ++j)
4274 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004275 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01004276
4277 // Set the size of the views.
4278 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
4279 {
4280 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
4281 if (dimIdx == splitDim)
4282 {
4283 dimSize = splitSize;
4284 }
4285 splitDesc.SetViewSize(j, dimIdx, dimSize);
4286 }
4287
4288 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4289 accumSplit += splitSize;
4290 }
4291
James Ward58dec6b2020-09-11 17:32:44 +01004292 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01004293 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01004294 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01004295
4296 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4297 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4298
4299 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4300 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004301 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01004302 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4303 }
4304
4305 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4306 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4307}
4308
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004309void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
4310{
4311 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
4312}
4313
Kevin May7d96b162021-02-03 17:38:41 +00004314void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09004315{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004316 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
4317}
4318
4319void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
4320{
Inki Daed4619e22020-09-10 15:33:54 +09004321 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4322 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4323 CHECK_VALID_SIZE(inputs.size(), 2);
4324
4325 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4326 CHECK_VALID_SIZE(outputs.size(), 1);
4327
Mike Kelly377fb212023-01-10 15:55:28 +00004328 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4329 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Inki Daed4619e22020-09-10 15:33:54 +09004330 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004331 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004332
4333 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01004334 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
4335 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
4336 {
4337 throw ParseException(
4338 fmt::format(
4339 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4340 CHECK_LOCATION().AsString()));
4341 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004342
4343 // Get const axis value from model and set it to descriptor.
4344 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4345 if (axisBufferPtr == nullptr)
4346 {
4347 throw ParseException(
4348 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4349 CHECK_LOCATION().AsString()));
4350 }
4351
4352 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4353 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4354 int32_t axis = axisData.front();
4355
4356 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4357 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4358 {
4359 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4360 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4361 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4362 throw ParseException(
4363 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4364 axis,
4365 CHECK_LOCATION().AsString()));
4366 }
4367
4368 ArgMinMaxDescriptor desc;
4369 desc.m_Axis = axis;
4370 desc.m_Function = argMinMaxFunction;
4371
4372 // Register a ArgMin/ArgMax layer.
4373 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
4374 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4375 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4376 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004377 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Inki Daed4619e22020-09-10 15:33:54 +09004378 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4379
4380 // Register input tensor to the layer.
4381 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4382 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4383
4384 // Register output tensor to the layer.
4385 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4386 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4387}
4388
Kevin May7d96b162021-02-03 17:38:41 +00004389void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004390{
4391 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4392
Kevin May7d96b162021-02-03 17:38:41 +00004393 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004394 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00004395 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004396 CHECK_VALID_SIZE(outputs.size(), 1);
4397
Mike Kelly377fb212023-01-10 15:55:28 +00004398 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4399 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4400 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Sadik Armagan26868492021-01-22 14:25:31 +00004401
4402 armnn::GatherDescriptor gatherDescriptor;
4403
Mike Kelly0d77ae12022-01-07 17:42:27 +00004404 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4405 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004406 auto axis = options->axis;
4407
Mike Kelly377fb212023-01-10 15:55:28 +00004408 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4409
Sadik Armagan26868492021-01-22 14:25:31 +00004410 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4411 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4412 auto outputDimensions = outputTensorInfo.GetNumDimensions();
4413 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4414 {
4415 throw ParseException(
4416 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4417 axis,
4418 inputDimensions, inputDimensions,
4419 CHECK_LOCATION().AsString()));
4420 }
4421 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4422 {
4423 throw ParseException(
4424 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4425 outputDimensions,
4426 inputDimensions, indicesDimensions,
4427 CHECK_LOCATION().AsString()));
4428 }
4429
4430 gatherDescriptor.m_Axis = axis;
4431
Sadik Armagan26868492021-01-22 14:25:31 +00004432 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4433 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004434 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Sadik Armagan26868492021-01-22 14:25:31 +00004435 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4436
4437 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4438 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4439
4440 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4441 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4442}
4443
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004444void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4445{
4446 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4447
4448 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4449 CHECK_VALID_SIZE(inputs.size(), 2);
4450 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4451 CHECK_VALID_SIZE(outputs.size(), 1);
4452
Mike Kelly377fb212023-01-10 15:55:28 +00004453 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4454 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004455
4456 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4457 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4458 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004459 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004460 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4461
4462 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4463 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4464
4465 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4466 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4467}
4468
Kevin May7d96b162021-02-03 17:38:41 +00004469void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004470{
4471 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4472
Kevin May7d96b162021-02-03 17:38:41 +00004473 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004474 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004475 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004476 CHECK_VALID_SIZE(outputs.size(), 1);
4477
4478 armnn::DepthToSpaceDescriptor descriptor;
4479
Mike Kelly0d77ae12022-01-07 17:42:27 +00004480 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4481 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004482 auto blockSize = options->block_size;
4483 if (blockSize < 2)
4484 {
4485 throw ParseException(
4486 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4487 blockSize,
4488 CHECK_LOCATION().AsString()));
4489 }
4490 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4491
4492 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4493 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4494 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004495 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan26868492021-01-22 14:25:31 +00004496 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4497
4498 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4499 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4500
4501 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4502 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4503}
4504
Kevin May7d96b162021-02-03 17:38:41 +00004505void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004506{
Sadik Armagana2747482021-02-09 10:28:54 +00004507 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4508}
4509
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004510void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4511{
4512 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4513}
4514
Sadik Armagana2747482021-02-09 10:28:54 +00004515void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4516{
4517 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4518}
4519
4520void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4521{
4522 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4523}
4524
4525void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4526{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004527 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4528
Mike Kelly0d77ae12022-01-07 17:42:27 +00004529 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4530 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004531
4532 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4533 CHECK_VALID_SIZE(inputs.size(), 2);
4534
4535 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4536 CHECK_VALID_SIZE(outputs.size(), 1);
4537
Sadik Armagana2747482021-02-09 10:28:54 +00004538 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004539
Mike Kelly377fb212023-01-10 15:55:28 +00004540 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4541 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004542
4543 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004544 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4545 // Get const axis value from model and set it to descriptor.
4546 if (axisBufferPtr != nullptr)
4547 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004548 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4549 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4550
4551 // Convert the axis to unsigned int and remove duplicates.
4552 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4553 std::set<unsigned int> uniqueAxis;
4554 std::transform(axisData.begin(),
4555 axisData.end(),
4556 std::inserter(uniqueAxis, uniqueAxis.begin()),
4557 [rank](int i)->unsigned int{
4558 return static_cast<uint32_t>(((i + rank) % rank)); });
4559 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004560 }
Sadik Armagana2747482021-02-09 10:28:54 +00004561 else
4562 {
4563 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4564 {
4565 desc.m_vAxis.push_back(i);
4566 }
4567 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004568
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004569 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004570 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004571
4572 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004573 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004574
Mike Kelly377fb212023-01-10 15:55:28 +00004575 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004576 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4577
4578 // Register input tensor to the layer.
4579 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4580 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4581
4582 // Register output tensor to the layer.
4583 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4584 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4585}
4586
Mike Kelly31dce2b2021-09-01 21:22:37 +01004587void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4588{
4589 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4590
4591 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4592 CHECK_VALID_SIZE(inputs.size(), 1);
4593
4594 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4595 CHECK_VALID_SIZE(outputs.size(), 1);
4596
4597 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4598 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4599
Mike Kelly377fb212023-01-10 15:55:28 +00004600 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly31dce2b2021-09-01 21:22:37 +01004601
4602 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4603 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4604
4605 armnn::NormalizationDescriptor descriptor;
4606 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4607 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4608 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4609 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4610 descriptor.m_K = options->bias;
4611 descriptor.m_Alpha = options->alpha;
4612 descriptor.m_Beta = options->beta;
4613
4614 // ArmNN expects normSize to be the full size of the normalization
4615 // window rather than the radius as in TfLite.
4616 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4617
4618 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4619 ARMNN_ASSERT(layer != nullptr);
4620
Mike Kelly377fb212023-01-10 15:55:28 +00004621 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Mike Kelly31dce2b2021-09-01 21:22:37 +01004622 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4623
4624 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4625 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4626
4627 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4628 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4629}
4630
Teresa Charlin28aa6692022-07-12 11:18:44 +01004631void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4632{
4633 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4634}
4635
Teresa Charlin93f0ad02023-03-23 15:28:02 +00004636void TfLiteParserImpl::ParseCeil(size_t subgraphIndex, size_t operatorIndex)
4637{
4638 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Ceil);
4639}
4640
Teresa Charlin28aa6692022-07-12 11:18:44 +01004641void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4642{
4643 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4644}
4645
4646void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
4647{
4648 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
4649}
4650
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004651void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4652{
4653 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4654}
4655
4656void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4657{
4658 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4659}
4660
John Mcloughlin0ec00872023-05-15 17:03:49 +01004661void TfLiteParserImpl::ParsePower(size_t subgraphIndex, size_t operatorIndex)
4662{
4663 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4664
4665 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4666 CHECK_VALID_SIZE(inputs.size(), 2);
4667
4668 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4669 CHECK_VALID_SIZE(outputs.size(), 1);
4670
4671 auto layerName = fmt::format("Power:{}:{}", subgraphIndex, operatorIndex);
4672
4673 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4674 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4675 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
4676
4677 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
4678 ARMNN_ASSERT(layer != nullptr);
4679
4680 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4681 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
4682 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4683
4684 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4685 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4686
4687 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4688 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4689}
4690
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004691void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4692{
4693 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4694}
4695
Teresa Charlin28aa6692022-07-12 11:18:44 +01004696void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
4697{
4698 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
4699}
4700
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004701void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4702{
4703 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4704}
4705
John Mcloughlin0ec00872023-05-15 17:03:49 +01004706void TfLiteParserImpl::ParseSquaredDifference(size_t subgraphIndex, size_t operatorIndex)
4707{
4708 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4709
4710 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4711 CHECK_VALID_SIZE(inputs.size(), 2);
4712
4713 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4714 CHECK_VALID_SIZE(outputs.size(), 1);
4715
4716 auto layerName = fmt::format("SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
4717
4718 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4719 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4720 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
4721
4722 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
4723 ARMNN_ASSERT(layer != nullptr);
4724
4725 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4726 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
4727 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4728
4729 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4730 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4731
4732 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4733 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4734}
4735
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004736void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4737{
4738 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4739
4740 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4741 CHECK_VALID_SIZE(inputs.size(), 1);
4742
4743 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4744 CHECK_VALID_SIZE(outputs.size(), 1);
4745
4746 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4747 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4748
4749 ElementwiseUnaryDescriptor desc;
4750 desc.m_Operation = unaryOperation;
4751 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4752 ARMNN_ASSERT(layer != nullptr);
4753
Mike Kelly377fb212023-01-10 15:55:28 +00004754 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004755 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4756
4757 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4758 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4759
4760 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4761 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4762}
4763
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004764void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4765{
4766 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4767}
4768
4769void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4770{
4771 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4772}
4773
4774void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4775{
4776 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4777}
4778
4779void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4780{
4781 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4782}
4783
4784void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4785{
4786 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4787}
4788
4789void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4790{
4791 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4792}
4793
4794void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4795 ComparisonOperation comparisonOperation)
4796{
4797 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4798
4799 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4800 CHECK_VALID_SIZE(inputs.size(), 2);
4801
4802 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4803 CHECK_VALID_SIZE(outputs.size(), 1);
4804
4805 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4806 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4807
Mike Kelly377fb212023-01-10 15:55:28 +00004808 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4809 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004810 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4811
4812 ComparisonDescriptor desc;
4813 desc.m_Operation = comparisonOperation;
4814 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4815 ARMNN_ASSERT(layer != nullptr);
4816
Mike Kelly377fb212023-01-10 15:55:28 +00004817 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004818 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4819
4820 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4821 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4822
4823 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4824 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4825}
4826
Mike Kelly04d82292023-01-19 18:29:40 +00004827armnn::IConnectableLayer* TfLiteParserImpl::AddReshapeLayer(armnn::IConnectableLayer* layer,
4828 unsigned int outputSlot,
4829 std::string reshapeLayerName,
4830 armnn::TensorInfo outputShape)
4831{
4832 ReshapeDescriptor desc;
4833 desc.m_TargetShape = outputShape.GetShape();
4834
4835 IConnectableLayer* reshapeLayer =
4836 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
4837
4838 auto & prevOutputSlot = layer->GetOutputSlot(outputSlot);
4839 prevOutputSlot.Connect(reshapeLayer->GetInputSlot(0));
4840 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputShape);
4841 return reshapeLayer;
4842}
4843
Kevin May7d96b162021-02-03 17:38:41 +00004844armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4845 unsigned int outputSlot,
4846 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004847{
4848 ActivationDescriptor activationDesc;
4849 std::string layerName = prevLayer->GetName();
4850
4851 switch(activationType)
4852 {
4853 case tflite::ActivationFunctionType_NONE:
4854 {
4855 // this is a no-op: return previous layer
4856 return prevLayer;
4857 }
4858 case tflite::ActivationFunctionType_RELU:
4859 {
4860 activationDesc.m_Function = ActivationFunction::ReLu;
4861 layerName += ":RELU";
4862 break;
4863 }
4864 case tflite::ActivationFunctionType_RELU6:
4865 {
4866 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4867 activationDesc.m_A = 6.0f;
4868 activationDesc.m_B = 0.0f;
4869 layerName += ":RELU6";
4870 break;
4871 }
4872 case tflite::ActivationFunctionType_TANH:
4873 {
4874 activationDesc.m_Function = ActivationFunction::TanH;
4875 activationDesc.m_A = 1.0f;
4876 activationDesc.m_B = 1.0f;
4877 layerName += ":TANH";
4878 break;
4879 }
4880
4881 // I only put these here as a reminder what others we could support
4882 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4883 case tflite::ActivationFunctionType_SIGN_BIT:
4884 default:
4885 {
4886 throw ParseException(
Mike Kelly377fb212023-01-10 15:55:28 +00004887 fmt::format("TfLite parser doesn't support fused activation: "
James Ward58dec6b2020-09-11 17:32:44 +01004888 "{}/{} {} ",
4889 activationType,
4890 tflite::EnumNameActivationFunctionType(activationType),
4891 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004892
4893 }
4894 }
4895
4896 IConnectableLayer* activationLayer =
4897 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4898
4899 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4900 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4901 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4902 return activationLayer;
4903}
4904
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004905armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4906 unsigned int outputSlot)
4907{
Teresa Charlin725728e2022-05-05 13:33:33 +01004908
4909 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4910 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4911
4912 if (dataType == DataType::Signed32)
4913 {
4914 return prevLayer;
4915 }
4916
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004917 std::string layerName = prevLayer->GetName();
4918 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4919
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004920 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4921 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004922
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004923 return floorLayer;
4924}
4925
Mike Kelly0d77ae12022-01-07 17:42:27 +00004926TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004927{
4928 if (fileName == nullptr)
4929 {
James Ward58dec6b2020-09-11 17:32:44 +01004930 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004931 CHECK_LOCATION().AsString()));
4932 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004933 std::error_code errorCode;
4934 fs::path pathToFile(fileName);
4935 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004936 {
James Ward58dec6b2020-09-11 17:32:44 +01004937 //fmt::format() could not be used here (format error)
4938 std::stringstream msg;
4939 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4940 << " " << CHECK_LOCATION().AsString();
James Ward58dec6b2020-09-11 17:32:44 +01004941 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004942 }
Colm Donelan0dfb2652023-06-22 10:19:17 +01004943 if (!fs::is_regular_file(pathToFile))
4944 {
4945 // Exclude non regular files.
4946 throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
4947 pathToFile.c_str()));
4948 }
4949
telsoa01c577f2c2018-08-31 09:22:23 +01004950 std::ifstream file(fileName, std::ios::binary);
4951 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4952 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4953 fileContent.size());
4954}
4955
Mike Kelly0d77ae12022-01-07 17:42:27 +00004956TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004957{
4958 if (binaryContent == nullptr)
4959 {
James Ward58dec6b2020-09-11 17:32:44 +01004960 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004961 CHECK_LOCATION().AsString()));
4962 }
4963 flatbuffers::Verifier verifier(binaryContent, len);
4964 if (verifier.VerifyBuffer<tflite::Model>() == false)
4965 {
4966 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004967 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4968 "flatbuffers format. size:{} {}",
4969 len,
4970 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004971 }
4972 return tflite::UnPackModel(binaryContent);
4973}
4974
Mike Kelly0d77ae12022-01-07 17:42:27 +00004975TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004976 size_t subgraphIndex,
4977 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004978{
4979 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4980
Mike Kelly0d77ae12022-01-07 17:42:27 +00004981 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4982 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004983
4984 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004985 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004986 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004987 {
mathad01c21025d2021-04-26 10:09:37 +01004988 // If the input location is -1 then assume input is turned off.
4989 if (operatorPtr->inputs[i] == -1)
4990 {
4991 continue;
4992 }
4993 else
4994 {
4995 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4996 result.push_back(subgraphPtr->tensors[inputId].get());
4997 }
telsoa01c577f2c2018-08-31 09:22:23 +01004998 }
4999 return result;
5000}
5001
Mike Kelly0d77ae12022-01-07 17:42:27 +00005002TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005003 size_t subgraphIndex,
5004 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005005{
5006 CHECK_MODEL(model, subgraphIndex, operatorIndex);
5007
Mike Kelly0d77ae12022-01-07 17:42:27 +00005008 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5009 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005010
5011 size_t outputCount = operatorPtr->outputs.size();
5012 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005013 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005014 {
5015 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5016 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01005017 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01005018 }
5019 return result;
5020}
5021
Mike Kelly0d77ae12022-01-07 17:42:27 +00005022TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005023 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005024{
5025 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005026 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005027
Derek Lambertiff05cc52019-04-26 13:05:17 +01005028 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01005029 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005030 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005031 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005032 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01005033 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01005034 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01005035 }
5036 return result;
5037}
5038
Mike Kelly0d77ae12022-01-07 17:42:27 +00005039TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005040 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005041{
5042 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005043 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005044
Derek Lambertiff05cc52019-04-26 13:05:17 +01005045 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01005046 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005047 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005048 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005049 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5050 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01005051 }
5052 return result;
5053}
5054
Kevin May7d96b162021-02-03 17:38:41 +00005055std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
5056 size_t subgraphIndex,
5057 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005058{
5059 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005060 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5061 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005062 return operatorPtr->inputs;
5063}
5064
Kevin May7d96b162021-02-03 17:38:41 +00005065std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
5066 size_t subgraphIndex,
5067 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005068{
5069 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005070 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5071 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005072 return operatorPtr->outputs;
5073}
5074
Kevin May7d96b162021-02-03 17:38:41 +00005075void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
5076 size_t operatorIndex,
5077 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00005078 const std::vector<unsigned int>& tensorIndexes,
5079 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005080{
5081 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01005082 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01005083
Finn Williamsd4fa5452021-03-01 12:31:41 +00005084 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01005085 {
5086 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005087 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
5088 " for subgraph:{} operator index:{} {}",
5089 tensorIndexes.size(),
5090 layer->GetNumInputSlots(),
5091 subgraphIndex,
5092 operatorIndex,
5093 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005094 }
5095
Finn Williamsd4fa5452021-03-01 12:31:41 +00005096 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01005097 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00005098 unsigned int tensorIndex = tensorIndexes[index];
5099 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01005100 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5101 }
5102}
5103
Kevin May7d96b162021-02-03 17:38:41 +00005104void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
5105 size_t operatorIndex,
5106 IConnectableLayer* layer,
5107 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01005108{
5109 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01005110 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01005111 if (tensorIndexes.size() != layer->GetNumOutputSlots())
5112 {
5113 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005114 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
5115 " for subgraph:{} operator index:{} {}",
5116 tensorIndexes.size(),
5117 layer->GetNumOutputSlots(),
5118 subgraphIndex,
5119 operatorIndex,
5120 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005121 }
5122
5123 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
5124 {
5125 unsigned int tensorIndex = tensorIndexes[slotIndex];
5126 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
5127 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5128 }
5129}
5130
Mike Kelly377fb212023-01-10 15:55:28 +00005131void TfLiteParserImpl::SetupInputLayerTensorInfos(size_t subgraphIndex)
5132{
5133 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5134
5135 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5136 for (auto const& tensorIdAndPtr : inputs)
5137 {
5138 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5139 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5140 }
5141}
5142
Kevin May7d96b162021-02-03 17:38:41 +00005143void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005144{
5145 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5146
5147 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005148 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005149 {
5150 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5151 IConnectableLayer* layer =
5152 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5153
5154 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5155 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5156
5157 RegisterOutputSlots(subgraphIndex,
5158 VIRTUAL_OPERATOR_ID,
5159 layer,
5160 { static_cast<uint32_t>(tensorIdAndPtr.first) });
5161 }
5162}
5163
Kevin May7d96b162021-02-03 17:38:41 +00005164void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005165{
5166 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5167
5168 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005169 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005170 {
5171 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5172 IConnectableLayer* layer =
5173 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5174
5175 RegisterInputSlots(subgraphIndex,
5176 VIRTUAL_OPERATOR_ID,
5177 layer,
5178 { static_cast<uint32_t>(tensorIdAndPtr.first) });
5179 }
5180}
5181
Mike Kelly377fb212023-01-10 15:55:28 +00005182void TfLiteParserImpl::SetupConstantLayerTensorInfos(size_t subgraph)
5183{
5184 CHECK_SUBGRAPH(m_Model, subgraph);
5185
5186 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5187 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5188 {
5189 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5190 {
5191 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5192 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5193 {
5194 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5195
5196 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5197
5198 m_TensorInfos.insert({tensorIndex, tensorInfo});
5199 }
5200 }
5201 }
5202}
5203
Mike Kelly5880b912022-01-28 16:18:54 +00005204void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005205{
Mike Kelly5880b912022-01-28 16:18:54 +00005206 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005207
Mike Kelly5880b912022-01-28 16:18:54 +00005208 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005209 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5210 {
5211 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5212 {
5213 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5214 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5215 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005216 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005217
Mike Kelly5880b912022-01-28 16:18:54 +00005218 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01005219 {
5220 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00005221 armnn::DataType dataType = tensorInfo.GetDataType();
5222
5223 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5224 != m_ConstantsToDequantize.end())
5225 {
5226 dataType = DataType::Float32;
5227 }
5228 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5229
5230 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5231 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5232
5233 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
5234 RegisterOutputSlots(subgraphIndex,
5235 VIRTUAL_OPERATOR_ID,
5236 layer,
5237 { tensorIndex });
5238 }
5239 else if (ShouldConstantTensorBeCreated(tensorIndex))
5240 {
5241 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5242 armnn::DataType dataType = tensorInfo.GetDataType();
5243
5244 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5245 != m_ConstantsToDequantize.end())
5246 {
5247 dataType = DataType::Float32;
5248 }
5249 // Make sure isConstant flag is set.
5250 tensorInfo.SetConstant();
5251 tensorInfo.SetDataType(dataType);
5252
5253 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005254
Matthew Sloyan81beae32021-07-13 19:46:11 +01005255 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005256 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005257
Matthew Sloyan81beae32021-07-13 19:46:11 +01005258 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5259 RegisterOutputSlots(subgraphIndex,
5260 VIRTUAL_OPERATOR_ID,
5261 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00005262 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01005263 }
5264 else
5265 {
5266 throw ParseException(
5267 fmt::format("Invalid Tensor: Tensor should be constant. {}",
5268 CHECK_LOCATION().AsString()));
5269 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005270 }
5271 }
5272 }
5273}
5274
telsoa01c577f2c2018-08-31 09:22:23 +01005275// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00005276TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005277{
5278 CHECK_BUFFER(model, bufferIndex);
5279 return model->buffers[bufferIndex].get();
5280}
5281
Matteo Martincigh747ef822018-12-18 09:26:39 +00005282template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00005283std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5284TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
5285 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00005286 armnn::TensorInfo& tensorInfo,
5287 armnn::Optional<armnn::PermutationVector&> permutationVector)
5288{
Matthew Sloyan81beae32021-07-13 19:46:11 +01005289 // Make sure isConstant flag is set.
5290 tensorInfo.SetConstant();
5291
Matteo Martincigh747ef822018-12-18 09:26:39 +00005292 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5293 tensorPtr,
5294 tensorInfo,
5295 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00005296 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00005297 return std::make_pair(constData.first, std::move(storage));
5298}
5299
Mike Kelly5880b912022-01-28 16:18:54 +00005300bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
5301{
5302 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
5303 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5304 != m_ConstantsToBeCreated.end());
5305}
5306
Finn Williamsd4fa5452021-03-01 12:31:41 +00005307bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
5308{
5309 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01005310 bool isConst = true;
5311
5312 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
5313 if (buffer->data.size() == 0)
5314 {
5315 isConst = false;
5316 }
5317
5318 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00005319}
5320
Kevin May7d96b162021-02-03 17:38:41 +00005321std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00005322TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
5323 armnn::TensorInfo& tensorInfo,
5324 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01005325{
5326 CHECK_TENSOR_PTR(tensorPtr);
5327 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5328 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5329
Matthew Sloyan81beae32021-07-13 19:46:11 +01005330 // Make sure isConstant flag is set.
5331 tensorInfo.SetConstant();
5332
telsoa01c577f2c2018-08-31 09:22:23 +01005333 switch (tensorInfo.GetDataType())
5334 {
5335 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005336 return CreateConstTensorAndStoreData<float>(bufferPtr,
5337 tensorPtr,
5338 tensorInfo,
5339 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00005340 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005341 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5342 tensorPtr,
5343 tensorInfo,
5344 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00005345 case armnn::DataType::QSymmS8:
5346 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5347 tensorPtr,
5348 tensorInfo,
5349 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00005350 case armnn::DataType::QAsymmS8:
5351 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5352 tensorPtr,
5353 tensorInfo,
5354 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005355 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005356 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5357 tensorPtr,
5358 tensorInfo,
5359 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005360 default:
5361 {
5362 std::stringstream errString;
5363 errString << "Unexpected datatype when creating const tensor: "
5364 << armnn::GetDataTypeName(tensorInfo.GetDataType())
5365 << " shape:" << tensorInfo.GetShape()
5366 << CHECK_LOCATION().AsString();
5367 throw ParseException(errString.str());
5368 }
5369 }
5370}
5371
Finn Williamsd4fa5452021-03-01 12:31:41 +00005372armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5373 armnn::TensorInfo& tensorInfo)
5374{
5375 CHECK_TENSOR_PTR(tensorPtr);
5376 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5377 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5378
Matthew Sloyan81beae32021-07-13 19:46:11 +01005379 // Make sure isConstant flag is set.
5380 tensorInfo.SetConstant();
5381
Finn Williamsd4fa5452021-03-01 12:31:41 +00005382 return ConstTensor(tensorInfo, bufferPtr->data.data());
5383}
5384
Mike Kelly5880b912022-01-28 16:18:54 +00005385std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5386TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5387 armnn::TensorInfo& tensorInfo,
5388 armnn::DataType inputDataType)
5389{
5390 CHECK_TENSOR_PTR(tensorPtr);
5391 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5392 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5393
5394 // Make sure isConstant flag is set.
5395 tensorInfo.SetConstant();
5396
Mike Kelly0506ef02023-01-03 16:29:44 +00005397 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
Mike Kelly5880b912022-01-28 16:18:54 +00005398 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005399 try
5400 {
5401 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5402 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5403 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
5404 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005405 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005406 {
5407 throw ParseException(
5408 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5409 GetDataTypeName(DataType::Float32),
5410 GetDataTypeName(tensorInfo.GetDataType()),
5411 CHECK_LOCATION().AsString()));
5412 }
Mike Kelly5880b912022-01-28 16:18:54 +00005413 }
5414 else
5415 {
5416 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5417 }
5418}
5419
5420std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5421TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
5422{
5423 CHECK_TENSOR_PTR(tensorPtr);
5424 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5425 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5426 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5427
5428 // Make sure isConstant flag is set.
5429 tensorInfo.SetConstant();
5430
5431 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5432 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005433 try
5434 {
5435 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5436 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5437 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
5438 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005439 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005440 {
5441 throw ParseException(
5442 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5443 GetDataTypeName(DataType::Float32),
5444 GetDataTypeName(tensorInfo.GetDataType()),
5445 CHECK_LOCATION().AsString()));
5446 }
Mike Kelly5880b912022-01-28 16:18:54 +00005447 }
5448 else
5449 {
5450 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5451 }
5452}
5453
Kevin May7d96b162021-02-03 17:38:41 +00005454BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
5455 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005456{
5457 CHECK_SUBGRAPH(m_Model, subgraphId);
5458 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005459 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005460 {
5461 if (input.second->name == name)
5462 {
5463 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00005464 auto inputTensorInfo = ToTensorInfo(input.second);
5465 // Input tensors are always treated as constant tensors during network execution.
5466 inputTensorInfo.SetConstant(true);
5467 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01005468 }
5469 }
5470
5471 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005472 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005473 {
5474 bindings << "'" << input.second->name << "' ";
5475 }
5476
5477 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005478 fmt::format("No input binding found for subgraph:{} and name:{}. "
5479 "Possible inputs are: [{}] {}",
5480 subgraphId,
5481 name,
5482 bindings.str(),
5483 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005484}
5485
Kevin May7d96b162021-02-03 17:38:41 +00005486BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
5487 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005488{
5489 CHECK_SUBGRAPH(m_Model, subgraphId);
5490 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005491 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005492 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005493 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01005494 if (output.second->name == name)
5495 {
5496 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Mike Kelly377fb212023-01-10 15:55:28 +00005497 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5498 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005499 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01005500 }
5501 }
5502
5503 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005504 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005505 {
5506 bindings << "'" << output.second->name << "' ";
5507 }
5508
5509 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005510 fmt::format("No output binding found for subgraph:{} and name:{}. "
5511 "Possible outputs are: [{}] {}",
5512 subgraphId,
5513 name,
5514 bindings.str(),
5515 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005516}
5517
Kevin May7d96b162021-02-03 17:38:41 +00005518size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01005519{
5520 return m_Model->subgraphs.size();
5521}
5522
Kevin May7d96b162021-02-03 17:38:41 +00005523std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005524{
5525 CHECK_SUBGRAPH(m_Model, subgraphId);
5526 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5527 std::vector<std::string> result;
5528 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005529 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005530 {
5531 result.push_back(input.second->name);
5532 }
5533 return result;
5534}
5535
Kevin May7d96b162021-02-03 17:38:41 +00005536std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005537{
5538 CHECK_SUBGRAPH(m_Model, subgraphId);
5539 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5540 std::vector<std::string> result;
5541 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005542 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005543 {
5544 result.push_back(output.second->name);
5545 }
5546 return result;
5547}
5548
Matthew Sloyanac001ee2021-02-03 10:43:04 +00005549const std::string TfLiteParserImpl::GetVersion()
5550{
5551 return TFLITE_PARSER_VERSION;
5552}
5553
Mike Kelly0d77ae12022-01-07 17:42:27 +00005554TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005555: m_FloatData(std::move(data))
5556, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005557, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005558, m_Int32Data(nullptr)
5559{
5560}
5561
Mike Kelly0d77ae12022-01-07 17:42:27 +00005562TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005563: m_FloatData(nullptr)
5564, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00005565, m_Int8Data(nullptr)
5566, m_Int32Data(nullptr)
5567{
5568}
5569
Mike Kelly0d77ae12022-01-07 17:42:27 +00005570TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00005571: m_FloatData(nullptr)
5572, m_Uint8Data(nullptr)
5573, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01005574, m_Int32Data(nullptr)
5575{
5576}
5577
Mike Kelly0d77ae12022-01-07 17:42:27 +00005578TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005579: m_FloatData(nullptr)
5580, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005581, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005582, m_Int32Data(std::move(data))
5583{
5584}
5585
5586} // armnnTfLiteParser