blob: 049d6049a700e84ebcd5de73c5367ef7324b49e6 [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>
telsoa01c577f2c2018-08-31 09:22:23 +010021
22// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000023#include <armnnUtils/Permute.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010024#include <armnnUtils/Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000025
Sadik Armagan479045b2018-10-01 11:51:37 +010026#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010027#include <VerificationHelpers.hpp>
28
29// The generated code based on the Tf Lite schema:
30#include <schema_generated.h>
31
Matteo Martincighe011d202019-11-28 11:35:47 +000032#include <flatbuffers/flexbuffers.h>
33
James Ward58dec6b2020-09-11 17:32:44 +010034#include <fmt/format.h>
telsoa01c577f2c2018-08-31 09:22:23 +010035
telsoa01c577f2c2018-08-31 09:22:23 +010036#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000037#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010038#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010039#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000040
41#define ARMNN_THROW_PARSE_EXCEPTION(msg) \
42 { \
43 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
44 << ": " \
45 << CHECK_LOCATION().AsString()).str()); \
46 }
telsoa01c577f2c2018-08-31 09:22:23 +010047
48using namespace armnn;
49using armnn::CheckLocation;
50namespace armnnTfLiteParser
51{
Kevin May7d96b162021-02-03 17:38:41 +000052
53ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
54 pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
55
56ITfLiteParser::~ITfLiteParser() = default;
57
58ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
59{
60 return new ITfLiteParser(options);
61}
62
63ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
64{
65 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
66}
67
68void ITfLiteParser::Destroy(ITfLiteParser* parser)
69{
70 delete parser;
71}
72
73armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
74{
75 return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
76}
77
Mike Kelly0d77ae12022-01-07 17:42:27 +000078armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
Kevin May7d96b162021-02-03 17:38:41 +000079{
80 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
81}
82
83BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
84 const std::string& name) const
85{
86 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
87}
88
89BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
90 const std::string& name) const
91{
92 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
93}
94
95size_t ITfLiteParser::GetSubgraphCount() const
96{
97 return pTfLiteParserImpl->GetSubgraphCount();
98}
99
100std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
101{
102 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
103}
104
105std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
106{
107 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
108}
109
telsoa01c577f2c2018-08-31 09:22:23 +0100110namespace
111{
jimfly01c25411c2018-11-14 17:47:22 +0000112
telsoa01c577f2c2018-08-31 09:22:23 +0100113const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
114
Mike Kelly0d77ae12022-01-07 17:42:27 +0000115void CheckSubgraph(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100116 size_t subgraphIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000117 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100118{
119 if (model.get() == nullptr)
120 {
121 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100122 fmt::format("{} was called with invalid (null) model. "
123 "Possible reason is that the model is not yet loaded and Unpack(ed). "
124 "subgraph:{} at {}",
125 location.m_Function,
126 subgraphIndex,
127 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100128 }
129 else if (subgraphIndex >= model->subgraphs.size())
130 {
131 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100132 fmt::format("{} was called with an invalid subgraph index. "
133 "subgraph:{} at {}",
134 location.m_Function,
135 subgraphIndex,
136 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100137 }
138}
139
140#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
141 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
142
Mike Kelly0d77ae12022-01-07 17:42:27 +0000143void CheckModel(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100144 size_t subgraphIndex,
145 size_t operatorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000146 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100147{
148 if (model.get() == nullptr)
149 {
150 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100151 fmt::format("{} was called with invalid (null) model. "
152 "Possible reason is that the model is not yet loaded and Unpack(ed). "
153 "subgraph:{} operator:{} at {}",
154 location.m_Function,
155 subgraphIndex,
156 operatorIndex,
157 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100158 }
159 else if (subgraphIndex >= model->subgraphs.size())
160 {
161 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100162 fmt::format("{} was called with an invalid subgraph index. "
163 "subgraph:{} operator:{} at {}",
164 location.m_Function,
165 subgraphIndex,
166 operatorIndex,
167 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100168 }
169 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170 operatorIndex != VIRTUAL_OPERATOR_ID)
171 {
172 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100173 fmt::format("{} was called with an invalid operator index. "
174 "subgraph:{} operator:{} at {}",
175 location.m_Function,
176 subgraphIndex,
177 operatorIndex,
178 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100179 }
180}
181
182#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
183 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
184
Mike Kelly0d77ae12022-01-07 17:42:27 +0000185void CheckTensor(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100186 size_t subgraphIndex,
187 size_t tensorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000188 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100189{
telsoa01c577f2c2018-08-31 09:22:23 +0100190 // the tensor index is the only one to check here
191 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
192 {
193 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100194 fmt::format("{} was called with an invalid tensor index. "
195 "subgraph:{} tensor:{} at {}",
196 location.m_Function,
197 subgraphIndex,
198 tensorIndex,
199 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100200 }
201}
202
203#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
204 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
205
Kevin May7d96b162021-02-03 17:38:41 +0000206void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000207 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100208{
209 if (rawPtr == nullptr)
210 {
211 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100212 fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100213 }
214}
215
216#define CHECK_TENSOR_PTR(TENSOR_PTR) \
217 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
218
Mike Kelly0d77ae12022-01-07 17:42:27 +0000219void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100220 size_t bufferIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000221 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100222{
223 if (model.get() == nullptr)
224 {
225 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100226 fmt::format("{} was called with invalid (null) model. "
227 "Possible reason is that the model is not yet loaded and Unpack(ed). "
228 "buffer:{} at {}",
229 location.m_Function,
230 bufferIndex,
231 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100232 }
233 else if (bufferIndex >= model->buffers.size())
234 {
235 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100236 fmt::format("{} was called with an invalid buffer index. "
237 "buffer index:{} at {}",
238 location.m_Function,
239 bufferIndex,
240 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100241 }
242 else if (model->buffers[bufferIndex].get() == nullptr)
243 {
244 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100245 fmt::format("The buffer #{} is null. {}",
246 bufferIndex,
247 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100248 }
249}
250
251#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
252 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
253
Kevin May7d96b162021-02-03 17:38:41 +0000254void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000255 const armnn::TensorInfo& tensorInfo,
telsoa01c577f2c2018-08-31 09:22:23 +0100256 uint32_t bufferId,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000257 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100258{
259 if (bufferPtr == nullptr)
260 {
261 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100262 fmt::format("BufferPtr is null for buffer:{}. {}",
263 bufferId,
264 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100265 }
266 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
267 tensorInfo.GetNumBytes() > bufferPtr->data.size())
268 {
269 std::stringstream ss;
270 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
271 << "For tensor: " << tensorInfo.GetShape()
272 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
273 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
274 throw ParseException(ss.str());
275 }
276}
277
Mike Kelly0d77ae12022-01-07 17:42:27 +0000278
279tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
280{
281 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
282 auto opcodeIndex = operatorPtr->opcode_index;
283
284// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
285#if defined(ARMNN_POST_TFLITE_2_3)
286 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
287 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
288#else
289 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
290#endif
291 return opcode;
292}
293
294std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
295 const TfLiteParserImpl::ModelPtr& model,
296 size_t bufferIndex)
297{
298 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
299 std::vector<unsigned int> buffer(info.GetNumElements());
300
301 if (info.GetDataType() == DataType::Signed32)
302 {
303 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
304 }
305 else if (info.GetDataType() == DataType::Signed64)
306 {
307 std::vector<uint64_t> uint64Buffer(info.GetNumElements());
308 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
309 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
310 }
Mike Kelly0506ef02023-01-03 16:29:44 +0000311 else
312 {
313 CheckLocation location = CHECK_LOCATION();
314 throw ParseException(
315 fmt::format("Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
316 GetDataTypeName(info.GetDataType()),
317 location.AsString()));
318 }
Mike Kelly0d77ae12022-01-07 17:42:27 +0000319 return buffer;
320}
321
telsoa01c577f2c2018-08-31 09:22:23 +0100322#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
323 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
324
325bool IsActivationSupported(tflite::ActivationFunctionType activationType)
326{
327 switch(activationType)
328 {
329 case tflite::ActivationFunctionType_NONE:
330 case tflite::ActivationFunctionType_RELU:
331 case tflite::ActivationFunctionType_RELU6:
332 case tflite::ActivationFunctionType_TANH:
333 {
334 return true;
335 }
336 default:
337 {
338 return false;
339 }
340 }
341}
342
343#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
344 do { \
345 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
346 { \
347 throw ParseException( \
Mike Kelly377fb212023-01-10 15:55:28 +0000348 fmt::format("TfLite parser doesn't support fused activation: " \
James Ward58dec6b2020-09-11 17:32:44 +0100349 "{}/{} in {} subgraph:{} operator:{} at {}", \
350 OPTION->fused_activation_function, \
351 tflite::EnumNameActivationFunctionType(\
352 OPTION->fused_activation_function), \
353 __func__, \
354 SUBGRAPH_INDEX, \
355 OPERATOR_INDEX, \
356 CHECK_LOCATION().FileLine())); \
telsoa01c577f2c2018-08-31 09:22:23 +0100357 } \
358 } while(false)
359
360
Mike Kelly0d77ae12022-01-07 17:42:27 +0000361std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t>& in)
telsoa01c577f2c2018-08-31 09:22:23 +0100362{
363 std::vector<unsigned int> result;
364 result.reserve(in.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +0000365 for (auto& i : in)
telsoa01c577f2c2018-08-31 09:22:23 +0100366 {
mathad01c21025d2021-04-26 10:09:37 +0100367 // If the location of the input data is -1 then the input should be ignored.
368 if (i == -1)
369 {
370 continue;
371 }
telsoa01c577f2c2018-08-31 09:22:23 +0100372 result.push_back(CHECKED_NON_NEGATIVE(i));
373 }
374 return result;
375}
376
Mike Kelly5880b912022-01-28 16:18:54 +0000377bool IsOptionalOperandPresent(int input)
378{
379 return (input >= 0);
380}
381
telsoa01c577f2c2018-08-31 09:22:23 +0100382void CalcPadding(uint32_t inputSize,
383 uint32_t filterSize,
384 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100385 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100386 uint32_t& paddingFront,
387 uint32_t& paddingBack,
388 tflite::Padding padding)
389{
390 paddingFront = 0;
391 paddingBack = 0;
392 if (padding == tflite::Padding_SAME)
393 {
394 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100395 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100397 if (temp > inputSize)
398 {
399 paddingFront = (temp - inputSize) / 2;
400 paddingBack = (temp - inputSize) - paddingFront;
401 }
402 }
403}
404
Teresa Charlin024ef0b2023-04-26 11:19:03 +0100405// Function that calculates explicit padding when the output shape is known.
406// At the moment the output is only given as an input parameter in Transpose Convolution,
407// not in Convolution and Depthwise Convolution
408void CalcPadding(uint32_t inputSize,
409 uint32_t filterSize,
410 uint32_t stride,
411 uint32_t dilation,
412 uint32_t& paddingFront,
413 uint32_t& paddingBack,
414 tflite::Padding padding,
415 uint32_t outputSize)
416{
417 IgnoreUnused(dilation);
418 paddingFront = 0;
419 paddingBack = 0;
420 if (padding == tflite::Padding_SAME)
421 {
422 uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize;
423 paddingFront = totalPadding / 2;
424 paddingBack = totalPadding - paddingFront;
425 }
426}
427
Kevin May7d96b162021-02-03 17:38:41 +0000428armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Finn Williamsb49ed182021-06-29 15:50:08 +0100429 const std::vector<unsigned int>& shape,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100430 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100431{
432 armnn::DataType type;
433 CHECK_TENSOR_PTR(tensorPtr);
434
435 switch (tensorPtr->type)
436 {
437 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000438 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100439 break;
440 case tflite::TensorType_FLOAT32:
441 type = armnn::DataType::Float32;
442 break;
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100443 case tflite::TensorType_FLOAT16:
444 type = armnn::DataType::Float16;
445 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000446 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000447 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000448 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000449 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000450 type = armnn::DataType::QAsymmS8;
451 }
452 else
453 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000454 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000455 type = armnn::DataType::QSymmS8;
456 }
Finn Williamsed66d142019-12-06 09:55:55 +0000457 break;
458 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000459 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000460 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100461 case tflite::TensorType_INT32:
462 type = armnn::DataType::Signed32;
463 break;
Inki Daed4619e22020-09-10 15:33:54 +0900464 case tflite::TensorType_INT64:
465 type = armnn::DataType::Signed64;
466 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100467 case tflite::TensorType_BOOL:
468 type = armnn::DataType::Boolean;
469 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100470 default:
471 {
472 CheckLocation location = CHECK_LOCATION();
473 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100474 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
475 tensorPtr->type,
476 tflite::EnumNameTensorType(tensorPtr->type),
477 tensorPtr->name,
478 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100479 }
480 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100481 TensorShape tensorShape;
482
483 std::vector<unsigned int> safeShape = shape;
484 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100485 {
486 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100487 }
488
489 if (!outputTensor)
490 {
491 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
492 }
493 else
494 {
Rob Hughesd812a312021-08-06 13:10:53 +0100495 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100496
497 // If a shape signature exists we will use that to infer dynamic tensors
498 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100499 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100500 // If the shape is incompatible with the shape signature override the shape
501 if (shapeSignatureSize != shape.size())
502 {
503 safeShape = {};
504
505 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
506 {
507 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
508 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
509 safeShape.push_back(dim);
510 }
511 }
512
Rob Hughesd812a312021-08-06 13:10:53 +0100513 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Mike Kelly04d82292023-01-19 18:29:40 +0000514 bool batchOnly = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100515 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
516 {
Mike Kelly04d82292023-01-19 18:29:40 +0000517 dimMask[i] = tensorPtr->shape_signature[i] != -1;
518
519 if (i > 0 && !dimMask[i])
520 {
521 batchOnly = false;
522 }
523 }
524 if (batchOnly)
525 {
526 dimMask[0] = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100527 }
Rob Hughesd812a312021-08-06 13:10:53 +0100528 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100529 }
530 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
531 else if (shape.size() == 0)
532 {
533 tensorShape = TensorShape(1, false);
534 }
535 else
536 {
537 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100538 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100539 }
540
Teresa Charlinacb3ec52023-04-03 19:57:00 +0100541 float quantizationScale = 1.0f;
Keith Davisd305e1a2020-01-22 11:57:54 +0000542 int32_t quantizationOffset = 0;
543
544 if (tensorPtr->quantization.get())
545 {
546 if (tensorPtr->quantization->scale.size() <= 1)
547 {
548 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
549 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
550
551 if (tensorPtr->quantization->scale.size() == 1)
552 {
553 quantizationScale = tensorPtr->quantization->scale[0];
554 }
555 if (tensorPtr->quantization->zero_point.size() == 1)
556 {
557 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000558 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100559 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000560 }
561
Sadik Armagand109a4d2020-07-28 10:42:13 +0100562 armnn::TensorInfo result(tensorShape,
563 type,
564 quantizationScale,
565 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000566 return result;
567 }
568 else
569 {
570 std::vector<float> quantizationScales;
571 std::vector<int32_t> quantizationOffsets;
572
573 // Scale
574 std::copy(tensorPtr->quantization->scale.begin(),
575 tensorPtr->quantization->scale.end(),
576 std::back_inserter(quantizationScales));
577
Keith Davis0c2eeac2020-02-11 16:51:50 +0000578 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100579 armnn::TensorInfo result(tensorShape,
580 type,
581 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100582 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000583 return result;
584 }
585 }
586 else
587 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100588 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000589 type,
590 quantizationScale,
591 quantizationOffset);
592 return result;
593 }
telsoa01c577f2c2018-08-31 09:22:23 +0100594}
595
Kevin May7d96b162021-02-03 17:38:41 +0000596armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Mike Kelly377fb212023-01-10 15:55:28 +0000597 const bool outputTensor = false)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100598{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000599 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100600 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100601}
602
telsoa01c577f2c2018-08-31 09:22:23 +0100603template<typename T>
604std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000605CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
606 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000607 armnn::TensorInfo& tensorInfo,
608 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100609{
Jan Eilers8eb25602020-03-09 12:13:48 +0000610 IgnoreUnused(tensorPtr);
Ryan OSheac229b3f2023-06-27 22:34:54 +0100611
612 if (!tensorPtr)
613 {
614 throw armnn::ParseException(fmt::format("Tensor pointer is null {}", CHECK_LOCATION().AsString()));
615 }
616
617 if (!bufferPtr)
618 {
619 throw armnn::ParseException(fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
620 }
telsoa01c577f2c2018-08-31 09:22:23 +0100621
622 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000623
624 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
625 {
626 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000627 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
628 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000629 }
630 else
631 {
632 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
633 }
634
Matthew Sloyan81beae32021-07-13 19:46:11 +0100635 // Make sure isConstant flag is set.
636 tensorInfo.SetConstant();
637
telsoa01c577f2c2018-08-31 09:22:23 +0100638 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
639}
640
telsoa01c577f2c2018-08-31 09:22:23 +0100641armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
642{
643 // generate the binding id by shifting the tensor id by 8 bit
644 // and add the subgraph id, which allows 256 subgraphs
645 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
646}
647
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000648bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
649{
650 const unsigned int actualSize = actual.GetNumDimensions();
651 if (actualSize != expected.size())
652 {
653 return false;
654 }
655
656 for (unsigned int i = 0u; i < actualSize; i++)
657 {
658 if (expected[i] < 0 ||
659 actual[i] != static_cast<unsigned int>(expected[i]))
660 {
661 return false;
662 }
663 }
664
665 return true;
666}
667
Cathal Corbett2b922e22022-09-23 15:49:24 +0100668bool CheckShape(const armnn::TensorShape& actual, const armnn::TensorShape& expected)
669{
670 std::vector<int32_t> expectedVec;
671 for (uint32_t i = 0; i < expected.GetNumDimensions(); i++)
672 {
673 expectedVec.push_back(expected[i]);
674 }
675 return CheckShape(actual, expectedVec);
676}
677
James Conroy05102392020-06-24 15:39:55 +0100678void CheckMatchingQuantization(const TensorInfo& first,
679 const TensorInfo& second,
680 const std::string& descName,
681 std::string const& firstName,
682 std::string const& secondName)
683{
684 if (!first.IsQuantized() ||
685 !second.IsQuantized())
686 {
687 // Not a quantized type, ignore the validation
688 return;
689 }
690
691 DataType firstDataType = first.GetDataType();
692 DataType secondDataType = second.GetDataType();
693
694 if (firstDataType != secondDataType)
695 {
696 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
697 " must be of the same quantized type, " +
698 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
699 secondName + " is " + GetDataTypeName(secondDataType));
700 }
701
702 if (!first.IsTypeSpaceMatch(second))
703 {
704 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
705 " must have the same quantization space, " +
706 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
707 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
708 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
709 " and scale " + std::to_string(second.GetQuantizationScale()));
710 }
711}
712
Mike Kelly377fb212023-01-10 15:55:28 +0000713bool IsDynamic(TfLiteParserImpl::TensorRawPtr tensorPtr)
714{
715 auto shape = tensorPtr->shape;
716
717 if (shape.empty())
718 {
719 return true;
720 }
721 auto shapeSig = tensorPtr->shape_signature;
722
723 if (shapeSig.empty())
724 {
725 return false;
726 }
727
728 for (unsigned int i = 0; i < shapeSig.size() ; ++i)
729 {
730 if (shapeSig[i] == -1)
731 {
732 return true;
733 }
734 }
735 return false;
736}
737
telsoa01c577f2c2018-08-31 09:22:23 +0100738} // <anonymous>
739
Kevin May7d96b162021-02-03 17:38:41 +0000740TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100741: m_Options(options)
742, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000743, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100744{
745 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100746 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000747 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100748 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
749 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000750 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
751 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
Samuel Yapfd3ba5a2022-08-24 17:04:34 +0100752 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
Idriss Chaouch564c13d2023-09-01 17:58:38 +0100753 m_ParserFunctions[tflite::BuiltinOperator_BROADCAST_TO] = &TfLiteParserImpl::ParseBroadcastTo;
Teresa Charlin93f0ad02023-03-23 15:28:02 +0000754 m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
mathad01b392e982021-04-07 12:07:30 +0100755 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000756 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
757 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100758 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +0100759 #if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100760 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100761 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000762 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
763 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
764 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
765 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100766 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000767 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300768 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000769 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100770 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000771 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000772 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
773 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin077cddb2023-09-15 15:19:21 +0100774 m_ParserFunctions[tflite::BuiltinOperator_GELU] = &TfLiteParserImpl::ParseGelu;
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;
Teresa Charlin6963b332023-07-11 11:35:41 +0100823 m_ParserFunctions[tflite::BuiltinOperator_SQUARE] = &TfLiteParserImpl::ParseSquare;
John Mcloughlin0ec00872023-05-15 17:03:49 +0100824 m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
Kevin May7d96b162021-02-03 17:38:41 +0000825 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
826 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
827 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
828 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
Teresa Charlin777008b2023-07-26 10:07:55 +0100829 m_ParserFunctions[tflite::BuiltinOperator_TILE] = &TfLiteParserImpl::ParseTile;
Kevin May7d96b162021-02-03 17:38:41 +0000830 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
831 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000832 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
833 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000834 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100835
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100836 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000837 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100838}
839
Mike Kelly377fb212023-01-10 15:55:28 +0000840armnn::TensorInfo TfLiteParserImpl::InputTensorInfo(size_t subgraphIndex,
841 size_t operatorIndex,
842 int input)
843{
844 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
845 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
846
847 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[input]);
848 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
849
850 if (search != m_TensorInfos.end())
851 {
852 return m_TensorInfos[inputId];
853 }
854 else
855 {
856 auto tensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
857 m_TensorInfos.insert({ inputId, tensorInfo });
858 return tensorInfo;
859 }
860}
861
862armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(size_t subgraphIndex,
863 size_t operatorIndex,
864 armnn::IConnectableLayer* layer,
865 int output,
866 std::vector<int> inputs)
867{
868 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
869 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
870
871 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
872
873 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
874
875 if (outputSearch != m_TensorInfos.end())
876 {
877 return m_TensorInfos[outputId];
878 }
879
880 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
881 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
882
883 if (IsDynamic(outputTensorPtr))
884 {
885 if (inputs.empty())
886 {
887 for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
888 {
889 inputs.emplace_back(i);
890 }
891 }
892 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
893 std::vector<armnn::TensorShape> inputShapes;
894
895 for (unsigned int i = 0; i < inputs.size(); ++i)
896 {
897 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[inputs[i]]);
898 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
899
900 if (search != m_TensorInfos.end())
901 {
902 auto &inputTensorInfo = m_TensorInfos[inputId];
903 inputShapes.push_back(inputTensorInfo.GetShape());
904 }
905 else
906 {
Mike Kelly377fb212023-01-10 15:55:28 +0000907 auto inputTensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
908 m_TensorInfos.insert({ inputId, inputTensorInfo});
909 inputShapes.push_back(inputTensorInfo.GetShape());
910 }
911 }
912 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
913 tensor.SetShape(outputShape);
914 }
915 m_TensorInfos.insert({ outputId, tensor});
916 return tensor;
917}
918
919armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(size_t subgraphIndex,
920 size_t operatorIndex,
921 armnn::IConnectableLayer* layer,
922 int output,
923 std::vector<armnn::TensorShape> inputShapes)
924{
925 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
926 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
927
928 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
929 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
930 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
931
932 if (IsDynamic(outputTensorPtr))
933 {
934 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
935 tensor.SetShape(outputShape);
936 }
937 m_TensorInfos.insert({ outputId, tensor});
938 return tensor;
939}
940
Kevin May7d96b162021-02-03 17:38:41 +0000941void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100942{
943 m_Network = armnn::INetworkPtr(nullptr, nullptr);
944 m_Model = nullptr;
945 m_SubgraphConnections.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000946 m_OverriddenOutputShapes.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000947 m_ConstantsToDequantize.clear();
948 m_ConstantsToBeCreated.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000949 m_TensorInfos.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100950}
951
Kevin May7d96b162021-02-03 17:38:41 +0000952INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100953{
954 ResetParser();
955 m_Model = LoadModelFromFile(graphFile);
956 return CreateNetworkFromModel();
957}
958
Mike Kelly0d77ae12022-01-07 17:42:27 +0000959INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100960{
961 ResetParser();
962 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
963 return CreateNetworkFromModel();
964}
965
Finn Williamsb49ed182021-06-29 15:50:08 +0100966
967armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
968{
969 ResetParser();
970 m_Model = std::move(model);
971
972 return CreateNetworkFromModel();
973}
974
Kevin May7d96b162021-02-03 17:38:41 +0000975INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100976{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100977
978 using NetworkOptions = std::vector<BackendOptions>;
979 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100980 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100981 {
Mike Kelly80512b02022-05-16 23:10:42 +0100982 if (m_Options.value().m_InferAndValidate)
983 {
984 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
985 {
986 { "InferAndValidate", true }
987 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100988
Mike Kelly80512b02022-05-16 23:10:42 +0100989 networkOptions.push_back(shapeInferenceMethodOption);
990 }
991 if (m_Options.value().m_AllowExpandedDims)
992 {
993 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
994 {
995 { "AllowExpandedDims", true }
996 });
997
998 networkOptions.push_back(shapeInferenceMethodOption);
999 }
Sadik Armagand109a4d2020-07-28 10:42:13 +01001000 }
Sadik Armagand109a4d2020-07-28 10:42:13 +01001001 m_Network = INetwork::Create(networkOptions);
Ryan OSheac229b3f2023-06-27 22:34:54 +01001002
1003 if (m_Model.get() == nullptr)
1004 {
1005 throw ParseException(fmt::format("Tflite Model pointer is null {}", CHECK_LOCATION().AsString()));
1006 }
telsoa01c577f2c2018-08-31 09:22:23 +01001007
Colm Donelan58125042023-11-08 21:11:05 +00001008 // Identify which subgraph we are going to parse. We only support one subgraph but there may be validation
1009 // subgraphs still stored in the model. We'll ignore these. In the tflite code base they are identified by
1010 // their name beginning with "VALIDATION:".
1011 size_t subgraphIndex = 0;
1012 uint8_t usableSubgraphs = 0;
1013 for (size_t i = 0; i < m_Model->subgraphs.size(); i++)
telsoa01c577f2c2018-08-31 09:22:23 +01001014 {
Colm Donelan58125042023-11-08 21:11:05 +00001015 if (m_Model->subgraphs[i]->name.rfind("VALIDATION:", 0) != 0)
1016 {
1017 usableSubgraphs++;
1018 subgraphIndex = i;
1019 }
telsoa01c577f2c2018-08-31 09:22:23 +01001020 }
1021
Colm Donelan58125042023-11-08 21:11:05 +00001022 if (usableSubgraphs > 1)
1023 {
1024 throw ParseException(
1025 fmt::format("Current TfLite parser only supports 1 non validation subgraph. This model has: {} {}",
1026 usableSubgraphs, CHECK_LOCATION().AsString()));
1027 }
1028
Colm Donelan6350d272020-06-09 16:56:25 +01001029 size_t operatorIndex = 0;
1030 try
telsoa01c577f2c2018-08-31 09:22:23 +01001031 {
Colm Donelan58125042023-11-08 21:11:05 +00001032 const SubgraphPtr& subgraph = m_Model->subgraphs[subgraphIndex];
1033 SetupInputLayerTensorInfos(subgraphIndex);
1034 SetupConstantLayerTensorInfos(subgraphIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00001035
Colm Donelan58125042023-11-08 21:11:05 +00001036 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
1037 for (const OperatorPtr& op : subgraph->operators)
1038 {
1039 const auto& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +01001040
1041// 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 +01001042#if defined(ARMNN_POST_TFLITE_2_3)
Colm Donelan58125042023-11-08 21:11:05 +00001043 auto builtinCode = std::max(opCodePtr->builtin_code,
1044 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
Jim Flynnfca233e2021-09-23 12:16:53 +01001045#else
Colm Donelan58125042023-11-08 21:11:05 +00001046 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001047#endif
telsoa01c577f2c2018-08-31 09:22:23 +01001048
Colm Donelan58125042023-11-08 21:11:05 +00001049 if (builtinCode > tflite::BuiltinOperator_MAX)
1050 {
1051 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
1052 "subgraph:{} operator idx:{}. {}",
1053 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1054 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001055 }
telsoa01c577f2c2018-08-31 09:22:23 +01001056
Colm Donelan58125042023-11-08 21:11:05 +00001057 // lookup and call the parser function
1058 auto& parserFunction = m_ParserFunctions[builtinCode];
1059 (this->*parserFunction)(subgraphIndex, operatorIndex);
1060 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +01001061 }
Colm Donelan58125042023-11-08 21:11:05 +00001062
1063 SetupInputLayers(subgraphIndex);
1064 SetupOutputLayers(subgraphIndex);
1065 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001066 }
Colm Donelan6350d272020-06-09 16:56:25 +01001067 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +01001068 {
Colm Donelan6350d272020-06-09 16:56:25 +01001069 std::stringstream errorString;
1070 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
1071 << subgraphIndex << " error: " << e.what();
1072 ARMNN_LOG(error) << errorString.str();
1073 std::stringstream errors;
1074 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +01001075 throw ParseException(errors.str());
1076 }
1077
1078 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +01001079 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001080 {
1081 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1082 {
1083 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
1084 {
1085 for (size_t inputSlotIdx = 0;
1086 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1087 ++inputSlotIdx)
1088 {
1089 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1090 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1091 }
1092 }
1093 }
1094 }
telsoa01c577f2c2018-08-31 09:22:23 +01001095 return std::move(m_Network);
1096}
1097
Mike Kelly0506ef02023-01-03 16:29:44 +00001098bool TfLiteParserImpl::ShouldConstantTensorBeConverted(TfLiteParserImpl::TensorRawPtr tensorPtr,
1099 armnn::DataType inputDataType,
1100 armnn::DataType tensorDataType)
Mike Kelly5880b912022-01-28 16:18:54 +00001101{
Mike Kelly0506ef02023-01-03 16:29:44 +00001102 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1103 (tensorDataType == DataType::QAsymmU8 ||
1104 tensorDataType == DataType::QAsymmS8 ||
1105 tensorDataType == DataType::QSymmS8 ||
1106 tensorDataType == DataType::Signed32 ||
1107 tensorDataType == DataType::Signed64));
Mike Kelly5880b912022-01-28 16:18:54 +00001108}
1109
Kevin May7d96b162021-02-03 17:38:41 +00001110void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
1111 size_t tensorIndex,
1112 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001113{
1114 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001115
1116 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1117
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001118 if (slot->GetOwningIConnectableLayer().GetType() != LayerType::Constant)
telsoa01c577f2c2018-08-31 09:22:23 +01001119 {
telsoa01c577f2c2018-08-31 09:22:23 +01001120
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001121 // assuming there is only one producer for that tensor
1122 if (tensorSlots.outputSlot != nullptr)
1123 {
1124 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
1125 "subgraph:{} tensor:{} {}",
1126 subgraphIndex,
1127 tensorIndex,
1128 CHECK_LOCATION().AsString()));
1129 }
1130 }
telsoa01c577f2c2018-08-31 09:22:23 +01001131 tensorSlots.outputSlot = slot;
1132}
1133
Kevin May7d96b162021-02-03 17:38:41 +00001134void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
1135 size_t tensorIndex,
1136 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001137{
1138 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001139
Finn Williamsd4fa5452021-03-01 12:31:41 +00001140 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01001141 tensorSlots.inputSlots.push_back(slot);
1142}
1143
Kevin May7d96b162021-02-03 17:38:41 +00001144void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001145{
1146 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1147
1148 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +00001149 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001150
1151 // Identify custom code defined for custom operator
1152 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1153 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1154
Mike Kelly377fb212023-01-10 15:55:28 +00001155 // Find parser function that corresponds to custom code (if any)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001156 auto iterator = m_CustomParserFunctions.find(customCode);
1157 if (iterator != m_CustomParserFunctions.end())
1158 {
1159 customParserFunction = iterator->second;
1160 }
1161
1162 // Run parser function
1163 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1164}
1165
Kevin May7d96b162021-02-03 17:38:41 +00001166void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001167{
1168 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001169
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001170 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1171
1172 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001173
1174// 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 +01001175#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001176 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1177 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1178#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001179 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001180#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001181
1182 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1183 {
1184 // Do not add StandInLayer, throw ParseException instead
1185 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001186 fmt::format("Operator not supported. "
1187 "subgraph:{} operator:{} "
1188 "opcode_index:{} opcode:{} / {} {}",
1189 subgraphIndex,
1190 operatorIndex,
1191 opcodeIndex,
1192 opcode,
1193 tflite::EnumNameBuiltinOperator(opcode),
1194 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001195 }
1196
1197 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1198 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1199
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001200 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1201 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001202
1203 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001204 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001205
1206 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1207 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001208
1209 if (!layer)
1210 {
1211 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1212 operatorIndex, CHECK_LOCATION().AsString()));
1213 }
James Conroy05102392020-06-24 15:39:55 +01001214
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001215 for (unsigned int i = 0u; i < numOutputs; ++i)
1216 {
Mike Kelly04d82292023-01-19 18:29:40 +00001217 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[0], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001218 }
1219
1220 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1221 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1222
1223 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1224 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001225}
1226
mathad01b392e982021-04-07 12:07:30 +01001227void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1228{
1229 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1230
1231 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1232 CHECK_VALID_SIZE(inputs.size(), 1);
1233 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1234 CHECK_VALID_SIZE(outputs.size(), 1);
1235
1236 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1237
1238 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001239
1240 if (!layer)
1241 {
1242 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1243 operatorIndex, CHECK_LOCATION().AsString()));
1244 }
mathad01b392e982021-04-07 12:07:30 +01001245
Mike Kelly377fb212023-01-10 15:55:28 +00001246 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
mathad01b392e982021-04-07 12:07:30 +01001247 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1248
1249 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1250 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1251
1252 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1253 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1254}
1255
Kevin May7d96b162021-02-03 17:38:41 +00001256void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001257{
1258 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1259
Mike Kelly0d77ae12022-01-07 17:42:27 +00001260 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1261 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001262
1263 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1264
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001265 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1266 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1267 CHECK_VALID_SIZE(outputs.size(), 1);
1268
telsoa01c577f2c2018-08-31 09:22:23 +01001269 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001270 inputs.size() == 3 ?
1271 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001272 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1273 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001274 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001275 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1276 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001277
Mike Kelly377fb212023-01-10 15:55:28 +00001278 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1279 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001280
1281 // assuming input is NHWC
1282 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001283 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001284
1285 // assuming the filter is OHWI : Output, H, W, Input
1286 // which is essentially the same as NHWC
1287 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001288 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001289
Pablo Tellof0bd6832019-04-26 17:58:13 +01001290 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1291 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1292 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1293 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001294
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001295 // Add the first input and weights tensor to the registration list.
1296 // The constant weights will be added by SetupConstantLayers.
1297 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1298 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001299
James Ward58dec6b2020-09-11 17:32:44 +01001300 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001301 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001302
Mike Kelly0506ef02023-01-03 16:29:44 +00001303 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
telsoa01c577f2c2018-08-31 09:22:23 +01001304 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001305 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001306 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001307
1308 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001309 {
Mike Kelly377fb212023-01-10 15:55:28 +00001310 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001311
1312 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1313 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1314
Mike Kelly0506ef02023-01-03 16:29:44 +00001315 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001316 {
1317 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1318 }
telsoa01c577f2c2018-08-31 09:22:23 +01001319 }
1320
Ryan OSheac229b3f2023-06-27 22:34:54 +01001321 if (!layer)
1322 {
1323 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1324 operatorIndex, CHECK_LOCATION().AsString()));
1325 }
telsoa01c577f2c2018-08-31 09:22:23 +01001326
Mike Kelly377fb212023-01-10 15:55:28 +00001327 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001328 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001329
1330 // register the input connection slots for the layer, connections are made after all layers have been created
1331 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001332 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001333
jimfly01c25411c2018-11-14 17:47:22 +00001334 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001335 // register the output connection slots for the layer, connections are made after all layers have been created
1336 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001337 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001338}
1339
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001340// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +01001341#if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001342void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1343{
1344 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1345
1346 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1347 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1348
1349 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1350
1351 Convolution3dDescriptor desc;
1352 desc.m_BiasEnabled = false;
1353 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1354 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1355 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1356 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1357 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1358 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1359 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1360
1361 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1362 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1363
1364 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1365 CHECK_VALID_SIZE(outputs.size(), 1);
1366
Mike Kelly377fb212023-01-10 15:55:28 +00001367 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1368 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001369
1370 // Assuming input is NDHWC
1371 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1372 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1373 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1374
1375 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1376 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1377 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1378 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1379
1380 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001381 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001382 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1383 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1384 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1385 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1386
Mike Kelly5880b912022-01-28 16:18:54 +00001387 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001388
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001389 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1390
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001391 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1392 // Add the first input and weights tensor to the registration list.
1393 // The constant weights will be added by SetupConstantLayers.
1394 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1395
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001396 if (inputs.size() == 3)
1397 {
1398 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001399
1400 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1401 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001402 }
1403
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001404 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001405
1406 if (!layer)
1407 {
1408 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1409 operatorIndex, CHECK_LOCATION().AsString()));
1410 }
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001411
Mike Kelly377fb212023-01-10 15:55:28 +00001412 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001413 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1414
1415 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001416 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001417
1418 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1419 // Register the output connection slots for the layer, connections are made after all layers have been created
1420 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1421 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1422}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001423#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001424
Kevin May7d96b162021-02-03 17:38:41 +00001425void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001426{
1427 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1428
Mike Kelly0d77ae12022-01-07 17:42:27 +00001429 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1430 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001431
1432 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1433
1434 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001435 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1436 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001437 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001438 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001439
1440 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1441 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001442 if (inputs.size() == 3)
1443 {
1444 desc.m_BiasEnabled = true;
1445 }
1446
telsoa01c577f2c2018-08-31 09:22:23 +01001447 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1448 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001449 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1450 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001451
Mike Kelly377fb212023-01-10 15:55:28 +00001452 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1453 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001454
Matteo Martincigh747ef822018-12-18 09:26:39 +00001455 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001456 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1457 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001458
1459 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001460 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1461 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1462
Pablo Tellof0bd6832019-04-26 17:58:13 +01001463 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1464 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1465 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1466 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001467
Jan Eilers53ef7952021-06-02 12:01:25 +01001468 // 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 +01001469 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001470
Cathal Corbett06902652022-04-14 17:55:11 +01001471 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1472 // Add the first input and weights tensor to the registration list.
1473 // The constant weights will be added by SetupConstantLayers.
1474 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1475
1476 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1477
1478 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001479 {
1480 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00001481 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Cathal Corbett06902652022-04-14 17:55:11 +01001482
1483 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1484 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001485 }
Ryan OSheac229b3f2023-06-27 22:34:54 +01001486
1487 if (!layer)
1488 {
1489 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1490 operatorIndex, CHECK_LOCATION().AsString()));
1491 }
telsoa01c577f2c2018-08-31 09:22:23 +01001492
Mike Kelly377fb212023-01-10 15:55:28 +00001493 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001494 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001495
1496 // register the input connection slots for the layer, connections are made after all layers have been created
1497 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001498 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001499
jimfly01c25411c2018-11-14 17:47:22 +00001500 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001501 // register the output connection slots for the layer, connections are made after all layers have been created
1502 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1503 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1504}
1505
Kevin May7d96b162021-02-03 17:38:41 +00001506void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001507{
1508 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1509
1510 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1511 CHECK_VALID_SIZE(inputs.size(), 1);
1512
1513 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1514 CHECK_VALID_SIZE(outputs.size(), 1);
1515
James Ward58dec6b2020-09-11 17:32:44 +01001516 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001517
1518 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001519
1520 if (!layer)
1521 {
1522 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1523 operatorIndex, CHECK_LOCATION().AsString()));
1524 }
Finn Williamsed66d142019-12-06 09:55:55 +00001525
Mike Kelly377fb212023-01-10 15:55:28 +00001526 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Finn Williamsed66d142019-12-06 09:55:55 +00001527 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1528
1529 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1530 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1531
1532 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1533 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1534}
1535
Teresa Charlin3ab85482021-06-08 16:59:29 +01001536void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1537{
1538 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1539
1540 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1541 CHECK_VALID_SIZE(inputs.size(), 2);
1542
1543 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1544 CHECK_VALID_SIZE(outputs.size(), 1);
1545
1546 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1547
Mike Kelly377fb212023-01-10 15:55:28 +00001548 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001549 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001550 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1551
Teresa Charlina7a605a2023-06-14 14:51:17 +01001552 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1553
1554 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1555 if (axisBufferPtr == nullptr)
1556 {
1557 throw ParseException(fmt::format("{}: Operation has invalid inputs. Failed to read axis.",
1558 CHECK_LOCATION().AsString()));
1559 }
1560
1561 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
1562 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
1563 int32_t axis = axisData[0];
1564
1565 auto inputRank = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1566 auto outputRank = inputRank + 1;
1567 if((axis < -1 * outputRank) || (outputRank <= axis))
1568 {
1569 throw ParseException(fmt::format("{}: Axis {} is not within [-{}, {}) range.",
1570 CHECK_LOCATION().AsString(), axis, outputRank, outputRank));
1571 }
1572
1573 axis = axis < 0 ? (axis + outputRank) : axis;
1574
1575 std::vector<unsigned int> shape(static_cast<unsigned int>(outputRank));
1576 unsigned int inputShapeIndex = 0;
1577 for (unsigned int i = 0; i < static_cast<unsigned int>(outputRank); ++i)
1578 {
1579 if (i == static_cast<unsigned int>(axis))
1580 {
1581 shape[i] = 1;
1582 }
1583 else
1584 {
1585 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1586 ++inputShapeIndex;
1587 }
1588 }
1589
Teresa Charlin3ab85482021-06-08 16:59:29 +01001590 ReshapeDescriptor reshapeDesc;
Teresa Charlina7a605a2023-06-14 14:51:17 +01001591 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(outputRank), shape.data());
1592 outputTensorInfo.SetShape(reshapeDesc.m_TargetShape);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001593
1594 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001595
1596 if (!layer)
1597 {
1598 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1599 operatorIndex, CHECK_LOCATION().AsString()));
1600 } layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001601
Teresa Charlina7a605a2023-06-14 14:51:17 +01001602 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
1603 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
1604
Teresa Charlin3ab85482021-06-08 16:59:29 +01001605 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1606 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1607
1608 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1609 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1610}
1611
Kevin May7d96b162021-02-03 17:38:41 +00001612void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001613{
1614 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1615
1616 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001617 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001618
1619 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1620 CHECK_VALID_SIZE(outputs.size(), 1);
1621
James Ward58dec6b2020-09-11 17:32:44 +01001622 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001623 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001624
josh minorba424d22019-11-13 10:55:17 -06001625 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001626 {
Mike Kelly377fb212023-01-10 15:55:28 +00001627 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Kevin May85d92602019-09-27 17:21:06 +01001628 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001629 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1630 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001631 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001632 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001633
Mike Kelly08759e22020-03-02 11:41:31 +00001634 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001635 }
Mike Kelly377fb212023-01-10 15:55:28 +00001636 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Keith Davis4cd29a02019-09-09 14:49:20 +01001637
James Conroy05102392020-06-24 15:39:55 +01001638 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001639
1640 if (!layer)
1641 {
1642 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1643 operatorIndex, CHECK_LOCATION().AsString()));
1644 }
Mike Kelly377fb212023-01-10 15:55:28 +00001645
1646 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1647 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001648 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1649
1650 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1651 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1652
1653 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1654 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1655}
1656
Kevin May7d96b162021-02-03 17:38:41 +00001657void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001658{
1659 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1660
Mike Kelly0d77ae12022-01-07 17:42:27 +00001661 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1662 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001663
1664 TransposeConvolution2dDescriptor desc;
1665 desc.m_BiasEnabled = false;
1666 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1667 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1668 desc.m_DataLayout = armnn::DataLayout::NHWC;
1669
1670 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001671 if (inputs.size() == 4)
1672 {
1673 desc.m_BiasEnabled = true;
1674 }
1675 else
1676 {
1677 CHECK_VALID_SIZE(inputs.size(), 3);
1678 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001679
1680 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1681 CHECK_VALID_SIZE(outputs.size(), 1);
1682
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001683
1684 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1685 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1686
1687 // TfLite uses NHWC tensors
1688 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1689 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1690
1691 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1692 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1693
Ryan OSheaf0a35b82023-02-21 18:32:30 +00001694 // This block determines the output shape of the transpose convolution. If the output shape tensor ptr is not null
1695 // And the tensor is a constant, we can access the data at load time and set the output shape of the
1696 // layer. If this is not constant, We do not have access to the shape data, so we have to use
1697 // infer output shape and skip this code block.
1698 if (inputs[0] && IsConstTensor(inputs[0]))
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001699 {
Mike Kelly377fb212023-01-10 15:55:28 +00001700 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001701 std::vector<int> output_shape(tensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00001702
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001703 if (tensorInfo.GetDataType() == DataType::Signed32)
1704 {
1705 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1706 }
1707 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1708 {
1709 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1710 {
1711 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1712 }
1713 }
1714 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1715 for (int dimension : output_shape)
1716 {
1717 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1718 }
1719 desc.m_OutputShapeEnabled = true;
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001720
1721 // TfLite uses NHWC tensors
1722 const unsigned int outputHeight = desc.m_OutputShape[1];
1723 const unsigned int outputWidth = desc.m_OutputShape[2];
1724
1725 CalcPadding(inputHeight,
1726 filterHeight,
1727 desc.m_StrideY,
1728 1, // DilationY
1729 desc.m_PadTop,
1730 desc.m_PadBottom,
1731 options->padding,
1732 outputHeight);
1733
1734 CalcPadding(inputWidth,
1735 filterWidth,
1736 desc.m_StrideX,
1737 1, // DilationX
1738 desc.m_PadLeft,
1739 desc.m_PadRight,
1740 options->padding,
1741 outputWidth);
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001742 }
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001743 else
1744 {
1745 CalcPadding(inputHeight,
1746 filterHeight,
1747 desc.m_StrideY,
1748 1, // DilationY
1749 desc.m_PadTop,
1750 desc.m_PadBottom,
1751 options->padding);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001752
Teresa Charlin024ef0b2023-04-26 11:19:03 +01001753 CalcPadding(inputWidth,
1754 filterWidth,
1755 desc.m_StrideX,
1756 1, // DilationX
1757 desc.m_PadLeft,
1758 desc.m_PadRight,
1759 options->padding);
1760 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001761
Mike Kelly5880b912022-01-28 16:18:54 +00001762 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001763
1764 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001765 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001766
David Monahan61683802021-01-12 09:11:07 +00001767 if (desc.m_BiasEnabled)
1768 {
Mike Kelly377fb212023-01-10 15:55:28 +00001769 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Mike Kelly5880b912022-01-28 16:18:54 +00001770 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001771 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001772 filterTensorAndData.first,
1773 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001774 layerName.c_str());
1775 }
1776 else
1777 {
1778 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001779 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001780 EmptyOptional(),
1781 layerName.c_str());
1782 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001783
Ryan OSheac229b3f2023-06-27 22:34:54 +01001784 if (!layer)
1785 {
1786 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1787 operatorIndex, CHECK_LOCATION().AsString()));
1788 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001789
Mike Kelly377fb212023-01-10 15:55:28 +00001790 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001791 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1792
1793 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1794 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001795 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001796
1797 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1798 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1799}
1800
Kevin May7d96b162021-02-03 17:38:41 +00001801void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001802{
1803 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1804}
1805
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001806void TfLiteParserImpl::ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex)
1807{
1808 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1809
1810 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1811 CHECK_VALID_SIZE(inputs.size(), 2);
1812
1813 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1814 CHECK_VALID_SIZE(outputs.size(), 1);
1815
1816 auto layerName = fmt::format("BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1817
Mike Kelly377fb212023-01-10 15:55:28 +00001818 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1819 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001820
1821 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1822 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1823
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001824 // Adjoint in tensorflow lite performs transpose operation
1825 BatchMatMulDescriptor descriptor(options->adj_x,
1826 options->adj_y,
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001827 false,
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001828 false);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001829 // Arbitrary DataLayout
1830
1831 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001832
1833 if (!layer)
1834 {
1835 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1836 operatorIndex, CHECK_LOCATION().AsString()));
1837 }
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001838
Mike Kelly377fb212023-01-10 15:55:28 +00001839 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001840 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1841
1842 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1843 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1844
1845 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1846 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1847}
1848
Kevin May7d96b162021-02-03 17:38:41 +00001849void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001850{
1851 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1852
1853 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1854 CHECK_VALID_SIZE(inputs.size(), 3);
1855
1856 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1857 CHECK_VALID_SIZE(outputs.size(), 1);
1858
Mike Kelly377fb212023-01-10 15:55:28 +00001859 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001860 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1861
Mike Kelly377fb212023-01-10 15:55:28 +00001862 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001863 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1864
1865 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1866 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1867
1868 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1869 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1870
1871 size_t step = 2;
1872 std::vector<std::pair<unsigned int, unsigned int>> crops;
1873 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1874 {
1875 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1876 }
1877
1878 armnn::BatchToSpaceNdDescriptor desc;
1879 desc.m_BlockShape = blockShape;
1880 desc.m_Crops = crops;
1881 desc.m_DataLayout = armnn::DataLayout::NHWC;
1882
James Ward58dec6b2020-09-11 17:32:44 +01001883 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001884
Mike Kelly377fb212023-01-10 15:55:28 +00001885 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01001886
1887 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01001888
1889 if (!layer)
1890 {
1891 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1892 operatorIndex, CHECK_LOCATION().AsString()));
1893 }
Mike Kelly377fb212023-01-10 15:55:28 +00001894
1895 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1896 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001897 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1898
1899 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1900 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1901
1902 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1903 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1904}
1905
Idriss Chaouch564c13d2023-09-01 17:58:38 +01001906void TfLiteParserImpl::ParseBroadcastTo(size_t subgraphIndex, size_t operatorIndex)
1907{
1908 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1909
1910 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1911 CHECK_VALID_SIZE(inputs.size(), 2);
1912
1913 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1914 CHECK_VALID_SIZE(outputs.size(), 1);
1915
1916 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1917 TensorInfo shapeTensorInfo = ToTensorInfo(inputs[1]);
1918 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
1919
1920 auto layerName = fmt::format("Broadcast_to:{}:{}", subgraphIndex, operatorIndex);
1921
1922 BroadcastToDescriptor descriptor;
1923
1924 auto shapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1925 if (shapeBufferPtr != nullptr)
1926 {
1927 std::vector<unsigned int> targetShape;
1928 unsigned int numElement = shapeTensorInfo.GetNumElements();
1929 auto shapeData = reinterpret_cast<const int32_t*>(shapeBufferPtr->data.data());
1930 if (shapeData)
1931 {
1932 for (unsigned int i = 0; i < numElement; ++i)
1933 {
1934 targetShape.push_back(armnn::numeric_cast<unsigned int>(shapeData[i]));
1935 }
1936 descriptor.m_BroadcastToShape = TensorShape(numElement, targetShape.data());
1937 }
1938 /// get dataShape from outputShape if missing
1939 else
1940 {
1941 if(outputTensorInfo.GetShape().GetNumElements() <= 1)
1942 {
1943 ARMNN_THROW_PARSE_EXCEPTION("For Broadcast_to layer, "
1944 "data and output shape are not found in the buffer.");
1945 }
1946 descriptor.m_BroadcastToShape = outputTensorInfo.GetShape();
1947 }
1948 }
1949 else
1950 {
1951 ARMNN_THROW_PARSE_EXCEPTION("For Broadcast_to layer, Shape data was not found in the buffer.");
1952 }
1953
1954 IConnectableLayer* layer = m_Network->AddBroadcastToLayer(descriptor, layerName.c_str());
1955 ARMNN_ASSERT(layer != nullptr);
1956
1957 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1958
1959 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1960 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1961
1962 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1963 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1964}
1965
Kevin May7d96b162021-02-03 17:38:41 +00001966void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001967{
1968 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1969
1970 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1971 CHECK_VALID_SIZE(inputs.size(), 1);
1972
1973 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1974 CHECK_VALID_SIZE(outputs.size(), 1);
1975
1976 L2NormalizationDescriptor desc;
1977 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001978 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001979 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1980
Ryan OSheac229b3f2023-06-27 22:34:54 +01001981 if (!layer)
1982 {
1983 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1984 operatorIndex, CHECK_LOCATION().AsString()));
1985 }
Matthew Jackson28c94572019-07-18 10:47:03 +01001986
Mike Kelly377fb212023-01-10 15:55:28 +00001987 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Jackson28c94572019-07-18 10:47:03 +01001988 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1989
1990 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1991 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1992
1993 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1994 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1995}
1996
Kevin May7d96b162021-02-03 17:38:41 +00001997void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001998{
1999 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
2000}
2001
Kevin May7d96b162021-02-03 17:38:41 +00002002void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02002003{
2004 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2005
2006 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2007 CHECK_VALID_SIZE(inputs.size(), 2);
2008
2009 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2010 CHECK_VALID_SIZE(outputs.size(), 1);
2011
James Ward58dec6b2020-09-11 17:32:44 +01002012 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01002013
Mike Kelly377fb212023-01-10 15:55:28 +00002014 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2015 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01002016 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02002017
Mike Kelly3ec30772023-03-08 13:47:17 +00002018 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Maximum, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002019
2020 if (!layer)
2021 {
2022 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2023 operatorIndex, CHECK_LOCATION().AsString()));
2024 }
Mike Kelly377fb212023-01-10 15:55:28 +00002025
2026 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2027 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02002028 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2029
2030 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002031 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02002032
2033 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2034 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2035}
2036
Kevin May7d96b162021-02-03 17:38:41 +00002037void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02002038{
2039 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2040
2041 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2042 CHECK_VALID_SIZE(inputs.size(), 2);
2043
2044 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2045 CHECK_VALID_SIZE(outputs.size(), 1);
2046
James Ward58dec6b2020-09-11 17:32:44 +01002047 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01002048
Mike Kelly377fb212023-01-10 15:55:28 +00002049 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2050 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01002051 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02002052
Mike Kelly3ec30772023-03-08 13:47:17 +00002053 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Minimum, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002054
2055 if (!layer)
2056 {
2057 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2058 operatorIndex, CHECK_LOCATION().AsString()));
2059 }
Mike Kelly377fb212023-01-10 15:55:28 +00002060
2061 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2062 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02002063 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2064
2065 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002066 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02002067
2068 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2069 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2070}
2071
Kevin May7d96b162021-02-03 17:38:41 +00002072void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
2073 size_t operatorIndex,
2074 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002075{
2076 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2077
Mike Kelly0d77ae12022-01-07 17:42:27 +00002078 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2079 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002080
2081 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2082
2083 std::string layerName;
2084
2085 switch (algorithm)
2086 {
2087 case PoolingAlgorithm::Average:
2088 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01002089 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002090 break;
2091 case PoolingAlgorithm::Max:
2092 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01002093 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002094 break;
2095 default:
Ryan OSheac229b3f2023-06-27 22:34:54 +01002096 throw ParseException(fmt::format("Unsupported Pooling Algorithm {}", CHECK_LOCATION().AsString()));
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002097 }
2098
2099 Pooling2dDescriptor desc;
2100
2101 desc.m_PoolType = algorithm;
2102 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
2103 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
2104 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
2105 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
2106 desc.m_PaddingMethod = PaddingMethod::Exclude;
2107 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00002108 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002109
2110 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2111 CHECK_VALID_SIZE(inputs.size(), 1);
Mike Kelly377fb212023-01-10 15:55:28 +00002112 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002113
2114 // assuming input is NHWC
2115 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
2116 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
2117
Pablo Tellof0bd6832019-04-26 17:58:13 +01002118 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
2119 desc.m_PadTop, desc.m_PadBottom, options->padding);
2120 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
2121 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002122
2123 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2124 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002125
James Conroy05102392020-06-24 15:39:55 +01002126 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002127
2128 if (!layer)
2129 {
2130 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2131 operatorIndex, CHECK_LOCATION().AsString()));
2132 }
Mike Kelly377fb212023-01-10 15:55:28 +00002133
2134 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2135 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
jimfly01c25411c2018-11-14 17:47:22 +00002136 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002137
2138 // register the input connection slots for the layer, connections are made after all layers have been created
2139 // only the tensors for the inputs are relevant, exclude the const tensors
2140 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00002141 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002142
jimfly01c25411c2018-11-14 17:47:22 +00002143 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01002144 // register the output connection slots for the layer, connections are made after all layers have been created
2145 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2146 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2147}
2148
Kevin May7d96b162021-02-03 17:38:41 +00002149void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06002150{
2151 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2152
2153 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2154 CHECK_VALID_SIZE(inputs.size(), 3);
2155 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2156 CHECK_VALID_SIZE(outputs.size(), 1);
2157
2158 SliceDescriptor desc;
2159
2160 // set begin tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00002161 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
josh minorba424d22019-11-13 10:55:17 -06002162 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2163
2164 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
2165 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2166
2167 // set size tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00002168 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
josh minorba424d22019-11-13 10:55:17 -06002169 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2170
Cathal Corbettde33dda2022-09-20 16:40:09 +01002171 std::vector<int> signedSize(sizeTensorInfo.GetNumElements(), 1);
2172
2173 // if size buffer data is not specified, all contents of size vector remain as values of 1
2174 if (sizeBufferPtr->data.data())
2175 {
2176 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2177 }
2178
josh minorba424d22019-11-13 10:55:17 -06002179 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00002180 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly7ba84d62021-09-10 15:27:19 +01002181
2182 for (unsigned int i = 0; i < signedSize.size(); ++i)
2183 {
2184 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01002185
Mike Kelly7ba84d62021-09-10 15:27:19 +01002186 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
2187 {
2188 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
2189 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
2190 signedValue,
2191 inputTensorInfo.GetShape()[i] - begin[i],
2192 CHECK_LOCATION().AsString()));
2193 }
2194
2195 if (signedValue == -1)
2196 {
2197 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
2198 }
2199 else
2200 {
2201 size[i] = static_cast<unsigned int>(signedValue);
2202 }
2203 }
2204
josh minorba424d22019-11-13 10:55:17 -06002205 desc = SliceDescriptor(begin, size);
2206
James Ward58dec6b2020-09-11 17:32:44 +01002207 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06002208
James Conroy05102392020-06-24 15:39:55 +01002209 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
Mike Kelly377fb212023-01-10 15:55:28 +00002210
2211 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2212 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
josh minorba424d22019-11-13 10:55:17 -06002213 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2214
2215 // register the input connection slots for the layer, connections are made after all layers have been created
2216 // only the tensors for the inputs are relevant, exclude the const tensors
2217 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2218 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2219
2220 // register the output connection slots for the layer, connections are made after all layers have been created
2221 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2222 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2223}
2224
Kevin May7d96b162021-02-03 17:38:41 +00002225void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002226{
2227 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002228 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2229 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01002230
2231 SoftmaxDescriptor desc;
2232 desc.m_Beta = options->beta;
2233
2234 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2235 CHECK_VALID_SIZE(inputs.size(), 1);
2236 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2237 CHECK_VALID_SIZE(outputs.size(), 1);
2238
James Ward58dec6b2020-09-11 17:32:44 +01002239 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002240 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2241
Mike Kelly377fb212023-01-10 15:55:28 +00002242 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
telsoa01c577f2c2018-08-31 09:22:23 +01002243 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2244
2245 // register the input connection slots for the layer, connections are made after all layers have been created
2246 // only the tensors for the inputs are relevant, exclude the const tensors
2247 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2248 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2249
2250 // register the output connection slots for the layer, connections are made after all layers have been created
2251 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2252 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2253}
2254
Teresa Charlinfd33a692022-06-29 15:35:57 +01002255void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
2256{
2257 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2258
2259 LogSoftmaxDescriptor desc;
2260
2261 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2262 CHECK_VALID_SIZE(inputs.size(), 1);
2263 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2264 CHECK_VALID_SIZE(outputs.size(), 1);
2265
2266 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2267 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2268
Mike Kelly377fb212023-01-10 15:55:28 +00002269 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Teresa Charlinfd33a692022-06-29 15:35:57 +01002270 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2271
2272 // register the input connection slots for the layer, connections are made after all layers have been created
2273 // only the tensors for the inputs are relevant, exclude the const tensors
2274 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2275 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2276
2277 // register the output connection slots for the layer, connections are made after all layers have been created
2278 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2279 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2280}
2281
Kevin May7d96b162021-02-03 17:38:41 +00002282void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002283{
2284 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2285
2286 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2287 CHECK_VALID_SIZE(inputs.size(), 3);
2288
2289 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2290 CHECK_VALID_SIZE(outputs.size(), 1);
2291
Mike Kelly377fb212023-01-10 15:55:28 +00002292 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002293 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2294
Mike Kelly377fb212023-01-10 15:55:28 +00002295 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002296 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2297
2298 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
2299 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
2300
2301 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
2302 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
2303
2304 size_t step = 2;
2305 std::vector<std::pair<unsigned int, unsigned int>> padList;
2306 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
2307 {
2308 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2309 }
2310
2311 armnn::SpaceToBatchNdDescriptor desc;
2312 desc.m_BlockShape = blockShape;
2313 desc.m_PadList = padList;
2314 desc.m_DataLayout = armnn::DataLayout::NHWC;
2315
James Ward58dec6b2020-09-11 17:32:44 +01002316 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002317
Mike Kelly377fb212023-01-10 15:55:28 +00002318 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01002319
2320 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002321
2322 if (!layer)
2323 {
2324 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2325 operatorIndex, CHECK_LOCATION().AsString()));
2326 }
Mike Kelly377fb212023-01-10 15:55:28 +00002327
2328 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2329 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002330 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2331
2332 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2333 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2334
2335 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2336 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2337}
2338
Teresa Charlin2a764ad2023-02-24 18:17:31 +00002339void TfLiteParserImpl::ParseSpaceToDepth(size_t subgraphIndex, size_t operatorIndex)
2340{
2341 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2342
2343 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2344 CHECK_VALID_SIZE(inputs.size(), 1);
2345 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2346 CHECK_VALID_SIZE(outputs.size(), 1);
2347
2348 armnn::SpaceToDepthDescriptor descriptor;
2349
2350 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2351 const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2352 auto blockSize = options->block_size;
2353 if (blockSize < 2)
2354 {
2355 throw ParseException(
2356 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
2357 blockSize,
2358 CHECK_LOCATION().AsString()));
2359 }
2360 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2361
2362 auto layerName = fmt::format("SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2363 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002364
2365 if (!layer)
2366 {
2367 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2368 operatorIndex, CHECK_LOCATION().AsString()));
2369 }
2370
Teresa Charlin2a764ad2023-02-24 18:17:31 +00002371 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2372 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2373
2374 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2375 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2376
2377 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2378 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2379}
2380
Teresa Charlin3ab85482021-06-08 16:59:29 +01002381armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00002382 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01002383{
Teresa Charlin3ab85482021-06-08 16:59:29 +01002384 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01002385 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2386
2387 if (inputTensorInfo.GetNumDimensions() > 4)
2388 {
2389 std::stringstream ss;
2390 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2391 << " shape:" << inputTensorInfo.GetShape() << " "
2392 << CHECK_LOCATION().AsString();
2393 throw ParseException(ss.str());
2394 }
2395
2396 if (squeezeDims.empty())
2397 {
2398 squeezeDims.assign(dimensionSequence,
2399 dimensionSequence+inputTensorInfo.GetNumDimensions());
2400 }
2401
2402 std::vector<uint32_t> outputDims;
2403 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2404 {
2405 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2406 auto currentDimension = inputTensorInfo.GetShape()[i];
2407 if (skipSqueeze || currentDimension != 1)
2408 {
2409 outputDims.push_back(currentDimension);
2410 }
2411 }
2412
2413 if (outputDims.size() > 4)
2414 {
2415 std::stringstream ss;
2416 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2417 << " shape:" << inputTensorInfo.GetShape() << " "
2418 << CHECK_LOCATION().AsString();
2419 throw ParseException(ss.str());
2420 }
2421
2422 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2423 outputDims.data());
2424
2425 // we need to preserve the tensor type and the quantization data as well
2426 TensorInfo outTensorInfo = inputTensorInfo;
2427 outTensorInfo.SetShape(outShape);
2428
2429 return outTensorInfo;
2430}
2431
Keith Davis0176fd82021-06-01 17:36:32 +01002432void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2433{
2434 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2435
2436 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2437 CHECK_VALID_SIZE(inputs.size(), 1);
2438 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2439 CHECK_VALID_SIZE(outputs.size(), 1);
2440
2441 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2442
2443 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002444
2445 if (!layer)
2446 {
2447 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2448 operatorIndex, CHECK_LOCATION().AsString()));
2449 }
Keith Davis0176fd82021-06-01 17:36:32 +01002450
Mike Kelly377fb212023-01-10 15:55:28 +00002451 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Keith Davis0176fd82021-06-01 17:36:32 +01002452 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2453
2454 // Check if output tensor type is Signed32 or Signed64
2455 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2456 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2457 {
2458 throw ParseException(
2459 fmt::format(
2460 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2461 CHECK_LOCATION().AsString()));
2462 }
2463
2464 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2466
2467 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2468 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2469}
2470
Kevin May7d96b162021-02-03 17:38:41 +00002471void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002472{
2473 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2474
2475 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2476 CHECK_VALID_SIZE(inputs.size(), 1);
2477
2478 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2479 CHECK_VALID_SIZE(outputs.size(), 1);
2480
2481 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2482 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002483 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002484
Mike Kelly377fb212023-01-10 15:55:28 +00002485 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002486
2487 std::vector<uint32_t> squeezeDim;
2488 // A single negative dim index is interpreted as a negative index in python
2489 // Meaning the index will be the shape size plus the negative index value
2490 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2491 {
2492 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2493 squeezeDim.push_back(static_cast<uint32_t>(dim));
2494 }
2495 else
2496 {
2497 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2498 }
2499
2500 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2501
James Conroy05102392020-06-24 15:39:55 +01002502 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002503
2504 ReshapeDescriptor reshapeDesc;
2505 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2506
Mike Kellyb2293702023-02-14 17:16:12 +00002507 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
2508 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2509
telsoa01c577f2c2018-08-31 09:22:23 +01002510 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002511
2512 if (!layer)
2513 {
2514 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2515 operatorIndex, CHECK_LOCATION().AsString()));
2516 }
2517
telsoa01c577f2c2018-08-31 09:22:23 +01002518 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2519
2520 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2521 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2522
2523 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2524 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2525}
2526
Kevin May7d96b162021-02-03 17:38:41 +00002527void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002528{
2529 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2530
2531 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2532 CHECK_VALID_SIZE(inputs.size(), 4);
2533
2534 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2535 CHECK_VALID_SIZE(outputs.size(), 1);
2536
Mike Kelly0d77ae12022-01-07 17:42:27 +00002537 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2538 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002539
2540 StridedSliceDescriptor desc;
2541 desc.m_BeginMask = options->begin_mask;
2542 desc.m_EllipsisMask = options->ellipsis_mask;
2543 desc.m_EndMask = options->end_mask;
2544 desc.m_NewAxisMask = options->new_axis_mask;
2545 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2546 desc.m_DataLayout = armnn::DataLayout::NHWC;
2547
Mike Kelly377fb212023-01-10 15:55:28 +00002548 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002549 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2550
2551 std::vector<int> begin(beginTensorInfo.GetNumElements());
David Monahan39085f72023-07-28 11:37:29 +01002552 if (beginBufferPtr->data.data() != nullptr)
2553 {
2554 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2555 }
2556 else
2557 {
2558 throw ParseException("ParseStridedSlice: Invalid input - the begin vector is null");
2559 }
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002560
Mike Kelly377fb212023-01-10 15:55:28 +00002561 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002562 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2563
2564 std::vector<int> end(endTensorInfo.GetNumElements());
David Monahan39085f72023-07-28 11:37:29 +01002565 if (endBufferPtr->data.data() != nullptr)
2566 {
2567 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2568 }
2569 else
2570 {
2571 throw ParseException("ParseStridedSlice: Invalid input - the end vector is null");
2572 }
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002573
Mike Kelly377fb212023-01-10 15:55:28 +00002574 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002575 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2576
2577 std::vector<int> stride(strideTensorInfo.GetNumElements());
David Monahan39085f72023-07-28 11:37:29 +01002578
2579 if (strideBufferPtr->data.data() != nullptr)
2580 {
2581 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2582 }
2583 else
2584 {
2585 throw ParseException("ParseStridedSlice: Invalid input - the stride vector is null");
2586 }
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002587
2588 desc.m_Begin = begin;
2589 desc.m_End = end;
2590 desc.m_Stride = stride;
2591
James Ward58dec6b2020-09-11 17:32:44 +01002592 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002593 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002594
2595 if (!layer)
2596 {
2597 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2598 operatorIndex, CHECK_LOCATION().AsString()));
2599 }
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002600
Mike Kelly377fb212023-01-10 15:55:28 +00002601 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002602 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2603
2604 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2605 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2606
2607 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2608 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2609}
2610
Kevin May7d96b162021-02-03 17:38:41 +00002611void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002612{
2613 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2614
Mike Kelly0d77ae12022-01-07 17:42:27 +00002615 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2616 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002617
2618 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2619 CHECK_VALID_SIZE(inputs.size(), 2);
2620
2621 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2622 CHECK_VALID_SIZE(outputs.size(), 1);
2623
Mike Kelly377fb212023-01-10 15:55:28 +00002624 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2625 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002626
James Ward58dec6b2020-09-11 17:32:44 +01002627 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002628 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Sub, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002629
2630 if (!layer)
2631 {
2632 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2633 operatorIndex, CHECK_LOCATION().AsString()));
2634 }
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002635
Mike Kelly377fb212023-01-10 15:55:28 +00002636 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002637 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2638
2639 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002640 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002641 if (options)
2642 {
2643 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2644 }
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002645
2646 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2647 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2648}
2649
Kevin May7d96b162021-02-03 17:38:41 +00002650void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302651{
2652 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2653
Mike Kelly0d77ae12022-01-07 17:42:27 +00002654 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2655 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302656
2657 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2658 CHECK_VALID_SIZE(inputs.size(), 2);
2659
2660 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2661 CHECK_VALID_SIZE(outputs.size(), 1);
2662
Mike Kelly377fb212023-01-10 15:55:28 +00002663 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2664 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302665
James Ward58dec6b2020-09-11 17:32:44 +01002666 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002667 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002668
2669 if (!layer)
2670 {
2671 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2672 operatorIndex, CHECK_LOCATION().AsString()));
2673 }
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302674
Mike Kelly377fb212023-01-10 15:55:28 +00002675 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302676 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2677
2678 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002679 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002680 if (options)
2681 {
2682 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2683 }
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302684
2685 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2686 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2687}
2688
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002689void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2690{
2691 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2692
2693 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2694 CHECK_VALID_SIZE(inputs.size(), 2);
2695
2696 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2697 CHECK_VALID_SIZE(outputs.size(), 1);
2698
Mike Kelly377fb212023-01-10 15:55:28 +00002699 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2700 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002701
2702 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002703 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002704
2705 if (!layer)
2706 {
2707 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2708 operatorIndex, CHECK_LOCATION().AsString()));
2709 }
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002710
Mike Kelly377fb212023-01-10 15:55:28 +00002711 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002712 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2713
2714 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2715 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2716 layer = AddFusedFloorLayer(layer, 0);
2717
2718 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2719 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2720}
2721
Kevin May7d96b162021-02-03 17:38:41 +00002722void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002723{
2724 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2725
Mike Kelly0d77ae12022-01-07 17:42:27 +00002726 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2727 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002728
2729 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2730 CHECK_VALID_SIZE(inputs.size(), 2);
2731
2732 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2733 CHECK_VALID_SIZE(outputs.size(), 1);
2734
Mike Kelly377fb212023-01-10 15:55:28 +00002735 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2736 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002737
James Ward58dec6b2020-09-11 17:32:44 +01002738 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002739 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Add, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002740
2741 if (!layer)
2742 {
2743 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2744 operatorIndex, CHECK_LOCATION().AsString()));
2745 }
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002746
Mike Kelly377fb212023-01-10 15:55:28 +00002747 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002748 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2749
2750 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002751 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002752 if (options)
2753 {
2754 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2755 }
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002756
2757 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2758 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2759}
2760
Kevin May7d96b162021-02-03 17:38:41 +00002761void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002762{
2763 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2764
Mike Kelly0d77ae12022-01-07 17:42:27 +00002765 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2766 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002767
2768 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2769 CHECK_VALID_SIZE(inputs.size(), 2);
2770
2771 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2772 CHECK_VALID_SIZE(outputs.size(), 1);
2773
Mike Kelly377fb212023-01-10 15:55:28 +00002774 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2775 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002776
James Ward58dec6b2020-09-11 17:32:44 +01002777 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly3ec30772023-03-08 13:47:17 +00002778 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002779
2780 if (!layer)
2781 {
2782 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2783 operatorIndex, CHECK_LOCATION().AsString()));
2784 }
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002785
Mike Kelly377fb212023-01-10 15:55:28 +00002786 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002787 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2788
2789 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002790 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Teresa Charlind04873f2023-05-23 14:16:28 +01002791 if (options)
2792 {
2793 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2794 }
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002795
2796 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2797 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2798}
2799
Kevin May7d96b162021-02-03 17:38:41 +00002800void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002801{
2802 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2803
2804 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2805
2806 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2807 CHECK_VALID_SIZE(outputs.size(), 1);
2808
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002809 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2810 TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002811
2812 armnn::MeanDescriptor desc;
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002813 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2814 // Get const axis value from model and set it to descriptor.
2815 if (axisBufferPtr != nullptr)
2816 {
2817 std::vector<int32_t> axisData(dimTensorInfo.GetNumElements());
2818 ::memcpy(axisData.data(), axisBufferPtr->data.data(), dimTensorInfo.GetNumBytes());
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002819
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002820 // Convert the axis to unsigned int and remove duplicates.
2821 auto rank = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2822 std::set<unsigned int> uniqueAxis;
2823 std::transform(axisData.begin(),
2824 axisData.end(),
2825 std::inserter(uniqueAxis, uniqueAxis.begin()),
2826 [rank](int i)->unsigned int{
2827 return static_cast<uint32_t>(((i + rank) % rank)); });
2828 desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2829 }
2830 else
2831 {
2832 for (uint32_t i = 0; i < inputTensorInfo.GetNumDimensions(); ++i)
2833 {
2834 desc.m_Axis.push_back(i);
2835 }
2836 }
2837
Sadik Armagand109a4d2020-07-28 10:42:13 +01002838 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002839
Teresa Charlin046e2cb2023-03-28 17:20:19 +01002840 desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002841
James Ward58dec6b2020-09-11 17:32:44 +01002842 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002843 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002844
2845 if (!layer)
2846 {
2847 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2848 operatorIndex, CHECK_LOCATION().AsString()));
2849 }
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002850
Mike Kelly377fb212023-01-10 15:55:28 +00002851 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002852 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2853
2854 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2855 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2856
2857 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2858 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2859}
2860
Kevin May7d96b162021-02-03 17:38:41 +00002861void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002862{
2863 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2864
Kevin May7d96b162021-02-03 17:38:41 +00002865 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002866
Kevin May7d96b162021-02-03 17:38:41 +00002867 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002868 CHECK_VALID_SIZE(outputs.size(), 1);
2869
Mike Kelly377fb212023-01-10 15:55:28 +00002870 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2871 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002872
Mike Kelly0d77ae12022-01-07 17:42:27 +00002873 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002874
2875 size_t step = 2;
2876 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002877 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2878
2879 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002880 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002881 CHECK_VALID_SIZE(inputs.size(), 2);
2882
2883 if (inputTensorInfo.IsQuantized())
2884 {
2885 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2886 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002887 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002888 else if (opcode == tflite::BuiltinOperator_PADV2)
2889 {
2890 CHECK_VALID_SIZE(inputs.size(), 3);
2891
Mike Kelly377fb212023-01-10 15:55:28 +00002892 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002893
2894 if (padValueTensorInfo.GetNumElements() != 1)
2895 {
2896 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2897 }
2898 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2899
2900 // Get the pad value from the input tensor
2901 if (padValueBufferPtr->data.size() > 0)
2902 {
2903 switch (padValueTensorInfo.GetDataType())
2904 {
2905 case armnn::DataType::Float32:
2906 {
2907 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2908 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2909 desc.m_PadValue = padValueBuffer[0];
2910 break;
2911 }
2912 case armnn::DataType::QAsymmU8:
2913 {
2914 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2915 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2916 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2917 padValueTensorInfo.GetQuantizationScale(),
2918 padValueTensorInfo.GetQuantizationOffset());
2919 break;
2920 }
2921 case armnn::DataType::QAsymmS8:
2922 case armnn::DataType::QSymmS8:
2923 {
2924 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2925 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2926 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2927 padValueTensorInfo.GetQuantizationScale(),
2928 padValueTensorInfo.GetQuantizationOffset());
2929 break;
2930 }
2931 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2932 }
2933 }
2934 else if (inputTensorInfo.IsQuantized())
2935 {
2936 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2937 }
2938 }
2939
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002940 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2941 {
2942 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2943 }
2944
Mike Kelly0d77ae12022-01-07 17:42:27 +00002945 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2946 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01002947
2948 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01002949
2950 if (!layer)
2951 {
2952 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2953 operatorIndex, CHECK_LOCATION().AsString()));
2954 }
2955
Mike Kelly377fb212023-01-10 15:55:28 +00002956 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002957 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2958
2959 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2960 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2961
2962 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2963 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2964}
2965
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002966void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2967{
2968 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2969
2970 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2971 CHECK_VALID_SIZE(inputs.size(), 2);
2972
2973 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2974 CHECK_VALID_SIZE(outputs.size(), 1);
2975
Mike Kelly377fb212023-01-10 15:55:28 +00002976 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002977
Mike Kelly377fb212023-01-10 15:55:28 +00002978 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002979 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2980
2981 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2982 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2983
2984 size_t step = 2;
2985 armnn::PadDescriptor desc;
2986 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2987 {
2988 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2989 }
2990
2991 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2992 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2993
2994 if (options->mode == tflite::MirrorPadMode_REFLECT)
2995 {
2996 desc.m_PaddingMode = PaddingMode::Reflect;
2997 }
2998 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2999 {
3000 desc.m_PaddingMode = PaddingMode::Symmetric;
3001 }
3002 else
3003 {
3004 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
3005 }
3006
3007 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
3008 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
3009 auto inputShape = inputTensorInfo.GetShape();
3010 auto padList = desc.m_PadList;
3011
3012 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
3013 for(unsigned int i = 0; i < padList.size(); ++i)
3014 {
3015 if(padList.at(i).first > (inputShape[i] - isReflect) ||
3016 padList.at(i).second > (inputShape[i] - isReflect))
3017 {
3018 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
3019 "equal (Symmetric) to the dimension size.");
3020 }
3021 }
3022
3023 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01003024
3025 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003026
3027 if (!layer)
3028 {
3029 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3030 operatorIndex, CHECK_LOCATION().AsString()));
3031 }
3032
Mike Kelly377fb212023-01-10 15:55:28 +00003033 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01003034 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3035
3036 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3037 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3038
3039 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3040 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3041}
3042
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003043void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
3044{
3045 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3046
3047 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3048 CHECK_VALID_SIZE(inputs.size(), 2);
3049
3050 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3051 CHECK_VALID_SIZE(outputs.size(), 1);
3052
3053 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
3054
Mike Kelly377fb212023-01-10 15:55:28 +00003055 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3056 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003057
3058 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
Mike Kelly377fb212023-01-10 15:55:28 +00003059
Ryan OSheac229b3f2023-06-27 22:34:54 +01003060 if (!layer)
3061 {
3062 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3063 operatorIndex, CHECK_LOCATION().AsString()));
3064 }
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003065
3066 if (IsConstTensor(inputs[1]))
3067 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003068 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01003069 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
3070 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003071
Mike Kelly5880b912022-01-28 16:18:54 +00003072 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
3073 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003074 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
3075 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00003076 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003077
3078 if (!constLayer)
3079 {
3080 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3081 operatorIndex, CHECK_LOCATION().AsString()));
3082 }
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003083
3084 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
3085 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
3086 RegisterOutputSlots(subgraphIndex,
3087 VIRTUAL_OPERATOR_ID,
3088 constLayer,
3089 { inputTensorIndexes[1] });
3090 }
3091 else
3092 {
3093 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3094 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
3095 }
3096
Mike Kelly377fb212023-01-10 15:55:28 +00003097 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Mike Kelly377fb212023-01-10 15:55:28 +00003098 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3099
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01003100 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3101 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3102}
3103
Kevin May7d96b162021-02-03 17:38:41 +00003104void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00003105{
3106 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3107
3108 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3109 CHECK_VALID_SIZE(inputs.size(), 1);
3110
3111 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3112 CHECK_VALID_SIZE(outputs.size(), 1);
3113
James Ward58dec6b2020-09-11 17:32:44 +01003114 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00003115
3116 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003117
3118 if (!layer)
3119 {
3120 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3121 operatorIndex, CHECK_LOCATION().AsString()));
3122 }
Sadik Armagan66dedc72019-12-10 16:32:07 +00003123
Mike Kelly377fb212023-01-10 15:55:28 +00003124 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan66dedc72019-12-10 16:32:07 +00003125 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3126
3127 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3128 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3129
3130 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3131 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3132}
Finn Williamsc42c3842019-01-22 14:18:11 +00003133
Kevin May7d96b162021-02-03 17:38:41 +00003134void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01003135{
Finn Williamsc42c3842019-01-22 14:18:11 +00003136 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01003137}
3138
Kevin May7d96b162021-02-03 17:38:41 +00003139void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01003140{
Finn Williamsc42c3842019-01-22 14:18:11 +00003141 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
3142}
Sadik Armagan58f39192018-09-17 14:14:39 +01003143
Kevin May7d96b162021-02-03 17:38:41 +00003144void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01003145{
Jan Eilers2f746b32020-07-28 14:00:06 +01003146 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01003147}
3148
Kevin May7d96b162021-02-03 17:38:41 +00003149void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00003150{
3151 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
3152}
3153
Kevin May7d96b162021-02-03 17:38:41 +00003154void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01003155{
3156 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
3157}
3158
Kevin May7d96b162021-02-03 17:38:41 +00003159void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00003160{
3161 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
3162}
3163
Kevin May7d96b162021-02-03 17:38:41 +00003164void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01003165{
3166 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
3167}
Finn Williamsc42c3842019-01-22 14:18:11 +00003168
Teresa Charlin077cddb2023-09-15 15:19:21 +01003169void TfLiteParserImpl::ParseGelu(size_t subgraphIndex, size_t operatorIndex)
3170{
3171 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Gelu);
3172}
3173
Kevin May7d96b162021-02-03 17:38:41 +00003174void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00003175{
3176 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00003177 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00003178 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01003179
3180 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3181 CHECK_VALID_SIZE(inputs.size(), 1);
3182
3183 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3184 CHECK_VALID_SIZE(outputs.size(), 1);
3185
James Ward58dec6b2020-09-11 17:32:44 +01003186 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01003187 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00003188 activationDesc.m_Function = activationType;
3189
3190 switch (activationType)
3191 {
3192 case ActivationFunction::ReLu:
3193 {
James Ward58dec6b2020-09-11 17:32:44 +01003194 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00003195 break;
3196 }
3197 case ActivationFunction::BoundedReLu:
3198 {
James Ward58dec6b2020-09-11 17:32:44 +01003199 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00003200 activationDesc.m_A = 6.0f;
3201 activationDesc.m_B = 0.0f;
3202 break;
3203 }
3204 case ActivationFunction::Sigmoid:
3205 {
James Ward58dec6b2020-09-11 17:32:44 +01003206 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00003207 break;
3208 }
Nina Drozd99851762019-04-09 09:37:38 +01003209 case ActivationFunction::TanH:
3210 {
James Ward58dec6b2020-09-11 17:32:44 +01003211 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01003212 activationDesc.m_A = 1.0f;
3213 activationDesc.m_B = 1.0f;
3214 break;
3215 }
Sadik Armagan12239e72020-05-27 11:06:17 +01003216 case ActivationFunction::LeakyReLu:
3217 {
James Ward58dec6b2020-09-11 17:32:44 +01003218 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00003219 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01003220 activationDesc.m_A = options->alpha;
3221 break;
3222 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00003223 case ActivationFunction::Elu:
3224 {
3225 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
3226 activationDesc.m_A = 1.0f;
3227 break;
3228 }
Jan Eilers2f746b32020-07-28 14:00:06 +01003229 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00003230 {
James Ward58dec6b2020-09-11 17:32:44 +01003231 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01003232 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00003233 }
Teresa Charlin077cddb2023-09-15 15:19:21 +01003234 case ActivationFunction::Gelu:
3235 {
3236 layerName += fmt::format("GELU:{}:{}", subgraphIndex, operatorIndex);
3237 break;
3238 }
Finn Williamsc42c3842019-01-22 14:18:11 +00003239 default:
3240 {
3241 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003242 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
3243 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00003244 }
3245 }
3246
3247 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01003248
Mike Kelly377fb212023-01-10 15:55:28 +00003249 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan58f39192018-09-17 14:14:39 +01003250 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3251
3252 // register the input connection slots for the layer, connections are made after all layers have been created
3253 // only the tensors for the inputs are relevant, exclude the const tensors
3254 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3255 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3256
3257 // register the output connection slots for the layer, connections are made after all layers have been created
3258 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3259 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3260}
Tianle Cheng20773482023-10-03 12:01:11 +01003261
Mike Kelly0d77ae12022-01-07 17:42:27 +00003262armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
3263 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01003264{
3265 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3266 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3267
3268 if (stretchDim != targetDimsIn.end())
3269 {
3270 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3271 {
3272 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003273 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01003274 }
3275
3276 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003277 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01003278 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3279
3280 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
Tianle Cheng20773482023-10-03 12:01:11 +01003281
3282 if (targetNumElements == 0)
3283 {
3284 if (inputTensorInfo.GetNumElements() == 0)
3285 {
3286 outputDims[stretchIndex] = 0;
3287 }
3288 else
3289 {
3290 throw ParseException(
3291 fmt::format("Input to reshape is a tensor with elements, but the requested shape has 0. {}",
3292 CHECK_LOCATION().AsString()));
3293 }
3294 }
3295 else
3296 {
3297 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
3298 }
Sadikb94967b2018-09-19 15:30:00 +01003299 }
3300
3301 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
3302
3303 TensorInfo reshapeInfo = inputTensorInfo;
3304 reshapeInfo.SetShape(outputShape);
3305
3306 return reshapeInfo;
3307}
3308
Kevin May7d96b162021-02-03 17:38:41 +00003309void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01003310{
3311 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3312
3313 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01003314
3315 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3316 CHECK_VALID_SIZE(outputs.size(), 1);
3317
Mike Kelly0d77ae12022-01-07 17:42:27 +00003318 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3319 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01003320 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01003321
Mike Kelly377fb212023-01-10 15:55:28 +00003322 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
kevmay0171972a82018-12-17 14:28:03 +00003323 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01003324 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00003325
Jan Eilersbac9b352020-07-13 13:40:24 +01003326 // Extracting new shape for the output
3327 // There are two ways it can be passed
3328 // * First is to define the target shape in the operator built-in options
3329 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00003330 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01003331 bool targetShapeFound = false;
3332 // Check if built-in options were given
3333 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00003334 {
Jan Eilersbac9b352020-07-13 13:40:24 +01003335 // make sure the parameter is given
3336 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00003337 {
Jan Eilersbac9b352020-07-13 13:40:24 +01003338 targetShape = options->new_shape;
3339 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00003340 }
Derek Lambertic9e52792020-03-11 11:42:26 +00003341 }
Jan Eilersbac9b352020-07-13 13:40:24 +01003342
3343 // If there is no built-in option given or if the built-in new_shape parameter was empty
3344 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00003345 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00003346 // Check for a second input tensor
3347 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01003348 {
3349 if (inputs[1]->is_variable)
3350 {
3351 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
3352 }
3353
3354 if (inputs[1]->shape.size() != 1)
3355 {
3356 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
3357 }
3358
3359 if (inputs[1]->type != tflite::TensorType_INT32)
3360 {
3361 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
3362 }
3363
Teresa Charlin6a056a42021-12-01 10:25:43 +00003364 // Extract target shape from input
3365 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3366 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00003367 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00003368 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003369 for (int i = 0; i < inputs[1]->shape[0]; ++i)
3370 {
3371 targetShape.push_back(values[i]);
3372 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00003373 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003374 else
Jan Eilersbac9b352020-07-13 13:40:24 +01003375 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003376 try
3377 {
3378 // We attempt to infer during Runtime.
Mike Kelly04d82292023-01-19 18:29:40 +00003379 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
3380
3381 if (reshapeShapes[0] == actualOutputTensorInfo.GetNumDimensions())
3382 {
3383 for (unsigned int i = 0; i < actualOutputTensorInfo.GetShape().GetNumDimensions(); ++i)
3384 {
3385 targetShape.push_back(actualOutputTensorInfo.GetShape()[i]);
3386 }
3387 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003388 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
Mike Kelly04d82292023-01-19 18:29:40 +00003389 else if (reshapeShapes[0] > 2)
Cathal Corbettd2f73232021-12-10 13:38:52 +00003390 {
3391 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
3392 "When inferring during runtime, the parser only supports "
3393 "shape (batch, -1) or (-1) for target shape input.",
3394 reshapeShapes[0],
3395 layerName,
3396 CHECK_LOCATION().AsString()));
3397 }
Mike Kelly04d82292023-01-19 18:29:40 +00003398 else
Cathal Corbettd2f73232021-12-10 13:38:52 +00003399 {
Mike Kelly04d82292023-01-19 18:29:40 +00003400 const int32_t numInputElements = inputTensorInfo.GetNumElements();
3401 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
3402 if (reshapeShapes[0] == 1)
3403 {
3404 targetShape = {numInputElements};
3405 }
3406 else if (reshapeShapes[0] == 2)
3407 {
3408 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3409 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003410 }
3411 }
3412 catch (const std::exception& exc)
3413 {
3414 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
3415 "Reshape operation. Reshape operator target shape input buffer data "
3416 "is null. " << exc.what());
3417 }
Jan Eilersbac9b352020-07-13 13:40:24 +01003418 }
3419 }
3420 else
Derek Lambertic9e52792020-03-11 11:42:26 +00003421 {
3422 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
3423 "At least one method required");
3424 }
Derek Lambertic9e52792020-03-11 11:42:26 +00003425 }
3426
kevmay0171972a82018-12-17 14:28:03 +00003427 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00003428 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01003429
kevmay0171972a82018-12-17 14:28:03 +00003430 // Check for valid input size and that reshape parameters equal output shape
Cathal Corbett2b922e22022-09-23 15:49:24 +01003431 // The output shape can be provided to us in 2 ways:
3432 // 1. through the normal 'shape' parameter given by outputs[indx]->shape
3433 // 2. through additional parameter 'shape_signature' given by outputs[indx]->buffer.
3434 // This parameter can sometimes contain -1 value not visible in the 'shape' parameter.
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00003435 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
3436 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00003437 {
Cathal Corbett2b922e22022-09-23 15:49:24 +01003438 // Attempt to extract output shape from secondary 'shape_signature'
3439 // parameter and try to CheckShape() with this param.
3440 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3441
3442 // if outputs[0]->shape_signature contain a -1 value, we need to compute its actual value
3443 // from reshape input in order to correctly verify reshape parameters equal output shape
3444 armnn::TensorInfo secondaryReshapeOutputTensorInfo =
3445 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, secondaryOutputTargetShape);
3446
3447 if (!CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.GetShape()))
3448 {
3449 std::stringstream ss;
3450 ss << "New shape defined in reshape parameters "
3451 << reshapeOutputTensorShape
3452 << " does not equal output shape "
3453 << actualOutputTensorInfo.GetShape()
3454 << ": "
3455 << CHECK_LOCATION().AsString();
3456 throw ParseException(ss.str());
3457 }
kevmay0171972a82018-12-17 14:28:03 +00003458 }
Mike Kelly377fb212023-01-10 15:55:28 +00003459 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
kevmay0171972a82018-12-17 14:28:03 +00003460
Sadikb94967b2018-09-19 15:30:00 +01003461 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00003462 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Mike Kelly377fb212023-01-10 15:55:28 +00003463 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
Sadikb94967b2018-09-19 15:30:00 +01003464
Sadikb94967b2018-09-19 15:30:00 +01003465 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003466
3467 if (!layer)
3468 {
3469 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3470 operatorIndex, CHECK_LOCATION().AsString()));
3471 }
3472
kevmay0171972a82018-12-17 14:28:03 +00003473 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01003474
3475 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3476 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3477
3478 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3479 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3480}
3481
Kevin May7d96b162021-02-03 17:38:41 +00003482void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003483{
Sadik Armagana3b31f02019-12-05 09:08:53 +00003484 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3485}
3486
Kevin May7d96b162021-02-03 17:38:41 +00003487void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003488{
3489 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3490}
3491
Kevin May7d96b162021-02-03 17:38:41 +00003492void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003493{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003494 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3495
3496 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3497 CHECK_VALID_SIZE(inputs.size(), 2);
3498
3499 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3500 CHECK_VALID_SIZE(outputs.size(), 1);
3501
Mike Kelly377fb212023-01-10 15:55:28 +00003502 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003503
3504 // Data for the parsed tensor args (size) must be stored locally.
3505 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
3506
3507 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3508 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
3509
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003510 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003511 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003512 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003513 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
3514 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003515
James Ward58dec6b2020-09-11 17:32:44 +01003516 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00003517
3518 switch (resizeMethod)
3519 {
3520 case ResizeMethod::Bilinear:
3521 {
James Ward58dec6b2020-09-11 17:32:44 +01003522 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00003523
3524 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3525 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3526
David Monahan4a0c9b92020-05-30 09:48:39 +01003527 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003528 break;
3529 }
3530 case ResizeMethod::NearestNeighbor:
3531 {
James Ward58dec6b2020-09-11 17:32:44 +01003532 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00003533 break;
3534 }
3535 default:
3536 {
3537 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003538 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
3539 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00003540 }
3541 }
3542
Mike Kelly377fb212023-01-10 15:55:28 +00003543 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01003544
3545 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003546
3547 if (!layer)
3548 {
3549 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3550 operatorIndex, CHECK_LOCATION().AsString()));
3551 }
3552
Mike Kelly377fb212023-01-10 15:55:28 +00003553 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3554 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003555 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3556
3557 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3558 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3559
3560 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3561 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3562}
3563
Tianle Chenge5a30ff2023-07-03 11:24:12 +01003564void TfLiteParserImpl::ParseReverseV2(size_t subgraphIndex, size_t operatorIndex)
3565{
3566 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3567
3568 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3569 CHECK_VALID_SIZE(inputs.size(), 2);
3570
3571 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3572 CHECK_VALID_SIZE(outputs.size(), 1);
3573
3574 auto layerName = fmt::format("ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3575
3576 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3577 TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
3578 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3579
Tracy Narinebb8d7592023-07-13 16:50:54 +01003580 IConnectableLayer* layer = m_Network->AddReverseV2Layer(layerName.c_str());
Tianle Chenge5a30ff2023-07-03 11:24:12 +01003581 ARMNN_ASSERT(layer != nullptr);
3582
3583 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3584
3585 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Tracy Narinebb8d7592023-07-13 16:50:54 +01003586 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Tianle Chenge5a30ff2023-07-03 11:24:12 +01003587
3588 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3589 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3590}
3591
Teresa Charlin777008b2023-07-26 10:07:55 +01003592void TfLiteParserImpl::ParseTile(size_t subgraphIndex, size_t operatorIndex)
3593{
3594 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3595
3596 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3597 CHECK_VALID_SIZE(inputs.size(), 2);
3598
3599 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3600 CHECK_VALID_SIZE(outputs.size(), 1);
3601
3602 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3603 TensorInfo multiplesTensorInfo = ToTensorInfo(inputs[1]);
3604 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3605
3606 auto layerName = fmt::format("Tile:{}:{}", subgraphIndex, operatorIndex);
3607
3608 TileDescriptor descriptor;
3609
3610 BufferRawPtr multiplesBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3611 if (multiplesBufferPtr != nullptr)
3612 {
3613 std::vector<int32_t> multiplesData(multiplesTensorInfo.GetNumElements());
3614 ::memcpy(multiplesData.data(), multiplesBufferPtr->data.data(), multiplesTensorInfo.GetNumBytes());
3615 descriptor.m_Multiples.assign(multiplesData.begin(), multiplesData.end());
3616 }
3617 else
3618 {
3619 ARMNN_THROW_PARSE_EXCEPTION("For Tile layer, Multiples data was not found in the buffer.");
3620 }
3621
3622 IConnectableLayer* layer = m_Network->AddTileLayer(descriptor, layerName.c_str());
3623 ARMNN_ASSERT(layer != nullptr);
3624
3625 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3626
3627 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3628 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3629
3630 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3631 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3632}
3633
Kevin May7d96b162021-02-03 17:38:41 +00003634void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01003635{
3636 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3637
Mike Kelly0d77ae12022-01-07 17:42:27 +00003638 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3639 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003640
3641 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3642
3643 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3644 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003645 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
3646
Sadik Armagan479045b2018-10-01 11:51:37 +01003647 CHECK_VALID_SIZE(outputs.size(), 1);
3648
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003649 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
Mike Kelly377fb212023-01-10 15:55:28 +00003650 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003651
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003652 const unsigned int concatDimInput = static_cast<unsigned int>(
Mike Kelly377fb212023-01-10 15:55:28 +00003653 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01003654
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003655 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3656 concatDescriptor.SetConcatAxis(concatDimInput);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003657 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01003658
3659 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3660 {
Mike Kelly377fb212023-01-10 15:55:28 +00003661 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
Sadik Armagan479045b2018-10-01 11:51:37 +01003662
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003663 // This set up concatDescriptor view origin
3664 armnnUtils::ProcessConcatInputTensorInfo(
Mike Kelly377fb212023-01-10 15:55:28 +00003665 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01003666 }
3667
James Ward58dec6b2020-09-11 17:32:44 +01003668 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01003669
Jim Flynn906f9462019-05-10 13:55:21 +01003670 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01003671
3672 if (!layer)
3673 {
3674 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3675 operatorIndex, CHECK_LOCATION().AsString()));
3676 }
3677
Mike Kelly377fb212023-01-10 15:55:28 +00003678 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003679 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01003680
James Conroy05102392020-06-24 15:39:55 +01003681 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003682 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01003683
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003684 // add fused activation layer
3685 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01003686
Sadik Armagan479045b2018-10-01 11:51:37 +01003687 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3688 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3689}
3690
Kevin May7d96b162021-02-03 17:38:41 +00003691void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003692{
3693 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3694
Mike Kelly0d77ae12022-01-07 17:42:27 +00003695 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003696 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3697
3698 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3699
3700 FullyConnectedDescriptor desc;
3701 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003702 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003703
3704 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3705 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3706 CHECK_VALID_SIZE(outputs.size(), 1);
3707
Mike Kelly377fb212023-01-10 15:55:28 +00003708 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003709
3710 // Fully Connected Layer accepts two dimensional weights input
3711 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3712 if (weightsDimension != 2)
3713 {
3714 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003715 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3716 "Node {}",
3717 weightsDimension,
3718 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003719 }
3720
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003721 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003722 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003723
Matthew Sloyan81beae32021-07-13 19:46:11 +01003724 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3725 // Add the first input tensor to the registration list
3726 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
Mike Kelly377fb212023-01-10 15:55:28 +00003727 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003728
3729 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3730
Matthew Sloyan81beae32021-07-13 19:46:11 +01003731 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3732 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003733
Mike Kelly0506ef02023-01-03 16:29:44 +00003734 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003735 {
3736 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3737 }
3738
Finn Williamsd4fa5452021-03-01 12:31:41 +00003739 if (inputs.size() == 3)
3740 {
3741 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00003742 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003743
3744 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3745 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003746
Mike Kelly0506ef02023-01-03 16:29:44 +00003747 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003748 {
3749 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3750 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003751 }
3752
Matthew Sloyan81beae32021-07-13 19:46:11 +01003753 // Filters and biases are always passed to fully connected as inputs
3754 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003755
Ryan OSheac229b3f2023-06-27 22:34:54 +01003756 if (!layer)
3757 {
3758 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3759 operatorIndex, CHECK_LOCATION().AsString()));
3760 }
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003761
Finn Williamsd4fa5452021-03-01 12:31:41 +00003762 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003763 if (inputTensorInfo.GetNumDimensions() > 2)
3764 {
3765 // Add reshape to flatten to 2D [batch_size, input_size],
3766 // where "input_size" corresponds to the number of inputs to the layer,
3767 // matching the second dimension of weights,
3768 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3769 std::vector<unsigned int> reshapedDimensions(2);
3770 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3771 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3772
3773 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3774 {
3775 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003776 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3777 reshapedDimensions[1],
3778 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003779 }
3780
Mike Kelly377fb212023-01-10 15:55:28 +00003781 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003782 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
Mike Kelly377fb212023-01-10 15:55:28 +00003783 inputTensorInfo = reshapedTensorInfo;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003784
James Ward58dec6b2020-09-11 17:32:44 +01003785 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003786 armnn::ReshapeDescriptor reshapeDescriptor;
3787 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
Mike Kelly04d82292023-01-19 18:29:40 +00003788 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor,
3789 reshapeLayerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003790
3791 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3792 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3793
3794 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003795 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3796 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3797 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003798 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003799
3800 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003801
Mike Kelly377fb212023-01-10 15:55:28 +00003802 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3803 { inputTensorInfo.GetShape(),
3804 filterTensorInfo.GetShape() });
Mike Kelly04d82292023-01-19 18:29:40 +00003805
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003806 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3807
Mike Kelly04d82292023-01-19 18:29:40 +00003808 if (outputTensorInfo.GetNumDimensions() > 2)
3809 {
3810 // Calculate reshape to flatten to 2D [batch_size, input_size]
3811 std::vector<unsigned int> reshapedDimensions(2);
3812 reshapedDimensions[1] = filterTensorInfo.GetShape()[0];
3813 reshapedDimensions[0] = outputTensorInfo.GetNumElements() / reshapedDimensions[1];
3814 armnn::TensorInfo reshapedOutputTensorInfo = outputTensorInfo;
3815 if (outputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3816 {
3817 throw ParseException(
3818 fmt::format("Failed to deduce output tensor shape from filter size {} {}",
3819 reshapedDimensions[1],
3820 CHECK_LOCATION().AsString()));
3821 }
3822 reshapedOutputTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3823 layer->GetOutputSlot(0).SetTensorInfo(reshapedOutputTensorInfo);
3824
3825 std::string reshapeLayerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3826 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3827 }
3828
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003829 // we need to add the activation layer and fortunately we don't need to care about the data layout
3830 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3831 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003832
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003833 // register the output connection slots for the layer, connections are made after all layers have been created
3834 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3835 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
Mike Kelly04d82292023-01-19 18:29:40 +00003836
3837 m_TensorInfos[outputTensorIndexes[0]] = layer->GetOutputSlot(0).GetTensorInfo();
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003838}
3839
Kevin May7d96b162021-02-03 17:38:41 +00003840void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003841{
3842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3843
Mike Kelly0d77ae12022-01-07 17:42:27 +00003844 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003845
3846 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3847 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3848 CHECK_VALID_SIZE(outputs.size(), 4);
3849
3850 // Obtain custom options from flexbuffers
3851 auto custom_options = operatorPtr->custom_options;
3852 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3853
3854 // Obtain descriptor information from tf lite
3855 DetectionPostProcessDescriptor desc;
3856 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3857 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3858 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3859 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3860 desc.m_NumClasses = m["num_classes"].AsUInt32();
3861 desc.m_ScaleH = m["h_scale"].AsFloat();
3862 desc.m_ScaleW = m["w_scale"].AsFloat();
3863 desc.m_ScaleX = m["x_scale"].AsFloat();
3864 desc.m_ScaleY = m["y_scale"].AsFloat();
3865
keidav0107d58c72019-02-26 11:57:39 +00003866 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003867 {
keidav0107d58c72019-02-26 11:57:39 +00003868 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003869 }
3870 if (!(m["detections_per_class"].IsNull()))
3871 {
3872 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3873 }
3874
3875 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3876 {
3877 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3878 "must be positive and less than or equal to 1.");
3879 }
3880
Mike Kelly377fb212023-01-10 15:55:28 +00003881 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003882 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003883
James Ward58dec6b2020-09-11 17:32:44 +01003884 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003885 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003886 layerName.c_str());
3887
Ryan OSheac229b3f2023-06-27 22:34:54 +01003888 if (!layer)
3889 {
3890 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3891 operatorIndex, CHECK_LOCATION().AsString()));
3892 }
keidav011b3e2ea2019-02-21 10:07:37 +00003893
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003894 // The model does not specify the output shapes.
3895 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3896 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
Mike Kelly377fb212023-01-10 15:55:28 +00003897 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3898 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3899 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3900 m_OverriddenOutputShapes.push_back({ 1 });
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003901
keidav011b3e2ea2019-02-21 10:07:37 +00003902 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3903 {
Mike Kelly377fb212023-01-10 15:55:28 +00003904 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverriddenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003905 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3906 }
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(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3911 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3912
3913 // Register the output connection slots for the layer, connections are made after all layers have been created
3914 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3915 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3916 outputTensorIndexes[1],
3917 outputTensorIndexes[2],
3918 outputTensorIndexes[3]});
3919}
3920
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003921/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003922void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003923{
3924 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3925
3926 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3927 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3928 CHECK_VALID_SIZE(outputs.size(), 1);
3929
3930 if (inputs.size() < 1)
3931 {
3932 throw ParseException("Pack must have at least one input.");
3933 }
3934
3935 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3936 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3937
3938 StackDescriptor desc;
3939 desc.m_Axis = static_cast<uint32_t>(options->axis);
3940 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3941
3942 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00003943 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003944 desc.m_InputShape = inputTensorInfo.GetShape();
3945
James Ward58dec6b2020-09-11 17:32:44 +01003946 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003947 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3948
Ryan OSheac229b3f2023-06-27 22:34:54 +01003949 if (!layer)
3950 {
3951 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3952 operatorIndex, CHECK_LOCATION().AsString()));
3953 }
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003954
Mike Kelly377fb212023-01-10 15:55:28 +00003955 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003956 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3957
3958 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3959 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3960
3961 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3962 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3963}
3964
Mike Kelly5880b912022-01-28 16:18:54 +00003965void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3966{
3967 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3968
3969 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3970 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3971
3972 if (inputs.size() < 2)
3973 {
3974 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3975 }
3976
3977 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3978 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3979 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3980 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003981 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly5880b912022-01-28 16:18:54 +00003982 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3983
3984 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3985 // Please refer to each operand at
3986 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3987 armnn::LstmInputParams params;
3988
3989 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3990 {
3991 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3992 inputTensorInfo).first;
3993 }
3994
3995 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3996 inputTensorInfo).first;
3997 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3998 inputTensorInfo).first;
3999 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
4000 inputTensorInfo).first;
4001
4002 // Recurrent weight tensors of size {n_cell, n_output}
4003 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
4004 {
4005 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
4006 inputTensorInfo).first;
4007 }
4008
4009 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
4010 inputTensorInfo).first;
4011 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
4012 inputTensorInfo).first;
4013 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
4014 inputTensorInfo).first;
4015
4016 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
4017 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
4018 {
4019 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
4020 inputTensorInfo).first;
4021 }
4022
4023 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
4024 {
4025 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
4026 inputTensorInfo).first;
4027 }
4028
4029 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
4030 {
4031 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
4032 inputTensorInfo).first;
4033 }
4034
4035 // Gates bias tensors of size {n_cell}
4036 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
4037 {
4038 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
4039 inputTensorInfo).first;
4040 }
4041
4042 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
4043 inputTensorInfo).first;
4044 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
4045 inputTensorInfo).first;
4046 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
4047 inputTensorInfo).first;
4048
4049 // Projection weight tensor of size {n_output, n_cell}
4050 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
4051 {
4052 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
4053 inputTensorInfo).first;
4054 }
4055 // Projection bias tensor of size {n_output}
4056 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
4057 {
4058 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
4059 inputTensorInfo).first;
4060 }
4061
4062 // These state tensors are defined as variable tensors, and will be modified by this op.
4063 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
4064 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
4065 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
4066 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
4067
4068 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
4069 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
4070 {
4071 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
4072 inputTensorInfo).first;
4073 }
4074
4075 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
4076 {
4077 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
4078 inputTensorInfo).first;
4079 }
4080
4081 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
4082 {
4083 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
4084 inputTensorInfo).first;
4085 }
4086
4087 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
4088 {
4089 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
4090 inputTensorInfo).first;
4091 }
4092
4093 // set the layer descriptor
4094 armnn::UnidirectionalSequenceLstmDescriptor desc;
4095 desc.m_ActivationFunc = nodeParams->fused_activation_function;
4096 desc.m_ClippingThresCell = nodeParams->cell_clip;
4097 desc.m_ClippingThresProj = nodeParams->proj_clip;
4098 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
4099 || params.m_RecurrentToInputWeights == nullptr
4100 || params.m_InputGateBias == nullptr);
4101 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
4102 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
4103 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
4104 || params.m_ForgetLayerNormWeights != nullptr
4105 || params.m_CellLayerNormWeights != nullptr
4106 || params.m_OutputLayerNormWeights != nullptr);
4107 desc.m_TimeMajor = nodeParams->time_major;
4108
Mike Kellyc0800a32022-06-15 10:57:52 +01004109 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00004110 {
4111 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
4112 inputTensorInfo).first;
4113 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
4114 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
4115
4116 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
4117 inputTensorInfo).first;
4118 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
4119 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
4120
4121 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
4122 inputTensorInfo).first;
4123 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
4124 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
4125
4126 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
4127 inputTensorInfo).first;
4128 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
4129 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
4130 }
4131 else
4132 {
4133 float defaultIntermediate = std::pow(2, -12);
4134 desc.m_InputIntermediateScale = defaultIntermediate;
4135 desc.m_ForgetIntermediateScale = defaultIntermediate;
4136 desc.m_CellIntermediateScale = defaultIntermediate;
4137 desc.m_OutputIntermediateScale = defaultIntermediate;
4138 }
4139
Mike Kellyc0800a32022-06-15 10:57:52 +01004140 if (operatorPtr->intermediates.size() > 4)
4141 {
4142 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
4143 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00004144
Mike Kellyc0800a32022-06-15 10:57:52 +01004145 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
4146 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
4147 }
Narumol Prangnawarat5f941242023-08-11 16:09:26 +01004148 unsigned int batchSize = desc.m_TimeMajor ? inputTensorInfo.GetShape()[1] : inputTensorInfo.GetShape()[0];
Mike Kelly5880b912022-01-28 16:18:54 +00004149 unsigned int outputSize = outputTensorInfo.GetShape()[2];
4150 unsigned int numUnits = cellStateInInfo.GetShape()[1];
4151
4152 armnn::DataType dataType = inputTensorInfo.GetDataType();
4153 float qScale = inputTensorInfo.GetQuantizationScale();
4154 float qOffset = inputTensorInfo.GetQuantizationOffset();
4155
4156 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
4157 if (!desc.m_CifgEnabled)
4158 {
4159 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
4160 }
4161 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
4162 cellStateInInfo.GetDataType(),
4163 cellStateInInfo.GetQuantizationScale(),
4164 cellStateInInfo.GetQuantizationOffset());
4165 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
4166
4167 armnn::LstmInputParamsInfo paramsInfo;
4168 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
4169 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
4170 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
4171 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
4172 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
4173 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
4174 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
4175 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
4176 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
4177
4178 if (!desc.m_CifgEnabled)
4179 {
4180 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
4181 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
4182 if (params.m_CellToInputWeights != nullptr)
4183 {
4184 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
4185 }
4186 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
4187 }
4188
4189 if (desc.m_ProjectionEnabled)
4190 {
4191 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
4192 if (params.m_ProjectionBias != nullptr)
4193 {
4194 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
4195 }
4196 }
4197
4198 if (desc.m_PeepholeEnabled)
4199 {
4200 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
4201 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
4202 }
4203
4204 if (desc.m_LayerNormEnabled)
4205 {
4206 if(!desc.m_CifgEnabled)
4207 {
4208 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
4209 }
4210 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
4211 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
4212 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
4213 }
4214
4215 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
4216 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
Ryan OSheac229b3f2023-06-27 22:34:54 +01004217
4218 if (!layer)
4219 {
4220 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4221 operatorIndex, CHECK_LOCATION().AsString()));
4222 }
Mike Kelly5880b912022-01-28 16:18:54 +00004223
4224 // register the input connection slots for the layer, connections are made after all layers have been created
4225 // only the tensors for the inputs are relevant, exclude the const tensors
4226 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
4227 operatorPtr->inputs[18],
4228 operatorPtr->inputs[19]});
4229 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
4230 inputTensorIndexes[1],
4231 inputTensorIndexes[2]});
4232
4233 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4234
4235 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
4236 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
4237 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
4238
4239 unsigned int tensorIndex = outputTensorIndexes[0];
4240 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
4241 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4242}
4243
Kevin May7d96b162021-02-03 17:38:41 +00004244void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01004245{
4246 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4247
Mike Kelly0d77ae12022-01-07 17:42:27 +00004248 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4249 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01004250
4251 // This unpackAxis indicates the axis to unpack
4252 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
4253
4254 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4255 CHECK_VALID_SIZE(inputs.size(), 1);
4256
Mike Kelly377fb212023-01-10 15:55:28 +00004257 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004258
4259 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
4260 {
4261 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004262 fmt::format("The unpack axis: {} cannot be greater than or equal to "
4263 "the number of input dimension {} {}",
4264 unpackAxis,
4265 inputTensorInfo.GetNumDimensions(),
4266 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004267 }
4268
Nina Drozd200e3802019-04-15 09:47:39 +01004269 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
4270 // If num is not defined, automatically infer from the length of the dimension axis.
4271 if(unpackNum == 0)
4272 {
4273 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
4274 }
4275
4276 // If unpack number cannot be inferred and is still zero, throw ParseException.
4277 if(unpackNum == 0)
4278 {
4279 throw ParseException("Number to unpack must greater than zero.");
4280 }
4281
4282 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4283 CHECK_VALID_SIZE(outputs.size(), unpackNum);
4284
4285 auto inputDimSize = inputTensorInfo.GetNumDimensions();
4286 std::vector<unsigned int> unpackDimSizes(inputDimSize);
4287
4288 // Add current input shape to unpackDimSizes
4289 for (unsigned int i = 0; i < inputDimSize; ++i)
4290 {
4291 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
4292 }
4293
4294 if (unpackDimSizes[unpackAxis] != unpackNum)
4295 {
4296 throw ParseException("Number to unpack must be the same as length of the dimension to "
4297 "unpack along.");
4298 }
4299
4300 unpackDimSizes[unpackAxis] /= unpackNum;
4301
4302 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
4303 for (unsigned int j = 0; j < unpackNum; ++j)
4304 {
4305 // Set the size of the views.
4306 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
4307 {
4308 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
4309 }
4310 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
4311 }
Mike Kelly363b5722023-10-11 14:25:50 +01004312 splitDesc.SetAxis(unpackAxis);
James Ward58dec6b2020-09-11 17:32:44 +01004313 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01004314 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004315
4316 if (!layer)
4317 {
4318 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4319 operatorIndex, CHECK_LOCATION().AsString()));
4320 }
Nina Drozd200e3802019-04-15 09:47:39 +01004321
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004322 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
4323 unpackDimSizes.data());
4324
Nina Drozd200e3802019-04-15 09:47:39 +01004325 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4326 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4327
Finn Williamsb49ed182021-06-29 15:50:08 +01004328 std::vector<unsigned int> reshapeDims;
4329 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
4330 {
4331 if (axis != unpackAxis)
4332 {
4333 reshapeDims.push_back(splitOutShape[axis]);
4334 }
4335 }
4336
4337 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
4338
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004339 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
4340 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4341 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004342 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01004343 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004344 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01004345 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004346 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
4347
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01004348 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
4349 outputTensorInfo.GetDataType(),
4350 outputTensorInfo.GetQuantizationScale(),
4351 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004352 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
4353
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01004354 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01004355
4356 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
4357 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
4358 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4359 }
Nina Drozd200e3802019-04-15 09:47:39 +01004360}
4361
Kevin May7d96b162021-02-03 17:38:41 +00004362void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01004363{
4364 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4365
Mike Kelly0d77ae12022-01-07 17:42:27 +00004366 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4367 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01004368
4369 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4370
Nina Drozd200e3802019-04-15 09:47:39 +01004371 // If number of splits cannot be inferred and is zero, throw ParseException.
4372 if(numSplits == 0)
4373 {
4374 throw ParseException("Number to splits must greater than zero.");
4375 }
4376
Nina Drozd0324f482019-04-08 10:52:10 +01004377 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4378 CHECK_VALID_SIZE(inputs.size(), 2);
4379 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4380 CHECK_VALID_SIZE(outputs.size(), numSplits);
4381
Mike Kelly377fb212023-01-10 15:55:28 +00004382 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4383 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Ryan OSheac229b3f2023-06-27 22:34:54 +01004384
4385 if (axisTensorInfo.GetNumElements() != 1)
4386 {
4387 throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4388 CHECK_LOCATION().AsString()));
4389 }
Nina Drozd0324f482019-04-08 10:52:10 +01004390
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004391 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004392 if (axisBufferPtr == nullptr)
4393 {
4394 throw ParseException(
4395 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4396 CHECK_LOCATION().AsString()));
4397 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004398
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004399 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4400 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4401 int32_t axis = axisData[0];
4402
4403 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4404 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4405 {
4406 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4407 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4408 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4409 throw ParseException(
4410 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4411 axis,
4412 CHECK_LOCATION().AsString()));
4413 }
4414
4415 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01004416
Nina Drozd0324f482019-04-08 10:52:10 +01004417 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004418 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01004419 {
4420 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004421 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4422 inputTensorInfo.GetNumDimensions(),
4423 MaxNumOfTensorDimensions,
4424 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01004425 }
4426
4427 std::vector<unsigned int> splitterDimSizes(inputDimSize);
4428
4429 // Add current input shape to splitterDimSizes
4430 for (unsigned int i = 0; i < inputDimSize; ++i)
4431 {
4432 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
4433 }
4434
4435 if (splitterDimSizes[splitDim] % numSplits != 0)
4436 {
4437 throw ParseException("Number of splits must evenly divide the dimension");
4438 }
4439 splitterDimSizes[splitDim] /= numSplits;
4440
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004441 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01004442 for (unsigned int j = 0; j < numSplits; ++j)
4443 {
4444 // Set the size of the views.
4445 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4446 {
4447 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4448 }
4449 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4450 }
Mike Kelly363b5722023-10-11 14:25:50 +01004451 if (axisTensorInfo.GetNumElements() == 1)
4452 {
4453 splitDesc.SetAxis(axis);
4454 }
James Ward58dec6b2020-09-11 17:32:44 +01004455 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01004456 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004457
4458 if (!layer)
4459 {
4460 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4461 operatorIndex, CHECK_LOCATION().AsString()));
4462 }
Nina Drozd0324f482019-04-08 10:52:10 +01004463
4464 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01004465 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01004466
Nina Drozd0324f482019-04-08 10:52:10 +01004467 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4468 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004469 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01004470 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01004471 }
4472
4473 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4474 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4475}
4476
Derek Lambertif0176992020-04-28 13:37:49 +01004477unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
4478{
4479 int numDims = armnn::numeric_cast<int>(numDimsIn);
4480 int v = idx < 0 ? numDims + idx : idx;
Ryan OSheac229b3f2023-06-27 22:34:54 +01004481
4482 if (v < 0 || v > numDims)
4483 {
4484 throw ParseException(fmt::format("Unable to compute index {}", CHECK_LOCATION().AsString()));
4485 }
Derek Lambertif0176992020-04-28 13:37:49 +01004486
4487 return static_cast<unsigned int>(v);
4488}
4489
Kevin May7d96b162021-02-03 17:38:41 +00004490void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01004491{
4492 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4493
Mike Kelly0d77ae12022-01-07 17:42:27 +00004494 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4495 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01004496
4497 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4498 CHECK_VALID_SIZE(inputs.size(), 3);
4499
4500 auto& inputTensor = inputs[0];
4501 auto& splitsTensor = inputs[1];
4502 auto& axisTensor = inputs[2];
4503
4504 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
4505 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
4506 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
Ryan OSheac229b3f2023-06-27 22:34:54 +01004507
4508 if (axisTensorInfo.GetNumElements() != 1)
4509 {
4510 throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4511 CHECK_LOCATION().AsString()));
4512 }
Derek Lambertif0176992020-04-28 13:37:49 +01004513
4514 // Inputs
4515 auto inputDimSize = inputTensorInfo.GetNumDimensions();
4516 if (inputDimSize > MaxNumOfTensorDimensions)
4517 {
4518 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004519 fmt::format("The number of dimensions: {} for input tensors of the "
4520 "SplitV op cannot be greater than {} {}",
4521 inputTensorInfo.GetNumDimensions(),
4522 MaxNumOfTensorDimensions,
4523 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01004524 }
4525
4526 // Get split axis
4527 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004528 if (axisBufferPtr == nullptr)
4529 {
4530 throw ParseException(
4531 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4532 CHECK_LOCATION().AsString()));
4533 }
4534
Derek Lambertif0176992020-04-28 13:37:49 +01004535 std::vector<int> axisData(axisTensorInfo.GetNumElements());
4536 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004537 int32_t axis = axisData[0];
4538
4539 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4540 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4541 {
4542 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4543 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4544 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4545 throw ParseException(
4546 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4547 axis,
4548 CHECK_LOCATION().AsString()));
4549 }
4550 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01004551
Derek Lambertif0176992020-04-28 13:37:49 +01004552 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01004553 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01004554 unsigned int numSplits{0};
4555
4556 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01004557 {
4558 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01004559 }
4560 else
4561 {
Ryan OShea86704732020-05-26 11:41:04 +01004562 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01004563 }
4564
4565 if (numSplits <=0)
4566 {
4567 throw ParseException("SplitV has invalid number of splits");
4568 }
4569
Jan Eilersc0761e92020-06-29 16:48:44 +01004570 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01004571 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01004572 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01004573
Jan Eilersc0761e92020-06-29 16:48:44 +01004574 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01004575 int numInferred{0};
4576 unsigned int inferIdx{0};
4577 int splitSum{0};
4578 for (auto split : splitsData)
4579 {
4580 if (split < 0)
4581 {
4582 numInferred++;
4583 inferIdx = idx;
4584 }
4585 else
4586 {
4587 splitSum += split;
4588 }
4589 idx++;
4590 }
4591 // Check for inferred Axis
4592 if (numInferred == 0)
4593 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004594 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01004595 {
4596 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
4597 }
4598 }
4599 else if (numInferred == 1)
4600 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004601 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01004602 }
4603 else
4604 {
4605 throw ParseException("Cannot infer split size for more than one split");
4606 }
4607
Derek Lambertif0176992020-04-28 13:37:49 +01004608 //Ouput size validation
4609 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4610 CHECK_VALID_SIZE(outputs.size(), numSplits);
4611
4612 // Setup Armnn descriptor
4613 SplitterDescriptor splitDesc(numSplits, inputDimSize);
4614 unsigned int accumSplit = 0;
4615 for (unsigned int j = 0; j < numSplits; ++j)
4616 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004617 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01004618
4619 // Set the size of the views.
4620 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
4621 {
4622 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
4623 if (dimIdx == splitDim)
4624 {
4625 dimSize = splitSize;
4626 }
4627 splitDesc.SetViewSize(j, dimIdx, dimSize);
4628 }
4629
4630 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4631 accumSplit += splitSize;
4632 }
Mike Kelly363b5722023-10-11 14:25:50 +01004633 splitDesc.SetAxis(axis);
Derek Lambertif0176992020-04-28 13:37:49 +01004634
James Ward58dec6b2020-09-11 17:32:44 +01004635 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01004636 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004637
4638 if (!layer)
4639 {
4640 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4641 operatorIndex, CHECK_LOCATION().AsString()));
4642 }
Derek Lambertif0176992020-04-28 13:37:49 +01004643
4644 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4645 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4646
4647 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4648 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004649 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01004650 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4651 }
4652
4653 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4654 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4655}
4656
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004657void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
4658{
4659 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
4660}
4661
Kevin May7d96b162021-02-03 17:38:41 +00004662void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09004663{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004664 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
4665}
4666
4667void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
4668{
Inki Daed4619e22020-09-10 15:33:54 +09004669 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4670 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4671 CHECK_VALID_SIZE(inputs.size(), 2);
4672
4673 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4674 CHECK_VALID_SIZE(outputs.size(), 1);
4675
Mike Kelly377fb212023-01-10 15:55:28 +00004676 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4677 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Inki Daed4619e22020-09-10 15:33:54 +09004678 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Ryan OSheac229b3f2023-06-27 22:34:54 +01004679
4680 if (axisTensorInfo.GetNumElements() != 1)
4681 {
4682 throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4683 CHECK_LOCATION().AsString()));
4684 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004685
4686 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01004687 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
4688 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
4689 {
4690 throw ParseException(
4691 fmt::format(
4692 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4693 CHECK_LOCATION().AsString()));
4694 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004695
4696 // Get const axis value from model and set it to descriptor.
4697 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4698 if (axisBufferPtr == nullptr)
4699 {
4700 throw ParseException(
4701 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4702 CHECK_LOCATION().AsString()));
4703 }
4704
4705 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4706 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4707 int32_t axis = axisData.front();
4708
4709 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4710 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4711 {
4712 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4713 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4714 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4715 throw ParseException(
4716 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4717 axis,
4718 CHECK_LOCATION().AsString()));
4719 }
4720
4721 ArgMinMaxDescriptor desc;
4722 desc.m_Axis = axis;
4723 desc.m_Function = argMinMaxFunction;
4724
4725 // Register a ArgMin/ArgMax layer.
4726 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
4727 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4728 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004729
4730 if (!layer)
4731 {
4732 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4733 operatorIndex, CHECK_LOCATION().AsString()));
4734 }
4735
Mike Kelly377fb212023-01-10 15:55:28 +00004736 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Inki Daed4619e22020-09-10 15:33:54 +09004737 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4738
4739 // Register input tensor to the layer.
4740 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4741 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4742
4743 // Register output tensor to the layer.
4744 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4745 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4746}
4747
Kevin May7d96b162021-02-03 17:38:41 +00004748void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004749{
4750 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4751
Kevin May7d96b162021-02-03 17:38:41 +00004752 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004753 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00004754 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004755 CHECK_VALID_SIZE(outputs.size(), 1);
4756
Mike Kelly377fb212023-01-10 15:55:28 +00004757 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4758 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4759 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Sadik Armagan26868492021-01-22 14:25:31 +00004760
4761 armnn::GatherDescriptor gatherDescriptor;
4762
Mike Kelly0d77ae12022-01-07 17:42:27 +00004763 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4764 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004765 auto axis = options->axis;
4766
Mike Kelly377fb212023-01-10 15:55:28 +00004767 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4768
Sadik Armagan26868492021-01-22 14:25:31 +00004769 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4770 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4771 auto outputDimensions = outputTensorInfo.GetNumDimensions();
4772 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4773 {
4774 throw ParseException(
4775 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4776 axis,
4777 inputDimensions, inputDimensions,
4778 CHECK_LOCATION().AsString()));
4779 }
4780 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4781 {
4782 throw ParseException(
4783 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4784 outputDimensions,
4785 inputDimensions, indicesDimensions,
4786 CHECK_LOCATION().AsString()));
4787 }
4788
4789 gatherDescriptor.m_Axis = axis;
4790
Sadik Armagan26868492021-01-22 14:25:31 +00004791 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004792
4793 if (!layer)
4794 {
4795 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4796 operatorIndex, CHECK_LOCATION().AsString()));
4797 }
4798
Mike Kelly377fb212023-01-10 15:55:28 +00004799 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Sadik Armagan26868492021-01-22 14:25:31 +00004800 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4801
4802 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4803 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4804
4805 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4806 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4807}
4808
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004809void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4810{
4811 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4812
4813 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4814 CHECK_VALID_SIZE(inputs.size(), 2);
4815 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4816 CHECK_VALID_SIZE(outputs.size(), 1);
4817
Mike Kelly377fb212023-01-10 15:55:28 +00004818 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4819 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004820
4821 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4822 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004823
4824 if (!layer)
4825 {
4826 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4827 operatorIndex, CHECK_LOCATION().AsString()));
4828 }
4829
Mike Kelly377fb212023-01-10 15:55:28 +00004830 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004831 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4832
4833 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4834 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4835
4836 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4837 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4838}
4839
Kevin May7d96b162021-02-03 17:38:41 +00004840void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004841{
4842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4843
Kevin May7d96b162021-02-03 17:38:41 +00004844 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004845 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004846 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004847 CHECK_VALID_SIZE(outputs.size(), 1);
4848
4849 armnn::DepthToSpaceDescriptor descriptor;
4850
Mike Kelly0d77ae12022-01-07 17:42:27 +00004851 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4852 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004853 auto blockSize = options->block_size;
4854 if (blockSize < 2)
4855 {
4856 throw ParseException(
4857 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4858 blockSize,
4859 CHECK_LOCATION().AsString()));
4860 }
4861 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4862
4863 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4864 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004865
4866 if (!layer)
4867 {
4868 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4869 operatorIndex, CHECK_LOCATION().AsString()));
4870 }
4871
Mike Kelly377fb212023-01-10 15:55:28 +00004872 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan26868492021-01-22 14:25:31 +00004873 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4874
4875 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4876 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4877
4878 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4879 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4880}
4881
Kevin May7d96b162021-02-03 17:38:41 +00004882void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004883{
Sadik Armagana2747482021-02-09 10:28:54 +00004884 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4885}
4886
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004887void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4888{
4889 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4890}
4891
Sadik Armagana2747482021-02-09 10:28:54 +00004892void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4893{
4894 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4895}
4896
4897void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4898{
4899 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4900}
4901
4902void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4903{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004904 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4905
Mike Kelly0d77ae12022-01-07 17:42:27 +00004906 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4907 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004908
4909 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4910 CHECK_VALID_SIZE(inputs.size(), 2);
4911
4912 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4913 CHECK_VALID_SIZE(outputs.size(), 1);
4914
Sadik Armagana2747482021-02-09 10:28:54 +00004915 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004916
Mike Kelly377fb212023-01-10 15:55:28 +00004917 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4918 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004919
4920 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004921 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4922 // Get const axis value from model and set it to descriptor.
4923 if (axisBufferPtr != nullptr)
4924 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004925 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4926 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4927
4928 // Convert the axis to unsigned int and remove duplicates.
4929 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4930 std::set<unsigned int> uniqueAxis;
4931 std::transform(axisData.begin(),
4932 axisData.end(),
4933 std::inserter(uniqueAxis, uniqueAxis.begin()),
4934 [rank](int i)->unsigned int{
4935 return static_cast<uint32_t>(((i + rank) % rank)); });
4936 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004937 }
Sadik Armagana2747482021-02-09 10:28:54 +00004938 else
4939 {
4940 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4941 {
4942 desc.m_vAxis.push_back(i);
4943 }
4944 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004945
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004946 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004947 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004948
4949 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004950 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004951
Mike Kelly377fb212023-01-10 15:55:28 +00004952 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004953 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4954
4955 // Register input tensor to the layer.
4956 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4957 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4958
4959 // Register output tensor to the layer.
4960 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4961 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4962}
4963
Mike Kelly31dce2b2021-09-01 21:22:37 +01004964void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4965{
4966 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4967
4968 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4969 CHECK_VALID_SIZE(inputs.size(), 1);
4970
4971 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4972 CHECK_VALID_SIZE(outputs.size(), 1);
4973
4974 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4975 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4976
Mike Kelly377fb212023-01-10 15:55:28 +00004977 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly31dce2b2021-09-01 21:22:37 +01004978
4979 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4980 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4981
4982 armnn::NormalizationDescriptor descriptor;
4983 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4984 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4985 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4986 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4987 descriptor.m_K = options->bias;
4988 descriptor.m_Alpha = options->alpha;
4989 descriptor.m_Beta = options->beta;
4990
4991 // ArmNN expects normSize to be the full size of the normalization
4992 // window rather than the radius as in TfLite.
4993 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4994
4995 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01004996
4997 if (!layer)
4998 {
4999 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5000 operatorIndex, CHECK_LOCATION().AsString()));
5001 }
Mike Kelly31dce2b2021-09-01 21:22:37 +01005002
Mike Kelly377fb212023-01-10 15:55:28 +00005003 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Mike Kelly31dce2b2021-09-01 21:22:37 +01005004 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5005
5006 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5007 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5008
5009 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5010 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5011}
5012
Teresa Charlin28aa6692022-07-12 11:18:44 +01005013void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
5014{
5015 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
5016}
5017
Teresa Charlin93f0ad02023-03-23 15:28:02 +00005018void TfLiteParserImpl::ParseCeil(size_t subgraphIndex, size_t operatorIndex)
5019{
5020 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Ceil);
5021}
5022
Teresa Charlin28aa6692022-07-12 11:18:44 +01005023void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
5024{
5025 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
5026}
5027
5028void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
5029{
5030 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
5031}
5032
Matthew Sloyaned7fce42021-04-15 20:46:24 +01005033void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
5034{
5035 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
5036}
5037
5038void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
5039{
5040 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
5041}
5042
John Mcloughlin0ec00872023-05-15 17:03:49 +01005043void TfLiteParserImpl::ParsePower(size_t subgraphIndex, size_t operatorIndex)
5044{
5045 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5046
5047 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5048 CHECK_VALID_SIZE(inputs.size(), 2);
5049
5050 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5051 CHECK_VALID_SIZE(outputs.size(), 1);
5052
5053 auto layerName = fmt::format("Power:{}:{}", subgraphIndex, operatorIndex);
5054
5055 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5056 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5057 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
5058
5059 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01005060
5061 if (!layer)
5062 {
5063 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5064 operatorIndex, CHECK_LOCATION().AsString()));
5065 }
John Mcloughlin0ec00872023-05-15 17:03:49 +01005066
5067 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5068 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5069 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5070
5071 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5072 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5073
5074 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5075 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5076}
5077
Matthew Sloyaned7fce42021-04-15 20:46:24 +01005078void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
5079{
5080 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
5081}
5082
Teresa Charlin28aa6692022-07-12 11:18:44 +01005083void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
5084{
5085 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
5086}
5087
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01005088void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
5089{
5090 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
5091}
5092
Teresa Charlin6963b332023-07-11 11:35:41 +01005093void TfLiteParserImpl::ParseSquare(size_t subgraphIndex, size_t operatorIndex)
5094{
5095 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5096
5097 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5098 CHECK_VALID_SIZE(inputs.size(), 1);
5099
5100 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5101 CHECK_VALID_SIZE(outputs.size(), 1);
5102
5103 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5104
5105 auto layerName = fmt::format("Square:{}:{}", subgraphIndex, operatorIndex);
5106 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
5107 ARMNN_ASSERT(layer != nullptr);
5108
5109 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 0});
5110 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5111 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5112
5113 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5114 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[0]});
5115
5116 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5117 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5118}
5119
John Mcloughlin0ec00872023-05-15 17:03:49 +01005120void TfLiteParserImpl::ParseSquaredDifference(size_t subgraphIndex, size_t operatorIndex)
5121{
5122 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5123
5124 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5125 CHECK_VALID_SIZE(inputs.size(), 2);
5126
5127 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5128 CHECK_VALID_SIZE(outputs.size(), 1);
5129
5130 auto layerName = fmt::format("SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
5131
5132 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5133 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
John Mcloughlin0ec00872023-05-15 17:03:49 +01005134
5135 IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01005136
5137 if (!layer)
5138 {
5139 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5140 operatorIndex, CHECK_LOCATION().AsString()));
5141 }
John Mcloughlin0ec00872023-05-15 17:03:49 +01005142
5143 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
John Mcloughlin0ec00872023-05-15 17:03:49 +01005144 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5145
5146 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5147 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5148
5149 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5150 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5151}
5152
Matthew Sloyaned7fce42021-04-15 20:46:24 +01005153void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
5154{
5155 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5156
5157 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5158 CHECK_VALID_SIZE(inputs.size(), 1);
5159
5160 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5161 CHECK_VALID_SIZE(outputs.size(), 1);
5162
5163 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
5164 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5165
5166 ElementwiseUnaryDescriptor desc;
5167 desc.m_Operation = unaryOperation;
5168 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01005169
5170 if (!layer)
5171 {
5172 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5173 operatorIndex, CHECK_LOCATION().AsString()));
5174 }
Matthew Sloyaned7fce42021-04-15 20:46:24 +01005175
Mike Kelly377fb212023-01-10 15:55:28 +00005176 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyaned7fce42021-04-15 20:46:24 +01005177 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5178
5179 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5180 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5181
5182 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5183 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5184}
5185
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03005186void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
5187{
5188 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
5189}
5190
5191void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
5192{
5193 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
5194}
5195
5196void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
5197{
5198 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
5199}
5200
5201void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
5202{
5203 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
5204}
5205
5206void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
5207{
5208 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
5209}
5210
5211void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
5212{
5213 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
5214}
5215
5216void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
5217 ComparisonOperation comparisonOperation)
5218{
5219 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5220
5221 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5222 CHECK_VALID_SIZE(inputs.size(), 2);
5223
5224 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5225 CHECK_VALID_SIZE(outputs.size(), 1);
5226
5227 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
5228 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5229
Mike Kelly377fb212023-01-10 15:55:28 +00005230 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5231 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03005232 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
5233
5234 ComparisonDescriptor desc;
5235 desc.m_Operation = comparisonOperation;
5236 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
Ryan OSheac229b3f2023-06-27 22:34:54 +01005237
5238 if (!layer)
5239 {
5240 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5241 operatorIndex, CHECK_LOCATION().AsString()));
5242 }
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03005243
Mike Kelly377fb212023-01-10 15:55:28 +00005244 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03005245 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5246
5247 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5248 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5249
5250 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5251 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5252}
5253
Mike Kelly04d82292023-01-19 18:29:40 +00005254armnn::IConnectableLayer* TfLiteParserImpl::AddReshapeLayer(armnn::IConnectableLayer* layer,
5255 unsigned int outputSlot,
5256 std::string reshapeLayerName,
5257 armnn::TensorInfo outputShape)
5258{
5259 ReshapeDescriptor desc;
5260 desc.m_TargetShape = outputShape.GetShape();
5261
5262 IConnectableLayer* reshapeLayer =
5263 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
5264
5265 auto & prevOutputSlot = layer->GetOutputSlot(outputSlot);
5266 prevOutputSlot.Connect(reshapeLayer->GetInputSlot(0));
5267 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputShape);
5268 return reshapeLayer;
5269}
5270
Kevin May7d96b162021-02-03 17:38:41 +00005271armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
5272 unsigned int outputSlot,
5273 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01005274{
5275 ActivationDescriptor activationDesc;
5276 std::string layerName = prevLayer->GetName();
5277
5278 switch(activationType)
5279 {
5280 case tflite::ActivationFunctionType_NONE:
5281 {
5282 // this is a no-op: return previous layer
5283 return prevLayer;
5284 }
5285 case tflite::ActivationFunctionType_RELU:
5286 {
5287 activationDesc.m_Function = ActivationFunction::ReLu;
5288 layerName += ":RELU";
5289 break;
5290 }
5291 case tflite::ActivationFunctionType_RELU6:
5292 {
5293 activationDesc.m_Function = ActivationFunction::BoundedReLu;
5294 activationDesc.m_A = 6.0f;
5295 activationDesc.m_B = 0.0f;
5296 layerName += ":RELU6";
5297 break;
5298 }
5299 case tflite::ActivationFunctionType_TANH:
5300 {
5301 activationDesc.m_Function = ActivationFunction::TanH;
5302 activationDesc.m_A = 1.0f;
5303 activationDesc.m_B = 1.0f;
5304 layerName += ":TANH";
5305 break;
5306 }
5307
5308 // I only put these here as a reminder what others we could support
5309 case tflite::ActivationFunctionType_RELU_N1_TO_1:
5310 case tflite::ActivationFunctionType_SIGN_BIT:
5311 default:
5312 {
5313 throw ParseException(
Mike Kelly377fb212023-01-10 15:55:28 +00005314 fmt::format("TfLite parser doesn't support fused activation: "
James Ward58dec6b2020-09-11 17:32:44 +01005315 "{}/{} {} ",
5316 activationType,
5317 tflite::EnumNameActivationFunctionType(activationType),
5318 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005319
5320 }
5321 }
5322
5323 IConnectableLayer* activationLayer =
5324 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
5325
5326 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5327 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
5328 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
5329 return activationLayer;
5330}
5331
Teresa Charlincdbd40b2022-02-25 13:21:55 +00005332armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
5333 unsigned int outputSlot)
5334{
Teresa Charlin725728e2022-05-05 13:33:33 +01005335
5336 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5337 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
5338
5339 if (dataType == DataType::Signed32)
5340 {
5341 return prevLayer;
5342 }
5343
Teresa Charlincdbd40b2022-02-25 13:21:55 +00005344 std::string layerName = prevLayer->GetName();
5345 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
5346
Teresa Charlincdbd40b2022-02-25 13:21:55 +00005347 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
5348 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01005349
Teresa Charlincdbd40b2022-02-25 13:21:55 +00005350 return floorLayer;
5351}
5352
Mike Kelly0d77ae12022-01-07 17:42:27 +00005353TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01005354{
5355 if (fileName == nullptr)
5356 {
James Ward58dec6b2020-09-11 17:32:44 +01005357 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01005358 CHECK_LOCATION().AsString()));
5359 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01005360 std::error_code errorCode;
5361 fs::path pathToFile(fileName);
5362 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01005363 {
James Ward58dec6b2020-09-11 17:32:44 +01005364 //fmt::format() could not be used here (format error)
5365 std::stringstream msg;
5366 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
5367 << " " << CHECK_LOCATION().AsString();
James Ward58dec6b2020-09-11 17:32:44 +01005368 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01005369 }
Colm Donelan0dfb2652023-06-22 10:19:17 +01005370 if (!fs::is_regular_file(pathToFile))
5371 {
5372 // Exclude non regular files.
5373 throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
5374 pathToFile.c_str()));
5375 }
5376
telsoa01c577f2c2018-08-31 09:22:23 +01005377 std::ifstream file(fileName, std::ios::binary);
5378 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5379 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
5380 fileContent.size());
5381}
5382
Mike Kelly0d77ae12022-01-07 17:42:27 +00005383TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01005384{
5385 if (binaryContent == nullptr)
5386 {
James Ward58dec6b2020-09-11 17:32:44 +01005387 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01005388 CHECK_LOCATION().AsString()));
5389 }
5390 flatbuffers::Verifier verifier(binaryContent, len);
5391 if (verifier.VerifyBuffer<tflite::Model>() == false)
5392 {
5393 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005394 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
5395 "flatbuffers format. size:{} {}",
5396 len,
5397 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005398 }
5399 return tflite::UnPackModel(binaryContent);
5400}
5401
Mike Kelly0d77ae12022-01-07 17:42:27 +00005402TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005403 size_t subgraphIndex,
5404 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005405{
5406 CHECK_MODEL(model, subgraphIndex, operatorIndex);
5407
Mike Kelly0d77ae12022-01-07 17:42:27 +00005408 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5409 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005410
5411 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01005412 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005413 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005414 {
mathad01c21025d2021-04-26 10:09:37 +01005415 // If the input location is -1 then assume input is turned off.
5416 if (operatorPtr->inputs[i] == -1)
5417 {
5418 continue;
5419 }
5420 else
5421 {
5422 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
5423 result.push_back(subgraphPtr->tensors[inputId].get());
5424 }
telsoa01c577f2c2018-08-31 09:22:23 +01005425 }
5426 return result;
5427}
5428
Mike Kelly0d77ae12022-01-07 17:42:27 +00005429TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005430 size_t subgraphIndex,
5431 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005432{
5433 CHECK_MODEL(model, subgraphIndex, operatorIndex);
5434
Mike Kelly0d77ae12022-01-07 17:42:27 +00005435 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5436 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005437
5438 size_t outputCount = operatorPtr->outputs.size();
5439 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005440 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005441 {
5442 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5443 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01005444 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01005445 }
5446 return result;
5447}
5448
Mike Kelly0d77ae12022-01-07 17:42:27 +00005449TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005450 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005451{
5452 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005453 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005454
Derek Lambertiff05cc52019-04-26 13:05:17 +01005455 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01005456 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005457 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005458 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005459 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01005460 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01005461 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01005462 }
5463 return result;
5464}
5465
Mike Kelly0d77ae12022-01-07 17:42:27 +00005466TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00005467 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005468{
5469 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005470 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005471
Derek Lambertiff05cc52019-04-26 13:05:17 +01005472 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01005473 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005474 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005475 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005476 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5477 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01005478 }
5479 return result;
5480}
5481
Kevin May7d96b162021-02-03 17:38:41 +00005482std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
5483 size_t subgraphIndex,
5484 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005485{
5486 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005487 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5488 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005489 return operatorPtr->inputs;
5490}
5491
Kevin May7d96b162021-02-03 17:38:41 +00005492std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
5493 size_t subgraphIndex,
5494 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005495{
5496 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005497 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5498 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01005499 return operatorPtr->outputs;
5500}
5501
Kevin May7d96b162021-02-03 17:38:41 +00005502void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
5503 size_t operatorIndex,
5504 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00005505 const std::vector<unsigned int>& tensorIndexes,
5506 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005507{
5508 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Ryan OSheac229b3f2023-06-27 22:34:54 +01005509
5510 if (!layer)
5511 {
5512 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5513 operatorIndex, CHECK_LOCATION().AsString()));
5514 }
Matthew Sloyan81beae32021-07-13 19:46:11 +01005515
Finn Williamsd4fa5452021-03-01 12:31:41 +00005516 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01005517 {
5518 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005519 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
5520 " for subgraph:{} operator index:{} {}",
5521 tensorIndexes.size(),
5522 layer->GetNumInputSlots(),
5523 subgraphIndex,
5524 operatorIndex,
5525 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005526 }
5527
Finn Williamsd4fa5452021-03-01 12:31:41 +00005528 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01005529 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00005530 unsigned int tensorIndex = tensorIndexes[index];
5531 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01005532 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5533 }
5534}
5535
Kevin May7d96b162021-02-03 17:38:41 +00005536void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
5537 size_t operatorIndex,
5538 IConnectableLayer* layer,
5539 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01005540{
5541 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Ryan OSheac229b3f2023-06-27 22:34:54 +01005542
5543 if (!layer)
5544 {
5545 throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5546 operatorIndex, CHECK_LOCATION().AsString()));
5547 }
5548
telsoa01c577f2c2018-08-31 09:22:23 +01005549 if (tensorIndexes.size() != layer->GetNumOutputSlots())
5550 {
5551 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005552 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
5553 " for subgraph:{} operator index:{} {}",
5554 tensorIndexes.size(),
5555 layer->GetNumOutputSlots(),
5556 subgraphIndex,
5557 operatorIndex,
5558 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005559 }
5560
5561 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
5562 {
5563 unsigned int tensorIndex = tensorIndexes[slotIndex];
5564 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
5565 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5566 }
5567}
5568
Mike Kelly377fb212023-01-10 15:55:28 +00005569void TfLiteParserImpl::SetupInputLayerTensorInfos(size_t subgraphIndex)
5570{
5571 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5572
5573 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5574 for (auto const& tensorIdAndPtr : inputs)
5575 {
5576 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5577 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5578 }
5579}
5580
Kevin May7d96b162021-02-03 17:38:41 +00005581void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005582{
5583 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5584
5585 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005586 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005587 {
5588 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5589 IConnectableLayer* layer =
5590 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5591
5592 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5593 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5594
5595 RegisterOutputSlots(subgraphIndex,
5596 VIRTUAL_OPERATOR_ID,
5597 layer,
5598 { static_cast<uint32_t>(tensorIdAndPtr.first) });
5599 }
5600}
5601
Kevin May7d96b162021-02-03 17:38:41 +00005602void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005603{
5604 CHECK_SUBGRAPH(m_Model, subgraphIndex);
5605
5606 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005607 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005608 {
5609 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5610 IConnectableLayer* layer =
5611 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5612
5613 RegisterInputSlots(subgraphIndex,
5614 VIRTUAL_OPERATOR_ID,
5615 layer,
5616 { static_cast<uint32_t>(tensorIdAndPtr.first) });
5617 }
5618}
5619
Mike Kelly377fb212023-01-10 15:55:28 +00005620void TfLiteParserImpl::SetupConstantLayerTensorInfos(size_t subgraph)
5621{
5622 CHECK_SUBGRAPH(m_Model, subgraph);
5623
5624 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5625 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5626 {
5627 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5628 {
5629 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5630 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5631 {
5632 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5633
5634 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5635
5636 m_TensorInfos.insert({tensorIndex, tensorInfo});
5637 }
5638 }
5639 }
5640}
5641
Mike Kelly5880b912022-01-28 16:18:54 +00005642void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005643{
Mike Kelly5880b912022-01-28 16:18:54 +00005644 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005645
Mike Kelly5880b912022-01-28 16:18:54 +00005646 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005647 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5648 {
5649 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5650 {
5651 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5652 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5653 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005654 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005655
Mike Kelly5880b912022-01-28 16:18:54 +00005656 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01005657 {
5658 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00005659 armnn::DataType dataType = tensorInfo.GetDataType();
5660
5661 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5662 != m_ConstantsToDequantize.end())
5663 {
5664 dataType = DataType::Float32;
5665 }
5666 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5667
5668 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5669 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5670
5671 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
5672 RegisterOutputSlots(subgraphIndex,
5673 VIRTUAL_OPERATOR_ID,
5674 layer,
5675 { tensorIndex });
5676 }
5677 else if (ShouldConstantTensorBeCreated(tensorIndex))
5678 {
5679 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5680 armnn::DataType dataType = tensorInfo.GetDataType();
5681
5682 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5683 != m_ConstantsToDequantize.end())
5684 {
5685 dataType = DataType::Float32;
5686 }
5687 // Make sure isConstant flag is set.
5688 tensorInfo.SetConstant();
5689 tensorInfo.SetDataType(dataType);
5690
5691 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005692
Matthew Sloyan81beae32021-07-13 19:46:11 +01005693 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005694 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005695
Matthew Sloyan81beae32021-07-13 19:46:11 +01005696 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5697 RegisterOutputSlots(subgraphIndex,
5698 VIRTUAL_OPERATOR_ID,
5699 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00005700 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01005701 }
5702 else
5703 {
5704 throw ParseException(
5705 fmt::format("Invalid Tensor: Tensor should be constant. {}",
5706 CHECK_LOCATION().AsString()));
5707 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005708 }
5709 }
5710 }
5711}
5712
telsoa01c577f2c2018-08-31 09:22:23 +01005713// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00005714TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005715{
5716 CHECK_BUFFER(model, bufferIndex);
5717 return model->buffers[bufferIndex].get();
5718}
5719
Matteo Martincigh747ef822018-12-18 09:26:39 +00005720template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00005721std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5722TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
5723 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00005724 armnn::TensorInfo& tensorInfo,
5725 armnn::Optional<armnn::PermutationVector&> permutationVector)
5726{
Matthew Sloyan81beae32021-07-13 19:46:11 +01005727 // Make sure isConstant flag is set.
5728 tensorInfo.SetConstant();
5729
Matteo Martincigh747ef822018-12-18 09:26:39 +00005730 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5731 tensorPtr,
5732 tensorInfo,
5733 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00005734 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00005735 return std::make_pair(constData.first, std::move(storage));
5736}
5737
Mike Kelly5880b912022-01-28 16:18:54 +00005738bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
5739{
5740 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
5741 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5742 != m_ConstantsToBeCreated.end());
5743}
5744
Finn Williamsd4fa5452021-03-01 12:31:41 +00005745bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
5746{
5747 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01005748 bool isConst = true;
5749
5750 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
5751 if (buffer->data.size() == 0)
5752 {
5753 isConst = false;
5754 }
5755
5756 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00005757}
5758
Kevin May7d96b162021-02-03 17:38:41 +00005759std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00005760TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
5761 armnn::TensorInfo& tensorInfo,
5762 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01005763{
5764 CHECK_TENSOR_PTR(tensorPtr);
5765 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5766 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5767
Matthew Sloyan81beae32021-07-13 19:46:11 +01005768 // Make sure isConstant flag is set.
5769 tensorInfo.SetConstant();
5770
telsoa01c577f2c2018-08-31 09:22:23 +01005771 switch (tensorInfo.GetDataType())
5772 {
5773 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005774 return CreateConstTensorAndStoreData<float>(bufferPtr,
5775 tensorPtr,
5776 tensorInfo,
5777 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00005778 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005779 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5780 tensorPtr,
5781 tensorInfo,
5782 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00005783 case armnn::DataType::QSymmS8:
5784 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5785 tensorPtr,
5786 tensorInfo,
5787 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00005788 case armnn::DataType::QAsymmS8:
5789 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5790 tensorPtr,
5791 tensorInfo,
5792 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005793 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005794 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5795 tensorPtr,
5796 tensorInfo,
5797 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005798 default:
5799 {
5800 std::stringstream errString;
5801 errString << "Unexpected datatype when creating const tensor: "
5802 << armnn::GetDataTypeName(tensorInfo.GetDataType())
5803 << " shape:" << tensorInfo.GetShape()
5804 << CHECK_LOCATION().AsString();
5805 throw ParseException(errString.str());
5806 }
5807 }
5808}
5809
Finn Williamsd4fa5452021-03-01 12:31:41 +00005810armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5811 armnn::TensorInfo& tensorInfo)
5812{
5813 CHECK_TENSOR_PTR(tensorPtr);
5814 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5815 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5816
Matthew Sloyan81beae32021-07-13 19:46:11 +01005817 // Make sure isConstant flag is set.
5818 tensorInfo.SetConstant();
5819
Finn Williamsd4fa5452021-03-01 12:31:41 +00005820 return ConstTensor(tensorInfo, bufferPtr->data.data());
5821}
5822
Mike Kelly5880b912022-01-28 16:18:54 +00005823std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5824TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5825 armnn::TensorInfo& tensorInfo,
5826 armnn::DataType inputDataType)
5827{
5828 CHECK_TENSOR_PTR(tensorPtr);
5829 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5830 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5831
5832 // Make sure isConstant flag is set.
5833 tensorInfo.SetConstant();
5834
Mike Kelly0506ef02023-01-03 16:29:44 +00005835 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
Mike Kelly5880b912022-01-28 16:18:54 +00005836 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005837 try
5838 {
5839 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5840 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5841 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
5842 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005843 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005844 {
5845 throw ParseException(
5846 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5847 GetDataTypeName(DataType::Float32),
5848 GetDataTypeName(tensorInfo.GetDataType()),
5849 CHECK_LOCATION().AsString()));
5850 }
Mike Kelly5880b912022-01-28 16:18:54 +00005851 }
5852 else
5853 {
5854 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5855 }
5856}
5857
5858std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5859TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
5860{
5861 CHECK_TENSOR_PTR(tensorPtr);
5862 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5863 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5864 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5865
5866 // Make sure isConstant flag is set.
5867 tensorInfo.SetConstant();
5868
5869 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5870 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005871 try
5872 {
5873 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5874 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5875 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
5876 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005877 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005878 {
5879 throw ParseException(
5880 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5881 GetDataTypeName(DataType::Float32),
5882 GetDataTypeName(tensorInfo.GetDataType()),
5883 CHECK_LOCATION().AsString()));
5884 }
Mike Kelly5880b912022-01-28 16:18:54 +00005885 }
5886 else
5887 {
5888 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5889 }
5890}
5891
Kevin May7d96b162021-02-03 17:38:41 +00005892BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
5893 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005894{
5895 CHECK_SUBGRAPH(m_Model, subgraphId);
5896 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005897 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005898 {
5899 if (input.second->name == name)
5900 {
5901 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00005902 auto inputTensorInfo = ToTensorInfo(input.second);
5903 // Input tensors are always treated as constant tensors during network execution.
5904 inputTensorInfo.SetConstant(true);
5905 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01005906 }
5907 }
5908
5909 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005910 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005911 {
5912 bindings << "'" << input.second->name << "' ";
5913 }
5914
5915 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005916 fmt::format("No input binding found for subgraph:{} and name:{}. "
5917 "Possible inputs are: [{}] {}",
5918 subgraphId,
5919 name,
5920 bindings.str(),
5921 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005922}
5923
Kevin May7d96b162021-02-03 17:38:41 +00005924BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
5925 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005926{
5927 CHECK_SUBGRAPH(m_Model, subgraphId);
5928 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005929 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005930 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005931 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01005932 if (output.second->name == name)
5933 {
5934 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Mike Kelly377fb212023-01-10 15:55:28 +00005935 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5936 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005937 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01005938 }
5939 }
5940
5941 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005942 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005943 {
5944 bindings << "'" << output.second->name << "' ";
5945 }
5946
5947 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005948 fmt::format("No output binding found for subgraph:{} and name:{}. "
5949 "Possible outputs are: [{}] {}",
5950 subgraphId,
5951 name,
5952 bindings.str(),
5953 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005954}
5955
Kevin May7d96b162021-02-03 17:38:41 +00005956size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01005957{
5958 return m_Model->subgraphs.size();
5959}
5960
Kevin May7d96b162021-02-03 17:38:41 +00005961std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005962{
5963 CHECK_SUBGRAPH(m_Model, subgraphId);
5964 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5965 std::vector<std::string> result;
5966 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005967 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005968 {
5969 result.push_back(input.second->name);
5970 }
5971 return result;
5972}
5973
Kevin May7d96b162021-02-03 17:38:41 +00005974std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005975{
5976 CHECK_SUBGRAPH(m_Model, subgraphId);
5977 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5978 std::vector<std::string> result;
5979 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005980 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005981 {
5982 result.push_back(output.second->name);
5983 }
5984 return result;
5985}
5986
Matthew Sloyanac001ee2021-02-03 10:43:04 +00005987const std::string TfLiteParserImpl::GetVersion()
5988{
5989 return TFLITE_PARSER_VERSION;
5990}
5991
Mike Kelly0d77ae12022-01-07 17:42:27 +00005992TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005993: m_FloatData(std::move(data))
5994, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005995, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005996, m_Int32Data(nullptr)
5997{
5998}
5999
Mike Kelly0d77ae12022-01-07 17:42:27 +00006000TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01006001: m_FloatData(nullptr)
6002, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00006003, m_Int8Data(nullptr)
6004, m_Int32Data(nullptr)
6005{
6006}
6007
Mike Kelly0d77ae12022-01-07 17:42:27 +00006008TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00006009: m_FloatData(nullptr)
6010, m_Uint8Data(nullptr)
6011, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01006012, m_Int32Data(nullptr)
6013{
6014}
6015
Mike Kelly0d77ae12022-01-07 17:42:27 +00006016TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01006017: m_FloatData(nullptr)
6018, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00006019, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01006020, m_Int32Data(std::move(data))
6021{
6022}
6023
6024} // armnnTfLiteParser