blob: 33567953a26946e6d78b93fba8ece10275b9edc1 [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.
Cathal Corbette126be92022-05-25 11:21:11 +0100687 #if defined(ARMNN_POST_TFLITE_2_4)
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;
Teresa Charlin8b0bee12022-07-12 11:18:44 +0100711 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100712 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000713 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
Teresa Charlin455172a2022-06-29 15:35:57 +0100714 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
Kevin May7d96b162021-02-03 17:38:41 +0000715 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
716 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
717 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
718 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
719 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100720 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000721 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
722 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300723 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000724 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
725 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000726 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100727 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000728 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
729 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
730 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000731 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
732 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100733 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000734 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
735 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
736 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100737 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100738 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100739 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Teresa Charlin8b0bee12022-07-12 11:18:44 +0100740 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
Kevin May7d96b162021-02-03 17:38:41 +0000741 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
742 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
743 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
744 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
745 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
746 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
747 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
748 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
749 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
750 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
751 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
752 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000753 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
754 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000755 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100756
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100757 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000758 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100759}
760
Kevin May7d96b162021-02-03 17:38:41 +0000761void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100762{
763 m_Network = armnn::INetworkPtr(nullptr, nullptr);
764 m_Model = nullptr;
765 m_SubgraphConnections.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000766 m_OverridenOutputShapes.clear();
767 m_ConstantsToDequantize.clear();
768 m_ConstantsToBeCreated.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100769}
770
Kevin May7d96b162021-02-03 17:38:41 +0000771INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100772{
773 ResetParser();
774 m_Model = LoadModelFromFile(graphFile);
775 return CreateNetworkFromModel();
776}
777
Mike Kelly0d77ae12022-01-07 17:42:27 +0000778INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100779{
780 ResetParser();
781 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
782 return CreateNetworkFromModel();
783}
784
Finn Williamsb49ed182021-06-29 15:50:08 +0100785
786armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
787{
788 ResetParser();
789 m_Model = std::move(model);
790
791 return CreateNetworkFromModel();
792}
793
Kevin May7d96b162021-02-03 17:38:41 +0000794INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100795{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100796
797 using NetworkOptions = std::vector<BackendOptions>;
798 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100799 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100800 {
Mike Kelly80512b02022-05-16 23:10:42 +0100801 if (m_Options.value().m_InferAndValidate)
802 {
803 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
804 {
805 { "InferAndValidate", true }
806 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100807
Mike Kelly80512b02022-05-16 23:10:42 +0100808 networkOptions.push_back(shapeInferenceMethodOption);
809 }
810 if (m_Options.value().m_AllowExpandedDims)
811 {
812 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
813 {
814 { "AllowExpandedDims", true }
815 });
816
817 networkOptions.push_back(shapeInferenceMethodOption);
818 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100819 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100820 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100821 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100822
telsoa01c577f2c2018-08-31 09:22:23 +0100823 if (m_Model->subgraphs.size() != 1)
824 {
825 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100826 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
827 m_Model->subgraphs.size(),
828 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100829 }
830
831 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100832 size_t operatorIndex = 0;
833 try
telsoa01c577f2c2018-08-31 09:22:23 +0100834 {
Colm Donelan6350d272020-06-09 16:56:25 +0100835 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100836 {
Colm Donelan6350d272020-06-09 16:56:25 +0100837 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
838 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100839 {
Colm Donelan6350d272020-06-09 16:56:25 +0100840 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100841
842// 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 +0100843#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100844 auto builtinCode = std::max(opCodePtr->builtin_code,
845 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
846#else
telsoa01c577f2c2018-08-31 09:22:23 +0100847 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100848#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100849
850 if (builtinCode > tflite::BuiltinOperator_MAX)
851 {
James Ward58dec6b2020-09-11 17:32:44 +0100852 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
853 "subgraph:{} operator idx:{}. {}",
854 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
855 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100856 }
857
858 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100859 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100860 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100861 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100862 }
telsoa01c577f2c2018-08-31 09:22:23 +0100863
Colm Donelan6350d272020-06-09 16:56:25 +0100864 SetupInputLayers(subgraphIndex);
865 SetupOutputLayers(subgraphIndex);
866 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100867
Colm Donelan6350d272020-06-09 16:56:25 +0100868 ++subgraphIndex;
869 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100870 }
telsoa01c577f2c2018-08-31 09:22:23 +0100871 }
Colm Donelan6350d272020-06-09 16:56:25 +0100872 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100873 {
Colm Donelan6350d272020-06-09 16:56:25 +0100874 std::stringstream errorString;
875 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
876 << subgraphIndex << " error: " << e.what();
877 ARMNN_LOG(error) << errorString.str();
878 std::stringstream errors;
879 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100880 throw ParseException(errors.str());
881 }
882
883 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100884 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100885 {
886 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
887 {
888 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
889 {
890 for (size_t inputSlotIdx = 0;
891 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
892 ++inputSlotIdx)
893 {
894 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
895 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
896 }
897 }
898 }
899 }
telsoa01c577f2c2018-08-31 09:22:23 +0100900 return std::move(m_Network);
901}
902
Mike Kelly5880b912022-01-28 16:18:54 +0000903std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
904 const TensorInfo& tensorInfo)
905{
906 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
907 tensorInfo.GetDataType() == DataType::QAsymmU8)
908 {
909 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
910
911 if (tensorInfo.HasPerAxisQuantization())
912 {
913 unsigned int axis = tensorInfo.GetQuantizationDim().value();
914 auto axisDimensionality = tensorInfo.GetShape()[axis];
915 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
916
917 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
918 {
919 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
920 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
921 tensorInfo.GetQuantizationOffset());
922 }
923 }
924 else
925 {
926 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
927 {
928 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
929 tensorInfo.GetQuantizationOffset());
930 }
931 }
932 return buffer;
933 }
934 throw ParseException(
935 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
936 GetDataTypeName(DataType::Float32),
937 GetDataTypeName(tensorInfo.GetDataType()),
938 CHECK_LOCATION().AsString()));
939}
940
Kevin May7d96b162021-02-03 17:38:41 +0000941void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
942 size_t tensorIndex,
943 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100944{
945 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100946 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
947 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100948
949 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
950
951 // assuming there is only one producer for that tensor
952 if (tensorSlots.outputSlot != nullptr)
953 {
James Ward58dec6b2020-09-11 17:32:44 +0100954 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
955 "subgraph:{} tensor:{} {}",
956 subgraphIndex,
957 tensorIndex,
958 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100959 }
960
961 tensorSlots.outputSlot = slot;
962}
963
Kevin May7d96b162021-02-03 17:38:41 +0000964void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
965 size_t tensorIndex,
966 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100967{
968 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100969 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
970 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100971
Finn Williamsd4fa5452021-03-01 12:31:41 +0000972 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100973 tensorSlots.inputSlots.push_back(slot);
974}
975
Kevin May7d96b162021-02-03 17:38:41 +0000976void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100977{
978 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
979
980 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000981 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100982
983 // Identify custom code defined for custom operator
984 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
985 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
986
987 // Find parser function that correspondes to custom code (if any)
988 auto iterator = m_CustomParserFunctions.find(customCode);
989 if (iterator != m_CustomParserFunctions.end())
990 {
991 customParserFunction = iterator->second;
992 }
993
994 // Run parser function
995 (this->*customParserFunction)(subgraphIndex, operatorIndex);
996}
997
Kevin May7d96b162021-02-03 17:38:41 +0000998void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100999{
1000 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001001
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001002 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1003
1004 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001005
1006// 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 +01001007#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001008 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1009 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1010#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001011 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001012#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001013
1014 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1015 {
1016 // Do not add StandInLayer, throw ParseException instead
1017 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001018 fmt::format("Operator not supported. "
1019 "subgraph:{} operator:{} "
1020 "opcode_index:{} opcode:{} / {} {}",
1021 subgraphIndex,
1022 operatorIndex,
1023 opcodeIndex,
1024 opcode,
1025 tflite::EnumNameBuiltinOperator(opcode),
1026 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001027 }
1028
1029 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1030 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1031
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001032 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1033 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001034
1035 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001036 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001037
1038 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1039 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001040 ARMNN_ASSERT(layer != nullptr);
1041
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001042 for (unsigned int i = 0u; i < numOutputs; ++i)
1043 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001044 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001045 }
1046
1047 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1048 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1049
1050 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1051 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001052}
1053
mathad01b392e982021-04-07 12:07:30 +01001054void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1055{
1056 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1057
1058 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1059 CHECK_VALID_SIZE(inputs.size(), 1);
1060 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1061 CHECK_VALID_SIZE(outputs.size(), 1);
1062
1063 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1064
1065 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1066 ARMNN_ASSERT(layer != nullptr);
1067
1068 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1069 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1070
1071 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1072 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1073
1074 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1075 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1076}
1077
Kevin May7d96b162021-02-03 17:38:41 +00001078void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001079{
1080 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1081
Mike Kelly0d77ae12022-01-07 17:42:27 +00001082 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1083 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001084
1085 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1086
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001087 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1088 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1089 CHECK_VALID_SIZE(outputs.size(), 1);
1090
telsoa01c577f2c2018-08-31 09:22:23 +01001091 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001092 inputs.size() == 3 ?
1093 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001094 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1095 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001096 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001097 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1098 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001099
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001100 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
telsoa01c577f2c2018-08-31 09:22:23 +01001101 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1102
1103 // assuming input is NHWC
1104 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001105 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001106
1107 // assuming the filter is OHWI : Output, H, W, Input
1108 // which is essentially the same as NHWC
1109 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001110 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001111
Pablo Tellof0bd6832019-04-26 17:58:13 +01001112 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1113 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1114 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1115 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001116
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001117 // Add the first input and weights tensor to the registration list.
1118 // The constant weights will be added by SetupConstantLayers.
1119 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1120 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001121
James Ward58dec6b2020-09-11 17:32:44 +01001122 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001123 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001124
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001125 if (IsConstTensor(inputs[1]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1126 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1127 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
telsoa01c577f2c2018-08-31 09:22:23 +01001128 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001129 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001130 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001131
1132 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001133 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001134 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1135
1136 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1137 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1138
1139 if (IsConstTensor(inputs[2]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1140 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1141 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
1142 {
1143 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1144 }
telsoa01c577f2c2018-08-31 09:22:23 +01001145 }
1146
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001147 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001148
Sadik Armagand109a4d2020-07-28 10:42:13 +01001149 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001150 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001151
1152 // register the input connection slots for the layer, connections are made after all layers have been created
1153 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001154 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001155
jimfly01c25411c2018-11-14 17:47:22 +00001156 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001157 // register the output connection slots for the layer, connections are made after all layers have been created
1158 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001159 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001160}
1161
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001162// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbette126be92022-05-25 11:21:11 +01001163#if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001164void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1165{
1166 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1167
1168 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1169 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1170
1171 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1172
1173 Convolution3dDescriptor desc;
1174 desc.m_BiasEnabled = false;
1175 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1176 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1177 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1178 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1179 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1180 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1181 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1182
1183 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1184 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1185
1186 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1187 CHECK_VALID_SIZE(outputs.size(), 1);
1188
1189 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1190 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1191
1192 // Assuming input is NDHWC
1193 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1194 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1195 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1196
1197 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1198 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1199 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1200 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1201
1202 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001203 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001204 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1205 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1206 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1207 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1208
Mike Kelly5880b912022-01-28 16:18:54 +00001209 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001210
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001211 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1212
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001213 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1214 // Add the first input and weights tensor to the registration list.
1215 // The constant weights will be added by SetupConstantLayers.
1216 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1217
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001218 if (inputs.size() == 3)
1219 {
1220 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001221
1222 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1223 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001224 }
1225
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001226 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001227 ARMNN_ASSERT(layer != nullptr);
1228
1229 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1230 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1231
1232 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001233 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001234
1235 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1236 // Register the output connection slots for the layer, connections are made after all layers have been created
1237 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1238 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1239}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001240#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001241
Kevin May7d96b162021-02-03 17:38:41 +00001242void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001243{
1244 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1245
Mike Kelly0d77ae12022-01-07 17:42:27 +00001246 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1247 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001248
1249 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1250
1251 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001252 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1253 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001254 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001255 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001256
1257 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1258 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001259 if (inputs.size() == 3)
1260 {
1261 desc.m_BiasEnabled = true;
1262 }
1263
telsoa01c577f2c2018-08-31 09:22:23 +01001264 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1265 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001266 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1267 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001268
telsoa01c577f2c2018-08-31 09:22:23 +01001269 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001270 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001271
Matteo Martincigh747ef822018-12-18 09:26:39 +00001272 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001273 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1274 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001275
1276 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001277 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1278 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1279
Pablo Tellof0bd6832019-04-26 17:58:13 +01001280 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1281 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1282 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1283 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001284
Jan Eilers53ef7952021-06-02 12:01:25 +01001285 // 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 +01001286 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001287
Cathal Corbett06902652022-04-14 17:55:11 +01001288 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1289 // Add the first input and weights tensor to the registration list.
1290 // The constant weights will be added by SetupConstantLayers.
1291 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1292
1293 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1294
1295 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001296 {
1297 desc.m_BiasEnabled = true;
1298 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001299
1300 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1301 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001302 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001303 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001304
Sadik Armagand109a4d2020-07-28 10:42:13 +01001305 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001306 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001307
1308 // register the input connection slots for the layer, connections are made after all layers have been created
1309 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001310 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001311
jimfly01c25411c2018-11-14 17:47:22 +00001312 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001313 // register the output connection slots for the layer, connections are made after all layers have been created
1314 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1315 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1316}
1317
Kevin May7d96b162021-02-03 17:38:41 +00001318void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001319{
1320 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1321
1322 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1323 CHECK_VALID_SIZE(inputs.size(), 1);
1324
1325 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1326 CHECK_VALID_SIZE(outputs.size(), 1);
1327
James Ward58dec6b2020-09-11 17:32:44 +01001328 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001329
1330 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001331 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001332
Sadik Armagand109a4d2020-07-28 10:42:13 +01001333 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001334 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1335
1336 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1337 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1338
1339 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1340 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1341}
1342
Teresa Charlin3ab85482021-06-08 16:59:29 +01001343void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1344{
1345 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1346
1347 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1348 CHECK_VALID_SIZE(inputs.size(), 2);
1349
1350 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1351 CHECK_VALID_SIZE(outputs.size(), 1);
1352
1353 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1354
1355 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1356 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1357
1358 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1359
1360 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001361
1362 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1363 {
1364 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1365 }
1366 else
1367 {
1368 int32_t axis = inputs[1]->shape[0];
1369
1370 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1371
1372 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1373 {
1374 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1375 }
1376
1377 if(axis < 0)
1378 {
1379 axis = inputDimSize + axis + 1;
1380 }
1381
Rob Hughesd812a312021-08-06 13:10:53 +01001382 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001383 unsigned int inputShapeIndex = 0;
1384 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1385 {
1386 if (i == static_cast<unsigned int>(axis))
1387 {
1388 shape[i] = 1;
1389 }
1390 else
1391 {
1392 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1393 ++inputShapeIndex;
1394 }
1395 }
1396
Rob Hughesd812a312021-08-06 13:10:53 +01001397 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001398 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001399
1400 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1401 ARMNN_ASSERT(layer != nullptr);
1402 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1403
1404 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1405 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1406
1407 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1408 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1409}
1410
Kevin May7d96b162021-02-03 17:38:41 +00001411void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001412{
1413 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1414
1415 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001416 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001417
1418 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1419 CHECK_VALID_SIZE(outputs.size(), 1);
1420
James Ward58dec6b2020-09-11 17:32:44 +01001421 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001422 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001423
josh minorba424d22019-11-13 10:55:17 -06001424 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001425 {
1426 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1427 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001428 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1429 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001430 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001431 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001432
Mike Kelly08759e22020-03-02 11:41:31 +00001433 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001434 }
1435
James Conroy05102392020-06-24 15:39:55 +01001436 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001437 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001438 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001439
James Conroy05102392020-06-24 15:39:55 +01001440 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001441 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001442 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1443
1444 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1445 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1446
1447 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1448 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1449}
1450
Kevin May7d96b162021-02-03 17:38:41 +00001451void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001452{
1453 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1454
Mike Kelly0d77ae12022-01-07 17:42:27 +00001455 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1456 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001457
1458 TransposeConvolution2dDescriptor desc;
1459 desc.m_BiasEnabled = false;
1460 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1461 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1462 desc.m_DataLayout = armnn::DataLayout::NHWC;
1463
1464 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001465 if (inputs.size() == 4)
1466 {
1467 desc.m_BiasEnabled = true;
1468 }
1469 else
1470 {
1471 CHECK_VALID_SIZE(inputs.size(), 3);
1472 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001473
1474 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1475 CHECK_VALID_SIZE(outputs.size(), 1);
1476
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001477 if (inputs[0])
1478 {
1479 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1480 std::vector<int> output_shape(tensorInfo.GetNumElements());
1481 if (tensorInfo.GetDataType() == DataType::Signed32)
1482 {
1483 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1484 }
1485 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1486 {
1487 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1488 {
1489 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1490 }
1491 }
1492 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1493 for (int dimension : output_shape)
1494 {
1495 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1496 }
1497 desc.m_OutputShapeEnabled = true;
1498 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001499 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001500 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1501
1502 // TfLite uses NHWC tensors
1503 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1504 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1505
1506 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1507 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1508
1509 CalcPadding(inputHeight,
1510 filterHeight,
1511 desc.m_StrideY,
1512 1, // DilationY
1513 desc.m_PadTop,
1514 desc.m_PadBottom,
1515 options->padding);
1516
1517 CalcPadding(inputWidth,
1518 filterWidth,
1519 desc.m_StrideX,
1520 1, // DilationX
1521 desc.m_PadLeft,
1522 desc.m_PadRight,
1523 options->padding);
1524
Mike Kelly5880b912022-01-28 16:18:54 +00001525 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001526
1527 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001528 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001529
David Monahan61683802021-01-12 09:11:07 +00001530 if (desc.m_BiasEnabled)
1531 {
1532 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001533 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001534 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001535 filterTensorAndData.first,
1536 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001537 layerName.c_str());
1538 }
1539 else
1540 {
1541 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001542 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001543 EmptyOptional(),
1544 layerName.c_str());
1545 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001546
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001547 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001548
Sadik Armagand109a4d2020-07-28 10:42:13 +01001549 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001550 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1551
1552 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1553 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001554 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001555
1556 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1557 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1558}
1559
Kevin May7d96b162021-02-03 17:38:41 +00001560void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001561{
1562 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1563}
1564
Kevin May7d96b162021-02-03 17:38:41 +00001565void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001566{
1567 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1568
1569 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1570 CHECK_VALID_SIZE(inputs.size(), 3);
1571
1572 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1573 CHECK_VALID_SIZE(outputs.size(), 1);
1574
1575 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1576 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1577
1578 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1579 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1580
1581 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1582 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1583
1584 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1585 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1586
1587 size_t step = 2;
1588 std::vector<std::pair<unsigned int, unsigned int>> crops;
1589 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1590 {
1591 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1592 }
1593
1594 armnn::BatchToSpaceNdDescriptor desc;
1595 desc.m_BlockShape = blockShape;
1596 desc.m_Crops = crops;
1597 desc.m_DataLayout = armnn::DataLayout::NHWC;
1598
James Ward58dec6b2020-09-11 17:32:44 +01001599 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001600
James Conroy05102392020-06-24 15:39:55 +01001601 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001602 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001603 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1604
1605 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1606 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001607 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1608
1609 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1610 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1611
1612 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1613 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1614}
1615
Kevin May7d96b162021-02-03 17:38:41 +00001616void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001617{
1618 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1619
1620 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1621 CHECK_VALID_SIZE(inputs.size(), 1);
1622
1623 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1624 CHECK_VALID_SIZE(outputs.size(), 1);
1625
1626 L2NormalizationDescriptor desc;
1627 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001628 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001629 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1630
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001631 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001632
Sadik Armagand109a4d2020-07-28 10:42:13 +01001633 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001634 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1635
1636 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1637 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1638
1639 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1640 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1641}
1642
Kevin May7d96b162021-02-03 17:38:41 +00001643void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001644{
1645 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1646}
1647
Kevin May7d96b162021-02-03 17:38:41 +00001648void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001649{
1650 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1651
1652 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1653 CHECK_VALID_SIZE(inputs.size(), 2);
1654
1655 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1656 CHECK_VALID_SIZE(outputs.size(), 1);
1657
James Ward58dec6b2020-09-11 17:32:44 +01001658 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001659
1660 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1661 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1662 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001663
Sadik Armagand109a4d2020-07-28 10:42:13 +01001664 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001665 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1666
1667 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1668 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001669 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1670
1671 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001672 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001673
1674 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1675 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1676}
1677
Kevin May7d96b162021-02-03 17:38:41 +00001678void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001679{
1680 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1681
1682 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1683 CHECK_VALID_SIZE(inputs.size(), 2);
1684
1685 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1686 CHECK_VALID_SIZE(outputs.size(), 1);
1687
James Ward58dec6b2020-09-11 17:32:44 +01001688 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001689
1690 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1691 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1692 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001693
Sadik Armagand109a4d2020-07-28 10:42:13 +01001694 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001695 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1696
1697 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1698 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001699 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1700
1701 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001702 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001703
1704 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1705 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1706}
1707
Kevin May7d96b162021-02-03 17:38:41 +00001708void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1709 size_t operatorIndex,
1710 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001711{
1712 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1713
Mike Kelly0d77ae12022-01-07 17:42:27 +00001714 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1715 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001716
1717 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1718
1719 std::string layerName;
1720
1721 switch (algorithm)
1722 {
1723 case PoolingAlgorithm::Average:
1724 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001725 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001726 break;
1727 case PoolingAlgorithm::Max:
1728 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001729 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001730 break;
1731 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001732 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001733 }
1734
1735 Pooling2dDescriptor desc;
1736
1737 desc.m_PoolType = algorithm;
1738 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1739 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1740 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1741 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1742 desc.m_PaddingMethod = PaddingMethod::Exclude;
1743 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001744 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001745
1746 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1747 CHECK_VALID_SIZE(inputs.size(), 1);
1748 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1749
1750 // assuming input is NHWC
1751 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1752 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1753
Pablo Tellof0bd6832019-04-26 17:58:13 +01001754 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1755 desc.m_PadTop, desc.m_PadBottom, options->padding);
1756 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1757 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001758
1759 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1760 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001761
Sadik Armagand109a4d2020-07-28 10:42:13 +01001762 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001763 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1764
1765 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1766 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001767 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001768
1769 // register the input connection slots for the layer, connections are made after all layers have been created
1770 // only the tensors for the inputs are relevant, exclude the const tensors
1771 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001772 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001773
jimfly01c25411c2018-11-14 17:47:22 +00001774 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001775 // register the output connection slots for the layer, connections are made after all layers have been created
1776 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1777 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1778}
1779
Kevin May7d96b162021-02-03 17:38:41 +00001780void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001781{
1782 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1783
1784 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1785 CHECK_VALID_SIZE(inputs.size(), 3);
1786 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1787 CHECK_VALID_SIZE(outputs.size(), 1);
1788
1789 SliceDescriptor desc;
1790
1791 // set begin tensor info for slice descriptor
1792 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1793 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1794
1795 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1796 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1797
1798 // set size tensor info for slice descriptor
1799 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1800 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1801
Mike Kelly7ba84d62021-09-10 15:27:19 +01001802 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1803 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001804 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001805 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1806
1807 for (unsigned int i = 0; i < signedSize.size(); ++i)
1808 {
1809 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001810
Mike Kelly7ba84d62021-09-10 15:27:19 +01001811 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1812 {
1813 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1814 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1815 signedValue,
1816 inputTensorInfo.GetShape()[i] - begin[i],
1817 CHECK_LOCATION().AsString()));
1818 }
1819
1820 if (signedValue == -1)
1821 {
1822 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1823 }
1824 else
1825 {
1826 size[i] = static_cast<unsigned int>(signedValue);
1827 }
1828 }
1829
josh minorba424d22019-11-13 10:55:17 -06001830 desc = SliceDescriptor(begin, size);
1831
James Ward58dec6b2020-09-11 17:32:44 +01001832 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001833
Sadik Armagand109a4d2020-07-28 10:42:13 +01001834 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001835 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1836
1837 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001838 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1839
1840 // register the input connection slots for the layer, connections are made after all layers have been created
1841 // only the tensors for the inputs are relevant, exclude the const tensors
1842 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1843 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1844
1845 // register the output connection slots for the layer, connections are made after all layers have been created
1846 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1847 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1848}
1849
Kevin May7d96b162021-02-03 17:38:41 +00001850void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001851{
1852 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001853 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1854 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001855
1856 SoftmaxDescriptor desc;
1857 desc.m_Beta = options->beta;
1858
1859 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1860 CHECK_VALID_SIZE(inputs.size(), 1);
1861 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1862 CHECK_VALID_SIZE(outputs.size(), 1);
1863
James Ward58dec6b2020-09-11 17:32:44 +01001864 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001865 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1866
Sadik Armagand109a4d2020-07-28 10:42:13 +01001867 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001868 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1869
1870 // register the input connection slots for the layer, connections are made after all layers have been created
1871 // only the tensors for the inputs are relevant, exclude the const tensors
1872 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1873 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1874
1875 // register the output connection slots for the layer, connections are made after all layers have been created
1876 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1877 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1878}
1879
Teresa Charlin455172a2022-06-29 15:35:57 +01001880void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
1881{
1882 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1883
1884 LogSoftmaxDescriptor desc;
1885
1886 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1887 CHECK_VALID_SIZE(inputs.size(), 1);
1888 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1889 CHECK_VALID_SIZE(outputs.size(), 1);
1890
1891 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
1892 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
1893
1894 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1895 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1896
1897 // register the input connection slots for the layer, connections are made after all layers have been created
1898 // only the tensors for the inputs are relevant, exclude the const tensors
1899 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1900 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1901
1902 // register the output connection slots for the layer, connections are made after all layers have been created
1903 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1904 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1905}
1906
Kevin May7d96b162021-02-03 17:38:41 +00001907void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001908{
1909 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1910
1911 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1912 CHECK_VALID_SIZE(inputs.size(), 3);
1913
1914 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1915 CHECK_VALID_SIZE(outputs.size(), 1);
1916
1917 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1918 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1919
1920 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1921 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1922
1923 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1924 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1925
1926 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1927 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1928
1929 size_t step = 2;
1930 std::vector<std::pair<unsigned int, unsigned int>> padList;
1931 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1932 {
1933 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1934 }
1935
1936 armnn::SpaceToBatchNdDescriptor desc;
1937 desc.m_BlockShape = blockShape;
1938 desc.m_PadList = padList;
1939 desc.m_DataLayout = armnn::DataLayout::NHWC;
1940
James Ward58dec6b2020-09-11 17:32:44 +01001941 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001942
James Conroy05102392020-06-24 15:39:55 +01001943 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001944 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001945 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1946
1947 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1948 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001949 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1950
1951 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1952 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1953
1954 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1955 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1956}
1957
Teresa Charlin3ab85482021-06-08 16:59:29 +01001958armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00001959 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001960{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001961 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001962 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1963
1964 if (inputTensorInfo.GetNumDimensions() > 4)
1965 {
1966 std::stringstream ss;
1967 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1968 << " shape:" << inputTensorInfo.GetShape() << " "
1969 << CHECK_LOCATION().AsString();
1970 throw ParseException(ss.str());
1971 }
1972
1973 if (squeezeDims.empty())
1974 {
1975 squeezeDims.assign(dimensionSequence,
1976 dimensionSequence+inputTensorInfo.GetNumDimensions());
1977 }
1978
1979 std::vector<uint32_t> outputDims;
1980 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1981 {
1982 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1983 auto currentDimension = inputTensorInfo.GetShape()[i];
1984 if (skipSqueeze || currentDimension != 1)
1985 {
1986 outputDims.push_back(currentDimension);
1987 }
1988 }
1989
1990 if (outputDims.size() > 4)
1991 {
1992 std::stringstream ss;
1993 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1994 << " shape:" << inputTensorInfo.GetShape() << " "
1995 << CHECK_LOCATION().AsString();
1996 throw ParseException(ss.str());
1997 }
1998
1999 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2000 outputDims.data());
2001
2002 // we need to preserve the tensor type and the quantization data as well
2003 TensorInfo outTensorInfo = inputTensorInfo;
2004 outTensorInfo.SetShape(outShape);
2005
2006 return outTensorInfo;
2007}
2008
Keith Davis0176fd82021-06-01 17:36:32 +01002009void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2010{
2011 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2012
2013 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2014 CHECK_VALID_SIZE(inputs.size(), 1);
2015 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2016 CHECK_VALID_SIZE(outputs.size(), 1);
2017
2018 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2019
2020 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2021 ARMNN_ASSERT(layer != nullptr);
2022
2023
2024 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2025 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2026
2027 // Check if output tensor type is Signed32 or Signed64
2028 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2029 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2030 {
2031 throw ParseException(
2032 fmt::format(
2033 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2034 CHECK_LOCATION().AsString()));
2035 }
2036
2037 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2038 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2039
2040 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2041 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2042}
2043
Kevin May7d96b162021-02-03 17:38:41 +00002044void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002045{
2046 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2047
2048 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2049 CHECK_VALID_SIZE(inputs.size(), 1);
2050
2051 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2052 CHECK_VALID_SIZE(outputs.size(), 1);
2053
2054 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2055 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002056 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002057
2058 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002059
2060 std::vector<uint32_t> squeezeDim;
2061 // A single negative dim index is interpreted as a negative index in python
2062 // Meaning the index will be the shape size plus the negative index value
2063 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2064 {
2065 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2066 squeezeDim.push_back(static_cast<uint32_t>(dim));
2067 }
2068 else
2069 {
2070 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2071 }
2072
2073 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2074
James Conroy05102392020-06-24 15:39:55 +01002075 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002076
2077 ReshapeDescriptor reshapeDesc;
2078 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2079
telsoa01c577f2c2018-08-31 09:22:23 +01002080 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002081 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002082 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2083
2084 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2085 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2086
2087 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2088 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2089}
2090
Kevin May7d96b162021-02-03 17:38:41 +00002091void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002092{
2093 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2094
2095 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2096 CHECK_VALID_SIZE(inputs.size(), 4);
2097
2098 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2099 CHECK_VALID_SIZE(outputs.size(), 1);
2100
Mike Kelly0d77ae12022-01-07 17:42:27 +00002101 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2102 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002103
2104 StridedSliceDescriptor desc;
2105 desc.m_BeginMask = options->begin_mask;
2106 desc.m_EllipsisMask = options->ellipsis_mask;
2107 desc.m_EndMask = options->end_mask;
2108 desc.m_NewAxisMask = options->new_axis_mask;
2109 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2110 desc.m_DataLayout = armnn::DataLayout::NHWC;
2111
2112 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2113 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2114
2115 std::vector<int> begin(beginTensorInfo.GetNumElements());
2116 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2117
2118 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2119 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2120
2121 std::vector<int> end(endTensorInfo.GetNumElements());
2122 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2123
2124 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2125 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2126
2127 std::vector<int> stride(strideTensorInfo.GetNumElements());
2128 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2129
2130 desc.m_Begin = begin;
2131 desc.m_End = end;
2132 desc.m_Stride = stride;
2133
James Ward58dec6b2020-09-11 17:32:44 +01002134 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002135 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002136 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002137
Sadik Armagand109a4d2020-07-28 10:42:13 +01002138 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002139 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2140
2141 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2142 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2143
2144 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2145 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2146}
2147
Kevin May7d96b162021-02-03 17:38:41 +00002148void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002149{
2150 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2151
Mike Kelly0d77ae12022-01-07 17:42:27 +00002152 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2153 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002154
2155 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2156 CHECK_VALID_SIZE(inputs.size(), 2);
2157
2158 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2159 CHECK_VALID_SIZE(outputs.size(), 1);
2160
2161 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2162 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2163
James Ward58dec6b2020-09-11 17:32:44 +01002164 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002165 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002166 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002167
Sadik Armagand109a4d2020-07-28 10:42:13 +01002168 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002169 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2170
2171 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002172 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002173
2174 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2175
2176 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2177 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2178}
2179
Kevin May7d96b162021-02-03 17:38:41 +00002180void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302181{
2182 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2183
Mike Kelly0d77ae12022-01-07 17:42:27 +00002184 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2185 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302186
2187 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2188 CHECK_VALID_SIZE(inputs.size(), 2);
2189
2190 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2191 CHECK_VALID_SIZE(outputs.size(), 1);
2192
2193 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2194 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2195
James Ward58dec6b2020-09-11 17:32:44 +01002196 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302197 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002198 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302199
Sadik Armagand109a4d2020-07-28 10:42:13 +01002200 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302201 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2202
2203 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002204 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302205 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2206
2207 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2208 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2209}
2210
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002211void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2212{
2213 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2214
2215 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2216 CHECK_VALID_SIZE(inputs.size(), 2);
2217
2218 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2219 CHECK_VALID_SIZE(outputs.size(), 1);
2220
2221 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2222 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2223
2224 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2225 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2226 ARMNN_ASSERT(layer != nullptr);
2227
2228 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2229 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2230
2231 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2232 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2233 layer = AddFusedFloorLayer(layer, 0);
2234
2235 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2236 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2237}
2238
Kevin May7d96b162021-02-03 17:38:41 +00002239void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002240{
2241 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2242
Mike Kelly0d77ae12022-01-07 17:42:27 +00002243 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2244 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002245
2246 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2247 CHECK_VALID_SIZE(inputs.size(), 2);
2248
2249 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2250 CHECK_VALID_SIZE(outputs.size(), 1);
2251
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002252 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2253 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2254
James Ward58dec6b2020-09-11 17:32:44 +01002255 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002256 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002257 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002258
Sadik Armagand109a4d2020-07-28 10:42:13 +01002259 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002260 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2261
2262 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002263 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002264 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2265
2266 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2267 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2268}
2269
Kevin May7d96b162021-02-03 17:38:41 +00002270void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002271{
2272 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2273
Mike Kelly0d77ae12022-01-07 17:42:27 +00002274 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2275 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002276
2277 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2278 CHECK_VALID_SIZE(inputs.size(), 2);
2279
2280 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2281 CHECK_VALID_SIZE(outputs.size(), 1);
2282
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002283 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2284 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2285
James Ward58dec6b2020-09-11 17:32:44 +01002286 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002287 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002288 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002289
Sadik Armagand109a4d2020-07-28 10:42:13 +01002290 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002291 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2292
2293 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002294 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002295 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2296
2297 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2298 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2299}
2300
Kevin May7d96b162021-02-03 17:38:41 +00002301void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002302{
2303 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2304
2305 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2306
2307 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2308 CHECK_VALID_SIZE(outputs.size(), 1);
2309
2310 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2311 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2312
2313 armnn::MeanDescriptor desc;
2314 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2315 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2316 desc.m_Axis = axis;
2317
2318 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002319 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002320
2321 desc.m_KeepDims =
2322 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2323 true : false;
2324
James Ward58dec6b2020-09-11 17:32:44 +01002325 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002326 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002327 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002328
2329 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2330
2331 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2332 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2333
2334 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2335 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2336}
2337
Kevin May7d96b162021-02-03 17:38:41 +00002338void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002339{
2340 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2341
Kevin May7d96b162021-02-03 17:38:41 +00002342 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002343
Kevin May7d96b162021-02-03 17:38:41 +00002344 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002345 CHECK_VALID_SIZE(outputs.size(), 1);
2346
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002347 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002348 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002349
Mike Kelly0d77ae12022-01-07 17:42:27 +00002350 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002351
2352 size_t step = 2;
2353 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002354 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2355
2356 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002357 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002358 CHECK_VALID_SIZE(inputs.size(), 2);
2359
2360 if (inputTensorInfo.IsQuantized())
2361 {
2362 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2363 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002364 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002365 else if (opcode == tflite::BuiltinOperator_PADV2)
2366 {
2367 CHECK_VALID_SIZE(inputs.size(), 3);
2368
2369 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2370
2371 if (padValueTensorInfo.GetNumElements() != 1)
2372 {
2373 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2374 }
2375 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2376
2377 // Get the pad value from the input tensor
2378 if (padValueBufferPtr->data.size() > 0)
2379 {
2380 switch (padValueTensorInfo.GetDataType())
2381 {
2382 case armnn::DataType::Float32:
2383 {
2384 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2385 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2386 desc.m_PadValue = padValueBuffer[0];
2387 break;
2388 }
2389 case armnn::DataType::QAsymmU8:
2390 {
2391 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2392 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2393 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2394 padValueTensorInfo.GetQuantizationScale(),
2395 padValueTensorInfo.GetQuantizationOffset());
2396 break;
2397 }
2398 case armnn::DataType::QAsymmS8:
2399 case armnn::DataType::QSymmS8:
2400 {
2401 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2402 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2403 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2404 padValueTensorInfo.GetQuantizationScale(),
2405 padValueTensorInfo.GetQuantizationOffset());
2406 break;
2407 }
2408 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2409 }
2410 }
2411 else if (inputTensorInfo.IsQuantized())
2412 {
2413 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2414 }
2415 }
2416
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002417 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2418 {
2419 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2420 }
2421
Mike Kelly0d77ae12022-01-07 17:42:27 +00002422 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2423 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002424 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002425
2426 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2427 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002428 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2429
2430 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2431 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2432
2433 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2434 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2435}
2436
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002437void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2438{
2439 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2440
2441 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2442 CHECK_VALID_SIZE(inputs.size(), 2);
2443
2444 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2445 CHECK_VALID_SIZE(outputs.size(), 1);
2446
2447 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2448
2449 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2450 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2451
2452 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2453 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2454
2455 size_t step = 2;
2456 armnn::PadDescriptor desc;
2457 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2458 {
2459 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2460 }
2461
2462 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2463 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2464
2465 if (options->mode == tflite::MirrorPadMode_REFLECT)
2466 {
2467 desc.m_PaddingMode = PaddingMode::Reflect;
2468 }
2469 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2470 {
2471 desc.m_PaddingMode = PaddingMode::Symmetric;
2472 }
2473 else
2474 {
2475 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2476 }
2477
2478 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2479 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2480 auto inputShape = inputTensorInfo.GetShape();
2481 auto padList = desc.m_PadList;
2482
2483 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2484 for(unsigned int i = 0; i < padList.size(); ++i)
2485 {
2486 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2487 padList.at(i).second > (inputShape[i] - isReflect))
2488 {
2489 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2490 "equal (Symmetric) to the dimension size.");
2491 }
2492 }
2493
2494 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2495 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2496
2497 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2498 ARMNN_ASSERT(layer != nullptr);
2499 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2500
2501 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2502 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2503
2504 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2505 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2506}
2507
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002508void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2509{
2510 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2511
2512 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2513 CHECK_VALID_SIZE(inputs.size(), 2);
2514
2515 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2516 CHECK_VALID_SIZE(outputs.size(), 1);
2517
2518 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2519
2520 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2521 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2522 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2523 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2524
2525 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2526 ARMNN_ASSERT(layer != nullptr);
2527 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2528
2529 if (IsConstTensor(inputs[1]))
2530 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002531 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002532 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2533 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002534
Mike Kelly5880b912022-01-28 16:18:54 +00002535 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2536 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002537 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2538 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002539 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002540 ARMNN_ASSERT(constLayer != nullptr);
2541
2542 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2543 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2544 RegisterOutputSlots(subgraphIndex,
2545 VIRTUAL_OPERATOR_ID,
2546 constLayer,
2547 { inputTensorIndexes[1] });
2548 }
2549 else
2550 {
2551 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2552 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2553 }
2554
2555 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2556 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2557}
2558
Kevin May7d96b162021-02-03 17:38:41 +00002559void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002560{
2561 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2562
2563 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2564 CHECK_VALID_SIZE(inputs.size(), 1);
2565
2566 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2567 CHECK_VALID_SIZE(outputs.size(), 1);
2568
James Ward58dec6b2020-09-11 17:32:44 +01002569 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002570
2571 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002572 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002573
Sadik Armagand109a4d2020-07-28 10:42:13 +01002574 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002575 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2576
2577 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2578 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2579
2580 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2581 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2582}
Finn Williamsc42c3842019-01-22 14:18:11 +00002583
Kevin May7d96b162021-02-03 17:38:41 +00002584void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002585{
Finn Williamsc42c3842019-01-22 14:18:11 +00002586 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002587}
2588
Kevin May7d96b162021-02-03 17:38:41 +00002589void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002590{
Finn Williamsc42c3842019-01-22 14:18:11 +00002591 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2592}
Sadik Armagan58f39192018-09-17 14:14:39 +01002593
Kevin May7d96b162021-02-03 17:38:41 +00002594void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002595{
Jan Eilers2f746b32020-07-28 14:00:06 +01002596 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002597}
2598
Kevin May7d96b162021-02-03 17:38:41 +00002599void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002600{
2601 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2602}
2603
Kevin May7d96b162021-02-03 17:38:41 +00002604void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002605{
2606 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2607}
2608
Kevin May7d96b162021-02-03 17:38:41 +00002609void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002610{
2611 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2612}
2613
Kevin May7d96b162021-02-03 17:38:41 +00002614void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002615{
2616 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2617}
Finn Williamsc42c3842019-01-22 14:18:11 +00002618
Kevin May7d96b162021-02-03 17:38:41 +00002619void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002620{
2621 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002622 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002623 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002624
2625 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2626 CHECK_VALID_SIZE(inputs.size(), 1);
2627
2628 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2629 CHECK_VALID_SIZE(outputs.size(), 1);
2630
James Ward58dec6b2020-09-11 17:32:44 +01002631 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002632 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002633 activationDesc.m_Function = activationType;
2634
2635 switch (activationType)
2636 {
2637 case ActivationFunction::ReLu:
2638 {
James Ward58dec6b2020-09-11 17:32:44 +01002639 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002640 break;
2641 }
2642 case ActivationFunction::BoundedReLu:
2643 {
James Ward58dec6b2020-09-11 17:32:44 +01002644 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002645 activationDesc.m_A = 6.0f;
2646 activationDesc.m_B = 0.0f;
2647 break;
2648 }
2649 case ActivationFunction::Sigmoid:
2650 {
James Ward58dec6b2020-09-11 17:32:44 +01002651 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002652 break;
2653 }
Nina Drozd99851762019-04-09 09:37:38 +01002654 case ActivationFunction::TanH:
2655 {
James Ward58dec6b2020-09-11 17:32:44 +01002656 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002657 activationDesc.m_A = 1.0f;
2658 activationDesc.m_B = 1.0f;
2659 break;
2660 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002661 case ActivationFunction::LeakyReLu:
2662 {
James Ward58dec6b2020-09-11 17:32:44 +01002663 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002664 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002665 activationDesc.m_A = options->alpha;
2666 break;
2667 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002668 case ActivationFunction::Elu:
2669 {
2670 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2671 activationDesc.m_A = 1.0f;
2672 break;
2673 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002674 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002675 {
James Ward58dec6b2020-09-11 17:32:44 +01002676 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002677 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002678 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002679 default:
2680 {
2681 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002682 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2683 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002684 }
2685 }
2686
2687 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002688
Sadik Armagand109a4d2020-07-28 10:42:13 +01002689 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002690 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2691
2692 // register the input connection slots for the layer, connections are made after all layers have been created
2693 // only the tensors for the inputs are relevant, exclude the const tensors
2694 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2695 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2696
2697 // register the output connection slots for the layer, connections are made after all layers have been created
2698 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2699 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2700}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002701armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2702 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002703{
2704 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2705 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2706
2707 if (stretchDim != targetDimsIn.end())
2708 {
2709 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2710 {
2711 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002712 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002713 }
2714
2715 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002716 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002717 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2718
2719 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2720 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2721 }
2722
2723 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2724
2725 TensorInfo reshapeInfo = inputTensorInfo;
2726 reshapeInfo.SetShape(outputShape);
2727
2728 return reshapeInfo;
2729}
2730
Kevin May7d96b162021-02-03 17:38:41 +00002731void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002732{
2733 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2734
2735 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002736
2737 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2738 CHECK_VALID_SIZE(outputs.size(), 1);
2739
Mike Kelly0d77ae12022-01-07 17:42:27 +00002740 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2741 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002742 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002743
2744 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002745 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002746 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002747
Jan Eilersbac9b352020-07-13 13:40:24 +01002748 // Extracting new shape for the output
2749 // There are two ways it can be passed
2750 // * First is to define the target shape in the operator built-in options
2751 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002752 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002753 bool targetShapeFound = false;
2754 // Check if built-in options were given
2755 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002756 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002757 // make sure the parameter is given
2758 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002759 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002760 targetShape = options->new_shape;
2761 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002762 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002763 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002764
2765 // If there is no built-in option given or if the built-in new_shape parameter was empty
2766 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002767 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002768 // Check for a second input tensor
2769 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002770 {
2771 if (inputs[1]->is_variable)
2772 {
2773 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2774 }
2775
2776 if (inputs[1]->shape.size() != 1)
2777 {
2778 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2779 }
2780
2781 if (inputs[1]->type != tflite::TensorType_INT32)
2782 {
2783 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2784 }
2785
Teresa Charlin6a056a42021-12-01 10:25:43 +00002786 // Extract target shape from input
2787 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2788 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002789 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002790 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002791 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2792 {
2793 targetShape.push_back(values[i]);
2794 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002795 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002796 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002797 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002798 try
2799 {
2800 // We attempt to infer during Runtime.
2801 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2802 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2803 if (reshapeShapes[0] > 2)
2804 {
2805 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2806 "When inferring during runtime, the parser only supports "
2807 "shape (batch, -1) or (-1) for target shape input.",
2808 reshapeShapes[0],
2809 layerName,
2810 CHECK_LOCATION().AsString()));
2811 }
2812
2813 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2814 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2815 if (reshapeShapes[0] == 1)
2816 {
2817 targetShape = {numInputElements};
2818 }
2819 else if (reshapeShapes[0] == 2)
2820 {
2821 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2822 }
2823 }
2824 catch (const std::exception& exc)
2825 {
2826 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2827 "Reshape operation. Reshape operator target shape input buffer data "
2828 "is null. " << exc.what());
2829 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002830 }
2831 }
2832 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002833 {
2834 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2835 "At least one method required");
2836 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002837 }
2838
kevmay0171972a82018-12-17 14:28:03 +00002839 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002840 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002841
kevmay0171972a82018-12-17 14:28:03 +00002842 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002843 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2844 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002845 {
2846 std::stringstream ss;
2847 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002848 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002849 << " does not equal output shape "
2850 << actualOutputTensorInfo.GetShape()
2851 << ": "
2852 << CHECK_LOCATION().AsString();
2853 throw ParseException(ss.str());
2854 }
2855
Sadikb94967b2018-09-19 15:30:00 +01002856 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002857 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002858
Sadikb94967b2018-09-19 15:30:00 +01002859 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002860 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002861 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002862
2863 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2864 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2865
2866 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2867 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2868}
2869
Kevin May7d96b162021-02-03 17:38:41 +00002870void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002871{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002872 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2873}
2874
Kevin May7d96b162021-02-03 17:38:41 +00002875void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002876{
2877 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2878}
2879
Kevin May7d96b162021-02-03 17:38:41 +00002880void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002881{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002882 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2883
2884 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2885 CHECK_VALID_SIZE(inputs.size(), 2);
2886
2887 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2888 CHECK_VALID_SIZE(outputs.size(), 1);
2889
2890 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2891
2892 // Data for the parsed tensor args (size) must be stored locally.
2893 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2894
2895 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2896 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2897
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002898 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002899 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002900 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002901 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2902 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002903
James Ward58dec6b2020-09-11 17:32:44 +01002904 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002905
2906 switch (resizeMethod)
2907 {
2908 case ResizeMethod::Bilinear:
2909 {
James Ward58dec6b2020-09-11 17:32:44 +01002910 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002911
2912 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2913 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2914
David Monahan4a0c9b92020-05-30 09:48:39 +01002915 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002916 break;
2917 }
2918 case ResizeMethod::NearestNeighbor:
2919 {
James Ward58dec6b2020-09-11 17:32:44 +01002920 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002921 break;
2922 }
2923 default:
2924 {
2925 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002926 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2927 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002928 }
2929 }
2930
James Conroy05102392020-06-24 15:39:55 +01002931 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002932 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002933 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2934
2935 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2936 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002937 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2938
2939 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2940 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2941
2942 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2943 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2944}
2945
Kevin May7d96b162021-02-03 17:38:41 +00002946void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002947{
2948 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2949
Mike Kelly0d77ae12022-01-07 17:42:27 +00002950 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2951 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002952
2953 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2954
2955 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2956 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2957 CHECK_VALID_SIZE(outputs.size(), 1);
2958
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002959 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2960 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002961
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002962 const unsigned int concatDimInput = static_cast<unsigned int>(
2963 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002964
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002965 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2966 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002967
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002968 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002969
2970 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2971 {
2972 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2973
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002974 // This set up concatDescriptor view origin
2975 armnnUtils::ProcessConcatInputTensorInfo(
2976 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002977 }
2978
James Ward58dec6b2020-09-11 17:32:44 +01002979 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002980 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002981
Jim Flynn906f9462019-05-10 13:55:21 +01002982 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002983 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002984 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002985
James Conroy05102392020-06-24 15:39:55 +01002986 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002987 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002988
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002989 // add fused activation layer
2990 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002991
Sadik Armagan479045b2018-10-01 11:51:37 +01002992 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2993 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2994}
2995
Kevin May7d96b162021-02-03 17:38:41 +00002996void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002997{
2998 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2999
Mike Kelly0d77ae12022-01-07 17:42:27 +00003000 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003001 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3002
3003 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3004
3005 FullyConnectedDescriptor desc;
3006 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003007 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003008
3009 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3010 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3011 CHECK_VALID_SIZE(outputs.size(), 1);
3012
3013 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
3014
3015 // Fully Connected Layer accepts two dimensional weights input
3016 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3017 if (weightsDimension != 2)
3018 {
3019 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003020 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3021 "Node {}",
3022 weightsDimension,
3023 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003024 }
3025
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003026 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003027 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003028
Matthew Sloyan81beae32021-07-13 19:46:11 +01003029 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3030 // Add the first input tensor to the registration list
3031 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3032 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00003033 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003034
3035 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3036
Matthew Sloyan81beae32021-07-13 19:46:11 +01003037 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3038 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003039
Mike Kelly5880b912022-01-28 16:18:54 +00003040 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3041 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3042 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3043 {
3044 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3045 }
3046
Finn Williamsd4fa5452021-03-01 12:31:41 +00003047 if (inputs.size() == 3)
3048 {
3049 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003050 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003051
3052 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3053 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003054
3055 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3056 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3057 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3058 {
3059 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3060 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003061 }
3062
Matthew Sloyan81beae32021-07-13 19:46:11 +01003063 // Filters and biases are always passed to fully connected as inputs
3064 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003065
3066 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003067
Finn Williamsd4fa5452021-03-01 12:31:41 +00003068 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003069 if (inputTensorInfo.GetNumDimensions() > 2)
3070 {
3071 // Add reshape to flatten to 2D [batch_size, input_size],
3072 // where "input_size" corresponds to the number of inputs to the layer,
3073 // matching the second dimension of weights,
3074 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3075 std::vector<unsigned int> reshapedDimensions(2);
3076 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3077 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3078
3079 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3080 {
3081 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003082 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3083 reshapedDimensions[1],
3084 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003085 }
3086
3087 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3088 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3089
James Ward58dec6b2020-09-11 17:32:44 +01003090 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003091 armnn::ReshapeDescriptor reshapeDescriptor;
3092 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3093 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003094
3095 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3096 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3097
3098 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003099 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3100 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3101 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003102 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003103
3104 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003105
Sadik Armagand109a4d2020-07-28 10:42:13 +01003106 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003107 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3108
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003109 // we need to add the activation layer and fortunately we don't need to care about the data layout
3110 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3111 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003112
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003113 // register the output connection slots for the layer, connections are made after all layers have been created
3114 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3115 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3116}
3117
Kevin May7d96b162021-02-03 17:38:41 +00003118void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003119{
3120 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3121
Mike Kelly0d77ae12022-01-07 17:42:27 +00003122 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003123
3124 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3125 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3126 CHECK_VALID_SIZE(outputs.size(), 4);
3127
3128 // Obtain custom options from flexbuffers
3129 auto custom_options = operatorPtr->custom_options;
3130 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3131
3132 // Obtain descriptor information from tf lite
3133 DetectionPostProcessDescriptor desc;
3134 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3135 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3136 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3137 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3138 desc.m_NumClasses = m["num_classes"].AsUInt32();
3139 desc.m_ScaleH = m["h_scale"].AsFloat();
3140 desc.m_ScaleW = m["w_scale"].AsFloat();
3141 desc.m_ScaleX = m["x_scale"].AsFloat();
3142 desc.m_ScaleY = m["y_scale"].AsFloat();
3143
keidav0107d58c72019-02-26 11:57:39 +00003144 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003145 {
keidav0107d58c72019-02-26 11:57:39 +00003146 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003147 }
3148 if (!(m["detections_per_class"].IsNull()))
3149 {
3150 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3151 }
3152
3153 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3154 {
3155 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3156 "must be positive and less than or equal to 1.");
3157 }
3158
3159 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003160 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003161
James Ward58dec6b2020-09-11 17:32:44 +01003162 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003163 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003164 layerName.c_str());
3165
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003166 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003167
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003168 // The model does not specify the output shapes.
3169 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3170 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3171 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3172 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3173 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3174 m_OverridenOutputShapes.push_back({ 1 });
3175
keidav011b3e2ea2019-02-21 10:07:37 +00003176 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3177 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003178 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003179 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3180 }
3181
3182 // Register the input connection slots for the layer, connections are made after all layers have been created
3183 // only the tensors for the inputs are relevant, exclude the const tensors
3184 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3185 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3186
3187 // Register the output connection slots for the layer, connections are made after all layers have been created
3188 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3189 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3190 outputTensorIndexes[1],
3191 outputTensorIndexes[2],
3192 outputTensorIndexes[3]});
3193}
3194
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003195/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003196void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003197{
3198 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3199
3200 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3201 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3202 CHECK_VALID_SIZE(outputs.size(), 1);
3203
3204 if (inputs.size() < 1)
3205 {
3206 throw ParseException("Pack must have at least one input.");
3207 }
3208
3209 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3210 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3211
3212 StackDescriptor desc;
3213 desc.m_Axis = static_cast<uint32_t>(options->axis);
3214 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3215
3216 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3217 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3218 desc.m_InputShape = inputTensorInfo.GetShape();
3219
James Ward58dec6b2020-09-11 17:32:44 +01003220 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003221 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3222
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003223 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003224
Sadik Armagand109a4d2020-07-28 10:42:13 +01003225 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003226 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3227
3228 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3229 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3230
3231 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3232 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3233}
3234
Mike Kelly5880b912022-01-28 16:18:54 +00003235void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3236{
3237 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3238
3239 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3240 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3241
3242 if (inputs.size() < 2)
3243 {
3244 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3245 }
3246
3247 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3248 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3249 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3250 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3251 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3252 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3253
3254 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3255 // Please refer to each operand at
3256 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3257 armnn::LstmInputParams params;
3258
3259 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3260 {
3261 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3262 inputTensorInfo).first;
3263 }
3264
3265 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3266 inputTensorInfo).first;
3267 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3268 inputTensorInfo).first;
3269 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3270 inputTensorInfo).first;
3271
3272 // Recurrent weight tensors of size {n_cell, n_output}
3273 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3274 {
3275 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3276 inputTensorInfo).first;
3277 }
3278
3279 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3280 inputTensorInfo).first;
3281 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3282 inputTensorInfo).first;
3283 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3284 inputTensorInfo).first;
3285
3286 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3287 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3288 {
3289 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3290 inputTensorInfo).first;
3291 }
3292
3293 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3294 {
3295 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3296 inputTensorInfo).first;
3297 }
3298
3299 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3300 {
3301 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3302 inputTensorInfo).first;
3303 }
3304
3305 // Gates bias tensors of size {n_cell}
3306 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3307 {
3308 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3309 inputTensorInfo).first;
3310 }
3311
3312 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3313 inputTensorInfo).first;
3314 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3315 inputTensorInfo).first;
3316 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3317 inputTensorInfo).first;
3318
3319 // Projection weight tensor of size {n_output, n_cell}
3320 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3321 {
3322 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3323 inputTensorInfo).first;
3324 }
3325 // Projection bias tensor of size {n_output}
3326 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3327 {
3328 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3329 inputTensorInfo).first;
3330 }
3331
3332 // These state tensors are defined as variable tensors, and will be modified by this op.
3333 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3334 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3335 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3336 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3337
3338 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3339 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3340 {
3341 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3342 inputTensorInfo).first;
3343 }
3344
3345 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3346 {
3347 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3348 inputTensorInfo).first;
3349 }
3350
3351 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3352 {
3353 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3354 inputTensorInfo).first;
3355 }
3356
3357 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3358 {
3359 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3360 inputTensorInfo).first;
3361 }
3362
3363 // set the layer descriptor
3364 armnn::UnidirectionalSequenceLstmDescriptor desc;
3365 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3366 desc.m_ClippingThresCell = nodeParams->cell_clip;
3367 desc.m_ClippingThresProj = nodeParams->proj_clip;
3368 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3369 || params.m_RecurrentToInputWeights == nullptr
3370 || params.m_InputGateBias == nullptr);
3371 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3372 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3373 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3374 || params.m_ForgetLayerNormWeights != nullptr
3375 || params.m_CellLayerNormWeights != nullptr
3376 || params.m_OutputLayerNormWeights != nullptr);
3377 desc.m_TimeMajor = nodeParams->time_major;
3378
Mike Kellyc0800a32022-06-15 10:57:52 +01003379 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003380 {
3381 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3382 inputTensorInfo).first;
3383 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3384 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3385
3386 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3387 inputTensorInfo).first;
3388 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3389 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3390
3391 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3392 inputTensorInfo).first;
3393 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3394 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3395
3396 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3397 inputTensorInfo).first;
3398 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3399 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3400 }
3401 else
3402 {
3403 float defaultIntermediate = std::pow(2, -12);
3404 desc.m_InputIntermediateScale = defaultIntermediate;
3405 desc.m_ForgetIntermediateScale = defaultIntermediate;
3406 desc.m_CellIntermediateScale = defaultIntermediate;
3407 desc.m_OutputIntermediateScale = defaultIntermediate;
3408 }
3409
Mike Kellyc0800a32022-06-15 10:57:52 +01003410 if (operatorPtr->intermediates.size() > 4)
3411 {
3412 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3413 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003414
Mike Kellyc0800a32022-06-15 10:57:52 +01003415 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3416 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3417 }
Mike Kelly5880b912022-01-28 16:18:54 +00003418 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3419 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3420 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3421
3422 armnn::DataType dataType = inputTensorInfo.GetDataType();
3423 float qScale = inputTensorInfo.GetQuantizationScale();
3424 float qOffset = inputTensorInfo.GetQuantizationOffset();
3425
3426 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3427 if (!desc.m_CifgEnabled)
3428 {
3429 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3430 }
3431 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3432 cellStateInInfo.GetDataType(),
3433 cellStateInInfo.GetQuantizationScale(),
3434 cellStateInInfo.GetQuantizationOffset());
3435 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3436
3437 armnn::LstmInputParamsInfo paramsInfo;
3438 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3439 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3440 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3441 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3442 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3443 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3444 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3445 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3446 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3447
3448 if (!desc.m_CifgEnabled)
3449 {
3450 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3451 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3452 if (params.m_CellToInputWeights != nullptr)
3453 {
3454 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3455 }
3456 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3457 }
3458
3459 if (desc.m_ProjectionEnabled)
3460 {
3461 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3462 if (params.m_ProjectionBias != nullptr)
3463 {
3464 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3465 }
3466 }
3467
3468 if (desc.m_PeepholeEnabled)
3469 {
3470 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3471 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3472 }
3473
3474 if (desc.m_LayerNormEnabled)
3475 {
3476 if(!desc.m_CifgEnabled)
3477 {
3478 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3479 }
3480 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3481 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3482 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3483 }
3484
3485 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3486 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3487 ARMNN_ASSERT(layer != nullptr);
3488
3489 // register the input connection slots for the layer, connections are made after all layers have been created
3490 // only the tensors for the inputs are relevant, exclude the const tensors
3491 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3492 operatorPtr->inputs[18],
3493 operatorPtr->inputs[19]});
3494 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3495 inputTensorIndexes[1],
3496 inputTensorIndexes[2]});
3497
3498 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3499
3500 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3501 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3502 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3503
3504 unsigned int tensorIndex = outputTensorIndexes[0];
3505 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3506 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3507}
3508
Kevin May7d96b162021-02-03 17:38:41 +00003509void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003510{
3511 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3512
Mike Kelly0d77ae12022-01-07 17:42:27 +00003513 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3514 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003515
3516 // This unpackAxis indicates the axis to unpack
3517 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3518
3519 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3520 CHECK_VALID_SIZE(inputs.size(), 1);
3521
3522 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003523
3524 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3525 {
3526 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003527 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3528 "the number of input dimension {} {}",
3529 unpackAxis,
3530 inputTensorInfo.GetNumDimensions(),
3531 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003532 }
3533
Nina Drozd200e3802019-04-15 09:47:39 +01003534 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3535 // If num is not defined, automatically infer from the length of the dimension axis.
3536 if(unpackNum == 0)
3537 {
3538 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3539 }
3540
3541 // If unpack number cannot be inferred and is still zero, throw ParseException.
3542 if(unpackNum == 0)
3543 {
3544 throw ParseException("Number to unpack must greater than zero.");
3545 }
3546
3547 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3548 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3549
3550 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3551 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3552
3553 // Add current input shape to unpackDimSizes
3554 for (unsigned int i = 0; i < inputDimSize; ++i)
3555 {
3556 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3557 }
3558
3559 if (unpackDimSizes[unpackAxis] != unpackNum)
3560 {
3561 throw ParseException("Number to unpack must be the same as length of the dimension to "
3562 "unpack along.");
3563 }
3564
3565 unpackDimSizes[unpackAxis] /= unpackNum;
3566
3567 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3568 for (unsigned int j = 0; j < unpackNum; ++j)
3569 {
3570 // Set the size of the views.
3571 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3572 {
3573 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3574 }
3575 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3576 }
3577
James Ward58dec6b2020-09-11 17:32:44 +01003578 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003579 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003580 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003581
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003582 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3583 unpackDimSizes.data());
3584
Nina Drozd200e3802019-04-15 09:47:39 +01003585 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3586 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3587
Finn Williamsb49ed182021-06-29 15:50:08 +01003588 std::vector<unsigned int> reshapeDims;
3589 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3590 {
3591 if (axis != unpackAxis)
3592 {
3593 reshapeDims.push_back(splitOutShape[axis]);
3594 }
3595 }
3596
3597 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3598
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003599 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3600 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3601 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003602 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003603 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003604 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003605 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003606 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3607
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003608 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3609 outputTensorInfo.GetDataType(),
3610 outputTensorInfo.GetQuantizationScale(),
3611 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003612 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3613
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003614 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003615
3616 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3617 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3618 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3619 }
Nina Drozd200e3802019-04-15 09:47:39 +01003620}
3621
Kevin May7d96b162021-02-03 17:38:41 +00003622void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003623{
3624 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3625
Mike Kelly0d77ae12022-01-07 17:42:27 +00003626 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3627 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003628
3629 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3630
Nina Drozd200e3802019-04-15 09:47:39 +01003631 // If number of splits cannot be inferred and is zero, throw ParseException.
3632 if(numSplits == 0)
3633 {
3634 throw ParseException("Number to splits must greater than zero.");
3635 }
3636
Nina Drozd0324f482019-04-08 10:52:10 +01003637 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3638 CHECK_VALID_SIZE(inputs.size(), 2);
3639 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3640 CHECK_VALID_SIZE(outputs.size(), numSplits);
3641
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003642 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3643 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3644 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003645
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003646 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003647 if (axisBufferPtr == nullptr)
3648 {
3649 throw ParseException(
3650 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3651 CHECK_LOCATION().AsString()));
3652 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003653
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003654 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3655 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3656 int32_t axis = axisData[0];
3657
3658 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3659 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3660 {
3661 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3662 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3663 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3664 throw ParseException(
3665 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3666 axis,
3667 CHECK_LOCATION().AsString()));
3668 }
3669
3670 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003671
Nina Drozd0324f482019-04-08 10:52:10 +01003672 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003673 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003674 {
3675 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003676 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3677 inputTensorInfo.GetNumDimensions(),
3678 MaxNumOfTensorDimensions,
3679 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003680 }
3681
3682 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3683
3684 // Add current input shape to splitterDimSizes
3685 for (unsigned int i = 0; i < inputDimSize; ++i)
3686 {
3687 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3688 }
3689
3690 if (splitterDimSizes[splitDim] % numSplits != 0)
3691 {
3692 throw ParseException("Number of splits must evenly divide the dimension");
3693 }
3694 splitterDimSizes[splitDim] /= numSplits;
3695
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003696 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003697 for (unsigned int j = 0; j < numSplits; ++j)
3698 {
3699 // Set the size of the views.
3700 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3701 {
3702 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3703 }
3704 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3705 }
3706
James Ward58dec6b2020-09-11 17:32:44 +01003707 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003708 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003709 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003710
3711 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003712 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003713
Nina Drozd0324f482019-04-08 10:52:10 +01003714 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3715 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003716 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003717 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003718 }
3719
3720 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3721 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3722}
3723
Derek Lambertif0176992020-04-28 13:37:49 +01003724unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3725{
3726 int numDims = armnn::numeric_cast<int>(numDimsIn);
3727 int v = idx < 0 ? numDims + idx : idx;
3728 ARMNN_ASSERT(v >= 0);
3729 ARMNN_ASSERT(v < numDims);
3730
3731 return static_cast<unsigned int>(v);
3732}
3733
Kevin May7d96b162021-02-03 17:38:41 +00003734void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003735{
3736 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3737
Mike Kelly0d77ae12022-01-07 17:42:27 +00003738 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3739 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003740
3741 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3742 CHECK_VALID_SIZE(inputs.size(), 3);
3743
3744 auto& inputTensor = inputs[0];
3745 auto& splitsTensor = inputs[1];
3746 auto& axisTensor = inputs[2];
3747
3748 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3749 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3750 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3751 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3752
3753 // Inputs
3754 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3755 if (inputDimSize > MaxNumOfTensorDimensions)
3756 {
3757 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003758 fmt::format("The number of dimensions: {} for input tensors of the "
3759 "SplitV op cannot be greater than {} {}",
3760 inputTensorInfo.GetNumDimensions(),
3761 MaxNumOfTensorDimensions,
3762 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003763 }
3764
3765 // Get split axis
3766 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003767 if (axisBufferPtr == nullptr)
3768 {
3769 throw ParseException(
3770 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3771 CHECK_LOCATION().AsString()));
3772 }
3773
Derek Lambertif0176992020-04-28 13:37:49 +01003774 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3775 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003776 int32_t axis = axisData[0];
3777
3778 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3779 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3780 {
3781 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3782 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3783 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3784 throw ParseException(
3785 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3786 axis,
3787 CHECK_LOCATION().AsString()));
3788 }
3789 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003790
Derek Lambertif0176992020-04-28 13:37:49 +01003791 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003792 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003793 unsigned int numSplits{0};
3794
3795 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003796 {
3797 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003798 }
3799 else
3800 {
Ryan OShea86704732020-05-26 11:41:04 +01003801 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003802 }
3803
3804 if (numSplits <=0)
3805 {
3806 throw ParseException("SplitV has invalid number of splits");
3807 }
3808
Jan Eilersc0761e92020-06-29 16:48:44 +01003809 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003810 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003811 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003812
Jan Eilersc0761e92020-06-29 16:48:44 +01003813 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003814 int numInferred{0};
3815 unsigned int inferIdx{0};
3816 int splitSum{0};
3817 for (auto split : splitsData)
3818 {
3819 if (split < 0)
3820 {
3821 numInferred++;
3822 inferIdx = idx;
3823 }
3824 else
3825 {
3826 splitSum += split;
3827 }
3828 idx++;
3829 }
3830 // Check for inferred Axis
3831 if (numInferred == 0)
3832 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003833 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003834 {
3835 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3836 }
3837 }
3838 else if (numInferred == 1)
3839 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003840 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003841 }
3842 else
3843 {
3844 throw ParseException("Cannot infer split size for more than one split");
3845 }
3846
Derek Lambertif0176992020-04-28 13:37:49 +01003847 //Ouput size validation
3848 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3849 CHECK_VALID_SIZE(outputs.size(), numSplits);
3850
3851 // Setup Armnn descriptor
3852 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3853 unsigned int accumSplit = 0;
3854 for (unsigned int j = 0; j < numSplits; ++j)
3855 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003856 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003857
3858 // Set the size of the views.
3859 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3860 {
3861 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3862 if (dimIdx == splitDim)
3863 {
3864 dimSize = splitSize;
3865 }
3866 splitDesc.SetViewSize(j, dimIdx, dimSize);
3867 }
3868
3869 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3870 accumSplit += splitSize;
3871 }
3872
James Ward58dec6b2020-09-11 17:32:44 +01003873 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003874 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003875 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003876
3877 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3878 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3879
3880 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3881 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003882 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003883 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3884 }
3885
3886 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3887 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3888}
3889
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003890void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3891{
3892 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3893}
3894
Kevin May7d96b162021-02-03 17:38:41 +00003895void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003896{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003897 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3898}
3899
3900void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3901{
Inki Daed4619e22020-09-10 15:33:54 +09003902 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3903 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3904 CHECK_VALID_SIZE(inputs.size(), 2);
3905
3906 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3907 CHECK_VALID_SIZE(outputs.size(), 1);
3908
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003909 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3910 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003911 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003912 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003913
3914 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003915 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3916 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3917 {
3918 throw ParseException(
3919 fmt::format(
3920 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3921 CHECK_LOCATION().AsString()));
3922 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003923
3924 // Get const axis value from model and set it to descriptor.
3925 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3926 if (axisBufferPtr == nullptr)
3927 {
3928 throw ParseException(
3929 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3930 CHECK_LOCATION().AsString()));
3931 }
3932
3933 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3934 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3935 int32_t axis = axisData.front();
3936
3937 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3938 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3939 {
3940 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3941 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3942 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3943 throw ParseException(
3944 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3945 axis,
3946 CHECK_LOCATION().AsString()));
3947 }
3948
3949 ArgMinMaxDescriptor desc;
3950 desc.m_Axis = axis;
3951 desc.m_Function = argMinMaxFunction;
3952
3953 // Register a ArgMin/ArgMax layer.
3954 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3955 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3956 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3957 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003958 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3959
3960 // Register input tensor to the layer.
3961 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3962 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3963
3964 // Register output tensor to the layer.
3965 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3966 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3967}
3968
Kevin May7d96b162021-02-03 17:38:41 +00003969void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003970{
3971 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3972
Kevin May7d96b162021-02-03 17:38:41 +00003973 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003974 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003975 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003976 CHECK_VALID_SIZE(outputs.size(), 1);
3977
3978 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3979 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3980 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3981
3982 armnn::GatherDescriptor gatherDescriptor;
3983
Mike Kelly0d77ae12022-01-07 17:42:27 +00003984 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3985 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00003986 auto axis = options->axis;
3987
3988 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3989 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3990 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3991 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3992 {
3993 throw ParseException(
3994 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3995 axis,
3996 inputDimensions, inputDimensions,
3997 CHECK_LOCATION().AsString()));
3998 }
3999 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4000 {
4001 throw ParseException(
4002 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4003 outputDimensions,
4004 inputDimensions, indicesDimensions,
4005 CHECK_LOCATION().AsString()));
4006 }
4007
4008 gatherDescriptor.m_Axis = axis;
4009
4010 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4011 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4012 ARMNN_ASSERT(layer != nullptr);
4013 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4014
4015 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4016 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4017
4018 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4019 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4020}
4021
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004022void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4023{
4024 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4025
4026 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4027 CHECK_VALID_SIZE(inputs.size(), 2);
4028 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4029 CHECK_VALID_SIZE(outputs.size(), 1);
4030
4031 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4032 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
4033 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4034
4035 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4036 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4037 ARMNN_ASSERT(layer != nullptr);
4038 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4039
4040 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4041 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4042
4043 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4044 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4045}
4046
Kevin May7d96b162021-02-03 17:38:41 +00004047void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004048{
4049 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4050
Kevin May7d96b162021-02-03 17:38:41 +00004051 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004052 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004053 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004054 CHECK_VALID_SIZE(outputs.size(), 1);
4055
4056 armnn::DepthToSpaceDescriptor descriptor;
4057
Mike Kelly0d77ae12022-01-07 17:42:27 +00004058 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4059 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004060 auto blockSize = options->block_size;
4061 if (blockSize < 2)
4062 {
4063 throw ParseException(
4064 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4065 blockSize,
4066 CHECK_LOCATION().AsString()));
4067 }
4068 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4069
4070 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4071 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4072 ARMNN_ASSERT(layer != nullptr);
4073 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4074 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4075
4076 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4077 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4078
4079 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4080 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4081}
4082
Kevin May7d96b162021-02-03 17:38:41 +00004083void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004084{
Sadik Armagana2747482021-02-09 10:28:54 +00004085 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4086}
4087
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004088void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4089{
4090 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4091}
4092
Sadik Armagana2747482021-02-09 10:28:54 +00004093void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4094{
4095 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4096}
4097
4098void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4099{
4100 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4101}
4102
4103void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4104{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004105 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4106
Mike Kelly0d77ae12022-01-07 17:42:27 +00004107 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4108 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004109
4110 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4111 CHECK_VALID_SIZE(inputs.size(), 2);
4112
4113 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4114 CHECK_VALID_SIZE(outputs.size(), 1);
4115
Sadik Armagana2747482021-02-09 10:28:54 +00004116 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004117
4118 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4119 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004120
4121 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004122 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4123 // Get const axis value from model and set it to descriptor.
4124 if (axisBufferPtr != nullptr)
4125 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004126 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4127 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4128
4129 // Convert the axis to unsigned int and remove duplicates.
4130 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4131 std::set<unsigned int> uniqueAxis;
4132 std::transform(axisData.begin(),
4133 axisData.end(),
4134 std::inserter(uniqueAxis, uniqueAxis.begin()),
4135 [rank](int i)->unsigned int{
4136 return static_cast<uint32_t>(((i + rank) % rank)); });
4137 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004138 }
Sadik Armagana2747482021-02-09 10:28:54 +00004139 else
4140 {
4141 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4142 {
4143 desc.m_vAxis.push_back(i);
4144 }
4145 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004146
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004147 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004148 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004149
4150 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004151 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004152
4153 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4154 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4155
4156 // Register input tensor to the layer.
4157 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4158 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4159
4160 // Register output tensor to the layer.
4161 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4162 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4163}
4164
Mike Kelly31dce2b2021-09-01 21:22:37 +01004165void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4166{
4167 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4168
4169 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4170 CHECK_VALID_SIZE(inputs.size(), 1);
4171
4172 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4173 CHECK_VALID_SIZE(outputs.size(), 1);
4174
4175 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4176 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4177
4178 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4179
4180 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4181 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4182
4183 armnn::NormalizationDescriptor descriptor;
4184 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4185 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4186 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4187 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4188 descriptor.m_K = options->bias;
4189 descriptor.m_Alpha = options->alpha;
4190 descriptor.m_Beta = options->beta;
4191
4192 // ArmNN expects normSize to be the full size of the normalization
4193 // window rather than the radius as in TfLite.
4194 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4195
4196 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4197 ARMNN_ASSERT(layer != nullptr);
4198
4199 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4200 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4201
4202 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4203 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4204
4205 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4206 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4207}
4208
Teresa Charlin8b0bee12022-07-12 11:18:44 +01004209void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4210{
4211 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4212}
4213
4214void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4215{
4216 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4217}
4218
4219void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
4220{
4221 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
4222}
4223
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004224void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4225{
4226 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4227}
4228
4229void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4230{
4231 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4232}
4233
4234void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4235{
4236 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4237}
4238
Teresa Charlin8b0bee12022-07-12 11:18:44 +01004239void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
4240{
4241 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
4242}
4243
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004244void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4245{
4246 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4247}
4248
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004249void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4250{
4251 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4252
4253 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4254 CHECK_VALID_SIZE(inputs.size(), 1);
4255
4256 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4257 CHECK_VALID_SIZE(outputs.size(), 1);
4258
4259 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4260 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4261
4262 ElementwiseUnaryDescriptor desc;
4263 desc.m_Operation = unaryOperation;
4264 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4265 ARMNN_ASSERT(layer != nullptr);
4266
4267 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4268 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4269
4270 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4271 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4272
4273 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4274 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4275}
4276
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004277void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4278{
4279 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4280}
4281
4282void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4283{
4284 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4285}
4286
4287void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4288{
4289 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4290}
4291
4292void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4293{
4294 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4295}
4296
4297void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4298{
4299 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4300}
4301
4302void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4303{
4304 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4305}
4306
4307void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4308 ComparisonOperation comparisonOperation)
4309{
4310 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4311
4312 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4313 CHECK_VALID_SIZE(inputs.size(), 2);
4314
4315 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4316 CHECK_VALID_SIZE(outputs.size(), 1);
4317
4318 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4319 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4320
4321 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4322 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4323 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4324
4325 ComparisonDescriptor desc;
4326 desc.m_Operation = comparisonOperation;
4327 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4328 ARMNN_ASSERT(layer != nullptr);
4329
4330 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4331 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4332
4333 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4334 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4335
4336 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4337 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4338}
4339
Kevin May7d96b162021-02-03 17:38:41 +00004340armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4341 unsigned int outputSlot,
4342 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004343{
4344 ActivationDescriptor activationDesc;
4345 std::string layerName = prevLayer->GetName();
4346
4347 switch(activationType)
4348 {
4349 case tflite::ActivationFunctionType_NONE:
4350 {
4351 // this is a no-op: return previous layer
4352 return prevLayer;
4353 }
4354 case tflite::ActivationFunctionType_RELU:
4355 {
4356 activationDesc.m_Function = ActivationFunction::ReLu;
4357 layerName += ":RELU";
4358 break;
4359 }
4360 case tflite::ActivationFunctionType_RELU6:
4361 {
4362 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4363 activationDesc.m_A = 6.0f;
4364 activationDesc.m_B = 0.0f;
4365 layerName += ":RELU6";
4366 break;
4367 }
4368 case tflite::ActivationFunctionType_TANH:
4369 {
4370 activationDesc.m_Function = ActivationFunction::TanH;
4371 activationDesc.m_A = 1.0f;
4372 activationDesc.m_B = 1.0f;
4373 layerName += ":TANH";
4374 break;
4375 }
4376
4377 // I only put these here as a reminder what others we could support
4378 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4379 case tflite::ActivationFunctionType_SIGN_BIT:
4380 default:
4381 {
4382 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004383 fmt::format("TfLite parser doesn't suppport fused activation: "
4384 "{}/{} {} ",
4385 activationType,
4386 tflite::EnumNameActivationFunctionType(activationType),
4387 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004388
4389 }
4390 }
4391
4392 IConnectableLayer* activationLayer =
4393 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4394
4395 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4396 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4397 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4398 return activationLayer;
4399}
4400
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004401armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4402 unsigned int outputSlot)
4403{
Teresa Charlin725728e2022-05-05 13:33:33 +01004404
4405 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4406 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4407
4408 if (dataType == DataType::Signed32)
4409 {
4410 return prevLayer;
4411 }
4412
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004413 std::string layerName = prevLayer->GetName();
4414 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4415
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004416 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4417 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004418
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004419 return floorLayer;
4420}
4421
Mike Kelly0d77ae12022-01-07 17:42:27 +00004422TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004423{
4424 if (fileName == nullptr)
4425 {
James Ward58dec6b2020-09-11 17:32:44 +01004426 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004427 CHECK_LOCATION().AsString()));
4428 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004429 std::error_code errorCode;
4430 fs::path pathToFile(fileName);
4431 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004432 {
James Ward58dec6b2020-09-11 17:32:44 +01004433 //fmt::format() could not be used here (format error)
4434 std::stringstream msg;
4435 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4436 << " " << CHECK_LOCATION().AsString();
4437
4438 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004439 }
4440 std::ifstream file(fileName, std::ios::binary);
4441 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4442 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4443 fileContent.size());
4444}
4445
Mike Kelly0d77ae12022-01-07 17:42:27 +00004446TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004447{
4448 if (binaryContent == nullptr)
4449 {
James Ward58dec6b2020-09-11 17:32:44 +01004450 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004451 CHECK_LOCATION().AsString()));
4452 }
4453 flatbuffers::Verifier verifier(binaryContent, len);
4454 if (verifier.VerifyBuffer<tflite::Model>() == false)
4455 {
4456 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004457 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4458 "flatbuffers format. size:{} {}",
4459 len,
4460 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004461 }
4462 return tflite::UnPackModel(binaryContent);
4463}
4464
Mike Kelly0d77ae12022-01-07 17:42:27 +00004465TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004466 size_t subgraphIndex,
4467 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004468{
4469 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4470
Mike Kelly0d77ae12022-01-07 17:42:27 +00004471 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4472 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004473
4474 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004475 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004476 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004477 {
mathad01c21025d2021-04-26 10:09:37 +01004478 // If the input location is -1 then assume input is turned off.
4479 if (operatorPtr->inputs[i] == -1)
4480 {
4481 continue;
4482 }
4483 else
4484 {
4485 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4486 result.push_back(subgraphPtr->tensors[inputId].get());
4487 }
telsoa01c577f2c2018-08-31 09:22:23 +01004488 }
4489 return result;
4490}
4491
Mike Kelly0d77ae12022-01-07 17:42:27 +00004492TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004493 size_t subgraphIndex,
4494 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004495{
4496 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4497
Mike Kelly0d77ae12022-01-07 17:42:27 +00004498 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4499 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004500
4501 size_t outputCount = operatorPtr->outputs.size();
4502 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004503 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004504 {
4505 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4506 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004507 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004508 }
4509 return result;
4510}
4511
Mike Kelly0d77ae12022-01-07 17:42:27 +00004512TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004513 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004514{
4515 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004516 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004517
Derek Lambertiff05cc52019-04-26 13:05:17 +01004518 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004519 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004520 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004521 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004522 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004523 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004524 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004525 }
4526 return result;
4527}
4528
Mike Kelly0d77ae12022-01-07 17:42:27 +00004529TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004530 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004531{
4532 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004533 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004534
Derek Lambertiff05cc52019-04-26 13:05:17 +01004535 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004536 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004537 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004538 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004539 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4540 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004541 }
4542 return result;
4543}
4544
Kevin May7d96b162021-02-03 17:38:41 +00004545std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4546 size_t subgraphIndex,
4547 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004548{
4549 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004550 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4551 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004552 return operatorPtr->inputs;
4553}
4554
Kevin May7d96b162021-02-03 17:38:41 +00004555std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4556 size_t subgraphIndex,
4557 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004558{
4559 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004560 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4561 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004562 return operatorPtr->outputs;
4563}
4564
Kevin May7d96b162021-02-03 17:38:41 +00004565void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4566 size_t operatorIndex,
4567 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004568 const std::vector<unsigned int>& tensorIndexes,
4569 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004570{
4571 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004572 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004573
Finn Williamsd4fa5452021-03-01 12:31:41 +00004574 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004575 {
4576 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004577 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4578 " for subgraph:{} operator index:{} {}",
4579 tensorIndexes.size(),
4580 layer->GetNumInputSlots(),
4581 subgraphIndex,
4582 operatorIndex,
4583 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004584 }
4585
Finn Williamsd4fa5452021-03-01 12:31:41 +00004586 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004587 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004588 unsigned int tensorIndex = tensorIndexes[index];
4589 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004590 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4591 }
4592}
4593
Kevin May7d96b162021-02-03 17:38:41 +00004594void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4595 size_t operatorIndex,
4596 IConnectableLayer* layer,
4597 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004598{
4599 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004600 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004601 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4602 {
4603 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004604 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4605 " for subgraph:{} operator index:{} {}",
4606 tensorIndexes.size(),
4607 layer->GetNumOutputSlots(),
4608 subgraphIndex,
4609 operatorIndex,
4610 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004611 }
4612
4613 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4614 {
4615 unsigned int tensorIndex = tensorIndexes[slotIndex];
4616 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4617 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4618 }
4619}
4620
Kevin May7d96b162021-02-03 17:38:41 +00004621void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004622{
4623 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4624
4625 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004626 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004627 {
4628 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4629 IConnectableLayer* layer =
4630 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4631
4632 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4633 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4634
4635 RegisterOutputSlots(subgraphIndex,
4636 VIRTUAL_OPERATOR_ID,
4637 layer,
4638 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4639 }
4640}
4641
Kevin May7d96b162021-02-03 17:38:41 +00004642void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004643{
4644 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4645
4646 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004647 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004648 {
4649 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4650 IConnectableLayer* layer =
4651 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4652
4653 RegisterInputSlots(subgraphIndex,
4654 VIRTUAL_OPERATOR_ID,
4655 layer,
4656 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4657 }
4658}
4659
Mike Kelly5880b912022-01-28 16:18:54 +00004660void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004661{
Mike Kelly5880b912022-01-28 16:18:54 +00004662 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004663
Mike Kelly5880b912022-01-28 16:18:54 +00004664 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004665 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4666 {
4667 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4668 {
4669 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4670 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4671 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004672 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004673
Mike Kelly5880b912022-01-28 16:18:54 +00004674 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004675 {
4676 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004677 armnn::DataType dataType = tensorInfo.GetDataType();
4678
4679 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4680 != m_ConstantsToDequantize.end())
4681 {
4682 dataType = DataType::Float32;
4683 }
4684 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4685
4686 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4687 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4688
4689 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4690 RegisterOutputSlots(subgraphIndex,
4691 VIRTUAL_OPERATOR_ID,
4692 layer,
4693 { tensorIndex });
4694 }
4695 else if (ShouldConstantTensorBeCreated(tensorIndex))
4696 {
4697 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4698 armnn::DataType dataType = tensorInfo.GetDataType();
4699
4700 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4701 != m_ConstantsToDequantize.end())
4702 {
4703 dataType = DataType::Float32;
4704 }
4705 // Make sure isConstant flag is set.
4706 tensorInfo.SetConstant();
4707 tensorInfo.SetDataType(dataType);
4708
4709 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004710
Matthew Sloyan81beae32021-07-13 19:46:11 +01004711 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004712 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004713
Matthew Sloyan81beae32021-07-13 19:46:11 +01004714 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4715 RegisterOutputSlots(subgraphIndex,
4716 VIRTUAL_OPERATOR_ID,
4717 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004718 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004719 }
4720 else
4721 {
4722 throw ParseException(
4723 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4724 CHECK_LOCATION().AsString()));
4725 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004726 }
4727 }
4728 }
4729}
4730
telsoa01c577f2c2018-08-31 09:22:23 +01004731// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004732TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004733{
4734 CHECK_BUFFER(model, bufferIndex);
4735 return model->buffers[bufferIndex].get();
4736}
4737
Matteo Martincigh747ef822018-12-18 09:26:39 +00004738template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004739std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4740TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4741 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004742 armnn::TensorInfo& tensorInfo,
4743 armnn::Optional<armnn::PermutationVector&> permutationVector)
4744{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004745 // Make sure isConstant flag is set.
4746 tensorInfo.SetConstant();
4747
Matteo Martincigh747ef822018-12-18 09:26:39 +00004748 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4749 tensorPtr,
4750 tensorInfo,
4751 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004752 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004753 return std::make_pair(constData.first, std::move(storage));
4754}
4755
Mike Kelly5880b912022-01-28 16:18:54 +00004756bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4757{
4758 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4759 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4760 != m_ConstantsToBeCreated.end());
4761}
4762
Finn Williamsd4fa5452021-03-01 12:31:41 +00004763bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4764{
4765 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004766 bool isConst = true;
4767
4768 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4769 if (buffer->data.size() == 0)
4770 {
4771 isConst = false;
4772 }
4773
4774 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004775}
4776
Kevin May7d96b162021-02-03 17:38:41 +00004777std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004778TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4779 armnn::TensorInfo& tensorInfo,
4780 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004781{
4782 CHECK_TENSOR_PTR(tensorPtr);
4783 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4784 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4785
Matthew Sloyan81beae32021-07-13 19:46:11 +01004786 // Make sure isConstant flag is set.
4787 tensorInfo.SetConstant();
4788
telsoa01c577f2c2018-08-31 09:22:23 +01004789 switch (tensorInfo.GetDataType())
4790 {
4791 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004792 return CreateConstTensorAndStoreData<float>(bufferPtr,
4793 tensorPtr,
4794 tensorInfo,
4795 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004796 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004797 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4798 tensorPtr,
4799 tensorInfo,
4800 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004801 case armnn::DataType::QSymmS8:
4802 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4803 tensorPtr,
4804 tensorInfo,
4805 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004806 case armnn::DataType::QAsymmS8:
4807 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4808 tensorPtr,
4809 tensorInfo,
4810 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004811 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004812 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4813 tensorPtr,
4814 tensorInfo,
4815 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004816 default:
4817 {
4818 std::stringstream errString;
4819 errString << "Unexpected datatype when creating const tensor: "
4820 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4821 << " shape:" << tensorInfo.GetShape()
4822 << CHECK_LOCATION().AsString();
4823 throw ParseException(errString.str());
4824 }
4825 }
4826}
4827
Finn Williamsd4fa5452021-03-01 12:31:41 +00004828armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4829 armnn::TensorInfo& tensorInfo)
4830{
4831 CHECK_TENSOR_PTR(tensorPtr);
4832 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4833 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4834
Matthew Sloyan81beae32021-07-13 19:46:11 +01004835 // Make sure isConstant flag is set.
4836 tensorInfo.SetConstant();
4837
Finn Williamsd4fa5452021-03-01 12:31:41 +00004838 return ConstTensor(tensorInfo, bufferPtr->data.data());
4839}
4840
Mike Kelly5880b912022-01-28 16:18:54 +00004841std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4842TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4843 armnn::TensorInfo& tensorInfo,
4844 armnn::DataType inputDataType)
4845{
4846 CHECK_TENSOR_PTR(tensorPtr);
4847 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4848 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4849
4850 // Make sure isConstant flag is set.
4851 tensorInfo.SetConstant();
4852
4853 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4854 {
4855 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4856 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4857 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4858 }
4859 else
4860 {
4861 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4862 }
4863}
4864
4865std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4866TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4867{
4868 CHECK_TENSOR_PTR(tensorPtr);
4869 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4870 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4871 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4872
4873 // Make sure isConstant flag is set.
4874 tensorInfo.SetConstant();
4875
4876 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4877 {
4878 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4879 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4880 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4881 }
4882 else
4883 {
4884 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4885 }
4886}
4887
Kevin May7d96b162021-02-03 17:38:41 +00004888BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4889 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004890{
4891 CHECK_SUBGRAPH(m_Model, subgraphId);
4892 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004893 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004894 {
4895 if (input.second->name == name)
4896 {
4897 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004898 auto inputTensorInfo = ToTensorInfo(input.second);
4899 // Input tensors are always treated as constant tensors during network execution.
4900 inputTensorInfo.SetConstant(true);
4901 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004902 }
4903 }
4904
4905 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004906 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004907 {
4908 bindings << "'" << input.second->name << "' ";
4909 }
4910
4911 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004912 fmt::format("No input binding found for subgraph:{} and name:{}. "
4913 "Possible inputs are: [{}] {}",
4914 subgraphId,
4915 name,
4916 bindings.str(),
4917 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004918}
4919
Kevin May7d96b162021-02-03 17:38:41 +00004920BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4921 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004922{
4923 CHECK_SUBGRAPH(m_Model, subgraphId);
4924 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004925 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004926 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004927 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004928 if (output.second->name == name)
4929 {
4930 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004931 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4932 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4933 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004934 }
4935 }
4936
4937 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004938 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004939 {
4940 bindings << "'" << output.second->name << "' ";
4941 }
4942
4943 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004944 fmt::format("No output binding found for subgraph:{} and name:{}. "
4945 "Possible outputs are: [{}] {}",
4946 subgraphId,
4947 name,
4948 bindings.str(),
4949 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004950}
4951
Kevin May7d96b162021-02-03 17:38:41 +00004952size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004953{
4954 return m_Model->subgraphs.size();
4955}
4956
Kevin May7d96b162021-02-03 17:38:41 +00004957std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004958{
4959 CHECK_SUBGRAPH(m_Model, subgraphId);
4960 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4961 std::vector<std::string> result;
4962 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004963 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004964 {
4965 result.push_back(input.second->name);
4966 }
4967 return result;
4968}
4969
Kevin May7d96b162021-02-03 17:38:41 +00004970std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004971{
4972 CHECK_SUBGRAPH(m_Model, subgraphId);
4973 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4974 std::vector<std::string> result;
4975 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004976 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004977 {
4978 result.push_back(output.second->name);
4979 }
4980 return result;
4981}
4982
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004983const std::string TfLiteParserImpl::GetVersion()
4984{
4985 return TFLITE_PARSER_VERSION;
4986}
4987
Mike Kelly0d77ae12022-01-07 17:42:27 +00004988TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004989: m_FloatData(std::move(data))
4990, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004991, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004992, m_Int32Data(nullptr)
4993{
4994}
4995
Mike Kelly0d77ae12022-01-07 17:42:27 +00004996TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004997: m_FloatData(nullptr)
4998, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004999, m_Int8Data(nullptr)
5000, m_Int32Data(nullptr)
5001{
5002}
5003
Mike Kelly0d77ae12022-01-07 17:42:27 +00005004TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00005005: m_FloatData(nullptr)
5006, m_Uint8Data(nullptr)
5007, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01005008, m_Int32Data(nullptr)
5009{
5010}
5011
Mike Kelly0d77ae12022-01-07 17:42:27 +00005012TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005013: m_FloatData(nullptr)
5014, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005015, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005016, m_Int32Data(std::move(data))
5017{
5018}
5019
5020} // armnnTfLiteParser