blob: 7cb9f6a7bcbdeb2a1bc58343c49eb6a9b098017c [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Mike Kellyc5789ca2020-07-06 19:24:15 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
Matteo Martincighe011d202019-11-28 11:35:47 +00005
telsoa01c577f2c2018-08-31 09:22:23 +01006#include "TfLiteParser.hpp"
7
Matthew Sloyanac001ee2021-02-03 10:43:04 +00008#include "armnnTfLiteParser/Version.hpp"
Mike Kelly5880b912022-01-28 16:18:54 +00009#include "armnn/LstmParams.hpp"
Matthew Sloyanac001ee2021-02-03 10:43:04 +000010
Sadik Armagand109a4d2020-07-28 10:42:13 +010011#include <armnn/BackendOptions.hpp>
Matthew Bentham39ef3e52020-01-20 10:09:09 +000012#include <armnn/Descriptors.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010013#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000014#include <armnn/Logging.hpp>
James Conroy05102392020-06-24 15:39:55 +010015#include <armnn/Tensor.hpp>
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +000016#include <armnnUtils/TensorUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010017#include <armnn/TypesUtils.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010018#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000019#include <armnn/utility/IgnoreUnused.hpp>
Derek Lambertif0176992020-04-28 13:37:49 +010020#include <armnn/utility/NumericCast.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010021
22// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000023#include <armnnUtils/Permute.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010024#include <armnnUtils/Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000025
Sadik Armagan479045b2018-10-01 11:51:37 +010026#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010027#include <VerificationHelpers.hpp>
28
29// The generated code based on the Tf Lite schema:
30#include <schema_generated.h>
31
Matteo Martincighe011d202019-11-28 11:35:47 +000032#include <flatbuffers/flexbuffers.h>
33
James Ward58dec6b2020-09-11 17:32:44 +010034#include <fmt/format.h>
telsoa01c577f2c2018-08-31 09:22:23 +010035
telsoa01c577f2c2018-08-31 09:22:23 +010036#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000037#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010038#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010039#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000040
41#define ARMNN_THROW_PARSE_EXCEPTION(msg) \
42 { \
43 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
44 << ": " \
45 << CHECK_LOCATION().AsString()).str()); \
46 }
telsoa01c577f2c2018-08-31 09:22:23 +010047
48using namespace armnn;
49using armnn::CheckLocation;
50namespace armnnTfLiteParser
51{
Kevin May7d96b162021-02-03 17:38:41 +000052
53ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
54 pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
55
56ITfLiteParser::~ITfLiteParser() = default;
57
58ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
59{
60 return new ITfLiteParser(options);
61}
62
63ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
64{
65 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
66}
67
68void ITfLiteParser::Destroy(ITfLiteParser* parser)
69{
70 delete parser;
71}
72
73armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
74{
75 return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
76}
77
Mike Kelly0d77ae12022-01-07 17:42:27 +000078armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
Kevin May7d96b162021-02-03 17:38:41 +000079{
80 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
81}
82
83BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
84 const std::string& name) const
85{
86 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
87}
88
89BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
90 const std::string& name) const
91{
92 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
93}
94
95size_t ITfLiteParser::GetSubgraphCount() const
96{
97 return pTfLiteParserImpl->GetSubgraphCount();
98}
99
100std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
101{
102 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
103}
104
105std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
106{
107 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
108}
109
telsoa01c577f2c2018-08-31 09:22:23 +0100110namespace
111{
jimfly01c25411c2018-11-14 17:47:22 +0000112
telsoa01c577f2c2018-08-31 09:22:23 +0100113const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
114
Mike Kelly0d77ae12022-01-07 17:42:27 +0000115void CheckSubgraph(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100116 size_t subgraphIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000117 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100118{
119 if (model.get() == nullptr)
120 {
121 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100122 fmt::format("{} was called with invalid (null) model. "
123 "Possible reason is that the model is not yet loaded and Unpack(ed). "
124 "subgraph:{} at {}",
125 location.m_Function,
126 subgraphIndex,
127 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100128 }
129 else if (subgraphIndex >= model->subgraphs.size())
130 {
131 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100132 fmt::format("{} was called with an invalid subgraph index. "
133 "subgraph:{} at {}",
134 location.m_Function,
135 subgraphIndex,
136 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100137 }
138}
139
140#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
141 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
142
Mike Kelly0d77ae12022-01-07 17:42:27 +0000143void CheckModel(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100144 size_t subgraphIndex,
145 size_t operatorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000146 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100147{
148 if (model.get() == nullptr)
149 {
150 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100151 fmt::format("{} was called with invalid (null) model. "
152 "Possible reason is that the model is not yet loaded and Unpack(ed). "
153 "subgraph:{} operator:{} at {}",
154 location.m_Function,
155 subgraphIndex,
156 operatorIndex,
157 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100158 }
159 else if (subgraphIndex >= model->subgraphs.size())
160 {
161 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100162 fmt::format("{} was called with an invalid subgraph index. "
163 "subgraph:{} operator:{} at {}",
164 location.m_Function,
165 subgraphIndex,
166 operatorIndex,
167 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100168 }
169 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170 operatorIndex != VIRTUAL_OPERATOR_ID)
171 {
172 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100173 fmt::format("{} was called with an invalid operator index. "
174 "subgraph:{} operator:{} at {}",
175 location.m_Function,
176 subgraphIndex,
177 operatorIndex,
178 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100179 }
180}
181
182#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
183 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
184
Mike Kelly0d77ae12022-01-07 17:42:27 +0000185void CheckTensor(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100186 size_t subgraphIndex,
187 size_t tensorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000188 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100189{
190 // not checking model, because I assume CHECK_MODEL already run
191 // and checked that. An assert would do.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100192 ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
telsoa01c577f2c2018-08-31 09:22:23 +0100193
194 // also subgraph index should be checked by CHECK_MODEL so
195 // I only add an assert here
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100196 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
telsoa01c577f2c2018-08-31 09:22:23 +0100197
198 // the tensor index is the only one to check here
199 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
200 {
201 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100202 fmt::format("{} was called with an invalid tensor index. "
203 "subgraph:{} tensor:{} at {}",
204 location.m_Function,
205 subgraphIndex,
206 tensorIndex,
207 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100208 }
209}
210
211#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
212 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
213
Kevin May7d96b162021-02-03 17:38:41 +0000214void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000215 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100216{
217 if (rawPtr == nullptr)
218 {
219 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100220 fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100221 }
222}
223
224#define CHECK_TENSOR_PTR(TENSOR_PTR) \
225 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
226
Mike Kelly0d77ae12022-01-07 17:42:27 +0000227void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100228 size_t bufferIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000229 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100230{
231 if (model.get() == nullptr)
232 {
233 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100234 fmt::format("{} was called with invalid (null) model. "
235 "Possible reason is that the model is not yet loaded and Unpack(ed). "
236 "buffer:{} at {}",
237 location.m_Function,
238 bufferIndex,
239 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100240 }
241 else if (bufferIndex >= model->buffers.size())
242 {
243 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100244 fmt::format("{} was called with an invalid buffer index. "
245 "buffer index:{} at {}",
246 location.m_Function,
247 bufferIndex,
248 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100249 }
250 else if (model->buffers[bufferIndex].get() == nullptr)
251 {
252 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100253 fmt::format("The buffer #{} is null. {}",
254 bufferIndex,
255 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100256 }
257}
258
259#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
260 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
261
Kevin May7d96b162021-02-03 17:38:41 +0000262void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000263 const armnn::TensorInfo& tensorInfo,
telsoa01c577f2c2018-08-31 09:22:23 +0100264 uint32_t bufferId,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000265 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100266{
267 if (bufferPtr == nullptr)
268 {
269 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100270 fmt::format("BufferPtr is null for buffer:{}. {}",
271 bufferId,
272 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100273 }
274 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
275 tensorInfo.GetNumBytes() > bufferPtr->data.size())
276 {
277 std::stringstream ss;
278 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
279 << "For tensor: " << tensorInfo.GetShape()
280 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
281 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
282 throw ParseException(ss.str());
283 }
284}
285
Mike Kelly0d77ae12022-01-07 17:42:27 +0000286
287tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
288{
289 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
290 auto opcodeIndex = operatorPtr->opcode_index;
291
292// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
293#if defined(ARMNN_POST_TFLITE_2_3)
294 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
295 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
296#else
297 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
298#endif
299 return opcode;
300}
301
302std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
303 const TfLiteParserImpl::ModelPtr& model,
304 size_t bufferIndex)
305{
306 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
307 std::vector<unsigned int> buffer(info.GetNumElements());
308
309 if (info.GetDataType() == DataType::Signed32)
310 {
311 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
312 }
313 else if (info.GetDataType() == DataType::Signed64)
314 {
315 std::vector<uint64_t> uint64Buffer(info.GetNumElements());
316 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
317 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
318 }
319 return buffer;
320}
321
telsoa01c577f2c2018-08-31 09:22:23 +0100322#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
323 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
324
325bool IsActivationSupported(tflite::ActivationFunctionType activationType)
326{
327 switch(activationType)
328 {
329 case tflite::ActivationFunctionType_NONE:
330 case tflite::ActivationFunctionType_RELU:
331 case tflite::ActivationFunctionType_RELU6:
332 case tflite::ActivationFunctionType_TANH:
333 {
334 return true;
335 }
336 default:
337 {
338 return false;
339 }
340 }
341}
342
343#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
344 do { \
345 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
346 { \
347 throw ParseException( \
James Ward58dec6b2020-09-11 17:32:44 +0100348 fmt::format("TfLite parser doesn't suppport fused activation: " \
349 "{}/{} in {} subgraph:{} operator:{} at {}", \
350 OPTION->fused_activation_function, \
351 tflite::EnumNameActivationFunctionType(\
352 OPTION->fused_activation_function), \
353 __func__, \
354 SUBGRAPH_INDEX, \
355 OPERATOR_INDEX, \
356 CHECK_LOCATION().FileLine())); \
telsoa01c577f2c2018-08-31 09:22:23 +0100357 } \
358 } while(false)
359
360
Mike Kelly0d77ae12022-01-07 17:42:27 +0000361std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t>& in)
telsoa01c577f2c2018-08-31 09:22:23 +0100362{
363 std::vector<unsigned int> result;
364 result.reserve(in.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +0000365 for (auto& i : in)
telsoa01c577f2c2018-08-31 09:22:23 +0100366 {
mathad01c21025d2021-04-26 10:09:37 +0100367 // If the location of the input data is -1 then the input should be ignored.
368 if (i == -1)
369 {
370 continue;
371 }
telsoa01c577f2c2018-08-31 09:22:23 +0100372 result.push_back(CHECKED_NON_NEGATIVE(i));
373 }
374 return result;
375}
376
Mike Kelly5880b912022-01-28 16:18:54 +0000377bool IsOptionalOperandPresent(int input)
378{
379 return (input >= 0);
380}
381
telsoa01c577f2c2018-08-31 09:22:23 +0100382void CalcPadding(uint32_t inputSize,
383 uint32_t filterSize,
384 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100385 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100386 uint32_t& paddingFront,
387 uint32_t& paddingBack,
388 tflite::Padding padding)
389{
390 paddingFront = 0;
391 paddingBack = 0;
392 if (padding == tflite::Padding_SAME)
393 {
394 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100395 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100397 if (temp > inputSize)
398 {
399 paddingFront = (temp - inputSize) / 2;
400 paddingBack = (temp - inputSize) - paddingFront;
401 }
402 }
403}
404
Kevin May7d96b162021-02-03 17:38:41 +0000405armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Finn Williamsb49ed182021-06-29 15:50:08 +0100406 const std::vector<unsigned int>& shape,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100407 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100408{
409 armnn::DataType type;
410 CHECK_TENSOR_PTR(tensorPtr);
411
412 switch (tensorPtr->type)
413 {
414 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000415 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100416 break;
417 case tflite::TensorType_FLOAT32:
418 type = armnn::DataType::Float32;
419 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000420 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000421 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000422 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000423 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000424 type = armnn::DataType::QAsymmS8;
425 }
426 else
427 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000428 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000429 type = armnn::DataType::QSymmS8;
430 }
Finn Williamsed66d142019-12-06 09:55:55 +0000431 break;
432 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000433 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000434 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100435 case tflite::TensorType_INT32:
436 type = armnn::DataType::Signed32;
437 break;
Inki Daed4619e22020-09-10 15:33:54 +0900438 case tflite::TensorType_INT64:
439 type = armnn::DataType::Signed64;
440 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100441 case tflite::TensorType_BOOL:
442 type = armnn::DataType::Boolean;
443 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100444 default:
445 {
446 CheckLocation location = CHECK_LOCATION();
447 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100448 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
449 tensorPtr->type,
450 tflite::EnumNameTensorType(tensorPtr->type),
451 tensorPtr->name,
452 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100453 }
454 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100455 TensorShape tensorShape;
456
457 std::vector<unsigned int> safeShape = shape;
458 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100459 {
460 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100461 }
462
463 if (!outputTensor)
464 {
465 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
466 }
467 else
468 {
Rob Hughesd812a312021-08-06 13:10:53 +0100469 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100470
471 // If a shape signature exists we will use that to infer dynamic tensors
472 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100473 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100474 // If the shape is incompatible with the shape signature override the shape
475 if (shapeSignatureSize != shape.size())
476 {
477 safeShape = {};
478
479 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
480 {
481 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
482 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
483 safeShape.push_back(dim);
484 }
485 }
486
Rob Hughesd812a312021-08-06 13:10:53 +0100487 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Finn Williamsb49ed182021-06-29 15:50:08 +0100488 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
489 {
490 dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false : true;
491 }
Rob Hughesd812a312021-08-06 13:10:53 +0100492 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100493 }
494 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
495 else if (shape.size() == 0)
496 {
497 tensorShape = TensorShape(1, false);
498 }
499 else
500 {
501 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100502 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100503 }
504
Keith Davisd305e1a2020-01-22 11:57:54 +0000505 float quantizationScale = 0.0f;
506 int32_t quantizationOffset = 0;
507
508 if (tensorPtr->quantization.get())
509 {
510 if (tensorPtr->quantization->scale.size() <= 1)
511 {
512 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
513 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
514
515 if (tensorPtr->quantization->scale.size() == 1)
516 {
517 quantizationScale = tensorPtr->quantization->scale[0];
518 }
519 if (tensorPtr->quantization->zero_point.size() == 1)
520 {
521 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000522 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100523 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000524 }
525
Sadik Armagand109a4d2020-07-28 10:42:13 +0100526 armnn::TensorInfo result(tensorShape,
527 type,
528 quantizationScale,
529 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000530 return result;
531 }
532 else
533 {
534 std::vector<float> quantizationScales;
535 std::vector<int32_t> quantizationOffsets;
536
537 // Scale
538 std::copy(tensorPtr->quantization->scale.begin(),
539 tensorPtr->quantization->scale.end(),
540 std::back_inserter(quantizationScales));
541
Keith Davis0c2eeac2020-02-11 16:51:50 +0000542 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100543 armnn::TensorInfo result(tensorShape,
544 type,
545 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100546 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000547 return result;
548 }
549 }
550 else
551 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100552 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000553 type,
554 quantizationScale,
555 quantizationOffset);
556 return result;
557 }
telsoa01c577f2c2018-08-31 09:22:23 +0100558}
559
Jan Eilers7612bd62021-04-06 17:29:03 +0100560armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr)
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000561{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000562 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100563 return ToTensorInfo(tensorPtr, dimensions);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000564}
565
Kevin May7d96b162021-02-03 17:38:41 +0000566armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100567 const bool outputTensor)
568{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000569 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100570 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100571}
572
telsoa01c577f2c2018-08-31 09:22:23 +0100573template<typename T>
574std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000575CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
576 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000577 armnn::TensorInfo& tensorInfo,
578 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100579{
Jan Eilers8eb25602020-03-09 12:13:48 +0000580 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100581 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
582 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100583 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100584
585 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000586
587 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
588 {
589 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000590 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
591 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000592 }
593 else
594 {
595 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
596 }
597
Matthew Sloyan81beae32021-07-13 19:46:11 +0100598 // Make sure isConstant flag is set.
599 tensorInfo.SetConstant();
600
telsoa01c577f2c2018-08-31 09:22:23 +0100601 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
602}
603
telsoa01c577f2c2018-08-31 09:22:23 +0100604armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
605{
606 // generate the binding id by shifting the tensor id by 8 bit
607 // and add the subgraph id, which allows 256 subgraphs
608 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
609}
610
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000611bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
612{
613 const unsigned int actualSize = actual.GetNumDimensions();
614 if (actualSize != expected.size())
615 {
616 return false;
617 }
618
619 for (unsigned int i = 0u; i < actualSize; i++)
620 {
621 if (expected[i] < 0 ||
622 actual[i] != static_cast<unsigned int>(expected[i]))
623 {
624 return false;
625 }
626 }
627
628 return true;
629}
630
James Conroy05102392020-06-24 15:39:55 +0100631void CheckMatchingQuantization(const TensorInfo& first,
632 const TensorInfo& second,
633 const std::string& descName,
634 std::string const& firstName,
635 std::string const& secondName)
636{
637 if (!first.IsQuantized() ||
638 !second.IsQuantized())
639 {
640 // Not a quantized type, ignore the validation
641 return;
642 }
643
644 DataType firstDataType = first.GetDataType();
645 DataType secondDataType = second.GetDataType();
646
647 if (firstDataType != secondDataType)
648 {
649 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
650 " must be of the same quantized type, " +
651 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
652 secondName + " is " + GetDataTypeName(secondDataType));
653 }
654
655 if (!first.IsTypeSpaceMatch(second))
656 {
657 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
658 " must have the same quantization space, " +
659 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
660 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
661 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
662 " and scale " + std::to_string(second.GetQuantizationScale()));
663 }
664}
665
telsoa01c577f2c2018-08-31 09:22:23 +0100666} // <anonymous>
667
Kevin May7d96b162021-02-03 17:38:41 +0000668TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100669: m_Options(options)
670, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000671, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100672{
673 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100674 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000675 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100676 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
677 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000678 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
679 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
mathad01b392e982021-04-07 12:07:30 +0100680 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000681 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
682 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100683 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
684 #if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100685 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100686 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000687 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
688 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
689 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
690 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100691 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000692 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300693 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000694 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100695 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000696 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000697 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
698 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin91a53ea2022-04-25 15:47:29 +0100699 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300700 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
701 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000702 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
703 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300704 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
705 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Mike Kelly31dce2b2021-09-01 21:22:37 +0100706 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
707 = &TfLiteParserImpl::ParseLocalResponseNormalization;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100708 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000709 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
710 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
711 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
712 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
713 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
714 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100715 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000716 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
717 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300718 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000719 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
720 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000721 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100722 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000723 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
724 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
725 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000726 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
727 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100728 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000729 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
730 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
731 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100732 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100733 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100734 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Kevin May7d96b162021-02-03 17:38:41 +0000735 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
736 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
737 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
738 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
739 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
740 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
741 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
742 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
743 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
744 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
745 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
746 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000747 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
748 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000749 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100750
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100751 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000752 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100753}
754
Kevin May7d96b162021-02-03 17:38:41 +0000755void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100756{
757 m_Network = armnn::INetworkPtr(nullptr, nullptr);
758 m_Model = nullptr;
759 m_SubgraphConnections.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000760 m_OverridenOutputShapes.clear();
761 m_ConstantsToDequantize.clear();
762 m_ConstantsToBeCreated.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100763}
764
Kevin May7d96b162021-02-03 17:38:41 +0000765INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100766{
767 ResetParser();
768 m_Model = LoadModelFromFile(graphFile);
769 return CreateNetworkFromModel();
770}
771
Mike Kelly0d77ae12022-01-07 17:42:27 +0000772INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100773{
774 ResetParser();
775 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
776 return CreateNetworkFromModel();
777}
778
Finn Williamsb49ed182021-06-29 15:50:08 +0100779
780armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
781{
782 ResetParser();
783 m_Model = std::move(model);
784
785 return CreateNetworkFromModel();
786}
787
Kevin May7d96b162021-02-03 17:38:41 +0000788INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100789{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100790
791 using NetworkOptions = std::vector<BackendOptions>;
792 NetworkOptions networkOptions = {};
793 if (m_Options && m_Options.value().m_InferAndValidate)
794 {
795 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
796 {
797 { "InferAndValidate", true }
798 });
799
800 networkOptions.push_back(shapeInferenceMethodOption);
801 }
802
803 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100804 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100805
telsoa01c577f2c2018-08-31 09:22:23 +0100806 if (m_Model->subgraphs.size() != 1)
807 {
808 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100809 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
810 m_Model->subgraphs.size(),
811 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100812 }
813
814 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100815 size_t operatorIndex = 0;
816 try
telsoa01c577f2c2018-08-31 09:22:23 +0100817 {
Colm Donelan6350d272020-06-09 16:56:25 +0100818 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100819 {
Colm Donelan6350d272020-06-09 16:56:25 +0100820 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
821 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100822 {
Colm Donelan6350d272020-06-09 16:56:25 +0100823 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100824
825// 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 +0100826#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100827 auto builtinCode = std::max(opCodePtr->builtin_code,
828 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
829#else
telsoa01c577f2c2018-08-31 09:22:23 +0100830 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100831#endif
telsoa01c577f2c2018-08-31 09:22:23 +0100832
833 if (builtinCode > tflite::BuiltinOperator_MAX)
834 {
James Ward58dec6b2020-09-11 17:32:44 +0100835 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
836 "subgraph:{} operator idx:{}. {}",
837 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
838 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100839 }
840
841 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100842 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100843 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100844 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100845 }
telsoa01c577f2c2018-08-31 09:22:23 +0100846
Colm Donelan6350d272020-06-09 16:56:25 +0100847 SetupInputLayers(subgraphIndex);
848 SetupOutputLayers(subgraphIndex);
849 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100850
Colm Donelan6350d272020-06-09 16:56:25 +0100851 ++subgraphIndex;
852 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100853 }
telsoa01c577f2c2018-08-31 09:22:23 +0100854 }
Colm Donelan6350d272020-06-09 16:56:25 +0100855 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100856 {
Colm Donelan6350d272020-06-09 16:56:25 +0100857 std::stringstream errorString;
858 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
859 << subgraphIndex << " error: " << e.what();
860 ARMNN_LOG(error) << errorString.str();
861 std::stringstream errors;
862 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100863 throw ParseException(errors.str());
864 }
865
866 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100867 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100868 {
869 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
870 {
871 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
872 {
873 for (size_t inputSlotIdx = 0;
874 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
875 ++inputSlotIdx)
876 {
877 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
878 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
879 }
880 }
881 }
882 }
telsoa01c577f2c2018-08-31 09:22:23 +0100883 return std::move(m_Network);
884}
885
Mike Kelly5880b912022-01-28 16:18:54 +0000886std::unique_ptr<float[]> AsFloatArray(TfLiteParserImpl::BufferRawPtr bufferPtr,
887 const TensorInfo& tensorInfo)
888{
889 if (tensorInfo.GetDataType() == DataType::QAsymmS8 || tensorInfo.GetDataType() == DataType::QSymmS8 ||
890 tensorInfo.GetDataType() == DataType::QAsymmU8)
891 {
892 std::unique_ptr<float[]> buffer(new float[tensorInfo.GetNumElements()]);
893
894 if (tensorInfo.HasPerAxisQuantization())
895 {
896 unsigned int axis = tensorInfo.GetQuantizationDim().value();
897 auto axisDimensionality = tensorInfo.GetShape()[axis];
898 auto axisFactor = armnnUtils::GetNumElementsAfter(tensorInfo.GetShape(), axis);
899
900 for (unsigned int i = 0; i < tensorInfo.GetNumDimensions(); ++i)
901 {
902 unsigned int axisIndex = (i / axisFactor) % axisDimensionality;
903 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScales()[axisIndex],
904 tensorInfo.GetQuantizationOffset());
905 }
906 }
907 else
908 {
909 for (unsigned int i = 0; i < tensorInfo.GetNumElements(); ++i)
910 {
911 buffer[i] = Dequantize<int8_t>(bufferPtr->data[i], tensorInfo.GetQuantizationScale(),
912 tensorInfo.GetQuantizationOffset());
913 }
914 }
915 return buffer;
916 }
917 throw ParseException(
918 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
919 GetDataTypeName(DataType::Float32),
920 GetDataTypeName(tensorInfo.GetDataType()),
921 CHECK_LOCATION().AsString()));
922}
923
Kevin May7d96b162021-02-03 17:38:41 +0000924void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
925 size_t tensorIndex,
926 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100927{
928 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100929 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
930 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100931
932 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
933
934 // assuming there is only one producer for that tensor
935 if (tensorSlots.outputSlot != nullptr)
936 {
James Ward58dec6b2020-09-11 17:32:44 +0100937 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
938 "subgraph:{} tensor:{} {}",
939 subgraphIndex,
940 tensorIndex,
941 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100942 }
943
944 tensorSlots.outputSlot = slot;
945}
946
Kevin May7d96b162021-02-03 17:38:41 +0000947void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
948 size_t tensorIndex,
949 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100950{
951 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100952 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
953 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100954
Finn Williamsd4fa5452021-03-01 12:31:41 +0000955 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100956 tensorSlots.inputSlots.push_back(slot);
957}
958
Kevin May7d96b162021-02-03 17:38:41 +0000959void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100960{
961 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
962
963 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000964 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100965
966 // Identify custom code defined for custom operator
967 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
968 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
969
970 // Find parser function that correspondes to custom code (if any)
971 auto iterator = m_CustomParserFunctions.find(customCode);
972 if (iterator != m_CustomParserFunctions.end())
973 {
974 customParserFunction = iterator->second;
975 }
976
977 // Run parser function
978 (this->*customParserFunction)(subgraphIndex, operatorIndex);
979}
980
Kevin May7d96b162021-02-03 17:38:41 +0000981void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100982{
983 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100984
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100985 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
986
987 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +0100988
989// 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 +0100990#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +0100991 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
992 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
993#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100994 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +0100995#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100996
997 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
998 {
999 // Do not add StandInLayer, throw ParseException instead
1000 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001001 fmt::format("Operator not supported. "
1002 "subgraph:{} operator:{} "
1003 "opcode_index:{} opcode:{} / {} {}",
1004 subgraphIndex,
1005 operatorIndex,
1006 opcodeIndex,
1007 opcode,
1008 tflite::EnumNameBuiltinOperator(opcode),
1009 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001010 }
1011
1012 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1013 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1014
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001015 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1016 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001017
1018 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001019 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001020
1021 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1022 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001023 ARMNN_ASSERT(layer != nullptr);
1024
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001025 for (unsigned int i = 0u; i < numOutputs; ++i)
1026 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01001027 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001028 }
1029
1030 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1031 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1032
1033 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1034 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001035}
1036
mathad01b392e982021-04-07 12:07:30 +01001037void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1038{
1039 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1040
1041 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1042 CHECK_VALID_SIZE(inputs.size(), 1);
1043 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1044 CHECK_VALID_SIZE(outputs.size(), 1);
1045
1046 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1047
1048 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1049 ARMNN_ASSERT(layer != nullptr);
1050
1051 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1052 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1053
1054 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1055 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1056
1057 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1058 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1059}
1060
Kevin May7d96b162021-02-03 17:38:41 +00001061void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001062{
1063 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1064
Mike Kelly0d77ae12022-01-07 17:42:27 +00001065 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1066 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001067
1068 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1069
1070 Convolution2dDescriptor desc;
1071 desc.m_BiasEnabled = false;
1072 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1073 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001074 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001075 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1076 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001077
telsoa01c577f2c2018-08-31 09:22:23 +01001078 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1079 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1080
1081 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1082 CHECK_VALID_SIZE(outputs.size(), 1);
1083
1084 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1085 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1086
1087 // assuming input is NHWC
1088 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1089 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1090
1091 // assuming the filter is OHWI : Output, H, W, Input
1092 // which is essentially the same as NHWC
1093 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1094 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1095
Pablo Tellof0bd6832019-04-26 17:58:13 +01001096 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1097 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1098 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1099 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001100
Mike Kelly5880b912022-01-28 16:18:54 +00001101 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001102 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +01001103
James Ward58dec6b2020-09-11 17:32:44 +01001104 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001105
1106 if (inputs.size() == 3)
1107 {
1108 desc.m_BiasEnabled = true;
1109 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00001110 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo, inputTensorInfo.GetDataType());
telsoa01c577f2c2018-08-31 09:22:23 +01001111 layer = m_Network->AddConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001112 filterTensorAndData.first,
1113 Optional<ConstTensor>(biasTensorAndData.first),
telsoa01c577f2c2018-08-31 09:22:23 +01001114 layerName.c_str());
1115 }
1116 else
1117 {
1118 layer = m_Network->AddConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001119 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001120 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001121 layerName.c_str());
1122 }
1123
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001124 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001125
Sadik Armagand109a4d2020-07-28 10:42:13 +01001126 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001127 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001128
1129 // register the input connection slots for the layer, connections are made after all layers have been created
1130 // only the tensors for the inputs are relevant, exclude the const tensors
1131 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001132 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +01001133
jimfly01c25411c2018-11-14 17:47:22 +00001134 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001135 // register the output connection slots for the layer, connections are made after all layers have been created
1136 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1137 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1138}
1139
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001140// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
1141#if defined(ARMNN_POST_TFLITE_2_3)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001142void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1143{
1144 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1145
1146 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1147 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1148
1149 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1150
1151 Convolution3dDescriptor desc;
1152 desc.m_BiasEnabled = false;
1153 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1154 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1155 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1156 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1157 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1158 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1159 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1160
1161 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1162 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1163
1164 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1165 CHECK_VALID_SIZE(outputs.size(), 1);
1166
1167 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1168 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1169
1170 // Assuming input is NDHWC
1171 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1172 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1173 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1174
1175 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1176 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1177 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1178 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1179
1180 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001181 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001182 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1183 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1184 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1185 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1186
Mike Kelly5880b912022-01-28 16:18:54 +00001187 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001188
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001189 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1190
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001191 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1192 // Add the first input and weights tensor to the registration list.
1193 // The constant weights will be added by SetupConstantLayers.
1194 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1195
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001196 if (inputs.size() == 3)
1197 {
1198 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001199
1200 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1201 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001202 }
1203
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001204 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001205 ARMNN_ASSERT(layer != nullptr);
1206
1207 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1208 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1209
1210 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001211 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001212
1213 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1214 // Register the output connection slots for the layer, connections are made after all layers have been created
1215 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1216 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1217}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001218#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001219
Kevin May7d96b162021-02-03 17:38:41 +00001220void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001221{
1222 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1223
Mike Kelly0d77ae12022-01-07 17:42:27 +00001224 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1225 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001226
1227 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1228
1229 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001230 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1231 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001232 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001233 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001234
1235 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1236 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001237 if (inputs.size() == 3)
1238 {
1239 desc.m_BiasEnabled = true;
1240 }
1241
telsoa01c577f2c2018-08-31 09:22:23 +01001242 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1243 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001244 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1245 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001246
telsoa01c577f2c2018-08-31 09:22:23 +01001247 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001248 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001249
Matteo Martincigh747ef822018-12-18 09:26:39 +00001250 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001251 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1252 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001253
1254 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001255 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1256 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1257
Pablo Tellof0bd6832019-04-26 17:58:13 +01001258 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1259 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1260 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1261 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001262
Jan Eilers53ef7952021-06-02 12:01:25 +01001263 // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
Cathal Corbett06902652022-04-14 17:55:11 +01001264 auto filterTensor = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
James Ward58dec6b2020-09-11 17:32:44 +01001265 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001266
Cathal Corbett06902652022-04-14 17:55:11 +01001267 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1268 // Add the first input and weights tensor to the registration list.
1269 // The constant weights will be added by SetupConstantLayers.
1270 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1271
1272 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1273
1274 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001275 {
1276 desc.m_BiasEnabled = true;
1277 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Cathal Corbett06902652022-04-14 17:55:11 +01001278 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
1279
1280 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1281 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001282 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001283 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001284
Sadik Armagand109a4d2020-07-28 10:42:13 +01001285 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001286 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001287
1288 // register the input connection slots for the layer, connections are made after all layers have been created
1289 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001290 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001291
jimfly01c25411c2018-11-14 17:47:22 +00001292 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001293 // register the output connection slots for the layer, connections are made after all layers have been created
1294 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1295 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1296}
1297
Kevin May7d96b162021-02-03 17:38:41 +00001298void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001299{
1300 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1301
1302 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1303 CHECK_VALID_SIZE(inputs.size(), 1);
1304
1305 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1306 CHECK_VALID_SIZE(outputs.size(), 1);
1307
James Ward58dec6b2020-09-11 17:32:44 +01001308 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001309
1310 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001311 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001312
Sadik Armagand109a4d2020-07-28 10:42:13 +01001313 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001314 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1315
1316 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1317 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1318
1319 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1320 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1321}
1322
Teresa Charlin3ab85482021-06-08 16:59:29 +01001323void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1324{
1325 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1326
1327 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1328 CHECK_VALID_SIZE(inputs.size(), 2);
1329
1330 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1331 CHECK_VALID_SIZE(outputs.size(), 1);
1332
1333 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1334
1335 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1336 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1337
1338 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1339
1340 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001341
1342 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1343 {
1344 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1345 }
1346 else
1347 {
1348 int32_t axis = inputs[1]->shape[0];
1349
1350 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1351
1352 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1353 {
1354 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1355 }
1356
1357 if(axis < 0)
1358 {
1359 axis = inputDimSize + axis + 1;
1360 }
1361
Rob Hughesd812a312021-08-06 13:10:53 +01001362 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001363 unsigned int inputShapeIndex = 0;
1364 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1365 {
1366 if (i == static_cast<unsigned int>(axis))
1367 {
1368 shape[i] = 1;
1369 }
1370 else
1371 {
1372 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1373 ++inputShapeIndex;
1374 }
1375 }
1376
Rob Hughesd812a312021-08-06 13:10:53 +01001377 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001378 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001379
1380 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1381 ARMNN_ASSERT(layer != nullptr);
1382 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1383
1384 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1385 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1386
1387 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1388 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1389}
1390
Kevin May7d96b162021-02-03 17:38:41 +00001391void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001392{
1393 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1394
1395 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001396 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001397
1398 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1399 CHECK_VALID_SIZE(outputs.size(), 1);
1400
James Ward58dec6b2020-09-11 17:32:44 +01001401 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001402 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001403
josh minorba424d22019-11-13 10:55:17 -06001404 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001405 {
1406 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1407 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001408 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1409 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001410 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001411 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001412
Mike Kelly08759e22020-03-02 11:41:31 +00001413 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001414 }
1415
James Conroy05102392020-06-24 15:39:55 +01001416 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001417 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001418 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001419
James Conroy05102392020-06-24 15:39:55 +01001420 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001421 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001422 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1423
1424 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1425 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1426
1427 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1428 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1429}
1430
Kevin May7d96b162021-02-03 17:38:41 +00001431void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001432{
1433 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1434
Mike Kelly0d77ae12022-01-07 17:42:27 +00001435 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1436 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001437
1438 TransposeConvolution2dDescriptor desc;
1439 desc.m_BiasEnabled = false;
1440 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1441 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1442 desc.m_DataLayout = armnn::DataLayout::NHWC;
1443
1444 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001445 if (inputs.size() == 4)
1446 {
1447 desc.m_BiasEnabled = true;
1448 }
1449 else
1450 {
1451 CHECK_VALID_SIZE(inputs.size(), 3);
1452 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001453
1454 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1455 CHECK_VALID_SIZE(outputs.size(), 1);
1456
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001457 if (inputs[0])
1458 {
1459 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1460 std::vector<int> output_shape(tensorInfo.GetNumElements());
1461 if (tensorInfo.GetDataType() == DataType::Signed32)
1462 {
1463 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1464 }
1465 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1466 {
1467 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1468 {
1469 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1470 }
1471 }
1472 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1473 for (int dimension : output_shape)
1474 {
1475 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1476 }
1477 desc.m_OutputShapeEnabled = true;
1478 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001479 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001480 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1481
1482 // TfLite uses NHWC tensors
1483 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1484 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1485
1486 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1487 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1488
1489 CalcPadding(inputHeight,
1490 filterHeight,
1491 desc.m_StrideY,
1492 1, // DilationY
1493 desc.m_PadTop,
1494 desc.m_PadBottom,
1495 options->padding);
1496
1497 CalcPadding(inputWidth,
1498 filterWidth,
1499 desc.m_StrideX,
1500 1, // DilationX
1501 desc.m_PadLeft,
1502 desc.m_PadRight,
1503 options->padding);
1504
Mike Kelly5880b912022-01-28 16:18:54 +00001505 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001506
1507 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001508 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001509
David Monahan61683802021-01-12 09:11:07 +00001510 if (desc.m_BiasEnabled)
1511 {
1512 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Mike Kelly5880b912022-01-28 16:18:54 +00001513 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001514 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001515 filterTensorAndData.first,
1516 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001517 layerName.c_str());
1518 }
1519 else
1520 {
1521 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001522 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001523 EmptyOptional(),
1524 layerName.c_str());
1525 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001526
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001527 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001528
Sadik Armagand109a4d2020-07-28 10:42:13 +01001529 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001530 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1531
1532 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1533 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001534 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001535
1536 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1537 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1538}
1539
Kevin May7d96b162021-02-03 17:38:41 +00001540void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001541{
1542 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1543}
1544
Kevin May7d96b162021-02-03 17:38:41 +00001545void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001546{
1547 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1548
1549 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1550 CHECK_VALID_SIZE(inputs.size(), 3);
1551
1552 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1553 CHECK_VALID_SIZE(outputs.size(), 1);
1554
1555 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1556 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1557
1558 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1559 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1560
1561 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1562 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1563
1564 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1565 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1566
1567 size_t step = 2;
1568 std::vector<std::pair<unsigned int, unsigned int>> crops;
1569 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1570 {
1571 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1572 }
1573
1574 armnn::BatchToSpaceNdDescriptor desc;
1575 desc.m_BlockShape = blockShape;
1576 desc.m_Crops = crops;
1577 desc.m_DataLayout = armnn::DataLayout::NHWC;
1578
James Ward58dec6b2020-09-11 17:32:44 +01001579 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001580
James Conroy05102392020-06-24 15:39:55 +01001581 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001582 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001583 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1584
1585 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1586 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001587 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1588
1589 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1590 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1591
1592 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1593 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1594}
1595
Kevin May7d96b162021-02-03 17:38:41 +00001596void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001597{
1598 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1599
1600 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1601 CHECK_VALID_SIZE(inputs.size(), 1);
1602
1603 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1604 CHECK_VALID_SIZE(outputs.size(), 1);
1605
1606 L2NormalizationDescriptor desc;
1607 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001608 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001609 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1610
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001611 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001612
Sadik Armagand109a4d2020-07-28 10:42:13 +01001613 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001614 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1615
1616 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1617 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1618
1619 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1620 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1621}
1622
Kevin May7d96b162021-02-03 17:38:41 +00001623void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001624{
1625 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1626}
1627
Kevin May7d96b162021-02-03 17:38:41 +00001628void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001629{
1630 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1631
1632 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1633 CHECK_VALID_SIZE(inputs.size(), 2);
1634
1635 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1636 CHECK_VALID_SIZE(outputs.size(), 1);
1637
James Ward58dec6b2020-09-11 17:32:44 +01001638 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001639
1640 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1641 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1642 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001643
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->AddMaximumLayer(layerName.c_str());
1648 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001649 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1650
1651 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001652 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001653
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::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001659{
1660 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1661
1662 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1663 CHECK_VALID_SIZE(inputs.size(), 2);
1664
1665 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1666 CHECK_VALID_SIZE(outputs.size(), 1);
1667
James Ward58dec6b2020-09-11 17:32:44 +01001668 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001669
1670 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1671 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1672 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001673
Sadik Armagand109a4d2020-07-28 10:42:13 +01001674 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001675 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1676
1677 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1678 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001679 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1680
1681 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001682 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001683
1684 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1685 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1686}
1687
Kevin May7d96b162021-02-03 17:38:41 +00001688void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1689 size_t operatorIndex,
1690 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001691{
1692 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1693
Mike Kelly0d77ae12022-01-07 17:42:27 +00001694 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1695 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001696
1697 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1698
1699 std::string layerName;
1700
1701 switch (algorithm)
1702 {
1703 case PoolingAlgorithm::Average:
1704 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001705 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001706 break;
1707 case PoolingAlgorithm::Max:
1708 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001709 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001710 break;
1711 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001712 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001713 }
1714
1715 Pooling2dDescriptor desc;
1716
1717 desc.m_PoolType = algorithm;
1718 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1719 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1720 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1721 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1722 desc.m_PaddingMethod = PaddingMethod::Exclude;
1723 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001724 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001725
1726 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1727 CHECK_VALID_SIZE(inputs.size(), 1);
1728 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1729
1730 // assuming input is NHWC
1731 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1732 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1733
Pablo Tellof0bd6832019-04-26 17:58:13 +01001734 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1735 desc.m_PadTop, desc.m_PadBottom, options->padding);
1736 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1737 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001738
1739 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1740 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001741
Sadik Armagand109a4d2020-07-28 10:42:13 +01001742 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001743 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1744
1745 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1746 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001747 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001748
1749 // register the input connection slots for the layer, connections are made after all layers have been created
1750 // only the tensors for the inputs are relevant, exclude the const tensors
1751 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001752 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001753
jimfly01c25411c2018-11-14 17:47:22 +00001754 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001755 // register the output connection slots for the layer, connections are made after all layers have been created
1756 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1757 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1758}
1759
Kevin May7d96b162021-02-03 17:38:41 +00001760void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001761{
1762 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1763
1764 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1765 CHECK_VALID_SIZE(inputs.size(), 3);
1766 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1767 CHECK_VALID_SIZE(outputs.size(), 1);
1768
1769 SliceDescriptor desc;
1770
1771 // set begin tensor info for slice descriptor
1772 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1773 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1774
1775 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1776 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1777
1778 // set size tensor info for slice descriptor
1779 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1780 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1781
Mike Kelly7ba84d62021-09-10 15:27:19 +01001782 std::vector<int> signedSize(sizeTensorInfo.GetNumElements());
1783 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
josh minorba424d22019-11-13 10:55:17 -06001784 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly7ba84d62021-09-10 15:27:19 +01001785 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1786
1787 for (unsigned int i = 0; i < signedSize.size(); ++i)
1788 {
1789 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001790
Mike Kelly7ba84d62021-09-10 15:27:19 +01001791 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1792 {
1793 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1794 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1795 signedValue,
1796 inputTensorInfo.GetShape()[i] - begin[i],
1797 CHECK_LOCATION().AsString()));
1798 }
1799
1800 if (signedValue == -1)
1801 {
1802 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
1803 }
1804 else
1805 {
1806 size[i] = static_cast<unsigned int>(signedValue);
1807 }
1808 }
1809
josh minorba424d22019-11-13 10:55:17 -06001810 desc = SliceDescriptor(begin, size);
1811
James Ward58dec6b2020-09-11 17:32:44 +01001812 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001813
Sadik Armagand109a4d2020-07-28 10:42:13 +01001814 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001815 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1816
1817 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001818 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1819
1820 // register the input connection slots for the layer, connections are made after all layers have been created
1821 // only the tensors for the inputs are relevant, exclude the const tensors
1822 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1823 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1824
1825 // register the output connection slots for the layer, connections are made after all layers have been created
1826 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1827 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1828}
1829
Kevin May7d96b162021-02-03 17:38:41 +00001830void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001831{
1832 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00001833 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1834 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001835
1836 SoftmaxDescriptor desc;
1837 desc.m_Beta = options->beta;
1838
1839 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1840 CHECK_VALID_SIZE(inputs.size(), 1);
1841 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1842 CHECK_VALID_SIZE(outputs.size(), 1);
1843
James Ward58dec6b2020-09-11 17:32:44 +01001844 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001845 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1846
Sadik Armagand109a4d2020-07-28 10:42:13 +01001847 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001848 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1849
1850 // register the input connection slots for the layer, connections are made after all layers have been created
1851 // only the tensors for the inputs are relevant, exclude the const tensors
1852 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1853 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1854
1855 // register the output connection slots for the layer, connections are made after all layers have been created
1856 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1857 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1858}
1859
Kevin May7d96b162021-02-03 17:38:41 +00001860void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001861{
1862 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1863
1864 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1865 CHECK_VALID_SIZE(inputs.size(), 3);
1866
1867 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1868 CHECK_VALID_SIZE(outputs.size(), 1);
1869
1870 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1871 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1872
1873 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1874 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1875
1876 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1877 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1878
1879 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1880 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1881
1882 size_t step = 2;
1883 std::vector<std::pair<unsigned int, unsigned int>> padList;
1884 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1885 {
1886 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1887 }
1888
1889 armnn::SpaceToBatchNdDescriptor desc;
1890 desc.m_BlockShape = blockShape;
1891 desc.m_PadList = padList;
1892 desc.m_DataLayout = armnn::DataLayout::NHWC;
1893
James Ward58dec6b2020-09-11 17:32:44 +01001894 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001895
James Conroy05102392020-06-24 15:39:55 +01001896 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001897 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001898 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1899
1900 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1901 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001902 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1903
1904 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1905 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1906
1907 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1908 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1909}
1910
Teresa Charlin3ab85482021-06-08 16:59:29 +01001911armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00001912 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001913{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001914 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001915 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1916
1917 if (inputTensorInfo.GetNumDimensions() > 4)
1918 {
1919 std::stringstream ss;
1920 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1921 << " shape:" << inputTensorInfo.GetShape() << " "
1922 << CHECK_LOCATION().AsString();
1923 throw ParseException(ss.str());
1924 }
1925
1926 if (squeezeDims.empty())
1927 {
1928 squeezeDims.assign(dimensionSequence,
1929 dimensionSequence+inputTensorInfo.GetNumDimensions());
1930 }
1931
1932 std::vector<uint32_t> outputDims;
1933 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1934 {
1935 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1936 auto currentDimension = inputTensorInfo.GetShape()[i];
1937 if (skipSqueeze || currentDimension != 1)
1938 {
1939 outputDims.push_back(currentDimension);
1940 }
1941 }
1942
1943 if (outputDims.size() > 4)
1944 {
1945 std::stringstream ss;
1946 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1947 << " shape:" << inputTensorInfo.GetShape() << " "
1948 << CHECK_LOCATION().AsString();
1949 throw ParseException(ss.str());
1950 }
1951
1952 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1953 outputDims.data());
1954
1955 // we need to preserve the tensor type and the quantization data as well
1956 TensorInfo outTensorInfo = inputTensorInfo;
1957 outTensorInfo.SetShape(outShape);
1958
1959 return outTensorInfo;
1960}
1961
Keith Davis0176fd82021-06-01 17:36:32 +01001962void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
1963{
1964 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1965
1966 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1967 CHECK_VALID_SIZE(inputs.size(), 1);
1968 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1969 CHECK_VALID_SIZE(outputs.size(), 1);
1970
1971 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
1972
1973 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
1974 ARMNN_ASSERT(layer != nullptr);
1975
1976
1977 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1978 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1979
1980 // Check if output tensor type is Signed32 or Signed64
1981 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
1982 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
1983 {
1984 throw ParseException(
1985 fmt::format(
1986 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
1987 CHECK_LOCATION().AsString()));
1988 }
1989
1990 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1991 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1992
1993 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1994 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1995}
1996
Kevin May7d96b162021-02-03 17:38:41 +00001997void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001998{
1999 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2000
2001 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2002 CHECK_VALID_SIZE(inputs.size(), 1);
2003
2004 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2005 CHECK_VALID_SIZE(outputs.size(), 1);
2006
2007 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2008 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002009 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002010
2011 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002012
2013 std::vector<uint32_t> squeezeDim;
2014 // A single negative dim index is interpreted as a negative index in python
2015 // Meaning the index will be the shape size plus the negative index value
2016 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2017 {
2018 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2019 squeezeDim.push_back(static_cast<uint32_t>(dim));
2020 }
2021 else
2022 {
2023 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2024 }
2025
2026 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2027
James Conroy05102392020-06-24 15:39:55 +01002028 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002029
2030 ReshapeDescriptor reshapeDesc;
2031 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2032
telsoa01c577f2c2018-08-31 09:22:23 +01002033 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002034 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002035 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2036
2037 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2038 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2039
2040 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2041 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2042}
2043
Kevin May7d96b162021-02-03 17:38:41 +00002044void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002045{
2046 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2047
2048 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2049 CHECK_VALID_SIZE(inputs.size(), 4);
2050
2051 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2052 CHECK_VALID_SIZE(outputs.size(), 1);
2053
Mike Kelly0d77ae12022-01-07 17:42:27 +00002054 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2055 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002056
2057 StridedSliceDescriptor desc;
2058 desc.m_BeginMask = options->begin_mask;
2059 desc.m_EllipsisMask = options->ellipsis_mask;
2060 desc.m_EndMask = options->end_mask;
2061 desc.m_NewAxisMask = options->new_axis_mask;
2062 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2063 desc.m_DataLayout = armnn::DataLayout::NHWC;
2064
2065 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
2066 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2067
2068 std::vector<int> begin(beginTensorInfo.GetNumElements());
2069 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2070
2071 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
2072 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2073
2074 std::vector<int> end(endTensorInfo.GetNumElements());
2075 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2076
2077 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
2078 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2079
2080 std::vector<int> stride(strideTensorInfo.GetNumElements());
2081 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2082
2083 desc.m_Begin = begin;
2084 desc.m_End = end;
2085 desc.m_Stride = stride;
2086
James Ward58dec6b2020-09-11 17:32:44 +01002087 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002088 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002089 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002090
Sadik Armagand109a4d2020-07-28 10:42:13 +01002091 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002092 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2093
2094 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2095 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2096
2097 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2098 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2099}
2100
Kevin May7d96b162021-02-03 17:38:41 +00002101void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002102{
2103 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2104
Mike Kelly0d77ae12022-01-07 17:42:27 +00002105 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2106 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002107
2108 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2109 CHECK_VALID_SIZE(inputs.size(), 2);
2110
2111 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2112 CHECK_VALID_SIZE(outputs.size(), 1);
2113
2114 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2115 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2116
James Ward58dec6b2020-09-11 17:32:44 +01002117 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002118 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002119 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002120
Sadik Armagand109a4d2020-07-28 10:42:13 +01002121 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002122 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2123
2124 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002125 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002126
2127 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
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::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302134{
2135 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2136
Mike Kelly0d77ae12022-01-07 17:42:27 +00002137 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2138 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302139
2140 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2141 CHECK_VALID_SIZE(inputs.size(), 2);
2142
2143 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2144 CHECK_VALID_SIZE(outputs.size(), 1);
2145
2146 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2147 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2148
James Ward58dec6b2020-09-11 17:32:44 +01002149 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302150 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002151 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302152
Sadik Armagand109a4d2020-07-28 10:42:13 +01002153 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302154 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2155
2156 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002157 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302158 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2159
2160 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2161 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2162}
2163
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002164void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2165{
2166 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2167
2168 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2169 CHECK_VALID_SIZE(inputs.size(), 2);
2170
2171 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2172 CHECK_VALID_SIZE(outputs.size(), 1);
2173
2174 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2175 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2176
2177 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2178 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2179 ARMNN_ASSERT(layer != nullptr);
2180
2181 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2182 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2183
2184 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2185 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2186 layer = AddFusedFloorLayer(layer, 0);
2187
2188 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2189 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2190}
2191
Kevin May7d96b162021-02-03 17:38:41 +00002192void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002193{
2194 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2195
Mike Kelly0d77ae12022-01-07 17:42:27 +00002196 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2197 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002198
2199 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2200 CHECK_VALID_SIZE(inputs.size(), 2);
2201
2202 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2203 CHECK_VALID_SIZE(outputs.size(), 1);
2204
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002205 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2206 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2207
James Ward58dec6b2020-09-11 17:32:44 +01002208 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002209 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002210 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002211
Sadik Armagand109a4d2020-07-28 10:42:13 +01002212 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002213 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2214
2215 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002216 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002217 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2218
2219 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2220 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2221}
2222
Kevin May7d96b162021-02-03 17:38:41 +00002223void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002224{
2225 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2226
Mike Kelly0d77ae12022-01-07 17:42:27 +00002227 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2228 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002229
2230 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2231 CHECK_VALID_SIZE(inputs.size(), 2);
2232
2233 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2234 CHECK_VALID_SIZE(outputs.size(), 1);
2235
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002236 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2237 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
2238
James Ward58dec6b2020-09-11 17:32:44 +01002239 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002240 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002241 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002242
Sadik Armagand109a4d2020-07-28 10:42:13 +01002243 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002244 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2245
2246 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002247 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002248 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2249
2250 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2251 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2252}
2253
Kevin May7d96b162021-02-03 17:38:41 +00002254void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002255{
2256 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2257
2258 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2259
2260 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2261 CHECK_VALID_SIZE(outputs.size(), 1);
2262
2263 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2264 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2265
2266 armnn::MeanDescriptor desc;
2267 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2268 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2269 desc.m_Axis = axis;
2270
2271 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002272 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002273
2274 desc.m_KeepDims =
2275 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2276 true : false;
2277
James Ward58dec6b2020-09-11 17:32:44 +01002278 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002279 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002280 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002281
2282 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2283
2284 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2285 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2286
2287 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2288 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2289}
2290
Kevin May7d96b162021-02-03 17:38:41 +00002291void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002292{
2293 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2294
Kevin May7d96b162021-02-03 17:38:41 +00002295 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002296
Kevin May7d96b162021-02-03 17:38:41 +00002297 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002298 CHECK_VALID_SIZE(outputs.size(), 1);
2299
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002300 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002301 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002302
Mike Kelly0d77ae12022-01-07 17:42:27 +00002303 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002304
2305 size_t step = 2;
2306 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002307 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2308
2309 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002310 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002311 CHECK_VALID_SIZE(inputs.size(), 2);
2312
2313 if (inputTensorInfo.IsQuantized())
2314 {
2315 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2316 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002317 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002318 else if (opcode == tflite::BuiltinOperator_PADV2)
2319 {
2320 CHECK_VALID_SIZE(inputs.size(), 3);
2321
2322 armnn::TensorInfo padValueTensorInfo = ToTensorInfo(inputs[2]);
2323
2324 if (padValueTensorInfo.GetNumElements() != 1)
2325 {
2326 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2327 }
2328 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2329
2330 // Get the pad value from the input tensor
2331 if (padValueBufferPtr->data.size() > 0)
2332 {
2333 switch (padValueTensorInfo.GetDataType())
2334 {
2335 case armnn::DataType::Float32:
2336 {
2337 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2338 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2339 desc.m_PadValue = padValueBuffer[0];
2340 break;
2341 }
2342 case armnn::DataType::QAsymmU8:
2343 {
2344 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2345 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2346 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2347 padValueTensorInfo.GetQuantizationScale(),
2348 padValueTensorInfo.GetQuantizationOffset());
2349 break;
2350 }
2351 case armnn::DataType::QAsymmS8:
2352 case armnn::DataType::QSymmS8:
2353 {
2354 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2355 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2356 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2357 padValueTensorInfo.GetQuantizationScale(),
2358 padValueTensorInfo.GetQuantizationOffset());
2359 break;
2360 }
2361 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2362 }
2363 }
2364 else if (inputTensorInfo.IsQuantized())
2365 {
2366 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2367 }
2368 }
2369
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002370 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2371 {
2372 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2373 }
2374
Mike Kelly0d77ae12022-01-07 17:42:27 +00002375 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2376 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002377 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002378
2379 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2380 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002381 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2382
2383 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2384 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2385
2386 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2387 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2388}
2389
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002390void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2391{
2392 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2393
2394 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2395 CHECK_VALID_SIZE(inputs.size(), 2);
2396
2397 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2398 CHECK_VALID_SIZE(outputs.size(), 1);
2399
2400 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2401
2402 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2403 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2404
2405 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2406 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2407
2408 size_t step = 2;
2409 armnn::PadDescriptor desc;
2410 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2411 {
2412 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2413 }
2414
2415 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2416 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2417
2418 if (options->mode == tflite::MirrorPadMode_REFLECT)
2419 {
2420 desc.m_PaddingMode = PaddingMode::Reflect;
2421 }
2422 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2423 {
2424 desc.m_PaddingMode = PaddingMode::Symmetric;
2425 }
2426 else
2427 {
2428 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2429 }
2430
2431 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2432 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2433 auto inputShape = inputTensorInfo.GetShape();
2434 auto padList = desc.m_PadList;
2435
2436 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2437 for(unsigned int i = 0; i < padList.size(); ++i)
2438 {
2439 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2440 padList.at(i).second > (inputShape[i] - isReflect))
2441 {
2442 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2443 "equal (Symmetric) to the dimension size.");
2444 }
2445 }
2446
2447 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2448 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2449
2450 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2451 ARMNN_ASSERT(layer != nullptr);
2452 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2453
2454 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2455 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2456
2457 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2458 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2459}
2460
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002461void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2462{
2463 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2464
2465 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2466 CHECK_VALID_SIZE(inputs.size(), 2);
2467
2468 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2469 CHECK_VALID_SIZE(outputs.size(), 1);
2470
2471 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2472
2473 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2474 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2475 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2476 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2477
2478 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2479 ARMNN_ASSERT(layer != nullptr);
2480 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2481
2482 if (IsConstTensor(inputs[1]))
2483 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002484 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002485 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2486 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002487
Mike Kelly5880b912022-01-28 16:18:54 +00002488 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2489 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002490 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2491 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002492 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002493 ARMNN_ASSERT(constLayer != nullptr);
2494
2495 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2496 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2497 RegisterOutputSlots(subgraphIndex,
2498 VIRTUAL_OPERATOR_ID,
2499 constLayer,
2500 { inputTensorIndexes[1] });
2501 }
2502 else
2503 {
2504 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2505 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2506 }
2507
2508 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2509 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2510}
2511
Kevin May7d96b162021-02-03 17:38:41 +00002512void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002513{
2514 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2515
2516 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2517 CHECK_VALID_SIZE(inputs.size(), 1);
2518
2519 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2520 CHECK_VALID_SIZE(outputs.size(), 1);
2521
James Ward58dec6b2020-09-11 17:32:44 +01002522 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002523
2524 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002525 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002526
Sadik Armagand109a4d2020-07-28 10:42:13 +01002527 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002528 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2529
2530 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2531 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2532
2533 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2534 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2535}
Finn Williamsc42c3842019-01-22 14:18:11 +00002536
Kevin May7d96b162021-02-03 17:38:41 +00002537void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002538{
Finn Williamsc42c3842019-01-22 14:18:11 +00002539 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002540}
2541
Kevin May7d96b162021-02-03 17:38:41 +00002542void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002543{
Finn Williamsc42c3842019-01-22 14:18:11 +00002544 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2545}
Sadik Armagan58f39192018-09-17 14:14:39 +01002546
Kevin May7d96b162021-02-03 17:38:41 +00002547void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002548{
Jan Eilers2f746b32020-07-28 14:00:06 +01002549 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002550}
2551
Kevin May7d96b162021-02-03 17:38:41 +00002552void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002553{
2554 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2555}
2556
Kevin May7d96b162021-02-03 17:38:41 +00002557void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002558{
2559 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2560}
2561
Kevin May7d96b162021-02-03 17:38:41 +00002562void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002563{
2564 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2565}
2566
Kevin May7d96b162021-02-03 17:38:41 +00002567void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002568{
2569 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2570}
Finn Williamsc42c3842019-01-22 14:18:11 +00002571
Kevin May7d96b162021-02-03 17:38:41 +00002572void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002573{
2574 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002575 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002576 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002577
2578 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2579 CHECK_VALID_SIZE(inputs.size(), 1);
2580
2581 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2582 CHECK_VALID_SIZE(outputs.size(), 1);
2583
James Ward58dec6b2020-09-11 17:32:44 +01002584 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002585 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002586 activationDesc.m_Function = activationType;
2587
2588 switch (activationType)
2589 {
2590 case ActivationFunction::ReLu:
2591 {
James Ward58dec6b2020-09-11 17:32:44 +01002592 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002593 break;
2594 }
2595 case ActivationFunction::BoundedReLu:
2596 {
James Ward58dec6b2020-09-11 17:32:44 +01002597 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002598 activationDesc.m_A = 6.0f;
2599 activationDesc.m_B = 0.0f;
2600 break;
2601 }
2602 case ActivationFunction::Sigmoid:
2603 {
James Ward58dec6b2020-09-11 17:32:44 +01002604 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002605 break;
2606 }
Nina Drozd99851762019-04-09 09:37:38 +01002607 case ActivationFunction::TanH:
2608 {
James Ward58dec6b2020-09-11 17:32:44 +01002609 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002610 activationDesc.m_A = 1.0f;
2611 activationDesc.m_B = 1.0f;
2612 break;
2613 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002614 case ActivationFunction::LeakyReLu:
2615 {
James Ward58dec6b2020-09-11 17:32:44 +01002616 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002617 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002618 activationDesc.m_A = options->alpha;
2619 break;
2620 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002621 case ActivationFunction::Elu:
2622 {
2623 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2624 activationDesc.m_A = 1.0f;
2625 break;
2626 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002627 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002628 {
James Ward58dec6b2020-09-11 17:32:44 +01002629 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002630 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002631 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002632 default:
2633 {
2634 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002635 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2636 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002637 }
2638 }
2639
2640 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002641
Sadik Armagand109a4d2020-07-28 10:42:13 +01002642 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002643 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2644
2645 // register the input connection slots for the layer, connections are made after all layers have been created
2646 // only the tensors for the inputs are relevant, exclude the const tensors
2647 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2648 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2649
2650 // register the output connection slots for the layer, connections are made after all layers have been created
2651 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2652 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2653}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002654armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2655 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002656{
2657 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2658 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2659
2660 if (stretchDim != targetDimsIn.end())
2661 {
2662 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2663 {
2664 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002665 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002666 }
2667
2668 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002669 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002670 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2671
2672 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2673 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2674 }
2675
2676 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2677
2678 TensorInfo reshapeInfo = inputTensorInfo;
2679 reshapeInfo.SetShape(outputShape);
2680
2681 return reshapeInfo;
2682}
2683
Kevin May7d96b162021-02-03 17:38:41 +00002684void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002685{
2686 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2687
2688 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002689
2690 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2691 CHECK_VALID_SIZE(outputs.size(), 1);
2692
Mike Kelly0d77ae12022-01-07 17:42:27 +00002693 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2694 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002695 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002696
2697 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002698 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002699 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002700
Jan Eilersbac9b352020-07-13 13:40:24 +01002701 // Extracting new shape for the output
2702 // There are two ways it can be passed
2703 // * First is to define the target shape in the operator built-in options
2704 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002705 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002706 bool targetShapeFound = false;
2707 // Check if built-in options were given
2708 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002709 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002710 // make sure the parameter is given
2711 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002712 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002713 targetShape = options->new_shape;
2714 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002715 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002716 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002717
2718 // If there is no built-in option given or if the built-in new_shape parameter was empty
2719 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002720 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002721 // Check for a second input tensor
2722 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002723 {
2724 if (inputs[1]->is_variable)
2725 {
2726 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2727 }
2728
2729 if (inputs[1]->shape.size() != 1)
2730 {
2731 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2732 }
2733
2734 if (inputs[1]->type != tflite::TensorType_INT32)
2735 {
2736 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2737 }
2738
Teresa Charlin6a056a42021-12-01 10:25:43 +00002739 // Extract target shape from input
2740 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2741 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00002742 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00002743 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002744 for (int i = 0; i < inputs[1]->shape[0]; ++i)
2745 {
2746 targetShape.push_back(values[i]);
2747 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00002748 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00002749 else
Jan Eilersbac9b352020-07-13 13:40:24 +01002750 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00002751 try
2752 {
2753 // We attempt to infer during Runtime.
2754 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
2755 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
2756 if (reshapeShapes[0] > 2)
2757 {
2758 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
2759 "When inferring during runtime, the parser only supports "
2760 "shape (batch, -1) or (-1) for target shape input.",
2761 reshapeShapes[0],
2762 layerName,
2763 CHECK_LOCATION().AsString()));
2764 }
2765
2766 const int32_t numInputElements = inputTensorInfo.GetNumElements();
2767 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
2768 if (reshapeShapes[0] == 1)
2769 {
2770 targetShape = {numInputElements};
2771 }
2772 else if (reshapeShapes[0] == 2)
2773 {
2774 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
2775 }
2776 }
2777 catch (const std::exception& exc)
2778 {
2779 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
2780 "Reshape operation. Reshape operator target shape input buffer data "
2781 "is null. " << exc.what());
2782 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002783 }
2784 }
2785 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002786 {
2787 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2788 "At least one method required");
2789 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002790 }
2791
kevmay0171972a82018-12-17 14:28:03 +00002792 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002793 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002794
kevmay0171972a82018-12-17 14:28:03 +00002795 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002796 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2797 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002798 {
2799 std::stringstream ss;
2800 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002801 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002802 << " does not equal output shape "
2803 << actualOutputTensorInfo.GetShape()
2804 << ": "
2805 << CHECK_LOCATION().AsString();
2806 throw ParseException(ss.str());
2807 }
2808
Sadikb94967b2018-09-19 15:30:00 +01002809 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002810 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002811
Sadikb94967b2018-09-19 15:30:00 +01002812 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002813 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002814 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002815
2816 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2817 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2818
2819 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2820 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2821}
2822
Kevin May7d96b162021-02-03 17:38:41 +00002823void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002824{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002825 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2826}
2827
Kevin May7d96b162021-02-03 17:38:41 +00002828void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002829{
2830 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2831}
2832
Kevin May7d96b162021-02-03 17:38:41 +00002833void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002834{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002835 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2836
2837 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2838 CHECK_VALID_SIZE(inputs.size(), 2);
2839
2840 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2841 CHECK_VALID_SIZE(outputs.size(), 1);
2842
2843 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2844
2845 // Data for the parsed tensor args (size) must be stored locally.
2846 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2847
2848 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2849 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2850
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002851 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002852 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002853 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002854 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2855 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002856
James Ward58dec6b2020-09-11 17:32:44 +01002857 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002858
2859 switch (resizeMethod)
2860 {
2861 case ResizeMethod::Bilinear:
2862 {
James Ward58dec6b2020-09-11 17:32:44 +01002863 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002864
2865 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2866 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2867
David Monahan4a0c9b92020-05-30 09:48:39 +01002868 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002869 break;
2870 }
2871 case ResizeMethod::NearestNeighbor:
2872 {
James Ward58dec6b2020-09-11 17:32:44 +01002873 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002874 break;
2875 }
2876 default:
2877 {
2878 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002879 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2880 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002881 }
2882 }
2883
James Conroy05102392020-06-24 15:39:55 +01002884 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002885 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002886 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2887
2888 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2889 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002890 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2891
2892 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2893 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2894
2895 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2896 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2897}
2898
Kevin May7d96b162021-02-03 17:38:41 +00002899void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002900{
2901 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2902
Mike Kelly0d77ae12022-01-07 17:42:27 +00002903 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2904 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002905
2906 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2907
2908 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2909 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2910 CHECK_VALID_SIZE(outputs.size(), 1);
2911
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002912 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2913 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002914
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002915 const unsigned int concatDimInput = static_cast<unsigned int>(
2916 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002917
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002918 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2919 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002920
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002921 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002922
2923 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2924 {
2925 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2926
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002927 // This set up concatDescriptor view origin
2928 armnnUtils::ProcessConcatInputTensorInfo(
2929 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002930 }
2931
James Ward58dec6b2020-09-11 17:32:44 +01002932 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002933 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002934
Jim Flynn906f9462019-05-10 13:55:21 +01002935 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002936 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002937 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002938
James Conroy05102392020-06-24 15:39:55 +01002939 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002940 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002941
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002942 // add fused activation layer
2943 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002944
Sadik Armagan479045b2018-10-01 11:51:37 +01002945 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2946 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2947}
2948
Kevin May7d96b162021-02-03 17:38:41 +00002949void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002950{
2951 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2952
Mike Kelly0d77ae12022-01-07 17:42:27 +00002953 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002954 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2955
2956 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2957
2958 FullyConnectedDescriptor desc;
2959 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002960 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002961
2962 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2963 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2964 CHECK_VALID_SIZE(outputs.size(), 1);
2965
2966 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2967
2968 // Fully Connected Layer accepts two dimensional weights input
2969 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2970 if (weightsDimension != 2)
2971 {
2972 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002973 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2974 "Node {}",
2975 weightsDimension,
2976 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002977 }
2978
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002979 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01002980 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002981
Matthew Sloyan81beae32021-07-13 19:46:11 +01002982 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2983 // Add the first input tensor to the registration list
2984 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2985 std::vector<unsigned int> ignoreInputWhenRegister = {};
Mike Kelly5880b912022-01-28 16:18:54 +00002986 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002987
2988 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
2989
Matthew Sloyan81beae32021-07-13 19:46:11 +01002990 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
2991 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002992
Mike Kelly5880b912022-01-28 16:18:54 +00002993 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
2994 (filterTensorInfo.GetDataType() == DataType::QAsymmU8 ||
2995 filterTensorInfo.GetDataType() == DataType::QAsymmS8))
2996 {
2997 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
2998 }
2999
Finn Williamsd4fa5452021-03-01 12:31:41 +00003000 if (inputs.size() == 3)
3001 {
3002 desc.m_BiasEnabled = true;
Mike Kelly5880b912022-01-28 16:18:54 +00003003 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003004
3005 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3006 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003007
3008 if (desc.m_ConstantWeights && inputTensorInfo.GetDataType() == DataType::Float32 &&
3009 (biasTensorInfo.GetDataType() == DataType::QAsymmU8 ||
3010 biasTensorInfo.GetDataType() == DataType::QAsymmS8))
3011 {
3012 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3013 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003014 }
3015
Matthew Sloyan81beae32021-07-13 19:46:11 +01003016 // Filters and biases are always passed to fully connected as inputs
3017 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003018
3019 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003020
Finn Williamsd4fa5452021-03-01 12:31:41 +00003021 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003022 if (inputTensorInfo.GetNumDimensions() > 2)
3023 {
3024 // Add reshape to flatten to 2D [batch_size, input_size],
3025 // where "input_size" corresponds to the number of inputs to the layer,
3026 // matching the second dimension of weights,
3027 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3028 std::vector<unsigned int> reshapedDimensions(2);
3029 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3030 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3031
3032 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3033 {
3034 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003035 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3036 reshapedDimensions[1],
3037 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003038 }
3039
3040 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
3041 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3042
James Ward58dec6b2020-09-11 17:32:44 +01003043 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003044 armnn::ReshapeDescriptor reshapeDescriptor;
3045 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3046 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003047
3048 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3049 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3050
3051 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003052 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3053 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3054 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003055 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003056
3057 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003058
Sadik Armagand109a4d2020-07-28 10:42:13 +01003059 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003060 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3061
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003062 // we need to add the activation layer and fortunately we don't need to care about the data layout
3063 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3064 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003065
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003066 // register the output connection slots for the layer, connections are made after all layers have been created
3067 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3068 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3069}
3070
Kevin May7d96b162021-02-03 17:38:41 +00003071void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003072{
3073 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3074
Mike Kelly0d77ae12022-01-07 17:42:27 +00003075 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003076
3077 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3078 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3079 CHECK_VALID_SIZE(outputs.size(), 4);
3080
3081 // Obtain custom options from flexbuffers
3082 auto custom_options = operatorPtr->custom_options;
3083 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3084
3085 // Obtain descriptor information from tf lite
3086 DetectionPostProcessDescriptor desc;
3087 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3088 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3089 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3090 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3091 desc.m_NumClasses = m["num_classes"].AsUInt32();
3092 desc.m_ScaleH = m["h_scale"].AsFloat();
3093 desc.m_ScaleW = m["w_scale"].AsFloat();
3094 desc.m_ScaleX = m["x_scale"].AsFloat();
3095 desc.m_ScaleY = m["y_scale"].AsFloat();
3096
keidav0107d58c72019-02-26 11:57:39 +00003097 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003098 {
keidav0107d58c72019-02-26 11:57:39 +00003099 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003100 }
3101 if (!(m["detections_per_class"].IsNull()))
3102 {
3103 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3104 }
3105
3106 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3107 {
3108 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3109 "must be positive and less than or equal to 1.");
3110 }
3111
3112 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003113 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003114
James Ward58dec6b2020-09-11 17:32:44 +01003115 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003116 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003117 layerName.c_str());
3118
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003119 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003120
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003121 // The model does not specify the output shapes.
3122 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3123 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3124 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3125 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3126 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
3127 m_OverridenOutputShapes.push_back({ 1 });
3128
keidav011b3e2ea2019-02-21 10:07:37 +00003129 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3130 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003131 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003132 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3133 }
3134
3135 // Register the input connection slots for the layer, connections are made after all layers have been created
3136 // only the tensors for the inputs are relevant, exclude the const tensors
3137 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3138 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3139
3140 // Register the output connection slots for the layer, connections are made after all layers have been created
3141 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3142 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3143 outputTensorIndexes[1],
3144 outputTensorIndexes[2],
3145 outputTensorIndexes[3]});
3146}
3147
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003148/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003149void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003150{
3151 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3152
3153 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3154 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3155 CHECK_VALID_SIZE(outputs.size(), 1);
3156
3157 if (inputs.size() < 1)
3158 {
3159 throw ParseException("Pack must have at least one input.");
3160 }
3161
3162 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3163 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3164
3165 StackDescriptor desc;
3166 desc.m_Axis = static_cast<uint32_t>(options->axis);
3167 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3168
3169 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3170 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3171 desc.m_InputShape = inputTensorInfo.GetShape();
3172
James Ward58dec6b2020-09-11 17:32:44 +01003173 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003174 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3175
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003176 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003177
Sadik Armagand109a4d2020-07-28 10:42:13 +01003178 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003179 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3180
3181 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3182 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3183
3184 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3185 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3186}
3187
Mike Kelly5880b912022-01-28 16:18:54 +00003188void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3189{
3190 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3191
3192 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3193 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3194
3195 if (inputs.size() < 2)
3196 {
3197 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3198 }
3199
3200 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3201 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3202 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3203 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3204 auto inputTensorInfo = ToTensorInfo(inputs[0]);
3205 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3206
3207 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3208 // Please refer to each operand at
3209 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3210 armnn::LstmInputParams params;
3211
3212 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3213 {
3214 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3215 inputTensorInfo).first;
3216 }
3217
3218 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3219 inputTensorInfo).first;
3220 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3221 inputTensorInfo).first;
3222 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3223 inputTensorInfo).first;
3224
3225 // Recurrent weight tensors of size {n_cell, n_output}
3226 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3227 {
3228 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3229 inputTensorInfo).first;
3230 }
3231
3232 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3233 inputTensorInfo).first;
3234 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3235 inputTensorInfo).first;
3236 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3237 inputTensorInfo).first;
3238
3239 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3240 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3241 {
3242 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3243 inputTensorInfo).first;
3244 }
3245
3246 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3247 {
3248 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3249 inputTensorInfo).first;
3250 }
3251
3252 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3253 {
3254 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3255 inputTensorInfo).first;
3256 }
3257
3258 // Gates bias tensors of size {n_cell}
3259 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3260 {
3261 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3262 inputTensorInfo).first;
3263 }
3264
3265 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3266 inputTensorInfo).first;
3267 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3268 inputTensorInfo).first;
3269 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3270 inputTensorInfo).first;
3271
3272 // Projection weight tensor of size {n_output, n_cell}
3273 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3274 {
3275 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3276 inputTensorInfo).first;
3277 }
3278 // Projection bias tensor of size {n_output}
3279 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3280 {
3281 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3282 inputTensorInfo).first;
3283 }
3284
3285 // These state tensors are defined as variable tensors, and will be modified by this op.
3286 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3287 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3288 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3289 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3290
3291 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3292 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3293 {
3294 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3295 inputTensorInfo).first;
3296 }
3297
3298 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3299 {
3300 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3301 inputTensorInfo).first;
3302 }
3303
3304 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3305 {
3306 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3307 inputTensorInfo).first;
3308 }
3309
3310 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3311 {
3312 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3313 inputTensorInfo).first;
3314 }
3315
3316 // set the layer descriptor
3317 armnn::UnidirectionalSequenceLstmDescriptor desc;
3318 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3319 desc.m_ClippingThresCell = nodeParams->cell_clip;
3320 desc.m_ClippingThresProj = nodeParams->proj_clip;
3321 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3322 || params.m_RecurrentToInputWeights == nullptr
3323 || params.m_InputGateBias == nullptr);
3324 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3325 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3326 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3327 || params.m_ForgetLayerNormWeights != nullptr
3328 || params.m_CellLayerNormWeights != nullptr
3329 || params.m_OutputLayerNormWeights != nullptr);
3330 desc.m_TimeMajor = nodeParams->time_major;
3331
3332 if (desc.m_LayerNormEnabled)
3333 {
3334 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3335 inputTensorInfo).first;
3336 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3337 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3338
3339 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3340 inputTensorInfo).first;
3341 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3342 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3343
3344 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3345 inputTensorInfo).first;
3346 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3347 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3348
3349 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3350 inputTensorInfo).first;
3351 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3352 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3353 }
3354 else
3355 {
3356 float defaultIntermediate = std::pow(2, -12);
3357 desc.m_InputIntermediateScale = defaultIntermediate;
3358 desc.m_ForgetIntermediateScale = defaultIntermediate;
3359 desc.m_CellIntermediateScale = defaultIntermediate;
3360 desc.m_OutputIntermediateScale = defaultIntermediate;
3361 }
3362
3363 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3364 inputTensorInfo).first;
3365
3366 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3367 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3368
3369 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3370 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3371 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3372
3373 armnn::DataType dataType = inputTensorInfo.GetDataType();
3374 float qScale = inputTensorInfo.GetQuantizationScale();
3375 float qOffset = inputTensorInfo.GetQuantizationOffset();
3376
3377 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3378 if (!desc.m_CifgEnabled)
3379 {
3380 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3381 }
3382 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3383 cellStateInInfo.GetDataType(),
3384 cellStateInInfo.GetQuantizationScale(),
3385 cellStateInInfo.GetQuantizationOffset());
3386 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3387
3388 armnn::LstmInputParamsInfo paramsInfo;
3389 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3390 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3391 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3392 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3393 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3394 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3395 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3396 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3397 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3398
3399 if (!desc.m_CifgEnabled)
3400 {
3401 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3402 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3403 if (params.m_CellToInputWeights != nullptr)
3404 {
3405 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3406 }
3407 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3408 }
3409
3410 if (desc.m_ProjectionEnabled)
3411 {
3412 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3413 if (params.m_ProjectionBias != nullptr)
3414 {
3415 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3416 }
3417 }
3418
3419 if (desc.m_PeepholeEnabled)
3420 {
3421 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3422 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3423 }
3424
3425 if (desc.m_LayerNormEnabled)
3426 {
3427 if(!desc.m_CifgEnabled)
3428 {
3429 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3430 }
3431 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3432 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3433 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3434 }
3435
3436 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3437 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3438 ARMNN_ASSERT(layer != nullptr);
3439
3440 // register the input connection slots for the layer, connections are made after all layers have been created
3441 // only the tensors for the inputs are relevant, exclude the const tensors
3442 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3443 operatorPtr->inputs[18],
3444 operatorPtr->inputs[19]});
3445 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3446 inputTensorIndexes[1],
3447 inputTensorIndexes[2]});
3448
3449 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3450
3451 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3452 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3453 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3454
3455 unsigned int tensorIndex = outputTensorIndexes[0];
3456 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3457 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3458}
3459
Kevin May7d96b162021-02-03 17:38:41 +00003460void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003461{
3462 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3463
Mike Kelly0d77ae12022-01-07 17:42:27 +00003464 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3465 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003466
3467 // This unpackAxis indicates the axis to unpack
3468 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3469
3470 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3471 CHECK_VALID_SIZE(inputs.size(), 1);
3472
3473 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003474
3475 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3476 {
3477 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003478 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3479 "the number of input dimension {} {}",
3480 unpackAxis,
3481 inputTensorInfo.GetNumDimensions(),
3482 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003483 }
3484
Nina Drozd200e3802019-04-15 09:47:39 +01003485 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3486 // If num is not defined, automatically infer from the length of the dimension axis.
3487 if(unpackNum == 0)
3488 {
3489 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3490 }
3491
3492 // If unpack number cannot be inferred and is still zero, throw ParseException.
3493 if(unpackNum == 0)
3494 {
3495 throw ParseException("Number to unpack must greater than zero.");
3496 }
3497
3498 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3499 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3500
3501 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3502 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3503
3504 // Add current input shape to unpackDimSizes
3505 for (unsigned int i = 0; i < inputDimSize; ++i)
3506 {
3507 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3508 }
3509
3510 if (unpackDimSizes[unpackAxis] != unpackNum)
3511 {
3512 throw ParseException("Number to unpack must be the same as length of the dimension to "
3513 "unpack along.");
3514 }
3515
3516 unpackDimSizes[unpackAxis] /= unpackNum;
3517
3518 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3519 for (unsigned int j = 0; j < unpackNum; ++j)
3520 {
3521 // Set the size of the views.
3522 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3523 {
3524 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3525 }
3526 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3527 }
3528
James Ward58dec6b2020-09-11 17:32:44 +01003529 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003530 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003531 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003532
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003533 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3534 unpackDimSizes.data());
3535
Nina Drozd200e3802019-04-15 09:47:39 +01003536 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3537 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3538
Finn Williamsb49ed182021-06-29 15:50:08 +01003539 std::vector<unsigned int> reshapeDims;
3540 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3541 {
3542 if (axis != unpackAxis)
3543 {
3544 reshapeDims.push_back(splitOutShape[axis]);
3545 }
3546 }
3547
3548 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3549
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003550 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3551 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3552 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003553 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003554 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003555 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003556 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003557 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3558
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003559 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3560 outputTensorInfo.GetDataType(),
3561 outputTensorInfo.GetQuantizationScale(),
3562 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003563 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3564
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003565 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003566
3567 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3568 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3569 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3570 }
Nina Drozd200e3802019-04-15 09:47:39 +01003571}
3572
Kevin May7d96b162021-02-03 17:38:41 +00003573void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003574{
3575 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3576
Mike Kelly0d77ae12022-01-07 17:42:27 +00003577 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3578 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003579
3580 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3581
Nina Drozd200e3802019-04-15 09:47:39 +01003582 // If number of splits cannot be inferred and is zero, throw ParseException.
3583 if(numSplits == 0)
3584 {
3585 throw ParseException("Number to splits must greater than zero.");
3586 }
3587
Nina Drozd0324f482019-04-08 10:52:10 +01003588 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3589 CHECK_VALID_SIZE(inputs.size(), 2);
3590 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3591 CHECK_VALID_SIZE(outputs.size(), numSplits);
3592
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003593 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
3594 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
3595 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003596
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003597 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003598 if (axisBufferPtr == nullptr)
3599 {
3600 throw ParseException(
3601 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3602 CHECK_LOCATION().AsString()));
3603 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003604
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003605 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3606 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3607 int32_t axis = axisData[0];
3608
3609 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3610 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3611 {
3612 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3613 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3614 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3615 throw ParseException(
3616 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3617 axis,
3618 CHECK_LOCATION().AsString()));
3619 }
3620
3621 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003622
Nina Drozd0324f482019-04-08 10:52:10 +01003623 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003624 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003625 {
3626 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003627 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3628 inputTensorInfo.GetNumDimensions(),
3629 MaxNumOfTensorDimensions,
3630 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003631 }
3632
3633 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3634
3635 // Add current input shape to splitterDimSizes
3636 for (unsigned int i = 0; i < inputDimSize; ++i)
3637 {
3638 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3639 }
3640
3641 if (splitterDimSizes[splitDim] % numSplits != 0)
3642 {
3643 throw ParseException("Number of splits must evenly divide the dimension");
3644 }
3645 splitterDimSizes[splitDim] /= numSplits;
3646
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003647 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003648 for (unsigned int j = 0; j < numSplits; ++j)
3649 {
3650 // Set the size of the views.
3651 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3652 {
3653 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3654 }
3655 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3656 }
3657
James Ward58dec6b2020-09-11 17:32:44 +01003658 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003659 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003660 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003661
3662 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003663 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003664
Nina Drozd0324f482019-04-08 10:52:10 +01003665 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3666 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003667 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003668 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003669 }
3670
3671 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3672 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3673}
3674
Derek Lambertif0176992020-04-28 13:37:49 +01003675unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
3676{
3677 int numDims = armnn::numeric_cast<int>(numDimsIn);
3678 int v = idx < 0 ? numDims + idx : idx;
3679 ARMNN_ASSERT(v >= 0);
3680 ARMNN_ASSERT(v < numDims);
3681
3682 return static_cast<unsigned int>(v);
3683}
3684
Kevin May7d96b162021-02-03 17:38:41 +00003685void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01003686{
3687 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3688
Mike Kelly0d77ae12022-01-07 17:42:27 +00003689 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3690 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01003691
3692 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3693 CHECK_VALID_SIZE(inputs.size(), 3);
3694
3695 auto& inputTensor = inputs[0];
3696 auto& splitsTensor = inputs[1];
3697 auto& axisTensor = inputs[2];
3698
3699 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3700 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3701 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3702 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3703
3704 // Inputs
3705 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3706 if (inputDimSize > MaxNumOfTensorDimensions)
3707 {
3708 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003709 fmt::format("The number of dimensions: {} for input tensors of the "
3710 "SplitV op cannot be greater than {} {}",
3711 inputTensorInfo.GetNumDimensions(),
3712 MaxNumOfTensorDimensions,
3713 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003714 }
3715
3716 // Get split axis
3717 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003718 if (axisBufferPtr == nullptr)
3719 {
3720 throw ParseException(
3721 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3722 CHECK_LOCATION().AsString()));
3723 }
3724
Derek Lambertif0176992020-04-28 13:37:49 +01003725 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3726 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003727 int32_t axis = axisData[0];
3728
3729 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3730 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3731 {
3732 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3733 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3734 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3735 throw ParseException(
3736 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3737 axis,
3738 CHECK_LOCATION().AsString()));
3739 }
3740 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003741
Derek Lambertif0176992020-04-28 13:37:49 +01003742 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003743 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003744 unsigned int numSplits{0};
3745
3746 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003747 {
3748 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003749 }
3750 else
3751 {
Ryan OShea86704732020-05-26 11:41:04 +01003752 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003753 }
3754
3755 if (numSplits <=0)
3756 {
3757 throw ParseException("SplitV has invalid number of splits");
3758 }
3759
Jan Eilersc0761e92020-06-29 16:48:44 +01003760 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003761 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003762 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003763
Jan Eilersc0761e92020-06-29 16:48:44 +01003764 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003765 int numInferred{0};
3766 unsigned int inferIdx{0};
3767 int splitSum{0};
3768 for (auto split : splitsData)
3769 {
3770 if (split < 0)
3771 {
3772 numInferred++;
3773 inferIdx = idx;
3774 }
3775 else
3776 {
3777 splitSum += split;
3778 }
3779 idx++;
3780 }
3781 // Check for inferred Axis
3782 if (numInferred == 0)
3783 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003784 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003785 {
3786 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3787 }
3788 }
3789 else if (numInferred == 1)
3790 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003791 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003792 }
3793 else
3794 {
3795 throw ParseException("Cannot infer split size for more than one split");
3796 }
3797
Derek Lambertif0176992020-04-28 13:37:49 +01003798 //Ouput size validation
3799 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3800 CHECK_VALID_SIZE(outputs.size(), numSplits);
3801
3802 // Setup Armnn descriptor
3803 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3804 unsigned int accumSplit = 0;
3805 for (unsigned int j = 0; j < numSplits; ++j)
3806 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003807 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003808
3809 // Set the size of the views.
3810 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3811 {
3812 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3813 if (dimIdx == splitDim)
3814 {
3815 dimSize = splitSize;
3816 }
3817 splitDesc.SetViewSize(j, dimIdx, dimSize);
3818 }
3819
3820 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3821 accumSplit += splitSize;
3822 }
3823
James Ward58dec6b2020-09-11 17:32:44 +01003824 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003825 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003826 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003827
3828 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3829 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3830
3831 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3832 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003833 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003834 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3835 }
3836
3837 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3838 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3839}
3840
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003841void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3842{
3843 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3844}
3845
Kevin May7d96b162021-02-03 17:38:41 +00003846void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003847{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003848 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3849}
3850
3851void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3852{
Inki Daed4619e22020-09-10 15:33:54 +09003853 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3854 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3855 CHECK_VALID_SIZE(inputs.size(), 2);
3856
3857 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3858 CHECK_VALID_SIZE(outputs.size(), 1);
3859
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003860 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3861 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003862 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003863 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003864
3865 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003866 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3867 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3868 {
3869 throw ParseException(
3870 fmt::format(
3871 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3872 CHECK_LOCATION().AsString()));
3873 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003874
3875 // Get const axis value from model and set it to descriptor.
3876 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3877 if (axisBufferPtr == nullptr)
3878 {
3879 throw ParseException(
3880 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3881 CHECK_LOCATION().AsString()));
3882 }
3883
3884 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3885 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3886 int32_t axis = axisData.front();
3887
3888 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3889 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3890 {
3891 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3892 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3893 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3894 throw ParseException(
3895 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3896 axis,
3897 CHECK_LOCATION().AsString()));
3898 }
3899
3900 ArgMinMaxDescriptor desc;
3901 desc.m_Axis = axis;
3902 desc.m_Function = argMinMaxFunction;
3903
3904 // Register a ArgMin/ArgMax layer.
3905 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3906 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3907 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3908 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003909 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3910
3911 // Register input tensor to the layer.
3912 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3913 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3914
3915 // Register output tensor to the layer.
3916 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3917 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3918}
3919
Kevin May7d96b162021-02-03 17:38:41 +00003920void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003921{
3922 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3923
Kevin May7d96b162021-02-03 17:38:41 +00003924 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003925 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003926 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003927 CHECK_VALID_SIZE(outputs.size(), 1);
3928
3929 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3930 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3931 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3932
3933 armnn::GatherDescriptor gatherDescriptor;
3934
Mike Kelly0d77ae12022-01-07 17:42:27 +00003935 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3936 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00003937 auto axis = options->axis;
3938
3939 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3940 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3941 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3942 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3943 {
3944 throw ParseException(
3945 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3946 axis,
3947 inputDimensions, inputDimensions,
3948 CHECK_LOCATION().AsString()));
3949 }
3950 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3951 {
3952 throw ParseException(
3953 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3954 outputDimensions,
3955 inputDimensions, indicesDimensions,
3956 CHECK_LOCATION().AsString()));
3957 }
3958
3959 gatherDescriptor.m_Axis = axis;
3960
3961 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3962 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3963 ARMNN_ASSERT(layer != nullptr);
3964 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3965
3966 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3967 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3968
3969 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3970 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3971}
3972
Teresa Charlin91a53ea2022-04-25 15:47:29 +01003973void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
3974{
3975 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3976
3977 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3978 CHECK_VALID_SIZE(inputs.size(), 2);
3979 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3980 CHECK_VALID_SIZE(outputs.size(), 1);
3981
3982 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3983 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3984 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3985
3986 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
3987 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
3988 ARMNN_ASSERT(layer != nullptr);
3989 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3990
3991 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3992 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3993
3994 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3995 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3996}
3997
Kevin May7d96b162021-02-03 17:38:41 +00003998void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003999{
4000 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4001
Kevin May7d96b162021-02-03 17:38:41 +00004002 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004003 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004004 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004005 CHECK_VALID_SIZE(outputs.size(), 1);
4006
4007 armnn::DepthToSpaceDescriptor descriptor;
4008
Mike Kelly0d77ae12022-01-07 17:42:27 +00004009 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4010 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004011 auto blockSize = options->block_size;
4012 if (blockSize < 2)
4013 {
4014 throw ParseException(
4015 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4016 blockSize,
4017 CHECK_LOCATION().AsString()));
4018 }
4019 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4020
4021 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4022 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4023 ARMNN_ASSERT(layer != nullptr);
4024 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4025 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4026
4027 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4028 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4029
4030 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4031 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4032}
4033
Kevin May7d96b162021-02-03 17:38:41 +00004034void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004035{
Sadik Armagana2747482021-02-09 10:28:54 +00004036 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4037}
4038
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004039void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4040{
4041 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4042}
4043
Sadik Armagana2747482021-02-09 10:28:54 +00004044void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4045{
4046 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4047}
4048
4049void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4050{
4051 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4052}
4053
4054void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4055{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004056 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4057
Mike Kelly0d77ae12022-01-07 17:42:27 +00004058 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4059 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004060
4061 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4062 CHECK_VALID_SIZE(inputs.size(), 2);
4063
4064 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4065 CHECK_VALID_SIZE(outputs.size(), 1);
4066
Sadik Armagana2747482021-02-09 10:28:54 +00004067 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004068
4069 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
4070 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004071
4072 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004073 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4074 // Get const axis value from model and set it to descriptor.
4075 if (axisBufferPtr != nullptr)
4076 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004077 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4078 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4079
4080 // Convert the axis to unsigned int and remove duplicates.
4081 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4082 std::set<unsigned int> uniqueAxis;
4083 std::transform(axisData.begin(),
4084 axisData.end(),
4085 std::inserter(uniqueAxis, uniqueAxis.begin()),
4086 [rank](int i)->unsigned int{
4087 return static_cast<uint32_t>(((i + rank) % rank)); });
4088 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004089 }
Sadik Armagana2747482021-02-09 10:28:54 +00004090 else
4091 {
4092 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4093 {
4094 desc.m_vAxis.push_back(i);
4095 }
4096 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004097
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004098 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004099 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004100
4101 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004102 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004103
4104 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4105 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4106
4107 // Register input tensor to the layer.
4108 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4109 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4110
4111 // Register output tensor to the layer.
4112 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4113 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4114}
4115
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004116void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4117{
4118 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4119}
4120
4121void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4122{
4123 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4124}
4125
Mike Kelly31dce2b2021-09-01 21:22:37 +01004126void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4127{
4128 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4129
4130 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4131 CHECK_VALID_SIZE(inputs.size(), 1);
4132
4133 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4134 CHECK_VALID_SIZE(outputs.size(), 1);
4135
4136 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4137 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4138
4139 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4140
4141 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4142 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4143
4144 armnn::NormalizationDescriptor descriptor;
4145 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4146 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4147 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4148 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4149 descriptor.m_K = options->bias;
4150 descriptor.m_Alpha = options->alpha;
4151 descriptor.m_Beta = options->beta;
4152
4153 // ArmNN expects normSize to be the full size of the normalization
4154 // window rather than the radius as in TfLite.
4155 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4156
4157 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4158 ARMNN_ASSERT(layer != nullptr);
4159
4160 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4161 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4162
4163 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4164 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4165
4166 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4167 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4168}
4169
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004170void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4171{
4172 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4173}
4174
4175void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4176{
4177 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4178}
4179
4180void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4181{
4182 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4183}
4184
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004185void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4186{
4187 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4188}
4189
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004190void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4191{
4192 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4193
4194 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4195 CHECK_VALID_SIZE(inputs.size(), 1);
4196
4197 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4198 CHECK_VALID_SIZE(outputs.size(), 1);
4199
4200 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4201 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4202
4203 ElementwiseUnaryDescriptor desc;
4204 desc.m_Operation = unaryOperation;
4205 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4206 ARMNN_ASSERT(layer != nullptr);
4207
4208 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4209 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4210
4211 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4212 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4213
4214 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4215 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4216}
4217
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004218void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4219{
4220 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4221}
4222
4223void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4224{
4225 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4226}
4227
4228void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4229{
4230 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4231}
4232
4233void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4234{
4235 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4236}
4237
4238void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4239{
4240 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4241}
4242
4243void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4244{
4245 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4246}
4247
4248void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4249 ComparisonOperation comparisonOperation)
4250{
4251 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4252
4253 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4254 CHECK_VALID_SIZE(inputs.size(), 2);
4255
4256 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4257 CHECK_VALID_SIZE(outputs.size(), 1);
4258
4259 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4260 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4261
4262 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
4263 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
4264 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4265
4266 ComparisonDescriptor desc;
4267 desc.m_Operation = comparisonOperation;
4268 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4269 ARMNN_ASSERT(layer != nullptr);
4270
4271 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
4272 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4273
4274 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4275 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4276
4277 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4278 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4279}
4280
Kevin May7d96b162021-02-03 17:38:41 +00004281armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4282 unsigned int outputSlot,
4283 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004284{
4285 ActivationDescriptor activationDesc;
4286 std::string layerName = prevLayer->GetName();
4287
4288 switch(activationType)
4289 {
4290 case tflite::ActivationFunctionType_NONE:
4291 {
4292 // this is a no-op: return previous layer
4293 return prevLayer;
4294 }
4295 case tflite::ActivationFunctionType_RELU:
4296 {
4297 activationDesc.m_Function = ActivationFunction::ReLu;
4298 layerName += ":RELU";
4299 break;
4300 }
4301 case tflite::ActivationFunctionType_RELU6:
4302 {
4303 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4304 activationDesc.m_A = 6.0f;
4305 activationDesc.m_B = 0.0f;
4306 layerName += ":RELU6";
4307 break;
4308 }
4309 case tflite::ActivationFunctionType_TANH:
4310 {
4311 activationDesc.m_Function = ActivationFunction::TanH;
4312 activationDesc.m_A = 1.0f;
4313 activationDesc.m_B = 1.0f;
4314 layerName += ":TANH";
4315 break;
4316 }
4317
4318 // I only put these here as a reminder what others we could support
4319 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4320 case tflite::ActivationFunctionType_SIGN_BIT:
4321 default:
4322 {
4323 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004324 fmt::format("TfLite parser doesn't suppport fused activation: "
4325 "{}/{} {} ",
4326 activationType,
4327 tflite::EnumNameActivationFunctionType(activationType),
4328 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004329
4330 }
4331 }
4332
4333 IConnectableLayer* activationLayer =
4334 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4335
4336 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4337 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4338 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4339 return activationLayer;
4340}
4341
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004342armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4343 unsigned int outputSlot)
4344{
Teresa Charlin725728e2022-05-05 13:33:33 +01004345
4346 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4347 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4348
4349 if (dataType == DataType::Signed32)
4350 {
4351 return prevLayer;
4352 }
4353
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004354 std::string layerName = prevLayer->GetName();
4355 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4356
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004357 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4358 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004359
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004360 return floorLayer;
4361}
4362
Mike Kelly0d77ae12022-01-07 17:42:27 +00004363TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004364{
4365 if (fileName == nullptr)
4366 {
James Ward58dec6b2020-09-11 17:32:44 +01004367 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004368 CHECK_LOCATION().AsString()));
4369 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004370 std::error_code errorCode;
4371 fs::path pathToFile(fileName);
4372 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004373 {
James Ward58dec6b2020-09-11 17:32:44 +01004374 //fmt::format() could not be used here (format error)
4375 std::stringstream msg;
4376 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4377 << " " << CHECK_LOCATION().AsString();
4378
4379 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004380 }
4381 std::ifstream file(fileName, std::ios::binary);
4382 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4383 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4384 fileContent.size());
4385}
4386
Mike Kelly0d77ae12022-01-07 17:42:27 +00004387TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004388{
4389 if (binaryContent == nullptr)
4390 {
James Ward58dec6b2020-09-11 17:32:44 +01004391 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004392 CHECK_LOCATION().AsString()));
4393 }
4394 flatbuffers::Verifier verifier(binaryContent, len);
4395 if (verifier.VerifyBuffer<tflite::Model>() == false)
4396 {
4397 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004398 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4399 "flatbuffers format. size:{} {}",
4400 len,
4401 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004402 }
4403 return tflite::UnPackModel(binaryContent);
4404}
4405
Mike Kelly0d77ae12022-01-07 17:42:27 +00004406TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004407 size_t subgraphIndex,
4408 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004409{
4410 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4411
Mike Kelly0d77ae12022-01-07 17:42:27 +00004412 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4413 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004414
4415 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004416 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004417 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004418 {
mathad01c21025d2021-04-26 10:09:37 +01004419 // If the input location is -1 then assume input is turned off.
4420 if (operatorPtr->inputs[i] == -1)
4421 {
4422 continue;
4423 }
4424 else
4425 {
4426 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4427 result.push_back(subgraphPtr->tensors[inputId].get());
4428 }
telsoa01c577f2c2018-08-31 09:22:23 +01004429 }
4430 return result;
4431}
4432
Mike Kelly0d77ae12022-01-07 17:42:27 +00004433TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004434 size_t subgraphIndex,
4435 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004436{
4437 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4438
Mike Kelly0d77ae12022-01-07 17:42:27 +00004439 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4440 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004441
4442 size_t outputCount = operatorPtr->outputs.size();
4443 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004444 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004445 {
4446 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4447 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004448 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004449 }
4450 return result;
4451}
4452
Mike Kelly0d77ae12022-01-07 17:42:27 +00004453TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004454 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004455{
4456 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004457 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004458
Derek Lambertiff05cc52019-04-26 13:05:17 +01004459 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004460 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004461 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004462 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004463 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004464 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004465 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004466 }
4467 return result;
4468}
4469
Mike Kelly0d77ae12022-01-07 17:42:27 +00004470TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004471 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004472{
4473 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004474 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004475
Derek Lambertiff05cc52019-04-26 13:05:17 +01004476 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004477 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004478 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004479 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004480 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4481 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004482 }
4483 return result;
4484}
4485
Kevin May7d96b162021-02-03 17:38:41 +00004486std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4487 size_t subgraphIndex,
4488 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004489{
4490 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004491 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4492 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004493 return operatorPtr->inputs;
4494}
4495
Kevin May7d96b162021-02-03 17:38:41 +00004496std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4497 size_t subgraphIndex,
4498 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004499{
4500 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004501 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4502 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004503 return operatorPtr->outputs;
4504}
4505
Kevin May7d96b162021-02-03 17:38:41 +00004506void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4507 size_t operatorIndex,
4508 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004509 const std::vector<unsigned int>& tensorIndexes,
4510 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004511{
4512 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004513 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004514
Finn Williamsd4fa5452021-03-01 12:31:41 +00004515 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004516 {
4517 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004518 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4519 " for subgraph:{} operator index:{} {}",
4520 tensorIndexes.size(),
4521 layer->GetNumInputSlots(),
4522 subgraphIndex,
4523 operatorIndex,
4524 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004525 }
4526
Finn Williamsd4fa5452021-03-01 12:31:41 +00004527 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004528 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004529 unsigned int tensorIndex = tensorIndexes[index];
4530 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004531 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4532 }
4533}
4534
Kevin May7d96b162021-02-03 17:38:41 +00004535void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4536 size_t operatorIndex,
4537 IConnectableLayer* layer,
4538 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004539{
4540 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004541 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004542 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4543 {
4544 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004545 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4546 " for subgraph:{} operator index:{} {}",
4547 tensorIndexes.size(),
4548 layer->GetNumOutputSlots(),
4549 subgraphIndex,
4550 operatorIndex,
4551 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004552 }
4553
4554 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4555 {
4556 unsigned int tensorIndex = tensorIndexes[slotIndex];
4557 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4558 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4559 }
4560}
4561
Kevin May7d96b162021-02-03 17:38:41 +00004562void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004563{
4564 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4565
4566 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004567 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004568 {
4569 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4570 IConnectableLayer* layer =
4571 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4572
4573 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4574 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4575
4576 RegisterOutputSlots(subgraphIndex,
4577 VIRTUAL_OPERATOR_ID,
4578 layer,
4579 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4580 }
4581}
4582
Kevin May7d96b162021-02-03 17:38:41 +00004583void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004584{
4585 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4586
4587 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004588 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004589 {
4590 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4591 IConnectableLayer* layer =
4592 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4593
4594 RegisterInputSlots(subgraphIndex,
4595 VIRTUAL_OPERATOR_ID,
4596 layer,
4597 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4598 }
4599}
4600
Mike Kelly5880b912022-01-28 16:18:54 +00004601void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004602{
Mike Kelly5880b912022-01-28 16:18:54 +00004603 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004604
Mike Kelly5880b912022-01-28 16:18:54 +00004605 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004606 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4607 {
4608 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4609 {
4610 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4611 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4612 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004613 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004614
Mike Kelly5880b912022-01-28 16:18:54 +00004615 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01004616 {
4617 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00004618 armnn::DataType dataType = tensorInfo.GetDataType();
4619
4620 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4621 != m_ConstantsToDequantize.end())
4622 {
4623 dataType = DataType::Float32;
4624 }
4625 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
4626
4627 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
4628 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
4629
4630 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
4631 RegisterOutputSlots(subgraphIndex,
4632 VIRTUAL_OPERATOR_ID,
4633 layer,
4634 { tensorIndex });
4635 }
4636 else if (ShouldConstantTensorBeCreated(tensorIndex))
4637 {
4638 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4639 armnn::DataType dataType = tensorInfo.GetDataType();
4640
4641 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
4642 != m_ConstantsToDequantize.end())
4643 {
4644 dataType = DataType::Float32;
4645 }
4646 // Make sure isConstant flag is set.
4647 tensorInfo.SetConstant();
4648 tensorInfo.SetDataType(dataType);
4649
4650 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004651
Matthew Sloyan81beae32021-07-13 19:46:11 +01004652 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004653 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004654
Matthew Sloyan81beae32021-07-13 19:46:11 +01004655 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4656 RegisterOutputSlots(subgraphIndex,
4657 VIRTUAL_OPERATOR_ID,
4658 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00004659 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01004660 }
4661 else
4662 {
4663 throw ParseException(
4664 fmt::format("Invalid Tensor: Tensor should be constant. {}",
4665 CHECK_LOCATION().AsString()));
4666 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004667 }
4668 }
4669 }
4670}
4671
telsoa01c577f2c2018-08-31 09:22:23 +01004672// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00004673TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004674{
4675 CHECK_BUFFER(model, bufferIndex);
4676 return model->buffers[bufferIndex].get();
4677}
4678
Matteo Martincigh747ef822018-12-18 09:26:39 +00004679template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00004680std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
4681TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
4682 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00004683 armnn::TensorInfo& tensorInfo,
4684 armnn::Optional<armnn::PermutationVector&> permutationVector)
4685{
Matthew Sloyan81beae32021-07-13 19:46:11 +01004686 // Make sure isConstant flag is set.
4687 tensorInfo.SetConstant();
4688
Matteo Martincigh747ef822018-12-18 09:26:39 +00004689 auto constData = CreateConstTensorImpl<T>(bufferPtr,
4690 tensorPtr,
4691 tensorInfo,
4692 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00004693 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00004694 return std::make_pair(constData.first, std::move(storage));
4695}
4696
Mike Kelly5880b912022-01-28 16:18:54 +00004697bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
4698{
4699 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
4700 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
4701 != m_ConstantsToBeCreated.end());
4702}
4703
Finn Williamsd4fa5452021-03-01 12:31:41 +00004704bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
4705{
4706 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01004707 bool isConst = true;
4708
4709 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
4710 if (buffer->data.size() == 0)
4711 {
4712 isConst = false;
4713 }
4714
4715 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00004716}
4717
Kevin May7d96b162021-02-03 17:38:41 +00004718std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00004719TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
4720 armnn::TensorInfo& tensorInfo,
4721 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01004722{
4723 CHECK_TENSOR_PTR(tensorPtr);
4724 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4725 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4726
Matthew Sloyan81beae32021-07-13 19:46:11 +01004727 // Make sure isConstant flag is set.
4728 tensorInfo.SetConstant();
4729
telsoa01c577f2c2018-08-31 09:22:23 +01004730 switch (tensorInfo.GetDataType())
4731 {
4732 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004733 return CreateConstTensorAndStoreData<float>(bufferPtr,
4734 tensorPtr,
4735 tensorInfo,
4736 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00004737 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004738 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
4739 tensorPtr,
4740 tensorInfo,
4741 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00004742 case armnn::DataType::QSymmS8:
4743 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4744 tensorPtr,
4745 tensorInfo,
4746 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00004747 case armnn::DataType::QAsymmS8:
4748 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
4749 tensorPtr,
4750 tensorInfo,
4751 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004752 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00004753 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
4754 tensorPtr,
4755 tensorInfo,
4756 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01004757 default:
4758 {
4759 std::stringstream errString;
4760 errString << "Unexpected datatype when creating const tensor: "
4761 << armnn::GetDataTypeName(tensorInfo.GetDataType())
4762 << " shape:" << tensorInfo.GetShape()
4763 << CHECK_LOCATION().AsString();
4764 throw ParseException(errString.str());
4765 }
4766 }
4767}
4768
Finn Williamsd4fa5452021-03-01 12:31:41 +00004769armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4770 armnn::TensorInfo& tensorInfo)
4771{
4772 CHECK_TENSOR_PTR(tensorPtr);
4773 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4774 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4775
Matthew Sloyan81beae32021-07-13 19:46:11 +01004776 // Make sure isConstant flag is set.
4777 tensorInfo.SetConstant();
4778
Finn Williamsd4fa5452021-03-01 12:31:41 +00004779 return ConstTensor(tensorInfo, bufferPtr->data.data());
4780}
4781
Mike Kelly5880b912022-01-28 16:18:54 +00004782std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
4783TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
4784 armnn::TensorInfo& tensorInfo,
4785 armnn::DataType inputDataType)
4786{
4787 CHECK_TENSOR_PTR(tensorPtr);
4788 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4789 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4790
4791 // Make sure isConstant flag is set.
4792 tensorInfo.SetConstant();
4793
4794 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4795 {
4796 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4797 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4798 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
4799 }
4800 else
4801 {
4802 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4803 }
4804}
4805
4806std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
4807TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
4808{
4809 CHECK_TENSOR_PTR(tensorPtr);
4810 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4811 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
4812 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
4813
4814 // Make sure isConstant flag is set.
4815 tensorInfo.SetConstant();
4816
4817 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
4818 {
4819 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
4820 std::unique_ptr<float[]> data = AsFloatArray(bufferPtr, tensorInfo);
4821 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
4822 }
4823 else
4824 {
4825 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
4826 }
4827}
4828
Kevin May7d96b162021-02-03 17:38:41 +00004829BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
4830 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004831{
4832 CHECK_SUBGRAPH(m_Model, subgraphId);
4833 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004834 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004835 {
4836 if (input.second->name == name)
4837 {
4838 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00004839 auto inputTensorInfo = ToTensorInfo(input.second);
4840 // Input tensors are always treated as constant tensors during network execution.
4841 inputTensorInfo.SetConstant(true);
4842 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01004843 }
4844 }
4845
4846 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004847 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004848 {
4849 bindings << "'" << input.second->name << "' ";
4850 }
4851
4852 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004853 fmt::format("No input binding found for subgraph:{} and name:{}. "
4854 "Possible inputs are: [{}] {}",
4855 subgraphId,
4856 name,
4857 bindings.str(),
4858 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004859}
4860
Kevin May7d96b162021-02-03 17:38:41 +00004861BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
4862 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01004863{
4864 CHECK_SUBGRAPH(m_Model, subgraphId);
4865 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004866 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004867 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004868 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01004869 if (output.second->name == name)
4870 {
4871 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00004872 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
4873 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
4874 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01004875 }
4876 }
4877
4878 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004879 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004880 {
4881 bindings << "'" << output.second->name << "' ";
4882 }
4883
4884 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004885 fmt::format("No output binding found for subgraph:{} and name:{}. "
4886 "Possible outputs are: [{}] {}",
4887 subgraphId,
4888 name,
4889 bindings.str(),
4890 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004891}
4892
Kevin May7d96b162021-02-03 17:38:41 +00004893size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004894{
4895 return m_Model->subgraphs.size();
4896}
4897
Kevin May7d96b162021-02-03 17:38:41 +00004898std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004899{
4900 CHECK_SUBGRAPH(m_Model, subgraphId);
4901 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4902 std::vector<std::string> result;
4903 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004904 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004905 {
4906 result.push_back(input.second->name);
4907 }
4908 return result;
4909}
4910
Kevin May7d96b162021-02-03 17:38:41 +00004911std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004912{
4913 CHECK_SUBGRAPH(m_Model, subgraphId);
4914 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4915 std::vector<std::string> result;
4916 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00004917 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004918 {
4919 result.push_back(output.second->name);
4920 }
4921 return result;
4922}
4923
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004924const std::string TfLiteParserImpl::GetVersion()
4925{
4926 return TFLITE_PARSER_VERSION;
4927}
4928
Mike Kelly0d77ae12022-01-07 17:42:27 +00004929TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004930: m_FloatData(std::move(data))
4931, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004932, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004933, m_Int32Data(nullptr)
4934{
4935}
4936
Mike Kelly0d77ae12022-01-07 17:42:27 +00004937TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004938: m_FloatData(nullptr)
4939, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004940, m_Int8Data(nullptr)
4941, m_Int32Data(nullptr)
4942{
4943}
4944
Mike Kelly0d77ae12022-01-07 17:42:27 +00004945TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00004946: m_FloatData(nullptr)
4947, m_Uint8Data(nullptr)
4948, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01004949, m_Int32Data(nullptr)
4950{
4951}
4952
Mike Kelly0d77ae12022-01-07 17:42:27 +00004953TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01004954: m_FloatData(nullptr)
4955, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004956, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004957, m_Int32Data(std::move(data))
4958{
4959}
4960
4961} // armnnTfLiteParser