blob: aa07f7b3f9f9c1fb7fa4c900a0f166aa8434684a [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Mike Kellyc5789ca2020-07-06 19:24:15 +01002// Copyright © 2017 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{
190 // not checking model, because I assume CHECK_MODEL already run
191 // and checked that. An assert would do.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100192 ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
telsoa01c577f2c2018-08-31 09:22:23 +0100193
194 // also subgraph index should be checked by CHECK_MODEL so
195 // I only add an assert here
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100196 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
telsoa01c577f2c2018-08-31 09:22:23 +0100197
198 // the tensor index is the only one to check here
199 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
200 {
201 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100202 fmt::format("{} was called with an invalid tensor index. "
203 "subgraph:{} tensor:{} at {}",
204 location.m_Function,
205 subgraphIndex,
206 tensorIndex,
207 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100208 }
209}
210
211#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
212 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
213
Kevin May7d96b162021-02-03 17:38:41 +0000214void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000215 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100216{
217 if (rawPtr == nullptr)
218 {
219 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100220 fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100221 }
222}
223
224#define CHECK_TENSOR_PTR(TENSOR_PTR) \
225 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
226
Mike Kelly0d77ae12022-01-07 17:42:27 +0000227void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100228 size_t bufferIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000229 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100230{
231 if (model.get() == nullptr)
232 {
233 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100234 fmt::format("{} was called with invalid (null) model. "
235 "Possible reason is that the model is not yet loaded and Unpack(ed). "
236 "buffer:{} at {}",
237 location.m_Function,
238 bufferIndex,
239 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100240 }
241 else if (bufferIndex >= model->buffers.size())
242 {
243 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100244 fmt::format("{} was called with an invalid buffer index. "
245 "buffer index:{} at {}",
246 location.m_Function,
247 bufferIndex,
248 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100249 }
250 else if (model->buffers[bufferIndex].get() == nullptr)
251 {
252 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100253 fmt::format("The buffer #{} is null. {}",
254 bufferIndex,
255 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100256 }
257}
258
259#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
260 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
261
Kevin May7d96b162021-02-03 17:38:41 +0000262void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000263 const armnn::TensorInfo& tensorInfo,
telsoa01c577f2c2018-08-31 09:22:23 +0100264 uint32_t bufferId,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000265 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100266{
267 if (bufferPtr == nullptr)
268 {
269 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100270 fmt::format("BufferPtr is null for buffer:{}. {}",
271 bufferId,
272 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100273 }
274 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
275 tensorInfo.GetNumBytes() > bufferPtr->data.size())
276 {
277 std::stringstream ss;
278 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
279 << "For tensor: " << tensorInfo.GetShape()
280 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
281 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
282 throw ParseException(ss.str());
283 }
284}
285
Mike Kelly0d77ae12022-01-07 17:42:27 +0000286
287tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
288{
289 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
290 auto opcodeIndex = operatorPtr->opcode_index;
291
292// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
293#if defined(ARMNN_POST_TFLITE_2_3)
294 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
295 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
296#else
297 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
298#endif
299 return opcode;
300}
301
302std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
303 const TfLiteParserImpl::ModelPtr& model,
304 size_t bufferIndex)
305{
306 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
307 std::vector<unsigned int> buffer(info.GetNumElements());
308
309 if (info.GetDataType() == DataType::Signed32)
310 {
311 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
312 }
313 else if (info.GetDataType() == DataType::Signed64)
314 {
315 std::vector<uint64_t> uint64Buffer(info.GetNumElements());
316 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
317 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
318 }
319 return buffer;
320}
321
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( \
James Ward58dec6b2020-09-11 17:32:44 +0100348 fmt::format("TfLite parser doesn't suppport fused activation: " \
349 "{}/{} in {} subgraph:{} operator:{} at {}", \
350 OPTION->fused_activation_function, \
351 tflite::EnumNameActivationFunctionType(\
352 OPTION->fused_activation_function), \
353 __func__, \
354 SUBGRAPH_INDEX, \
355 OPERATOR_INDEX, \
356 CHECK_LOCATION().FileLine())); \
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
Kevin May7d96b162021-02-03 17:38:41 +0000405armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Finn Williamsb49ed182021-06-29 15:50:08 +0100406 const std::vector<unsigned int>& shape,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100407 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100408{
409 armnn::DataType type;
410 CHECK_TENSOR_PTR(tensorPtr);
411
412 switch (tensorPtr->type)
413 {
414 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000415 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100416 break;
417 case tflite::TensorType_FLOAT32:
418 type = armnn::DataType::Float32;
419 break;
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100420 case tflite::TensorType_FLOAT16:
421 type = armnn::DataType::Float16;
422 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000423 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000424 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000425 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000426 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000427 type = armnn::DataType::QAsymmS8;
428 }
429 else
430 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000431 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000432 type = armnn::DataType::QSymmS8;
433 }
Finn Williamsed66d142019-12-06 09:55:55 +0000434 break;
435 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000436 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000437 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100438 case tflite::TensorType_INT32:
439 type = armnn::DataType::Signed32;
440 break;
Inki Daed4619e22020-09-10 15:33:54 +0900441 case tflite::TensorType_INT64:
442 type = armnn::DataType::Signed64;
443 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100444 case tflite::TensorType_BOOL:
445 type = armnn::DataType::Boolean;
446 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100447 default:
448 {
449 CheckLocation location = CHECK_LOCATION();
450 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100451 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
452 tensorPtr->type,
453 tflite::EnumNameTensorType(tensorPtr->type),
454 tensorPtr->name,
455 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100456 }
457 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100458 TensorShape tensorShape;
459
460 std::vector<unsigned int> safeShape = shape;
461 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100462 {
463 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100464 }
465
466 if (!outputTensor)
467 {
468 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
469 }
470 else
471 {
Rob Hughesd812a312021-08-06 13:10:53 +0100472 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100473
474 // If a shape signature exists we will use that to infer dynamic tensors
475 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100476 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100477 // If the shape is incompatible with the shape signature override the shape
478 if (shapeSignatureSize != shape.size())
479 {
480 safeShape = {};
481
482 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
483 {
484 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
485 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
486 safeShape.push_back(dim);
487 }
488 }
489
Rob Hughesd812a312021-08-06 13:10:53 +0100490 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Finn Williamsb49ed182021-06-29 15:50:08 +0100491 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
492 {
493 dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false : true;
494 }
Rob Hughesd812a312021-08-06 13:10:53 +0100495 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100496 }
497 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
498 else if (shape.size() == 0)
499 {
500 tensorShape = TensorShape(1, false);
501 }
502 else
503 {
504 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100505 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100506 }
507
Keith Davisd305e1a2020-01-22 11:57:54 +0000508 float quantizationScale = 0.0f;
509 int32_t quantizationOffset = 0;
510
511 if (tensorPtr->quantization.get())
512 {
513 if (tensorPtr->quantization->scale.size() <= 1)
514 {
515 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
516 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
517
518 if (tensorPtr->quantization->scale.size() == 1)
519 {
520 quantizationScale = tensorPtr->quantization->scale[0];
521 }
522 if (tensorPtr->quantization->zero_point.size() == 1)
523 {
524 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000525 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100526 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000527 }
528
Sadik Armagand109a4d2020-07-28 10:42:13 +0100529 armnn::TensorInfo result(tensorShape,
530 type,
531 quantizationScale,
532 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000533 return result;
534 }
535 else
536 {
537 std::vector<float> quantizationScales;
538 std::vector<int32_t> quantizationOffsets;
539
540 // Scale
541 std::copy(tensorPtr->quantization->scale.begin(),
542 tensorPtr->quantization->scale.end(),
543 std::back_inserter(quantizationScales));
544
Keith Davis0c2eeac2020-02-11 16:51:50 +0000545 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100546 armnn::TensorInfo result(tensorShape,
547 type,
548 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100549 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000550 return result;
551 }
552 }
553 else
554 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100555 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000556 type,
557 quantizationScale,
558 quantizationOffset);
559 return result;
560 }
telsoa01c577f2c2018-08-31 09:22:23 +0100561}
562
Jan Eilers7612bd62021-04-06 17:29:03 +0100563armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr)
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000564{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000565 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100566 return ToTensorInfo(tensorPtr, dimensions);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000567}
568
Kevin May7d96b162021-02-03 17:38:41 +0000569armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100570 const bool outputTensor)
571{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000572 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100573 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100574}
575
telsoa01c577f2c2018-08-31 09:22:23 +0100576template<typename T>
577std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000578CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
579 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000580 armnn::TensorInfo& tensorInfo,
581 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100582{
Jan Eilers8eb25602020-03-09 12:13:48 +0000583 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100584 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
585 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100586 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100587
588 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000589
590 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
591 {
592 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000593 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
594 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000595 }
596 else
597 {
598 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
599 }
600
Matthew Sloyan81beae32021-07-13 19:46:11 +0100601 // Make sure isConstant flag is set.
602 tensorInfo.SetConstant();
603
telsoa01c577f2c2018-08-31 09:22:23 +0100604 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
605}
606
telsoa01c577f2c2018-08-31 09:22:23 +0100607armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
608{
609 // generate the binding id by shifting the tensor id by 8 bit
610 // and add the subgraph id, which allows 256 subgraphs
611 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
612}
613
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000614bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
615{
616 const unsigned int actualSize = actual.GetNumDimensions();
617 if (actualSize != expected.size())
618 {
619 return false;
620 }
621
622 for (unsigned int i = 0u; i < actualSize; i++)
623 {
624 if (expected[i] < 0 ||
625 actual[i] != static_cast<unsigned int>(expected[i]))
626 {
627 return false;
628 }
629 }
630
631 return true;
632}
633
James Conroy05102392020-06-24 15:39:55 +0100634void CheckMatchingQuantization(const TensorInfo& first,
635 const TensorInfo& second,
636 const std::string& descName,
637 std::string const& firstName,
638 std::string const& secondName)
639{
640 if (!first.IsQuantized() ||
641 !second.IsQuantized())
642 {
643 // Not a quantized type, ignore the validation
644 return;
645 }
646
647 DataType firstDataType = first.GetDataType();
648 DataType secondDataType = second.GetDataType();
649
650 if (firstDataType != secondDataType)
651 {
652 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
653 " must be of the same quantized type, " +
654 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
655 secondName + " is " + GetDataTypeName(secondDataType));
656 }
657
658 if (!first.IsTypeSpaceMatch(second))
659 {
660 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
661 " must have the same quantization space, " +
662 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
663 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
664 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
665 " and scale " + std::to_string(second.GetQuantizationScale()));
666 }
667}
668
telsoa01c577f2c2018-08-31 09:22:23 +0100669} // <anonymous>
670
Kevin May7d96b162021-02-03 17:38:41 +0000671TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100672: m_Options(options)
673, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000674, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100675{
676 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100677 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000678 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100679 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
680 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000681 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
682 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
mathad01b392e982021-04-07 12:07:30 +0100683 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000684 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
685 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100686 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
687 #if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100688 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100689 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000690 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
691 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
692 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
693 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100694 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000695 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300696 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000697 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100698 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000699 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000700 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
701 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin91a53ea2022-04-25 15:47:29 +0100702 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300703 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
704 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000705 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
706 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300707 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
708 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Mike Kelly31dce2b2021-09-01 21:22:37 +0100709 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
710 = &TfLiteParserImpl::ParseLocalResponseNormalization;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100711 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000712 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
713 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
714 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
715 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
716 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
717 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100718 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000719 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
720 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300721 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000722 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
723 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000724 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100725 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000726 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
727 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
728 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000729 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
730 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100731 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000732 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
733 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
734 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100735 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100736 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100737 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Kevin May7d96b162021-02-03 17:38:41 +0000738 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
739 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
740 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
741 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
742 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
743 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
744 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
745 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
746 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
747 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
748 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
749 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000750 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
751 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000752 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100753
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100754 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000755 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100756}
757
Kevin May7d96b162021-02-03 17:38:41 +0000758void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100759{
760 m_Network = armnn::INetworkPtr(nullptr, nullptr);
761 m_Model = nullptr;
762 m_SubgraphConnections.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000763 m_OverridenOutputShapes.clear();
764 m_ConstantsToDequantize.clear();
765 m_ConstantsToBeCreated.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100766}
767
Kevin May7d96b162021-02-03 17:38:41 +0000768INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100769{
770 ResetParser();
771 m_Model = LoadModelFromFile(graphFile);
772 return CreateNetworkFromModel();
773}
774
Mike Kelly0d77ae12022-01-07 17:42:27 +0000775INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100776{
777 ResetParser();
778 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
779 return CreateNetworkFromModel();
780}
781
Finn Williamsb49ed182021-06-29 15:50:08 +0100782
783armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
784{
785 ResetParser();
786 m_Model = std::move(model);
787
788 return CreateNetworkFromModel();
789}
790
Kevin May7d96b162021-02-03 17:38:41 +0000791INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100792{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100793
794 using NetworkOptions = std::vector<BackendOptions>;
795 NetworkOptions networkOptions = {};
796 if (m_Options && m_Options.value().m_InferAndValidate)
797 {
798 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
799 {
800 { "InferAndValidate", true }
801 });
802
803 networkOptions.push_back(shapeInferenceMethodOption);
804 }
805
806 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100807 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100808
telsoa01c577f2c2018-08-31 09:22:23 +0100809 if (m_Model->subgraphs.size() != 1)
810 {
811 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100812 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
813 m_Model->subgraphs.size(),
814 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100815 }
816
817 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100818 size_t operatorIndex = 0;
819 try
telsoa01c577f2c2018-08-31 09:22:23 +0100820 {
Colm Donelan6350d272020-06-09 16:56:25 +0100821 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100822 {
Colm Donelan6350d272020-06-09 16:56:25 +0100823 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
824 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100825 {
Colm Donelan6350d272020-06-09 16:56:25 +0100826 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100827
828// 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 +0100829#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100830 auto builtinCode = std::max(opCodePtr->builtin_code,
831 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
832#else
telsoa01c577f2c2018-08-31 09:22:23 +0100833 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100834#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100835
836 if (builtinCode > tflite::BuiltinOperator_MAX)
837 {
James Ward58dec6b2020-09-11 17:32:44 +0100838 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
839 "subgraph:{} operator idx:{}. {}",
840 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
841 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100842 }
843
844 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100845 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100846 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100847 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100848 }
telsoa01c577f2c2018-08-31 09:22:23 +0100849
Colm Donelan6350d272020-06-09 16:56:25 +0100850 SetupInputLayers(subgraphIndex);
851 SetupOutputLayers(subgraphIndex);
852 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100853
Colm Donelan6350d272020-06-09 16:56:25 +0100854 ++subgraphIndex;
855 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100856 }
telsoa01c577f2c2018-08-31 09:22:23 +0100857 }
Colm Donelan6350d272020-06-09 16:56:25 +0100858 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100859 {
Colm Donelan6350d272020-06-09 16:56:25 +0100860 std::stringstream errorString;
861 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
862 << subgraphIndex << " error: " << e.what();
863 ARMNN_LOG(error) << errorString.str();
864 std::stringstream errors;
865 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100866 throw ParseException(errors.str());
867 }
868
869 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100870 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100871 {
872 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
873 {
874 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
875 {
876 for (size_t inputSlotIdx = 0;
877 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
878 ++inputSlotIdx)
879 {
880 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
881 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
882 }
883 }
884 }
885 }
telsoa01c577f2c2018-08-31 09:22:23 +0100886 return std::move(m_Network);
887}
888
Mike Kelly5880b912022-01-28 16:18:54 +0000889std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
890 const TensorInfo& tensorInfo)
891{
892 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
893 tensorInfo.GetDataType() == DataType::QAsymmU8)
894 {
895 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
896
897 if (tensorInfo.HasPerAxisQuantization())
898 {
899 unsigned int axis = tensorInfo.GetQuantizationDim().value();
900 auto axisDimensionality = tensorInfo.GetShape()[axis];
901 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
902
903 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
904 {
905 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
906 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
907 tensorInfo.GetQuantizationOffset());
908 }
909 }
910 else
911 {
912 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
913 {
914 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
915 tensorInfo.GetQuantizationOffset());
916 }
917 }
918 return buffer;
919 }
920 throw ParseException(
921 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
922 GetDataTypeName(DataType::Float32),
923 GetDataTypeName(tensorInfo.GetDataType()),
924 CHECK_LOCATION().AsString()));
925}
926
Kevin May7d96b162021-02-03 17:38:41 +0000927void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
928 size_t tensorIndex,
929 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100930{
931 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100932 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
933 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100934
935 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
936
937 // assuming there is only one producer for that tensor
938 if (tensorSlots.outputSlot != nullptr)
939 {
James Ward58dec6b2020-09-11 17:32:44 +0100940 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
941 "subgraph:{} tensor:{} {}",
942 subgraphIndex,
943 tensorIndex,
944 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100945 }
946
947 tensorSlots.outputSlot = slot;
948}
949
Kevin May7d96b162021-02-03 17:38:41 +0000950void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
951 size_t tensorIndex,
952 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100953{
954 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100955 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
956 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100957
Finn Williamsd4fa5452021-03-01 12:31:41 +0000958 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100959 tensorSlots.inputSlots.push_back(slot);
960}
961
Kevin May7d96b162021-02-03 17:38:41 +0000962void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100963{
964 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
965
966 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000967 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100968
969 // Identify custom code defined for custom operator
970 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
971 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
972
973 // Find parser function that correspondes to custom code (if any)
974 auto iterator = m_CustomParserFunctions.find(customCode);
975 if (iterator != m_CustomParserFunctions.end())
976 {
977 customParserFunction = iterator->second;
978 }
979
980 // Run parser function
981 (this->*customParserFunction)(subgraphIndex, operatorIndex);
982}
983
Kevin May7d96b162021-02-03 17:38:41 +0000984void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100985{
986 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100987
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100988 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
989
990 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +0100991
992// 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 +0100993#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100994 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
995 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
996#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100997 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100998#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100999
1000 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1001 {
1002 // Do not add StandInLayer, throw ParseException instead
1003 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001004 fmt::format("Operator not supported. "
1005 "subgraph:{} operator:{} "
1006 "opcode_index:{} opcode:{} / {} {}",
1007 subgraphIndex,
1008 operatorIndex,
1009 opcodeIndex,
1010 opcode,
1011 tflite::EnumNameBuiltinOperator(opcode),
1012 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001013 }
1014
1015 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1016 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1017
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001018 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1019 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001020
1021 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001022 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001023
1024 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1025 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001026 ARMNN_ASSERT(layer != nullptr);
1027
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001028 for (unsigned int i = 0u; i < numOutputs; ++i)
1029 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001030 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001031 }
1032
1033 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1034 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1035
1036 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1037 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001038}
1039
mathad01b392e982021-04-07 12:07:30 +01001040void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1041{
1042 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1043
1044 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1045 CHECK_VALID_SIZE(inputs.size(), 1);
1046 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1047 CHECK_VALID_SIZE(outputs.size(), 1);
1048
1049 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1050
1051 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1052 ARMNN_ASSERT(layer != nullptr);
1053
1054 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1055 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1056
1057 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1058 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1059
1060 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1061 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1062}
1063
Kevin May7d96b162021-02-03 17:38:41 +00001064void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001065{
1066 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1067
Mike Kelly0d77ae12022-01-07 17:42:27 +00001068 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1069 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001070
1071 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1072
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001073 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1074 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1075 CHECK_VALID_SIZE(outputs.size(), 1);
1076
telsoa01c577f2c2018-08-31 09:22:23 +01001077 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001078 inputs.size() == 3 ?
1079 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001080 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1081 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001082 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001083 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1084 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001085
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001086 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
telsoa01c577f2c2018-08-31 09:22:23 +01001087 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1088
1089 // assuming input is NHWC
1090 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001091 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001092
1093 // assuming the filter is OHWI : Output, H, W, Input
1094 // which is essentially the same as NHWC
1095 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001096 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001097
Pablo Tellof0bd6832019-04-26 17:58:13 +01001098 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1099 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1100 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1101 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001102
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001103 // Add the first input and weights tensor to the registration list.
1104 // The constant weights will be added by SetupConstantLayers.
1105 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1106 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001107
James Ward58dec6b2020-09-11 17:32:44 +01001108 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001109 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001110
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001111 if (IsConstTensor(inputs[1]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1112 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1113 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
telsoa01c577f2c2018-08-31 09:22:23 +01001114 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001115 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001116 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001117
1118 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001119 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001120 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1121
1122 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1123 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1124
1125 if (IsConstTensor(inputs[2]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1126 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1127 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
1128 {
1129 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1130 }
telsoa01c577f2c2018-08-31 09:22:23 +01001131 }
1132
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001133 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001134
Sadik Armagand109a4d2020-07-28 10:42:13 +01001135 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001136 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001137
1138 // register the input connection slots for the layer, connections are made after all layers have been created
1139 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001140 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001141
jimfly01c25411c2018-11-14 17:47:22 +00001142 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001143 // register the output connection slots for the layer, connections are made after all layers have been created
1144 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001145 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001146}
1147
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001148// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
1149#if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001150void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1151{
1152 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1153
1154 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1155 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1156
1157 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1158
1159 Convolution3dDescriptor desc;
1160 desc.m_BiasEnabled = false;
1161 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1162 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1163 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1164 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1165 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1166 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1167 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1168
1169 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1170 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1171
1172 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1173 CHECK_VALID_SIZE(outputs.size(), 1);
1174
1175 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1176 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1177
1178 // Assuming input is NDHWC
1179 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1180 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1181 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1182
1183 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1184 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1185 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1186 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1187
1188 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001189 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001190 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1191 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1192 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1193 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1194
Mike Kelly5880b912022-01-28 16:18:54 +00001195 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001196
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001197 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1198
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001199 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1200 // Add the first input and weights tensor to the registration list.
1201 // The constant weights will be added by SetupConstantLayers.
1202 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1203
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001204 if (inputs.size() == 3)
1205 {
1206 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001207
1208 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1209 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001210 }
1211
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001212 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001213 ARMNN_ASSERT(layer != nullptr);
1214
1215 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1216 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1217
1218 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001219 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001220
1221 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1222 // Register the output connection slots for the layer, connections are made after all layers have been created
1223 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1224 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1225}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001226#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001227
Kevin May7d96b162021-02-03 17:38:41 +00001228void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001229{
1230 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1231
Mike Kelly0d77ae12022-01-07 17:42:27 +00001232 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1233 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001234
1235 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1236
1237 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001238 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1239 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001240 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001241 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001242
1243 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1244 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001245 if (inputs.size() == 3)
1246 {
1247 desc.m_BiasEnabled = true;
1248 }
1249
telsoa01c577f2c2018-08-31 09:22:23 +01001250 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1251 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001252 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1253 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001254
telsoa01c577f2c2018-08-31 09:22:23 +01001255 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001256 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001257
Matteo Martincigh747ef822018-12-18 09:26:39 +00001258 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001259 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1260 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001261
1262 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001263 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1264 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1265
Pablo Tellof0bd6832019-04-26 17:58:13 +01001266 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1267 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1268 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1269 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001270
Jan Eilers53ef7952021-06-02 12:01:25 +01001271 // 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 +01001272 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001273
Cathal Corbett06902652022-04-14 17:55:11 +01001274 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1275 // Add the first input and weights tensor to the registration list.
1276 // The constant weights will be added by SetupConstantLayers.
1277 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1278
1279 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1280
1281 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001282 {
1283 desc.m_BiasEnabled = true;
1284 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001285
1286 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1287 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001288 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001289 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001290
Sadik Armagand109a4d2020-07-28 10:42:13 +01001291 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001292 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001293
1294 // register the input connection slots for the layer, connections are made after all layers have been created
1295 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001296 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001297
jimfly01c25411c2018-11-14 17:47:22 +00001298 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001299 // register the output connection slots for the layer, connections are made after all layers have been created
1300 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1301 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1302}
1303
Kevin May7d96b162021-02-03 17:38:41 +00001304void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001305{
1306 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1307
1308 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1309 CHECK_VALID_SIZE(inputs.size(), 1);
1310
1311 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1312 CHECK_VALID_SIZE(outputs.size(), 1);
1313
James Ward58dec6b2020-09-11 17:32:44 +01001314 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001315
1316 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001317 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001318
Sadik Armagand109a4d2020-07-28 10:42:13 +01001319 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001320 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1321
1322 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1323 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1324
1325 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1326 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1327}
1328
Teresa Charlin3ab85482021-06-08 16:59:29 +01001329void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1330{
1331 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1332
1333 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1334 CHECK_VALID_SIZE(inputs.size(), 2);
1335
1336 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1337 CHECK_VALID_SIZE(outputs.size(), 1);
1338
1339 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1340
1341 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1342 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1343
1344 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1345
1346 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001347
1348 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1349 {
1350 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1351 }
1352 else
1353 {
1354 int32_t axis = inputs[1]->shape[0];
1355
1356 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1357
1358 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1359 {
1360 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1361 }
1362
1363 if(axis < 0)
1364 {
1365 axis = inputDimSize + axis + 1;
1366 }
1367
Rob Hughesd812a312021-08-06 13:10:53 +01001368 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001369 unsigned int inputShapeIndex = 0;
1370 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1371 {
1372 if (i == static_cast<unsigned int>(axis))
1373 {
1374 shape[i] = 1;
1375 }
1376 else
1377 {
1378 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1379 ++inputShapeIndex;
1380 }
1381 }
1382
Rob Hughesd812a312021-08-06 13:10:53 +01001383 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001384 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001385
1386 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1387 ARMNN_ASSERT(layer != nullptr);
1388 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1389
1390 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1391 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1392
1393 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1394 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1395}
1396
Kevin May7d96b162021-02-03 17:38:41 +00001397void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001398{
1399 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1400
1401 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001402 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001403
1404 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1405 CHECK_VALID_SIZE(outputs.size(), 1);
1406
James Ward58dec6b2020-09-11 17:32:44 +01001407 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001408 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001409
josh minorba424d22019-11-13 10:55:17 -06001410 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001411 {
1412 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1413 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001414 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1415 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001416 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001417 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001418
Mike Kelly08759e22020-03-02 11:41:31 +00001419 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001420 }
1421
James Conroy05102392020-06-24 15:39:55 +01001422 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001423 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001424 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001425
James Conroy05102392020-06-24 15:39:55 +01001426 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001427 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001428 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1429
1430 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1431 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1432
1433 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1434 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1435}
1436
Kevin May7d96b162021-02-03 17:38:41 +00001437void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001438{
1439 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1440
Mike Kelly0d77ae12022-01-07 17:42:27 +00001441 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1442 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001443
1444 TransposeConvolution2dDescriptor desc;
1445 desc.m_BiasEnabled = false;
1446 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1447 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1448 desc.m_DataLayout = armnn::DataLayout::NHWC;
1449
1450 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001451 if (inputs.size() == 4)
1452 {
1453 desc.m_BiasEnabled = true;
1454 }
1455 else
1456 {
1457 CHECK_VALID_SIZE(inputs.size(), 3);
1458 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001459
1460 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1461 CHECK_VALID_SIZE(outputs.size(), 1);
1462
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001463 if (inputs[0])
1464 {
1465 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1466 std::vector<int> output_shape(tensorInfo.GetNumElements());
1467 if (tensorInfo.GetDataType() == DataType::Signed32)
1468 {
1469 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1470 }
1471 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1472 {
1473 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1474 {
1475 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1476 }
1477 }
1478 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1479 for (int dimension : output_shape)
1480 {
1481 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1482 }
1483 desc.m_OutputShapeEnabled = true;
1484 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001485 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001486 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1487
1488 // TfLite uses NHWC tensors
1489 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1490 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1491
1492 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1493 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1494
1495 CalcPadding(inputHeight,
1496 filterHeight,
1497 desc.m_StrideY,
1498 1, // DilationY
1499 desc.m_PadTop,
1500 desc.m_PadBottom,
1501 options->padding);
1502
1503 CalcPadding(inputWidth,
1504 filterWidth,
1505 desc.m_StrideX,
1506 1, // DilationX
1507 desc.m_PadLeft,
1508 desc.m_PadRight,
1509 options->padding);
1510
Mike Kelly5880b912022-01-28 16:18:54 +00001511 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001512
1513 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001514 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001515
David Monahan61683802021-01-12 09:11:07 +00001516 if (desc.m_BiasEnabled)
1517 {
1518 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001519 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001520 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001521 filterTensorAndData.first,
1522 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001523 layerName.c_str());
1524 }
1525 else
1526 {
1527 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001528 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001529 EmptyOptional(),
1530 layerName.c_str());
1531 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001532
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001533 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001534
Sadik Armagand109a4d2020-07-28 10:42:13 +01001535 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001536 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1537
1538 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1539 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001540 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001541
1542 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1543 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1544}
1545
Kevin May7d96b162021-02-03 17:38:41 +00001546void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001547{
1548 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1549}
1550
Kevin May7d96b162021-02-03 17:38:41 +00001551void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001552{
1553 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1554
1555 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1556 CHECK_VALID_SIZE(inputs.size(), 3);
1557
1558 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1559 CHECK_VALID_SIZE(outputs.size(), 1);
1560
1561 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1562 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1563
1564 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1565 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1566
1567 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1568 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1569
1570 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1571 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1572
1573 size_t step = 2;
1574 std::vector<std::pair<unsigned int, unsigned int>> crops;
1575 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1576 {
1577 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1578 }
1579
1580 armnn::BatchToSpaceNdDescriptor desc;
1581 desc.m_BlockShape = blockShape;
1582 desc.m_Crops = crops;
1583 desc.m_DataLayout = armnn::DataLayout::NHWC;
1584
James Ward58dec6b2020-09-11 17:32:44 +01001585 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001586
James Conroy05102392020-06-24 15:39:55 +01001587 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001588 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001589 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1590
1591 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1592 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001593 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1594
1595 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1596 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1597
1598 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1599 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1600}
1601
Kevin May7d96b162021-02-03 17:38:41 +00001602void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001603{
1604 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1605
1606 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1607 CHECK_VALID_SIZE(inputs.size(), 1);
1608
1609 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1610 CHECK_VALID_SIZE(outputs.size(), 1);
1611
1612 L2NormalizationDescriptor desc;
1613 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001614 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001615 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1616
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001617 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001618
Sadik Armagand109a4d2020-07-28 10:42:13 +01001619 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001620 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1621
1622 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1623 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1624
1625 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1626 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1627}
1628
Kevin May7d96b162021-02-03 17:38:41 +00001629void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001630{
1631 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1632}
1633
Kevin May7d96b162021-02-03 17:38:41 +00001634void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001635{
1636 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1637
1638 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1639 CHECK_VALID_SIZE(inputs.size(), 2);
1640
1641 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1642 CHECK_VALID_SIZE(outputs.size(), 1);
1643
James Ward58dec6b2020-09-11 17:32:44 +01001644 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001645
1646 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1647 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1648 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001649
Sadik Armagand109a4d2020-07-28 10:42:13 +01001650 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001651 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1652
1653 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1654 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001655 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1656
1657 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001658 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001659
1660 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1661 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1662}
1663
Kevin May7d96b162021-02-03 17:38:41 +00001664void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001665{
1666 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1667
1668 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1669 CHECK_VALID_SIZE(inputs.size(), 2);
1670
1671 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1672 CHECK_VALID_SIZE(outputs.size(), 1);
1673
James Ward58dec6b2020-09-11 17:32:44 +01001674 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001675
1676 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1677 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1678 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001679
Sadik Armagand109a4d2020-07-28 10:42:13 +01001680 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001681 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1682
1683 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1684 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001685 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1686
1687 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001688 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001689
1690 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1691 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1692}
1693
Kevin May7d96b162021-02-03 17:38:41 +00001694void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1695 size_t operatorIndex,
1696 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001697{
1698 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1699
Mike Kelly0d77ae12022-01-07 17:42:27 +00001700 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1701 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001702
1703 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1704
1705 std::string layerName;
1706
1707 switch (algorithm)
1708 {
1709 case PoolingAlgorithm::Average:
1710 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001711 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001712 break;
1713 case PoolingAlgorithm::Max:
1714 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001715 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001716 break;
1717 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001718 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001719 }
1720
1721 Pooling2dDescriptor desc;
1722
1723 desc.m_PoolType = algorithm;
1724 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1725 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1726 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1727 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1728 desc.m_PaddingMethod = PaddingMethod::Exclude;
1729 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001730 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001731
1732 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1733 CHECK_VALID_SIZE(inputs.size(), 1);
1734 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1735
1736 // assuming input is NHWC
1737 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1738 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1739
Pablo Tellof0bd6832019-04-26 17:58:13 +01001740 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1741 desc.m_PadTop, desc.m_PadBottom, options->padding);
1742 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1743 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001744
1745 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1746 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001747
Sadik Armagand109a4d2020-07-28 10:42:13 +01001748 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001749 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1750
1751 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1752 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001753 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001754
1755 // register the input connection slots for the layer, connections are made after all layers have been created
1756 // only the tensors for the inputs are relevant, exclude the const tensors
1757 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001758 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001759
jimfly01c25411c2018-11-14 17:47:22 +00001760 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001761 // register the output connection slots for the layer, connections are made after all layers have been created
1762 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1763 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1764}
1765
Kevin May7d96b162021-02-03 17:38:41 +00001766void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001767{
1768 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1769
1770 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1771 CHECK_VALID_SIZE(inputs.size(), 3);
1772 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1773 CHECK_VALID_SIZE(outputs.size(), 1);
1774
1775 SliceDescriptor desc;
1776
1777 // set begin tensor info for slice descriptor
1778 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1779 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1780
1781 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1782 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1783
1784 // set size tensor info for slice descriptor
1785 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1786 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1787
Mike Kelly7ba84d62021-09-10 15:27:19 +01001788 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1789 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001790 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001791 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1792
1793 for (unsigned int i = 0; i < signedSize.size(); ++i)
1794 {
1795 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001796
Mike Kelly7ba84d62021-09-10 15:27:19 +01001797 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1798 {
1799 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1800 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1801 signedValue,
1802 inputTensorInfo.GetShape()[i] - begin[i],
1803 CHECK_LOCATION().AsString()));
1804 }
1805
1806 if (signedValue == -1)
1807 {
1808 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1809 }
1810 else
1811 {
1812 size[i] = static_cast<unsigned int>(signedValue);
1813 }
1814 }
1815
josh minorba424d22019-11-13 10:55:17 -06001816 desc = SliceDescriptor(begin, size);
1817
James Ward58dec6b2020-09-11 17:32:44 +01001818 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001819
Sadik Armagand109a4d2020-07-28 10:42:13 +01001820 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001821 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1822
1823 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001824 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1825
1826 // register the input connection slots for the layer, connections are made after all layers have been created
1827 // only the tensors for the inputs are relevant, exclude the const tensors
1828 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1829 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1830
1831 // register the output connection slots for the layer, connections are made after all layers have been created
1832 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1833 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1834}
1835
Kevin May7d96b162021-02-03 17:38:41 +00001836void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001837{
1838 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001839 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1840 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001841
1842 SoftmaxDescriptor desc;
1843 desc.m_Beta = options->beta;
1844
1845 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1846 CHECK_VALID_SIZE(inputs.size(), 1);
1847 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1848 CHECK_VALID_SIZE(outputs.size(), 1);
1849
James Ward58dec6b2020-09-11 17:32:44 +01001850 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001851 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1852
Sadik Armagand109a4d2020-07-28 10:42:13 +01001853 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001854 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1855
1856 // register the input connection slots for the layer, connections are made after all layers have been created
1857 // only the tensors for the inputs are relevant, exclude the const tensors
1858 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1859 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1860
1861 // register the output connection slots for the layer, connections are made after all layers have been created
1862 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1863 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1864}
1865
Kevin May7d96b162021-02-03 17:38:41 +00001866void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001867{
1868 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1869
1870 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1871 CHECK_VALID_SIZE(inputs.size(), 3);
1872
1873 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1874 CHECK_VALID_SIZE(outputs.size(), 1);
1875
1876 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1877 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1878
1879 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1880 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1881
1882 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1883 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1884
1885 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1886 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1887
1888 size_t step = 2;
1889 std::vector<std::pair<unsigned int, unsigned int>> padList;
1890 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1891 {
1892 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1893 }
1894
1895 armnn::SpaceToBatchNdDescriptor desc;
1896 desc.m_BlockShape = blockShape;
1897 desc.m_PadList = padList;
1898 desc.m_DataLayout = armnn::DataLayout::NHWC;
1899
James Ward58dec6b2020-09-11 17:32:44 +01001900 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001901
James Conroy05102392020-06-24 15:39:55 +01001902 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001903 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001904 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1905
1906 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1907 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001908 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1909
1910 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1911 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1912
1913 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1914 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1915}
1916
Teresa Charlin3ab85482021-06-08 16:59:29 +01001917armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00001918 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001919{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001920 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001921 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1922
1923 if (inputTensorInfo.GetNumDimensions() > 4)
1924 {
1925 std::stringstream ss;
1926 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1927 << " shape:" << inputTensorInfo.GetShape() << " "
1928 << CHECK_LOCATION().AsString();
1929 throw ParseException(ss.str());
1930 }
1931
1932 if (squeezeDims.empty())
1933 {
1934 squeezeDims.assign(dimensionSequence,
1935 dimensionSequence+inputTensorInfo.GetNumDimensions());
1936 }
1937
1938 std::vector<uint32_t> outputDims;
1939 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1940 {
1941 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1942 auto currentDimension = inputTensorInfo.GetShape()[i];
1943 if (skipSqueeze || currentDimension != 1)
1944 {
1945 outputDims.push_back(currentDimension);
1946 }
1947 }
1948
1949 if (outputDims.size() > 4)
1950 {
1951 std::stringstream ss;
1952 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1953 << " shape:" << inputTensorInfo.GetShape() << " "
1954 << CHECK_LOCATION().AsString();
1955 throw ParseException(ss.str());
1956 }
1957
1958 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1959 outputDims.data());
1960
1961 // we need to preserve the tensor type and the quantization data as well
1962 TensorInfo outTensorInfo = inputTensorInfo;
1963 outTensorInfo.SetShape(outShape);
1964
1965 return outTensorInfo;
1966}
1967
Keith Davis0176fd82021-06-01 17:36:32 +01001968void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
1969{
1970 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1971
1972 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1973 CHECK_VALID_SIZE(inputs.size(), 1);
1974 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1975 CHECK_VALID_SIZE(outputs.size(), 1);
1976
1977 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
1978
1979 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
1980 ARMNN_ASSERT(layer != nullptr);
1981
1982
1983 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1984 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1985
1986 // Check if output tensor type is Signed32 or Signed64
1987 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
1988 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
1989 {
1990 throw ParseException(
1991 fmt::format(
1992 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
1993 CHECK_LOCATION().AsString()));
1994 }
1995
1996 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1997 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1998
1999 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2000 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2001}
2002
Kevin May7d96b162021-02-03 17:38:41 +00002003void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002004{
2005 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2006
2007 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2008 CHECK_VALID_SIZE(inputs.size(), 1);
2009
2010 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2011 CHECK_VALID_SIZE(outputs.size(), 1);
2012
2013 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2014 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002015 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002016
2017 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002018
2019 std::vector<uint32_t> squeezeDim;
2020 // A single negative dim index is interpreted as a negative index in python
2021 // Meaning the index will be the shape size plus the negative index value
2022 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2023 {
2024 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2025 squeezeDim.push_back(static_cast<uint32_t>(dim));
2026 }
2027 else
2028 {
2029 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2030 }
2031
2032 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2033
James Conroy05102392020-06-24 15:39:55 +01002034 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002035
2036 ReshapeDescriptor reshapeDesc;
2037 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2038
telsoa01c577f2c2018-08-31 09:22:23 +01002039 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002040 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002041 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2042
2043 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2044 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2045
2046 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2047 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2048}
2049
Kevin May7d96b162021-02-03 17:38:41 +00002050void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002051{
2052 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2053
2054 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2055 CHECK_VALID_SIZE(inputs.size(), 4);
2056
2057 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2058 CHECK_VALID_SIZE(outputs.size(), 1);
2059
Mike Kelly0d77ae12022-01-07 17:42:27 +00002060 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2061 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002062
2063 StridedSliceDescriptor desc;
2064 desc.m_BeginMask = options->begin_mask;
2065 desc.m_EllipsisMask = options->ellipsis_mask;
2066 desc.m_EndMask = options->end_mask;
2067 desc.m_NewAxisMask = options->new_axis_mask;
2068 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2069 desc.m_DataLayout = armnn::DataLayout::NHWC;
2070
2071 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2072 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2073
2074 std::vector<int> begin(beginTensorInfo.GetNumElements());
2075 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2076
2077 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2078 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2079
2080 std::vector<int> end(endTensorInfo.GetNumElements());
2081 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2082
2083 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2084 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2085
2086 std::vector<int> stride(strideTensorInfo.GetNumElements());
2087 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2088
2089 desc.m_Begin = begin;
2090 desc.m_End = end;
2091 desc.m_Stride = stride;
2092
James Ward58dec6b2020-09-11 17:32:44 +01002093 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002094 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002095 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002096
Sadik Armagand109a4d2020-07-28 10:42:13 +01002097 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002098 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2099
2100 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2101 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2102
2103 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2104 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2105}
2106
Kevin May7d96b162021-02-03 17:38:41 +00002107void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002108{
2109 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2110
Mike Kelly0d77ae12022-01-07 17:42:27 +00002111 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2112 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002113
2114 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2115 CHECK_VALID_SIZE(inputs.size(), 2);
2116
2117 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2118 CHECK_VALID_SIZE(outputs.size(), 1);
2119
2120 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2121 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2122
James Ward58dec6b2020-09-11 17:32:44 +01002123 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002124 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002125 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002126
Sadik Armagand109a4d2020-07-28 10:42:13 +01002127 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002128 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2129
2130 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002131 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002132
2133 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2134
2135 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2136 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2137}
2138
Kevin May7d96b162021-02-03 17:38:41 +00002139void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302140{
2141 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2142
Mike Kelly0d77ae12022-01-07 17:42:27 +00002143 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2144 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302145
2146 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2147 CHECK_VALID_SIZE(inputs.size(), 2);
2148
2149 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2150 CHECK_VALID_SIZE(outputs.size(), 1);
2151
2152 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2153 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2154
James Ward58dec6b2020-09-11 17:32:44 +01002155 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302156 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002157 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302158
Sadik Armagand109a4d2020-07-28 10:42:13 +01002159 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302160 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2161
2162 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002163 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302164 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2165
2166 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2167 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2168}
2169
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002170void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2171{
2172 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2173
2174 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2175 CHECK_VALID_SIZE(inputs.size(), 2);
2176
2177 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2178 CHECK_VALID_SIZE(outputs.size(), 1);
2179
2180 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2181 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2182
2183 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2184 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2185 ARMNN_ASSERT(layer != nullptr);
2186
2187 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2188 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2189
2190 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2191 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2192 layer = AddFusedFloorLayer(layer, 0);
2193
2194 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2195 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2196}
2197
Kevin May7d96b162021-02-03 17:38:41 +00002198void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002199{
2200 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2201
Mike Kelly0d77ae12022-01-07 17:42:27 +00002202 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2203 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002204
2205 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2206 CHECK_VALID_SIZE(inputs.size(), 2);
2207
2208 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2209 CHECK_VALID_SIZE(outputs.size(), 1);
2210
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002211 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2212 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2213
James Ward58dec6b2020-09-11 17:32:44 +01002214 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002215 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002216 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002217
Sadik Armagand109a4d2020-07-28 10:42:13 +01002218 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002219 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2220
2221 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002222 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002223 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2224
2225 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2226 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2227}
2228
Kevin May7d96b162021-02-03 17:38:41 +00002229void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002230{
2231 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2232
Mike Kelly0d77ae12022-01-07 17:42:27 +00002233 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2234 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002235
2236 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2237 CHECK_VALID_SIZE(inputs.size(), 2);
2238
2239 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2240 CHECK_VALID_SIZE(outputs.size(), 1);
2241
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002242 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2243 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2244
James Ward58dec6b2020-09-11 17:32:44 +01002245 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002246 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002247 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002248
Sadik Armagand109a4d2020-07-28 10:42:13 +01002249 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002250 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2251
2252 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002253 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002254 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2255
2256 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2257 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2258}
2259
Kevin May7d96b162021-02-03 17:38:41 +00002260void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002261{
2262 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2263
2264 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2265
2266 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2267 CHECK_VALID_SIZE(outputs.size(), 1);
2268
2269 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2270 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2271
2272 armnn::MeanDescriptor desc;
2273 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2274 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2275 desc.m_Axis = axis;
2276
2277 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002278 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002279
2280 desc.m_KeepDims =
2281 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2282 true : false;
2283
James Ward58dec6b2020-09-11 17:32:44 +01002284 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002285 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002286 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002287
2288 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2289
2290 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2291 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2292
2293 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2294 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2295}
2296
Kevin May7d96b162021-02-03 17:38:41 +00002297void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002298{
2299 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2300
Kevin May7d96b162021-02-03 17:38:41 +00002301 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002302
Kevin May7d96b162021-02-03 17:38:41 +00002303 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002304 CHECK_VALID_SIZE(outputs.size(), 1);
2305
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002306 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002307 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002308
Mike Kelly0d77ae12022-01-07 17:42:27 +00002309 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002310
2311 size_t step = 2;
2312 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002313 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2314
2315 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002316 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002317 CHECK_VALID_SIZE(inputs.size(), 2);
2318
2319 if (inputTensorInfo.IsQuantized())
2320 {
2321 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2322 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002323 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002324 else if (opcode == tflite::BuiltinOperator_PADV2)
2325 {
2326 CHECK_VALID_SIZE(inputs.size(), 3);
2327
2328 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2329
2330 if (padValueTensorInfo.GetNumElements() != 1)
2331 {
2332 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2333 }
2334 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2335
2336 // Get the pad value from the input tensor
2337 if (padValueBufferPtr->data.size() > 0)
2338 {
2339 switch (padValueTensorInfo.GetDataType())
2340 {
2341 case armnn::DataType::Float32:
2342 {
2343 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2344 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2345 desc.m_PadValue = padValueBuffer[0];
2346 break;
2347 }
2348 case armnn::DataType::QAsymmU8:
2349 {
2350 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2351 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2352 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2353 padValueTensorInfo.GetQuantizationScale(),
2354 padValueTensorInfo.GetQuantizationOffset());
2355 break;
2356 }
2357 case armnn::DataType::QAsymmS8:
2358 case armnn::DataType::QSymmS8:
2359 {
2360 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2361 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2362 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2363 padValueTensorInfo.GetQuantizationScale(),
2364 padValueTensorInfo.GetQuantizationOffset());
2365 break;
2366 }
2367 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2368 }
2369 }
2370 else if (inputTensorInfo.IsQuantized())
2371 {
2372 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2373 }
2374 }
2375
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002376 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2377 {
2378 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2379 }
2380
Mike Kelly0d77ae12022-01-07 17:42:27 +00002381 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2382 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002383 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002384
2385 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2386 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002387 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2388
2389 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2390 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2391
2392 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2393 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2394}
2395
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002396void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2397{
2398 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2399
2400 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2401 CHECK_VALID_SIZE(inputs.size(), 2);
2402
2403 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2404 CHECK_VALID_SIZE(outputs.size(), 1);
2405
2406 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2407
2408 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2409 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2410
2411 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2412 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2413
2414 size_t step = 2;
2415 armnn::PadDescriptor desc;
2416 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2417 {
2418 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2419 }
2420
2421 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2422 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2423
2424 if (options->mode == tflite::MirrorPadMode_REFLECT)
2425 {
2426 desc.m_PaddingMode = PaddingMode::Reflect;
2427 }
2428 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2429 {
2430 desc.m_PaddingMode = PaddingMode::Symmetric;
2431 }
2432 else
2433 {
2434 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2435 }
2436
2437 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2438 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2439 auto inputShape = inputTensorInfo.GetShape();
2440 auto padList = desc.m_PadList;
2441
2442 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2443 for(unsigned int i = 0; i < padList.size(); ++i)
2444 {
2445 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2446 padList.at(i).second > (inputShape[i] - isReflect))
2447 {
2448 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2449 "equal (Symmetric) to the dimension size.");
2450 }
2451 }
2452
2453 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2454 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2455
2456 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2457 ARMNN_ASSERT(layer != nullptr);
2458 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2459
2460 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2461 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2462
2463 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2464 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2465}
2466
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002467void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2468{
2469 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2470
2471 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2472 CHECK_VALID_SIZE(inputs.size(), 2);
2473
2474 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2475 CHECK_VALID_SIZE(outputs.size(), 1);
2476
2477 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2478
2479 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2480 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2481 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2482 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2483
2484 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2485 ARMNN_ASSERT(layer != nullptr);
2486 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2487
2488 if (IsConstTensor(inputs[1]))
2489 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002490 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002491 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2492 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002493
Mike Kelly5880b912022-01-28 16:18:54 +00002494 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2495 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002496 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2497 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002498 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002499 ARMNN_ASSERT(constLayer != nullptr);
2500
2501 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2502 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2503 RegisterOutputSlots(subgraphIndex,
2504 VIRTUAL_OPERATOR_ID,
2505 constLayer,
2506 { inputTensorIndexes[1] });
2507 }
2508 else
2509 {
2510 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2511 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2512 }
2513
2514 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2515 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2516}
2517
Kevin May7d96b162021-02-03 17:38:41 +00002518void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002519{
2520 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2521
2522 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2523 CHECK_VALID_SIZE(inputs.size(), 1);
2524
2525 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2526 CHECK_VALID_SIZE(outputs.size(), 1);
2527
James Ward58dec6b2020-09-11 17:32:44 +01002528 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002529
2530 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002531 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002532
Sadik Armagand109a4d2020-07-28 10:42:13 +01002533 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002534 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2535
2536 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2537 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2538
2539 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2540 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2541}
Finn Williamsc42c3842019-01-22 14:18:11 +00002542
Kevin May7d96b162021-02-03 17:38:41 +00002543void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002544{
Finn Williamsc42c3842019-01-22 14:18:11 +00002545 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002546}
2547
Kevin May7d96b162021-02-03 17:38:41 +00002548void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002549{
Finn Williamsc42c3842019-01-22 14:18:11 +00002550 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2551}
Sadik Armagan58f39192018-09-17 14:14:39 +01002552
Kevin May7d96b162021-02-03 17:38:41 +00002553void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002554{
Jan Eilers2f746b32020-07-28 14:00:06 +01002555 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002556}
2557
Kevin May7d96b162021-02-03 17:38:41 +00002558void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002559{
2560 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2561}
2562
Kevin May7d96b162021-02-03 17:38:41 +00002563void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002564{
2565 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2566}
2567
Kevin May7d96b162021-02-03 17:38:41 +00002568void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002569{
2570 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2571}
2572
Kevin May7d96b162021-02-03 17:38:41 +00002573void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002574{
2575 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2576}
Finn Williamsc42c3842019-01-22 14:18:11 +00002577
Kevin May7d96b162021-02-03 17:38:41 +00002578void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002579{
2580 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002581 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002582 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002583
2584 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2585 CHECK_VALID_SIZE(inputs.size(), 1);
2586
2587 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2588 CHECK_VALID_SIZE(outputs.size(), 1);
2589
James Ward58dec6b2020-09-11 17:32:44 +01002590 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002591 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002592 activationDesc.m_Function = activationType;
2593
2594 switch (activationType)
2595 {
2596 case ActivationFunction::ReLu:
2597 {
James Ward58dec6b2020-09-11 17:32:44 +01002598 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002599 break;
2600 }
2601 case ActivationFunction::BoundedReLu:
2602 {
James Ward58dec6b2020-09-11 17:32:44 +01002603 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002604 activationDesc.m_A = 6.0f;
2605 activationDesc.m_B = 0.0f;
2606 break;
2607 }
2608 case ActivationFunction::Sigmoid:
2609 {
James Ward58dec6b2020-09-11 17:32:44 +01002610 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002611 break;
2612 }
Nina Drozd99851762019-04-09 09:37:38 +01002613 case ActivationFunction::TanH:
2614 {
James Ward58dec6b2020-09-11 17:32:44 +01002615 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002616 activationDesc.m_A = 1.0f;
2617 activationDesc.m_B = 1.0f;
2618 break;
2619 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002620 case ActivationFunction::LeakyReLu:
2621 {
James Ward58dec6b2020-09-11 17:32:44 +01002622 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002623 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002624 activationDesc.m_A = options->alpha;
2625 break;
2626 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002627 case ActivationFunction::Elu:
2628 {
2629 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2630 activationDesc.m_A = 1.0f;
2631 break;
2632 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002633 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002634 {
James Ward58dec6b2020-09-11 17:32:44 +01002635 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002636 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002637 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002638 default:
2639 {
2640 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002641 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2642 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002643 }
2644 }
2645
2646 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002647
Sadik Armagand109a4d2020-07-28 10:42:13 +01002648 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002649 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2650
2651 // register the input connection slots for the layer, connections are made after all layers have been created
2652 // only the tensors for the inputs are relevant, exclude the const tensors
2653 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2654 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2655
2656 // register the output connection slots for the layer, connections are made after all layers have been created
2657 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2658 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2659}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002660armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2661 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002662{
2663 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2664 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2665
2666 if (stretchDim != targetDimsIn.end())
2667 {
2668 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2669 {
2670 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002671 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002672 }
2673
2674 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002675 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002676 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2677
2678 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2679 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2680 }
2681
2682 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2683
2684 TensorInfo reshapeInfo = inputTensorInfo;
2685 reshapeInfo.SetShape(outputShape);
2686
2687 return reshapeInfo;
2688}
2689
Kevin May7d96b162021-02-03 17:38:41 +00002690void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002691{
2692 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2693
2694 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002695
2696 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2697 CHECK_VALID_SIZE(outputs.size(), 1);
2698
Mike Kelly0d77ae12022-01-07 17:42:27 +00002699 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2700 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002701 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002702
2703 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002704 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002705 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002706
Jan Eilersbac9b352020-07-13 13:40:24 +01002707 // Extracting new shape for the output
2708 // There are two ways it can be passed
2709 // * First is to define the target shape in the operator built-in options
2710 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002711 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002712 bool targetShapeFound = false;
2713 // Check if built-in options were given
2714 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002715 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002716 // make sure the parameter is given
2717 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002718 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002719 targetShape = options->new_shape;
2720 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002721 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002722 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002723
2724 // If there is no built-in option given or if the built-in new_shape parameter was empty
2725 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002726 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002727 // Check for a second input tensor
2728 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002729 {
2730 if (inputs[1]->is_variable)
2731 {
2732 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2733 }
2734
2735 if (inputs[1]->shape.size() != 1)
2736 {
2737 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2738 }
2739
2740 if (inputs[1]->type != tflite::TensorType_INT32)
2741 {
2742 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2743 }
2744
Teresa Charlin6a056a42021-12-01 10:25:43 +00002745 // Extract target shape from input
2746 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2747 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002748 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002749 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002750 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2751 {
2752 targetShape.push_back(values[i]);
2753 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002754 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002755 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002756 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002757 try
2758 {
2759 // We attempt to infer during Runtime.
2760 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2761 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2762 if (reshapeShapes[0] > 2)
2763 {
2764 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2765 "When inferring during runtime, the parser only supports "
2766 "shape (batch, -1) or (-1) for target shape input.",
2767 reshapeShapes[0],
2768 layerName,
2769 CHECK_LOCATION().AsString()));
2770 }
2771
2772 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2773 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2774 if (reshapeShapes[0] == 1)
2775 {
2776 targetShape = {numInputElements};
2777 }
2778 else if (reshapeShapes[0] == 2)
2779 {
2780 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2781 }
2782 }
2783 catch (const std::exception& exc)
2784 {
2785 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2786 "Reshape operation. Reshape operator target shape input buffer data "
2787 "is null. " << exc.what());
2788 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002789 }
2790 }
2791 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002792 {
2793 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2794 "At least one method required");
2795 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002796 }
2797
kevmay0171972a82018-12-17 14:28:03 +00002798 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002799 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002800
kevmay0171972a82018-12-17 14:28:03 +00002801 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002802 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2803 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002804 {
2805 std::stringstream ss;
2806 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002807 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002808 << " does not equal output shape "
2809 << actualOutputTensorInfo.GetShape()
2810 << ": "
2811 << CHECK_LOCATION().AsString();
2812 throw ParseException(ss.str());
2813 }
2814
Sadikb94967b2018-09-19 15:30:00 +01002815 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002816 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002817
Sadikb94967b2018-09-19 15:30:00 +01002818 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002819 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002820 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002821
2822 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2823 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2824
2825 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2826 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2827}
2828
Kevin May7d96b162021-02-03 17:38:41 +00002829void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002830{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002831 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2832}
2833
Kevin May7d96b162021-02-03 17:38:41 +00002834void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002835{
2836 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2837}
2838
Kevin May7d96b162021-02-03 17:38:41 +00002839void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002840{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002841 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2842
2843 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2844 CHECK_VALID_SIZE(inputs.size(), 2);
2845
2846 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2847 CHECK_VALID_SIZE(outputs.size(), 1);
2848
2849 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2850
2851 // Data for the parsed tensor args (size) must be stored locally.
2852 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2853
2854 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2855 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2856
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002857 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002858 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002859 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002860 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2861 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002862
James Ward58dec6b2020-09-11 17:32:44 +01002863 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002864
2865 switch (resizeMethod)
2866 {
2867 case ResizeMethod::Bilinear:
2868 {
James Ward58dec6b2020-09-11 17:32:44 +01002869 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002870
2871 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2872 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2873
David Monahan4a0c9b92020-05-30 09:48:39 +01002874 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002875 break;
2876 }
2877 case ResizeMethod::NearestNeighbor:
2878 {
James Ward58dec6b2020-09-11 17:32:44 +01002879 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002880 break;
2881 }
2882 default:
2883 {
2884 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002885 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2886 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002887 }
2888 }
2889
James Conroy05102392020-06-24 15:39:55 +01002890 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002891 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002892 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2893
2894 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2895 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002896 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2897
2898 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2899 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2900
2901 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2902 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2903}
2904
Kevin May7d96b162021-02-03 17:38:41 +00002905void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002906{
2907 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2908
Mike Kelly0d77ae12022-01-07 17:42:27 +00002909 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2910 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002911
2912 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2913
2914 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2915 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2916 CHECK_VALID_SIZE(outputs.size(), 1);
2917
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002918 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2919 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002920
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002921 const unsigned int concatDimInput = static_cast<unsigned int>(
2922 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002923
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002924 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2925 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002926
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002927 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002928
2929 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2930 {
2931 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2932
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002933 // This set up concatDescriptor view origin
2934 armnnUtils::ProcessConcatInputTensorInfo(
2935 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002936 }
2937
James Ward58dec6b2020-09-11 17:32:44 +01002938 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002939 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002940
Jim Flynn906f9462019-05-10 13:55:21 +01002941 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002942 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002943 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002944
James Conroy05102392020-06-24 15:39:55 +01002945 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002946 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002947
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002948 // add fused activation layer
2949 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002950
Sadik Armagan479045b2018-10-01 11:51:37 +01002951 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2952 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2953}
2954
Kevin May7d96b162021-02-03 17:38:41 +00002955void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002956{
2957 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2958
Mike Kelly0d77ae12022-01-07 17:42:27 +00002959 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002960 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2961
2962 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2963
2964 FullyConnectedDescriptor desc;
2965 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002966 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002967
2968 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2969 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2970 CHECK_VALID_SIZE(outputs.size(), 1);
2971
2972 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2973
2974 // Fully Connected Layer accepts two dimensional weights input
2975 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2976 if (weightsDimension != 2)
2977 {
2978 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002979 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2980 "Node {}",
2981 weightsDimension,
2982 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002983 }
2984
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002985 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01002986 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002987
Matthew Sloyan81beae32021-07-13 19:46:11 +01002988 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2989 // Add the first input tensor to the registration list
2990 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2991 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00002992 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002993
2994 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
2995
Matthew Sloyan81beae32021-07-13 19:46:11 +01002996 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
2997 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002998
Mike Kelly5880b912022-01-28 16:18:54 +00002999 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3000 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3001 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3002 {
3003 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3004 }
3005
Finn Williamsd4fa5452021-03-01 12:31:41 +00003006 if (inputs.size() == 3)
3007 {
3008 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003009 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003010
3011 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3012 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003013
3014 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3015 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3016 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3017 {
3018 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3019 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003020 }
3021
Matthew Sloyan81beae32021-07-13 19:46:11 +01003022 // Filters and biases are always passed to fully connected as inputs
3023 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003024
3025 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003026
Finn Williamsd4fa5452021-03-01 12:31:41 +00003027 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003028 if (inputTensorInfo.GetNumDimensions() > 2)
3029 {
3030 // Add reshape to flatten to 2D [batch_size, input_size],
3031 // where "input_size" corresponds to the number of inputs to the layer,
3032 // matching the second dimension of weights,
3033 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3034 std::vector<unsigned int> reshapedDimensions(2);
3035 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3036 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3037
3038 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3039 {
3040 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003041 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3042 reshapedDimensions[1],
3043 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003044 }
3045
3046 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3047 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3048
James Ward58dec6b2020-09-11 17:32:44 +01003049 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003050 armnn::ReshapeDescriptor reshapeDescriptor;
3051 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3052 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003053
3054 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3055 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3056
3057 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003058 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3059 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3060 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003061 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003062
3063 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003064
Sadik Armagand109a4d2020-07-28 10:42:13 +01003065 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003066 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3067
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003068 // we need to add the activation layer and fortunately we don't need to care about the data layout
3069 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3070 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003071
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003072 // register the output connection slots for the layer, connections are made after all layers have been created
3073 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3074 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3075}
3076
Kevin May7d96b162021-02-03 17:38:41 +00003077void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003078{
3079 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3080
Mike Kelly0d77ae12022-01-07 17:42:27 +00003081 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003082
3083 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3084 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3085 CHECK_VALID_SIZE(outputs.size(), 4);
3086
3087 // Obtain custom options from flexbuffers
3088 auto custom_options = operatorPtr->custom_options;
3089 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3090
3091 // Obtain descriptor information from tf lite
3092 DetectionPostProcessDescriptor desc;
3093 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3094 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3095 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3096 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3097 desc.m_NumClasses = m["num_classes"].AsUInt32();
3098 desc.m_ScaleH = m["h_scale"].AsFloat();
3099 desc.m_ScaleW = m["w_scale"].AsFloat();
3100 desc.m_ScaleX = m["x_scale"].AsFloat();
3101 desc.m_ScaleY = m["y_scale"].AsFloat();
3102
keidav0107d58c72019-02-26 11:57:39 +00003103 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003104 {
keidav0107d58c72019-02-26 11:57:39 +00003105 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003106 }
3107 if (!(m["detections_per_class"].IsNull()))
3108 {
3109 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3110 }
3111
3112 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3113 {
3114 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3115 "must be positive and less than or equal to 1.");
3116 }
3117
3118 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003119 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003120
James Ward58dec6b2020-09-11 17:32:44 +01003121 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003122 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003123 layerName.c_str());
3124
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003125 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003126
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003127 // The model does not specify the output shapes.
3128 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3129 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3130 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3131 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3132 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3133 m_OverridenOutputShapes.push_back({ 1 });
3134
keidav011b3e2ea2019-02-21 10:07:37 +00003135 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3136 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003137 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003138 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3139 }
3140
3141 // Register the input connection slots for the layer, connections are made after all layers have been created
3142 // only the tensors for the inputs are relevant, exclude the const tensors
3143 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3144 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3145
3146 // Register the output connection slots for the layer, connections are made after all layers have been created
3147 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3148 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3149 outputTensorIndexes[1],
3150 outputTensorIndexes[2],
3151 outputTensorIndexes[3]});
3152}
3153
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003154/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003155void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003156{
3157 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3158
3159 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3160 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3161 CHECK_VALID_SIZE(outputs.size(), 1);
3162
3163 if (inputs.size() < 1)
3164 {
3165 throw ParseException("Pack must have at least one input.");
3166 }
3167
3168 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3169 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3170
3171 StackDescriptor desc;
3172 desc.m_Axis = static_cast<uint32_t>(options->axis);
3173 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3174
3175 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3176 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3177 desc.m_InputShape = inputTensorInfo.GetShape();
3178
James Ward58dec6b2020-09-11 17:32:44 +01003179 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003180 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3181
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003182 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003183
Sadik Armagand109a4d2020-07-28 10:42:13 +01003184 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003185 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3186
3187 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3188 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3189
3190 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3191 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3192}
3193
Mike Kelly5880b912022-01-28 16:18:54 +00003194void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3195{
3196 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3197
3198 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3199 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3200
3201 if (inputs.size() < 2)
3202 {
3203 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3204 }
3205
3206 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3207 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3208 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3209 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3210 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3211 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3212
3213 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3214 // Please refer to each operand at
3215 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3216 armnn::LstmInputParams params;
3217
3218 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3219 {
3220 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3221 inputTensorInfo).first;
3222 }
3223
3224 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3225 inputTensorInfo).first;
3226 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3227 inputTensorInfo).first;
3228 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3229 inputTensorInfo).first;
3230
3231 // Recurrent weight tensors of size {n_cell, n_output}
3232 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3233 {
3234 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3235 inputTensorInfo).first;
3236 }
3237
3238 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3239 inputTensorInfo).first;
3240 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3241 inputTensorInfo).first;
3242 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3243 inputTensorInfo).first;
3244
3245 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3246 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3247 {
3248 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3249 inputTensorInfo).first;
3250 }
3251
3252 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3253 {
3254 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3255 inputTensorInfo).first;
3256 }
3257
3258 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3259 {
3260 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3261 inputTensorInfo).first;
3262 }
3263
3264 // Gates bias tensors of size {n_cell}
3265 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3266 {
3267 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3268 inputTensorInfo).first;
3269 }
3270
3271 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3272 inputTensorInfo).first;
3273 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3274 inputTensorInfo).first;
3275 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3276 inputTensorInfo).first;
3277
3278 // Projection weight tensor of size {n_output, n_cell}
3279 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3280 {
3281 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3282 inputTensorInfo).first;
3283 }
3284 // Projection bias tensor of size {n_output}
3285 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3286 {
3287 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3288 inputTensorInfo).first;
3289 }
3290
3291 // These state tensors are defined as variable tensors, and will be modified by this op.
3292 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3293 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3294 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3295 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3296
3297 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3298 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3299 {
3300 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3301 inputTensorInfo).first;
3302 }
3303
3304 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3305 {
3306 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3307 inputTensorInfo).first;
3308 }
3309
3310 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3311 {
3312 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3313 inputTensorInfo).first;
3314 }
3315
3316 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3317 {
3318 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3319 inputTensorInfo).first;
3320 }
3321
3322 // set the layer descriptor
3323 armnn::UnidirectionalSequenceLstmDescriptor desc;
3324 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3325 desc.m_ClippingThresCell = nodeParams->cell_clip;
3326 desc.m_ClippingThresProj = nodeParams->proj_clip;
3327 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3328 || params.m_RecurrentToInputWeights == nullptr
3329 || params.m_InputGateBias == nullptr);
3330 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3331 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3332 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3333 || params.m_ForgetLayerNormWeights != nullptr
3334 || params.m_CellLayerNormWeights != nullptr
3335 || params.m_OutputLayerNormWeights != nullptr);
3336 desc.m_TimeMajor = nodeParams->time_major;
3337
3338 if (desc.m_LayerNormEnabled)
3339 {
3340 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3341 inputTensorInfo).first;
3342 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3343 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3344
3345 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3346 inputTensorInfo).first;
3347 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3348 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3349
3350 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3351 inputTensorInfo).first;
3352 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3353 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3354
3355 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3356 inputTensorInfo).first;
3357 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3358 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3359 }
3360 else
3361 {
3362 float defaultIntermediate = std::pow(2, -12);
3363 desc.m_InputIntermediateScale = defaultIntermediate;
3364 desc.m_ForgetIntermediateScale = defaultIntermediate;
3365 desc.m_CellIntermediateScale = defaultIntermediate;
3366 desc.m_OutputIntermediateScale = defaultIntermediate;
3367 }
3368
3369 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3370 inputTensorInfo).first;
3371
3372 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3373 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3374
3375 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3376 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3377 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3378
3379 armnn::DataType dataType = inputTensorInfo.GetDataType();
3380 float qScale = inputTensorInfo.GetQuantizationScale();
3381 float qOffset = inputTensorInfo.GetQuantizationOffset();
3382
3383 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3384 if (!desc.m_CifgEnabled)
3385 {
3386 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3387 }
3388 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3389 cellStateInInfo.GetDataType(),
3390 cellStateInInfo.GetQuantizationScale(),
3391 cellStateInInfo.GetQuantizationOffset());
3392 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3393
3394 armnn::LstmInputParamsInfo paramsInfo;
3395 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3396 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3397 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3398 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3399 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3400 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3401 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3402 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3403 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3404
3405 if (!desc.m_CifgEnabled)
3406 {
3407 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3408 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3409 if (params.m_CellToInputWeights != nullptr)
3410 {
3411 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3412 }
3413 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3414 }
3415
3416 if (desc.m_ProjectionEnabled)
3417 {
3418 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3419 if (params.m_ProjectionBias != nullptr)
3420 {
3421 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3422 }
3423 }
3424
3425 if (desc.m_PeepholeEnabled)
3426 {
3427 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3428 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3429 }
3430
3431 if (desc.m_LayerNormEnabled)
3432 {
3433 if(!desc.m_CifgEnabled)
3434 {
3435 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3436 }
3437 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3438 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3439 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3440 }
3441
3442 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3443 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3444 ARMNN_ASSERT(layer != nullptr);
3445
3446 // register the input connection slots for the layer, connections are made after all layers have been created
3447 // only the tensors for the inputs are relevant, exclude the const tensors
3448 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3449 operatorPtr->inputs[18],
3450 operatorPtr->inputs[19]});
3451 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3452 inputTensorIndexes[1],
3453 inputTensorIndexes[2]});
3454
3455 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3456
3457 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3458 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3459 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3460
3461 unsigned int tensorIndex = outputTensorIndexes[0];
3462 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3463 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3464}
3465
Kevin May7d96b162021-02-03 17:38:41 +00003466void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003467{
3468 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3469
Mike Kelly0d77ae12022-01-07 17:42:27 +00003470 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3471 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003472
3473 // This unpackAxis indicates the axis to unpack
3474 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3475
3476 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3477 CHECK_VALID_SIZE(inputs.size(), 1);
3478
3479 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003480
3481 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3482 {
3483 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003484 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3485 "the number of input dimension {} {}",
3486 unpackAxis,
3487 inputTensorInfo.GetNumDimensions(),
3488 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003489 }
3490
Nina Drozd200e3802019-04-15 09:47:39 +01003491 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3492 // If num is not defined, automatically infer from the length of the dimension axis.
3493 if(unpackNum == 0)
3494 {
3495 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3496 }
3497
3498 // If unpack number cannot be inferred and is still zero, throw ParseException.
3499 if(unpackNum == 0)
3500 {
3501 throw ParseException("Number to unpack must greater than zero.");
3502 }
3503
3504 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3505 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3506
3507 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3508 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3509
3510 // Add current input shape to unpackDimSizes
3511 for (unsigned int i = 0; i < inputDimSize; ++i)
3512 {
3513 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3514 }
3515
3516 if (unpackDimSizes[unpackAxis] != unpackNum)
3517 {
3518 throw ParseException("Number to unpack must be the same as length of the dimension to "
3519 "unpack along.");
3520 }
3521
3522 unpackDimSizes[unpackAxis] /= unpackNum;
3523
3524 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3525 for (unsigned int j = 0; j < unpackNum; ++j)
3526 {
3527 // Set the size of the views.
3528 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3529 {
3530 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3531 }
3532 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3533 }
3534
James Ward58dec6b2020-09-11 17:32:44 +01003535 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003536 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003537 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003538
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003539 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3540 unpackDimSizes.data());
3541
Nina Drozd200e3802019-04-15 09:47:39 +01003542 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3543 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3544
Finn Williamsb49ed182021-06-29 15:50:08 +01003545 std::vector<unsigned int> reshapeDims;
3546 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3547 {
3548 if (axis != unpackAxis)
3549 {
3550 reshapeDims.push_back(splitOutShape[axis]);
3551 }
3552 }
3553
3554 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3555
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003556 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3557 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3558 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003559 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003560 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003561 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003562 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003563 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3564
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003565 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3566 outputTensorInfo.GetDataType(),
3567 outputTensorInfo.GetQuantizationScale(),
3568 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003569 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3570
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003571 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003572
3573 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3574 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3575 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3576 }
Nina Drozd200e3802019-04-15 09:47:39 +01003577}
3578
Kevin May7d96b162021-02-03 17:38:41 +00003579void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003580{
3581 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3582
Mike Kelly0d77ae12022-01-07 17:42:27 +00003583 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3584 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003585
3586 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3587
Nina Drozd200e3802019-04-15 09:47:39 +01003588 // If number of splits cannot be inferred and is zero, throw ParseException.
3589 if(numSplits == 0)
3590 {
3591 throw ParseException("Number to splits must greater than zero.");
3592 }
3593
Nina Drozd0324f482019-04-08 10:52:10 +01003594 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3595 CHECK_VALID_SIZE(inputs.size(), 2);
3596 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3597 CHECK_VALID_SIZE(outputs.size(), numSplits);
3598
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003599 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3600 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3601 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003602
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003603 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003604 if (axisBufferPtr == nullptr)
3605 {
3606 throw ParseException(
3607 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3608 CHECK_LOCATION().AsString()));
3609 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003610
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003611 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3612 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3613 int32_t axis = axisData[0];
3614
3615 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3616 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3617 {
3618 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3619 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3620 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3621 throw ParseException(
3622 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3623 axis,
3624 CHECK_LOCATION().AsString()));
3625 }
3626
3627 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003628
Nina Drozd0324f482019-04-08 10:52:10 +01003629 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003630 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003631 {
3632 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003633 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3634 inputTensorInfo.GetNumDimensions(),
3635 MaxNumOfTensorDimensions,
3636 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003637 }
3638
3639 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3640
3641 // Add current input shape to splitterDimSizes
3642 for (unsigned int i = 0; i < inputDimSize; ++i)
3643 {
3644 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3645 }
3646
3647 if (splitterDimSizes[splitDim] % numSplits != 0)
3648 {
3649 throw ParseException("Number of splits must evenly divide the dimension");
3650 }
3651 splitterDimSizes[splitDim] /= numSplits;
3652
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003653 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003654 for (unsigned int j = 0; j < numSplits; ++j)
3655 {
3656 // Set the size of the views.
3657 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3658 {
3659 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3660 }
3661 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3662 }
3663
James Ward58dec6b2020-09-11 17:32:44 +01003664 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003665 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003666 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003667
3668 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003669 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003670
Nina Drozd0324f482019-04-08 10:52:10 +01003671 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3672 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003673 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003674 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003675 }
3676
3677 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3678 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3679}
3680
Derek Lambertif0176992020-04-28 13:37:49 +01003681unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3682{
3683 int numDims = armnn::numeric_cast<int>(numDimsIn);
3684 int v = idx < 0 ? numDims + idx : idx;
3685 ARMNN_ASSERT(v >= 0);
3686 ARMNN_ASSERT(v < numDims);
3687
3688 return static_cast<unsigned int>(v);
3689}
3690
Kevin May7d96b162021-02-03 17:38:41 +00003691void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003692{
3693 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3694
Mike Kelly0d77ae12022-01-07 17:42:27 +00003695 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3696 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003697
3698 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3699 CHECK_VALID_SIZE(inputs.size(), 3);
3700
3701 auto& inputTensor = inputs[0];
3702 auto& splitsTensor = inputs[1];
3703 auto& axisTensor = inputs[2];
3704
3705 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3706 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3707 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3708 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3709
3710 // Inputs
3711 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3712 if (inputDimSize > MaxNumOfTensorDimensions)
3713 {
3714 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003715 fmt::format("The number of dimensions: {} for input tensors of the "
3716 "SplitV op cannot be greater than {} {}",
3717 inputTensorInfo.GetNumDimensions(),
3718 MaxNumOfTensorDimensions,
3719 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003720 }
3721
3722 // Get split axis
3723 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003724 if (axisBufferPtr == nullptr)
3725 {
3726 throw ParseException(
3727 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3728 CHECK_LOCATION().AsString()));
3729 }
3730
Derek Lambertif0176992020-04-28 13:37:49 +01003731 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3732 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003733 int32_t axis = axisData[0];
3734
3735 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3736 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3737 {
3738 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3739 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3740 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3741 throw ParseException(
3742 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3743 axis,
3744 CHECK_LOCATION().AsString()));
3745 }
3746 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003747
Derek Lambertif0176992020-04-28 13:37:49 +01003748 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003749 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003750 unsigned int numSplits{0};
3751
3752 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003753 {
3754 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003755 }
3756 else
3757 {
Ryan OShea86704732020-05-26 11:41:04 +01003758 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003759 }
3760
3761 if (numSplits <=0)
3762 {
3763 throw ParseException("SplitV has invalid number of splits");
3764 }
3765
Jan Eilersc0761e92020-06-29 16:48:44 +01003766 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003767 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003768 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003769
Jan Eilersc0761e92020-06-29 16:48:44 +01003770 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003771 int numInferred{0};
3772 unsigned int inferIdx{0};
3773 int splitSum{0};
3774 for (auto split : splitsData)
3775 {
3776 if (split < 0)
3777 {
3778 numInferred++;
3779 inferIdx = idx;
3780 }
3781 else
3782 {
3783 splitSum += split;
3784 }
3785 idx++;
3786 }
3787 // Check for inferred Axis
3788 if (numInferred == 0)
3789 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003790 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003791 {
3792 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3793 }
3794 }
3795 else if (numInferred == 1)
3796 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003797 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003798 }
3799 else
3800 {
3801 throw ParseException("Cannot infer split size for more than one split");
3802 }
3803
Derek Lambertif0176992020-04-28 13:37:49 +01003804 //Ouput size validation
3805 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3806 CHECK_VALID_SIZE(outputs.size(), numSplits);
3807
3808 // Setup Armnn descriptor
3809 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3810 unsigned int accumSplit = 0;
3811 for (unsigned int j = 0; j < numSplits; ++j)
3812 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003813 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003814
3815 // Set the size of the views.
3816 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3817 {
3818 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3819 if (dimIdx == splitDim)
3820 {
3821 dimSize = splitSize;
3822 }
3823 splitDesc.SetViewSize(j, dimIdx, dimSize);
3824 }
3825
3826 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3827 accumSplit += splitSize;
3828 }
3829
James Ward58dec6b2020-09-11 17:32:44 +01003830 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003831 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003832 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003833
3834 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3835 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3836
3837 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3838 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003839 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003840 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3841 }
3842
3843 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3844 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3845}
3846
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003847void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3848{
3849 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3850}
3851
Kevin May7d96b162021-02-03 17:38:41 +00003852void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003853{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003854 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3855}
3856
3857void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3858{
Inki Daed4619e22020-09-10 15:33:54 +09003859 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3860 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3861 CHECK_VALID_SIZE(inputs.size(), 2);
3862
3863 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3864 CHECK_VALID_SIZE(outputs.size(), 1);
3865
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003866 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3867 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003868 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003869 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003870
3871 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003872 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3873 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3874 {
3875 throw ParseException(
3876 fmt::format(
3877 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3878 CHECK_LOCATION().AsString()));
3879 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003880
3881 // Get const axis value from model and set it to descriptor.
3882 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3883 if (axisBufferPtr == nullptr)
3884 {
3885 throw ParseException(
3886 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3887 CHECK_LOCATION().AsString()));
3888 }
3889
3890 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3891 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3892 int32_t axis = axisData.front();
3893
3894 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3895 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3896 {
3897 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3898 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3899 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3900 throw ParseException(
3901 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3902 axis,
3903 CHECK_LOCATION().AsString()));
3904 }
3905
3906 ArgMinMaxDescriptor desc;
3907 desc.m_Axis = axis;
3908 desc.m_Function = argMinMaxFunction;
3909
3910 // Register a ArgMin/ArgMax layer.
3911 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3912 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3913 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3914 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003915 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3916
3917 // Register input tensor to the layer.
3918 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3919 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3920
3921 // Register output tensor to the layer.
3922 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3923 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3924}
3925
Kevin May7d96b162021-02-03 17:38:41 +00003926void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003927{
3928 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3929
Kevin May7d96b162021-02-03 17:38:41 +00003930 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003931 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003932 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003933 CHECK_VALID_SIZE(outputs.size(), 1);
3934
3935 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3936 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3937 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3938
3939 armnn::GatherDescriptor gatherDescriptor;
3940
Mike Kelly0d77ae12022-01-07 17:42:27 +00003941 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3942 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00003943 auto axis = options->axis;
3944
3945 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3946 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3947 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3948 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3949 {
3950 throw ParseException(
3951 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3952 axis,
3953 inputDimensions, inputDimensions,
3954 CHECK_LOCATION().AsString()));
3955 }
3956 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3957 {
3958 throw ParseException(
3959 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3960 outputDimensions,
3961 inputDimensions, indicesDimensions,
3962 CHECK_LOCATION().AsString()));
3963 }
3964
3965 gatherDescriptor.m_Axis = axis;
3966
3967 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3968 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3969 ARMNN_ASSERT(layer != nullptr);
3970 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3971
3972 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3973 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3974
3975 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3976 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3977}
3978
Teresa Charlin91a53ea2022-04-25 15:47:29 +01003979void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
3980{
3981 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3982
3983 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3984 CHECK_VALID_SIZE(inputs.size(), 2);
3985 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3986 CHECK_VALID_SIZE(outputs.size(), 1);
3987
3988 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3989 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3990 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3991
3992 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
3993 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
3994 ARMNN_ASSERT(layer != nullptr);
3995 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3996
3997 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3998 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3999
4000 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4001 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4002}
4003
Kevin May7d96b162021-02-03 17:38:41 +00004004void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004005{
4006 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4007
Kevin May7d96b162021-02-03 17:38:41 +00004008 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004009 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004010 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004011 CHECK_VALID_SIZE(outputs.size(), 1);
4012
4013 armnn::DepthToSpaceDescriptor descriptor;
4014
Mike Kelly0d77ae12022-01-07 17:42:27 +00004015 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4016 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004017 auto blockSize = options->block_size;
4018 if (blockSize < 2)
4019 {
4020 throw ParseException(
4021 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4022 blockSize,
4023 CHECK_LOCATION().AsString()));
4024 }
4025 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4026
4027 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4028 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4029 ARMNN_ASSERT(layer != nullptr);
4030 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4031 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4032
4033 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4034 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4035
4036 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4037 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4038}
4039
Kevin May7d96b162021-02-03 17:38:41 +00004040void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004041{
Sadik Armagana2747482021-02-09 10:28:54 +00004042 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4043}
4044
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004045void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4046{
4047 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4048}
4049
Sadik Armagana2747482021-02-09 10:28:54 +00004050void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4051{
4052 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4053}
4054
4055void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4056{
4057 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4058}
4059
4060void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4061{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004062 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4063
Mike Kelly0d77ae12022-01-07 17:42:27 +00004064 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4065 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004066
4067 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4068 CHECK_VALID_SIZE(inputs.size(), 2);
4069
4070 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4071 CHECK_VALID_SIZE(outputs.size(), 1);
4072
Sadik Armagana2747482021-02-09 10:28:54 +00004073 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004074
4075 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4076 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004077
4078 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004079 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4080 // Get const axis value from model and set it to descriptor.
4081 if (axisBufferPtr != nullptr)
4082 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004083 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4084 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4085
4086 // Convert the axis to unsigned int and remove duplicates.
4087 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4088 std::set<unsigned int> uniqueAxis;
4089 std::transform(axisData.begin(),
4090 axisData.end(),
4091 std::inserter(uniqueAxis, uniqueAxis.begin()),
4092 [rank](int i)->unsigned int{
4093 return static_cast<uint32_t>(((i + rank) % rank)); });
4094 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004095 }
Sadik Armagana2747482021-02-09 10:28:54 +00004096 else
4097 {
4098 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4099 {
4100 desc.m_vAxis.push_back(i);
4101 }
4102 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004103
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004104 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004105 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004106
4107 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004108 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004109
4110 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4111 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4112
4113 // Register input tensor to the layer.
4114 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4115 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4116
4117 // Register output tensor to the layer.
4118 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4119 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4120}
4121
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004122void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4123{
4124 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4125}
4126
4127void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4128{
4129 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4130}
4131
Mike Kelly31dce2b2021-09-01 21:22:37 +01004132void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4133{
4134 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4135
4136 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4137 CHECK_VALID_SIZE(inputs.size(), 1);
4138
4139 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4140 CHECK_VALID_SIZE(outputs.size(), 1);
4141
4142 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4143 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4144
4145 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4146
4147 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4148 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4149
4150 armnn::NormalizationDescriptor descriptor;
4151 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4152 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4153 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4154 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4155 descriptor.m_K = options->bias;
4156 descriptor.m_Alpha = options->alpha;
4157 descriptor.m_Beta = options->beta;
4158
4159 // ArmNN expects normSize to be the full size of the normalization
4160 // window rather than the radius as in TfLite.
4161 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4162
4163 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4164 ARMNN_ASSERT(layer != nullptr);
4165
4166 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4167 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4168
4169 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4170 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4171
4172 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4173 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4174}
4175
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004176void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4177{
4178 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4179}
4180
4181void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4182{
4183 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4184}
4185
4186void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4187{
4188 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4189}
4190
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004191void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4192{
4193 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4194}
4195
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004196void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4197{
4198 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4199
4200 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4201 CHECK_VALID_SIZE(inputs.size(), 1);
4202
4203 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4204 CHECK_VALID_SIZE(outputs.size(), 1);
4205
4206 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4207 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4208
4209 ElementwiseUnaryDescriptor desc;
4210 desc.m_Operation = unaryOperation;
4211 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4212 ARMNN_ASSERT(layer != nullptr);
4213
4214 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4215 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4216
4217 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4218 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4219
4220 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4221 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4222}
4223
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004224void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4225{
4226 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4227}
4228
4229void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4230{
4231 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4232}
4233
4234void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4235{
4236 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4237}
4238
4239void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4240{
4241 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4242}
4243
4244void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4245{
4246 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4247}
4248
4249void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4250{
4251 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4252}
4253
4254void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4255 ComparisonOperation comparisonOperation)
4256{
4257 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4258
4259 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4260 CHECK_VALID_SIZE(inputs.size(), 2);
4261
4262 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4263 CHECK_VALID_SIZE(outputs.size(), 1);
4264
4265 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4266 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4267
4268 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4269 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4270 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4271
4272 ComparisonDescriptor desc;
4273 desc.m_Operation = comparisonOperation;
4274 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4275 ARMNN_ASSERT(layer != nullptr);
4276
4277 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4278 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4279
4280 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4281 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4282
4283 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4284 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4285}
4286
Kevin May7d96b162021-02-03 17:38:41 +00004287armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4288 unsigned int outputSlot,
4289 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004290{
4291 ActivationDescriptor activationDesc;
4292 std::string layerName = prevLayer->GetName();
4293
4294 switch(activationType)
4295 {
4296 case tflite::ActivationFunctionType_NONE:
4297 {
4298 // this is a no-op: return previous layer
4299 return prevLayer;
4300 }
4301 case tflite::ActivationFunctionType_RELU:
4302 {
4303 activationDesc.m_Function = ActivationFunction::ReLu;
4304 layerName += ":RELU";
4305 break;
4306 }
4307 case tflite::ActivationFunctionType_RELU6:
4308 {
4309 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4310 activationDesc.m_A = 6.0f;
4311 activationDesc.m_B = 0.0f;
4312 layerName += ":RELU6";
4313 break;
4314 }
4315 case tflite::ActivationFunctionType_TANH:
4316 {
4317 activationDesc.m_Function = ActivationFunction::TanH;
4318 activationDesc.m_A = 1.0f;
4319 activationDesc.m_B = 1.0f;
4320 layerName += ":TANH";
4321 break;
4322 }
4323
4324 // I only put these here as a reminder what others we could support
4325 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4326 case tflite::ActivationFunctionType_SIGN_BIT:
4327 default:
4328 {
4329 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004330 fmt::format("TfLite parser doesn't suppport fused activation: "
4331 "{}/{} {} ",
4332 activationType,
4333 tflite::EnumNameActivationFunctionType(activationType),
4334 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004335
4336 }
4337 }
4338
4339 IConnectableLayer* activationLayer =
4340 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4341
4342 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4343 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4344 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4345 return activationLayer;
4346}
4347
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004348armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4349 unsigned int outputSlot)
4350{
Teresa Charlin725728e2022-05-05 13:33:33 +01004351
4352 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4353 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4354
4355 if (dataType == DataType::Signed32)
4356 {
4357 return prevLayer;
4358 }
4359
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004360 std::string layerName = prevLayer->GetName();
4361 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4362
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004363 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4364 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004365
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004366 return floorLayer;
4367}
4368
Mike Kelly0d77ae12022-01-07 17:42:27 +00004369TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004370{
4371 if (fileName == nullptr)
4372 {
James Ward58dec6b2020-09-11 17:32:44 +01004373 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004374 CHECK_LOCATION().AsString()));
4375 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004376 std::error_code errorCode;
4377 fs::path pathToFile(fileName);
4378 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004379 {
James Ward58dec6b2020-09-11 17:32:44 +01004380 //fmt::format() could not be used here (format error)
4381 std::stringstream msg;
4382 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4383 << " " << CHECK_LOCATION().AsString();
4384
4385 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004386 }
4387 std::ifstream file(fileName, std::ios::binary);
4388 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4389 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4390 fileContent.size());
4391}
4392
Mike Kelly0d77ae12022-01-07 17:42:27 +00004393TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004394{
4395 if (binaryContent == nullptr)
4396 {
James Ward58dec6b2020-09-11 17:32:44 +01004397 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004398 CHECK_LOCATION().AsString()));
4399 }
4400 flatbuffers::Verifier verifier(binaryContent, len);
4401 if (verifier.VerifyBuffer<tflite::Model>() == false)
4402 {
4403 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004404 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4405 "flatbuffers format. size:{} {}",
4406 len,
4407 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004408 }
4409 return tflite::UnPackModel(binaryContent);
4410}
4411
Mike Kelly0d77ae12022-01-07 17:42:27 +00004412TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004413 size_t subgraphIndex,
4414 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004415{
4416 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4417
Mike Kelly0d77ae12022-01-07 17:42:27 +00004418 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4419 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004420
4421 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004422 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004423 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004424 {
mathad01c21025d2021-04-26 10:09:37 +01004425 // If the input location is -1 then assume input is turned off.
4426 if (operatorPtr->inputs[i] == -1)
4427 {
4428 continue;
4429 }
4430 else
4431 {
4432 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4433 result.push_back(subgraphPtr->tensors[inputId].get());
4434 }
telsoa01c577f2c2018-08-31 09:22:23 +01004435 }
4436 return result;
4437}
4438
Mike Kelly0d77ae12022-01-07 17:42:27 +00004439TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004440 size_t subgraphIndex,
4441 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004442{
4443 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4444
Mike Kelly0d77ae12022-01-07 17:42:27 +00004445 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4446 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004447
4448 size_t outputCount = operatorPtr->outputs.size();
4449 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004450 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004451 {
4452 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4453 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004454 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004455 }
4456 return result;
4457}
4458
Mike Kelly0d77ae12022-01-07 17:42:27 +00004459TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004460 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004461{
4462 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004463 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004464
Derek Lambertiff05cc52019-04-26 13:05:17 +01004465 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004466 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004467 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004468 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004469 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004470 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004471 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004472 }
4473 return result;
4474}
4475
Mike Kelly0d77ae12022-01-07 17:42:27 +00004476TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004477 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004478{
4479 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004480 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004481
Derek Lambertiff05cc52019-04-26 13:05:17 +01004482 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004483 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004484 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004485 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004486 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4487 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004488 }
4489 return result;
4490}
4491
Kevin May7d96b162021-02-03 17:38:41 +00004492std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4493 size_t subgraphIndex,
4494 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004495{
4496 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004497 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4498 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004499 return operatorPtr->inputs;
4500}
4501
Kevin May7d96b162021-02-03 17:38:41 +00004502std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4503 size_t subgraphIndex,
4504 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004505{
4506 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004507 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4508 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004509 return operatorPtr->outputs;
4510}
4511
Kevin May7d96b162021-02-03 17:38:41 +00004512void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4513 size_t operatorIndex,
4514 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004515 const std::vector<unsigned int>& tensorIndexes,
4516 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004517{
4518 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004519 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004520
Finn Williamsd4fa5452021-03-01 12:31:41 +00004521 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004522 {
4523 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004524 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4525 " for subgraph:{} operator index:{} {}",
4526 tensorIndexes.size(),
4527 layer->GetNumInputSlots(),
4528 subgraphIndex,
4529 operatorIndex,
4530 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004531 }
4532
Finn Williamsd4fa5452021-03-01 12:31:41 +00004533 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004534 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004535 unsigned int tensorIndex = tensorIndexes[index];
4536 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004537 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4538 }
4539}
4540
Kevin May7d96b162021-02-03 17:38:41 +00004541void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4542 size_t operatorIndex,
4543 IConnectableLayer* layer,
4544 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004545{
4546 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004547 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004548 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4549 {
4550 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004551 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4552 " for subgraph:{} operator index:{} {}",
4553 tensorIndexes.size(),
4554 layer->GetNumOutputSlots(),
4555 subgraphIndex,
4556 operatorIndex,
4557 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004558 }
4559
4560 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4561 {
4562 unsigned int tensorIndex = tensorIndexes[slotIndex];
4563 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4564 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4565 }
4566}
4567
Kevin May7d96b162021-02-03 17:38:41 +00004568void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004569{
4570 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4571
4572 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004573 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004574 {
4575 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4576 IConnectableLayer* layer =
4577 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4578
4579 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4580 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4581
4582 RegisterOutputSlots(subgraphIndex,
4583 VIRTUAL_OPERATOR_ID,
4584 layer,
4585 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4586 }
4587}
4588
Kevin May7d96b162021-02-03 17:38:41 +00004589void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004590{
4591 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4592
4593 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004594 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004595 {
4596 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4597 IConnectableLayer* layer =
4598 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4599
4600 RegisterInputSlots(subgraphIndex,
4601 VIRTUAL_OPERATOR_ID,
4602 layer,
4603 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4604 }
4605}
4606
Mike Kelly5880b912022-01-28 16:18:54 +00004607void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004608{
Mike Kelly5880b912022-01-28 16:18:54 +00004609 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004610
Mike Kelly5880b912022-01-28 16:18:54 +00004611 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004612 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4613 {
4614 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4615 {
4616 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4617 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4618 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004619 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004620
Mike Kelly5880b912022-01-28 16:18:54 +00004621 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004622 {
4623 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004624 armnn::DataType dataType = tensorInfo.GetDataType();
4625
4626 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4627 != m_ConstantsToDequantize.end())
4628 {
4629 dataType = DataType::Float32;
4630 }
4631 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4632
4633 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4634 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4635
4636 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4637 RegisterOutputSlots(subgraphIndex,
4638 VIRTUAL_OPERATOR_ID,
4639 layer,
4640 { tensorIndex });
4641 }
4642 else if (ShouldConstantTensorBeCreated(tensorIndex))
4643 {
4644 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4645 armnn::DataType dataType = tensorInfo.GetDataType();
4646
4647 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4648 != m_ConstantsToDequantize.end())
4649 {
4650 dataType = DataType::Float32;
4651 }
4652 // Make sure isConstant flag is set.
4653 tensorInfo.SetConstant();
4654 tensorInfo.SetDataType(dataType);
4655
4656 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004657
Matthew Sloyan81beae32021-07-13 19:46:11 +01004658 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004659 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004660
Matthew Sloyan81beae32021-07-13 19:46:11 +01004661 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4662 RegisterOutputSlots(subgraphIndex,
4663 VIRTUAL_OPERATOR_ID,
4664 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004665 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004666 }
4667 else
4668 {
4669 throw ParseException(
4670 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4671 CHECK_LOCATION().AsString()));
4672 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004673 }
4674 }
4675 }
4676}
4677
telsoa01c577f2c2018-08-31 09:22:23 +01004678// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004679TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004680{
4681 CHECK_BUFFER(model, bufferIndex);
4682 return model->buffers[bufferIndex].get();
4683}
4684
Matteo Martincigh747ef822018-12-18 09:26:39 +00004685template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004686std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4687TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4688 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004689 armnn::TensorInfo& tensorInfo,
4690 armnn::Optional<armnn::PermutationVector&> permutationVector)
4691{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004692 // Make sure isConstant flag is set.
4693 tensorInfo.SetConstant();
4694
Matteo Martincigh747ef822018-12-18 09:26:39 +00004695 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4696 tensorPtr,
4697 tensorInfo,
4698 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004699 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004700 return std::make_pair(constData.first, std::move(storage));
4701}
4702
Mike Kelly5880b912022-01-28 16:18:54 +00004703bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4704{
4705 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4706 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4707 != m_ConstantsToBeCreated.end());
4708}
4709
Finn Williamsd4fa5452021-03-01 12:31:41 +00004710bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4711{
4712 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004713 bool isConst = true;
4714
4715 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4716 if (buffer->data.size() == 0)
4717 {
4718 isConst = false;
4719 }
4720
4721 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004722}
4723
Kevin May7d96b162021-02-03 17:38:41 +00004724std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004725TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4726 armnn::TensorInfo& tensorInfo,
4727 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004728{
4729 CHECK_TENSOR_PTR(tensorPtr);
4730 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4731 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4732
Matthew Sloyan81beae32021-07-13 19:46:11 +01004733 // Make sure isConstant flag is set.
4734 tensorInfo.SetConstant();
4735
telsoa01c577f2c2018-08-31 09:22:23 +01004736 switch (tensorInfo.GetDataType())
4737 {
4738 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004739 return CreateConstTensorAndStoreData<float>(bufferPtr,
4740 tensorPtr,
4741 tensorInfo,
4742 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004743 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004744 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4745 tensorPtr,
4746 tensorInfo,
4747 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004748 case armnn::DataType::QSymmS8:
4749 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4750 tensorPtr,
4751 tensorInfo,
4752 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004753 case armnn::DataType::QAsymmS8:
4754 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4755 tensorPtr,
4756 tensorInfo,
4757 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004758 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004759 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4760 tensorPtr,
4761 tensorInfo,
4762 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004763 default:
4764 {
4765 std::stringstream errString;
4766 errString << "Unexpected datatype when creating const tensor: "
4767 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4768 << " shape:" << tensorInfo.GetShape()
4769 << CHECK_LOCATION().AsString();
4770 throw ParseException(errString.str());
4771 }
4772 }
4773}
4774
Finn Williamsd4fa5452021-03-01 12:31:41 +00004775armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4776 armnn::TensorInfo& tensorInfo)
4777{
4778 CHECK_TENSOR_PTR(tensorPtr);
4779 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4780 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4781
Matthew Sloyan81beae32021-07-13 19:46:11 +01004782 // Make sure isConstant flag is set.
4783 tensorInfo.SetConstant();
4784
Finn Williamsd4fa5452021-03-01 12:31:41 +00004785 return ConstTensor(tensorInfo, bufferPtr->data.data());
4786}
4787
Mike Kelly5880b912022-01-28 16:18:54 +00004788std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4789TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4790 armnn::TensorInfo& tensorInfo,
4791 armnn::DataType inputDataType)
4792{
4793 CHECK_TENSOR_PTR(tensorPtr);
4794 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4795 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4796
4797 // Make sure isConstant flag is set.
4798 tensorInfo.SetConstant();
4799
4800 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4801 {
4802 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4803 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4804 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4805 }
4806 else
4807 {
4808 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4809 }
4810}
4811
4812std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4813TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4814{
4815 CHECK_TENSOR_PTR(tensorPtr);
4816 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4817 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4818 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4819
4820 // Make sure isConstant flag is set.
4821 tensorInfo.SetConstant();
4822
4823 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4824 {
4825 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4826 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4827 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4828 }
4829 else
4830 {
4831 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4832 }
4833}
4834
Kevin May7d96b162021-02-03 17:38:41 +00004835BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4836 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004837{
4838 CHECK_SUBGRAPH(m_Model, subgraphId);
4839 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004840 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004841 {
4842 if (input.second->name == name)
4843 {
4844 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004845 auto inputTensorInfo = ToTensorInfo(input.second);
4846 // Input tensors are always treated as constant tensors during network execution.
4847 inputTensorInfo.SetConstant(true);
4848 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004849 }
4850 }
4851
4852 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004853 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004854 {
4855 bindings << "'" << input.second->name << "' ";
4856 }
4857
4858 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004859 fmt::format("No input binding found for subgraph:{} and name:{}. "
4860 "Possible inputs are: [{}] {}",
4861 subgraphId,
4862 name,
4863 bindings.str(),
4864 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004865}
4866
Kevin May7d96b162021-02-03 17:38:41 +00004867BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4868 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004869{
4870 CHECK_SUBGRAPH(m_Model, subgraphId);
4871 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004872 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004873 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004874 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004875 if (output.second->name == name)
4876 {
4877 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004878 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4879 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4880 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004881 }
4882 }
4883
4884 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004885 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004886 {
4887 bindings << "'" << output.second->name << "' ";
4888 }
4889
4890 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004891 fmt::format("No output binding found for subgraph:{} and name:{}. "
4892 "Possible outputs are: [{}] {}",
4893 subgraphId,
4894 name,
4895 bindings.str(),
4896 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004897}
4898
Kevin May7d96b162021-02-03 17:38:41 +00004899size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004900{
4901 return m_Model->subgraphs.size();
4902}
4903
Kevin May7d96b162021-02-03 17:38:41 +00004904std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004905{
4906 CHECK_SUBGRAPH(m_Model, subgraphId);
4907 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4908 std::vector<std::string> result;
4909 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004910 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004911 {
4912 result.push_back(input.second->name);
4913 }
4914 return result;
4915}
4916
Kevin May7d96b162021-02-03 17:38:41 +00004917std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004918{
4919 CHECK_SUBGRAPH(m_Model, subgraphId);
4920 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4921 std::vector<std::string> result;
4922 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004923 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004924 {
4925 result.push_back(output.second->name);
4926 }
4927 return result;
4928}
4929
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004930const std::string TfLiteParserImpl::GetVersion()
4931{
4932 return TFLITE_PARSER_VERSION;
4933}
4934
Mike Kelly0d77ae12022-01-07 17:42:27 +00004935TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004936: m_FloatData(std::move(data))
4937, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004938, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004939, m_Int32Data(nullptr)
4940{
4941}
4942
Mike Kelly0d77ae12022-01-07 17:42:27 +00004943TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004944: m_FloatData(nullptr)
4945, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004946, m_Int8Data(nullptr)
4947, m_Int32Data(nullptr)
4948{
4949}
4950
Mike Kelly0d77ae12022-01-07 17:42:27 +00004951TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00004952: m_FloatData(nullptr)
4953, m_Uint8Data(nullptr)
4954, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01004955, m_Int32Data(nullptr)
4956{
4957}
4958
Mike Kelly0d77ae12022-01-07 17:42:27 +00004959TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004960: m_FloatData(nullptr)
4961, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004962, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004963, m_Int32Data(std::move(data))
4964{
4965}
4966
4967} // armnnTfLiteParser