blob: 479fc4f4749d2de90d0fc533d56fdabe95c589e3 [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 = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100796 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100797 {
Mike Kelly80512b02022-05-16 23:10:42 +0100798 if (m_Options.value().m_InferAndValidate)
799 {
800 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
801 {
802 { "InferAndValidate", true }
803 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100804
Mike Kelly80512b02022-05-16 23:10:42 +0100805 networkOptions.push_back(shapeInferenceMethodOption);
806 }
807 if (m_Options.value().m_AllowExpandedDims)
808 {
809 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
810 {
811 { "AllowExpandedDims", true }
812 });
813
814 networkOptions.push_back(shapeInferenceMethodOption);
815 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100816 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100817 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100818 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100819
telsoa01c577f2c2018-08-31 09:22:23 +0100820 if (m_Model->subgraphs.size() != 1)
821 {
822 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100823 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
824 m_Model->subgraphs.size(),
825 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100826 }
827
828 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100829 size_t operatorIndex = 0;
830 try
telsoa01c577f2c2018-08-31 09:22:23 +0100831 {
Colm Donelan6350d272020-06-09 16:56:25 +0100832 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100833 {
Colm Donelan6350d272020-06-09 16:56:25 +0100834 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
835 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100836 {
Colm Donelan6350d272020-06-09 16:56:25 +0100837 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100838
839// 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 +0100840#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100841 auto builtinCode = std::max(opCodePtr->builtin_code,
842 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
843#else
telsoa01c577f2c2018-08-31 09:22:23 +0100844 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100845#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100846
847 if (builtinCode > tflite::BuiltinOperator_MAX)
848 {
James Ward58dec6b2020-09-11 17:32:44 +0100849 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
850 "subgraph:{} operator idx:{}. {}",
851 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
852 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100853 }
854
855 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100856 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100857 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100858 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100859 }
telsoa01c577f2c2018-08-31 09:22:23 +0100860
Colm Donelan6350d272020-06-09 16:56:25 +0100861 SetupInputLayers(subgraphIndex);
862 SetupOutputLayers(subgraphIndex);
863 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100864
Colm Donelan6350d272020-06-09 16:56:25 +0100865 ++subgraphIndex;
866 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100867 }
telsoa01c577f2c2018-08-31 09:22:23 +0100868 }
Colm Donelan6350d272020-06-09 16:56:25 +0100869 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100870 {
Colm Donelan6350d272020-06-09 16:56:25 +0100871 std::stringstream errorString;
872 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
873 << subgraphIndex << " error: " << e.what();
874 ARMNN_LOG(error) << errorString.str();
875 std::stringstream errors;
876 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100877 throw ParseException(errors.str());
878 }
879
880 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100881 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100882 {
883 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
884 {
885 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
886 {
887 for (size_t inputSlotIdx = 0;
888 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
889 ++inputSlotIdx)
890 {
891 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
892 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
893 }
894 }
895 }
896 }
telsoa01c577f2c2018-08-31 09:22:23 +0100897 return std::move(m_Network);
898}
899
Mike Kelly5880b912022-01-28 16:18:54 +0000900std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
901 const TensorInfo& tensorInfo)
902{
903 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
904 tensorInfo.GetDataType() == DataType::QAsymmU8)
905 {
906 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
907
908 if (tensorInfo.HasPerAxisQuantization())
909 {
910 unsigned int axis = tensorInfo.GetQuantizationDim().value();
911 auto axisDimensionality = tensorInfo.GetShape()[axis];
912 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
913
914 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
915 {
916 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
917 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
918 tensorInfo.GetQuantizationOffset());
919 }
920 }
921 else
922 {
923 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
924 {
925 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
926 tensorInfo.GetQuantizationOffset());
927 }
928 }
929 return buffer;
930 }
931 throw ParseException(
932 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
933 GetDataTypeName(DataType::Float32),
934 GetDataTypeName(tensorInfo.GetDataType()),
935 CHECK_LOCATION().AsString()));
936}
937
Kevin May7d96b162021-02-03 17:38:41 +0000938void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
939 size_t tensorIndex,
940 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100941{
942 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100943 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
944 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100945
946 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
947
948 // assuming there is only one producer for that tensor
949 if (tensorSlots.outputSlot != nullptr)
950 {
James Ward58dec6b2020-09-11 17:32:44 +0100951 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
952 "subgraph:{} tensor:{} {}",
953 subgraphIndex,
954 tensorIndex,
955 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100956 }
957
958 tensorSlots.outputSlot = slot;
959}
960
Kevin May7d96b162021-02-03 17:38:41 +0000961void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
962 size_t tensorIndex,
963 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100964{
965 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100966 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
967 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100968
Finn Williamsd4fa5452021-03-01 12:31:41 +0000969 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100970 tensorSlots.inputSlots.push_back(slot);
971}
972
Kevin May7d96b162021-02-03 17:38:41 +0000973void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100974{
975 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
976
977 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000978 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100979
980 // Identify custom code defined for custom operator
981 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
982 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
983
984 // Find parser function that correspondes to custom code (if any)
985 auto iterator = m_CustomParserFunctions.find(customCode);
986 if (iterator != m_CustomParserFunctions.end())
987 {
988 customParserFunction = iterator->second;
989 }
990
991 // Run parser function
992 (this->*customParserFunction)(subgraphIndex, operatorIndex);
993}
994
Kevin May7d96b162021-02-03 17:38:41 +0000995void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100996{
997 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100998
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100999 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1000
1001 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001002
1003// 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 +01001004#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001005 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1006 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1007#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001008 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001009#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001010
1011 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1012 {
1013 // Do not add StandInLayer, throw ParseException instead
1014 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001015 fmt::format("Operator not supported. "
1016 "subgraph:{} operator:{} "
1017 "opcode_index:{} opcode:{} / {} {}",
1018 subgraphIndex,
1019 operatorIndex,
1020 opcodeIndex,
1021 opcode,
1022 tflite::EnumNameBuiltinOperator(opcode),
1023 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001024 }
1025
1026 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1027 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1028
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001029 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1030 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001031
1032 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001033 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001034
1035 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1036 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001037 ARMNN_ASSERT(layer != nullptr);
1038
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001039 for (unsigned int i = 0u; i < numOutputs; ++i)
1040 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001041 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001042 }
1043
1044 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1045 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1046
1047 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1048 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001049}
1050
mathad01b392e982021-04-07 12:07:30 +01001051void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1052{
1053 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1054
1055 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1056 CHECK_VALID_SIZE(inputs.size(), 1);
1057 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1058 CHECK_VALID_SIZE(outputs.size(), 1);
1059
1060 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1061
1062 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1063 ARMNN_ASSERT(layer != nullptr);
1064
1065 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1066 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1067
1068 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1069 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1070
1071 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1072 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1073}
1074
Kevin May7d96b162021-02-03 17:38:41 +00001075void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001076{
1077 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1078
Mike Kelly0d77ae12022-01-07 17:42:27 +00001079 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1080 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001081
1082 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1083
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001084 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1085 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1086 CHECK_VALID_SIZE(outputs.size(), 1);
1087
telsoa01c577f2c2018-08-31 09:22:23 +01001088 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001089 inputs.size() == 3 ?
1090 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001091 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1092 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001093 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001094 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1095 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001096
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001097 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
telsoa01c577f2c2018-08-31 09:22:23 +01001098 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1099
1100 // assuming input is NHWC
1101 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001102 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001103
1104 // assuming the filter is OHWI : Output, H, W, Input
1105 // which is essentially the same as NHWC
1106 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001107 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001108
Pablo Tellof0bd6832019-04-26 17:58:13 +01001109 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1110 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1111 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1112 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001113
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001114 // Add the first input and weights tensor to the registration list.
1115 // The constant weights will be added by SetupConstantLayers.
1116 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1117 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001118
James Ward58dec6b2020-09-11 17:32:44 +01001119 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001120 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001121
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001122 if (IsConstTensor(inputs[1]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1123 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1124 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
telsoa01c577f2c2018-08-31 09:22:23 +01001125 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001126 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001127 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001128
1129 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001130 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001131 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1132
1133 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1134 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1135
1136 if (IsConstTensor(inputs[2]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1137 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1138 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
1139 {
1140 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1141 }
telsoa01c577f2c2018-08-31 09:22:23 +01001142 }
1143
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001144 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001145
Sadik Armagand109a4d2020-07-28 10:42:13 +01001146 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001147 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001148
1149 // register the input connection slots for the layer, connections are made after all layers have been created
1150 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001151 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001152
jimfly01c25411c2018-11-14 17:47:22 +00001153 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001154 // register the output connection slots for the layer, connections are made after all layers have been created
1155 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001156 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001157}
1158
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001159// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
1160#if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001161void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1162{
1163 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1164
1165 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1166 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1167
1168 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1169
1170 Convolution3dDescriptor desc;
1171 desc.m_BiasEnabled = false;
1172 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1173 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1174 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1175 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1176 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1177 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1178 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1179
1180 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1181 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1182
1183 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1184 CHECK_VALID_SIZE(outputs.size(), 1);
1185
1186 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1187 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1188
1189 // Assuming input is NDHWC
1190 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1191 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1192 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1193
1194 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1195 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1196 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1197 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1198
1199 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001200 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001201 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1202 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1203 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1204 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1205
Mike Kelly5880b912022-01-28 16:18:54 +00001206 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001207
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001208 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1209
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001210 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1211 // Add the first input and weights tensor to the registration list.
1212 // The constant weights will be added by SetupConstantLayers.
1213 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1214
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001215 if (inputs.size() == 3)
1216 {
1217 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001218
1219 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1220 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001221 }
1222
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001223 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001224 ARMNN_ASSERT(layer != nullptr);
1225
1226 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1227 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1228
1229 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001230 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001231
1232 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1233 // Register the output connection slots for the layer, connections are made after all layers have been created
1234 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1235 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1236}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001237#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001238
Kevin May7d96b162021-02-03 17:38:41 +00001239void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001240{
1241 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1242
Mike Kelly0d77ae12022-01-07 17:42:27 +00001243 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1244 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001245
1246 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1247
1248 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001249 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1250 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001251 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001252 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001253
1254 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1255 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001256 if (inputs.size() == 3)
1257 {
1258 desc.m_BiasEnabled = true;
1259 }
1260
telsoa01c577f2c2018-08-31 09:22:23 +01001261 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1262 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001263 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1264 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001265
telsoa01c577f2c2018-08-31 09:22:23 +01001266 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001267 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001268
Matteo Martincigh747ef822018-12-18 09:26:39 +00001269 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001270 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1271 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001272
1273 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001274 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1275 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1276
Pablo Tellof0bd6832019-04-26 17:58:13 +01001277 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1278 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1279 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1280 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001281
Jan Eilers53ef7952021-06-02 12:01:25 +01001282 // 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 +01001283 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001284
Cathal Corbett06902652022-04-14 17:55:11 +01001285 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1286 // Add the first input and weights tensor to the registration list.
1287 // The constant weights will be added by SetupConstantLayers.
1288 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1289
1290 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1291
1292 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001293 {
1294 desc.m_BiasEnabled = true;
1295 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001296
1297 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1298 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001299 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001300 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001301
Sadik Armagand109a4d2020-07-28 10:42:13 +01001302 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001303 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001304
1305 // register the input connection slots for the layer, connections are made after all layers have been created
1306 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001307 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001308
jimfly01c25411c2018-11-14 17:47:22 +00001309 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001310 // register the output connection slots for the layer, connections are made after all layers have been created
1311 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1312 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1313}
1314
Kevin May7d96b162021-02-03 17:38:41 +00001315void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001316{
1317 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1318
1319 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1320 CHECK_VALID_SIZE(inputs.size(), 1);
1321
1322 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1323 CHECK_VALID_SIZE(outputs.size(), 1);
1324
James Ward58dec6b2020-09-11 17:32:44 +01001325 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001326
1327 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001328 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001329
Sadik Armagand109a4d2020-07-28 10:42:13 +01001330 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001331 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1332
1333 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1334 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1335
1336 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1337 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1338}
1339
Teresa Charlin3ab85482021-06-08 16:59:29 +01001340void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1341{
1342 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1343
1344 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1345 CHECK_VALID_SIZE(inputs.size(), 2);
1346
1347 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1348 CHECK_VALID_SIZE(outputs.size(), 1);
1349
1350 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1351
1352 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1353 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1354
1355 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1356
1357 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001358
1359 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1360 {
1361 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1362 }
1363 else
1364 {
1365 int32_t axis = inputs[1]->shape[0];
1366
1367 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1368
1369 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1370 {
1371 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1372 }
1373
1374 if(axis < 0)
1375 {
1376 axis = inputDimSize + axis + 1;
1377 }
1378
Rob Hughesd812a312021-08-06 13:10:53 +01001379 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001380 unsigned int inputShapeIndex = 0;
1381 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1382 {
1383 if (i == static_cast<unsigned int>(axis))
1384 {
1385 shape[i] = 1;
1386 }
1387 else
1388 {
1389 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1390 ++inputShapeIndex;
1391 }
1392 }
1393
Rob Hughesd812a312021-08-06 13:10:53 +01001394 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001395 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001396
1397 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1398 ARMNN_ASSERT(layer != nullptr);
1399 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1400
1401 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1402 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1403
1404 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1405 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1406}
1407
Kevin May7d96b162021-02-03 17:38:41 +00001408void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001409{
1410 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1411
1412 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001413 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001414
1415 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1416 CHECK_VALID_SIZE(outputs.size(), 1);
1417
James Ward58dec6b2020-09-11 17:32:44 +01001418 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001419 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001420
josh minorba424d22019-11-13 10:55:17 -06001421 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001422 {
1423 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1424 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001425 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1426 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001427 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001428 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001429
Mike Kelly08759e22020-03-02 11:41:31 +00001430 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001431 }
1432
James Conroy05102392020-06-24 15:39:55 +01001433 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001434 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001435 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001436
James Conroy05102392020-06-24 15:39:55 +01001437 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001438 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001439 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1440
1441 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1442 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1443
1444 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1445 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1446}
1447
Kevin May7d96b162021-02-03 17:38:41 +00001448void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001449{
1450 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1451
Mike Kelly0d77ae12022-01-07 17:42:27 +00001452 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1453 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001454
1455 TransposeConvolution2dDescriptor desc;
1456 desc.m_BiasEnabled = false;
1457 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1458 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1459 desc.m_DataLayout = armnn::DataLayout::NHWC;
1460
1461 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001462 if (inputs.size() == 4)
1463 {
1464 desc.m_BiasEnabled = true;
1465 }
1466 else
1467 {
1468 CHECK_VALID_SIZE(inputs.size(), 3);
1469 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001470
1471 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1472 CHECK_VALID_SIZE(outputs.size(), 1);
1473
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001474 if (inputs[0])
1475 {
1476 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1477 std::vector<int> output_shape(tensorInfo.GetNumElements());
1478 if (tensorInfo.GetDataType() == DataType::Signed32)
1479 {
1480 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1481 }
1482 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1483 {
1484 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1485 {
1486 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1487 }
1488 }
1489 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1490 for (int dimension : output_shape)
1491 {
1492 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1493 }
1494 desc.m_OutputShapeEnabled = true;
1495 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001496 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001497 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1498
1499 // TfLite uses NHWC tensors
1500 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1501 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1502
1503 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1504 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1505
1506 CalcPadding(inputHeight,
1507 filterHeight,
1508 desc.m_StrideY,
1509 1, // DilationY
1510 desc.m_PadTop,
1511 desc.m_PadBottom,
1512 options->padding);
1513
1514 CalcPadding(inputWidth,
1515 filterWidth,
1516 desc.m_StrideX,
1517 1, // DilationX
1518 desc.m_PadLeft,
1519 desc.m_PadRight,
1520 options->padding);
1521
Mike Kelly5880b912022-01-28 16:18:54 +00001522 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001523
1524 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001525 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001526
David Monahan61683802021-01-12 09:11:07 +00001527 if (desc.m_BiasEnabled)
1528 {
1529 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001530 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001531 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001532 filterTensorAndData.first,
1533 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001534 layerName.c_str());
1535 }
1536 else
1537 {
1538 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001539 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001540 EmptyOptional(),
1541 layerName.c_str());
1542 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001543
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001544 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001545
Sadik Armagand109a4d2020-07-28 10:42:13 +01001546 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001547 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1548
1549 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1550 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001551 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001552
1553 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1554 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1555}
1556
Kevin May7d96b162021-02-03 17:38:41 +00001557void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001558{
1559 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1560}
1561
Kevin May7d96b162021-02-03 17:38:41 +00001562void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001563{
1564 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1565
1566 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1567 CHECK_VALID_SIZE(inputs.size(), 3);
1568
1569 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1570 CHECK_VALID_SIZE(outputs.size(), 1);
1571
1572 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1573 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1574
1575 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1576 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1577
1578 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1579 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1580
1581 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1582 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1583
1584 size_t step = 2;
1585 std::vector<std::pair<unsigned int, unsigned int>> crops;
1586 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1587 {
1588 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1589 }
1590
1591 armnn::BatchToSpaceNdDescriptor desc;
1592 desc.m_BlockShape = blockShape;
1593 desc.m_Crops = crops;
1594 desc.m_DataLayout = armnn::DataLayout::NHWC;
1595
James Ward58dec6b2020-09-11 17:32:44 +01001596 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001597
James Conroy05102392020-06-24 15:39:55 +01001598 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001599 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001600 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1601
1602 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1603 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001604 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1605
1606 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1607 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1608
1609 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1610 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1611}
1612
Kevin May7d96b162021-02-03 17:38:41 +00001613void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001614{
1615 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1616
1617 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1618 CHECK_VALID_SIZE(inputs.size(), 1);
1619
1620 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1621 CHECK_VALID_SIZE(outputs.size(), 1);
1622
1623 L2NormalizationDescriptor desc;
1624 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001625 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001626 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1627
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001628 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001629
Sadik Armagand109a4d2020-07-28 10:42:13 +01001630 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001631 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1632
1633 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1634 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1635
1636 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1637 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1638}
1639
Kevin May7d96b162021-02-03 17:38:41 +00001640void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001641{
1642 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1643}
1644
Kevin May7d96b162021-02-03 17:38:41 +00001645void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001646{
1647 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1648
1649 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1650 CHECK_VALID_SIZE(inputs.size(), 2);
1651
1652 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1653 CHECK_VALID_SIZE(outputs.size(), 1);
1654
James Ward58dec6b2020-09-11 17:32:44 +01001655 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001656
1657 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1658 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1659 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001660
Sadik Armagand109a4d2020-07-28 10:42:13 +01001661 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001662 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1663
1664 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1665 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001666 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1667
1668 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001669 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001670
1671 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1672 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1673}
1674
Kevin May7d96b162021-02-03 17:38:41 +00001675void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001676{
1677 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1678
1679 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1680 CHECK_VALID_SIZE(inputs.size(), 2);
1681
1682 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1683 CHECK_VALID_SIZE(outputs.size(), 1);
1684
James Ward58dec6b2020-09-11 17:32:44 +01001685 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001686
1687 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1688 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1689 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001690
Sadik Armagand109a4d2020-07-28 10:42:13 +01001691 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001692 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1693
1694 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1695 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001696 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1697
1698 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001699 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001700
1701 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1702 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1703}
1704
Kevin May7d96b162021-02-03 17:38:41 +00001705void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1706 size_t operatorIndex,
1707 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001708{
1709 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1710
Mike Kelly0d77ae12022-01-07 17:42:27 +00001711 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1712 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001713
1714 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1715
1716 std::string layerName;
1717
1718 switch (algorithm)
1719 {
1720 case PoolingAlgorithm::Average:
1721 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001722 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001723 break;
1724 case PoolingAlgorithm::Max:
1725 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001726 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001727 break;
1728 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001729 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001730 }
1731
1732 Pooling2dDescriptor desc;
1733
1734 desc.m_PoolType = algorithm;
1735 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1736 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1737 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1738 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1739 desc.m_PaddingMethod = PaddingMethod::Exclude;
1740 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001741 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001742
1743 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1744 CHECK_VALID_SIZE(inputs.size(), 1);
1745 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1746
1747 // assuming input is NHWC
1748 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1749 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1750
Pablo Tellof0bd6832019-04-26 17:58:13 +01001751 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1752 desc.m_PadTop, desc.m_PadBottom, options->padding);
1753 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1754 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001755
1756 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1757 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001758
Sadik Armagand109a4d2020-07-28 10:42:13 +01001759 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001760 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1761
1762 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1763 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001764 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001765
1766 // register the input connection slots for the layer, connections are made after all layers have been created
1767 // only the tensors for the inputs are relevant, exclude the const tensors
1768 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001769 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001770
jimfly01c25411c2018-11-14 17:47:22 +00001771 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001772 // register the output connection slots for the layer, connections are made after all layers have been created
1773 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1774 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1775}
1776
Kevin May7d96b162021-02-03 17:38:41 +00001777void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001778{
1779 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1780
1781 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1782 CHECK_VALID_SIZE(inputs.size(), 3);
1783 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1784 CHECK_VALID_SIZE(outputs.size(), 1);
1785
1786 SliceDescriptor desc;
1787
1788 // set begin tensor info for slice descriptor
1789 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1790 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1791
1792 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1793 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1794
1795 // set size tensor info for slice descriptor
1796 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1797 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1798
Mike Kelly7ba84d62021-09-10 15:27:19 +01001799 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1800 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001801 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001802 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1803
1804 for (unsigned int i = 0; i < signedSize.size(); ++i)
1805 {
1806 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001807
Mike Kelly7ba84d62021-09-10 15:27:19 +01001808 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1809 {
1810 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1811 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1812 signedValue,
1813 inputTensorInfo.GetShape()[i] - begin[i],
1814 CHECK_LOCATION().AsString()));
1815 }
1816
1817 if (signedValue == -1)
1818 {
1819 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1820 }
1821 else
1822 {
1823 size[i] = static_cast<unsigned int>(signedValue);
1824 }
1825 }
1826
josh minorba424d22019-11-13 10:55:17 -06001827 desc = SliceDescriptor(begin, size);
1828
James Ward58dec6b2020-09-11 17:32:44 +01001829 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001830
Sadik Armagand109a4d2020-07-28 10:42:13 +01001831 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001832 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1833
1834 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001835 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1836
1837 // register the input connection slots for the layer, connections are made after all layers have been created
1838 // only the tensors for the inputs are relevant, exclude the const tensors
1839 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1840 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1841
1842 // register the output connection slots for the layer, connections are made after all layers have been created
1843 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1844 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1845}
1846
Kevin May7d96b162021-02-03 17:38:41 +00001847void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001848{
1849 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001850 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1851 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001852
1853 SoftmaxDescriptor desc;
1854 desc.m_Beta = options->beta;
1855
1856 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1857 CHECK_VALID_SIZE(inputs.size(), 1);
1858 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1859 CHECK_VALID_SIZE(outputs.size(), 1);
1860
James Ward58dec6b2020-09-11 17:32:44 +01001861 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001862 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1863
Sadik Armagand109a4d2020-07-28 10:42:13 +01001864 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001865 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1866
1867 // register the input connection slots for the layer, connections are made after all layers have been created
1868 // only the tensors for the inputs are relevant, exclude the const tensors
1869 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1870 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1871
1872 // register the output connection slots for the layer, connections are made after all layers have been created
1873 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1874 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1875}
1876
Kevin May7d96b162021-02-03 17:38:41 +00001877void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001878{
1879 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1880
1881 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1882 CHECK_VALID_SIZE(inputs.size(), 3);
1883
1884 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1885 CHECK_VALID_SIZE(outputs.size(), 1);
1886
1887 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1888 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1889
1890 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1891 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1892
1893 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1894 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1895
1896 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1897 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1898
1899 size_t step = 2;
1900 std::vector<std::pair<unsigned int, unsigned int>> padList;
1901 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1902 {
1903 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1904 }
1905
1906 armnn::SpaceToBatchNdDescriptor desc;
1907 desc.m_BlockShape = blockShape;
1908 desc.m_PadList = padList;
1909 desc.m_DataLayout = armnn::DataLayout::NHWC;
1910
James Ward58dec6b2020-09-11 17:32:44 +01001911 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001912
James Conroy05102392020-06-24 15:39:55 +01001913 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001914 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001915 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1916
1917 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1918 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001919 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1920
1921 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1922 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1923
1924 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1925 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1926}
1927
Teresa Charlin3ab85482021-06-08 16:59:29 +01001928armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00001929 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001930{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001931 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001932 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1933
1934 if (inputTensorInfo.GetNumDimensions() > 4)
1935 {
1936 std::stringstream ss;
1937 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1938 << " shape:" << inputTensorInfo.GetShape() << " "
1939 << CHECK_LOCATION().AsString();
1940 throw ParseException(ss.str());
1941 }
1942
1943 if (squeezeDims.empty())
1944 {
1945 squeezeDims.assign(dimensionSequence,
1946 dimensionSequence+inputTensorInfo.GetNumDimensions());
1947 }
1948
1949 std::vector<uint32_t> outputDims;
1950 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1951 {
1952 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1953 auto currentDimension = inputTensorInfo.GetShape()[i];
1954 if (skipSqueeze || currentDimension != 1)
1955 {
1956 outputDims.push_back(currentDimension);
1957 }
1958 }
1959
1960 if (outputDims.size() > 4)
1961 {
1962 std::stringstream ss;
1963 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1964 << " shape:" << inputTensorInfo.GetShape() << " "
1965 << CHECK_LOCATION().AsString();
1966 throw ParseException(ss.str());
1967 }
1968
1969 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1970 outputDims.data());
1971
1972 // we need to preserve the tensor type and the quantization data as well
1973 TensorInfo outTensorInfo = inputTensorInfo;
1974 outTensorInfo.SetShape(outShape);
1975
1976 return outTensorInfo;
1977}
1978
Keith Davis0176fd82021-06-01 17:36:32 +01001979void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
1980{
1981 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1982
1983 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1984 CHECK_VALID_SIZE(inputs.size(), 1);
1985 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1986 CHECK_VALID_SIZE(outputs.size(), 1);
1987
1988 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
1989
1990 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
1991 ARMNN_ASSERT(layer != nullptr);
1992
1993
1994 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1995 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1996
1997 // Check if output tensor type is Signed32 or Signed64
1998 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
1999 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2000 {
2001 throw ParseException(
2002 fmt::format(
2003 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2004 CHECK_LOCATION().AsString()));
2005 }
2006
2007 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2008 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2009
2010 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2011 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2012}
2013
Kevin May7d96b162021-02-03 17:38:41 +00002014void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002015{
2016 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2017
2018 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2019 CHECK_VALID_SIZE(inputs.size(), 1);
2020
2021 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2022 CHECK_VALID_SIZE(outputs.size(), 1);
2023
2024 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2025 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002026 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002027
2028 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002029
2030 std::vector<uint32_t> squeezeDim;
2031 // A single negative dim index is interpreted as a negative index in python
2032 // Meaning the index will be the shape size plus the negative index value
2033 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2034 {
2035 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2036 squeezeDim.push_back(static_cast<uint32_t>(dim));
2037 }
2038 else
2039 {
2040 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2041 }
2042
2043 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2044
James Conroy05102392020-06-24 15:39:55 +01002045 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002046
2047 ReshapeDescriptor reshapeDesc;
2048 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2049
telsoa01c577f2c2018-08-31 09:22:23 +01002050 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002051 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002052 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2053
2054 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2055 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2056
2057 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2058 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2059}
2060
Kevin May7d96b162021-02-03 17:38:41 +00002061void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002062{
2063 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2064
2065 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2066 CHECK_VALID_SIZE(inputs.size(), 4);
2067
2068 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2069 CHECK_VALID_SIZE(outputs.size(), 1);
2070
Mike Kelly0d77ae12022-01-07 17:42:27 +00002071 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2072 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002073
2074 StridedSliceDescriptor desc;
2075 desc.m_BeginMask = options->begin_mask;
2076 desc.m_EllipsisMask = options->ellipsis_mask;
2077 desc.m_EndMask = options->end_mask;
2078 desc.m_NewAxisMask = options->new_axis_mask;
2079 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2080 desc.m_DataLayout = armnn::DataLayout::NHWC;
2081
2082 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2083 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2084
2085 std::vector<int> begin(beginTensorInfo.GetNumElements());
2086 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2087
2088 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2089 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2090
2091 std::vector<int> end(endTensorInfo.GetNumElements());
2092 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2093
2094 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2095 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2096
2097 std::vector<int> stride(strideTensorInfo.GetNumElements());
2098 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2099
2100 desc.m_Begin = begin;
2101 desc.m_End = end;
2102 desc.m_Stride = stride;
2103
James Ward58dec6b2020-09-11 17:32:44 +01002104 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002105 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002106 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002107
Sadik Armagand109a4d2020-07-28 10:42:13 +01002108 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002109 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2110
2111 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2112 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2113
2114 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2115 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2116}
2117
Kevin May7d96b162021-02-03 17:38:41 +00002118void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002119{
2120 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2121
Mike Kelly0d77ae12022-01-07 17:42:27 +00002122 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2123 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002124
2125 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2126 CHECK_VALID_SIZE(inputs.size(), 2);
2127
2128 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2129 CHECK_VALID_SIZE(outputs.size(), 1);
2130
2131 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2132 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2133
James Ward58dec6b2020-09-11 17:32:44 +01002134 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002135 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002136 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002137
Sadik Armagand109a4d2020-07-28 10:42:13 +01002138 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002139 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2140
2141 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002142 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002143
2144 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2145
2146 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2147 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2148}
2149
Kevin May7d96b162021-02-03 17:38:41 +00002150void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302151{
2152 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2153
Mike Kelly0d77ae12022-01-07 17:42:27 +00002154 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2155 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302156
2157 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2158 CHECK_VALID_SIZE(inputs.size(), 2);
2159
2160 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2161 CHECK_VALID_SIZE(outputs.size(), 1);
2162
2163 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2164 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2165
James Ward58dec6b2020-09-11 17:32:44 +01002166 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302167 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002168 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302169
Sadik Armagand109a4d2020-07-28 10:42:13 +01002170 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302171 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2172
2173 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002174 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302175 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2176
2177 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2178 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2179}
2180
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002181void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2182{
2183 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2184
2185 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2186 CHECK_VALID_SIZE(inputs.size(), 2);
2187
2188 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2189 CHECK_VALID_SIZE(outputs.size(), 1);
2190
2191 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2192 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2193
2194 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2195 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2196 ARMNN_ASSERT(layer != nullptr);
2197
2198 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2199 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2200
2201 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2202 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2203 layer = AddFusedFloorLayer(layer, 0);
2204
2205 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2206 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2207}
2208
Kevin May7d96b162021-02-03 17:38:41 +00002209void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002210{
2211 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2212
Mike Kelly0d77ae12022-01-07 17:42:27 +00002213 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2214 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002215
2216 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2217 CHECK_VALID_SIZE(inputs.size(), 2);
2218
2219 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2220 CHECK_VALID_SIZE(outputs.size(), 1);
2221
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002222 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2223 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2224
James Ward58dec6b2020-09-11 17:32:44 +01002225 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002226 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002227 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002228
Sadik Armagand109a4d2020-07-28 10:42:13 +01002229 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002230 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2231
2232 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002233 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002234 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2235
2236 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2237 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2238}
2239
Kevin May7d96b162021-02-03 17:38:41 +00002240void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002241{
2242 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2243
Mike Kelly0d77ae12022-01-07 17:42:27 +00002244 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2245 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002246
2247 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2248 CHECK_VALID_SIZE(inputs.size(), 2);
2249
2250 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2251 CHECK_VALID_SIZE(outputs.size(), 1);
2252
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002253 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2254 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2255
James Ward58dec6b2020-09-11 17:32:44 +01002256 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002257 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002258 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002259
Sadik Armagand109a4d2020-07-28 10:42:13 +01002260 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002261 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2262
2263 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002264 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002265 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2266
2267 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2268 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2269}
2270
Kevin May7d96b162021-02-03 17:38:41 +00002271void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002272{
2273 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2274
2275 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2276
2277 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2278 CHECK_VALID_SIZE(outputs.size(), 1);
2279
2280 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2281 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2282
2283 armnn::MeanDescriptor desc;
2284 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2285 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2286 desc.m_Axis = axis;
2287
2288 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002289 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002290
2291 desc.m_KeepDims =
2292 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2293 true : false;
2294
James Ward58dec6b2020-09-11 17:32:44 +01002295 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002296 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002297 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002298
2299 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2300
2301 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2302 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2303
2304 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2305 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2306}
2307
Kevin May7d96b162021-02-03 17:38:41 +00002308void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002309{
2310 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2311
Kevin May7d96b162021-02-03 17:38:41 +00002312 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002313
Kevin May7d96b162021-02-03 17:38:41 +00002314 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002315 CHECK_VALID_SIZE(outputs.size(), 1);
2316
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002317 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002318 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002319
Mike Kelly0d77ae12022-01-07 17:42:27 +00002320 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002321
2322 size_t step = 2;
2323 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002324 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2325
2326 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002327 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002328 CHECK_VALID_SIZE(inputs.size(), 2);
2329
2330 if (inputTensorInfo.IsQuantized())
2331 {
2332 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2333 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002334 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002335 else if (opcode == tflite::BuiltinOperator_PADV2)
2336 {
2337 CHECK_VALID_SIZE(inputs.size(), 3);
2338
2339 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2340
2341 if (padValueTensorInfo.GetNumElements() != 1)
2342 {
2343 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2344 }
2345 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2346
2347 // Get the pad value from the input tensor
2348 if (padValueBufferPtr->data.size() > 0)
2349 {
2350 switch (padValueTensorInfo.GetDataType())
2351 {
2352 case armnn::DataType::Float32:
2353 {
2354 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2355 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2356 desc.m_PadValue = padValueBuffer[0];
2357 break;
2358 }
2359 case armnn::DataType::QAsymmU8:
2360 {
2361 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2362 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2363 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2364 padValueTensorInfo.GetQuantizationScale(),
2365 padValueTensorInfo.GetQuantizationOffset());
2366 break;
2367 }
2368 case armnn::DataType::QAsymmS8:
2369 case armnn::DataType::QSymmS8:
2370 {
2371 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2372 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2373 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2374 padValueTensorInfo.GetQuantizationScale(),
2375 padValueTensorInfo.GetQuantizationOffset());
2376 break;
2377 }
2378 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2379 }
2380 }
2381 else if (inputTensorInfo.IsQuantized())
2382 {
2383 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2384 }
2385 }
2386
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002387 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2388 {
2389 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2390 }
2391
Mike Kelly0d77ae12022-01-07 17:42:27 +00002392 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2393 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002394 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002395
2396 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2397 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002398 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2399
2400 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2401 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2402
2403 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2404 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2405}
2406
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002407void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2408{
2409 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2410
2411 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2412 CHECK_VALID_SIZE(inputs.size(), 2);
2413
2414 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2415 CHECK_VALID_SIZE(outputs.size(), 1);
2416
2417 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2418
2419 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2420 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2421
2422 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2423 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2424
2425 size_t step = 2;
2426 armnn::PadDescriptor desc;
2427 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2428 {
2429 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2430 }
2431
2432 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2433 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2434
2435 if (options->mode == tflite::MirrorPadMode_REFLECT)
2436 {
2437 desc.m_PaddingMode = PaddingMode::Reflect;
2438 }
2439 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2440 {
2441 desc.m_PaddingMode = PaddingMode::Symmetric;
2442 }
2443 else
2444 {
2445 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2446 }
2447
2448 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2449 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2450 auto inputShape = inputTensorInfo.GetShape();
2451 auto padList = desc.m_PadList;
2452
2453 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2454 for(unsigned int i = 0; i < padList.size(); ++i)
2455 {
2456 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2457 padList.at(i).second > (inputShape[i] - isReflect))
2458 {
2459 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2460 "equal (Symmetric) to the dimension size.");
2461 }
2462 }
2463
2464 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2465 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2466
2467 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2468 ARMNN_ASSERT(layer != nullptr);
2469 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2470
2471 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2472 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2473
2474 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2475 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2476}
2477
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002478void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2479{
2480 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2481
2482 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2483 CHECK_VALID_SIZE(inputs.size(), 2);
2484
2485 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2486 CHECK_VALID_SIZE(outputs.size(), 1);
2487
2488 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2489
2490 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2491 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2492 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2493 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2494
2495 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2496 ARMNN_ASSERT(layer != nullptr);
2497 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2498
2499 if (IsConstTensor(inputs[1]))
2500 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002501 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002502 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2503 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002504
Mike Kelly5880b912022-01-28 16:18:54 +00002505 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2506 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002507 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2508 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002509 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002510 ARMNN_ASSERT(constLayer != nullptr);
2511
2512 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2513 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2514 RegisterOutputSlots(subgraphIndex,
2515 VIRTUAL_OPERATOR_ID,
2516 constLayer,
2517 { inputTensorIndexes[1] });
2518 }
2519 else
2520 {
2521 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2522 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2523 }
2524
2525 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2526 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2527}
2528
Kevin May7d96b162021-02-03 17:38:41 +00002529void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002530{
2531 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2532
2533 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2534 CHECK_VALID_SIZE(inputs.size(), 1);
2535
2536 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2537 CHECK_VALID_SIZE(outputs.size(), 1);
2538
James Ward58dec6b2020-09-11 17:32:44 +01002539 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002540
2541 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002542 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002543
Sadik Armagand109a4d2020-07-28 10:42:13 +01002544 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002545 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2546
2547 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2548 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2549
2550 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2551 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2552}
Finn Williamsc42c3842019-01-22 14:18:11 +00002553
Kevin May7d96b162021-02-03 17:38:41 +00002554void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002555{
Finn Williamsc42c3842019-01-22 14:18:11 +00002556 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002557}
2558
Kevin May7d96b162021-02-03 17:38:41 +00002559void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002560{
Finn Williamsc42c3842019-01-22 14:18:11 +00002561 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2562}
Sadik Armagan58f39192018-09-17 14:14:39 +01002563
Kevin May7d96b162021-02-03 17:38:41 +00002564void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002565{
Jan Eilers2f746b32020-07-28 14:00:06 +01002566 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002567}
2568
Kevin May7d96b162021-02-03 17:38:41 +00002569void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002570{
2571 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2572}
2573
Kevin May7d96b162021-02-03 17:38:41 +00002574void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002575{
2576 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2577}
2578
Kevin May7d96b162021-02-03 17:38:41 +00002579void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002580{
2581 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2582}
2583
Kevin May7d96b162021-02-03 17:38:41 +00002584void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002585{
2586 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2587}
Finn Williamsc42c3842019-01-22 14:18:11 +00002588
Kevin May7d96b162021-02-03 17:38:41 +00002589void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002590{
2591 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002592 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002593 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002594
2595 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2596 CHECK_VALID_SIZE(inputs.size(), 1);
2597
2598 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2599 CHECK_VALID_SIZE(outputs.size(), 1);
2600
James Ward58dec6b2020-09-11 17:32:44 +01002601 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002602 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002603 activationDesc.m_Function = activationType;
2604
2605 switch (activationType)
2606 {
2607 case ActivationFunction::ReLu:
2608 {
James Ward58dec6b2020-09-11 17:32:44 +01002609 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002610 break;
2611 }
2612 case ActivationFunction::BoundedReLu:
2613 {
James Ward58dec6b2020-09-11 17:32:44 +01002614 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002615 activationDesc.m_A = 6.0f;
2616 activationDesc.m_B = 0.0f;
2617 break;
2618 }
2619 case ActivationFunction::Sigmoid:
2620 {
James Ward58dec6b2020-09-11 17:32:44 +01002621 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002622 break;
2623 }
Nina Drozd99851762019-04-09 09:37:38 +01002624 case ActivationFunction::TanH:
2625 {
James Ward58dec6b2020-09-11 17:32:44 +01002626 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002627 activationDesc.m_A = 1.0f;
2628 activationDesc.m_B = 1.0f;
2629 break;
2630 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002631 case ActivationFunction::LeakyReLu:
2632 {
James Ward58dec6b2020-09-11 17:32:44 +01002633 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002634 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002635 activationDesc.m_A = options->alpha;
2636 break;
2637 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002638 case ActivationFunction::Elu:
2639 {
2640 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2641 activationDesc.m_A = 1.0f;
2642 break;
2643 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002644 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002645 {
James Ward58dec6b2020-09-11 17:32:44 +01002646 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002647 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002648 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002649 default:
2650 {
2651 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002652 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2653 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002654 }
2655 }
2656
2657 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002658
Sadik Armagand109a4d2020-07-28 10:42:13 +01002659 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002660 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2661
2662 // register the input connection slots for the layer, connections are made after all layers have been created
2663 // only the tensors for the inputs are relevant, exclude the const tensors
2664 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2665 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2666
2667 // register the output connection slots for the layer, connections are made after all layers have been created
2668 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2669 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2670}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002671armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2672 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002673{
2674 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2675 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2676
2677 if (stretchDim != targetDimsIn.end())
2678 {
2679 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2680 {
2681 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002682 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002683 }
2684
2685 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002686 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002687 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2688
2689 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2690 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2691 }
2692
2693 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2694
2695 TensorInfo reshapeInfo = inputTensorInfo;
2696 reshapeInfo.SetShape(outputShape);
2697
2698 return reshapeInfo;
2699}
2700
Kevin May7d96b162021-02-03 17:38:41 +00002701void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002702{
2703 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2704
2705 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002706
2707 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2708 CHECK_VALID_SIZE(outputs.size(), 1);
2709
Mike Kelly0d77ae12022-01-07 17:42:27 +00002710 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2711 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002712 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002713
2714 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002715 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002716 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002717
Jan Eilersbac9b352020-07-13 13:40:24 +01002718 // Extracting new shape for the output
2719 // There are two ways it can be passed
2720 // * First is to define the target shape in the operator built-in options
2721 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002722 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002723 bool targetShapeFound = false;
2724 // Check if built-in options were given
2725 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002726 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002727 // make sure the parameter is given
2728 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002729 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002730 targetShape = options->new_shape;
2731 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002732 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002733 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002734
2735 // If there is no built-in option given or if the built-in new_shape parameter was empty
2736 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002737 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002738 // Check for a second input tensor
2739 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002740 {
2741 if (inputs[1]->is_variable)
2742 {
2743 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2744 }
2745
2746 if (inputs[1]->shape.size() != 1)
2747 {
2748 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2749 }
2750
2751 if (inputs[1]->type != tflite::TensorType_INT32)
2752 {
2753 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2754 }
2755
Teresa Charlin6a056a42021-12-01 10:25:43 +00002756 // Extract target shape from input
2757 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2758 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002759 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002760 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002761 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2762 {
2763 targetShape.push_back(values[i]);
2764 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002765 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002766 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002767 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002768 try
2769 {
2770 // We attempt to infer during Runtime.
2771 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2772 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2773 if (reshapeShapes[0] > 2)
2774 {
2775 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2776 "When inferring during runtime, the parser only supports "
2777 "shape (batch, -1) or (-1) for target shape input.",
2778 reshapeShapes[0],
2779 layerName,
2780 CHECK_LOCATION().AsString()));
2781 }
2782
2783 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2784 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2785 if (reshapeShapes[0] == 1)
2786 {
2787 targetShape = {numInputElements};
2788 }
2789 else if (reshapeShapes[0] == 2)
2790 {
2791 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2792 }
2793 }
2794 catch (const std::exception& exc)
2795 {
2796 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2797 "Reshape operation. Reshape operator target shape input buffer data "
2798 "is null. " << exc.what());
2799 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002800 }
2801 }
2802 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002803 {
2804 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2805 "At least one method required");
2806 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002807 }
2808
kevmay0171972a82018-12-17 14:28:03 +00002809 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002810 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002811
kevmay0171972a82018-12-17 14:28:03 +00002812 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002813 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2814 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002815 {
2816 std::stringstream ss;
2817 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002818 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002819 << " does not equal output shape "
2820 << actualOutputTensorInfo.GetShape()
2821 << ": "
2822 << CHECK_LOCATION().AsString();
2823 throw ParseException(ss.str());
2824 }
2825
Sadikb94967b2018-09-19 15:30:00 +01002826 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002827 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002828
Sadikb94967b2018-09-19 15:30:00 +01002829 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002830 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002831 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002832
2833 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2834 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2835
2836 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2837 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2838}
2839
Kevin May7d96b162021-02-03 17:38:41 +00002840void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002841{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002842 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2843}
2844
Kevin May7d96b162021-02-03 17:38:41 +00002845void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002846{
2847 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2848}
2849
Kevin May7d96b162021-02-03 17:38:41 +00002850void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002851{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002852 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2853
2854 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2855 CHECK_VALID_SIZE(inputs.size(), 2);
2856
2857 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2858 CHECK_VALID_SIZE(outputs.size(), 1);
2859
2860 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2861
2862 // Data for the parsed tensor args (size) must be stored locally.
2863 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2864
2865 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2866 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2867
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002868 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002869 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002870 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002871 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2872 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002873
James Ward58dec6b2020-09-11 17:32:44 +01002874 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002875
2876 switch (resizeMethod)
2877 {
2878 case ResizeMethod::Bilinear:
2879 {
James Ward58dec6b2020-09-11 17:32:44 +01002880 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002881
2882 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2883 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2884
David Monahan4a0c9b92020-05-30 09:48:39 +01002885 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002886 break;
2887 }
2888 case ResizeMethod::NearestNeighbor:
2889 {
James Ward58dec6b2020-09-11 17:32:44 +01002890 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002891 break;
2892 }
2893 default:
2894 {
2895 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002896 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2897 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002898 }
2899 }
2900
James Conroy05102392020-06-24 15:39:55 +01002901 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002902 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002903 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2904
2905 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2906 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002907 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2908
2909 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2910 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2911
2912 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2913 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2914}
2915
Kevin May7d96b162021-02-03 17:38:41 +00002916void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002917{
2918 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2919
Mike Kelly0d77ae12022-01-07 17:42:27 +00002920 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2921 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002922
2923 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2924
2925 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2926 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2927 CHECK_VALID_SIZE(outputs.size(), 1);
2928
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002929 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2930 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002931
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002932 const unsigned int concatDimInput = static_cast<unsigned int>(
2933 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002934
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002935 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2936 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002937
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002938 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002939
2940 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2941 {
2942 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2943
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002944 // This set up concatDescriptor view origin
2945 armnnUtils::ProcessConcatInputTensorInfo(
2946 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002947 }
2948
James Ward58dec6b2020-09-11 17:32:44 +01002949 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002950 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002951
Jim Flynn906f9462019-05-10 13:55:21 +01002952 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002953 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002954 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002955
James Conroy05102392020-06-24 15:39:55 +01002956 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002957 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002958
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002959 // add fused activation layer
2960 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002961
Sadik Armagan479045b2018-10-01 11:51:37 +01002962 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2963 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2964}
2965
Kevin May7d96b162021-02-03 17:38:41 +00002966void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002967{
2968 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2969
Mike Kelly0d77ae12022-01-07 17:42:27 +00002970 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002971 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2972
2973 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2974
2975 FullyConnectedDescriptor desc;
2976 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002977 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002978
2979 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2980 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2981 CHECK_VALID_SIZE(outputs.size(), 1);
2982
2983 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2984
2985 // Fully Connected Layer accepts two dimensional weights input
2986 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2987 if (weightsDimension != 2)
2988 {
2989 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002990 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2991 "Node {}",
2992 weightsDimension,
2993 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002994 }
2995
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002996 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01002997 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002998
Matthew Sloyan81beae32021-07-13 19:46:11 +01002999 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3000 // Add the first input tensor to the registration list
3001 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3002 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00003003 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003004
3005 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3006
Matthew Sloyan81beae32021-07-13 19:46:11 +01003007 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3008 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003009
Mike Kelly5880b912022-01-28 16:18:54 +00003010 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3011 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3012 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3013 {
3014 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3015 }
3016
Finn Williamsd4fa5452021-03-01 12:31:41 +00003017 if (inputs.size() == 3)
3018 {
3019 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003020 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003021
3022 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3023 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003024
3025 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3026 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3027 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3028 {
3029 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3030 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003031 }
3032
Matthew Sloyan81beae32021-07-13 19:46:11 +01003033 // Filters and biases are always passed to fully connected as inputs
3034 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003035
3036 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003037
Finn Williamsd4fa5452021-03-01 12:31:41 +00003038 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003039 if (inputTensorInfo.GetNumDimensions() > 2)
3040 {
3041 // Add reshape to flatten to 2D [batch_size, input_size],
3042 // where "input_size" corresponds to the number of inputs to the layer,
3043 // matching the second dimension of weights,
3044 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3045 std::vector<unsigned int> reshapedDimensions(2);
3046 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3047 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3048
3049 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3050 {
3051 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003052 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3053 reshapedDimensions[1],
3054 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003055 }
3056
3057 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3058 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3059
James Ward58dec6b2020-09-11 17:32:44 +01003060 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003061 armnn::ReshapeDescriptor reshapeDescriptor;
3062 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3063 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003064
3065 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3066 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3067
3068 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003069 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3070 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3071 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003072 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003073
3074 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003075
Sadik Armagand109a4d2020-07-28 10:42:13 +01003076 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003077 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3078
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003079 // we need to add the activation layer and fortunately we don't need to care about the data layout
3080 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3081 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003082
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003083 // register the output connection slots for the layer, connections are made after all layers have been created
3084 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3085 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3086}
3087
Kevin May7d96b162021-02-03 17:38:41 +00003088void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003089{
3090 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3091
Mike Kelly0d77ae12022-01-07 17:42:27 +00003092 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003093
3094 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3095 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3096 CHECK_VALID_SIZE(outputs.size(), 4);
3097
3098 // Obtain custom options from flexbuffers
3099 auto custom_options = operatorPtr->custom_options;
3100 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3101
3102 // Obtain descriptor information from tf lite
3103 DetectionPostProcessDescriptor desc;
3104 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3105 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3106 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3107 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3108 desc.m_NumClasses = m["num_classes"].AsUInt32();
3109 desc.m_ScaleH = m["h_scale"].AsFloat();
3110 desc.m_ScaleW = m["w_scale"].AsFloat();
3111 desc.m_ScaleX = m["x_scale"].AsFloat();
3112 desc.m_ScaleY = m["y_scale"].AsFloat();
3113
keidav0107d58c72019-02-26 11:57:39 +00003114 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003115 {
keidav0107d58c72019-02-26 11:57:39 +00003116 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003117 }
3118 if (!(m["detections_per_class"].IsNull()))
3119 {
3120 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3121 }
3122
3123 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3124 {
3125 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3126 "must be positive and less than or equal to 1.");
3127 }
3128
3129 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003130 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003131
James Ward58dec6b2020-09-11 17:32:44 +01003132 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003133 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003134 layerName.c_str());
3135
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003136 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003137
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003138 // The model does not specify the output shapes.
3139 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3140 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3141 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3142 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3143 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3144 m_OverridenOutputShapes.push_back({ 1 });
3145
keidav011b3e2ea2019-02-21 10:07:37 +00003146 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3147 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003148 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003149 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3150 }
3151
3152 // Register the input connection slots for the layer, connections are made after all layers have been created
3153 // only the tensors for the inputs are relevant, exclude the const tensors
3154 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3155 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3156
3157 // Register the output connection slots for the layer, connections are made after all layers have been created
3158 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3159 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3160 outputTensorIndexes[1],
3161 outputTensorIndexes[2],
3162 outputTensorIndexes[3]});
3163}
3164
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003165/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003166void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003167{
3168 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3169
3170 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3171 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3172 CHECK_VALID_SIZE(outputs.size(), 1);
3173
3174 if (inputs.size() < 1)
3175 {
3176 throw ParseException("Pack must have at least one input.");
3177 }
3178
3179 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3180 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3181
3182 StackDescriptor desc;
3183 desc.m_Axis = static_cast<uint32_t>(options->axis);
3184 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3185
3186 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3187 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3188 desc.m_InputShape = inputTensorInfo.GetShape();
3189
James Ward58dec6b2020-09-11 17:32:44 +01003190 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003191 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3192
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003193 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003194
Sadik Armagand109a4d2020-07-28 10:42:13 +01003195 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003196 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3197
3198 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3199 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3200
3201 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3202 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3203}
3204
Mike Kelly5880b912022-01-28 16:18:54 +00003205void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3206{
3207 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3208
3209 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3210 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3211
3212 if (inputs.size() < 2)
3213 {
3214 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3215 }
3216
3217 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3218 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3219 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3220 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3221 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3222 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3223
3224 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3225 // Please refer to each operand at
3226 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3227 armnn::LstmInputParams params;
3228
3229 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3230 {
3231 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3232 inputTensorInfo).first;
3233 }
3234
3235 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3236 inputTensorInfo).first;
3237 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3238 inputTensorInfo).first;
3239 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3240 inputTensorInfo).first;
3241
3242 // Recurrent weight tensors of size {n_cell, n_output}
3243 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3244 {
3245 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3246 inputTensorInfo).first;
3247 }
3248
3249 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3250 inputTensorInfo).first;
3251 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3252 inputTensorInfo).first;
3253 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3254 inputTensorInfo).first;
3255
3256 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3257 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3258 {
3259 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3260 inputTensorInfo).first;
3261 }
3262
3263 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3264 {
3265 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3266 inputTensorInfo).first;
3267 }
3268
3269 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3270 {
3271 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3272 inputTensorInfo).first;
3273 }
3274
3275 // Gates bias tensors of size {n_cell}
3276 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3277 {
3278 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3279 inputTensorInfo).first;
3280 }
3281
3282 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3283 inputTensorInfo).first;
3284 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3285 inputTensorInfo).first;
3286 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3287 inputTensorInfo).first;
3288
3289 // Projection weight tensor of size {n_output, n_cell}
3290 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3291 {
3292 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3293 inputTensorInfo).first;
3294 }
3295 // Projection bias tensor of size {n_output}
3296 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3297 {
3298 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3299 inputTensorInfo).first;
3300 }
3301
3302 // These state tensors are defined as variable tensors, and will be modified by this op.
3303 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3304 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3305 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3306 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3307
3308 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3309 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3310 {
3311 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3312 inputTensorInfo).first;
3313 }
3314
3315 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3316 {
3317 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3318 inputTensorInfo).first;
3319 }
3320
3321 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3322 {
3323 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3324 inputTensorInfo).first;
3325 }
3326
3327 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3328 {
3329 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3330 inputTensorInfo).first;
3331 }
3332
3333 // set the layer descriptor
3334 armnn::UnidirectionalSequenceLstmDescriptor desc;
3335 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3336 desc.m_ClippingThresCell = nodeParams->cell_clip;
3337 desc.m_ClippingThresProj = nodeParams->proj_clip;
3338 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3339 || params.m_RecurrentToInputWeights == nullptr
3340 || params.m_InputGateBias == nullptr);
3341 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3342 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3343 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3344 || params.m_ForgetLayerNormWeights != nullptr
3345 || params.m_CellLayerNormWeights != nullptr
3346 || params.m_OutputLayerNormWeights != nullptr);
3347 desc.m_TimeMajor = nodeParams->time_major;
3348
Mike Kellyc0800a32022-06-15 10:57:52 +01003349 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003350 {
3351 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3352 inputTensorInfo).first;
3353 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3354 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3355
3356 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3357 inputTensorInfo).first;
3358 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3359 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3360
3361 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3362 inputTensorInfo).first;
3363 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3364 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3365
3366 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3367 inputTensorInfo).first;
3368 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3369 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3370 }
3371 else
3372 {
3373 float defaultIntermediate = std::pow(2, -12);
3374 desc.m_InputIntermediateScale = defaultIntermediate;
3375 desc.m_ForgetIntermediateScale = defaultIntermediate;
3376 desc.m_CellIntermediateScale = defaultIntermediate;
3377 desc.m_OutputIntermediateScale = defaultIntermediate;
3378 }
3379
Mike Kellyc0800a32022-06-15 10:57:52 +01003380 if (operatorPtr->intermediates.size() > 4)
3381 {
3382 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3383 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003384
Mike Kellyc0800a32022-06-15 10:57:52 +01003385 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3386 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3387 }
Mike Kelly5880b912022-01-28 16:18:54 +00003388 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3389 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3390 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3391
3392 armnn::DataType dataType = inputTensorInfo.GetDataType();
3393 float qScale = inputTensorInfo.GetQuantizationScale();
3394 float qOffset = inputTensorInfo.GetQuantizationOffset();
3395
3396 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3397 if (!desc.m_CifgEnabled)
3398 {
3399 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3400 }
3401 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3402 cellStateInInfo.GetDataType(),
3403 cellStateInInfo.GetQuantizationScale(),
3404 cellStateInInfo.GetQuantizationOffset());
3405 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3406
3407 armnn::LstmInputParamsInfo paramsInfo;
3408 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3409 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3410 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3411 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3412 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3413 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3414 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3415 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3416 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3417
3418 if (!desc.m_CifgEnabled)
3419 {
3420 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3421 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3422 if (params.m_CellToInputWeights != nullptr)
3423 {
3424 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3425 }
3426 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3427 }
3428
3429 if (desc.m_ProjectionEnabled)
3430 {
3431 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3432 if (params.m_ProjectionBias != nullptr)
3433 {
3434 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3435 }
3436 }
3437
3438 if (desc.m_PeepholeEnabled)
3439 {
3440 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3441 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3442 }
3443
3444 if (desc.m_LayerNormEnabled)
3445 {
3446 if(!desc.m_CifgEnabled)
3447 {
3448 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3449 }
3450 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3451 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3452 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3453 }
3454
3455 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3456 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3457 ARMNN_ASSERT(layer != nullptr);
3458
3459 // register the input connection slots for the layer, connections are made after all layers have been created
3460 // only the tensors for the inputs are relevant, exclude the const tensors
3461 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3462 operatorPtr->inputs[18],
3463 operatorPtr->inputs[19]});
3464 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3465 inputTensorIndexes[1],
3466 inputTensorIndexes[2]});
3467
3468 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3469
3470 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3471 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3472 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3473
3474 unsigned int tensorIndex = outputTensorIndexes[0];
3475 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3476 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3477}
3478
Kevin May7d96b162021-02-03 17:38:41 +00003479void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003480{
3481 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3482
Mike Kelly0d77ae12022-01-07 17:42:27 +00003483 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3484 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003485
3486 // This unpackAxis indicates the axis to unpack
3487 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3488
3489 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3490 CHECK_VALID_SIZE(inputs.size(), 1);
3491
3492 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003493
3494 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3495 {
3496 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003497 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3498 "the number of input dimension {} {}",
3499 unpackAxis,
3500 inputTensorInfo.GetNumDimensions(),
3501 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003502 }
3503
Nina Drozd200e3802019-04-15 09:47:39 +01003504 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3505 // If num is not defined, automatically infer from the length of the dimension axis.
3506 if(unpackNum == 0)
3507 {
3508 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3509 }
3510
3511 // If unpack number cannot be inferred and is still zero, throw ParseException.
3512 if(unpackNum == 0)
3513 {
3514 throw ParseException("Number to unpack must greater than zero.");
3515 }
3516
3517 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3518 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3519
3520 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3521 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3522
3523 // Add current input shape to unpackDimSizes
3524 for (unsigned int i = 0; i < inputDimSize; ++i)
3525 {
3526 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3527 }
3528
3529 if (unpackDimSizes[unpackAxis] != unpackNum)
3530 {
3531 throw ParseException("Number to unpack must be the same as length of the dimension to "
3532 "unpack along.");
3533 }
3534
3535 unpackDimSizes[unpackAxis] /= unpackNum;
3536
3537 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3538 for (unsigned int j = 0; j < unpackNum; ++j)
3539 {
3540 // Set the size of the views.
3541 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3542 {
3543 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3544 }
3545 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3546 }
3547
James Ward58dec6b2020-09-11 17:32:44 +01003548 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003549 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003550 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003551
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003552 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3553 unpackDimSizes.data());
3554
Nina Drozd200e3802019-04-15 09:47:39 +01003555 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3556 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3557
Finn Williamsb49ed182021-06-29 15:50:08 +01003558 std::vector<unsigned int> reshapeDims;
3559 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3560 {
3561 if (axis != unpackAxis)
3562 {
3563 reshapeDims.push_back(splitOutShape[axis]);
3564 }
3565 }
3566
3567 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3568
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003569 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3570 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3571 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003572 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003573 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003574 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003575 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003576 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3577
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003578 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3579 outputTensorInfo.GetDataType(),
3580 outputTensorInfo.GetQuantizationScale(),
3581 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003582 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3583
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003584 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003585
3586 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3587 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3588 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3589 }
Nina Drozd200e3802019-04-15 09:47:39 +01003590}
3591
Kevin May7d96b162021-02-03 17:38:41 +00003592void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003593{
3594 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3595
Mike Kelly0d77ae12022-01-07 17:42:27 +00003596 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3597 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003598
3599 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3600
Nina Drozd200e3802019-04-15 09:47:39 +01003601 // If number of splits cannot be inferred and is zero, throw ParseException.
3602 if(numSplits == 0)
3603 {
3604 throw ParseException("Number to splits must greater than zero.");
3605 }
3606
Nina Drozd0324f482019-04-08 10:52:10 +01003607 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3608 CHECK_VALID_SIZE(inputs.size(), 2);
3609 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3610 CHECK_VALID_SIZE(outputs.size(), numSplits);
3611
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003612 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3613 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3614 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003615
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003616 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003617 if (axisBufferPtr == nullptr)
3618 {
3619 throw ParseException(
3620 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3621 CHECK_LOCATION().AsString()));
3622 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003623
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003624 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3625 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3626 int32_t axis = axisData[0];
3627
3628 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3629 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3630 {
3631 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3632 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3633 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3634 throw ParseException(
3635 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3636 axis,
3637 CHECK_LOCATION().AsString()));
3638 }
3639
3640 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003641
Nina Drozd0324f482019-04-08 10:52:10 +01003642 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003643 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003644 {
3645 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003646 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3647 inputTensorInfo.GetNumDimensions(),
3648 MaxNumOfTensorDimensions,
3649 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003650 }
3651
3652 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3653
3654 // Add current input shape to splitterDimSizes
3655 for (unsigned int i = 0; i < inputDimSize; ++i)
3656 {
3657 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3658 }
3659
3660 if (splitterDimSizes[splitDim] % numSplits != 0)
3661 {
3662 throw ParseException("Number of splits must evenly divide the dimension");
3663 }
3664 splitterDimSizes[splitDim] /= numSplits;
3665
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003666 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003667 for (unsigned int j = 0; j < numSplits; ++j)
3668 {
3669 // Set the size of the views.
3670 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3671 {
3672 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3673 }
3674 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3675 }
3676
James Ward58dec6b2020-09-11 17:32:44 +01003677 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003678 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003679 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003680
3681 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003682 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003683
Nina Drozd0324f482019-04-08 10:52:10 +01003684 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3685 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003686 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003687 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003688 }
3689
3690 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3691 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3692}
3693
Derek Lambertif0176992020-04-28 13:37:49 +01003694unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3695{
3696 int numDims = armnn::numeric_cast<int>(numDimsIn);
3697 int v = idx < 0 ? numDims + idx : idx;
3698 ARMNN_ASSERT(v >= 0);
3699 ARMNN_ASSERT(v < numDims);
3700
3701 return static_cast<unsigned int>(v);
3702}
3703
Kevin May7d96b162021-02-03 17:38:41 +00003704void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003705{
3706 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3707
Mike Kelly0d77ae12022-01-07 17:42:27 +00003708 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3709 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003710
3711 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3712 CHECK_VALID_SIZE(inputs.size(), 3);
3713
3714 auto& inputTensor = inputs[0];
3715 auto& splitsTensor = inputs[1];
3716 auto& axisTensor = inputs[2];
3717
3718 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3719 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3720 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3721 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3722
3723 // Inputs
3724 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3725 if (inputDimSize > MaxNumOfTensorDimensions)
3726 {
3727 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003728 fmt::format("The number of dimensions: {} for input tensors of the "
3729 "SplitV op cannot be greater than {} {}",
3730 inputTensorInfo.GetNumDimensions(),
3731 MaxNumOfTensorDimensions,
3732 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003733 }
3734
3735 // Get split axis
3736 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003737 if (axisBufferPtr == nullptr)
3738 {
3739 throw ParseException(
3740 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3741 CHECK_LOCATION().AsString()));
3742 }
3743
Derek Lambertif0176992020-04-28 13:37:49 +01003744 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3745 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003746 int32_t axis = axisData[0];
3747
3748 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3749 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3750 {
3751 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3752 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3753 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3754 throw ParseException(
3755 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3756 axis,
3757 CHECK_LOCATION().AsString()));
3758 }
3759 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003760
Derek Lambertif0176992020-04-28 13:37:49 +01003761 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003762 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003763 unsigned int numSplits{0};
3764
3765 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003766 {
3767 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003768 }
3769 else
3770 {
Ryan OShea86704732020-05-26 11:41:04 +01003771 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003772 }
3773
3774 if (numSplits <=0)
3775 {
3776 throw ParseException("SplitV has invalid number of splits");
3777 }
3778
Jan Eilersc0761e92020-06-29 16:48:44 +01003779 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003780 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003781 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003782
Jan Eilersc0761e92020-06-29 16:48:44 +01003783 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003784 int numInferred{0};
3785 unsigned int inferIdx{0};
3786 int splitSum{0};
3787 for (auto split : splitsData)
3788 {
3789 if (split < 0)
3790 {
3791 numInferred++;
3792 inferIdx = idx;
3793 }
3794 else
3795 {
3796 splitSum += split;
3797 }
3798 idx++;
3799 }
3800 // Check for inferred Axis
3801 if (numInferred == 0)
3802 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003803 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003804 {
3805 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3806 }
3807 }
3808 else if (numInferred == 1)
3809 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003810 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003811 }
3812 else
3813 {
3814 throw ParseException("Cannot infer split size for more than one split");
3815 }
3816
Derek Lambertif0176992020-04-28 13:37:49 +01003817 //Ouput size validation
3818 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3819 CHECK_VALID_SIZE(outputs.size(), numSplits);
3820
3821 // Setup Armnn descriptor
3822 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3823 unsigned int accumSplit = 0;
3824 for (unsigned int j = 0; j < numSplits; ++j)
3825 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003826 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003827
3828 // Set the size of the views.
3829 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3830 {
3831 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3832 if (dimIdx == splitDim)
3833 {
3834 dimSize = splitSize;
3835 }
3836 splitDesc.SetViewSize(j, dimIdx, dimSize);
3837 }
3838
3839 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3840 accumSplit += splitSize;
3841 }
3842
James Ward58dec6b2020-09-11 17:32:44 +01003843 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003844 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003845 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003846
3847 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3848 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3849
3850 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3851 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003852 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003853 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3854 }
3855
3856 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3857 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3858}
3859
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003860void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3861{
3862 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3863}
3864
Kevin May7d96b162021-02-03 17:38:41 +00003865void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003866{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003867 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3868}
3869
3870void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3871{
Inki Daed4619e22020-09-10 15:33:54 +09003872 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3873 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3874 CHECK_VALID_SIZE(inputs.size(), 2);
3875
3876 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3877 CHECK_VALID_SIZE(outputs.size(), 1);
3878
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003879 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3880 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003881 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003882 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003883
3884 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003885 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3886 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3887 {
3888 throw ParseException(
3889 fmt::format(
3890 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3891 CHECK_LOCATION().AsString()));
3892 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003893
3894 // Get const axis value from model and set it to descriptor.
3895 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3896 if (axisBufferPtr == nullptr)
3897 {
3898 throw ParseException(
3899 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3900 CHECK_LOCATION().AsString()));
3901 }
3902
3903 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3904 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3905 int32_t axis = axisData.front();
3906
3907 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3908 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3909 {
3910 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3911 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3912 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3913 throw ParseException(
3914 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3915 axis,
3916 CHECK_LOCATION().AsString()));
3917 }
3918
3919 ArgMinMaxDescriptor desc;
3920 desc.m_Axis = axis;
3921 desc.m_Function = argMinMaxFunction;
3922
3923 // Register a ArgMin/ArgMax layer.
3924 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3925 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3926 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3927 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003928 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3929
3930 // Register input tensor to the layer.
3931 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3932 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3933
3934 // Register output tensor to the layer.
3935 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3936 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3937}
3938
Kevin May7d96b162021-02-03 17:38:41 +00003939void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003940{
3941 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3942
Kevin May7d96b162021-02-03 17:38:41 +00003943 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003944 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003945 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003946 CHECK_VALID_SIZE(outputs.size(), 1);
3947
3948 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3949 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3950 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3951
3952 armnn::GatherDescriptor gatherDescriptor;
3953
Mike Kelly0d77ae12022-01-07 17:42:27 +00003954 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3955 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00003956 auto axis = options->axis;
3957
3958 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3959 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3960 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3961 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3962 {
3963 throw ParseException(
3964 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3965 axis,
3966 inputDimensions, inputDimensions,
3967 CHECK_LOCATION().AsString()));
3968 }
3969 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3970 {
3971 throw ParseException(
3972 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3973 outputDimensions,
3974 inputDimensions, indicesDimensions,
3975 CHECK_LOCATION().AsString()));
3976 }
3977
3978 gatherDescriptor.m_Axis = axis;
3979
3980 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3981 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3982 ARMNN_ASSERT(layer != nullptr);
3983 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3984
3985 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3986 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3987
3988 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3989 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3990}
3991
Teresa Charlin91a53ea2022-04-25 15:47:29 +01003992void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
3993{
3994 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3995
3996 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3997 CHECK_VALID_SIZE(inputs.size(), 2);
3998 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3999 CHECK_VALID_SIZE(outputs.size(), 1);
4000
4001 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4002 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
4003 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4004
4005 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4006 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4007 ARMNN_ASSERT(layer != nullptr);
4008 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4009
4010 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4011 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4012
4013 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4014 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4015}
4016
Kevin May7d96b162021-02-03 17:38:41 +00004017void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004018{
4019 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4020
Kevin May7d96b162021-02-03 17:38:41 +00004021 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004022 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004023 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004024 CHECK_VALID_SIZE(outputs.size(), 1);
4025
4026 armnn::DepthToSpaceDescriptor descriptor;
4027
Mike Kelly0d77ae12022-01-07 17:42:27 +00004028 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4029 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004030 auto blockSize = options->block_size;
4031 if (blockSize < 2)
4032 {
4033 throw ParseException(
4034 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4035 blockSize,
4036 CHECK_LOCATION().AsString()));
4037 }
4038 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4039
4040 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4041 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4042 ARMNN_ASSERT(layer != nullptr);
4043 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4044 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4045
4046 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4047 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4048
4049 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4050 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4051}
4052
Kevin May7d96b162021-02-03 17:38:41 +00004053void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004054{
Sadik Armagana2747482021-02-09 10:28:54 +00004055 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4056}
4057
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004058void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4059{
4060 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4061}
4062
Sadik Armagana2747482021-02-09 10:28:54 +00004063void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4064{
4065 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4066}
4067
4068void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4069{
4070 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4071}
4072
4073void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4074{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004075 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4076
Mike Kelly0d77ae12022-01-07 17:42:27 +00004077 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4078 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004079
4080 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4081 CHECK_VALID_SIZE(inputs.size(), 2);
4082
4083 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4084 CHECK_VALID_SIZE(outputs.size(), 1);
4085
Sadik Armagana2747482021-02-09 10:28:54 +00004086 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004087
4088 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4089 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004090
4091 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004092 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4093 // Get const axis value from model and set it to descriptor.
4094 if (axisBufferPtr != nullptr)
4095 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004096 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4097 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4098
4099 // Convert the axis to unsigned int and remove duplicates.
4100 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4101 std::set<unsigned int> uniqueAxis;
4102 std::transform(axisData.begin(),
4103 axisData.end(),
4104 std::inserter(uniqueAxis, uniqueAxis.begin()),
4105 [rank](int i)->unsigned int{
4106 return static_cast<uint32_t>(((i + rank) % rank)); });
4107 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004108 }
Sadik Armagana2747482021-02-09 10:28:54 +00004109 else
4110 {
4111 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4112 {
4113 desc.m_vAxis.push_back(i);
4114 }
4115 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004116
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004117 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004118 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004119
4120 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004121 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004122
4123 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4124 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4125
4126 // Register input tensor to the layer.
4127 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4128 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4129
4130 // Register output tensor to the layer.
4131 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4132 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4133}
4134
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004135void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4136{
4137 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4138}
4139
4140void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4141{
4142 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4143}
4144
Mike Kelly31dce2b2021-09-01 21:22:37 +01004145void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4146{
4147 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4148
4149 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4150 CHECK_VALID_SIZE(inputs.size(), 1);
4151
4152 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4153 CHECK_VALID_SIZE(outputs.size(), 1);
4154
4155 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4156 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4157
4158 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4159
4160 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4161 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4162
4163 armnn::NormalizationDescriptor descriptor;
4164 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4165 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4166 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4167 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4168 descriptor.m_K = options->bias;
4169 descriptor.m_Alpha = options->alpha;
4170 descriptor.m_Beta = options->beta;
4171
4172 // ArmNN expects normSize to be the full size of the normalization
4173 // window rather than the radius as in TfLite.
4174 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4175
4176 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4177 ARMNN_ASSERT(layer != nullptr);
4178
4179 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4180 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4181
4182 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4183 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4184
4185 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4186 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4187}
4188
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004189void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4190{
4191 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4192}
4193
4194void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4195{
4196 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4197}
4198
4199void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4200{
4201 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4202}
4203
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004204void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4205{
4206 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4207}
4208
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004209void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4210{
4211 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4212
4213 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4214 CHECK_VALID_SIZE(inputs.size(), 1);
4215
4216 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4217 CHECK_VALID_SIZE(outputs.size(), 1);
4218
4219 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4220 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4221
4222 ElementwiseUnaryDescriptor desc;
4223 desc.m_Operation = unaryOperation;
4224 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4225 ARMNN_ASSERT(layer != nullptr);
4226
4227 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4228 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4229
4230 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4231 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4232
4233 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4234 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4235}
4236
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004237void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4238{
4239 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4240}
4241
4242void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4243{
4244 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4245}
4246
4247void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4248{
4249 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4250}
4251
4252void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4253{
4254 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4255}
4256
4257void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4258{
4259 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4260}
4261
4262void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4263{
4264 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4265}
4266
4267void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4268 ComparisonOperation comparisonOperation)
4269{
4270 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4271
4272 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4273 CHECK_VALID_SIZE(inputs.size(), 2);
4274
4275 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4276 CHECK_VALID_SIZE(outputs.size(), 1);
4277
4278 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4279 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4280
4281 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4282 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4283 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4284
4285 ComparisonDescriptor desc;
4286 desc.m_Operation = comparisonOperation;
4287 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4288 ARMNN_ASSERT(layer != nullptr);
4289
4290 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4291 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4292
4293 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4294 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4295
4296 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4297 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4298}
4299
Kevin May7d96b162021-02-03 17:38:41 +00004300armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4301 unsigned int outputSlot,
4302 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004303{
4304 ActivationDescriptor activationDesc;
4305 std::string layerName = prevLayer->GetName();
4306
4307 switch(activationType)
4308 {
4309 case tflite::ActivationFunctionType_NONE:
4310 {
4311 // this is a no-op: return previous layer
4312 return prevLayer;
4313 }
4314 case tflite::ActivationFunctionType_RELU:
4315 {
4316 activationDesc.m_Function = ActivationFunction::ReLu;
4317 layerName += ":RELU";
4318 break;
4319 }
4320 case tflite::ActivationFunctionType_RELU6:
4321 {
4322 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4323 activationDesc.m_A = 6.0f;
4324 activationDesc.m_B = 0.0f;
4325 layerName += ":RELU6";
4326 break;
4327 }
4328 case tflite::ActivationFunctionType_TANH:
4329 {
4330 activationDesc.m_Function = ActivationFunction::TanH;
4331 activationDesc.m_A = 1.0f;
4332 activationDesc.m_B = 1.0f;
4333 layerName += ":TANH";
4334 break;
4335 }
4336
4337 // I only put these here as a reminder what others we could support
4338 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4339 case tflite::ActivationFunctionType_SIGN_BIT:
4340 default:
4341 {
4342 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004343 fmt::format("TfLite parser doesn't suppport fused activation: "
4344 "{}/{} {} ",
4345 activationType,
4346 tflite::EnumNameActivationFunctionType(activationType),
4347 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004348
4349 }
4350 }
4351
4352 IConnectableLayer* activationLayer =
4353 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4354
4355 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4356 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4357 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4358 return activationLayer;
4359}
4360
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004361armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4362 unsigned int outputSlot)
4363{
Teresa Charlin725728e2022-05-05 13:33:33 +01004364
4365 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4366 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4367
4368 if (dataType == DataType::Signed32)
4369 {
4370 return prevLayer;
4371 }
4372
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004373 std::string layerName = prevLayer->GetName();
4374 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4375
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004376 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4377 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004378
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004379 return floorLayer;
4380}
4381
Mike Kelly0d77ae12022-01-07 17:42:27 +00004382TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004383{
4384 if (fileName == nullptr)
4385 {
James Ward58dec6b2020-09-11 17:32:44 +01004386 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004387 CHECK_LOCATION().AsString()));
4388 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004389 std::error_code errorCode;
4390 fs::path pathToFile(fileName);
4391 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004392 {
James Ward58dec6b2020-09-11 17:32:44 +01004393 //fmt::format() could not be used here (format error)
4394 std::stringstream msg;
4395 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4396 << " " << CHECK_LOCATION().AsString();
4397
4398 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004399 }
4400 std::ifstream file(fileName, std::ios::binary);
4401 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4402 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4403 fileContent.size());
4404}
4405
Mike Kelly0d77ae12022-01-07 17:42:27 +00004406TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004407{
4408 if (binaryContent == nullptr)
4409 {
James Ward58dec6b2020-09-11 17:32:44 +01004410 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004411 CHECK_LOCATION().AsString()));
4412 }
4413 flatbuffers::Verifier verifier(binaryContent, len);
4414 if (verifier.VerifyBuffer<tflite::Model>() == false)
4415 {
4416 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004417 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4418 "flatbuffers format. size:{} {}",
4419 len,
4420 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004421 }
4422 return tflite::UnPackModel(binaryContent);
4423}
4424
Mike Kelly0d77ae12022-01-07 17:42:27 +00004425TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004426 size_t subgraphIndex,
4427 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004428{
4429 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4430
Mike Kelly0d77ae12022-01-07 17:42:27 +00004431 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4432 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004433
4434 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004435 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004436 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004437 {
mathad01c21025d2021-04-26 10:09:37 +01004438 // If the input location is -1 then assume input is turned off.
4439 if (operatorPtr->inputs[i] == -1)
4440 {
4441 continue;
4442 }
4443 else
4444 {
4445 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4446 result.push_back(subgraphPtr->tensors[inputId].get());
4447 }
telsoa01c577f2c2018-08-31 09:22:23 +01004448 }
4449 return result;
4450}
4451
Mike Kelly0d77ae12022-01-07 17:42:27 +00004452TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004453 size_t subgraphIndex,
4454 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004455{
4456 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4457
Mike Kelly0d77ae12022-01-07 17:42:27 +00004458 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4459 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004460
4461 size_t outputCount = operatorPtr->outputs.size();
4462 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004463 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004464 {
4465 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4466 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004467 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004468 }
4469 return result;
4470}
4471
Mike Kelly0d77ae12022-01-07 17:42:27 +00004472TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004473 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004474{
4475 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004476 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004477
Derek Lambertiff05cc52019-04-26 13:05:17 +01004478 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004479 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004480 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004481 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004482 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004483 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004484 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004485 }
4486 return result;
4487}
4488
Mike Kelly0d77ae12022-01-07 17:42:27 +00004489TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004490 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004491{
4492 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004493 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004494
Derek Lambertiff05cc52019-04-26 13:05:17 +01004495 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004496 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004497 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004498 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004499 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4500 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004501 }
4502 return result;
4503}
4504
Kevin May7d96b162021-02-03 17:38:41 +00004505std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4506 size_t subgraphIndex,
4507 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004508{
4509 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004510 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4511 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004512 return operatorPtr->inputs;
4513}
4514
Kevin May7d96b162021-02-03 17:38:41 +00004515std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4516 size_t subgraphIndex,
4517 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004518{
4519 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004520 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4521 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004522 return operatorPtr->outputs;
4523}
4524
Kevin May7d96b162021-02-03 17:38:41 +00004525void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4526 size_t operatorIndex,
4527 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004528 const std::vector<unsigned int>& tensorIndexes,
4529 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004530{
4531 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004532 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004533
Finn Williamsd4fa5452021-03-01 12:31:41 +00004534 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004535 {
4536 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004537 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4538 " for subgraph:{} operator index:{} {}",
4539 tensorIndexes.size(),
4540 layer->GetNumInputSlots(),
4541 subgraphIndex,
4542 operatorIndex,
4543 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004544 }
4545
Finn Williamsd4fa5452021-03-01 12:31:41 +00004546 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004547 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004548 unsigned int tensorIndex = tensorIndexes[index];
4549 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004550 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4551 }
4552}
4553
Kevin May7d96b162021-02-03 17:38:41 +00004554void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4555 size_t operatorIndex,
4556 IConnectableLayer* layer,
4557 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004558{
4559 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004560 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004561 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4562 {
4563 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004564 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4565 " for subgraph:{} operator index:{} {}",
4566 tensorIndexes.size(),
4567 layer->GetNumOutputSlots(),
4568 subgraphIndex,
4569 operatorIndex,
4570 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004571 }
4572
4573 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4574 {
4575 unsigned int tensorIndex = tensorIndexes[slotIndex];
4576 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4577 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4578 }
4579}
4580
Kevin May7d96b162021-02-03 17:38:41 +00004581void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004582{
4583 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4584
4585 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004586 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004587 {
4588 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4589 IConnectableLayer* layer =
4590 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4591
4592 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4593 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4594
4595 RegisterOutputSlots(subgraphIndex,
4596 VIRTUAL_OPERATOR_ID,
4597 layer,
4598 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4599 }
4600}
4601
Kevin May7d96b162021-02-03 17:38:41 +00004602void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004603{
4604 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4605
4606 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004607 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004608 {
4609 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4610 IConnectableLayer* layer =
4611 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4612
4613 RegisterInputSlots(subgraphIndex,
4614 VIRTUAL_OPERATOR_ID,
4615 layer,
4616 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4617 }
4618}
4619
Mike Kelly5880b912022-01-28 16:18:54 +00004620void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004621{
Mike Kelly5880b912022-01-28 16:18:54 +00004622 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004623
Mike Kelly5880b912022-01-28 16:18:54 +00004624 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004625 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4626 {
4627 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4628 {
4629 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4630 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4631 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004632 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004633
Mike Kelly5880b912022-01-28 16:18:54 +00004634 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004635 {
4636 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004637 armnn::DataType dataType = tensorInfo.GetDataType();
4638
4639 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4640 != m_ConstantsToDequantize.end())
4641 {
4642 dataType = DataType::Float32;
4643 }
4644 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4645
4646 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4647 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4648
4649 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4650 RegisterOutputSlots(subgraphIndex,
4651 VIRTUAL_OPERATOR_ID,
4652 layer,
4653 { tensorIndex });
4654 }
4655 else if (ShouldConstantTensorBeCreated(tensorIndex))
4656 {
4657 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4658 armnn::DataType dataType = tensorInfo.GetDataType();
4659
4660 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4661 != m_ConstantsToDequantize.end())
4662 {
4663 dataType = DataType::Float32;
4664 }
4665 // Make sure isConstant flag is set.
4666 tensorInfo.SetConstant();
4667 tensorInfo.SetDataType(dataType);
4668
4669 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004670
Matthew Sloyan81beae32021-07-13 19:46:11 +01004671 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004672 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004673
Matthew Sloyan81beae32021-07-13 19:46:11 +01004674 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4675 RegisterOutputSlots(subgraphIndex,
4676 VIRTUAL_OPERATOR_ID,
4677 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004678 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004679 }
4680 else
4681 {
4682 throw ParseException(
4683 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4684 CHECK_LOCATION().AsString()));
4685 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004686 }
4687 }
4688 }
4689}
4690
telsoa01c577f2c2018-08-31 09:22:23 +01004691// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004692TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004693{
4694 CHECK_BUFFER(model, bufferIndex);
4695 return model->buffers[bufferIndex].get();
4696}
4697
Matteo Martincigh747ef822018-12-18 09:26:39 +00004698template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004699std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4700TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4701 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004702 armnn::TensorInfo& tensorInfo,
4703 armnn::Optional<armnn::PermutationVector&> permutationVector)
4704{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004705 // Make sure isConstant flag is set.
4706 tensorInfo.SetConstant();
4707
Matteo Martincigh747ef822018-12-18 09:26:39 +00004708 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4709 tensorPtr,
4710 tensorInfo,
4711 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004712 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004713 return std::make_pair(constData.first, std::move(storage));
4714}
4715
Mike Kelly5880b912022-01-28 16:18:54 +00004716bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4717{
4718 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4719 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4720 != m_ConstantsToBeCreated.end());
4721}
4722
Finn Williamsd4fa5452021-03-01 12:31:41 +00004723bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4724{
4725 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004726 bool isConst = true;
4727
4728 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4729 if (buffer->data.size() == 0)
4730 {
4731 isConst = false;
4732 }
4733
4734 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004735}
4736
Kevin May7d96b162021-02-03 17:38:41 +00004737std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004738TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4739 armnn::TensorInfo& tensorInfo,
4740 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004741{
4742 CHECK_TENSOR_PTR(tensorPtr);
4743 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4744 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4745
Matthew Sloyan81beae32021-07-13 19:46:11 +01004746 // Make sure isConstant flag is set.
4747 tensorInfo.SetConstant();
4748
telsoa01c577f2c2018-08-31 09:22:23 +01004749 switch (tensorInfo.GetDataType())
4750 {
4751 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004752 return CreateConstTensorAndStoreData<float>(bufferPtr,
4753 tensorPtr,
4754 tensorInfo,
4755 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004756 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004757 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4758 tensorPtr,
4759 tensorInfo,
4760 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004761 case armnn::DataType::QSymmS8:
4762 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4763 tensorPtr,
4764 tensorInfo,
4765 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004766 case armnn::DataType::QAsymmS8:
4767 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4768 tensorPtr,
4769 tensorInfo,
4770 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004771 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004772 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4773 tensorPtr,
4774 tensorInfo,
4775 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004776 default:
4777 {
4778 std::stringstream errString;
4779 errString << "Unexpected datatype when creating const tensor: "
4780 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4781 << " shape:" << tensorInfo.GetShape()
4782 << CHECK_LOCATION().AsString();
4783 throw ParseException(errString.str());
4784 }
4785 }
4786}
4787
Finn Williamsd4fa5452021-03-01 12:31:41 +00004788armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4789 armnn::TensorInfo& tensorInfo)
4790{
4791 CHECK_TENSOR_PTR(tensorPtr);
4792 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4793 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4794
Matthew Sloyan81beae32021-07-13 19:46:11 +01004795 // Make sure isConstant flag is set.
4796 tensorInfo.SetConstant();
4797
Finn Williamsd4fa5452021-03-01 12:31:41 +00004798 return ConstTensor(tensorInfo, bufferPtr->data.data());
4799}
4800
Mike Kelly5880b912022-01-28 16:18:54 +00004801std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4802TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4803 armnn::TensorInfo& tensorInfo,
4804 armnn::DataType inputDataType)
4805{
4806 CHECK_TENSOR_PTR(tensorPtr);
4807 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4808 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4809
4810 // Make sure isConstant flag is set.
4811 tensorInfo.SetConstant();
4812
4813 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4814 {
4815 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4816 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4817 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4818 }
4819 else
4820 {
4821 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4822 }
4823}
4824
4825std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4826TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4827{
4828 CHECK_TENSOR_PTR(tensorPtr);
4829 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4830 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4831 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4832
4833 // Make sure isConstant flag is set.
4834 tensorInfo.SetConstant();
4835
4836 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4837 {
4838 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4839 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4840 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4841 }
4842 else
4843 {
4844 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4845 }
4846}
4847
Kevin May7d96b162021-02-03 17:38:41 +00004848BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4849 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004850{
4851 CHECK_SUBGRAPH(m_Model, subgraphId);
4852 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004853 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004854 {
4855 if (input.second->name == name)
4856 {
4857 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004858 auto inputTensorInfo = ToTensorInfo(input.second);
4859 // Input tensors are always treated as constant tensors during network execution.
4860 inputTensorInfo.SetConstant(true);
4861 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004862 }
4863 }
4864
4865 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004866 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004867 {
4868 bindings << "'" << input.second->name << "' ";
4869 }
4870
4871 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004872 fmt::format("No input binding found for subgraph:{} and name:{}. "
4873 "Possible inputs are: [{}] {}",
4874 subgraphId,
4875 name,
4876 bindings.str(),
4877 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004878}
4879
Kevin May7d96b162021-02-03 17:38:41 +00004880BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4881 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004882{
4883 CHECK_SUBGRAPH(m_Model, subgraphId);
4884 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004885 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004886 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004887 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004888 if (output.second->name == name)
4889 {
4890 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004891 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4892 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4893 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004894 }
4895 }
4896
4897 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004898 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004899 {
4900 bindings << "'" << output.second->name << "' ";
4901 }
4902
4903 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004904 fmt::format("No output binding found for subgraph:{} and name:{}. "
4905 "Possible outputs are: [{}] {}",
4906 subgraphId,
4907 name,
4908 bindings.str(),
4909 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004910}
4911
Kevin May7d96b162021-02-03 17:38:41 +00004912size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004913{
4914 return m_Model->subgraphs.size();
4915}
4916
Kevin May7d96b162021-02-03 17:38:41 +00004917std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004918{
4919 CHECK_SUBGRAPH(m_Model, subgraphId);
4920 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4921 std::vector<std::string> result;
4922 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004923 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004924 {
4925 result.push_back(input.second->name);
4926 }
4927 return result;
4928}
4929
Kevin May7d96b162021-02-03 17:38:41 +00004930std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004931{
4932 CHECK_SUBGRAPH(m_Model, subgraphId);
4933 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4934 std::vector<std::string> result;
4935 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004936 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004937 {
4938 result.push_back(output.second->name);
4939 }
4940 return result;
4941}
4942
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004943const std::string TfLiteParserImpl::GetVersion()
4944{
4945 return TFLITE_PARSER_VERSION;
4946}
4947
Mike Kelly0d77ae12022-01-07 17:42:27 +00004948TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004949: m_FloatData(std::move(data))
4950, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004951, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004952, m_Int32Data(nullptr)
4953{
4954}
4955
Mike Kelly0d77ae12022-01-07 17:42:27 +00004956TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004957: m_FloatData(nullptr)
4958, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004959, m_Int8Data(nullptr)
4960, m_Int32Data(nullptr)
4961{
4962}
4963
Mike Kelly0d77ae12022-01-07 17:42:27 +00004964TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00004965: m_FloatData(nullptr)
4966, m_Uint8Data(nullptr)
4967, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01004968, m_Int32Data(nullptr)
4969{
4970}
4971
Mike Kelly0d77ae12022-01-07 17:42:27 +00004972TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004973: m_FloatData(nullptr)
4974, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004975, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004976, m_Int32Data(std::move(data))
4977{
4978}
4979
4980} // armnnTfLiteParser