blob: 030420345ee918a0d948852a9e94f2ac75476545 [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;
Samuel Yape7cd8f92022-08-24 17:04:34 +0100683 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
mathad01b392e982021-04-07 12:07:30 +0100684 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000685 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
686 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100687 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbette126be92022-05-25 11:21:11 +0100688 #if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100689 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100690 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000691 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
692 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
693 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
694 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100695 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000696 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300697 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000698 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100699 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000700 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000701 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
702 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin91a53ea2022-04-25 15:47:29 +0100703 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300704 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
705 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000706 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
707 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300708 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
709 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Mike Kelly31dce2b2021-09-01 21:22:37 +0100710 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
711 = &TfLiteParserImpl::ParseLocalResponseNormalization;
Teresa Charlin8b0bee12022-07-12 11:18:44 +0100712 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100713 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000714 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
Teresa Charlin455172a2022-06-29 15:35:57 +0100715 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
Kevin May7d96b162021-02-03 17:38:41 +0000716 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
717 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
718 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
719 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
720 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100721 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000722 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
723 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300724 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000725 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
726 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000727 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100728 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000729 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
730 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
731 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000732 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
733 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100734 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000735 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
736 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
737 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100738 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100739 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100740 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Teresa Charlin8b0bee12022-07-12 11:18:44 +0100741 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
Kevin May7d96b162021-02-03 17:38:41 +0000742 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
743 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
744 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
745 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
746 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
747 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
748 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
749 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
750 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
751 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
752 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
753 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000754 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
755 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000756 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100757
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100758 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000759 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100760}
761
Kevin May7d96b162021-02-03 17:38:41 +0000762void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100763{
764 m_Network = armnn::INetworkPtr(nullptr, nullptr);
765 m_Model = nullptr;
766 m_SubgraphConnections.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000767 m_OverridenOutputShapes.clear();
768 m_ConstantsToDequantize.clear();
769 m_ConstantsToBeCreated.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100770}
771
Kevin May7d96b162021-02-03 17:38:41 +0000772INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100773{
774 ResetParser();
775 m_Model = LoadModelFromFile(graphFile);
776 return CreateNetworkFromModel();
777}
778
Mike Kelly0d77ae12022-01-07 17:42:27 +0000779INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100780{
781 ResetParser();
782 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
783 return CreateNetworkFromModel();
784}
785
Finn Williamsb49ed182021-06-29 15:50:08 +0100786
787armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
788{
789 ResetParser();
790 m_Model = std::move(model);
791
792 return CreateNetworkFromModel();
793}
794
Kevin May7d96b162021-02-03 17:38:41 +0000795INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100796{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100797
798 using NetworkOptions = std::vector<BackendOptions>;
799 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100800 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100801 {
Mike Kelly80512b02022-05-16 23:10:42 +0100802 if (m_Options.value().m_InferAndValidate)
803 {
804 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
805 {
806 { "InferAndValidate", true }
807 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100808
Mike Kelly80512b02022-05-16 23:10:42 +0100809 networkOptions.push_back(shapeInferenceMethodOption);
810 }
811 if (m_Options.value().m_AllowExpandedDims)
812 {
813 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
814 {
815 { "AllowExpandedDims", true }
816 });
817
818 networkOptions.push_back(shapeInferenceMethodOption);
819 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100820 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100821 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100822 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100823
telsoa01c577f2c2018-08-31 09:22:23 +0100824 if (m_Model->subgraphs.size() != 1)
825 {
826 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100827 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
828 m_Model->subgraphs.size(),
829 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100830 }
831
832 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100833 size_t operatorIndex = 0;
834 try
telsoa01c577f2c2018-08-31 09:22:23 +0100835 {
Colm Donelan6350d272020-06-09 16:56:25 +0100836 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100837 {
Colm Donelan6350d272020-06-09 16:56:25 +0100838 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
839 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100840 {
Colm Donelan6350d272020-06-09 16:56:25 +0100841 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100842
843// 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 +0100844#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100845 auto builtinCode = std::max(opCodePtr->builtin_code,
846 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
847#else
telsoa01c577f2c2018-08-31 09:22:23 +0100848 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100849#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100850
851 if (builtinCode > tflite::BuiltinOperator_MAX)
852 {
James Ward58dec6b2020-09-11 17:32:44 +0100853 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
854 "subgraph:{} operator idx:{}. {}",
855 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
856 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100857 }
858
859 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100860 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100861 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100862 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100863 }
telsoa01c577f2c2018-08-31 09:22:23 +0100864
Colm Donelan6350d272020-06-09 16:56:25 +0100865 SetupInputLayers(subgraphIndex);
866 SetupOutputLayers(subgraphIndex);
867 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100868
Colm Donelan6350d272020-06-09 16:56:25 +0100869 ++subgraphIndex;
870 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100871 }
telsoa01c577f2c2018-08-31 09:22:23 +0100872 }
Colm Donelan6350d272020-06-09 16:56:25 +0100873 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100874 {
Colm Donelan6350d272020-06-09 16:56:25 +0100875 std::stringstream errorString;
876 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
877 << subgraphIndex << " error: " << e.what();
878 ARMNN_LOG(error) << errorString.str();
879 std::stringstream errors;
880 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100881 throw ParseException(errors.str());
882 }
883
884 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100885 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100886 {
887 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
888 {
889 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
890 {
891 for (size_t inputSlotIdx = 0;
892 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
893 ++inputSlotIdx)
894 {
895 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
896 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
897 }
898 }
899 }
900 }
telsoa01c577f2c2018-08-31 09:22:23 +0100901 return std::move(m_Network);
902}
903
Mike Kelly5880b912022-01-28 16:18:54 +0000904std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
905 const TensorInfo& tensorInfo)
906{
907 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
908 tensorInfo.GetDataType() == DataType::QAsymmU8)
909 {
910 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
911
912 if (tensorInfo.HasPerAxisQuantization())
913 {
914 unsigned int axis = tensorInfo.GetQuantizationDim().value();
915 auto axisDimensionality = tensorInfo.GetShape()[axis];
916 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
917
918 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
919 {
920 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
921 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
922 tensorInfo.GetQuantizationOffset());
923 }
924 }
925 else
926 {
927 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
928 {
929 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
930 tensorInfo.GetQuantizationOffset());
931 }
932 }
933 return buffer;
934 }
935 throw ParseException(
936 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
937 GetDataTypeName(DataType::Float32),
938 GetDataTypeName(tensorInfo.GetDataType()),
939 CHECK_LOCATION().AsString()));
940}
941
Kevin May7d96b162021-02-03 17:38:41 +0000942void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
943 size_t tensorIndex,
944 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100945{
946 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100947 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
948 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100949
950 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
951
Nikhil Raj2803c8d2022-08-03 18:20:59 +0100952 if (slot->GetOwningIConnectableLayer().GetType() != LayerType::Constant)
telsoa01c577f2c2018-08-31 09:22:23 +0100953 {
telsoa01c577f2c2018-08-31 09:22:23 +0100954
Nikhil Raj2803c8d2022-08-03 18:20:59 +0100955 // assuming there is only one producer for that tensor
956 if (tensorSlots.outputSlot != nullptr)
957 {
958 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
959 "subgraph:{} tensor:{} {}",
960 subgraphIndex,
961 tensorIndex,
962 CHECK_LOCATION().AsString()));
963 }
964 }
telsoa01c577f2c2018-08-31 09:22:23 +0100965 tensorSlots.outputSlot = slot;
966}
967
Kevin May7d96b162021-02-03 17:38:41 +0000968void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
969 size_t tensorIndex,
970 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100971{
972 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100973 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
974 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100975
Finn Williamsd4fa5452021-03-01 12:31:41 +0000976 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100977 tensorSlots.inputSlots.push_back(slot);
978}
979
Kevin May7d96b162021-02-03 17:38:41 +0000980void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100981{
982 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
983
984 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000985 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100986
987 // Identify custom code defined for custom operator
988 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
989 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
990
991 // Find parser function that correspondes to custom code (if any)
992 auto iterator = m_CustomParserFunctions.find(customCode);
993 if (iterator != m_CustomParserFunctions.end())
994 {
995 customParserFunction = iterator->second;
996 }
997
998 // Run parser function
999 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1000}
1001
Kevin May7d96b162021-02-03 17:38:41 +00001002void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001003{
1004 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001005
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001006 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1007
1008 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001009
1010// 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 +01001011#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001012 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1013 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1014#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001015 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001016#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001017
1018 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1019 {
1020 // Do not add StandInLayer, throw ParseException instead
1021 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001022 fmt::format("Operator not supported. "
1023 "subgraph:{} operator:{} "
1024 "opcode_index:{} opcode:{} / {} {}",
1025 subgraphIndex,
1026 operatorIndex,
1027 opcodeIndex,
1028 opcode,
1029 tflite::EnumNameBuiltinOperator(opcode),
1030 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001031 }
1032
1033 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1034 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1035
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001036 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1037 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001038
1039 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001040 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001041
1042 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1043 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001044 ARMNN_ASSERT(layer != nullptr);
1045
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001046 for (unsigned int i = 0u; i < numOutputs; ++i)
1047 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001048 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001049 }
1050
1051 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1052 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1053
1054 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1055 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001056}
1057
mathad01b392e982021-04-07 12:07:30 +01001058void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1059{
1060 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1061
1062 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1063 CHECK_VALID_SIZE(inputs.size(), 1);
1064 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1065 CHECK_VALID_SIZE(outputs.size(), 1);
1066
1067 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1068
1069 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1070 ARMNN_ASSERT(layer != nullptr);
1071
1072 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1073 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1074
1075 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1076 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1077
1078 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1079 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1080}
1081
Kevin May7d96b162021-02-03 17:38:41 +00001082void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001083{
1084 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1085
Mike Kelly0d77ae12022-01-07 17:42:27 +00001086 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1087 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001088
1089 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1090
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001091 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1092 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1093 CHECK_VALID_SIZE(outputs.size(), 1);
1094
telsoa01c577f2c2018-08-31 09:22:23 +01001095 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001096 inputs.size() == 3 ?
1097 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001098 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1099 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001100 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001101 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1102 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001103
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001104 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
telsoa01c577f2c2018-08-31 09:22:23 +01001105 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1106
1107 // assuming input is NHWC
1108 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001109 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001110
1111 // assuming the filter is OHWI : Output, H, W, Input
1112 // which is essentially the same as NHWC
1113 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001114 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001115
Pablo Tellof0bd6832019-04-26 17:58:13 +01001116 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1117 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1118 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1119 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001120
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001121 // Add the first input and weights tensor to the registration list.
1122 // The constant weights will be added by SetupConstantLayers.
1123 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1124 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001125
James Ward58dec6b2020-09-11 17:32:44 +01001126 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001127 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001128
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001129 if (IsConstTensor(inputs[1]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1130 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1131 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
telsoa01c577f2c2018-08-31 09:22:23 +01001132 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001133 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001134 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001135
1136 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001137 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001138 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1139
1140 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1141 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1142
1143 if (IsConstTensor(inputs[2]) && inputTensorInfo.GetDataType() == DataType::Float32 &&
1144 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
1145 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
1146 {
1147 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1148 }
telsoa01c577f2c2018-08-31 09:22:23 +01001149 }
1150
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001151 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001152
Sadik Armagand109a4d2020-07-28 10:42:13 +01001153 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001154 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001155
1156 // register the input connection slots for the layer, connections are made after all layers have been created
1157 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001158 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001159
jimfly01c25411c2018-11-14 17:47:22 +00001160 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001161 // register the output connection slots for the layer, connections are made after all layers have been created
1162 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001163 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001164}
1165
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001166// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbette126be92022-05-25 11:21:11 +01001167#if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001168void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1169{
1170 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1171
1172 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1173 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1174
1175 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1176
1177 Convolution3dDescriptor desc;
1178 desc.m_BiasEnabled = false;
1179 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1180 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1181 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1182 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1183 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1184 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1185 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1186
1187 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1188 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1189
1190 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1191 CHECK_VALID_SIZE(outputs.size(), 1);
1192
1193 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1194 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1195
1196 // Assuming input is NDHWC
1197 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1198 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1199 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1200
1201 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1202 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1203 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1204 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1205
1206 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001207 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001208 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1209 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1210 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1211 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1212
Mike Kelly5880b912022-01-28 16:18:54 +00001213 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001214
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001215 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1216
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001217 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1218 // Add the first input and weights tensor to the registration list.
1219 // The constant weights will be added by SetupConstantLayers.
1220 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1221
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001222 if (inputs.size() == 3)
1223 {
1224 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001225
1226 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1227 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001228 }
1229
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001230 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001231 ARMNN_ASSERT(layer != nullptr);
1232
1233 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1234 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1235
1236 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001237 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001238
1239 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1240 // Register the output connection slots for the layer, connections are made after all layers have been created
1241 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1242 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1243}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001244#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001245
Kevin May7d96b162021-02-03 17:38:41 +00001246void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001247{
1248 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1249
Mike Kelly0d77ae12022-01-07 17:42:27 +00001250 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1251 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001252
1253 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1254
1255 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001256 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1257 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001258 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001259 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001260
1261 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1262 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001263 if (inputs.size() == 3)
1264 {
1265 desc.m_BiasEnabled = true;
1266 }
1267
telsoa01c577f2c2018-08-31 09:22:23 +01001268 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1269 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001270 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1271 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001272
telsoa01c577f2c2018-08-31 09:22:23 +01001273 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001274 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001275
Matteo Martincigh747ef822018-12-18 09:26:39 +00001276 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001277 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1278 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001279
1280 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001281 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1282 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1283
Pablo Tellof0bd6832019-04-26 17:58:13 +01001284 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1285 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1286 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1287 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001288
Jan Eilers53ef7952021-06-02 12:01:25 +01001289 // 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 +01001290 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001291
Cathal Corbett06902652022-04-14 17:55:11 +01001292 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1293 // Add the first input and weights tensor to the registration list.
1294 // The constant weights will be added by SetupConstantLayers.
1295 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1296
1297 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1298
1299 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001300 {
1301 desc.m_BiasEnabled = true;
1302 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001303
1304 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1305 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001306 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001307 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001308
Sadik Armagand109a4d2020-07-28 10:42:13 +01001309 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001310 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001311
1312 // register the input connection slots for the layer, connections are made after all layers have been created
1313 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001314 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001315
jimfly01c25411c2018-11-14 17:47:22 +00001316 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001317 // register the output connection slots for the layer, connections are made after all layers have been created
1318 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1319 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1320}
1321
Kevin May7d96b162021-02-03 17:38:41 +00001322void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001323{
1324 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1325
1326 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1327 CHECK_VALID_SIZE(inputs.size(), 1);
1328
1329 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1330 CHECK_VALID_SIZE(outputs.size(), 1);
1331
James Ward58dec6b2020-09-11 17:32:44 +01001332 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001333
1334 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001335 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001336
Sadik Armagand109a4d2020-07-28 10:42:13 +01001337 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001338 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1339
1340 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1341 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1342
1343 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1344 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1345}
1346
Teresa Charlin3ab85482021-06-08 16:59:29 +01001347void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1348{
1349 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1350
1351 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1352 CHECK_VALID_SIZE(inputs.size(), 2);
1353
1354 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1355 CHECK_VALID_SIZE(outputs.size(), 1);
1356
1357 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1358
1359 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1360 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1361
1362 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1363
1364 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001365
1366 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1367 {
1368 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1369 }
1370 else
1371 {
1372 int32_t axis = inputs[1]->shape[0];
1373
1374 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1375
1376 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1377 {
1378 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1379 }
1380
1381 if(axis < 0)
1382 {
1383 axis = inputDimSize + axis + 1;
1384 }
1385
Rob Hughesd812a312021-08-06 13:10:53 +01001386 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001387 unsigned int inputShapeIndex = 0;
1388 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1389 {
1390 if (i == static_cast<unsigned int>(axis))
1391 {
1392 shape[i] = 1;
1393 }
1394 else
1395 {
1396 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1397 ++inputShapeIndex;
1398 }
1399 }
1400
Rob Hughesd812a312021-08-06 13:10:53 +01001401 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001402 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001403
1404 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1405 ARMNN_ASSERT(layer != nullptr);
1406 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1407
1408 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1409 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1410
1411 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1412 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1413}
1414
Kevin May7d96b162021-02-03 17:38:41 +00001415void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001416{
1417 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1418
1419 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001420 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001421
1422 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1423 CHECK_VALID_SIZE(outputs.size(), 1);
1424
James Ward58dec6b2020-09-11 17:32:44 +01001425 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001426 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001427
josh minorba424d22019-11-13 10:55:17 -06001428 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001429 {
1430 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1431 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001432 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1433 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001434 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001435 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001436
Mike Kelly08759e22020-03-02 11:41:31 +00001437 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001438 }
1439
James Conroy05102392020-06-24 15:39:55 +01001440 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001441 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001442 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001443
James Conroy05102392020-06-24 15:39:55 +01001444 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001445 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001446 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1447
1448 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1449 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1450
1451 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1452 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1453}
1454
Kevin May7d96b162021-02-03 17:38:41 +00001455void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001456{
1457 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1458
Mike Kelly0d77ae12022-01-07 17:42:27 +00001459 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1460 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001461
1462 TransposeConvolution2dDescriptor desc;
1463 desc.m_BiasEnabled = false;
1464 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1465 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1466 desc.m_DataLayout = armnn::DataLayout::NHWC;
1467
1468 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001469 if (inputs.size() == 4)
1470 {
1471 desc.m_BiasEnabled = true;
1472 }
1473 else
1474 {
1475 CHECK_VALID_SIZE(inputs.size(), 3);
1476 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001477
1478 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1479 CHECK_VALID_SIZE(outputs.size(), 1);
1480
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001481 if (inputs[0])
1482 {
1483 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1484 std::vector<int> output_shape(tensorInfo.GetNumElements());
1485 if (tensorInfo.GetDataType() == DataType::Signed32)
1486 {
1487 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1488 }
1489 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1490 {
1491 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1492 {
1493 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1494 }
1495 }
1496 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1497 for (int dimension : output_shape)
1498 {
1499 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1500 }
1501 desc.m_OutputShapeEnabled = true;
1502 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001503 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001504 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1505
1506 // TfLite uses NHWC tensors
1507 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1508 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1509
1510 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1511 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1512
1513 CalcPadding(inputHeight,
1514 filterHeight,
1515 desc.m_StrideY,
1516 1, // DilationY
1517 desc.m_PadTop,
1518 desc.m_PadBottom,
1519 options->padding);
1520
1521 CalcPadding(inputWidth,
1522 filterWidth,
1523 desc.m_StrideX,
1524 1, // DilationX
1525 desc.m_PadLeft,
1526 desc.m_PadRight,
1527 options->padding);
1528
Mike Kelly5880b912022-01-28 16:18:54 +00001529 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001530
1531 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001532 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001533
David Monahan61683802021-01-12 09:11:07 +00001534 if (desc.m_BiasEnabled)
1535 {
1536 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001537 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001538 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001539 filterTensorAndData.first,
1540 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001541 layerName.c_str());
1542 }
1543 else
1544 {
1545 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001546 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001547 EmptyOptional(),
1548 layerName.c_str());
1549 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001550
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001551 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001552
Sadik Armagand109a4d2020-07-28 10:42:13 +01001553 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001554 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1555
1556 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1557 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001558 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001559
1560 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1561 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1562}
1563
Kevin May7d96b162021-02-03 17:38:41 +00001564void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001565{
1566 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1567}
1568
Samuel Yape7cd8f92022-08-24 17:04:34 +01001569void TfLiteParserImpl::ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex)
1570{
1571 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1572
1573 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1574 CHECK_VALID_SIZE(inputs.size(), 2);
1575
1576 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1577 CHECK_VALID_SIZE(outputs.size(), 1);
1578
1579 auto layerName = fmt::format("BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1580
1581 TensorInfo inputXTensorInfo = ToTensorInfo(inputs[0]);
1582 TensorInfo inputYTensorInfo = ToTensorInfo(inputs[1]);
1583
1584 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1585
1586 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1587 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1588
1589 BatchMatMulDescriptor descriptor(false,
1590 false,
1591 options->adj_x,
1592 options->adj_y);
1593 // Arbitrary DataLayout
1594
1595 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1596 ARMNN_ASSERT(layer != nullptr);
1597
1598 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1599
1600 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1601 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1602
1603 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1604 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1605}
1606
Kevin May7d96b162021-02-03 17:38:41 +00001607void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001608{
1609 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1610
1611 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1612 CHECK_VALID_SIZE(inputs.size(), 3);
1613
1614 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1615 CHECK_VALID_SIZE(outputs.size(), 1);
1616
1617 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1618 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1619
1620 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1621 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1622
1623 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1624 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1625
1626 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1627 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1628
1629 size_t step = 2;
1630 std::vector<std::pair<unsigned int, unsigned int>> crops;
1631 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1632 {
1633 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1634 }
1635
1636 armnn::BatchToSpaceNdDescriptor desc;
1637 desc.m_BlockShape = blockShape;
1638 desc.m_Crops = crops;
1639 desc.m_DataLayout = armnn::DataLayout::NHWC;
1640
James Ward58dec6b2020-09-11 17:32:44 +01001641 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001642
James Conroy05102392020-06-24 15:39:55 +01001643 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001644 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001645 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1646
1647 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1648 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001649 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1650
1651 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1652 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1653
1654 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1655 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1656}
1657
Kevin May7d96b162021-02-03 17:38:41 +00001658void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001659{
1660 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1661
1662 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1663 CHECK_VALID_SIZE(inputs.size(), 1);
1664
1665 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1666 CHECK_VALID_SIZE(outputs.size(), 1);
1667
1668 L2NormalizationDescriptor desc;
1669 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001670 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001671 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1672
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001673 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001674
Sadik Armagand109a4d2020-07-28 10:42:13 +01001675 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001676 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1677
1678 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1679 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1680
1681 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1682 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1683}
1684
Kevin May7d96b162021-02-03 17:38:41 +00001685void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001686{
1687 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1688}
1689
Kevin May7d96b162021-02-03 17:38:41 +00001690void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001691{
1692 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1693
1694 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1695 CHECK_VALID_SIZE(inputs.size(), 2);
1696
1697 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1698 CHECK_VALID_SIZE(outputs.size(), 1);
1699
James Ward58dec6b2020-09-11 17:32:44 +01001700 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001701
1702 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1703 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1704 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001705
Sadik Armagand109a4d2020-07-28 10:42:13 +01001706 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001707 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1708
1709 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1710 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001711 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1712
1713 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001714 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001715
1716 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1717 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1718}
1719
Kevin May7d96b162021-02-03 17:38:41 +00001720void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001721{
1722 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1723
1724 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1725 CHECK_VALID_SIZE(inputs.size(), 2);
1726
1727 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1728 CHECK_VALID_SIZE(outputs.size(), 1);
1729
James Ward58dec6b2020-09-11 17:32:44 +01001730 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001731
1732 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1733 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1734 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001735
Sadik Armagand109a4d2020-07-28 10:42:13 +01001736 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001737 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1738
1739 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1740 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001741 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1742
1743 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001744 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001745
1746 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1747 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1748}
1749
Kevin May7d96b162021-02-03 17:38:41 +00001750void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1751 size_t operatorIndex,
1752 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001753{
1754 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1755
Mike Kelly0d77ae12022-01-07 17:42:27 +00001756 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1757 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001758
1759 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1760
1761 std::string layerName;
1762
1763 switch (algorithm)
1764 {
1765 case PoolingAlgorithm::Average:
1766 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001767 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001768 break;
1769 case PoolingAlgorithm::Max:
1770 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001771 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001772 break;
1773 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001774 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001775 }
1776
1777 Pooling2dDescriptor desc;
1778
1779 desc.m_PoolType = algorithm;
1780 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1781 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1782 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1783 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1784 desc.m_PaddingMethod = PaddingMethod::Exclude;
1785 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001786 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001787
1788 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1789 CHECK_VALID_SIZE(inputs.size(), 1);
1790 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1791
1792 // assuming input is NHWC
1793 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1794 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1795
Pablo Tellof0bd6832019-04-26 17:58:13 +01001796 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1797 desc.m_PadTop, desc.m_PadBottom, options->padding);
1798 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1799 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001800
1801 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1802 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001803
Sadik Armagand109a4d2020-07-28 10:42:13 +01001804 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001805 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1806
1807 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1808 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001809 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001810
1811 // register the input connection slots for the layer, connections are made after all layers have been created
1812 // only the tensors for the inputs are relevant, exclude the const tensors
1813 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001814 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001815
jimfly01c25411c2018-11-14 17:47:22 +00001816 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001817 // register the output connection slots for the layer, connections are made after all layers have been created
1818 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1819 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1820}
1821
Kevin May7d96b162021-02-03 17:38:41 +00001822void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001823{
1824 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1825
1826 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1827 CHECK_VALID_SIZE(inputs.size(), 3);
1828 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1829 CHECK_VALID_SIZE(outputs.size(), 1);
1830
1831 SliceDescriptor desc;
1832
1833 // set begin tensor info for slice descriptor
1834 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1835 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1836
1837 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1838 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1839
1840 // set size tensor info for slice descriptor
1841 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1842 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1843
Mike Kelly7ba84d62021-09-10 15:27:19 +01001844 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1845 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001846 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001847 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1848
1849 for (unsigned int i = 0; i < signedSize.size(); ++i)
1850 {
1851 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001852
Mike Kelly7ba84d62021-09-10 15:27:19 +01001853 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1854 {
1855 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1856 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1857 signedValue,
1858 inputTensorInfo.GetShape()[i] - begin[i],
1859 CHECK_LOCATION().AsString()));
1860 }
1861
1862 if (signedValue == -1)
1863 {
1864 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1865 }
1866 else
1867 {
1868 size[i] = static_cast<unsigned int>(signedValue);
1869 }
1870 }
1871
josh minorba424d22019-11-13 10:55:17 -06001872 desc = SliceDescriptor(begin, size);
1873
James Ward58dec6b2020-09-11 17:32:44 +01001874 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001875
Sadik Armagand109a4d2020-07-28 10:42:13 +01001876 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001877 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1878
1879 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001880 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1881
1882 // register the input connection slots for the layer, connections are made after all layers have been created
1883 // only the tensors for the inputs are relevant, exclude the const tensors
1884 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1885 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1886
1887 // register the output connection slots for the layer, connections are made after all layers have been created
1888 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1889 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1890}
1891
Kevin May7d96b162021-02-03 17:38:41 +00001892void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001893{
1894 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001895 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1896 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001897
1898 SoftmaxDescriptor desc;
1899 desc.m_Beta = options->beta;
1900
1901 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1902 CHECK_VALID_SIZE(inputs.size(), 1);
1903 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1904 CHECK_VALID_SIZE(outputs.size(), 1);
1905
James Ward58dec6b2020-09-11 17:32:44 +01001906 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001907 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1908
Sadik Armagand109a4d2020-07-28 10:42:13 +01001909 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001910 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1911
1912 // register the input connection slots for the layer, connections are made after all layers have been created
1913 // only the tensors for the inputs are relevant, exclude the const tensors
1914 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1915 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1916
1917 // register the output connection slots for the layer, connections are made after all layers have been created
1918 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1919 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1920}
1921
Teresa Charlin455172a2022-06-29 15:35:57 +01001922void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
1923{
1924 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1925
1926 LogSoftmaxDescriptor desc;
1927
1928 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1929 CHECK_VALID_SIZE(inputs.size(), 1);
1930 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1931 CHECK_VALID_SIZE(outputs.size(), 1);
1932
1933 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
1934 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
1935
1936 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1937 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1938
1939 // register the input connection slots for the layer, connections are made after all layers have been created
1940 // only the tensors for the inputs are relevant, exclude the const tensors
1941 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1942 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1943
1944 // register the output connection slots for the layer, connections are made after all layers have been created
1945 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1946 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1947}
1948
Kevin May7d96b162021-02-03 17:38:41 +00001949void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001950{
1951 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1952
1953 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1954 CHECK_VALID_SIZE(inputs.size(), 3);
1955
1956 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1957 CHECK_VALID_SIZE(outputs.size(), 1);
1958
1959 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1960 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1961
1962 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1963 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1964
1965 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1966 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1967
1968 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1969 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1970
1971 size_t step = 2;
1972 std::vector<std::pair<unsigned int, unsigned int>> padList;
1973 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1974 {
1975 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1976 }
1977
1978 armnn::SpaceToBatchNdDescriptor desc;
1979 desc.m_BlockShape = blockShape;
1980 desc.m_PadList = padList;
1981 desc.m_DataLayout = armnn::DataLayout::NHWC;
1982
James Ward58dec6b2020-09-11 17:32:44 +01001983 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001984
James Conroy05102392020-06-24 15:39:55 +01001985 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001986 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001987 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1988
1989 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1990 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001991 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1992
1993 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1994 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1995
1996 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1997 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1998}
1999
Teresa Charlin3ab85482021-06-08 16:59:29 +01002000armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00002001 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01002002{
Teresa Charlin3ab85482021-06-08 16:59:29 +01002003 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01002004 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2005
2006 if (inputTensorInfo.GetNumDimensions() > 4)
2007 {
2008 std::stringstream ss;
2009 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2010 << " shape:" << inputTensorInfo.GetShape() << " "
2011 << CHECK_LOCATION().AsString();
2012 throw ParseException(ss.str());
2013 }
2014
2015 if (squeezeDims.empty())
2016 {
2017 squeezeDims.assign(dimensionSequence,
2018 dimensionSequence+inputTensorInfo.GetNumDimensions());
2019 }
2020
2021 std::vector<uint32_t> outputDims;
2022 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2023 {
2024 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2025 auto currentDimension = inputTensorInfo.GetShape()[i];
2026 if (skipSqueeze || currentDimension != 1)
2027 {
2028 outputDims.push_back(currentDimension);
2029 }
2030 }
2031
2032 if (outputDims.size() > 4)
2033 {
2034 std::stringstream ss;
2035 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2036 << " shape:" << inputTensorInfo.GetShape() << " "
2037 << CHECK_LOCATION().AsString();
2038 throw ParseException(ss.str());
2039 }
2040
2041 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2042 outputDims.data());
2043
2044 // we need to preserve the tensor type and the quantization data as well
2045 TensorInfo outTensorInfo = inputTensorInfo;
2046 outTensorInfo.SetShape(outShape);
2047
2048 return outTensorInfo;
2049}
2050
Keith Davis0176fd82021-06-01 17:36:32 +01002051void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2052{
2053 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2054
2055 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2056 CHECK_VALID_SIZE(inputs.size(), 1);
2057 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2058 CHECK_VALID_SIZE(outputs.size(), 1);
2059
2060 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2061
2062 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2063 ARMNN_ASSERT(layer != nullptr);
2064
2065
2066 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2067 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2068
2069 // Check if output tensor type is Signed32 or Signed64
2070 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2071 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2072 {
2073 throw ParseException(
2074 fmt::format(
2075 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2076 CHECK_LOCATION().AsString()));
2077 }
2078
2079 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2080 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2081
2082 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2083 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2084}
2085
Kevin May7d96b162021-02-03 17:38:41 +00002086void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002087{
2088 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2089
2090 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2091 CHECK_VALID_SIZE(inputs.size(), 1);
2092
2093 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2094 CHECK_VALID_SIZE(outputs.size(), 1);
2095
2096 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2097 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002098 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002099
2100 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002101
2102 std::vector<uint32_t> squeezeDim;
2103 // A single negative dim index is interpreted as a negative index in python
2104 // Meaning the index will be the shape size plus the negative index value
2105 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2106 {
2107 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2108 squeezeDim.push_back(static_cast<uint32_t>(dim));
2109 }
2110 else
2111 {
2112 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2113 }
2114
2115 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2116
James Conroy05102392020-06-24 15:39:55 +01002117 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002118
2119 ReshapeDescriptor reshapeDesc;
2120 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2121
telsoa01c577f2c2018-08-31 09:22:23 +01002122 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002123 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002124 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2125
2126 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2127 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2128
2129 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2130 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2131}
2132
Kevin May7d96b162021-02-03 17:38:41 +00002133void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002134{
2135 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2136
2137 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2138 CHECK_VALID_SIZE(inputs.size(), 4);
2139
2140 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2141 CHECK_VALID_SIZE(outputs.size(), 1);
2142
Mike Kelly0d77ae12022-01-07 17:42:27 +00002143 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2144 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002145
2146 StridedSliceDescriptor desc;
2147 desc.m_BeginMask = options->begin_mask;
2148 desc.m_EllipsisMask = options->ellipsis_mask;
2149 desc.m_EndMask = options->end_mask;
2150 desc.m_NewAxisMask = options->new_axis_mask;
2151 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2152 desc.m_DataLayout = armnn::DataLayout::NHWC;
2153
2154 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2155 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2156
2157 std::vector<int> begin(beginTensorInfo.GetNumElements());
2158 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2159
2160 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2161 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2162
2163 std::vector<int> end(endTensorInfo.GetNumElements());
2164 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2165
2166 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2167 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2168
2169 std::vector<int> stride(strideTensorInfo.GetNumElements());
2170 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2171
2172 desc.m_Begin = begin;
2173 desc.m_End = end;
2174 desc.m_Stride = stride;
2175
James Ward58dec6b2020-09-11 17:32:44 +01002176 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002177 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002178 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002179
Sadik Armagand109a4d2020-07-28 10:42:13 +01002180 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002181 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2182
2183 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2184 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2185
2186 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2187 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2188}
2189
Kevin May7d96b162021-02-03 17:38:41 +00002190void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002191{
2192 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2193
Mike Kelly0d77ae12022-01-07 17:42:27 +00002194 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2195 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002196
2197 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2198 CHECK_VALID_SIZE(inputs.size(), 2);
2199
2200 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2201 CHECK_VALID_SIZE(outputs.size(), 1);
2202
2203 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2204 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2205
James Ward58dec6b2020-09-11 17:32:44 +01002206 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002207 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002208 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002209
Sadik Armagand109a4d2020-07-28 10:42:13 +01002210 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002211 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2212
2213 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002214 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002215
2216 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2217
2218 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2219 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2220}
2221
Kevin May7d96b162021-02-03 17:38:41 +00002222void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302223{
2224 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2225
Mike Kelly0d77ae12022-01-07 17:42:27 +00002226 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2227 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302228
2229 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2230 CHECK_VALID_SIZE(inputs.size(), 2);
2231
2232 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2233 CHECK_VALID_SIZE(outputs.size(), 1);
2234
2235 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2236 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2237
James Ward58dec6b2020-09-11 17:32:44 +01002238 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302239 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002240 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302241
Sadik Armagand109a4d2020-07-28 10:42:13 +01002242 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302243 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2244
2245 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002246 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302247 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2248
2249 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2250 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2251}
2252
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002253void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2254{
2255 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2256
2257 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2258 CHECK_VALID_SIZE(inputs.size(), 2);
2259
2260 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2261 CHECK_VALID_SIZE(outputs.size(), 1);
2262
2263 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2264 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2265
2266 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2267 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2268 ARMNN_ASSERT(layer != nullptr);
2269
2270 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2271 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2272
2273 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2274 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2275 layer = AddFusedFloorLayer(layer, 0);
2276
2277 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2278 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2279}
2280
Kevin May7d96b162021-02-03 17:38:41 +00002281void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002282{
2283 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2284
Mike Kelly0d77ae12022-01-07 17:42:27 +00002285 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2286 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002287
2288 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2289 CHECK_VALID_SIZE(inputs.size(), 2);
2290
2291 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2292 CHECK_VALID_SIZE(outputs.size(), 1);
2293
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002294 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2295 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2296
James Ward58dec6b2020-09-11 17:32:44 +01002297 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002298 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002299 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002300
Sadik Armagand109a4d2020-07-28 10:42:13 +01002301 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002302 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2303
2304 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002305 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002306 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2307
2308 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2309 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2310}
2311
Kevin May7d96b162021-02-03 17:38:41 +00002312void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002313{
2314 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2315
Mike Kelly0d77ae12022-01-07 17:42:27 +00002316 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2317 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002318
2319 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2320 CHECK_VALID_SIZE(inputs.size(), 2);
2321
2322 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2323 CHECK_VALID_SIZE(outputs.size(), 1);
2324
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002325 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2326 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2327
James Ward58dec6b2020-09-11 17:32:44 +01002328 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002329 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002330 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002331
Sadik Armagand109a4d2020-07-28 10:42:13 +01002332 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002333 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2334
2335 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002336 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002337 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2338
2339 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2340 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2341}
2342
Kevin May7d96b162021-02-03 17:38:41 +00002343void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002344{
2345 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2346
2347 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2348
2349 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2350 CHECK_VALID_SIZE(outputs.size(), 1);
2351
2352 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2353 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2354
2355 armnn::MeanDescriptor desc;
2356 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2357 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2358 desc.m_Axis = axis;
2359
2360 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002361 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002362
2363 desc.m_KeepDims =
2364 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2365 true : false;
2366
James Ward58dec6b2020-09-11 17:32:44 +01002367 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002368 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002369 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002370
2371 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2372
2373 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2374 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2375
2376 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2377 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2378}
2379
Kevin May7d96b162021-02-03 17:38:41 +00002380void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002381{
2382 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2383
Kevin May7d96b162021-02-03 17:38:41 +00002384 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002385
Kevin May7d96b162021-02-03 17:38:41 +00002386 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002387 CHECK_VALID_SIZE(outputs.size(), 1);
2388
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002389 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002390 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002391
Mike Kelly0d77ae12022-01-07 17:42:27 +00002392 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002393
2394 size_t step = 2;
2395 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002396 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2397
2398 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002399 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002400 CHECK_VALID_SIZE(inputs.size(), 2);
2401
2402 if (inputTensorInfo.IsQuantized())
2403 {
2404 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2405 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002406 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002407 else if (opcode == tflite::BuiltinOperator_PADV2)
2408 {
2409 CHECK_VALID_SIZE(inputs.size(), 3);
2410
2411 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2412
2413 if (padValueTensorInfo.GetNumElements() != 1)
2414 {
2415 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2416 }
2417 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2418
2419 // Get the pad value from the input tensor
2420 if (padValueBufferPtr->data.size() > 0)
2421 {
2422 switch (padValueTensorInfo.GetDataType())
2423 {
2424 case armnn::DataType::Float32:
2425 {
2426 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2427 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2428 desc.m_PadValue = padValueBuffer[0];
2429 break;
2430 }
2431 case armnn::DataType::QAsymmU8:
2432 {
2433 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2434 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2435 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2436 padValueTensorInfo.GetQuantizationScale(),
2437 padValueTensorInfo.GetQuantizationOffset());
2438 break;
2439 }
2440 case armnn::DataType::QAsymmS8:
2441 case armnn::DataType::QSymmS8:
2442 {
2443 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2444 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2445 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2446 padValueTensorInfo.GetQuantizationScale(),
2447 padValueTensorInfo.GetQuantizationOffset());
2448 break;
2449 }
2450 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2451 }
2452 }
2453 else if (inputTensorInfo.IsQuantized())
2454 {
2455 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2456 }
2457 }
2458
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002459 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2460 {
2461 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2462 }
2463
Mike Kelly0d77ae12022-01-07 17:42:27 +00002464 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2465 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002466 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002467
2468 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2469 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002470 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2471
2472 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2473 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2474
2475 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2476 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2477}
2478
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002479void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2480{
2481 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2482
2483 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2484 CHECK_VALID_SIZE(inputs.size(), 2);
2485
2486 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2487 CHECK_VALID_SIZE(outputs.size(), 1);
2488
2489 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2490
2491 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2492 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2493
2494 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2495 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2496
2497 size_t step = 2;
2498 armnn::PadDescriptor desc;
2499 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2500 {
2501 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2502 }
2503
2504 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2505 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2506
2507 if (options->mode == tflite::MirrorPadMode_REFLECT)
2508 {
2509 desc.m_PaddingMode = PaddingMode::Reflect;
2510 }
2511 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2512 {
2513 desc.m_PaddingMode = PaddingMode::Symmetric;
2514 }
2515 else
2516 {
2517 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2518 }
2519
2520 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2521 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2522 auto inputShape = inputTensorInfo.GetShape();
2523 auto padList = desc.m_PadList;
2524
2525 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2526 for(unsigned int i = 0; i < padList.size(); ++i)
2527 {
2528 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2529 padList.at(i).second > (inputShape[i] - isReflect))
2530 {
2531 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2532 "equal (Symmetric) to the dimension size.");
2533 }
2534 }
2535
2536 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2537 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2538
2539 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2540 ARMNN_ASSERT(layer != nullptr);
2541 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2542
2543 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2544 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2545
2546 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2547 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2548}
2549
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002550void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2551{
2552 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2553
2554 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2555 CHECK_VALID_SIZE(inputs.size(), 2);
2556
2557 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2558 CHECK_VALID_SIZE(outputs.size(), 1);
2559
2560 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2561
2562 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2563 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2564 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2565 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2566
2567 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2568 ARMNN_ASSERT(layer != nullptr);
2569 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2570
2571 if (IsConstTensor(inputs[1]))
2572 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002573 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002574 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2575 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002576
Mike Kelly5880b912022-01-28 16:18:54 +00002577 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2578 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002579 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2580 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002581 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002582 ARMNN_ASSERT(constLayer != nullptr);
2583
2584 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2585 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2586 RegisterOutputSlots(subgraphIndex,
2587 VIRTUAL_OPERATOR_ID,
2588 constLayer,
2589 { inputTensorIndexes[1] });
2590 }
2591 else
2592 {
2593 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2594 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2595 }
2596
2597 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2598 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2599}
2600
Kevin May7d96b162021-02-03 17:38:41 +00002601void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002602{
2603 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2604
2605 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2606 CHECK_VALID_SIZE(inputs.size(), 1);
2607
2608 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2609 CHECK_VALID_SIZE(outputs.size(), 1);
2610
James Ward58dec6b2020-09-11 17:32:44 +01002611 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002612
2613 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002614 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002615
Sadik Armagand109a4d2020-07-28 10:42:13 +01002616 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002617 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2618
2619 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2620 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2621
2622 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2623 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2624}
Finn Williamsc42c3842019-01-22 14:18:11 +00002625
Kevin May7d96b162021-02-03 17:38:41 +00002626void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002627{
Finn Williamsc42c3842019-01-22 14:18:11 +00002628 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002629}
2630
Kevin May7d96b162021-02-03 17:38:41 +00002631void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002632{
Finn Williamsc42c3842019-01-22 14:18:11 +00002633 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2634}
Sadik Armagan58f39192018-09-17 14:14:39 +01002635
Kevin May7d96b162021-02-03 17:38:41 +00002636void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002637{
Jan Eilers2f746b32020-07-28 14:00:06 +01002638 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002639}
2640
Kevin May7d96b162021-02-03 17:38:41 +00002641void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002642{
2643 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2644}
2645
Kevin May7d96b162021-02-03 17:38:41 +00002646void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002647{
2648 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2649}
2650
Kevin May7d96b162021-02-03 17:38:41 +00002651void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002652{
2653 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2654}
2655
Kevin May7d96b162021-02-03 17:38:41 +00002656void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002657{
2658 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2659}
Finn Williamsc42c3842019-01-22 14:18:11 +00002660
Kevin May7d96b162021-02-03 17:38:41 +00002661void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002662{
2663 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002664 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002665 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002666
2667 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2668 CHECK_VALID_SIZE(inputs.size(), 1);
2669
2670 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2671 CHECK_VALID_SIZE(outputs.size(), 1);
2672
James Ward58dec6b2020-09-11 17:32:44 +01002673 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002674 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002675 activationDesc.m_Function = activationType;
2676
2677 switch (activationType)
2678 {
2679 case ActivationFunction::ReLu:
2680 {
James Ward58dec6b2020-09-11 17:32:44 +01002681 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002682 break;
2683 }
2684 case ActivationFunction::BoundedReLu:
2685 {
James Ward58dec6b2020-09-11 17:32:44 +01002686 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002687 activationDesc.m_A = 6.0f;
2688 activationDesc.m_B = 0.0f;
2689 break;
2690 }
2691 case ActivationFunction::Sigmoid:
2692 {
James Ward58dec6b2020-09-11 17:32:44 +01002693 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002694 break;
2695 }
Nina Drozd99851762019-04-09 09:37:38 +01002696 case ActivationFunction::TanH:
2697 {
James Ward58dec6b2020-09-11 17:32:44 +01002698 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002699 activationDesc.m_A = 1.0f;
2700 activationDesc.m_B = 1.0f;
2701 break;
2702 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002703 case ActivationFunction::LeakyReLu:
2704 {
James Ward58dec6b2020-09-11 17:32:44 +01002705 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002706 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002707 activationDesc.m_A = options->alpha;
2708 break;
2709 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002710 case ActivationFunction::Elu:
2711 {
2712 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2713 activationDesc.m_A = 1.0f;
2714 break;
2715 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002716 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002717 {
James Ward58dec6b2020-09-11 17:32:44 +01002718 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002719 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002720 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002721 default:
2722 {
2723 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002724 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2725 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002726 }
2727 }
2728
2729 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002730
Sadik Armagand109a4d2020-07-28 10:42:13 +01002731 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002732 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2733
2734 // register the input connection slots for the layer, connections are made after all layers have been created
2735 // only the tensors for the inputs are relevant, exclude the const tensors
2736 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2737 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2738
2739 // register the output connection slots for the layer, connections are made after all layers have been created
2740 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2741 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2742}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002743armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2744 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002745{
2746 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2747 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2748
2749 if (stretchDim != targetDimsIn.end())
2750 {
2751 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2752 {
2753 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002754 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002755 }
2756
2757 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002758 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002759 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2760
2761 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2762 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2763 }
2764
2765 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2766
2767 TensorInfo reshapeInfo = inputTensorInfo;
2768 reshapeInfo.SetShape(outputShape);
2769
2770 return reshapeInfo;
2771}
2772
Kevin May7d96b162021-02-03 17:38:41 +00002773void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002774{
2775 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2776
2777 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002778
2779 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2780 CHECK_VALID_SIZE(outputs.size(), 1);
2781
Mike Kelly0d77ae12022-01-07 17:42:27 +00002782 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2783 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002784 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002785
2786 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002787 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002788 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002789
Jan Eilersbac9b352020-07-13 13:40:24 +01002790 // Extracting new shape for the output
2791 // There are two ways it can be passed
2792 // * First is to define the target shape in the operator built-in options
2793 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002794 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002795 bool targetShapeFound = false;
2796 // Check if built-in options were given
2797 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002798 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002799 // make sure the parameter is given
2800 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002801 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002802 targetShape = options->new_shape;
2803 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002804 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002805 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002806
2807 // If there is no built-in option given or if the built-in new_shape parameter was empty
2808 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002809 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002810 // Check for a second input tensor
2811 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002812 {
2813 if (inputs[1]->is_variable)
2814 {
2815 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2816 }
2817
2818 if (inputs[1]->shape.size() != 1)
2819 {
2820 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2821 }
2822
2823 if (inputs[1]->type != tflite::TensorType_INT32)
2824 {
2825 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2826 }
2827
Teresa Charlin6a056a42021-12-01 10:25:43 +00002828 // Extract target shape from input
2829 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2830 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002831 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002832 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002833 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2834 {
2835 targetShape.push_back(values[i]);
2836 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002837 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002838 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002839 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002840 try
2841 {
2842 // We attempt to infer during Runtime.
2843 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2844 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2845 if (reshapeShapes[0] > 2)
2846 {
2847 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2848 "When inferring during runtime, the parser only supports "
2849 "shape (batch, -1) or (-1) for target shape input.",
2850 reshapeShapes[0],
2851 layerName,
2852 CHECK_LOCATION().AsString()));
2853 }
2854
2855 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2856 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2857 if (reshapeShapes[0] == 1)
2858 {
2859 targetShape = {numInputElements};
2860 }
2861 else if (reshapeShapes[0] == 2)
2862 {
2863 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2864 }
2865 }
2866 catch (const std::exception& exc)
2867 {
2868 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2869 "Reshape operation. Reshape operator target shape input buffer data "
2870 "is null. " << exc.what());
2871 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002872 }
2873 }
2874 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002875 {
2876 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2877 "At least one method required");
2878 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002879 }
2880
kevmay0171972a82018-12-17 14:28:03 +00002881 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002882 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002883
kevmay0171972a82018-12-17 14:28:03 +00002884 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002885 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2886 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002887 {
2888 std::stringstream ss;
2889 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002890 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002891 << " does not equal output shape "
2892 << actualOutputTensorInfo.GetShape()
2893 << ": "
2894 << CHECK_LOCATION().AsString();
2895 throw ParseException(ss.str());
2896 }
2897
Sadikb94967b2018-09-19 15:30:00 +01002898 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002899 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002900
Sadikb94967b2018-09-19 15:30:00 +01002901 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002902 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002903 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002904
2905 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2906 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2907
2908 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2909 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2910}
2911
Kevin May7d96b162021-02-03 17:38:41 +00002912void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002913{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002914 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2915}
2916
Kevin May7d96b162021-02-03 17:38:41 +00002917void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002918{
2919 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2920}
2921
Kevin May7d96b162021-02-03 17:38:41 +00002922void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002923{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002924 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2925
2926 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2927 CHECK_VALID_SIZE(inputs.size(), 2);
2928
2929 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2930 CHECK_VALID_SIZE(outputs.size(), 1);
2931
2932 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2933
2934 // Data for the parsed tensor args (size) must be stored locally.
2935 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2936
2937 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2938 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2939
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002940 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002941 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002942 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002943 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2944 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002945
James Ward58dec6b2020-09-11 17:32:44 +01002946 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002947
2948 switch (resizeMethod)
2949 {
2950 case ResizeMethod::Bilinear:
2951 {
James Ward58dec6b2020-09-11 17:32:44 +01002952 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002953
2954 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2955 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2956
David Monahan4a0c9b92020-05-30 09:48:39 +01002957 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002958 break;
2959 }
2960 case ResizeMethod::NearestNeighbor:
2961 {
James Ward58dec6b2020-09-11 17:32:44 +01002962 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002963 break;
2964 }
2965 default:
2966 {
2967 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002968 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2969 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002970 }
2971 }
2972
James Conroy05102392020-06-24 15:39:55 +01002973 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002974 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002975 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2976
2977 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2978 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002979 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2980
2981 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2982 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2983
2984 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2985 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2986}
2987
Kevin May7d96b162021-02-03 17:38:41 +00002988void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002989{
2990 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2991
Mike Kelly0d77ae12022-01-07 17:42:27 +00002992 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2993 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002994
2995 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2996
2997 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2998 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2999 CHECK_VALID_SIZE(outputs.size(), 1);
3000
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003001 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
3002 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003003
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003004 const unsigned int concatDimInput = static_cast<unsigned int>(
3005 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01003006
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003007 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3008 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01003009
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003010 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01003011
3012 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3013 {
3014 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
3015
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003016 // This set up concatDescriptor view origin
3017 armnnUtils::ProcessConcatInputTensorInfo(
3018 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01003019 }
3020
James Ward58dec6b2020-09-11 17:32:44 +01003021 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01003022 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01003023
Jim Flynn906f9462019-05-10 13:55:21 +01003024 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003025 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003026 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01003027
James Conroy05102392020-06-24 15:39:55 +01003028 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003029 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01003030
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003031 // add fused activation layer
3032 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01003033
Sadik Armagan479045b2018-10-01 11:51:37 +01003034 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3035 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3036}
3037
Kevin May7d96b162021-02-03 17:38:41 +00003038void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003039{
3040 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3041
Mike Kelly0d77ae12022-01-07 17:42:27 +00003042 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003043 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3044
3045 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3046
3047 FullyConnectedDescriptor desc;
3048 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003049 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003050
3051 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3052 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3053 CHECK_VALID_SIZE(outputs.size(), 1);
3054
3055 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
3056
3057 // Fully Connected Layer accepts two dimensional weights input
3058 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3059 if (weightsDimension != 2)
3060 {
3061 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003062 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3063 "Node {}",
3064 weightsDimension,
3065 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003066 }
3067
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003068 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003069 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003070
Matthew Sloyan81beae32021-07-13 19:46:11 +01003071 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3072 // Add the first input tensor to the registration list
3073 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3074 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00003075 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003076
3077 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3078
Matthew Sloyan81beae32021-07-13 19:46:11 +01003079 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3080 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003081
Mike Kelly5880b912022-01-28 16:18:54 +00003082 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3083 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3084 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
3085 {
3086 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3087 }
3088
Finn Williamsd4fa5452021-03-01 12:31:41 +00003089 if (inputs.size() == 3)
3090 {
3091 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003092 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003093
3094 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3095 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003096
3097 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3098 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3099 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3100 {
3101 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3102 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003103 }
3104
Matthew Sloyan81beae32021-07-13 19:46:11 +01003105 // Filters and biases are always passed to fully connected as inputs
3106 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003107
3108 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003109
Finn Williamsd4fa5452021-03-01 12:31:41 +00003110 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003111 if (inputTensorInfo.GetNumDimensions() > 2)
3112 {
3113 // Add reshape to flatten to 2D [batch_size, input_size],
3114 // where "input_size" corresponds to the number of inputs to the layer,
3115 // matching the second dimension of weights,
3116 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3117 std::vector<unsigned int> reshapedDimensions(2);
3118 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3119 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3120
3121 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3122 {
3123 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003124 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3125 reshapedDimensions[1],
3126 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003127 }
3128
3129 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3130 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3131
James Ward58dec6b2020-09-11 17:32:44 +01003132 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003133 armnn::ReshapeDescriptor reshapeDescriptor;
3134 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3135 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003136
3137 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3138 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3139
3140 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003141 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3142 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3143 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003144 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003145
3146 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003147
Sadik Armagand109a4d2020-07-28 10:42:13 +01003148 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003149 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3150
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003151 // we need to add the activation layer and fortunately we don't need to care about the data layout
3152 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3153 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003154
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003155 // register the output connection slots for the layer, connections are made after all layers have been created
3156 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3157 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3158}
3159
Kevin May7d96b162021-02-03 17:38:41 +00003160void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003161{
3162 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3163
Mike Kelly0d77ae12022-01-07 17:42:27 +00003164 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003165
3166 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3167 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3168 CHECK_VALID_SIZE(outputs.size(), 4);
3169
3170 // Obtain custom options from flexbuffers
3171 auto custom_options = operatorPtr->custom_options;
3172 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3173
3174 // Obtain descriptor information from tf lite
3175 DetectionPostProcessDescriptor desc;
3176 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3177 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3178 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3179 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3180 desc.m_NumClasses = m["num_classes"].AsUInt32();
3181 desc.m_ScaleH = m["h_scale"].AsFloat();
3182 desc.m_ScaleW = m["w_scale"].AsFloat();
3183 desc.m_ScaleX = m["x_scale"].AsFloat();
3184 desc.m_ScaleY = m["y_scale"].AsFloat();
3185
keidav0107d58c72019-02-26 11:57:39 +00003186 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003187 {
keidav0107d58c72019-02-26 11:57:39 +00003188 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003189 }
3190 if (!(m["detections_per_class"].IsNull()))
3191 {
3192 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3193 }
3194
3195 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3196 {
3197 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3198 "must be positive and less than or equal to 1.");
3199 }
3200
3201 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003202 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003203
James Ward58dec6b2020-09-11 17:32:44 +01003204 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003205 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003206 layerName.c_str());
3207
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003208 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003209
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003210 // The model does not specify the output shapes.
3211 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3212 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3213 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3214 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3215 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3216 m_OverridenOutputShapes.push_back({ 1 });
3217
keidav011b3e2ea2019-02-21 10:07:37 +00003218 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3219 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003220 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003221 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3222 }
3223
3224 // Register the input connection slots for the layer, connections are made after all layers have been created
3225 // only the tensors for the inputs are relevant, exclude the const tensors
3226 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3227 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3228
3229 // Register the output connection slots for the layer, connections are made after all layers have been created
3230 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3231 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3232 outputTensorIndexes[1],
3233 outputTensorIndexes[2],
3234 outputTensorIndexes[3]});
3235}
3236
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003237/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003238void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003239{
3240 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3241
3242 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3243 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3244 CHECK_VALID_SIZE(outputs.size(), 1);
3245
3246 if (inputs.size() < 1)
3247 {
3248 throw ParseException("Pack must have at least one input.");
3249 }
3250
3251 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3252 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3253
3254 StackDescriptor desc;
3255 desc.m_Axis = static_cast<uint32_t>(options->axis);
3256 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3257
3258 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3259 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3260 desc.m_InputShape = inputTensorInfo.GetShape();
3261
James Ward58dec6b2020-09-11 17:32:44 +01003262 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003263 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3264
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003265 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003266
Sadik Armagand109a4d2020-07-28 10:42:13 +01003267 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003268 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3269
3270 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3271 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3272
3273 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3274 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3275}
3276
Mike Kelly5880b912022-01-28 16:18:54 +00003277void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3278{
3279 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3280
3281 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3282 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3283
3284 if (inputs.size() < 2)
3285 {
3286 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3287 }
3288
3289 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3290 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3291 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3292 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3293 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3294 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3295
3296 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3297 // Please refer to each operand at
3298 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3299 armnn::LstmInputParams params;
3300
3301 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3302 {
3303 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3304 inputTensorInfo).first;
3305 }
3306
3307 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3308 inputTensorInfo).first;
3309 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3310 inputTensorInfo).first;
3311 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3312 inputTensorInfo).first;
3313
3314 // Recurrent weight tensors of size {n_cell, n_output}
3315 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3316 {
3317 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3318 inputTensorInfo).first;
3319 }
3320
3321 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3322 inputTensorInfo).first;
3323 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3324 inputTensorInfo).first;
3325 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3326 inputTensorInfo).first;
3327
3328 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3329 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3330 {
3331 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3332 inputTensorInfo).first;
3333 }
3334
3335 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3336 {
3337 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3338 inputTensorInfo).first;
3339 }
3340
3341 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3342 {
3343 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3344 inputTensorInfo).first;
3345 }
3346
3347 // Gates bias tensors of size {n_cell}
3348 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3349 {
3350 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3351 inputTensorInfo).first;
3352 }
3353
3354 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3355 inputTensorInfo).first;
3356 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3357 inputTensorInfo).first;
3358 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3359 inputTensorInfo).first;
3360
3361 // Projection weight tensor of size {n_output, n_cell}
3362 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3363 {
3364 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3365 inputTensorInfo).first;
3366 }
3367 // Projection bias tensor of size {n_output}
3368 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3369 {
3370 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3371 inputTensorInfo).first;
3372 }
3373
3374 // These state tensors are defined as variable tensors, and will be modified by this op.
3375 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3376 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3377 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3378 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3379
3380 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3381 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3382 {
3383 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3384 inputTensorInfo).first;
3385 }
3386
3387 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3388 {
3389 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3390 inputTensorInfo).first;
3391 }
3392
3393 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3394 {
3395 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3396 inputTensorInfo).first;
3397 }
3398
3399 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3400 {
3401 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3402 inputTensorInfo).first;
3403 }
3404
3405 // set the layer descriptor
3406 armnn::UnidirectionalSequenceLstmDescriptor desc;
3407 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3408 desc.m_ClippingThresCell = nodeParams->cell_clip;
3409 desc.m_ClippingThresProj = nodeParams->proj_clip;
3410 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3411 || params.m_RecurrentToInputWeights == nullptr
3412 || params.m_InputGateBias == nullptr);
3413 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3414 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3415 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3416 || params.m_ForgetLayerNormWeights != nullptr
3417 || params.m_CellLayerNormWeights != nullptr
3418 || params.m_OutputLayerNormWeights != nullptr);
3419 desc.m_TimeMajor = nodeParams->time_major;
3420
Mike Kellyc0800a32022-06-15 10:57:52 +01003421 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003422 {
3423 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3424 inputTensorInfo).first;
3425 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3426 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3427
3428 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3429 inputTensorInfo).first;
3430 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3431 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3432
3433 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3434 inputTensorInfo).first;
3435 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3436 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3437
3438 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3439 inputTensorInfo).first;
3440 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3441 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3442 }
3443 else
3444 {
3445 float defaultIntermediate = std::pow(2, -12);
3446 desc.m_InputIntermediateScale = defaultIntermediate;
3447 desc.m_ForgetIntermediateScale = defaultIntermediate;
3448 desc.m_CellIntermediateScale = defaultIntermediate;
3449 desc.m_OutputIntermediateScale = defaultIntermediate;
3450 }
3451
Mike Kellyc0800a32022-06-15 10:57:52 +01003452 if (operatorPtr->intermediates.size() > 4)
3453 {
3454 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3455 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003456
Mike Kellyc0800a32022-06-15 10:57:52 +01003457 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3458 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3459 }
Mike Kelly5880b912022-01-28 16:18:54 +00003460 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3461 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3462 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3463
3464 armnn::DataType dataType = inputTensorInfo.GetDataType();
3465 float qScale = inputTensorInfo.GetQuantizationScale();
3466 float qOffset = inputTensorInfo.GetQuantizationOffset();
3467
3468 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3469 if (!desc.m_CifgEnabled)
3470 {
3471 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3472 }
3473 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3474 cellStateInInfo.GetDataType(),
3475 cellStateInInfo.GetQuantizationScale(),
3476 cellStateInInfo.GetQuantizationOffset());
3477 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3478
3479 armnn::LstmInputParamsInfo paramsInfo;
3480 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3481 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3482 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3483 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3484 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3485 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3486 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3487 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3488 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3489
3490 if (!desc.m_CifgEnabled)
3491 {
3492 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3493 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3494 if (params.m_CellToInputWeights != nullptr)
3495 {
3496 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3497 }
3498 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3499 }
3500
3501 if (desc.m_ProjectionEnabled)
3502 {
3503 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3504 if (params.m_ProjectionBias != nullptr)
3505 {
3506 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3507 }
3508 }
3509
3510 if (desc.m_PeepholeEnabled)
3511 {
3512 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3513 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3514 }
3515
3516 if (desc.m_LayerNormEnabled)
3517 {
3518 if(!desc.m_CifgEnabled)
3519 {
3520 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3521 }
3522 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3523 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3524 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3525 }
3526
3527 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3528 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3529 ARMNN_ASSERT(layer != nullptr);
3530
3531 // register the input connection slots for the layer, connections are made after all layers have been created
3532 // only the tensors for the inputs are relevant, exclude the const tensors
3533 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3534 operatorPtr->inputs[18],
3535 operatorPtr->inputs[19]});
3536 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3537 inputTensorIndexes[1],
3538 inputTensorIndexes[2]});
3539
3540 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3541
3542 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3543 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3544 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3545
3546 unsigned int tensorIndex = outputTensorIndexes[0];
3547 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3548 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3549}
3550
Kevin May7d96b162021-02-03 17:38:41 +00003551void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003552{
3553 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3554
Mike Kelly0d77ae12022-01-07 17:42:27 +00003555 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3556 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003557
3558 // This unpackAxis indicates the axis to unpack
3559 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3560
3561 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3562 CHECK_VALID_SIZE(inputs.size(), 1);
3563
3564 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003565
3566 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3567 {
3568 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003569 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3570 "the number of input dimension {} {}",
3571 unpackAxis,
3572 inputTensorInfo.GetNumDimensions(),
3573 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003574 }
3575
Nina Drozd200e3802019-04-15 09:47:39 +01003576 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3577 // If num is not defined, automatically infer from the length of the dimension axis.
3578 if(unpackNum == 0)
3579 {
3580 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3581 }
3582
3583 // If unpack number cannot be inferred and is still zero, throw ParseException.
3584 if(unpackNum == 0)
3585 {
3586 throw ParseException("Number to unpack must greater than zero.");
3587 }
3588
3589 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3590 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3591
3592 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3593 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3594
3595 // Add current input shape to unpackDimSizes
3596 for (unsigned int i = 0; i < inputDimSize; ++i)
3597 {
3598 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3599 }
3600
3601 if (unpackDimSizes[unpackAxis] != unpackNum)
3602 {
3603 throw ParseException("Number to unpack must be the same as length of the dimension to "
3604 "unpack along.");
3605 }
3606
3607 unpackDimSizes[unpackAxis] /= unpackNum;
3608
3609 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3610 for (unsigned int j = 0; j < unpackNum; ++j)
3611 {
3612 // Set the size of the views.
3613 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3614 {
3615 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3616 }
3617 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3618 }
3619
James Ward58dec6b2020-09-11 17:32:44 +01003620 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003621 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003622 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003623
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003624 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3625 unpackDimSizes.data());
3626
Nina Drozd200e3802019-04-15 09:47:39 +01003627 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3628 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3629
Finn Williamsb49ed182021-06-29 15:50:08 +01003630 std::vector<unsigned int> reshapeDims;
3631 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3632 {
3633 if (axis != unpackAxis)
3634 {
3635 reshapeDims.push_back(splitOutShape[axis]);
3636 }
3637 }
3638
3639 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3640
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003641 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3642 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3643 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003644 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003645 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003646 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003647 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003648 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3649
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003650 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3651 outputTensorInfo.GetDataType(),
3652 outputTensorInfo.GetQuantizationScale(),
3653 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003654 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3655
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003656 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003657
3658 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3659 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3660 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3661 }
Nina Drozd200e3802019-04-15 09:47:39 +01003662}
3663
Kevin May7d96b162021-02-03 17:38:41 +00003664void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003665{
3666 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3667
Mike Kelly0d77ae12022-01-07 17:42:27 +00003668 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3669 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003670
3671 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3672
Nina Drozd200e3802019-04-15 09:47:39 +01003673 // If number of splits cannot be inferred and is zero, throw ParseException.
3674 if(numSplits == 0)
3675 {
3676 throw ParseException("Number to splits must greater than zero.");
3677 }
3678
Nina Drozd0324f482019-04-08 10:52:10 +01003679 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3680 CHECK_VALID_SIZE(inputs.size(), 2);
3681 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3682 CHECK_VALID_SIZE(outputs.size(), numSplits);
3683
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003684 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3685 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3686 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003687
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003688 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003689 if (axisBufferPtr == nullptr)
3690 {
3691 throw ParseException(
3692 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3693 CHECK_LOCATION().AsString()));
3694 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003695
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003696 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3697 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3698 int32_t axis = axisData[0];
3699
3700 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3701 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3702 {
3703 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3704 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3705 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3706 throw ParseException(
3707 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3708 axis,
3709 CHECK_LOCATION().AsString()));
3710 }
3711
3712 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003713
Nina Drozd0324f482019-04-08 10:52:10 +01003714 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003715 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003716 {
3717 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003718 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3719 inputTensorInfo.GetNumDimensions(),
3720 MaxNumOfTensorDimensions,
3721 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003722 }
3723
3724 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3725
3726 // Add current input shape to splitterDimSizes
3727 for (unsigned int i = 0; i < inputDimSize; ++i)
3728 {
3729 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3730 }
3731
3732 if (splitterDimSizes[splitDim] % numSplits != 0)
3733 {
3734 throw ParseException("Number of splits must evenly divide the dimension");
3735 }
3736 splitterDimSizes[splitDim] /= numSplits;
3737
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003738 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003739 for (unsigned int j = 0; j < numSplits; ++j)
3740 {
3741 // Set the size of the views.
3742 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3743 {
3744 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3745 }
3746 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3747 }
3748
James Ward58dec6b2020-09-11 17:32:44 +01003749 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003750 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003751 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003752
3753 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003754 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003755
Nina Drozd0324f482019-04-08 10:52:10 +01003756 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3757 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003758 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003759 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003760 }
3761
3762 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3763 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3764}
3765
Derek Lambertif0176992020-04-28 13:37:49 +01003766unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3767{
3768 int numDims = armnn::numeric_cast<int>(numDimsIn);
3769 int v = idx < 0 ? numDims + idx : idx;
3770 ARMNN_ASSERT(v >= 0);
3771 ARMNN_ASSERT(v < numDims);
3772
3773 return static_cast<unsigned int>(v);
3774}
3775
Kevin May7d96b162021-02-03 17:38:41 +00003776void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003777{
3778 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3779
Mike Kelly0d77ae12022-01-07 17:42:27 +00003780 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3781 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003782
3783 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3784 CHECK_VALID_SIZE(inputs.size(), 3);
3785
3786 auto& inputTensor = inputs[0];
3787 auto& splitsTensor = inputs[1];
3788 auto& axisTensor = inputs[2];
3789
3790 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3791 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3792 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3793 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3794
3795 // Inputs
3796 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3797 if (inputDimSize > MaxNumOfTensorDimensions)
3798 {
3799 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003800 fmt::format("The number of dimensions: {} for input tensors of the "
3801 "SplitV op cannot be greater than {} {}",
3802 inputTensorInfo.GetNumDimensions(),
3803 MaxNumOfTensorDimensions,
3804 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003805 }
3806
3807 // Get split axis
3808 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003809 if (axisBufferPtr == nullptr)
3810 {
3811 throw ParseException(
3812 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3813 CHECK_LOCATION().AsString()));
3814 }
3815
Derek Lambertif0176992020-04-28 13:37:49 +01003816 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3817 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003818 int32_t axis = axisData[0];
3819
3820 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3821 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3822 {
3823 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3824 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3825 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3826 throw ParseException(
3827 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3828 axis,
3829 CHECK_LOCATION().AsString()));
3830 }
3831 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003832
Derek Lambertif0176992020-04-28 13:37:49 +01003833 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003834 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003835 unsigned int numSplits{0};
3836
3837 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003838 {
3839 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003840 }
3841 else
3842 {
Ryan OShea86704732020-05-26 11:41:04 +01003843 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003844 }
3845
3846 if (numSplits <=0)
3847 {
3848 throw ParseException("SplitV has invalid number of splits");
3849 }
3850
Jan Eilersc0761e92020-06-29 16:48:44 +01003851 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003852 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003853 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003854
Jan Eilersc0761e92020-06-29 16:48:44 +01003855 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003856 int numInferred{0};
3857 unsigned int inferIdx{0};
3858 int splitSum{0};
3859 for (auto split : splitsData)
3860 {
3861 if (split < 0)
3862 {
3863 numInferred++;
3864 inferIdx = idx;
3865 }
3866 else
3867 {
3868 splitSum += split;
3869 }
3870 idx++;
3871 }
3872 // Check for inferred Axis
3873 if (numInferred == 0)
3874 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003875 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003876 {
3877 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3878 }
3879 }
3880 else if (numInferred == 1)
3881 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003882 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003883 }
3884 else
3885 {
3886 throw ParseException("Cannot infer split size for more than one split");
3887 }
3888
Derek Lambertif0176992020-04-28 13:37:49 +01003889 //Ouput size validation
3890 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3891 CHECK_VALID_SIZE(outputs.size(), numSplits);
3892
3893 // Setup Armnn descriptor
3894 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3895 unsigned int accumSplit = 0;
3896 for (unsigned int j = 0; j < numSplits; ++j)
3897 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003898 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003899
3900 // Set the size of the views.
3901 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3902 {
3903 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3904 if (dimIdx == splitDim)
3905 {
3906 dimSize = splitSize;
3907 }
3908 splitDesc.SetViewSize(j, dimIdx, dimSize);
3909 }
3910
3911 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3912 accumSplit += splitSize;
3913 }
3914
James Ward58dec6b2020-09-11 17:32:44 +01003915 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003916 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003917 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003918
3919 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3920 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3921
3922 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3923 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003924 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003925 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3926 }
3927
3928 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3929 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3930}
3931
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003932void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3933{
3934 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3935}
3936
Kevin May7d96b162021-02-03 17:38:41 +00003937void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003938{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003939 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3940}
3941
3942void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3943{
Inki Daed4619e22020-09-10 15:33:54 +09003944 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3945 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3946 CHECK_VALID_SIZE(inputs.size(), 2);
3947
3948 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3949 CHECK_VALID_SIZE(outputs.size(), 1);
3950
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003951 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3952 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003953 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003954 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003955
3956 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003957 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3958 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3959 {
3960 throw ParseException(
3961 fmt::format(
3962 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3963 CHECK_LOCATION().AsString()));
3964 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003965
3966 // Get const axis value from model and set it to descriptor.
3967 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3968 if (axisBufferPtr == nullptr)
3969 {
3970 throw ParseException(
3971 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3972 CHECK_LOCATION().AsString()));
3973 }
3974
3975 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3976 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3977 int32_t axis = axisData.front();
3978
3979 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3980 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3981 {
3982 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3983 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3984 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3985 throw ParseException(
3986 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3987 axis,
3988 CHECK_LOCATION().AsString()));
3989 }
3990
3991 ArgMinMaxDescriptor desc;
3992 desc.m_Axis = axis;
3993 desc.m_Function = argMinMaxFunction;
3994
3995 // Register a ArgMin/ArgMax layer.
3996 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3997 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3998 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3999 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09004000 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4001
4002 // Register input tensor to the layer.
4003 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4004 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4005
4006 // Register output tensor to the layer.
4007 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4008 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4009}
4010
Kevin May7d96b162021-02-03 17:38:41 +00004011void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004012{
4013 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4014
Kevin May7d96b162021-02-03 17:38:41 +00004015 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004016 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00004017 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004018 CHECK_VALID_SIZE(outputs.size(), 1);
4019
4020 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4021 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
4022 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4023
4024 armnn::GatherDescriptor gatherDescriptor;
4025
Mike Kelly0d77ae12022-01-07 17:42:27 +00004026 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4027 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004028 auto axis = options->axis;
4029
4030 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4031 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4032 auto outputDimensions = outputTensorInfo.GetNumDimensions();
4033 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4034 {
4035 throw ParseException(
4036 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4037 axis,
4038 inputDimensions, inputDimensions,
4039 CHECK_LOCATION().AsString()));
4040 }
4041 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4042 {
4043 throw ParseException(
4044 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4045 outputDimensions,
4046 inputDimensions, indicesDimensions,
4047 CHECK_LOCATION().AsString()));
4048 }
4049
4050 gatherDescriptor.m_Axis = axis;
4051
4052 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4053 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4054 ARMNN_ASSERT(layer != nullptr);
4055 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4056
4057 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4058 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4059
4060 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4061 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4062}
4063
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004064void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4065{
4066 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4067
4068 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4069 CHECK_VALID_SIZE(inputs.size(), 2);
4070 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4071 CHECK_VALID_SIZE(outputs.size(), 1);
4072
4073 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4074 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
4075 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4076
4077 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4078 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4079 ARMNN_ASSERT(layer != nullptr);
4080 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4081
4082 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4083 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4084
4085 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4086 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4087}
4088
Kevin May7d96b162021-02-03 17:38:41 +00004089void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004090{
4091 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4092
Kevin May7d96b162021-02-03 17:38:41 +00004093 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004094 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004095 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004096 CHECK_VALID_SIZE(outputs.size(), 1);
4097
4098 armnn::DepthToSpaceDescriptor descriptor;
4099
Mike Kelly0d77ae12022-01-07 17:42:27 +00004100 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4101 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004102 auto blockSize = options->block_size;
4103 if (blockSize < 2)
4104 {
4105 throw ParseException(
4106 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4107 blockSize,
4108 CHECK_LOCATION().AsString()));
4109 }
4110 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4111
4112 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4113 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4114 ARMNN_ASSERT(layer != nullptr);
4115 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4116 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4117
4118 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4119 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4120
4121 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4122 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4123}
4124
Kevin May7d96b162021-02-03 17:38:41 +00004125void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004126{
Sadik Armagana2747482021-02-09 10:28:54 +00004127 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4128}
4129
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004130void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4131{
4132 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4133}
4134
Sadik Armagana2747482021-02-09 10:28:54 +00004135void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4136{
4137 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4138}
4139
4140void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4141{
4142 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4143}
4144
4145void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4146{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004147 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4148
Mike Kelly0d77ae12022-01-07 17:42:27 +00004149 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4150 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004151
4152 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4153 CHECK_VALID_SIZE(inputs.size(), 2);
4154
4155 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4156 CHECK_VALID_SIZE(outputs.size(), 1);
4157
Sadik Armagana2747482021-02-09 10:28:54 +00004158 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004159
4160 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4161 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004162
4163 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004164 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4165 // Get const axis value from model and set it to descriptor.
4166 if (axisBufferPtr != nullptr)
4167 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004168 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4169 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4170
4171 // Convert the axis to unsigned int and remove duplicates.
4172 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4173 std::set<unsigned int> uniqueAxis;
4174 std::transform(axisData.begin(),
4175 axisData.end(),
4176 std::inserter(uniqueAxis, uniqueAxis.begin()),
4177 [rank](int i)->unsigned int{
4178 return static_cast<uint32_t>(((i + rank) % rank)); });
4179 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004180 }
Sadik Armagana2747482021-02-09 10:28:54 +00004181 else
4182 {
4183 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4184 {
4185 desc.m_vAxis.push_back(i);
4186 }
4187 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004188
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004189 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004190 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004191
4192 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004193 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004194
4195 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4196 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4197
4198 // Register input tensor to the layer.
4199 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4200 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4201
4202 // Register output tensor to the layer.
4203 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4204 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4205}
4206
Mike Kelly31dce2b2021-09-01 21:22:37 +01004207void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4208{
4209 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4210
4211 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4212 CHECK_VALID_SIZE(inputs.size(), 1);
4213
4214 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4215 CHECK_VALID_SIZE(outputs.size(), 1);
4216
4217 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4218 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4219
4220 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4221
4222 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4223 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4224
4225 armnn::NormalizationDescriptor descriptor;
4226 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4227 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4228 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4229 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4230 descriptor.m_K = options->bias;
4231 descriptor.m_Alpha = options->alpha;
4232 descriptor.m_Beta = options->beta;
4233
4234 // ArmNN expects normSize to be the full size of the normalization
4235 // window rather than the radius as in TfLite.
4236 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4237
4238 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4239 ARMNN_ASSERT(layer != nullptr);
4240
4241 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4242 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4243
4244 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4245 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4246
4247 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4248 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4249}
4250
Teresa Charlin8b0bee12022-07-12 11:18:44 +01004251void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4252{
4253 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4254}
4255
4256void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4257{
4258 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4259}
4260
4261void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
4262{
4263 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
4264}
4265
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004266void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4267{
4268 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4269}
4270
4271void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4272{
4273 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4274}
4275
4276void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4277{
4278 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4279}
4280
Teresa Charlin8b0bee12022-07-12 11:18:44 +01004281void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
4282{
4283 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
4284}
4285
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004286void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4287{
4288 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4289}
4290
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004291void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4292{
4293 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4294
4295 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4296 CHECK_VALID_SIZE(inputs.size(), 1);
4297
4298 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4299 CHECK_VALID_SIZE(outputs.size(), 1);
4300
4301 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4302 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4303
4304 ElementwiseUnaryDescriptor desc;
4305 desc.m_Operation = unaryOperation;
4306 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4307 ARMNN_ASSERT(layer != nullptr);
4308
4309 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4310 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4311
4312 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4313 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4314
4315 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4316 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4317}
4318
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004319void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4320{
4321 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4322}
4323
4324void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4325{
4326 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4327}
4328
4329void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4330{
4331 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4332}
4333
4334void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4335{
4336 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4337}
4338
4339void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4340{
4341 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4342}
4343
4344void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4345{
4346 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4347}
4348
4349void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4350 ComparisonOperation comparisonOperation)
4351{
4352 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4353
4354 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4355 CHECK_VALID_SIZE(inputs.size(), 2);
4356
4357 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4358 CHECK_VALID_SIZE(outputs.size(), 1);
4359
4360 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4361 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4362
4363 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4364 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4365 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4366
4367 ComparisonDescriptor desc;
4368 desc.m_Operation = comparisonOperation;
4369 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4370 ARMNN_ASSERT(layer != nullptr);
4371
4372 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4373 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4374
4375 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4376 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4377
4378 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4379 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4380}
4381
Kevin May7d96b162021-02-03 17:38:41 +00004382armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4383 unsigned int outputSlot,
4384 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004385{
4386 ActivationDescriptor activationDesc;
4387 std::string layerName = prevLayer->GetName();
4388
4389 switch(activationType)
4390 {
4391 case tflite::ActivationFunctionType_NONE:
4392 {
4393 // this is a no-op: return previous layer
4394 return prevLayer;
4395 }
4396 case tflite::ActivationFunctionType_RELU:
4397 {
4398 activationDesc.m_Function = ActivationFunction::ReLu;
4399 layerName += ":RELU";
4400 break;
4401 }
4402 case tflite::ActivationFunctionType_RELU6:
4403 {
4404 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4405 activationDesc.m_A = 6.0f;
4406 activationDesc.m_B = 0.0f;
4407 layerName += ":RELU6";
4408 break;
4409 }
4410 case tflite::ActivationFunctionType_TANH:
4411 {
4412 activationDesc.m_Function = ActivationFunction::TanH;
4413 activationDesc.m_A = 1.0f;
4414 activationDesc.m_B = 1.0f;
4415 layerName += ":TANH";
4416 break;
4417 }
4418
4419 // I only put these here as a reminder what others we could support
4420 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4421 case tflite::ActivationFunctionType_SIGN_BIT:
4422 default:
4423 {
4424 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004425 fmt::format("TfLite parser doesn't suppport fused activation: "
4426 "{}/{} {} ",
4427 activationType,
4428 tflite::EnumNameActivationFunctionType(activationType),
4429 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004430
4431 }
4432 }
4433
4434 IConnectableLayer* activationLayer =
4435 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4436
4437 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4438 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4439 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4440 return activationLayer;
4441}
4442
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004443armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4444 unsigned int outputSlot)
4445{
Teresa Charlin725728e2022-05-05 13:33:33 +01004446
4447 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4448 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4449
4450 if (dataType == DataType::Signed32)
4451 {
4452 return prevLayer;
4453 }
4454
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004455 std::string layerName = prevLayer->GetName();
4456 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4457
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004458 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4459 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004460
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004461 return floorLayer;
4462}
4463
Mike Kelly0d77ae12022-01-07 17:42:27 +00004464TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004465{
4466 if (fileName == nullptr)
4467 {
James Ward58dec6b2020-09-11 17:32:44 +01004468 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004469 CHECK_LOCATION().AsString()));
4470 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004471 std::error_code errorCode;
4472 fs::path pathToFile(fileName);
4473 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004474 {
James Ward58dec6b2020-09-11 17:32:44 +01004475 //fmt::format() could not be used here (format error)
4476 std::stringstream msg;
4477 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4478 << " " << CHECK_LOCATION().AsString();
4479
4480 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004481 }
4482 std::ifstream file(fileName, std::ios::binary);
4483 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4484 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4485 fileContent.size());
4486}
4487
Mike Kelly0d77ae12022-01-07 17:42:27 +00004488TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004489{
4490 if (binaryContent == nullptr)
4491 {
James Ward58dec6b2020-09-11 17:32:44 +01004492 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004493 CHECK_LOCATION().AsString()));
4494 }
4495 flatbuffers::Verifier verifier(binaryContent, len);
4496 if (verifier.VerifyBuffer<tflite::Model>() == false)
4497 {
4498 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004499 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4500 "flatbuffers format. size:{} {}",
4501 len,
4502 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004503 }
4504 return tflite::UnPackModel(binaryContent);
4505}
4506
Mike Kelly0d77ae12022-01-07 17:42:27 +00004507TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004508 size_t subgraphIndex,
4509 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004510{
4511 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4512
Mike Kelly0d77ae12022-01-07 17:42:27 +00004513 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4514 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004515
4516 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004517 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004518 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004519 {
mathad01c21025d2021-04-26 10:09:37 +01004520 // If the input location is -1 then assume input is turned off.
4521 if (operatorPtr->inputs[i] == -1)
4522 {
4523 continue;
4524 }
4525 else
4526 {
4527 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4528 result.push_back(subgraphPtr->tensors[inputId].get());
4529 }
telsoa01c577f2c2018-08-31 09:22:23 +01004530 }
4531 return result;
4532}
4533
Mike Kelly0d77ae12022-01-07 17:42:27 +00004534TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004535 size_t subgraphIndex,
4536 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004537{
4538 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4539
Mike Kelly0d77ae12022-01-07 17:42:27 +00004540 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4541 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004542
4543 size_t outputCount = operatorPtr->outputs.size();
4544 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004545 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004546 {
4547 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4548 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004549 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004550 }
4551 return result;
4552}
4553
Mike Kelly0d77ae12022-01-07 17:42:27 +00004554TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004555 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004556{
4557 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004558 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004559
Derek Lambertiff05cc52019-04-26 13:05:17 +01004560 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004561 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004562 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004563 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004564 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004565 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004566 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004567 }
4568 return result;
4569}
4570
Mike Kelly0d77ae12022-01-07 17:42:27 +00004571TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004572 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004573{
4574 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004575 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004576
Derek Lambertiff05cc52019-04-26 13:05:17 +01004577 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004578 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004579 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004580 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004581 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4582 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004583 }
4584 return result;
4585}
4586
Kevin May7d96b162021-02-03 17:38:41 +00004587std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4588 size_t subgraphIndex,
4589 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004590{
4591 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004592 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4593 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004594 return operatorPtr->inputs;
4595}
4596
Kevin May7d96b162021-02-03 17:38:41 +00004597std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4598 size_t subgraphIndex,
4599 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004600{
4601 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004602 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4603 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004604 return operatorPtr->outputs;
4605}
4606
Kevin May7d96b162021-02-03 17:38:41 +00004607void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4608 size_t operatorIndex,
4609 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004610 const std::vector<unsigned int>& tensorIndexes,
4611 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004612{
4613 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004614 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004615
Finn Williamsd4fa5452021-03-01 12:31:41 +00004616 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004617 {
4618 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004619 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4620 " for subgraph:{} operator index:{} {}",
4621 tensorIndexes.size(),
4622 layer->GetNumInputSlots(),
4623 subgraphIndex,
4624 operatorIndex,
4625 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004626 }
4627
Finn Williamsd4fa5452021-03-01 12:31:41 +00004628 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004629 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004630 unsigned int tensorIndex = tensorIndexes[index];
4631 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004632 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4633 }
4634}
4635
Kevin May7d96b162021-02-03 17:38:41 +00004636void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4637 size_t operatorIndex,
4638 IConnectableLayer* layer,
4639 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004640{
4641 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004642 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004643 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4644 {
4645 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004646 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4647 " for subgraph:{} operator index:{} {}",
4648 tensorIndexes.size(),
4649 layer->GetNumOutputSlots(),
4650 subgraphIndex,
4651 operatorIndex,
4652 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004653 }
4654
4655 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4656 {
4657 unsigned int tensorIndex = tensorIndexes[slotIndex];
4658 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4659 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4660 }
4661}
4662
Kevin May7d96b162021-02-03 17:38:41 +00004663void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004664{
4665 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4666
4667 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004668 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004669 {
4670 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4671 IConnectableLayer* layer =
4672 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4673
4674 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4675 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4676
4677 RegisterOutputSlots(subgraphIndex,
4678 VIRTUAL_OPERATOR_ID,
4679 layer,
4680 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4681 }
4682}
4683
Kevin May7d96b162021-02-03 17:38:41 +00004684void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004685{
4686 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4687
4688 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004689 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004690 {
4691 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4692 IConnectableLayer* layer =
4693 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4694
4695 RegisterInputSlots(subgraphIndex,
4696 VIRTUAL_OPERATOR_ID,
4697 layer,
4698 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4699 }
4700}
4701
Mike Kelly5880b912022-01-28 16:18:54 +00004702void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004703{
Mike Kelly5880b912022-01-28 16:18:54 +00004704 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004705
Mike Kelly5880b912022-01-28 16:18:54 +00004706 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004707 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4708 {
4709 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4710 {
4711 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4712 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4713 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004714 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004715
Mike Kelly5880b912022-01-28 16:18:54 +00004716 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004717 {
4718 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004719 armnn::DataType dataType = tensorInfo.GetDataType();
4720
4721 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4722 != m_ConstantsToDequantize.end())
4723 {
4724 dataType = DataType::Float32;
4725 }
4726 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4727
4728 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4729 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4730
4731 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4732 RegisterOutputSlots(subgraphIndex,
4733 VIRTUAL_OPERATOR_ID,
4734 layer,
4735 { tensorIndex });
4736 }
4737 else if (ShouldConstantTensorBeCreated(tensorIndex))
4738 {
4739 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4740 armnn::DataType dataType = tensorInfo.GetDataType();
4741
4742 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4743 != m_ConstantsToDequantize.end())
4744 {
4745 dataType = DataType::Float32;
4746 }
4747 // Make sure isConstant flag is set.
4748 tensorInfo.SetConstant();
4749 tensorInfo.SetDataType(dataType);
4750
4751 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004752
Matthew Sloyan81beae32021-07-13 19:46:11 +01004753 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004754 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004755
Matthew Sloyan81beae32021-07-13 19:46:11 +01004756 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4757 RegisterOutputSlots(subgraphIndex,
4758 VIRTUAL_OPERATOR_ID,
4759 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004760 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004761 }
4762 else
4763 {
4764 throw ParseException(
4765 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4766 CHECK_LOCATION().AsString()));
4767 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004768 }
4769 }
4770 }
4771}
4772
telsoa01c577f2c2018-08-31 09:22:23 +01004773// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004774TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004775{
4776 CHECK_BUFFER(model, bufferIndex);
4777 return model->buffers[bufferIndex].get();
4778}
4779
Matteo Martincigh747ef822018-12-18 09:26:39 +00004780template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004781std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4782TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4783 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004784 armnn::TensorInfo& tensorInfo,
4785 armnn::Optional<armnn::PermutationVector&> permutationVector)
4786{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004787 // Make sure isConstant flag is set.
4788 tensorInfo.SetConstant();
4789
Matteo Martincigh747ef822018-12-18 09:26:39 +00004790 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4791 tensorPtr,
4792 tensorInfo,
4793 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004794 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004795 return std::make_pair(constData.first, std::move(storage));
4796}
4797
Mike Kelly5880b912022-01-28 16:18:54 +00004798bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4799{
4800 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4801 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4802 != m_ConstantsToBeCreated.end());
4803}
4804
Finn Williamsd4fa5452021-03-01 12:31:41 +00004805bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4806{
4807 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004808 bool isConst = true;
4809
4810 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4811 if (buffer->data.size() == 0)
4812 {
4813 isConst = false;
4814 }
4815
4816 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004817}
4818
Kevin May7d96b162021-02-03 17:38:41 +00004819std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004820TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4821 armnn::TensorInfo& tensorInfo,
4822 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004823{
4824 CHECK_TENSOR_PTR(tensorPtr);
4825 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4826 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4827
Matthew Sloyan81beae32021-07-13 19:46:11 +01004828 // Make sure isConstant flag is set.
4829 tensorInfo.SetConstant();
4830
telsoa01c577f2c2018-08-31 09:22:23 +01004831 switch (tensorInfo.GetDataType())
4832 {
4833 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004834 return CreateConstTensorAndStoreData<float>(bufferPtr,
4835 tensorPtr,
4836 tensorInfo,
4837 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004838 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004839 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4840 tensorPtr,
4841 tensorInfo,
4842 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004843 case armnn::DataType::QSymmS8:
4844 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4845 tensorPtr,
4846 tensorInfo,
4847 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004848 case armnn::DataType::QAsymmS8:
4849 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4850 tensorPtr,
4851 tensorInfo,
4852 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004853 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004854 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4855 tensorPtr,
4856 tensorInfo,
4857 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004858 default:
4859 {
4860 std::stringstream errString;
4861 errString << "Unexpected datatype when creating const tensor: "
4862 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4863 << " shape:" << tensorInfo.GetShape()
4864 << CHECK_LOCATION().AsString();
4865 throw ParseException(errString.str());
4866 }
4867 }
4868}
4869
Finn Williamsd4fa5452021-03-01 12:31:41 +00004870armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4871 armnn::TensorInfo& tensorInfo)
4872{
4873 CHECK_TENSOR_PTR(tensorPtr);
4874 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4875 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4876
Matthew Sloyan81beae32021-07-13 19:46:11 +01004877 // Make sure isConstant flag is set.
4878 tensorInfo.SetConstant();
4879
Finn Williamsd4fa5452021-03-01 12:31:41 +00004880 return ConstTensor(tensorInfo, bufferPtr->data.data());
4881}
4882
Mike Kelly5880b912022-01-28 16:18:54 +00004883std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4884TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4885 armnn::TensorInfo& tensorInfo,
4886 armnn::DataType inputDataType)
4887{
4888 CHECK_TENSOR_PTR(tensorPtr);
4889 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4890 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4891
4892 // Make sure isConstant flag is set.
4893 tensorInfo.SetConstant();
4894
4895 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4896 {
4897 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4898 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4899 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4900 }
4901 else
4902 {
4903 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4904 }
4905}
4906
4907std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4908TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4909{
4910 CHECK_TENSOR_PTR(tensorPtr);
4911 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4912 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4913 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4914
4915 // Make sure isConstant flag is set.
4916 tensorInfo.SetConstant();
4917
4918 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4919 {
4920 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4921 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4922 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4923 }
4924 else
4925 {
4926 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4927 }
4928}
4929
Kevin May7d96b162021-02-03 17:38:41 +00004930BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4931 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004932{
4933 CHECK_SUBGRAPH(m_Model, subgraphId);
4934 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004935 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004936 {
4937 if (input.second->name == name)
4938 {
4939 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004940 auto inputTensorInfo = ToTensorInfo(input.second);
4941 // Input tensors are always treated as constant tensors during network execution.
4942 inputTensorInfo.SetConstant(true);
4943 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004944 }
4945 }
4946
4947 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004948 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004949 {
4950 bindings << "'" << input.second->name << "' ";
4951 }
4952
4953 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004954 fmt::format("No input binding found for subgraph:{} and name:{}. "
4955 "Possible inputs are: [{}] {}",
4956 subgraphId,
4957 name,
4958 bindings.str(),
4959 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004960}
4961
Kevin May7d96b162021-02-03 17:38:41 +00004962BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4963 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004964{
4965 CHECK_SUBGRAPH(m_Model, subgraphId);
4966 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004967 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004968 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004969 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004970 if (output.second->name == name)
4971 {
4972 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004973 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4974 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4975 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004976 }
4977 }
4978
4979 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004980 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004981 {
4982 bindings << "'" << output.second->name << "' ";
4983 }
4984
4985 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004986 fmt::format("No output binding found for subgraph:{} and name:{}. "
4987 "Possible outputs are: [{}] {}",
4988 subgraphId,
4989 name,
4990 bindings.str(),
4991 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004992}
4993
Kevin May7d96b162021-02-03 17:38:41 +00004994size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004995{
4996 return m_Model->subgraphs.size();
4997}
4998
Kevin May7d96b162021-02-03 17:38:41 +00004999std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005000{
5001 CHECK_SUBGRAPH(m_Model, subgraphId);
5002 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5003 std::vector<std::string> result;
5004 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005005 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005006 {
5007 result.push_back(input.second->name);
5008 }
5009 return result;
5010}
5011
Kevin May7d96b162021-02-03 17:38:41 +00005012std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005013{
5014 CHECK_SUBGRAPH(m_Model, subgraphId);
5015 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5016 std::vector<std::string> result;
5017 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005018 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005019 {
5020 result.push_back(output.second->name);
5021 }
5022 return result;
5023}
5024
Matthew Sloyanac001ee2021-02-03 10:43:04 +00005025const std::string TfLiteParserImpl::GetVersion()
5026{
5027 return TFLITE_PARSER_VERSION;
5028}
5029
Mike Kelly0d77ae12022-01-07 17:42:27 +00005030TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005031: m_FloatData(std::move(data))
5032, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005033, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005034, m_Int32Data(nullptr)
5035{
5036}
5037
Mike Kelly0d77ae12022-01-07 17:42:27 +00005038TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005039: m_FloatData(nullptr)
5040, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00005041, m_Int8Data(nullptr)
5042, m_Int32Data(nullptr)
5043{
5044}
5045
Mike Kelly0d77ae12022-01-07 17:42:27 +00005046TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00005047: m_FloatData(nullptr)
5048, m_Uint8Data(nullptr)
5049, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01005050, m_Int32Data(nullptr)
5051{
5052}
5053
Mike Kelly0d77ae12022-01-07 17:42:27 +00005054TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005055: m_FloatData(nullptr)
5056, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005057, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005058, m_Int32Data(std::move(data))
5059{
5060}
5061
5062} // armnnTfLiteParser