blob: 5dbb5ee503320eedd2d63fdd0dce43e8454601cf [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Teresa Charlin455172a2022-06-29 15:35:57 +01002// Copyright © 2022 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;
Teresa Charlin455172a2022-06-29 15:35:57 +0100713 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
Kevin May7d96b162021-02-03 17:38:41 +0000714 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
715 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
716 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
717 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
718 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100719 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000720 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
721 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300722 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000723 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
724 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000725 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100726 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000727 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
728 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
729 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000730 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
731 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100732 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000733 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
734 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
735 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100736 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100737 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100738 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Kevin May7d96b162021-02-03 17:38:41 +0000739 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
740 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
741 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
742 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
743 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
744 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
745 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
746 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
747 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
748 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
749 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
750 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000751 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
752 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000753 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100754
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100755 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000756 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100757}
758
Kevin May7d96b162021-02-03 17:38:41 +0000759void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100760{
761 m_Network = armnn::INetworkPtr(nullptr, nullptr);
762 m_Model = nullptr;
763 m_SubgraphConnections.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000764 m_OverridenOutputShapes.clear();
765 m_ConstantsToDequantize.clear();
766 m_ConstantsToBeCreated.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100767}
768
Kevin May7d96b162021-02-03 17:38:41 +0000769INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100770{
771 ResetParser();
772 m_Model = LoadModelFromFile(graphFile);
773 return CreateNetworkFromModel();
774}
775
Mike Kelly0d77ae12022-01-07 17:42:27 +0000776INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100777{
778 ResetParser();
779 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
780 return CreateNetworkFromModel();
781}
782
Finn Williamsb49ed182021-06-29 15:50:08 +0100783
784armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
785{
786 ResetParser();
787 m_Model = std::move(model);
788
789 return CreateNetworkFromModel();
790}
791
Kevin May7d96b162021-02-03 17:38:41 +0000792INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100793{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100794
795 using NetworkOptions = std::vector<BackendOptions>;
796 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100797 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100798 {
Mike Kelly80512b02022-05-16 23:10:42 +0100799 if (m_Options.value().m_InferAndValidate)
800 {
801 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
802 {
803 { "InferAndValidate", true }
804 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100805
Mike Kelly80512b02022-05-16 23:10:42 +0100806 networkOptions.push_back(shapeInferenceMethodOption);
807 }
808 if (m_Options.value().m_AllowExpandedDims)
809 {
810 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
811 {
812 { "AllowExpandedDims", true }
813 });
814
815 networkOptions.push_back(shapeInferenceMethodOption);
816 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100817 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100818 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100819 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100820
telsoa01c577f2c2018-08-31 09:22:23 +0100821 if (m_Model->subgraphs.size() != 1)
822 {
823 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100824 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
825 m_Model->subgraphs.size(),
826 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100827 }
828
829 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100830 size_t operatorIndex = 0;
831 try
telsoa01c577f2c2018-08-31 09:22:23 +0100832 {
Colm Donelan6350d272020-06-09 16:56:25 +0100833 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100834 {
Colm Donelan6350d272020-06-09 16:56:25 +0100835 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
836 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100837 {
Colm Donelan6350d272020-06-09 16:56:25 +0100838 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100839
840// 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 +0100841#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100842 auto builtinCode = std::max(opCodePtr->builtin_code,
843 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
844#else
telsoa01c577f2c2018-08-31 09:22:23 +0100845 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100846#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100847
848 if (builtinCode > tflite::BuiltinOperator_MAX)
849 {
James Ward58dec6b2020-09-11 17:32:44 +0100850 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
851 "subgraph:{} operator idx:{}. {}",
852 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
853 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100854 }
855
856 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100857 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100858 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100859 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100860 }
telsoa01c577f2c2018-08-31 09:22:23 +0100861
Colm Donelan6350d272020-06-09 16:56:25 +0100862 SetupInputLayers(subgraphIndex);
863 SetupOutputLayers(subgraphIndex);
864 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100865
Colm Donelan6350d272020-06-09 16:56:25 +0100866 ++subgraphIndex;
867 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100868 }
telsoa01c577f2c2018-08-31 09:22:23 +0100869 }
Colm Donelan6350d272020-06-09 16:56:25 +0100870 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100871 {
Colm Donelan6350d272020-06-09 16:56:25 +0100872 std::stringstream errorString;
873 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
874 << subgraphIndex << " error: " << e.what();
875 ARMNN_LOG(error) << errorString.str();
876 std::stringstream errors;
877 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100878 throw ParseException(errors.str());
879 }
880
881 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100882 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100883 {
884 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
885 {
886 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
887 {
888 for (size_t inputSlotIdx = 0;
889 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
890 ++inputSlotIdx)
891 {
892 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
893 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
894 }
895 }
896 }
897 }
telsoa01c577f2c2018-08-31 09:22:23 +0100898 return std::move(m_Network);
899}
900
Mike Kelly5880b912022-01-28 16:18:54 +0000901std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
902 const TensorInfo& tensorInfo)
903{
904 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
905 tensorInfo.GetDataType() == DataType::QAsymmU8)
906 {
907 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
908
909 if (tensorInfo.HasPerAxisQuantization())
910 {
911 unsigned int axis = tensorInfo.GetQuantizationDim().value();
912 auto axisDimensionality = tensorInfo.GetShape()[axis];
913 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
914
915 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
916 {
917 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
918 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
919 tensorInfo.GetQuantizationOffset());
920 }
921 }
922 else
923 {
924 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
925 {
926 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
927 tensorInfo.GetQuantizationOffset());
928 }
929 }
930 return buffer;
931 }
932 throw ParseException(
933 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
934 GetDataTypeName(DataType::Float32),
935 GetDataTypeName(tensorInfo.GetDataType()),
936 CHECK_LOCATION().AsString()));
937}
938
Kevin May7d96b162021-02-03 17:38:41 +0000939void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
940 size_t tensorIndex,
941 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100942{
943 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100944 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
945 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100946
947 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
948
949 // assuming there is only one producer for that tensor
950 if (tensorSlots.outputSlot != nullptr)
951 {
James Ward58dec6b2020-09-11 17:32:44 +0100952 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
953 "subgraph:{} tensor:{} {}",
954 subgraphIndex,
955 tensorIndex,
956 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100957 }
958
959 tensorSlots.outputSlot = slot;
960}
961
Kevin May7d96b162021-02-03 17:38:41 +0000962void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
963 size_t tensorIndex,
964 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100965{
966 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100967 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
968 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100969
Finn Williamsd4fa5452021-03-01 12:31:41 +0000970 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100971 tensorSlots.inputSlots.push_back(slot);
972}
973
Kevin May7d96b162021-02-03 17:38:41 +0000974void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100975{
976 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
977
978 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000979 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100980
981 // Identify custom code defined for custom operator
982 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
983 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
984
985 // Find parser function that correspondes to custom code (if any)
986 auto iterator = m_CustomParserFunctions.find(customCode);
987 if (iterator != m_CustomParserFunctions.end())
988 {
989 customParserFunction = iterator->second;
990 }
991
992 // Run parser function
993 (this->*customParserFunction)(subgraphIndex, operatorIndex);
994}
995
Kevin May7d96b162021-02-03 17:38:41 +0000996void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100997{
998 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100999
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001000 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1001
1002 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001003
1004// 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 +01001005#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001006 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1007 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1008#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001009 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001010#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001011
1012 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1013 {
1014 // Do not add StandInLayer, throw ParseException instead
1015 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001016 fmt::format("Operator not supported. "
1017 "subgraph:{} operator:{} "
1018 "opcode_index:{} opcode:{} / {} {}",
1019 subgraphIndex,
1020 operatorIndex,
1021 opcodeIndex,
1022 opcode,
1023 tflite::EnumNameBuiltinOperator(opcode),
1024 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001025 }
1026
1027 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1028 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1029
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001030 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1031 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001032
1033 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001034 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001035
1036 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1037 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001038 ARMNN_ASSERT(layer != nullptr);
1039
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001040 for (unsigned int i = 0u; i < numOutputs; ++i)
1041 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001042 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001043 }
1044
1045 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1046 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1047
1048 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1049 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001050}
1051
mathad01b392e982021-04-07 12:07:30 +01001052void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1053{
1054 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1055
1056 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1057 CHECK_VALID_SIZE(inputs.size(), 1);
1058 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1059 CHECK_VALID_SIZE(outputs.size(), 1);
1060
1061 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1062
1063 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1064 ARMNN_ASSERT(layer != nullptr);
1065
1066 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1067 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1068
1069 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1070 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1071
1072 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1073 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1074}
1075
Kevin May7d96b162021-02-03 17:38:41 +00001076void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001077{
1078 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1079
Mike Kelly0d77ae12022-01-07 17:42:27 +00001080 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1081 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001082
1083 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1084
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001085 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1086 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1087 CHECK_VALID_SIZE(outputs.size(), 1);
1088
telsoa01c577f2c2018-08-31 09:22:23 +01001089 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001090 inputs.size() == 3 ?
1091 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001092 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1093 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001094 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001095 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1096 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001097
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001098 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
telsoa01c577f2c2018-08-31 09:22:23 +01001099 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1100
1101 // assuming input is NHWC
1102 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001103 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001104
1105 // assuming the filter is OHWI : Output, H, W, Input
1106 // which is essentially the same as NHWC
1107 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001108 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001109
Pablo Tellof0bd6832019-04-26 17:58:13 +01001110 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1111 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1112 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1113 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001114
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001115 // Add the first input and weights tensor to the registration list.
1116 // The constant weights will be added by SetupConstantLayers.
1117 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1118 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001119
James Ward58dec6b2020-09-11 17:32:44 +01001120 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001121 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001122
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001123 if (IsConstTensor(inputs[1]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1124 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1125 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
telsoa01c577f2c2018-08-31 09:22:23 +01001126 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001127 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001128 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001129
1130 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001131 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001132 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1133
1134 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1135 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1136
1137 if (IsConstTensor(inputs[2]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1138 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1139 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
1140 {
1141 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1142 }
telsoa01c577f2c2018-08-31 09:22:23 +01001143 }
1144
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001145 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001146
Sadik Armagand109a4d2020-07-28 10:42:13 +01001147 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001148 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001149
1150 // register the input connection slots for the layer, connections are made after all layers have been created
1151 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001152 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001153
jimfly01c25411c2018-11-14 17:47:22 +00001154 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001155 // register the output connection slots for the layer, connections are made after all layers have been created
1156 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001157 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001158}
1159
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001160// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
1161#if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001162void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1163{
1164 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1165
1166 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1167 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1168
1169 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1170
1171 Convolution3dDescriptor desc;
1172 desc.m_BiasEnabled = false;
1173 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1174 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1175 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1176 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1177 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1178 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1179 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1180
1181 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1182 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1183
1184 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1185 CHECK_VALID_SIZE(outputs.size(), 1);
1186
1187 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1188 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1189
1190 // Assuming input is NDHWC
1191 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1192 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1193 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1194
1195 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1196 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1197 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1198 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1199
1200 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001201 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001202 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1203 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1204 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1205 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1206
Mike Kelly5880b912022-01-28 16:18:54 +00001207 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001208
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001209 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1210
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001211 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1212 // Add the first input and weights tensor to the registration list.
1213 // The constant weights will be added by SetupConstantLayers.
1214 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1215
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001216 if (inputs.size() == 3)
1217 {
1218 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001219
1220 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1221 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001222 }
1223
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001224 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001225 ARMNN_ASSERT(layer != nullptr);
1226
1227 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1228 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1229
1230 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001231 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001232
1233 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1234 // Register the output connection slots for the layer, connections are made after all layers have been created
1235 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1236 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1237}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001238#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001239
Kevin May7d96b162021-02-03 17:38:41 +00001240void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001241{
1242 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1243
Mike Kelly0d77ae12022-01-07 17:42:27 +00001244 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1245 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001246
1247 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1248
1249 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001250 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1251 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001252 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001253 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001254
1255 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1256 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001257 if (inputs.size() == 3)
1258 {
1259 desc.m_BiasEnabled = true;
1260 }
1261
telsoa01c577f2c2018-08-31 09:22:23 +01001262 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1263 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001264 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1265 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001266
telsoa01c577f2c2018-08-31 09:22:23 +01001267 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001268 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001269
Matteo Martincigh747ef822018-12-18 09:26:39 +00001270 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001271 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1272 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001273
1274 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001275 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1276 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1277
Pablo Tellof0bd6832019-04-26 17:58:13 +01001278 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1279 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1280 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1281 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001282
Jan Eilers53ef7952021-06-02 12:01:25 +01001283 // 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 +01001284 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001285
Cathal Corbett06902652022-04-14 17:55:11 +01001286 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1287 // Add the first input and weights tensor to the registration list.
1288 // The constant weights will be added by SetupConstantLayers.
1289 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1290
1291 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1292
1293 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001294 {
1295 desc.m_BiasEnabled = true;
1296 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001297
1298 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1299 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001300 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001301 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001302
Sadik Armagand109a4d2020-07-28 10:42:13 +01001303 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001304 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001305
1306 // register the input connection slots for the layer, connections are made after all layers have been created
1307 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001308 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001309
jimfly01c25411c2018-11-14 17:47:22 +00001310 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001311 // register the output connection slots for the layer, connections are made after all layers have been created
1312 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1313 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1314}
1315
Kevin May7d96b162021-02-03 17:38:41 +00001316void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001317{
1318 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1319
1320 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1321 CHECK_VALID_SIZE(inputs.size(), 1);
1322
1323 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1324 CHECK_VALID_SIZE(outputs.size(), 1);
1325
James Ward58dec6b2020-09-11 17:32:44 +01001326 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001327
1328 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001329 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001330
Sadik Armagand109a4d2020-07-28 10:42:13 +01001331 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001332 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1333
1334 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1335 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1336
1337 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1338 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1339}
1340
Teresa Charlin3ab85482021-06-08 16:59:29 +01001341void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1342{
1343 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1344
1345 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1346 CHECK_VALID_SIZE(inputs.size(), 2);
1347
1348 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1349 CHECK_VALID_SIZE(outputs.size(), 1);
1350
1351 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1352
1353 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1354 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1355
1356 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1357
1358 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001359
1360 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1361 {
1362 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1363 }
1364 else
1365 {
1366 int32_t axis = inputs[1]->shape[0];
1367
1368 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1369
1370 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1371 {
1372 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1373 }
1374
1375 if(axis < 0)
1376 {
1377 axis = inputDimSize + axis + 1;
1378 }
1379
Rob Hughesd812a312021-08-06 13:10:53 +01001380 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001381 unsigned int inputShapeIndex = 0;
1382 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1383 {
1384 if (i == static_cast<unsigned int>(axis))
1385 {
1386 shape[i] = 1;
1387 }
1388 else
1389 {
1390 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1391 ++inputShapeIndex;
1392 }
1393 }
1394
Rob Hughesd812a312021-08-06 13:10:53 +01001395 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001396 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001397
1398 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1399 ARMNN_ASSERT(layer != nullptr);
1400 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1401
1402 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1403 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1404
1405 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1406 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1407}
1408
Kevin May7d96b162021-02-03 17:38:41 +00001409void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001410{
1411 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1412
1413 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001414 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001415
1416 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1417 CHECK_VALID_SIZE(outputs.size(), 1);
1418
James Ward58dec6b2020-09-11 17:32:44 +01001419 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001420 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001421
josh minorba424d22019-11-13 10:55:17 -06001422 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001423 {
1424 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1425 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001426 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1427 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001428 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001429 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001430
Mike Kelly08759e22020-03-02 11:41:31 +00001431 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001432 }
1433
James Conroy05102392020-06-24 15:39:55 +01001434 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001435 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001436 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001437
James Conroy05102392020-06-24 15:39:55 +01001438 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001439 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001440 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1441
1442 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1443 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1444
1445 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1446 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1447}
1448
Kevin May7d96b162021-02-03 17:38:41 +00001449void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001450{
1451 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1452
Mike Kelly0d77ae12022-01-07 17:42:27 +00001453 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1454 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001455
1456 TransposeConvolution2dDescriptor desc;
1457 desc.m_BiasEnabled = false;
1458 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1459 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1460 desc.m_DataLayout = armnn::DataLayout::NHWC;
1461
1462 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001463 if (inputs.size() == 4)
1464 {
1465 desc.m_BiasEnabled = true;
1466 }
1467 else
1468 {
1469 CHECK_VALID_SIZE(inputs.size(), 3);
1470 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001471
1472 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1473 CHECK_VALID_SIZE(outputs.size(), 1);
1474
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001475 if (inputs[0])
1476 {
1477 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1478 std::vector<int> output_shape(tensorInfo.GetNumElements());
1479 if (tensorInfo.GetDataType() == DataType::Signed32)
1480 {
1481 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1482 }
1483 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1484 {
1485 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1486 {
1487 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1488 }
1489 }
1490 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1491 for (int dimension : output_shape)
1492 {
1493 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1494 }
1495 desc.m_OutputShapeEnabled = true;
1496 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001497 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001498 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1499
1500 // TfLite uses NHWC tensors
1501 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1502 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1503
1504 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1505 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1506
1507 CalcPadding(inputHeight,
1508 filterHeight,
1509 desc.m_StrideY,
1510 1, // DilationY
1511 desc.m_PadTop,
1512 desc.m_PadBottom,
1513 options->padding);
1514
1515 CalcPadding(inputWidth,
1516 filterWidth,
1517 desc.m_StrideX,
1518 1, // DilationX
1519 desc.m_PadLeft,
1520 desc.m_PadRight,
1521 options->padding);
1522
Mike Kelly5880b912022-01-28 16:18:54 +00001523 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001524
1525 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001526 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001527
David Monahan61683802021-01-12 09:11:07 +00001528 if (desc.m_BiasEnabled)
1529 {
1530 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001531 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001532 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001533 filterTensorAndData.first,
1534 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001535 layerName.c_str());
1536 }
1537 else
1538 {
1539 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001540 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001541 EmptyOptional(),
1542 layerName.c_str());
1543 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001544
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001545 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001546
Sadik Armagand109a4d2020-07-28 10:42:13 +01001547 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001548 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1549
1550 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1551 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001552 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001553
1554 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1555 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1556}
1557
Kevin May7d96b162021-02-03 17:38:41 +00001558void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001559{
1560 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1561}
1562
Kevin May7d96b162021-02-03 17:38:41 +00001563void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001564{
1565 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1566
1567 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1568 CHECK_VALID_SIZE(inputs.size(), 3);
1569
1570 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1571 CHECK_VALID_SIZE(outputs.size(), 1);
1572
1573 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1574 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1575
1576 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1577 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1578
1579 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1580 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1581
1582 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1583 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1584
1585 size_t step = 2;
1586 std::vector<std::pair<unsigned int, unsigned int>> crops;
1587 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1588 {
1589 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1590 }
1591
1592 armnn::BatchToSpaceNdDescriptor desc;
1593 desc.m_BlockShape = blockShape;
1594 desc.m_Crops = crops;
1595 desc.m_DataLayout = armnn::DataLayout::NHWC;
1596
James Ward58dec6b2020-09-11 17:32:44 +01001597 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001598
James Conroy05102392020-06-24 15:39:55 +01001599 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001600 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001601 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1602
1603 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1604 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001605 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1606
1607 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1608 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1609
1610 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1611 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1612}
1613
Kevin May7d96b162021-02-03 17:38:41 +00001614void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001615{
1616 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1617
1618 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1619 CHECK_VALID_SIZE(inputs.size(), 1);
1620
1621 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1622 CHECK_VALID_SIZE(outputs.size(), 1);
1623
1624 L2NormalizationDescriptor desc;
1625 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001626 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001627 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1628
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001629 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001630
Sadik Armagand109a4d2020-07-28 10:42:13 +01001631 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001632 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1633
1634 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1635 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1636
1637 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1638 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1639}
1640
Kevin May7d96b162021-02-03 17:38:41 +00001641void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001642{
1643 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1644}
1645
Kevin May7d96b162021-02-03 17:38:41 +00001646void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001647{
1648 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1649
1650 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1651 CHECK_VALID_SIZE(inputs.size(), 2);
1652
1653 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1654 CHECK_VALID_SIZE(outputs.size(), 1);
1655
James Ward58dec6b2020-09-11 17:32:44 +01001656 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001657
1658 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1659 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1660 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001661
Sadik Armagand109a4d2020-07-28 10:42:13 +01001662 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001663 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1664
1665 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1666 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001667 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1668
1669 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001670 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001671
1672 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1673 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1674}
1675
Kevin May7d96b162021-02-03 17:38:41 +00001676void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001677{
1678 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1679
1680 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1681 CHECK_VALID_SIZE(inputs.size(), 2);
1682
1683 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1684 CHECK_VALID_SIZE(outputs.size(), 1);
1685
James Ward58dec6b2020-09-11 17:32:44 +01001686 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001687
1688 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1689 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1690 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001691
Sadik Armagand109a4d2020-07-28 10:42:13 +01001692 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001693 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1694
1695 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1696 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001697 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1698
1699 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001700 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001701
1702 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1703 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1704}
1705
Kevin May7d96b162021-02-03 17:38:41 +00001706void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1707 size_t operatorIndex,
1708 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001709{
1710 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1711
Mike Kelly0d77ae12022-01-07 17:42:27 +00001712 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1713 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001714
1715 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1716
1717 std::string layerName;
1718
1719 switch (algorithm)
1720 {
1721 case PoolingAlgorithm::Average:
1722 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001723 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001724 break;
1725 case PoolingAlgorithm::Max:
1726 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001727 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001728 break;
1729 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001730 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001731 }
1732
1733 Pooling2dDescriptor desc;
1734
1735 desc.m_PoolType = algorithm;
1736 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1737 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1738 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1739 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1740 desc.m_PaddingMethod = PaddingMethod::Exclude;
1741 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001742 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001743
1744 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1745 CHECK_VALID_SIZE(inputs.size(), 1);
1746 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1747
1748 // assuming input is NHWC
1749 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1750 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1751
Pablo Tellof0bd6832019-04-26 17:58:13 +01001752 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1753 desc.m_PadTop, desc.m_PadBottom, options->padding);
1754 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1755 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001756
1757 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1758 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001759
Sadik Armagand109a4d2020-07-28 10:42:13 +01001760 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001761 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1762
1763 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1764 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001765 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001766
1767 // register the input connection slots for the layer, connections are made after all layers have been created
1768 // only the tensors for the inputs are relevant, exclude the const tensors
1769 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001770 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001771
jimfly01c25411c2018-11-14 17:47:22 +00001772 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001773 // register the output connection slots for the layer, connections are made after all layers have been created
1774 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1775 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1776}
1777
Kevin May7d96b162021-02-03 17:38:41 +00001778void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001779{
1780 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1781
1782 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1783 CHECK_VALID_SIZE(inputs.size(), 3);
1784 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1785 CHECK_VALID_SIZE(outputs.size(), 1);
1786
1787 SliceDescriptor desc;
1788
1789 // set begin tensor info for slice descriptor
1790 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1791 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1792
1793 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1794 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1795
1796 // set size tensor info for slice descriptor
1797 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1798 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1799
Mike Kelly7ba84d62021-09-10 15:27:19 +01001800 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1801 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001802 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001803 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1804
1805 for (unsigned int i = 0; i < signedSize.size(); ++i)
1806 {
1807 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001808
Mike Kelly7ba84d62021-09-10 15:27:19 +01001809 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1810 {
1811 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1812 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1813 signedValue,
1814 inputTensorInfo.GetShape()[i] - begin[i],
1815 CHECK_LOCATION().AsString()));
1816 }
1817
1818 if (signedValue == -1)
1819 {
1820 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1821 }
1822 else
1823 {
1824 size[i] = static_cast<unsigned int>(signedValue);
1825 }
1826 }
1827
josh minorba424d22019-11-13 10:55:17 -06001828 desc = SliceDescriptor(begin, size);
1829
James Ward58dec6b2020-09-11 17:32:44 +01001830 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001831
Sadik Armagand109a4d2020-07-28 10:42:13 +01001832 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001833 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1834
1835 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001836 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1837
1838 // register the input connection slots for the layer, connections are made after all layers have been created
1839 // only the tensors for the inputs are relevant, exclude the const tensors
1840 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1841 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1842
1843 // register the output connection slots for the layer, connections are made after all layers have been created
1844 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1845 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1846}
1847
Kevin May7d96b162021-02-03 17:38:41 +00001848void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001849{
1850 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001851 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1852 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001853
1854 SoftmaxDescriptor desc;
1855 desc.m_Beta = options->beta;
1856
1857 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1858 CHECK_VALID_SIZE(inputs.size(), 1);
1859 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1860 CHECK_VALID_SIZE(outputs.size(), 1);
1861
James Ward58dec6b2020-09-11 17:32:44 +01001862 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001863 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1864
Sadik Armagand109a4d2020-07-28 10:42:13 +01001865 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001866 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1867
1868 // register the input connection slots for the layer, connections are made after all layers have been created
1869 // only the tensors for the inputs are relevant, exclude the const tensors
1870 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1871 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1872
1873 // register the output connection slots for the layer, connections are made after all layers have been created
1874 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1875 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1876}
1877
Teresa Charlin455172a2022-06-29 15:35:57 +01001878void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
1879{
1880 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1881
1882 LogSoftmaxDescriptor desc;
1883
1884 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1885 CHECK_VALID_SIZE(inputs.size(), 1);
1886 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1887 CHECK_VALID_SIZE(outputs.size(), 1);
1888
1889 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
1890 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
1891
1892 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1893 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1894
1895 // register the input connection slots for the layer, connections are made after all layers have been created
1896 // only the tensors for the inputs are relevant, exclude the const tensors
1897 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1898 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1899
1900 // register the output connection slots for the layer, connections are made after all layers have been created
1901 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1902 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1903}
1904
Kevin May7d96b162021-02-03 17:38:41 +00001905void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001906{
1907 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1908
1909 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1910 CHECK_VALID_SIZE(inputs.size(), 3);
1911
1912 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1913 CHECK_VALID_SIZE(outputs.size(), 1);
1914
1915 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1916 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1917
1918 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1919 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1920
1921 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1922 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1923
1924 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1925 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1926
1927 size_t step = 2;
1928 std::vector<std::pair<unsigned int, unsigned int>> padList;
1929 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1930 {
1931 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1932 }
1933
1934 armnn::SpaceToBatchNdDescriptor desc;
1935 desc.m_BlockShape = blockShape;
1936 desc.m_PadList = padList;
1937 desc.m_DataLayout = armnn::DataLayout::NHWC;
1938
James Ward58dec6b2020-09-11 17:32:44 +01001939 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001940
James Conroy05102392020-06-24 15:39:55 +01001941 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001942 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001943 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1944
1945 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1946 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001947 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1948
1949 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1950 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1951
1952 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1953 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1954}
1955
Teresa Charlin3ab85482021-06-08 16:59:29 +01001956armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00001957 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001958{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001959 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001960 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1961
1962 if (inputTensorInfo.GetNumDimensions() > 4)
1963 {
1964 std::stringstream ss;
1965 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1966 << " shape:" << inputTensorInfo.GetShape() << " "
1967 << CHECK_LOCATION().AsString();
1968 throw ParseException(ss.str());
1969 }
1970
1971 if (squeezeDims.empty())
1972 {
1973 squeezeDims.assign(dimensionSequence,
1974 dimensionSequence+inputTensorInfo.GetNumDimensions());
1975 }
1976
1977 std::vector<uint32_t> outputDims;
1978 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1979 {
1980 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1981 auto currentDimension = inputTensorInfo.GetShape()[i];
1982 if (skipSqueeze || currentDimension != 1)
1983 {
1984 outputDims.push_back(currentDimension);
1985 }
1986 }
1987
1988 if (outputDims.size() > 4)
1989 {
1990 std::stringstream ss;
1991 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1992 << " shape:" << inputTensorInfo.GetShape() << " "
1993 << CHECK_LOCATION().AsString();
1994 throw ParseException(ss.str());
1995 }
1996
1997 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1998 outputDims.data());
1999
2000 // we need to preserve the tensor type and the quantization data as well
2001 TensorInfo outTensorInfo = inputTensorInfo;
2002 outTensorInfo.SetShape(outShape);
2003
2004 return outTensorInfo;
2005}
2006
Keith Davis0176fd82021-06-01 17:36:32 +01002007void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2008{
2009 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2010
2011 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2012 CHECK_VALID_SIZE(inputs.size(), 1);
2013 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2014 CHECK_VALID_SIZE(outputs.size(), 1);
2015
2016 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2017
2018 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2019 ARMNN_ASSERT(layer != nullptr);
2020
2021
2022 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2023 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2024
2025 // Check if output tensor type is Signed32 or Signed64
2026 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2027 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2028 {
2029 throw ParseException(
2030 fmt::format(
2031 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2032 CHECK_LOCATION().AsString()));
2033 }
2034
2035 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2036 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2037
2038 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2039 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2040}
2041
Kevin May7d96b162021-02-03 17:38:41 +00002042void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002043{
2044 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2045
2046 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2047 CHECK_VALID_SIZE(inputs.size(), 1);
2048
2049 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2050 CHECK_VALID_SIZE(outputs.size(), 1);
2051
2052 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2053 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002054 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002055
2056 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002057
2058 std::vector<uint32_t> squeezeDim;
2059 // A single negative dim index is interpreted as a negative index in python
2060 // Meaning the index will be the shape size plus the negative index value
2061 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2062 {
2063 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2064 squeezeDim.push_back(static_cast<uint32_t>(dim));
2065 }
2066 else
2067 {
2068 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2069 }
2070
2071 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2072
James Conroy05102392020-06-24 15:39:55 +01002073 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002074
2075 ReshapeDescriptor reshapeDesc;
2076 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2077
telsoa01c577f2c2018-08-31 09:22:23 +01002078 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002079 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002080 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2081
2082 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2083 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2084
2085 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2086 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2087}
2088
Kevin May7d96b162021-02-03 17:38:41 +00002089void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002090{
2091 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2092
2093 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2094 CHECK_VALID_SIZE(inputs.size(), 4);
2095
2096 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2097 CHECK_VALID_SIZE(outputs.size(), 1);
2098
Mike Kelly0d77ae12022-01-07 17:42:27 +00002099 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2100 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002101
2102 StridedSliceDescriptor desc;
2103 desc.m_BeginMask = options->begin_mask;
2104 desc.m_EllipsisMask = options->ellipsis_mask;
2105 desc.m_EndMask = options->end_mask;
2106 desc.m_NewAxisMask = options->new_axis_mask;
2107 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2108 desc.m_DataLayout = armnn::DataLayout::NHWC;
2109
2110 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2111 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2112
2113 std::vector<int> begin(beginTensorInfo.GetNumElements());
2114 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2115
2116 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2117 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2118
2119 std::vector<int> end(endTensorInfo.GetNumElements());
2120 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2121
2122 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2123 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2124
2125 std::vector<int> stride(strideTensorInfo.GetNumElements());
2126 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2127
2128 desc.m_Begin = begin;
2129 desc.m_End = end;
2130 desc.m_Stride = stride;
2131
James Ward58dec6b2020-09-11 17:32:44 +01002132 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002133 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002134 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002135
Sadik Armagand109a4d2020-07-28 10:42:13 +01002136 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002137 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2138
2139 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2140 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2141
2142 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2143 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2144}
2145
Kevin May7d96b162021-02-03 17:38:41 +00002146void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002147{
2148 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2149
Mike Kelly0d77ae12022-01-07 17:42:27 +00002150 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2151 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002152
2153 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2154 CHECK_VALID_SIZE(inputs.size(), 2);
2155
2156 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2157 CHECK_VALID_SIZE(outputs.size(), 1);
2158
2159 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2160 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2161
James Ward58dec6b2020-09-11 17:32:44 +01002162 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002163 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002164 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002165
Sadik Armagand109a4d2020-07-28 10:42:13 +01002166 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002167 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2168
2169 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002170 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002171
2172 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2173
2174 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2175 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2176}
2177
Kevin May7d96b162021-02-03 17:38:41 +00002178void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302179{
2180 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2181
Mike Kelly0d77ae12022-01-07 17:42:27 +00002182 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2183 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302184
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
James Ward58dec6b2020-09-11 17:32:44 +01002194 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302195 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002196 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302197
Sadik Armagand109a4d2020-07-28 10:42:13 +01002198 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302199 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2200
2201 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002202 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302203 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2204
2205 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2206 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2207}
2208
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002209void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2210{
2211 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2212
2213 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2214 CHECK_VALID_SIZE(inputs.size(), 2);
2215
2216 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2217 CHECK_VALID_SIZE(outputs.size(), 1);
2218
2219 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2220 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2221
2222 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2223 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2224 ARMNN_ASSERT(layer != nullptr);
2225
2226 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2227 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2228
2229 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2230 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2231 layer = AddFusedFloorLayer(layer, 0);
2232
2233 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2234 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2235}
2236
Kevin May7d96b162021-02-03 17:38:41 +00002237void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002238{
2239 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2240
Mike Kelly0d77ae12022-01-07 17:42:27 +00002241 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2242 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002243
2244 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2245 CHECK_VALID_SIZE(inputs.size(), 2);
2246
2247 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2248 CHECK_VALID_SIZE(outputs.size(), 1);
2249
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002250 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2251 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2252
James Ward58dec6b2020-09-11 17:32:44 +01002253 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002254 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002255 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002256
Sadik Armagand109a4d2020-07-28 10:42:13 +01002257 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002258 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2259
2260 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002261 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002262 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2263
2264 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2265 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2266}
2267
Kevin May7d96b162021-02-03 17:38:41 +00002268void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002269{
2270 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2271
Mike Kelly0d77ae12022-01-07 17:42:27 +00002272 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2273 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002274
2275 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2276 CHECK_VALID_SIZE(inputs.size(), 2);
2277
2278 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2279 CHECK_VALID_SIZE(outputs.size(), 1);
2280
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002281 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2282 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2283
James Ward58dec6b2020-09-11 17:32:44 +01002284 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002285 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002286 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002287
Sadik Armagand109a4d2020-07-28 10:42:13 +01002288 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002289 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2290
2291 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002292 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002293 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2294
2295 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2296 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2297}
2298
Kevin May7d96b162021-02-03 17:38:41 +00002299void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002300{
2301 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2302
2303 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2304
2305 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2306 CHECK_VALID_SIZE(outputs.size(), 1);
2307
2308 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2309 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2310
2311 armnn::MeanDescriptor desc;
2312 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2313 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2314 desc.m_Axis = axis;
2315
2316 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002317 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002318
2319 desc.m_KeepDims =
2320 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2321 true : false;
2322
James Ward58dec6b2020-09-11 17:32:44 +01002323 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002324 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002325 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002326
2327 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2328
2329 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2330 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2331
2332 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2333 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2334}
2335
Kevin May7d96b162021-02-03 17:38:41 +00002336void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002337{
2338 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2339
Kevin May7d96b162021-02-03 17:38:41 +00002340 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002341
Kevin May7d96b162021-02-03 17:38:41 +00002342 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002343 CHECK_VALID_SIZE(outputs.size(), 1);
2344
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002345 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002346 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002347
Mike Kelly0d77ae12022-01-07 17:42:27 +00002348 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002349
2350 size_t step = 2;
2351 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002352 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2353
2354 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002355 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002356 CHECK_VALID_SIZE(inputs.size(), 2);
2357
2358 if (inputTensorInfo.IsQuantized())
2359 {
2360 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2361 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002362 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002363 else if (opcode == tflite::BuiltinOperator_PADV2)
2364 {
2365 CHECK_VALID_SIZE(inputs.size(), 3);
2366
2367 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2368
2369 if (padValueTensorInfo.GetNumElements() != 1)
2370 {
2371 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2372 }
2373 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2374
2375 // Get the pad value from the input tensor
2376 if (padValueBufferPtr->data.size() > 0)
2377 {
2378 switch (padValueTensorInfo.GetDataType())
2379 {
2380 case armnn::DataType::Float32:
2381 {
2382 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2383 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2384 desc.m_PadValue = padValueBuffer[0];
2385 break;
2386 }
2387 case armnn::DataType::QAsymmU8:
2388 {
2389 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2390 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2391 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2392 padValueTensorInfo.GetQuantizationScale(),
2393 padValueTensorInfo.GetQuantizationOffset());
2394 break;
2395 }
2396 case armnn::DataType::QAsymmS8:
2397 case armnn::DataType::QSymmS8:
2398 {
2399 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2400 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2401 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2402 padValueTensorInfo.GetQuantizationScale(),
2403 padValueTensorInfo.GetQuantizationOffset());
2404 break;
2405 }
2406 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2407 }
2408 }
2409 else if (inputTensorInfo.IsQuantized())
2410 {
2411 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2412 }
2413 }
2414
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002415 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2416 {
2417 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2418 }
2419
Mike Kelly0d77ae12022-01-07 17:42:27 +00002420 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2421 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002422 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002423
2424 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2425 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002426 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2427
2428 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2429 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2430
2431 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2432 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2433}
2434
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002435void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2436{
2437 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2438
2439 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2440 CHECK_VALID_SIZE(inputs.size(), 2);
2441
2442 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2443 CHECK_VALID_SIZE(outputs.size(), 1);
2444
2445 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2446
2447 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2448 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2449
2450 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2451 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2452
2453 size_t step = 2;
2454 armnn::PadDescriptor desc;
2455 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2456 {
2457 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2458 }
2459
2460 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2461 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2462
2463 if (options->mode == tflite::MirrorPadMode_REFLECT)
2464 {
2465 desc.m_PaddingMode = PaddingMode::Reflect;
2466 }
2467 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2468 {
2469 desc.m_PaddingMode = PaddingMode::Symmetric;
2470 }
2471 else
2472 {
2473 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2474 }
2475
2476 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2477 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2478 auto inputShape = inputTensorInfo.GetShape();
2479 auto padList = desc.m_PadList;
2480
2481 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2482 for(unsigned int i = 0; i < padList.size(); ++i)
2483 {
2484 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2485 padList.at(i).second > (inputShape[i] - isReflect))
2486 {
2487 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2488 "equal (Symmetric) to the dimension size.");
2489 }
2490 }
2491
2492 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2493 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2494
2495 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2496 ARMNN_ASSERT(layer != nullptr);
2497 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2498
2499 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2500 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2501
2502 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2503 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2504}
2505
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002506void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2507{
2508 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2509
2510 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2511 CHECK_VALID_SIZE(inputs.size(), 2);
2512
2513 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2514 CHECK_VALID_SIZE(outputs.size(), 1);
2515
2516 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2517
2518 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2519 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2520 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2521 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2522
2523 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2524 ARMNN_ASSERT(layer != nullptr);
2525 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2526
2527 if (IsConstTensor(inputs[1]))
2528 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002529 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002530 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2531 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002532
Mike Kelly5880b912022-01-28 16:18:54 +00002533 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2534 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002535 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2536 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002537 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002538 ARMNN_ASSERT(constLayer != nullptr);
2539
2540 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2541 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2542 RegisterOutputSlots(subgraphIndex,
2543 VIRTUAL_OPERATOR_ID,
2544 constLayer,
2545 { inputTensorIndexes[1] });
2546 }
2547 else
2548 {
2549 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2550 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2551 }
2552
2553 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2554 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2555}
2556
Kevin May7d96b162021-02-03 17:38:41 +00002557void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002558{
2559 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2560
2561 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2562 CHECK_VALID_SIZE(inputs.size(), 1);
2563
2564 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2565 CHECK_VALID_SIZE(outputs.size(), 1);
2566
James Ward58dec6b2020-09-11 17:32:44 +01002567 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002568
2569 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002570 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002571
Sadik Armagand109a4d2020-07-28 10:42:13 +01002572 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002573 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2574
2575 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2576 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2577
2578 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2579 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2580}
Finn Williamsc42c3842019-01-22 14:18:11 +00002581
Kevin May7d96b162021-02-03 17:38:41 +00002582void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002583{
Finn Williamsc42c3842019-01-22 14:18:11 +00002584 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002585}
2586
Kevin May7d96b162021-02-03 17:38:41 +00002587void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002588{
Finn Williamsc42c3842019-01-22 14:18:11 +00002589 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2590}
Sadik Armagan58f39192018-09-17 14:14:39 +01002591
Kevin May7d96b162021-02-03 17:38:41 +00002592void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002593{
Jan Eilers2f746b32020-07-28 14:00:06 +01002594 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002595}
2596
Kevin May7d96b162021-02-03 17:38:41 +00002597void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002598{
2599 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2600}
2601
Kevin May7d96b162021-02-03 17:38:41 +00002602void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002603{
2604 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2605}
2606
Kevin May7d96b162021-02-03 17:38:41 +00002607void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002608{
2609 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2610}
2611
Kevin May7d96b162021-02-03 17:38:41 +00002612void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002613{
2614 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2615}
Finn Williamsc42c3842019-01-22 14:18:11 +00002616
Kevin May7d96b162021-02-03 17:38:41 +00002617void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002618{
2619 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002620 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002621 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002622
2623 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2624 CHECK_VALID_SIZE(inputs.size(), 1);
2625
2626 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2627 CHECK_VALID_SIZE(outputs.size(), 1);
2628
James Ward58dec6b2020-09-11 17:32:44 +01002629 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002630 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002631 activationDesc.m_Function = activationType;
2632
2633 switch (activationType)
2634 {
2635 case ActivationFunction::ReLu:
2636 {
James Ward58dec6b2020-09-11 17:32:44 +01002637 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002638 break;
2639 }
2640 case ActivationFunction::BoundedReLu:
2641 {
James Ward58dec6b2020-09-11 17:32:44 +01002642 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002643 activationDesc.m_A = 6.0f;
2644 activationDesc.m_B = 0.0f;
2645 break;
2646 }
2647 case ActivationFunction::Sigmoid:
2648 {
James Ward58dec6b2020-09-11 17:32:44 +01002649 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002650 break;
2651 }
Nina Drozd99851762019-04-09 09:37:38 +01002652 case ActivationFunction::TanH:
2653 {
James Ward58dec6b2020-09-11 17:32:44 +01002654 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002655 activationDesc.m_A = 1.0f;
2656 activationDesc.m_B = 1.0f;
2657 break;
2658 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002659 case ActivationFunction::LeakyReLu:
2660 {
James Ward58dec6b2020-09-11 17:32:44 +01002661 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002662 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002663 activationDesc.m_A = options->alpha;
2664 break;
2665 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002666 case ActivationFunction::Elu:
2667 {
2668 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2669 activationDesc.m_A = 1.0f;
2670 break;
2671 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002672 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002673 {
James Ward58dec6b2020-09-11 17:32:44 +01002674 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002675 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002676 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002677 default:
2678 {
2679 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002680 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2681 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002682 }
2683 }
2684
2685 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002686
Sadik Armagand109a4d2020-07-28 10:42:13 +01002687 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002688 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2689
2690 // register the input connection slots for the layer, connections are made after all layers have been created
2691 // only the tensors for the inputs are relevant, exclude the const tensors
2692 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2693 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2694
2695 // register the output connection slots for the layer, connections are made after all layers have been created
2696 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2697 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2698}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002699armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2700 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002701{
2702 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2703 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2704
2705 if (stretchDim != targetDimsIn.end())
2706 {
2707 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2708 {
2709 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002710 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002711 }
2712
2713 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002714 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002715 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2716
2717 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2718 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2719 }
2720
2721 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2722
2723 TensorInfo reshapeInfo = inputTensorInfo;
2724 reshapeInfo.SetShape(outputShape);
2725
2726 return reshapeInfo;
2727}
2728
Kevin May7d96b162021-02-03 17:38:41 +00002729void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002730{
2731 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2732
2733 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002734
2735 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2736 CHECK_VALID_SIZE(outputs.size(), 1);
2737
Mike Kelly0d77ae12022-01-07 17:42:27 +00002738 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2739 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002740 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002741
2742 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002743 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002744 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002745
Jan Eilersbac9b352020-07-13 13:40:24 +01002746 // Extracting new shape for the output
2747 // There are two ways it can be passed
2748 // * First is to define the target shape in the operator built-in options
2749 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002750 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002751 bool targetShapeFound = false;
2752 // Check if built-in options were given
2753 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002754 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002755 // make sure the parameter is given
2756 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002757 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002758 targetShape = options->new_shape;
2759 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002760 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002761 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002762
2763 // If there is no built-in option given or if the built-in new_shape parameter was empty
2764 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002765 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002766 // Check for a second input tensor
2767 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002768 {
2769 if (inputs[1]->is_variable)
2770 {
2771 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2772 }
2773
2774 if (inputs[1]->shape.size() != 1)
2775 {
2776 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2777 }
2778
2779 if (inputs[1]->type != tflite::TensorType_INT32)
2780 {
2781 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2782 }
2783
Teresa Charlin6a056a42021-12-01 10:25:43 +00002784 // Extract target shape from input
2785 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2786 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002787 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002788 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002789 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2790 {
2791 targetShape.push_back(values[i]);
2792 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002793 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002794 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002795 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002796 try
2797 {
2798 // We attempt to infer during Runtime.
2799 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2800 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2801 if (reshapeShapes[0] > 2)
2802 {
2803 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2804 "When inferring during runtime, the parser only supports "
2805 "shape (batch, -1) or (-1) for target shape input.",
2806 reshapeShapes[0],
2807 layerName,
2808 CHECK_LOCATION().AsString()));
2809 }
2810
2811 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2812 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2813 if (reshapeShapes[0] == 1)
2814 {
2815 targetShape = {numInputElements};
2816 }
2817 else if (reshapeShapes[0] == 2)
2818 {
2819 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2820 }
2821 }
2822 catch (const std::exception& exc)
2823 {
2824 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2825 "Reshape operation. Reshape operator target shape input buffer data "
2826 "is null. " << exc.what());
2827 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002828 }
2829 }
2830 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002831 {
2832 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2833 "At least one method required");
2834 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002835 }
2836
kevmay0171972a82018-12-17 14:28:03 +00002837 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002838 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002839
kevmay0171972a82018-12-17 14:28:03 +00002840 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002841 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2842 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002843 {
2844 std::stringstream ss;
2845 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002846 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002847 << " does not equal output shape "
2848 << actualOutputTensorInfo.GetShape()
2849 << ": "
2850 << CHECK_LOCATION().AsString();
2851 throw ParseException(ss.str());
2852 }
2853
Sadikb94967b2018-09-19 15:30:00 +01002854 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002855 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002856
Sadikb94967b2018-09-19 15:30:00 +01002857 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002858 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002859 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002860
2861 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2862 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2863
2864 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2865 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2866}
2867
Kevin May7d96b162021-02-03 17:38:41 +00002868void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002869{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002870 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2871}
2872
Kevin May7d96b162021-02-03 17:38:41 +00002873void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002874{
2875 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2876}
2877
Kevin May7d96b162021-02-03 17:38:41 +00002878void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002879{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002880 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2881
2882 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2883 CHECK_VALID_SIZE(inputs.size(), 2);
2884
2885 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2886 CHECK_VALID_SIZE(outputs.size(), 1);
2887
2888 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2889
2890 // Data for the parsed tensor args (size) must be stored locally.
2891 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2892
2893 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2894 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2895
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002896 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002897 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002898 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002899 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2900 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002901
James Ward58dec6b2020-09-11 17:32:44 +01002902 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002903
2904 switch (resizeMethod)
2905 {
2906 case ResizeMethod::Bilinear:
2907 {
James Ward58dec6b2020-09-11 17:32:44 +01002908 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002909
2910 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2911 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2912
David Monahan4a0c9b92020-05-30 09:48:39 +01002913 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002914 break;
2915 }
2916 case ResizeMethod::NearestNeighbor:
2917 {
James Ward58dec6b2020-09-11 17:32:44 +01002918 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002919 break;
2920 }
2921 default:
2922 {
2923 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002924 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2925 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002926 }
2927 }
2928
James Conroy05102392020-06-24 15:39:55 +01002929 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002930 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002931 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2932
2933 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2934 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002935 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2936
2937 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2938 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2939
2940 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2941 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2942}
2943
Kevin May7d96b162021-02-03 17:38:41 +00002944void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002945{
2946 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2947
Mike Kelly0d77ae12022-01-07 17:42:27 +00002948 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2949 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002950
2951 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2952
2953 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2954 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2955 CHECK_VALID_SIZE(outputs.size(), 1);
2956
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002957 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2958 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002959
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002960 const unsigned int concatDimInput = static_cast<unsigned int>(
2961 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002962
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002963 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2964 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002965
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002966 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002967
2968 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2969 {
2970 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2971
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002972 // This set up concatDescriptor view origin
2973 armnnUtils::ProcessConcatInputTensorInfo(
2974 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002975 }
2976
James Ward58dec6b2020-09-11 17:32:44 +01002977 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002978 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002979
Jim Flynn906f9462019-05-10 13:55:21 +01002980 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002981 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002982 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002983
James Conroy05102392020-06-24 15:39:55 +01002984 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002985 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002986
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002987 // add fused activation layer
2988 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002989
Sadik Armagan479045b2018-10-01 11:51:37 +01002990 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2991 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2992}
2993
Kevin May7d96b162021-02-03 17:38:41 +00002994void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002995{
2996 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2997
Mike Kelly0d77ae12022-01-07 17:42:27 +00002998 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002999 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3000
3001 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3002
3003 FullyConnectedDescriptor desc;
3004 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003005 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003006
3007 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3008 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3009 CHECK_VALID_SIZE(outputs.size(), 1);
3010
3011 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
3012
3013 // Fully Connected Layer accepts two dimensional weights input
3014 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3015 if (weightsDimension != 2)
3016 {
3017 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003018 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3019 "Node {}",
3020 weightsDimension,
3021 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003022 }
3023
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003024 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003025 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003026
Matthew Sloyan81beae32021-07-13 19:46:11 +01003027 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3028 // Add the first input tensor to the registration list
3029 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3030 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00003031 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003032
3033 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3034
Matthew Sloyan81beae32021-07-13 19:46:11 +01003035 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3036 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003037
Mike Kelly5880b912022-01-28 16:18:54 +00003038 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3039 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3040 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3041 {
3042 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3043 }
3044
Finn Williamsd4fa5452021-03-01 12:31:41 +00003045 if (inputs.size() == 3)
3046 {
3047 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003048 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003049
3050 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3051 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003052
3053 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3054 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3055 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3056 {
3057 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3058 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003059 }
3060
Matthew Sloyan81beae32021-07-13 19:46:11 +01003061 // Filters and biases are always passed to fully connected as inputs
3062 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003063
3064 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003065
Finn Williamsd4fa5452021-03-01 12:31:41 +00003066 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003067 if (inputTensorInfo.GetNumDimensions() > 2)
3068 {
3069 // Add reshape to flatten to 2D [batch_size, input_size],
3070 // where "input_size" corresponds to the number of inputs to the layer,
3071 // matching the second dimension of weights,
3072 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3073 std::vector<unsigned int> reshapedDimensions(2);
3074 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3075 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3076
3077 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3078 {
3079 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003080 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3081 reshapedDimensions[1],
3082 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003083 }
3084
3085 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3086 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3087
James Ward58dec6b2020-09-11 17:32:44 +01003088 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003089 armnn::ReshapeDescriptor reshapeDescriptor;
3090 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3091 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003092
3093 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3094 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3095
3096 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003097 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3098 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3099 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003100 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003101
3102 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003103
Sadik Armagand109a4d2020-07-28 10:42:13 +01003104 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003105 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3106
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003107 // we need to add the activation layer and fortunately we don't need to care about the data layout
3108 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3109 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003110
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003111 // register the output connection slots for the layer, connections are made after all layers have been created
3112 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3113 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3114}
3115
Kevin May7d96b162021-02-03 17:38:41 +00003116void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003117{
3118 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3119
Mike Kelly0d77ae12022-01-07 17:42:27 +00003120 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003121
3122 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3123 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3124 CHECK_VALID_SIZE(outputs.size(), 4);
3125
3126 // Obtain custom options from flexbuffers
3127 auto custom_options = operatorPtr->custom_options;
3128 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3129
3130 // Obtain descriptor information from tf lite
3131 DetectionPostProcessDescriptor desc;
3132 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3133 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3134 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3135 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3136 desc.m_NumClasses = m["num_classes"].AsUInt32();
3137 desc.m_ScaleH = m["h_scale"].AsFloat();
3138 desc.m_ScaleW = m["w_scale"].AsFloat();
3139 desc.m_ScaleX = m["x_scale"].AsFloat();
3140 desc.m_ScaleY = m["y_scale"].AsFloat();
3141
keidav0107d58c72019-02-26 11:57:39 +00003142 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003143 {
keidav0107d58c72019-02-26 11:57:39 +00003144 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003145 }
3146 if (!(m["detections_per_class"].IsNull()))
3147 {
3148 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3149 }
3150
3151 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3152 {
3153 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3154 "must be positive and less than or equal to 1.");
3155 }
3156
3157 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003158 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003159
James Ward58dec6b2020-09-11 17:32:44 +01003160 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003161 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003162 layerName.c_str());
3163
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003164 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003165
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003166 // The model does not specify the output shapes.
3167 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3168 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3169 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3170 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3171 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3172 m_OverridenOutputShapes.push_back({ 1 });
3173
keidav011b3e2ea2019-02-21 10:07:37 +00003174 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3175 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003176 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003177 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3178 }
3179
3180 // Register the input connection slots for the layer, connections are made after all layers have been created
3181 // only the tensors for the inputs are relevant, exclude the const tensors
3182 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3183 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3184
3185 // Register the output connection slots for the layer, connections are made after all layers have been created
3186 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3187 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3188 outputTensorIndexes[1],
3189 outputTensorIndexes[2],
3190 outputTensorIndexes[3]});
3191}
3192
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003193/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003194void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003195{
3196 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3197
3198 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3199 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3200 CHECK_VALID_SIZE(outputs.size(), 1);
3201
3202 if (inputs.size() < 1)
3203 {
3204 throw ParseException("Pack must have at least one input.");
3205 }
3206
3207 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3208 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3209
3210 StackDescriptor desc;
3211 desc.m_Axis = static_cast<uint32_t>(options->axis);
3212 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3213
3214 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3215 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3216 desc.m_InputShape = inputTensorInfo.GetShape();
3217
James Ward58dec6b2020-09-11 17:32:44 +01003218 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003219 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3220
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003221 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003222
Sadik Armagand109a4d2020-07-28 10:42:13 +01003223 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003224 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3225
3226 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3227 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3228
3229 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3230 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3231}
3232
Mike Kelly5880b912022-01-28 16:18:54 +00003233void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3234{
3235 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3236
3237 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3238 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3239
3240 if (inputs.size() < 2)
3241 {
3242 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3243 }
3244
3245 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3246 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3247 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3248 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3249 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3250 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3251
3252 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3253 // Please refer to each operand at
3254 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3255 armnn::LstmInputParams params;
3256
3257 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3258 {
3259 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3260 inputTensorInfo).first;
3261 }
3262
3263 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3264 inputTensorInfo).first;
3265 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3266 inputTensorInfo).first;
3267 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3268 inputTensorInfo).first;
3269
3270 // Recurrent weight tensors of size {n_cell, n_output}
3271 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3272 {
3273 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3274 inputTensorInfo).first;
3275 }
3276
3277 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3278 inputTensorInfo).first;
3279 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3280 inputTensorInfo).first;
3281 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3282 inputTensorInfo).first;
3283
3284 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3285 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3286 {
3287 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3288 inputTensorInfo).first;
3289 }
3290
3291 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3292 {
3293 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3294 inputTensorInfo).first;
3295 }
3296
3297 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3298 {
3299 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3300 inputTensorInfo).first;
3301 }
3302
3303 // Gates bias tensors of size {n_cell}
3304 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3305 {
3306 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3307 inputTensorInfo).first;
3308 }
3309
3310 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3311 inputTensorInfo).first;
3312 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3313 inputTensorInfo).first;
3314 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3315 inputTensorInfo).first;
3316
3317 // Projection weight tensor of size {n_output, n_cell}
3318 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3319 {
3320 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3321 inputTensorInfo).first;
3322 }
3323 // Projection bias tensor of size {n_output}
3324 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3325 {
3326 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3327 inputTensorInfo).first;
3328 }
3329
3330 // These state tensors are defined as variable tensors, and will be modified by this op.
3331 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3332 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3333 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3334 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3335
3336 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3337 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3338 {
3339 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3340 inputTensorInfo).first;
3341 }
3342
3343 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3344 {
3345 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3346 inputTensorInfo).first;
3347 }
3348
3349 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3350 {
3351 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3352 inputTensorInfo).first;
3353 }
3354
3355 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3356 {
3357 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3358 inputTensorInfo).first;
3359 }
3360
3361 // set the layer descriptor
3362 armnn::UnidirectionalSequenceLstmDescriptor desc;
3363 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3364 desc.m_ClippingThresCell = nodeParams->cell_clip;
3365 desc.m_ClippingThresProj = nodeParams->proj_clip;
3366 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3367 || params.m_RecurrentToInputWeights == nullptr
3368 || params.m_InputGateBias == nullptr);
3369 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3370 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3371 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3372 || params.m_ForgetLayerNormWeights != nullptr
3373 || params.m_CellLayerNormWeights != nullptr
3374 || params.m_OutputLayerNormWeights != nullptr);
3375 desc.m_TimeMajor = nodeParams->time_major;
3376
Mike Kellyc0800a32022-06-15 10:57:52 +01003377 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003378 {
3379 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3380 inputTensorInfo).first;
3381 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3382 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3383
3384 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3385 inputTensorInfo).first;
3386 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3387 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3388
3389 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3390 inputTensorInfo).first;
3391 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3392 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3393
3394 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3395 inputTensorInfo).first;
3396 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3397 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3398 }
3399 else
3400 {
3401 float defaultIntermediate = std::pow(2, -12);
3402 desc.m_InputIntermediateScale = defaultIntermediate;
3403 desc.m_ForgetIntermediateScale = defaultIntermediate;
3404 desc.m_CellIntermediateScale = defaultIntermediate;
3405 desc.m_OutputIntermediateScale = defaultIntermediate;
3406 }
3407
Mike Kellyc0800a32022-06-15 10:57:52 +01003408 if (operatorPtr->intermediates.size() > 4)
3409 {
3410 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3411 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003412
Mike Kellyc0800a32022-06-15 10:57:52 +01003413 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3414 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3415 }
Mike Kelly5880b912022-01-28 16:18:54 +00003416 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3417 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3418 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3419
3420 armnn::DataType dataType = inputTensorInfo.GetDataType();
3421 float qScale = inputTensorInfo.GetQuantizationScale();
3422 float qOffset = inputTensorInfo.GetQuantizationOffset();
3423
3424 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3425 if (!desc.m_CifgEnabled)
3426 {
3427 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3428 }
3429 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3430 cellStateInInfo.GetDataType(),
3431 cellStateInInfo.GetQuantizationScale(),
3432 cellStateInInfo.GetQuantizationOffset());
3433 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3434
3435 armnn::LstmInputParamsInfo paramsInfo;
3436 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3437 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3438 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3439 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3440 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3441 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3442 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3443 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3444 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3445
3446 if (!desc.m_CifgEnabled)
3447 {
3448 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3449 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3450 if (params.m_CellToInputWeights != nullptr)
3451 {
3452 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3453 }
3454 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3455 }
3456
3457 if (desc.m_ProjectionEnabled)
3458 {
3459 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3460 if (params.m_ProjectionBias != nullptr)
3461 {
3462 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3463 }
3464 }
3465
3466 if (desc.m_PeepholeEnabled)
3467 {
3468 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3469 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3470 }
3471
3472 if (desc.m_LayerNormEnabled)
3473 {
3474 if(!desc.m_CifgEnabled)
3475 {
3476 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3477 }
3478 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3479 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3480 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3481 }
3482
3483 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3484 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3485 ARMNN_ASSERT(layer != nullptr);
3486
3487 // register the input connection slots for the layer, connections are made after all layers have been created
3488 // only the tensors for the inputs are relevant, exclude the const tensors
3489 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3490 operatorPtr->inputs[18],
3491 operatorPtr->inputs[19]});
3492 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3493 inputTensorIndexes[1],
3494 inputTensorIndexes[2]});
3495
3496 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3497
3498 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3499 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3500 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3501
3502 unsigned int tensorIndex = outputTensorIndexes[0];
3503 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3504 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3505}
3506
Kevin May7d96b162021-02-03 17:38:41 +00003507void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003508{
3509 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3510
Mike Kelly0d77ae12022-01-07 17:42:27 +00003511 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3512 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003513
3514 // This unpackAxis indicates the axis to unpack
3515 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3516
3517 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3518 CHECK_VALID_SIZE(inputs.size(), 1);
3519
3520 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003521
3522 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3523 {
3524 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003525 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3526 "the number of input dimension {} {}",
3527 unpackAxis,
3528 inputTensorInfo.GetNumDimensions(),
3529 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003530 }
3531
Nina Drozd200e3802019-04-15 09:47:39 +01003532 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3533 // If num is not defined, automatically infer from the length of the dimension axis.
3534 if(unpackNum == 0)
3535 {
3536 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3537 }
3538
3539 // If unpack number cannot be inferred and is still zero, throw ParseException.
3540 if(unpackNum == 0)
3541 {
3542 throw ParseException("Number to unpack must greater than zero.");
3543 }
3544
3545 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3546 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3547
3548 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3549 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3550
3551 // Add current input shape to unpackDimSizes
3552 for (unsigned int i = 0; i < inputDimSize; ++i)
3553 {
3554 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3555 }
3556
3557 if (unpackDimSizes[unpackAxis] != unpackNum)
3558 {
3559 throw ParseException("Number to unpack must be the same as length of the dimension to "
3560 "unpack along.");
3561 }
3562
3563 unpackDimSizes[unpackAxis] /= unpackNum;
3564
3565 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3566 for (unsigned int j = 0; j < unpackNum; ++j)
3567 {
3568 // Set the size of the views.
3569 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3570 {
3571 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3572 }
3573 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3574 }
3575
James Ward58dec6b2020-09-11 17:32:44 +01003576 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003577 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003578 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003579
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003580 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3581 unpackDimSizes.data());
3582
Nina Drozd200e3802019-04-15 09:47:39 +01003583 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3584 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3585
Finn Williamsb49ed182021-06-29 15:50:08 +01003586 std::vector<unsigned int> reshapeDims;
3587 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3588 {
3589 if (axis != unpackAxis)
3590 {
3591 reshapeDims.push_back(splitOutShape[axis]);
3592 }
3593 }
3594
3595 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3596
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003597 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3598 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3599 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003600 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003601 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003602 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003603 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003604 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3605
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003606 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3607 outputTensorInfo.GetDataType(),
3608 outputTensorInfo.GetQuantizationScale(),
3609 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003610 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3611
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003612 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003613
3614 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3615 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3616 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3617 }
Nina Drozd200e3802019-04-15 09:47:39 +01003618}
3619
Kevin May7d96b162021-02-03 17:38:41 +00003620void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003621{
3622 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3623
Mike Kelly0d77ae12022-01-07 17:42:27 +00003624 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3625 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003626
3627 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3628
Nina Drozd200e3802019-04-15 09:47:39 +01003629 // If number of splits cannot be inferred and is zero, throw ParseException.
3630 if(numSplits == 0)
3631 {
3632 throw ParseException("Number to splits must greater than zero.");
3633 }
3634
Nina Drozd0324f482019-04-08 10:52:10 +01003635 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3636 CHECK_VALID_SIZE(inputs.size(), 2);
3637 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3638 CHECK_VALID_SIZE(outputs.size(), numSplits);
3639
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003640 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3641 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3642 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003643
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003644 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003645 if (axisBufferPtr == nullptr)
3646 {
3647 throw ParseException(
3648 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3649 CHECK_LOCATION().AsString()));
3650 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003651
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003652 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3653 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3654 int32_t axis = axisData[0];
3655
3656 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3657 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3658 {
3659 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3660 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3661 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3662 throw ParseException(
3663 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3664 axis,
3665 CHECK_LOCATION().AsString()));
3666 }
3667
3668 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003669
Nina Drozd0324f482019-04-08 10:52:10 +01003670 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003671 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003672 {
3673 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003674 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3675 inputTensorInfo.GetNumDimensions(),
3676 MaxNumOfTensorDimensions,
3677 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003678 }
3679
3680 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3681
3682 // Add current input shape to splitterDimSizes
3683 for (unsigned int i = 0; i < inputDimSize; ++i)
3684 {
3685 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3686 }
3687
3688 if (splitterDimSizes[splitDim] % numSplits != 0)
3689 {
3690 throw ParseException("Number of splits must evenly divide the dimension");
3691 }
3692 splitterDimSizes[splitDim] /= numSplits;
3693
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003694 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003695 for (unsigned int j = 0; j < numSplits; ++j)
3696 {
3697 // Set the size of the views.
3698 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3699 {
3700 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3701 }
3702 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3703 }
3704
James Ward58dec6b2020-09-11 17:32:44 +01003705 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003706 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003707 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003708
3709 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003710 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003711
Nina Drozd0324f482019-04-08 10:52:10 +01003712 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3713 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003714 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003715 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003716 }
3717
3718 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3719 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3720}
3721
Derek Lambertif0176992020-04-28 13:37:49 +01003722unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3723{
3724 int numDims = armnn::numeric_cast<int>(numDimsIn);
3725 int v = idx < 0 ? numDims + idx : idx;
3726 ARMNN_ASSERT(v >= 0);
3727 ARMNN_ASSERT(v < numDims);
3728
3729 return static_cast<unsigned int>(v);
3730}
3731
Kevin May7d96b162021-02-03 17:38:41 +00003732void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003733{
3734 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3735
Mike Kelly0d77ae12022-01-07 17:42:27 +00003736 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3737 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003738
3739 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3740 CHECK_VALID_SIZE(inputs.size(), 3);
3741
3742 auto& inputTensor = inputs[0];
3743 auto& splitsTensor = inputs[1];
3744 auto& axisTensor = inputs[2];
3745
3746 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3747 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3748 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3749 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3750
3751 // Inputs
3752 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3753 if (inputDimSize > MaxNumOfTensorDimensions)
3754 {
3755 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003756 fmt::format("The number of dimensions: {} for input tensors of the "
3757 "SplitV op cannot be greater than {} {}",
3758 inputTensorInfo.GetNumDimensions(),
3759 MaxNumOfTensorDimensions,
3760 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003761 }
3762
3763 // Get split axis
3764 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003765 if (axisBufferPtr == nullptr)
3766 {
3767 throw ParseException(
3768 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3769 CHECK_LOCATION().AsString()));
3770 }
3771
Derek Lambertif0176992020-04-28 13:37:49 +01003772 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3773 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003774 int32_t axis = axisData[0];
3775
3776 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3777 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3778 {
3779 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3780 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3781 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3782 throw ParseException(
3783 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3784 axis,
3785 CHECK_LOCATION().AsString()));
3786 }
3787 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003788
Derek Lambertif0176992020-04-28 13:37:49 +01003789 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003790 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003791 unsigned int numSplits{0};
3792
3793 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003794 {
3795 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003796 }
3797 else
3798 {
Ryan OShea86704732020-05-26 11:41:04 +01003799 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003800 }
3801
3802 if (numSplits <=0)
3803 {
3804 throw ParseException("SplitV has invalid number of splits");
3805 }
3806
Jan Eilersc0761e92020-06-29 16:48:44 +01003807 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003808 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003809 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003810
Jan Eilersc0761e92020-06-29 16:48:44 +01003811 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003812 int numInferred{0};
3813 unsigned int inferIdx{0};
3814 int splitSum{0};
3815 for (auto split : splitsData)
3816 {
3817 if (split < 0)
3818 {
3819 numInferred++;
3820 inferIdx = idx;
3821 }
3822 else
3823 {
3824 splitSum += split;
3825 }
3826 idx++;
3827 }
3828 // Check for inferred Axis
3829 if (numInferred == 0)
3830 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003831 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003832 {
3833 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3834 }
3835 }
3836 else if (numInferred == 1)
3837 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003838 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003839 }
3840 else
3841 {
3842 throw ParseException("Cannot infer split size for more than one split");
3843 }
3844
Derek Lambertif0176992020-04-28 13:37:49 +01003845 //Ouput size validation
3846 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3847 CHECK_VALID_SIZE(outputs.size(), numSplits);
3848
3849 // Setup Armnn descriptor
3850 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3851 unsigned int accumSplit = 0;
3852 for (unsigned int j = 0; j < numSplits; ++j)
3853 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003854 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003855
3856 // Set the size of the views.
3857 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3858 {
3859 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3860 if (dimIdx == splitDim)
3861 {
3862 dimSize = splitSize;
3863 }
3864 splitDesc.SetViewSize(j, dimIdx, dimSize);
3865 }
3866
3867 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3868 accumSplit += splitSize;
3869 }
3870
James Ward58dec6b2020-09-11 17:32:44 +01003871 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003872 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003873 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003874
3875 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3876 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3877
3878 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3879 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003880 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003881 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3882 }
3883
3884 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3885 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3886}
3887
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003888void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3889{
3890 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3891}
3892
Kevin May7d96b162021-02-03 17:38:41 +00003893void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003894{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003895 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3896}
3897
3898void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3899{
Inki Daed4619e22020-09-10 15:33:54 +09003900 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3901 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3902 CHECK_VALID_SIZE(inputs.size(), 2);
3903
3904 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3905 CHECK_VALID_SIZE(outputs.size(), 1);
3906
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003907 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3908 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003909 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003910 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003911
3912 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003913 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3914 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3915 {
3916 throw ParseException(
3917 fmt::format(
3918 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3919 CHECK_LOCATION().AsString()));
3920 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003921
3922 // Get const axis value from model and set it to descriptor.
3923 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3924 if (axisBufferPtr == nullptr)
3925 {
3926 throw ParseException(
3927 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3928 CHECK_LOCATION().AsString()));
3929 }
3930
3931 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3932 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3933 int32_t axis = axisData.front();
3934
3935 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3936 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3937 {
3938 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3939 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3940 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3941 throw ParseException(
3942 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3943 axis,
3944 CHECK_LOCATION().AsString()));
3945 }
3946
3947 ArgMinMaxDescriptor desc;
3948 desc.m_Axis = axis;
3949 desc.m_Function = argMinMaxFunction;
3950
3951 // Register a ArgMin/ArgMax layer.
3952 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3953 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3954 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3955 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003956 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3957
3958 // Register input tensor to the layer.
3959 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3960 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3961
3962 // Register output tensor to the layer.
3963 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3964 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3965}
3966
Kevin May7d96b162021-02-03 17:38:41 +00003967void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003968{
3969 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3970
Kevin May7d96b162021-02-03 17:38:41 +00003971 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003972 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003973 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003974 CHECK_VALID_SIZE(outputs.size(), 1);
3975
3976 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3977 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3978 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3979
3980 armnn::GatherDescriptor gatherDescriptor;
3981
Mike Kelly0d77ae12022-01-07 17:42:27 +00003982 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3983 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00003984 auto axis = options->axis;
3985
3986 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3987 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3988 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3989 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3990 {
3991 throw ParseException(
3992 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3993 axis,
3994 inputDimensions, inputDimensions,
3995 CHECK_LOCATION().AsString()));
3996 }
3997 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3998 {
3999 throw ParseException(
4000 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4001 outputDimensions,
4002 inputDimensions, indicesDimensions,
4003 CHECK_LOCATION().AsString()));
4004 }
4005
4006 gatherDescriptor.m_Axis = axis;
4007
4008 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4009 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4010 ARMNN_ASSERT(layer != nullptr);
4011 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4012
4013 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4014 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4015
4016 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4017 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4018}
4019
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004020void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4021{
4022 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4023
4024 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4025 CHECK_VALID_SIZE(inputs.size(), 2);
4026 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4027 CHECK_VALID_SIZE(outputs.size(), 1);
4028
4029 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4030 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
4031 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4032
4033 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4034 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4035 ARMNN_ASSERT(layer != nullptr);
4036 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4037
4038 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4039 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4040
4041 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4042 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4043}
4044
Kevin May7d96b162021-02-03 17:38:41 +00004045void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004046{
4047 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4048
Kevin May7d96b162021-02-03 17:38:41 +00004049 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004050 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004051 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004052 CHECK_VALID_SIZE(outputs.size(), 1);
4053
4054 armnn::DepthToSpaceDescriptor descriptor;
4055
Mike Kelly0d77ae12022-01-07 17:42:27 +00004056 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4057 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004058 auto blockSize = options->block_size;
4059 if (blockSize < 2)
4060 {
4061 throw ParseException(
4062 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4063 blockSize,
4064 CHECK_LOCATION().AsString()));
4065 }
4066 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4067
4068 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4069 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4070 ARMNN_ASSERT(layer != nullptr);
4071 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4072 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4073
4074 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4075 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4076
4077 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4078 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4079}
4080
Kevin May7d96b162021-02-03 17:38:41 +00004081void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004082{
Sadik Armagana2747482021-02-09 10:28:54 +00004083 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4084}
4085
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004086void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4087{
4088 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4089}
4090
Sadik Armagana2747482021-02-09 10:28:54 +00004091void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4092{
4093 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4094}
4095
4096void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4097{
4098 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4099}
4100
4101void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4102{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004103 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4104
Mike Kelly0d77ae12022-01-07 17:42:27 +00004105 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4106 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004107
4108 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4109 CHECK_VALID_SIZE(inputs.size(), 2);
4110
4111 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4112 CHECK_VALID_SIZE(outputs.size(), 1);
4113
Sadik Armagana2747482021-02-09 10:28:54 +00004114 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004115
4116 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4117 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004118
4119 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004120 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4121 // Get const axis value from model and set it to descriptor.
4122 if (axisBufferPtr != nullptr)
4123 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004124 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4125 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4126
4127 // Convert the axis to unsigned int and remove duplicates.
4128 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4129 std::set<unsigned int> uniqueAxis;
4130 std::transform(axisData.begin(),
4131 axisData.end(),
4132 std::inserter(uniqueAxis, uniqueAxis.begin()),
4133 [rank](int i)->unsigned int{
4134 return static_cast<uint32_t>(((i + rank) % rank)); });
4135 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004136 }
Sadik Armagana2747482021-02-09 10:28:54 +00004137 else
4138 {
4139 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4140 {
4141 desc.m_vAxis.push_back(i);
4142 }
4143 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004144
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004145 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004146 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004147
4148 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004149 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004150
4151 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4152 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4153
4154 // Register input tensor to the layer.
4155 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4156 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4157
4158 // Register output tensor to the layer.
4159 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4160 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4161}
4162
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004163void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4164{
4165 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4166}
4167
4168void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4169{
4170 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4171}
4172
Mike Kelly31dce2b2021-09-01 21:22:37 +01004173void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4174{
4175 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4176
4177 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4178 CHECK_VALID_SIZE(inputs.size(), 1);
4179
4180 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4181 CHECK_VALID_SIZE(outputs.size(), 1);
4182
4183 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4184 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4185
4186 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4187
4188 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4189 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4190
4191 armnn::NormalizationDescriptor descriptor;
4192 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4193 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4194 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4195 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4196 descriptor.m_K = options->bias;
4197 descriptor.m_Alpha = options->alpha;
4198 descriptor.m_Beta = options->beta;
4199
4200 // ArmNN expects normSize to be the full size of the normalization
4201 // window rather than the radius as in TfLite.
4202 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4203
4204 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4205 ARMNN_ASSERT(layer != nullptr);
4206
4207 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4208 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4209
4210 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4211 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4212
4213 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4214 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4215}
4216
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004217void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4218{
4219 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4220}
4221
4222void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4223{
4224 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4225}
4226
4227void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4228{
4229 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4230}
4231
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004232void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4233{
4234 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4235}
4236
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004237void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4238{
4239 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4240
4241 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4242 CHECK_VALID_SIZE(inputs.size(), 1);
4243
4244 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4245 CHECK_VALID_SIZE(outputs.size(), 1);
4246
4247 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4248 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4249
4250 ElementwiseUnaryDescriptor desc;
4251 desc.m_Operation = unaryOperation;
4252 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4253 ARMNN_ASSERT(layer != nullptr);
4254
4255 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4256 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4257
4258 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4259 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4260
4261 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4262 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4263}
4264
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004265void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4266{
4267 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4268}
4269
4270void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4271{
4272 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4273}
4274
4275void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4276{
4277 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4278}
4279
4280void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4281{
4282 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4283}
4284
4285void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4286{
4287 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4288}
4289
4290void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4291{
4292 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4293}
4294
4295void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4296 ComparisonOperation comparisonOperation)
4297{
4298 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4299
4300 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4301 CHECK_VALID_SIZE(inputs.size(), 2);
4302
4303 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4304 CHECK_VALID_SIZE(outputs.size(), 1);
4305
4306 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4307 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4308
4309 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4310 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4311 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4312
4313 ComparisonDescriptor desc;
4314 desc.m_Operation = comparisonOperation;
4315 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4316 ARMNN_ASSERT(layer != nullptr);
4317
4318 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4319 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4320
4321 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4322 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4323
4324 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4325 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4326}
4327
Kevin May7d96b162021-02-03 17:38:41 +00004328armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4329 unsigned int outputSlot,
4330 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004331{
4332 ActivationDescriptor activationDesc;
4333 std::string layerName = prevLayer->GetName();
4334
4335 switch(activationType)
4336 {
4337 case tflite::ActivationFunctionType_NONE:
4338 {
4339 // this is a no-op: return previous layer
4340 return prevLayer;
4341 }
4342 case tflite::ActivationFunctionType_RELU:
4343 {
4344 activationDesc.m_Function = ActivationFunction::ReLu;
4345 layerName += ":RELU";
4346 break;
4347 }
4348 case tflite::ActivationFunctionType_RELU6:
4349 {
4350 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4351 activationDesc.m_A = 6.0f;
4352 activationDesc.m_B = 0.0f;
4353 layerName += ":RELU6";
4354 break;
4355 }
4356 case tflite::ActivationFunctionType_TANH:
4357 {
4358 activationDesc.m_Function = ActivationFunction::TanH;
4359 activationDesc.m_A = 1.0f;
4360 activationDesc.m_B = 1.0f;
4361 layerName += ":TANH";
4362 break;
4363 }
4364
4365 // I only put these here as a reminder what others we could support
4366 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4367 case tflite::ActivationFunctionType_SIGN_BIT:
4368 default:
4369 {
4370 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004371 fmt::format("TfLite parser doesn't suppport fused activation: "
4372 "{}/{} {} ",
4373 activationType,
4374 tflite::EnumNameActivationFunctionType(activationType),
4375 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004376
4377 }
4378 }
4379
4380 IConnectableLayer* activationLayer =
4381 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4382
4383 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4384 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4385 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4386 return activationLayer;
4387}
4388
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004389armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4390 unsigned int outputSlot)
4391{
Teresa Charlin725728e2022-05-05 13:33:33 +01004392
4393 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4394 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4395
4396 if (dataType == DataType::Signed32)
4397 {
4398 return prevLayer;
4399 }
4400
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004401 std::string layerName = prevLayer->GetName();
4402 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4403
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004404 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4405 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004406
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004407 return floorLayer;
4408}
4409
Mike Kelly0d77ae12022-01-07 17:42:27 +00004410TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004411{
4412 if (fileName == nullptr)
4413 {
James Ward58dec6b2020-09-11 17:32:44 +01004414 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004415 CHECK_LOCATION().AsString()));
4416 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004417 std::error_code errorCode;
4418 fs::path pathToFile(fileName);
4419 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004420 {
James Ward58dec6b2020-09-11 17:32:44 +01004421 //fmt::format() could not be used here (format error)
4422 std::stringstream msg;
4423 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4424 << " " << CHECK_LOCATION().AsString();
4425
4426 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004427 }
4428 std::ifstream file(fileName, std::ios::binary);
4429 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4430 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4431 fileContent.size());
4432}
4433
Mike Kelly0d77ae12022-01-07 17:42:27 +00004434TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004435{
4436 if (binaryContent == nullptr)
4437 {
James Ward58dec6b2020-09-11 17:32:44 +01004438 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004439 CHECK_LOCATION().AsString()));
4440 }
4441 flatbuffers::Verifier verifier(binaryContent, len);
4442 if (verifier.VerifyBuffer<tflite::Model>() == false)
4443 {
4444 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004445 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4446 "flatbuffers format. size:{} {}",
4447 len,
4448 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004449 }
4450 return tflite::UnPackModel(binaryContent);
4451}
4452
Mike Kelly0d77ae12022-01-07 17:42:27 +00004453TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004454 size_t subgraphIndex,
4455 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004456{
4457 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4458
Mike Kelly0d77ae12022-01-07 17:42:27 +00004459 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4460 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004461
4462 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004463 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004464 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004465 {
mathad01c21025d2021-04-26 10:09:37 +01004466 // If the input location is -1 then assume input is turned off.
4467 if (operatorPtr->inputs[i] == -1)
4468 {
4469 continue;
4470 }
4471 else
4472 {
4473 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4474 result.push_back(subgraphPtr->tensors[inputId].get());
4475 }
telsoa01c577f2c2018-08-31 09:22:23 +01004476 }
4477 return result;
4478}
4479
Mike Kelly0d77ae12022-01-07 17:42:27 +00004480TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004481 size_t subgraphIndex,
4482 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004483{
4484 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4485
Mike Kelly0d77ae12022-01-07 17:42:27 +00004486 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4487 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004488
4489 size_t outputCount = operatorPtr->outputs.size();
4490 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004491 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004492 {
4493 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4494 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004495 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004496 }
4497 return result;
4498}
4499
Mike Kelly0d77ae12022-01-07 17:42:27 +00004500TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004501 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004502{
4503 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004504 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004505
Derek Lambertiff05cc52019-04-26 13:05:17 +01004506 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004507 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004508 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004509 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004510 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004511 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004512 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004513 }
4514 return result;
4515}
4516
Mike Kelly0d77ae12022-01-07 17:42:27 +00004517TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004518 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004519{
4520 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004521 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004522
Derek Lambertiff05cc52019-04-26 13:05:17 +01004523 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004524 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004525 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004526 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004527 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4528 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004529 }
4530 return result;
4531}
4532
Kevin May7d96b162021-02-03 17:38:41 +00004533std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4534 size_t subgraphIndex,
4535 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004536{
4537 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004538 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4539 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004540 return operatorPtr->inputs;
4541}
4542
Kevin May7d96b162021-02-03 17:38:41 +00004543std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4544 size_t subgraphIndex,
4545 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004546{
4547 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004548 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4549 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004550 return operatorPtr->outputs;
4551}
4552
Kevin May7d96b162021-02-03 17:38:41 +00004553void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4554 size_t operatorIndex,
4555 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004556 const std::vector<unsigned int>& tensorIndexes,
4557 unsigned int startingSlotIndex)
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);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004561
Finn Williamsd4fa5452021-03-01 12:31:41 +00004562 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004563 {
4564 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004565 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4566 " for subgraph:{} operator index:{} {}",
4567 tensorIndexes.size(),
4568 layer->GetNumInputSlots(),
4569 subgraphIndex,
4570 operatorIndex,
4571 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004572 }
4573
Finn Williamsd4fa5452021-03-01 12:31:41 +00004574 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004575 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004576 unsigned int tensorIndex = tensorIndexes[index];
4577 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004578 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4579 }
4580}
4581
Kevin May7d96b162021-02-03 17:38:41 +00004582void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4583 size_t operatorIndex,
4584 IConnectableLayer* layer,
4585 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004586{
4587 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004588 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004589 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4590 {
4591 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004592 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4593 " for subgraph:{} operator index:{} {}",
4594 tensorIndexes.size(),
4595 layer->GetNumOutputSlots(),
4596 subgraphIndex,
4597 operatorIndex,
4598 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004599 }
4600
4601 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4602 {
4603 unsigned int tensorIndex = tensorIndexes[slotIndex];
4604 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4605 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4606 }
4607}
4608
Kevin May7d96b162021-02-03 17:38:41 +00004609void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004610{
4611 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4612
4613 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004614 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004615 {
4616 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4617 IConnectableLayer* layer =
4618 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4619
4620 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4621 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4622
4623 RegisterOutputSlots(subgraphIndex,
4624 VIRTUAL_OPERATOR_ID,
4625 layer,
4626 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4627 }
4628}
4629
Kevin May7d96b162021-02-03 17:38:41 +00004630void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004631{
4632 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4633
4634 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004635 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004636 {
4637 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4638 IConnectableLayer* layer =
4639 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4640
4641 RegisterInputSlots(subgraphIndex,
4642 VIRTUAL_OPERATOR_ID,
4643 layer,
4644 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4645 }
4646}
4647
Mike Kelly5880b912022-01-28 16:18:54 +00004648void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004649{
Mike Kelly5880b912022-01-28 16:18:54 +00004650 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004651
Mike Kelly5880b912022-01-28 16:18:54 +00004652 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004653 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4654 {
4655 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4656 {
4657 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4658 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4659 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004660 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004661
Mike Kelly5880b912022-01-28 16:18:54 +00004662 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004663 {
4664 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004665 armnn::DataType dataType = tensorInfo.GetDataType();
4666
4667 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4668 != m_ConstantsToDequantize.end())
4669 {
4670 dataType = DataType::Float32;
4671 }
4672 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4673
4674 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4675 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4676
4677 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4678 RegisterOutputSlots(subgraphIndex,
4679 VIRTUAL_OPERATOR_ID,
4680 layer,
4681 { tensorIndex });
4682 }
4683 else if (ShouldConstantTensorBeCreated(tensorIndex))
4684 {
4685 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4686 armnn::DataType dataType = tensorInfo.GetDataType();
4687
4688 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4689 != m_ConstantsToDequantize.end())
4690 {
4691 dataType = DataType::Float32;
4692 }
4693 // Make sure isConstant flag is set.
4694 tensorInfo.SetConstant();
4695 tensorInfo.SetDataType(dataType);
4696
4697 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004698
Matthew Sloyan81beae32021-07-13 19:46:11 +01004699 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004700 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004701
Matthew Sloyan81beae32021-07-13 19:46:11 +01004702 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4703 RegisterOutputSlots(subgraphIndex,
4704 VIRTUAL_OPERATOR_ID,
4705 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004706 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004707 }
4708 else
4709 {
4710 throw ParseException(
4711 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4712 CHECK_LOCATION().AsString()));
4713 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004714 }
4715 }
4716 }
4717}
4718
telsoa01c577f2c2018-08-31 09:22:23 +01004719// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004720TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004721{
4722 CHECK_BUFFER(model, bufferIndex);
4723 return model->buffers[bufferIndex].get();
4724}
4725
Matteo Martincigh747ef822018-12-18 09:26:39 +00004726template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004727std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4728TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4729 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004730 armnn::TensorInfo& tensorInfo,
4731 armnn::Optional<armnn::PermutationVector&> permutationVector)
4732{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004733 // Make sure isConstant flag is set.
4734 tensorInfo.SetConstant();
4735
Matteo Martincigh747ef822018-12-18 09:26:39 +00004736 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4737 tensorPtr,
4738 tensorInfo,
4739 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004740 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004741 return std::make_pair(constData.first, std::move(storage));
4742}
4743
Mike Kelly5880b912022-01-28 16:18:54 +00004744bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4745{
4746 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4747 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4748 != m_ConstantsToBeCreated.end());
4749}
4750
Finn Williamsd4fa5452021-03-01 12:31:41 +00004751bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4752{
4753 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004754 bool isConst = true;
4755
4756 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4757 if (buffer->data.size() == 0)
4758 {
4759 isConst = false;
4760 }
4761
4762 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004763}
4764
Kevin May7d96b162021-02-03 17:38:41 +00004765std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004766TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4767 armnn::TensorInfo& tensorInfo,
4768 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004769{
4770 CHECK_TENSOR_PTR(tensorPtr);
4771 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4772 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4773
Matthew Sloyan81beae32021-07-13 19:46:11 +01004774 // Make sure isConstant flag is set.
4775 tensorInfo.SetConstant();
4776
telsoa01c577f2c2018-08-31 09:22:23 +01004777 switch (tensorInfo.GetDataType())
4778 {
4779 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004780 return CreateConstTensorAndStoreData<float>(bufferPtr,
4781 tensorPtr,
4782 tensorInfo,
4783 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004784 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004785 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4786 tensorPtr,
4787 tensorInfo,
4788 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004789 case armnn::DataType::QSymmS8:
4790 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4791 tensorPtr,
4792 tensorInfo,
4793 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004794 case armnn::DataType::QAsymmS8:
4795 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4796 tensorPtr,
4797 tensorInfo,
4798 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004799 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004800 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4801 tensorPtr,
4802 tensorInfo,
4803 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004804 default:
4805 {
4806 std::stringstream errString;
4807 errString << "Unexpected datatype when creating const tensor: "
4808 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4809 << " shape:" << tensorInfo.GetShape()
4810 << CHECK_LOCATION().AsString();
4811 throw ParseException(errString.str());
4812 }
4813 }
4814}
4815
Finn Williamsd4fa5452021-03-01 12:31:41 +00004816armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4817 armnn::TensorInfo& tensorInfo)
4818{
4819 CHECK_TENSOR_PTR(tensorPtr);
4820 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4821 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4822
Matthew Sloyan81beae32021-07-13 19:46:11 +01004823 // Make sure isConstant flag is set.
4824 tensorInfo.SetConstant();
4825
Finn Williamsd4fa5452021-03-01 12:31:41 +00004826 return ConstTensor(tensorInfo, bufferPtr->data.data());
4827}
4828
Mike Kelly5880b912022-01-28 16:18:54 +00004829std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4830TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4831 armnn::TensorInfo& tensorInfo,
4832 armnn::DataType inputDataType)
4833{
4834 CHECK_TENSOR_PTR(tensorPtr);
4835 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4836 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4837
4838 // Make sure isConstant flag is set.
4839 tensorInfo.SetConstant();
4840
4841 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4842 {
4843 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4844 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4845 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4846 }
4847 else
4848 {
4849 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4850 }
4851}
4852
4853std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4854TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4855{
4856 CHECK_TENSOR_PTR(tensorPtr);
4857 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4858 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4859 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4860
4861 // Make sure isConstant flag is set.
4862 tensorInfo.SetConstant();
4863
4864 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4865 {
4866 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4867 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4868 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4869 }
4870 else
4871 {
4872 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4873 }
4874}
4875
Kevin May7d96b162021-02-03 17:38:41 +00004876BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4877 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004878{
4879 CHECK_SUBGRAPH(m_Model, subgraphId);
4880 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004881 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004882 {
4883 if (input.second->name == name)
4884 {
4885 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004886 auto inputTensorInfo = ToTensorInfo(input.second);
4887 // Input tensors are always treated as constant tensors during network execution.
4888 inputTensorInfo.SetConstant(true);
4889 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004890 }
4891 }
4892
4893 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004894 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004895 {
4896 bindings << "'" << input.second->name << "' ";
4897 }
4898
4899 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004900 fmt::format("No input binding found for subgraph:{} and name:{}. "
4901 "Possible inputs are: [{}] {}",
4902 subgraphId,
4903 name,
4904 bindings.str(),
4905 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004906}
4907
Kevin May7d96b162021-02-03 17:38:41 +00004908BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4909 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004910{
4911 CHECK_SUBGRAPH(m_Model, subgraphId);
4912 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004913 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004914 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004915 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004916 if (output.second->name == name)
4917 {
4918 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004919 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4920 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4921 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004922 }
4923 }
4924
4925 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004926 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004927 {
4928 bindings << "'" << output.second->name << "' ";
4929 }
4930
4931 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004932 fmt::format("No output binding found for subgraph:{} and name:{}. "
4933 "Possible outputs are: [{}] {}",
4934 subgraphId,
4935 name,
4936 bindings.str(),
4937 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004938}
4939
Kevin May7d96b162021-02-03 17:38:41 +00004940size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004941{
4942 return m_Model->subgraphs.size();
4943}
4944
Kevin May7d96b162021-02-03 17:38:41 +00004945std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004946{
4947 CHECK_SUBGRAPH(m_Model, subgraphId);
4948 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4949 std::vector<std::string> result;
4950 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004951 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004952 {
4953 result.push_back(input.second->name);
4954 }
4955 return result;
4956}
4957
Kevin May7d96b162021-02-03 17:38:41 +00004958std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004959{
4960 CHECK_SUBGRAPH(m_Model, subgraphId);
4961 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4962 std::vector<std::string> result;
4963 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004964 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004965 {
4966 result.push_back(output.second->name);
4967 }
4968 return result;
4969}
4970
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004971const std::string TfLiteParserImpl::GetVersion()
4972{
4973 return TFLITE_PARSER_VERSION;
4974}
4975
Mike Kelly0d77ae12022-01-07 17:42:27 +00004976TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004977: m_FloatData(std::move(data))
4978, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004979, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004980, m_Int32Data(nullptr)
4981{
4982}
4983
Mike Kelly0d77ae12022-01-07 17:42:27 +00004984TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004985: m_FloatData(nullptr)
4986, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004987, m_Int8Data(nullptr)
4988, m_Int32Data(nullptr)
4989{
4990}
4991
Mike Kelly0d77ae12022-01-07 17:42:27 +00004992TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00004993: m_FloatData(nullptr)
4994, m_Uint8Data(nullptr)
4995, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01004996, m_Int32Data(nullptr)
4997{
4998}
4999
Mike Kelly0d77ae12022-01-07 17:42:27 +00005000TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005001: m_FloatData(nullptr)
5002, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005003, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005004, m_Int32Data(std::move(data))
5005{
5006}
5007
5008} // armnnTfLiteParser