blob: b7e2762ade1edef62d42c1ca85109ffcb1aee232 [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Mike Kelly04d82292023-01-19 18:29:40 +00002// Copyright © 2017-2023 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>
Mike Kelly377fb212023-01-10 15:55:28 +000021#include <armnn/LayerSupport.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010022
23// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000024#include <armnnUtils/Permute.hpp>
Rob Hughes9542f902021-07-14 09:48:54 +010025#include <armnnUtils/Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000026
Sadik Armagan479045b2018-10-01 11:51:37 +010027#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010028#include <VerificationHelpers.hpp>
29
30// The generated code based on the Tf Lite schema:
31#include <schema_generated.h>
32
Matteo Martincighe011d202019-11-28 11:35:47 +000033#include <flatbuffers/flexbuffers.h>
34
James Ward58dec6b2020-09-11 17:32:44 +010035#include <fmt/format.h>
telsoa01c577f2c2018-08-31 09:22:23 +010036
telsoa01c577f2c2018-08-31 09:22:23 +010037#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000038#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010039#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010040#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000041
42#define ARMNN_THROW_PARSE_EXCEPTION(msg) \
43 { \
44 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
45 << ": " \
46 << CHECK_LOCATION().AsString()).str()); \
47 }
telsoa01c577f2c2018-08-31 09:22:23 +010048
49using namespace armnn;
50using armnn::CheckLocation;
51namespace armnnTfLiteParser
52{
Kevin May7d96b162021-02-03 17:38:41 +000053
54ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
55 pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
56
57ITfLiteParser::~ITfLiteParser() = default;
58
59ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
60{
61 return new ITfLiteParser(options);
62}
63
64ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
65{
66 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
67}
68
69void ITfLiteParser::Destroy(ITfLiteParser* parser)
70{
71 delete parser;
72}
73
74armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
75{
76 return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
77}
78
Mike Kelly0d77ae12022-01-07 17:42:27 +000079armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
Kevin May7d96b162021-02-03 17:38:41 +000080{
81 return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
82}
83
84BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
85 const std::string& name) const
86{
87 return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
88}
89
90BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
91 const std::string& name) const
92{
93 return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
94}
95
96size_t ITfLiteParser::GetSubgraphCount() const
97{
98 return pTfLiteParserImpl->GetSubgraphCount();
99}
100
101std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
102{
103 return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
104}
105
106std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
107{
108 return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
109}
110
telsoa01c577f2c2018-08-31 09:22:23 +0100111namespace
112{
jimfly01c25411c2018-11-14 17:47:22 +0000113
telsoa01c577f2c2018-08-31 09:22:23 +0100114const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
115
Mike Kelly0d77ae12022-01-07 17:42:27 +0000116void CheckSubgraph(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100117 size_t subgraphIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000118 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100119{
120 if (model.get() == nullptr)
121 {
122 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100123 fmt::format("{} was called with invalid (null) model. "
124 "Possible reason is that the model is not yet loaded and Unpack(ed). "
125 "subgraph:{} at {}",
126 location.m_Function,
127 subgraphIndex,
128 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100129 }
130 else if (subgraphIndex >= model->subgraphs.size())
131 {
132 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100133 fmt::format("{} was called with an invalid subgraph index. "
134 "subgraph:{} at {}",
135 location.m_Function,
136 subgraphIndex,
137 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100138 }
139}
140
141#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
142 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
143
Mike Kelly0d77ae12022-01-07 17:42:27 +0000144void CheckModel(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100145 size_t subgraphIndex,
146 size_t operatorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000147 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100148{
149 if (model.get() == nullptr)
150 {
151 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100152 fmt::format("{} was called with invalid (null) model. "
153 "Possible reason is that the model is not yet loaded and Unpack(ed). "
154 "subgraph:{} operator:{} at {}",
155 location.m_Function,
156 subgraphIndex,
157 operatorIndex,
158 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100159 }
160 else if (subgraphIndex >= model->subgraphs.size())
161 {
162 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100163 fmt::format("{} was called with an invalid subgraph index. "
164 "subgraph:{} operator:{} at {}",
165 location.m_Function,
166 subgraphIndex,
167 operatorIndex,
168 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100169 }
170 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
171 operatorIndex != VIRTUAL_OPERATOR_ID)
172 {
173 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100174 fmt::format("{} was called with an invalid operator index. "
175 "subgraph:{} operator:{} at {}",
176 location.m_Function,
177 subgraphIndex,
178 operatorIndex,
179 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100180 }
181}
182
183#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
184 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
185
Mike Kelly0d77ae12022-01-07 17:42:27 +0000186void CheckTensor(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100187 size_t subgraphIndex,
188 size_t tensorIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000189 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100190{
191 // not checking model, because I assume CHECK_MODEL already run
192 // and checked that. An assert would do.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100193 ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
telsoa01c577f2c2018-08-31 09:22:23 +0100194
195 // also subgraph index should be checked by CHECK_MODEL so
196 // I only add an assert here
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100197 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
telsoa01c577f2c2018-08-31 09:22:23 +0100198
199 // the tensor index is the only one to check here
200 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
201 {
202 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100203 fmt::format("{} was called with an invalid tensor index. "
204 "subgraph:{} tensor:{} at {}",
205 location.m_Function,
206 subgraphIndex,
207 tensorIndex,
208 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100209 }
210}
211
212#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
213 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
214
Kevin May7d96b162021-02-03 17:38:41 +0000215void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000216 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100217{
218 if (rawPtr == nullptr)
219 {
220 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100221 fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100222 }
223}
224
225#define CHECK_TENSOR_PTR(TENSOR_PTR) \
226 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
227
Mike Kelly0d77ae12022-01-07 17:42:27 +0000228void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
telsoa01c577f2c2018-08-31 09:22:23 +0100229 size_t bufferIndex,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000230 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100231{
232 if (model.get() == nullptr)
233 {
234 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100235 fmt::format("{} was called with invalid (null) model. "
236 "Possible reason is that the model is not yet loaded and Unpack(ed). "
237 "buffer:{} at {}",
238 location.m_Function,
239 bufferIndex,
240 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100241 }
242 else if (bufferIndex >= model->buffers.size())
243 {
244 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100245 fmt::format("{} was called with an invalid buffer index. "
246 "buffer index:{} at {}",
247 location.m_Function,
248 bufferIndex,
249 location.FileLine()));
telsoa01c577f2c2018-08-31 09:22:23 +0100250 }
251 else if (model->buffers[bufferIndex].get() == nullptr)
252 {
253 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100254 fmt::format("The buffer #{} is null. {}",
255 bufferIndex,
256 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100257 }
258}
259
260#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
261 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
262
Kevin May7d96b162021-02-03 17:38:41 +0000263void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000264 const armnn::TensorInfo& tensorInfo,
telsoa01c577f2c2018-08-31 09:22:23 +0100265 uint32_t bufferId,
Mike Kelly0d77ae12022-01-07 17:42:27 +0000266 const CheckLocation& location)
telsoa01c577f2c2018-08-31 09:22:23 +0100267{
268 if (bufferPtr == nullptr)
269 {
270 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100271 fmt::format("BufferPtr is null for buffer:{}. {}",
272 bufferId,
273 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100274 }
275 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
276 tensorInfo.GetNumBytes() > bufferPtr->data.size())
277 {
278 std::stringstream ss;
279 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
280 << "For tensor: " << tensorInfo.GetShape()
281 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
282 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
283 throw ParseException(ss.str());
284 }
285}
286
Mike Kelly0d77ae12022-01-07 17:42:27 +0000287
288tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
289{
290 const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
291 auto opcodeIndex = operatorPtr->opcode_index;
292
293// work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
294#if defined(ARMNN_POST_TFLITE_2_3)
295 auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
296 static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
297#else
298 auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
299#endif
300 return opcode;
301}
302
303std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
304 const TfLiteParserImpl::ModelPtr& model,
305 size_t bufferIndex)
306{
307 TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
308 std::vector<unsigned int> buffer(info.GetNumElements());
309
310 if (info.GetDataType() == DataType::Signed32)
311 {
312 ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
313 }
314 else if (info.GetDataType() == DataType::Signed64)
315 {
316 std::vector<uint64_t> uint64Buffer(info.GetNumElements());
317 ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
318 buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
319 }
Mike Kelly0506ef02023-01-03 16:29:44 +0000320 else
321 {
322 CheckLocation location = CHECK_LOCATION();
323 throw ParseException(
324 fmt::format("Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
325 GetDataTypeName(info.GetDataType()),
326 location.AsString()));
327 }
Mike Kelly0d77ae12022-01-07 17:42:27 +0000328 return buffer;
329}
330
telsoa01c577f2c2018-08-31 09:22:23 +0100331#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
332 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
333
334bool IsActivationSupported(tflite::ActivationFunctionType activationType)
335{
336 switch(activationType)
337 {
338 case tflite::ActivationFunctionType_NONE:
339 case tflite::ActivationFunctionType_RELU:
340 case tflite::ActivationFunctionType_RELU6:
341 case tflite::ActivationFunctionType_TANH:
342 {
343 return true;
344 }
345 default:
346 {
347 return false;
348 }
349 }
350}
351
352#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
353 do { \
354 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
355 { \
356 throw ParseException( \
Mike Kelly377fb212023-01-10 15:55:28 +0000357 fmt::format("TfLite parser doesn't support fused activation: " \
James Ward58dec6b2020-09-11 17:32:44 +0100358 "{}/{} in {} subgraph:{} operator:{} at {}", \
359 OPTION->fused_activation_function, \
360 tflite::EnumNameActivationFunctionType(\
361 OPTION->fused_activation_function), \
362 __func__, \
363 SUBGRAPH_INDEX, \
364 OPERATOR_INDEX, \
365 CHECK_LOCATION().FileLine())); \
telsoa01c577f2c2018-08-31 09:22:23 +0100366 } \
367 } while(false)
368
369
Mike Kelly0d77ae12022-01-07 17:42:27 +0000370std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t>& in)
telsoa01c577f2c2018-08-31 09:22:23 +0100371{
372 std::vector<unsigned int> result;
373 result.reserve(in.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +0000374 for (auto& i : in)
telsoa01c577f2c2018-08-31 09:22:23 +0100375 {
mathad01c21025d2021-04-26 10:09:37 +0100376 // If the location of the input data is -1 then the input should be ignored.
377 if (i == -1)
378 {
379 continue;
380 }
telsoa01c577f2c2018-08-31 09:22:23 +0100381 result.push_back(CHECKED_NON_NEGATIVE(i));
382 }
383 return result;
384}
385
Mike Kelly5880b912022-01-28 16:18:54 +0000386bool IsOptionalOperandPresent(int input)
387{
388 return (input >= 0);
389}
390
telsoa01c577f2c2018-08-31 09:22:23 +0100391void CalcPadding(uint32_t inputSize,
392 uint32_t filterSize,
393 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100394 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100395 uint32_t& paddingFront,
396 uint32_t& paddingBack,
397 tflite::Padding padding)
398{
399 paddingFront = 0;
400 paddingBack = 0;
401 if (padding == tflite::Padding_SAME)
402 {
403 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100404 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
405 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100406 if (temp > inputSize)
407 {
408 paddingFront = (temp - inputSize) / 2;
409 paddingBack = (temp - inputSize) - paddingFront;
410 }
411 }
412}
413
Kevin May7d96b162021-02-03 17:38:41 +0000414armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Finn Williamsb49ed182021-06-29 15:50:08 +0100415 const std::vector<unsigned int>& shape,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100416 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100417{
418 armnn::DataType type;
419 CHECK_TENSOR_PTR(tensorPtr);
420
421 switch (tensorPtr->type)
422 {
423 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000424 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100425 break;
426 case tflite::TensorType_FLOAT32:
427 type = armnn::DataType::Float32;
428 break;
Keith Davisb4dd5cc2022-04-07 11:32:00 +0100429 case tflite::TensorType_FLOAT16:
430 type = armnn::DataType::Float16;
431 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000432 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000433 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000434 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000435 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000436 type = armnn::DataType::QAsymmS8;
437 }
438 else
439 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000440 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000441 type = armnn::DataType::QSymmS8;
442 }
Finn Williamsed66d142019-12-06 09:55:55 +0000443 break;
444 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000445 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000446 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100447 case tflite::TensorType_INT32:
448 type = armnn::DataType::Signed32;
449 break;
Inki Daed4619e22020-09-10 15:33:54 +0900450 case tflite::TensorType_INT64:
451 type = armnn::DataType::Signed64;
452 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100453 case tflite::TensorType_BOOL:
454 type = armnn::DataType::Boolean;
455 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100456 default:
457 {
458 CheckLocation location = CHECK_LOCATION();
459 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100460 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
461 tensorPtr->type,
462 tflite::EnumNameTensorType(tensorPtr->type),
463 tensorPtr->name,
464 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100465 }
466 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100467 TensorShape tensorShape;
468
469 std::vector<unsigned int> safeShape = shape;
470 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100471 {
472 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100473 }
474
475 if (!outputTensor)
476 {
477 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
478 }
479 else
480 {
Rob Hughesd812a312021-08-06 13:10:53 +0100481 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100482
483 // If a shape signature exists we will use that to infer dynamic tensors
484 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100485 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100486 // If the shape is incompatible with the shape signature override the shape
487 if (shapeSignatureSize != shape.size())
488 {
489 safeShape = {};
490
491 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
492 {
493 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
494 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
495 safeShape.push_back(dim);
496 }
497 }
498
Rob Hughesd812a312021-08-06 13:10:53 +0100499 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Mike Kelly04d82292023-01-19 18:29:40 +0000500 bool batchOnly = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100501 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
502 {
Mike Kelly04d82292023-01-19 18:29:40 +0000503 dimMask[i] = tensorPtr->shape_signature[i] != -1;
504
505 if (i > 0 && !dimMask[i])
506 {
507 batchOnly = false;
508 }
509 }
510 if (batchOnly)
511 {
512 dimMask[0] = true;
Finn Williamsb49ed182021-06-29 15:50:08 +0100513 }
Rob Hughesd812a312021-08-06 13:10:53 +0100514 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100515 }
516 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
517 else if (shape.size() == 0)
518 {
519 tensorShape = TensorShape(1, false);
520 }
521 else
522 {
523 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100524 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100525 }
526
Keith Davisd305e1a2020-01-22 11:57:54 +0000527 float quantizationScale = 0.0f;
528 int32_t quantizationOffset = 0;
529
530 if (tensorPtr->quantization.get())
531 {
532 if (tensorPtr->quantization->scale.size() <= 1)
533 {
534 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
535 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
536
537 if (tensorPtr->quantization->scale.size() == 1)
538 {
539 quantizationScale = tensorPtr->quantization->scale[0];
540 }
541 if (tensorPtr->quantization->zero_point.size() == 1)
542 {
543 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000544 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100545 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000546 }
547
Sadik Armagand109a4d2020-07-28 10:42:13 +0100548 armnn::TensorInfo result(tensorShape,
549 type,
550 quantizationScale,
551 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000552 return result;
553 }
554 else
555 {
556 std::vector<float> quantizationScales;
557 std::vector<int32_t> quantizationOffsets;
558
559 // Scale
560 std::copy(tensorPtr->quantization->scale.begin(),
561 tensorPtr->quantization->scale.end(),
562 std::back_inserter(quantizationScales));
563
Keith Davis0c2eeac2020-02-11 16:51:50 +0000564 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100565 armnn::TensorInfo result(tensorShape,
566 type,
567 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100568 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000569 return result;
570 }
571 }
572 else
573 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100574 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000575 type,
576 quantizationScale,
577 quantizationOffset);
578 return result;
579 }
telsoa01c577f2c2018-08-31 09:22:23 +0100580}
581
Kevin May7d96b162021-02-03 17:38:41 +0000582armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Mike Kelly377fb212023-01-10 15:55:28 +0000583 const bool outputTensor = false)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100584{
Mike Kelly0d77ae12022-01-07 17:42:27 +0000585 auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100586 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100587}
588
telsoa01c577f2c2018-08-31 09:22:23 +0100589template<typename T>
590std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000591CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
592 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000593 armnn::TensorInfo& tensorInfo,
594 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100595{
Jan Eilers8eb25602020-03-09 12:13:48 +0000596 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100597 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
598 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100599 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100600
601 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000602
603 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
604 {
605 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000606 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
607 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000608 }
609 else
610 {
611 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
612 }
613
Matthew Sloyan81beae32021-07-13 19:46:11 +0100614 // Make sure isConstant flag is set.
615 tensorInfo.SetConstant();
616
telsoa01c577f2c2018-08-31 09:22:23 +0100617 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
618}
619
telsoa01c577f2c2018-08-31 09:22:23 +0100620armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
621{
622 // generate the binding id by shifting the tensor id by 8 bit
623 // and add the subgraph id, which allows 256 subgraphs
624 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
625}
626
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000627bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
628{
629 const unsigned int actualSize = actual.GetNumDimensions();
630 if (actualSize != expected.size())
631 {
632 return false;
633 }
634
635 for (unsigned int i = 0u; i < actualSize; i++)
636 {
637 if (expected[i] < 0 ||
638 actual[i] != static_cast<unsigned int>(expected[i]))
639 {
640 return false;
641 }
642 }
643
644 return true;
645}
646
Cathal Corbett2b922e22022-09-23 15:49:24 +0100647bool CheckShape(const armnn::TensorShape& actual, const armnn::TensorShape& expected)
648{
649 std::vector<int32_t> expectedVec;
650 for (uint32_t i = 0; i < expected.GetNumDimensions(); i++)
651 {
652 expectedVec.push_back(expected[i]);
653 }
654 return CheckShape(actual, expectedVec);
655}
656
James Conroy05102392020-06-24 15:39:55 +0100657void CheckMatchingQuantization(const TensorInfo& first,
658 const TensorInfo& second,
659 const std::string& descName,
660 std::string const& firstName,
661 std::string const& secondName)
662{
663 if (!first.IsQuantized() ||
664 !second.IsQuantized())
665 {
666 // Not a quantized type, ignore the validation
667 return;
668 }
669
670 DataType firstDataType = first.GetDataType();
671 DataType secondDataType = second.GetDataType();
672
673 if (firstDataType != secondDataType)
674 {
675 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
676 " must be of the same quantized type, " +
677 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
678 secondName + " is " + GetDataTypeName(secondDataType));
679 }
680
681 if (!first.IsTypeSpaceMatch(second))
682 {
683 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
684 " must have the same quantization space, " +
685 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
686 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
687 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
688 " and scale " + std::to_string(second.GetQuantizationScale()));
689 }
690}
691
Mike Kelly377fb212023-01-10 15:55:28 +0000692bool IsDynamic(TfLiteParserImpl::TensorRawPtr tensorPtr)
693{
694 auto shape = tensorPtr->shape;
695
696 if (shape.empty())
697 {
698 return true;
699 }
700 auto shapeSig = tensorPtr->shape_signature;
701
702 if (shapeSig.empty())
703 {
704 return false;
705 }
706
707 for (unsigned int i = 0; i < shapeSig.size() ; ++i)
708 {
709 if (shapeSig[i] == -1)
710 {
711 return true;
712 }
713 }
714 return false;
715}
716
telsoa01c577f2c2018-08-31 09:22:23 +0100717} // <anonymous>
718
Kevin May7d96b162021-02-03 17:38:41 +0000719TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100720: m_Options(options)
721, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000722, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100723{
724 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100725 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000726 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100727 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
728 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000729 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
730 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
Samuel Yapfd3ba5a2022-08-24 17:04:34 +0100731 m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
mathad01b392e982021-04-07 12:07:30 +0100732 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000733 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
734 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100735 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +0100736 #if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +0100737 m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
Matthew Sloyan4d217c02021-10-07 11:48:58 +0100738 #endif
Kevin May7d96b162021-02-03 17:38:41 +0000739 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
740 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
741 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
742 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100743 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000744 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300745 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000746 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100747 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Teresa Charlincdbd40b2022-02-25 13:21:55 +0000748 m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000749 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
750 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Teresa Charlin91a53ea2022-04-25 15:47:29 +0100751 m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300752 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
753 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000754 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
755 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300756 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
757 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Mike Kelly31dce2b2021-09-01 21:22:37 +0100758 m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
759 = &TfLiteParserImpl::ParseLocalResponseNormalization;
Teresa Charlin28aa6692022-07-12 11:18:44 +0100760 m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100761 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000762 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
Teresa Charlinfd33a692022-06-29 15:35:57 +0100763 m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
Kevin May7d96b162021-02-03 17:38:41 +0000764 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
765 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
766 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
767 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
768 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +0100769 m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
Kevin May7d96b162021-02-03 17:38:41 +0000770 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
771 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300772 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000773 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
774 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Mike Kelly0d77ae12022-01-07 17:42:27 +0000775 m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100776 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000777 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
778 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
779 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000780 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
781 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Teresa Charlin4e3e8312021-08-05 12:34:37 +0100782 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
Kevin May7d96b162021-02-03 17:38:41 +0000783 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
784 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
785 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100786 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Teresa Charlinf0fce5b2022-05-04 17:24:43 +0100787 m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100788 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Teresa Charlin28aa6692022-07-12 11:18:44 +0100789 m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
Kevin May7d96b162021-02-03 17:38:41 +0000790 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
791 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
792 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
Teresa Charlin2a764ad2023-02-24 18:17:31 +0000793 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
Kevin May7d96b162021-02-03 17:38:41 +0000794 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
795 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
796 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
797 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
798 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
799 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
800 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
801 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
802 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
Mike Kelly5880b912022-01-28 16:18:54 +0000803 m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
804 = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
Kevin May7d96b162021-02-03 17:38:41 +0000805 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100806
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100807 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000808 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100809}
810
Mike Kelly377fb212023-01-10 15:55:28 +0000811armnn::TensorInfo TfLiteParserImpl::InputTensorInfo(size_t subgraphIndex,
812 size_t operatorIndex,
813 int input)
814{
815 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
816 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
817
818 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[input]);
819 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
820
821 if (search != m_TensorInfos.end())
822 {
823 return m_TensorInfos[inputId];
824 }
825 else
826 {
827 auto tensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
828 m_TensorInfos.insert({ inputId, tensorInfo });
829 return tensorInfo;
830 }
831}
832
833armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(size_t subgraphIndex,
834 size_t operatorIndex,
835 armnn::IConnectableLayer* layer,
836 int output,
837 std::vector<int> inputs)
838{
839 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
840 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
841
842 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
843
844 auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
845
846 if (outputSearch != m_TensorInfos.end())
847 {
848 return m_TensorInfos[outputId];
849 }
850
851 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
852 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
853
854 if (IsDynamic(outputTensorPtr))
855 {
856 if (inputs.empty())
857 {
858 for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
859 {
860 inputs.emplace_back(i);
861 }
862 }
863 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
864 std::vector<armnn::TensorShape> inputShapes;
865
866 for (unsigned int i = 0; i < inputs.size(); ++i)
867 {
868 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[inputs[i]]);
869 auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
870
871 if (search != m_TensorInfos.end())
872 {
873 auto &inputTensorInfo = m_TensorInfos[inputId];
874 inputShapes.push_back(inputTensorInfo.GetShape());
875 }
876 else
877 {
878 m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
879 auto inputTensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
880 m_TensorInfos.insert({ inputId, inputTensorInfo});
881 inputShapes.push_back(inputTensorInfo.GetShape());
882 }
883 }
884 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
885 tensor.SetShape(outputShape);
886 }
887 m_TensorInfos.insert({ outputId, tensor});
888 return tensor;
889}
890
891armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(size_t subgraphIndex,
892 size_t operatorIndex,
893 armnn::IConnectableLayer* layer,
894 int output,
895 std::vector<armnn::TensorShape> inputShapes)
896{
897 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
898 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
899
900 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
901 const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
902 TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
903
904 if (IsDynamic(outputTensorPtr))
905 {
906 const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
907 tensor.SetShape(outputShape);
908 }
909 m_TensorInfos.insert({ outputId, tensor});
910 return tensor;
911}
912
Kevin May7d96b162021-02-03 17:38:41 +0000913void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100914{
915 m_Network = armnn::INetworkPtr(nullptr, nullptr);
916 m_Model = nullptr;
917 m_SubgraphConnections.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000918 m_OverriddenOutputShapes.clear();
Mike Kelly5880b912022-01-28 16:18:54 +0000919 m_ConstantsToDequantize.clear();
920 m_ConstantsToBeCreated.clear();
Mike Kelly377fb212023-01-10 15:55:28 +0000921 m_TensorInfos.clear();
telsoa01c577f2c2018-08-31 09:22:23 +0100922}
923
Kevin May7d96b162021-02-03 17:38:41 +0000924INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100925{
926 ResetParser();
927 m_Model = LoadModelFromFile(graphFile);
928 return CreateNetworkFromModel();
929}
930
Mike Kelly0d77ae12022-01-07 17:42:27 +0000931INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100932{
933 ResetParser();
934 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
935 return CreateNetworkFromModel();
936}
937
Finn Williamsb49ed182021-06-29 15:50:08 +0100938
939armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
940{
941 ResetParser();
942 m_Model = std::move(model);
943
944 return CreateNetworkFromModel();
945}
946
Kevin May7d96b162021-02-03 17:38:41 +0000947INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100948{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100949
950 using NetworkOptions = std::vector<BackendOptions>;
951 NetworkOptions networkOptions = {};
Mike Kelly80512b02022-05-16 23:10:42 +0100952 if (m_Options)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100953 {
Mike Kelly80512b02022-05-16 23:10:42 +0100954 if (m_Options.value().m_InferAndValidate)
955 {
956 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
957 {
958 { "InferAndValidate", true }
959 });
Sadik Armagand109a4d2020-07-28 10:42:13 +0100960
Mike Kelly80512b02022-05-16 23:10:42 +0100961 networkOptions.push_back(shapeInferenceMethodOption);
962 }
963 if (m_Options.value().m_AllowExpandedDims)
964 {
965 BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
966 {
967 { "AllowExpandedDims", true }
968 });
969
970 networkOptions.push_back(shapeInferenceMethodOption);
971 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100972 }
Sadik Armagand109a4d2020-07-28 10:42:13 +0100973 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100974 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100975
telsoa01c577f2c2018-08-31 09:22:23 +0100976 if (m_Model->subgraphs.size() != 1)
977 {
978 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100979 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
980 m_Model->subgraphs.size(),
981 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100982 }
983
984 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100985 size_t operatorIndex = 0;
986 try
telsoa01c577f2c2018-08-31 09:22:23 +0100987 {
Colm Donelan6350d272020-06-09 16:56:25 +0100988 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100989 {
Mike Kelly377fb212023-01-10 15:55:28 +0000990 SetupInputLayerTensorInfos(subgraphIndex);
991 SetupConstantLayerTensorInfos(subgraphIndex);
992
Colm Donelan6350d272020-06-09 16:56:25 +0100993 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
994 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100995 {
Colm Donelan6350d272020-06-09 16:56:25 +0100996 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
Jim Flynnfca233e2021-09-23 12:16:53 +0100997
998// 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 +0100999#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001000 auto builtinCode = std::max(opCodePtr->builtin_code,
1001 static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
1002#else
telsoa01c577f2c2018-08-31 09:22:23 +01001003 auto builtinCode = opCodePtr->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001004#endif
telsoa01c577f2c2018-08-31 09:22:23 +01001005
1006 if (builtinCode > tflite::BuiltinOperator_MAX)
1007 {
James Ward58dec6b2020-09-11 17:32:44 +01001008 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
1009 "subgraph:{} operator idx:{}. {}",
1010 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1011 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01001012 }
1013
1014 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +01001015 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +01001016 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +01001017 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +01001018 }
telsoa01c577f2c2018-08-31 09:22:23 +01001019
Colm Donelan6350d272020-06-09 16:56:25 +01001020 SetupInputLayers(subgraphIndex);
1021 SetupOutputLayers(subgraphIndex);
1022 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001023
Colm Donelan6350d272020-06-09 16:56:25 +01001024 ++subgraphIndex;
1025 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +01001026 }
telsoa01c577f2c2018-08-31 09:22:23 +01001027 }
Colm Donelan6350d272020-06-09 16:56:25 +01001028 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +01001029 {
Colm Donelan6350d272020-06-09 16:56:25 +01001030 std::stringstream errorString;
1031 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
1032 << subgraphIndex << " error: " << e.what();
1033 ARMNN_LOG(error) << errorString.str();
1034 std::stringstream errors;
1035 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +01001036 throw ParseException(errors.str());
1037 }
1038
1039 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +01001040 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001041 {
1042 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1043 {
1044 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
1045 {
1046 for (size_t inputSlotIdx = 0;
1047 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1048 ++inputSlotIdx)
1049 {
1050 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1051 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1052 }
1053 }
1054 }
1055 }
telsoa01c577f2c2018-08-31 09:22:23 +01001056 return std::move(m_Network);
1057}
1058
Mike Kelly0506ef02023-01-03 16:29:44 +00001059bool TfLiteParserImpl::ShouldConstantTensorBeConverted(TfLiteParserImpl::TensorRawPtr tensorPtr,
1060 armnn::DataType inputDataType,
1061 armnn::DataType tensorDataType)
Mike Kelly5880b912022-01-28 16:18:54 +00001062{
Mike Kelly0506ef02023-01-03 16:29:44 +00001063 return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1064 (tensorDataType == DataType::QAsymmU8 ||
1065 tensorDataType == DataType::QAsymmS8 ||
1066 tensorDataType == DataType::QSymmS8 ||
1067 tensorDataType == DataType::Signed32 ||
1068 tensorDataType == DataType::Signed64));
Mike Kelly5880b912022-01-28 16:18:54 +00001069}
1070
Kevin May7d96b162021-02-03 17:38:41 +00001071void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
1072 size_t tensorIndex,
1073 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001074{
1075 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001076 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1077 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001078
1079 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1080
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001081 if (slot->GetOwningIConnectableLayer().GetType() != LayerType::Constant)
telsoa01c577f2c2018-08-31 09:22:23 +01001082 {
telsoa01c577f2c2018-08-31 09:22:23 +01001083
Nikhil Rajd4d1c312022-08-03 18:20:59 +01001084 // assuming there is only one producer for that tensor
1085 if (tensorSlots.outputSlot != nullptr)
1086 {
1087 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
1088 "subgraph:{} tensor:{} {}",
1089 subgraphIndex,
1090 tensorIndex,
1091 CHECK_LOCATION().AsString()));
1092 }
1093 }
telsoa01c577f2c2018-08-31 09:22:23 +01001094 tensorSlots.outputSlot = slot;
1095}
1096
Kevin May7d96b162021-02-03 17:38:41 +00001097void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
1098 size_t tensorIndex,
1099 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +01001100{
1101 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001102 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
1103 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001104
Finn Williamsd4fa5452021-03-01 12:31:41 +00001105 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01001106 tensorSlots.inputSlots.push_back(slot);
1107}
1108
Kevin May7d96b162021-02-03 17:38:41 +00001109void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001110{
1111 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1112
1113 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +00001114 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001115
1116 // Identify custom code defined for custom operator
1117 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1118 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1119
Mike Kelly377fb212023-01-10 15:55:28 +00001120 // Find parser function that corresponds to custom code (if any)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001121 auto iterator = m_CustomParserFunctions.find(customCode);
1122 if (iterator != m_CustomParserFunctions.end())
1123 {
1124 customParserFunction = iterator->second;
1125 }
1126
1127 // Run parser function
1128 (this->*customParserFunction)(subgraphIndex, operatorIndex);
1129}
1130
Kevin May7d96b162021-02-03 17:38:41 +00001131void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001132{
1133 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001134
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001135 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1136
1137 auto opcodeIndex = operatorPtr->opcode_index;
Jim Flynnfca233e2021-09-23 12:16:53 +01001138
1139// 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 +01001140#if defined(ARMNN_POST_TFLITE_2_3)
Jim Flynnfca233e2021-09-23 12:16:53 +01001141 auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1142 static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1143#else
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001144 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
Jim Flynnfca233e2021-09-23 12:16:53 +01001145#endif
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001146
1147 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1148 {
1149 // Do not add StandInLayer, throw ParseException instead
1150 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01001151 fmt::format("Operator not supported. "
1152 "subgraph:{} operator:{} "
1153 "opcode_index:{} opcode:{} / {} {}",
1154 subgraphIndex,
1155 operatorIndex,
1156 opcodeIndex,
1157 opcode,
1158 tflite::EnumNameBuiltinOperator(opcode),
1159 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001160 }
1161
1162 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1163 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1164
Matthew Sloyan589e3e82020-09-11 16:17:48 +01001165 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1166 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001167
1168 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +01001169 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001170
1171 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1172 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001173 ARMNN_ASSERT(layer != nullptr);
1174
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001175 for (unsigned int i = 0u; i < numOutputs; ++i)
1176 {
Mike Kelly04d82292023-01-19 18:29:40 +00001177 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[0], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01001178 }
1179
1180 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1181 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1182
1183 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1184 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +01001185}
1186
mathad01b392e982021-04-07 12:07:30 +01001187void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1188{
1189 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1190
1191 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1192 CHECK_VALID_SIZE(inputs.size(), 1);
1193 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1194 CHECK_VALID_SIZE(outputs.size(), 1);
1195
1196 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1197
1198 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1199 ARMNN_ASSERT(layer != nullptr);
1200
Mike Kelly377fb212023-01-10 15:55:28 +00001201 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
mathad01b392e982021-04-07 12:07:30 +01001202 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1203
1204 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1205 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1206
1207 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1208 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1209}
1210
Kevin May7d96b162021-02-03 17:38:41 +00001211void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001212{
1213 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1214
Mike Kelly0d77ae12022-01-07 17:42:27 +00001215 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1216 const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001217
1218 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1219
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001220 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1221 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1222 CHECK_VALID_SIZE(outputs.size(), 1);
1223
telsoa01c577f2c2018-08-31 09:22:23 +01001224 Convolution2dDescriptor desc;
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001225 inputs.size() == 3 ?
1226 desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
telsoa01c577f2c2018-08-31 09:22:23 +01001227 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1228 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001229 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +01001230 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1231 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001232
Mike Kelly377fb212023-01-10 15:55:28 +00001233 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1234 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001235
1236 // assuming input is NHWC
1237 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001238 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001239
1240 // assuming the filter is OHWI : Output, H, W, Input
1241 // which is essentially the same as NHWC
1242 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001243 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
telsoa01c577f2c2018-08-31 09:22:23 +01001244
Pablo Tellof0bd6832019-04-26 17:58:13 +01001245 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1246 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1247 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1248 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001249
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001250 // Add the first input and weights tensor to the registration list.
1251 // The constant weights will be added by SetupConstantLayers.
1252 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1253 std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
telsoa01c577f2c2018-08-31 09:22:23 +01001254
James Ward58dec6b2020-09-11 17:32:44 +01001255 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001256 armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
telsoa01c577f2c2018-08-31 09:22:23 +01001257
Mike Kelly0506ef02023-01-03 16:29:44 +00001258 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
telsoa01c577f2c2018-08-31 09:22:23 +01001259 {
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001260 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
telsoa01c577f2c2018-08-31 09:22:23 +01001261 }
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001262
1263 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001264 {
Mike Kelly377fb212023-01-10 15:55:28 +00001265 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001266
1267 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1268 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1269
Mike Kelly0506ef02023-01-03 16:29:44 +00001270 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001271 {
1272 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1273 }
telsoa01c577f2c2018-08-31 09:22:23 +01001274 }
1275
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001276 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001277
Mike Kelly377fb212023-01-10 15:55:28 +00001278 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001279 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001280
1281 // register the input connection slots for the layer, connections are made after all layers have been created
1282 // only the tensors for the inputs are relevant, exclude the const tensors
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001283 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001284
jimfly01c25411c2018-11-14 17:47:22 +00001285 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001286 // register the output connection slots for the layer, connections are made after all layers have been created
1287 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
Keith Davisb4dd5cc2022-04-07 11:32:00 +01001288 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
telsoa01c577f2c2018-08-31 09:22:23 +01001289}
1290
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001291// Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
Cathal Corbett80b4ef02022-05-25 11:21:11 +01001292#if defined(ARMNN_POST_TFLITE_2_4)
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001293void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1294{
1295 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1296
1297 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1298 const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1299
1300 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1301
1302 Convolution3dDescriptor desc;
1303 desc.m_BiasEnabled = false;
1304 desc.m_DataLayout = armnn::DataLayout::NDHWC;
1305 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1306 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1307 desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1308 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1309 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1310 desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1311
1312 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1313 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1314
1315 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1316 CHECK_VALID_SIZE(outputs.size(), 1);
1317
Mike Kelly377fb212023-01-10 15:55:28 +00001318 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1319 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001320
1321 // Assuming input is NDHWC
1322 unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1323 unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1324 unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1325
1326 // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1327 unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1328 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1329 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1330
1331 CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
Teresa Charlin502ab942022-03-23 17:23:07 +00001332 desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001333 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1334 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1335 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1336 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1337
Mike Kelly5880b912022-01-28 16:18:54 +00001338 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001339
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001340 auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1341
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001342 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1343 // Add the first input and weights tensor to the registration list.
1344 // The constant weights will be added by SetupConstantLayers.
1345 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1346
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001347 if (inputs.size() == 3)
1348 {
1349 desc.m_BiasEnabled = true;
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001350
1351 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1352 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001353 }
1354
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001355 armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001356 ARMNN_ASSERT(layer != nullptr);
1357
Mike Kelly377fb212023-01-10 15:55:28 +00001358 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001359 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1360
1361 // Register the input connection slots for the layer, connections are made after all layers have been created
Matthew Sloyan5d7b0a32021-10-18 13:07:49 +01001362 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001363
1364 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1365 // Register the output connection slots for the layer, connections are made after all layers have been created
1366 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1367 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1368}
Matthew Sloyan4d217c02021-10-07 11:48:58 +01001369#endif
Matthew Sloyaneb5f8102021-10-05 17:31:42 +01001370
Kevin May7d96b162021-02-03 17:38:41 +00001371void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001372{
1373 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1374
Mike Kelly0d77ae12022-01-07 17:42:27 +00001375 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1376 const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01001377
1378 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1379
1380 DepthwiseConvolution2dDescriptor desc;
telsoa01c577f2c2018-08-31 09:22:23 +01001381 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1382 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001383 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001384 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001385
1386 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1387 CHECK_VALID_SIZE(inputs.size(), 2, 3);
Cathal Corbett06902652022-04-14 17:55:11 +01001388 if (inputs.size() == 3)
1389 {
1390 desc.m_BiasEnabled = true;
1391 }
1392
telsoa01c577f2c2018-08-31 09:22:23 +01001393 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1394 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001395 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1396 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001397
Mike Kelly377fb212023-01-10 15:55:28 +00001398 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1399 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
telsoa01c577f2c2018-08-31 09:22:23 +01001400
Matteo Martincigh747ef822018-12-18 09:26:39 +00001401 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001402 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1403 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001404
1405 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001406 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1407 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1408
Pablo Tellof0bd6832019-04-26 17:58:13 +01001409 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1410 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1411 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1412 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001413
Jan Eilers53ef7952021-06-02 12:01:25 +01001414 // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
James Ward58dec6b2020-09-11 17:32:44 +01001415 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001416
Cathal Corbett06902652022-04-14 17:55:11 +01001417 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1418 // Add the first input and weights tensor to the registration list.
1419 // The constant weights will be added by SetupConstantLayers.
1420 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1421
1422 armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1423
1424 if (desc.m_BiasEnabled)
telsoa01c577f2c2018-08-31 09:22:23 +01001425 {
1426 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00001427 TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Cathal Corbett06902652022-04-14 17:55:11 +01001428
1429 // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1430 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
telsoa01c577f2c2018-08-31 09:22:23 +01001431 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001432 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001433
Mike Kelly377fb212023-01-10 15:55:28 +00001434 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
jimfly01c25411c2018-11-14 17:47:22 +00001435 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001436
1437 // register the input connection slots for the layer, connections are made after all layers have been created
1438 // only the tensors for the inputs are relevant, exclude the const tensors
Cathal Corbett06902652022-04-14 17:55:11 +01001439 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
telsoa01c577f2c2018-08-31 09:22:23 +01001440
jimfly01c25411c2018-11-14 17:47:22 +00001441 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001442 // register the output connection slots for the layer, connections are made after all layers have been created
1443 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1444 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1445}
1446
Kevin May7d96b162021-02-03 17:38:41 +00001447void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001448{
1449 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1450
1451 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1452 CHECK_VALID_SIZE(inputs.size(), 1);
1453
1454 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1455 CHECK_VALID_SIZE(outputs.size(), 1);
1456
James Ward58dec6b2020-09-11 17:32:44 +01001457 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001458
1459 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001460 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001461
Mike Kelly377fb212023-01-10 15:55:28 +00001462 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Finn Williamsed66d142019-12-06 09:55:55 +00001463 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1464
1465 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1466 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1467
1468 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1469 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1470}
1471
Teresa Charlin3ab85482021-06-08 16:59:29 +01001472void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1473{
1474 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1475
1476 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1477 CHECK_VALID_SIZE(inputs.size(), 2);
1478
1479 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1480 CHECK_VALID_SIZE(outputs.size(), 1);
1481
1482 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1483
Mike Kelly377fb212023-01-10 15:55:28 +00001484 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001485 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1486
1487 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1488
1489 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001490
1491 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1492 {
1493 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1494 }
1495 else
1496 {
1497 int32_t axis = inputs[1]->shape[0];
1498
1499 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1500
1501 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1502 {
1503 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1504 }
1505
1506 if(axis < 0)
1507 {
1508 axis = inputDimSize + axis + 1;
1509 }
1510
Rob Hughesd812a312021-08-06 13:10:53 +01001511 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001512 unsigned int inputShapeIndex = 0;
1513 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1514 {
1515 if (i == static_cast<unsigned int>(axis))
1516 {
1517 shape[i] = 1;
1518 }
1519 else
1520 {
1521 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1522 ++inputShapeIndex;
1523 }
1524 }
1525
Rob Hughesd812a312021-08-06 13:10:53 +01001526 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001527 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001528
1529 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1530 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001531
1532 reshapeDesc.m_TargetShape = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0}).GetShape();
1533 outputTensorInfo.SetShape(reshapeDesc.m_TargetShape);
1534
Teresa Charlin3ab85482021-06-08 16:59:29 +01001535 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1536
1537 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1538 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1539
1540 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1541 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1542}
1543
Kevin May7d96b162021-02-03 17:38:41 +00001544void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001545{
1546 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1547
1548 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001549 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001550
1551 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1552 CHECK_VALID_SIZE(outputs.size(), 1);
1553
James Ward58dec6b2020-09-11 17:32:44 +01001554 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001555 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001556
josh minorba424d22019-11-13 10:55:17 -06001557 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001558 {
Mike Kelly377fb212023-01-10 15:55:28 +00001559 armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Kevin May85d92602019-09-27 17:21:06 +01001560 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001561 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1562 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001563 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001564 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001565
Mike Kelly08759e22020-03-02 11:41:31 +00001566 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001567 }
Mike Kelly377fb212023-01-10 15:55:28 +00001568 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Keith Davis4cd29a02019-09-09 14:49:20 +01001569
James Conroy05102392020-06-24 15:39:55 +01001570 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001571 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001572
1573 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1574 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001575 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1576
1577 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1578 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1579
1580 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1581 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1582}
1583
Kevin May7d96b162021-02-03 17:38:41 +00001584void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001585{
1586 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1587
Mike Kelly0d77ae12022-01-07 17:42:27 +00001588 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1589 const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001590
1591 TransposeConvolution2dDescriptor desc;
1592 desc.m_BiasEnabled = false;
1593 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1594 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1595 desc.m_DataLayout = armnn::DataLayout::NHWC;
1596
1597 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001598 if (inputs.size() == 4)
1599 {
1600 desc.m_BiasEnabled = true;
1601 }
1602 else
1603 {
1604 CHECK_VALID_SIZE(inputs.size(), 3);
1605 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001606
1607 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1608 CHECK_VALID_SIZE(outputs.size(), 1);
1609
Ryan OSheaf0a35b82023-02-21 18:32:30 +00001610 // This block determines the output shape of the transpose convolution. If the output shape tensor ptr is not null
1611 // And the tensor is a constant, we can access the data at load time and set the output shape of the
1612 // layer. If this is not constant, We do not have access to the shape data, so we have to use
1613 // infer output shape and skip this code block.
1614 if (inputs[0] && IsConstTensor(inputs[0]))
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001615 {
Mike Kelly377fb212023-01-10 15:55:28 +00001616 armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001617 std::vector<int> output_shape(tensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00001618
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001619 if (tensorInfo.GetDataType() == DataType::Signed32)
1620 {
1621 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1622 }
1623 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1624 {
1625 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1626 {
1627 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1628 }
1629 }
1630 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1631 for (int dimension : output_shape)
1632 {
1633 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1634 }
1635 desc.m_OutputShapeEnabled = true;
1636 }
Mike Kelly377fb212023-01-10 15:55:28 +00001637 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1638 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001639
1640 // TfLite uses NHWC tensors
1641 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1642 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1643
1644 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1645 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1646
1647 CalcPadding(inputHeight,
1648 filterHeight,
1649 desc.m_StrideY,
1650 1, // DilationY
1651 desc.m_PadTop,
1652 desc.m_PadBottom,
1653 options->padding);
1654
1655 CalcPadding(inputWidth,
1656 filterWidth,
1657 desc.m_StrideX,
1658 1, // DilationX
1659 desc.m_PadLeft,
1660 desc.m_PadRight,
1661 options->padding);
1662
Mike Kelly5880b912022-01-28 16:18:54 +00001663 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001664
1665 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001666 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001667
David Monahan61683802021-01-12 09:11:07 +00001668 if (desc.m_BiasEnabled)
1669 {
Mike Kelly377fb212023-01-10 15:55:28 +00001670 auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Mike Kelly5880b912022-01-28 16:18:54 +00001671 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
David Monahan61683802021-01-12 09:11:07 +00001672 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001673 filterTensorAndData.first,
1674 biasConstTensor.first,
David Monahan61683802021-01-12 09:11:07 +00001675 layerName.c_str());
1676 }
1677 else
1678 {
1679 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Mike Kelly5880b912022-01-28 16:18:54 +00001680 filterTensorAndData.first,
David Monahan61683802021-01-12 09:11:07 +00001681 EmptyOptional(),
1682 layerName.c_str());
1683 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001684
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001685 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001686
Mike Kelly377fb212023-01-10 15:55:28 +00001687 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001688 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1689
1690 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1691 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001692 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001693
1694 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1695 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1696}
1697
Kevin May7d96b162021-02-03 17:38:41 +00001698void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001699{
1700 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1701}
1702
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001703void TfLiteParserImpl::ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex)
1704{
1705 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1706
1707 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1708 CHECK_VALID_SIZE(inputs.size(), 2);
1709
1710 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1711 CHECK_VALID_SIZE(outputs.size(), 1);
1712
1713 auto layerName = fmt::format("BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1714
Mike Kelly377fb212023-01-10 15:55:28 +00001715 TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1716 TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001717
1718 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1719 const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1720
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001721 // Adjoint in tensorflow lite performs transpose operation
1722 BatchMatMulDescriptor descriptor(options->adj_x,
1723 options->adj_y,
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001724 false,
Teresa Charlinbc37a6b2022-09-22 10:12:58 +01001725 false);
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001726 // Arbitrary DataLayout
1727
1728 IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1729 ARMNN_ASSERT(layer != nullptr);
1730
Mike Kelly377fb212023-01-10 15:55:28 +00001731 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Samuel Yapfd3ba5a2022-08-24 17:04:34 +01001732 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1733
1734 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1735 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1736
1737 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1738 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1739}
1740
Kevin May7d96b162021-02-03 17:38:41 +00001741void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001742{
1743 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1744
1745 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1746 CHECK_VALID_SIZE(inputs.size(), 3);
1747
1748 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1749 CHECK_VALID_SIZE(outputs.size(), 1);
1750
Mike Kelly377fb212023-01-10 15:55:28 +00001751 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001752 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1753
Mike Kelly377fb212023-01-10 15:55:28 +00001754 armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001755 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1756
1757 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1758 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1759
1760 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1761 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1762
1763 size_t step = 2;
1764 std::vector<std::pair<unsigned int, unsigned int>> crops;
1765 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1766 {
1767 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1768 }
1769
1770 armnn::BatchToSpaceNdDescriptor desc;
1771 desc.m_BlockShape = blockShape;
1772 desc.m_Crops = crops;
1773 desc.m_DataLayout = armnn::DataLayout::NHWC;
1774
James Ward58dec6b2020-09-11 17:32:44 +01001775 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001776
Mike Kelly377fb212023-01-10 15:55:28 +00001777 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01001778
1779 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1780 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001781
1782 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1783 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001784 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1785
1786 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1787 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1788
1789 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1790 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1791}
1792
Kevin May7d96b162021-02-03 17:38:41 +00001793void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001794{
1795 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1796
1797 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1798 CHECK_VALID_SIZE(inputs.size(), 1);
1799
1800 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1801 CHECK_VALID_SIZE(outputs.size(), 1);
1802
1803 L2NormalizationDescriptor desc;
1804 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001805 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001806 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1807
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001808 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001809
Mike Kelly377fb212023-01-10 15:55:28 +00001810 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Jackson28c94572019-07-18 10:47:03 +01001811 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1812
1813 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1814 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1815
1816 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1817 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1818}
1819
Kevin May7d96b162021-02-03 17:38:41 +00001820void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001821{
1822 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1823}
1824
Kevin May7d96b162021-02-03 17:38:41 +00001825void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001826{
1827 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1828
1829 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1830 CHECK_VALID_SIZE(inputs.size(), 2);
1831
1832 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1833 CHECK_VALID_SIZE(outputs.size(), 1);
1834
James Ward58dec6b2020-09-11 17:32:44 +01001835 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001836
Mike Kelly377fb212023-01-10 15:55:28 +00001837 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1838 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01001839 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001840
James Conroy05102392020-06-24 15:39:55 +01001841 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1842 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001843
1844 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1845 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001846 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1847
1848 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001849 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001850
1851 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1852 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1853}
1854
Kevin May7d96b162021-02-03 17:38:41 +00001855void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001856{
1857 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1858
1859 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1860 CHECK_VALID_SIZE(inputs.size(), 2);
1861
1862 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1863 CHECK_VALID_SIZE(outputs.size(), 1);
1864
James Ward58dec6b2020-09-11 17:32:44 +01001865 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001866
Mike Kelly377fb212023-01-10 15:55:28 +00001867 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1868 TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
James Conroy05102392020-06-24 15:39:55 +01001869 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001870
James Conroy05102392020-06-24 15:39:55 +01001871 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1872 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001873
1874 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1875 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001876 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1877
1878 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001879 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001880
1881 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1882 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1883}
1884
Kevin May7d96b162021-02-03 17:38:41 +00001885void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1886 size_t operatorIndex,
1887 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001888{
1889 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1890
Mike Kelly0d77ae12022-01-07 17:42:27 +00001891 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1892 const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001893
1894 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1895
1896 std::string layerName;
1897
1898 switch (algorithm)
1899 {
1900 case PoolingAlgorithm::Average:
1901 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001902 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001903 break;
1904 case PoolingAlgorithm::Max:
1905 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001906 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001907 break;
1908 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001909 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001910 }
1911
1912 Pooling2dDescriptor desc;
1913
1914 desc.m_PoolType = algorithm;
1915 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1916 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1917 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1918 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1919 desc.m_PaddingMethod = PaddingMethod::Exclude;
1920 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001921 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001922
1923 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1924 CHECK_VALID_SIZE(inputs.size(), 1);
Mike Kelly377fb212023-01-10 15:55:28 +00001925 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001926
1927 // assuming input is NHWC
1928 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1929 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1930
Pablo Tellof0bd6832019-04-26 17:58:13 +01001931 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1932 desc.m_PadTop, desc.m_PadBottom, options->padding);
1933 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1934 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001935
1936 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1937 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001938
James Conroy05102392020-06-24 15:39:55 +01001939 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1940 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00001941
1942 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1943 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
jimfly01c25411c2018-11-14 17:47:22 +00001944 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001945
1946 // register the input connection slots for the layer, connections are made after all layers have been created
1947 // only the tensors for the inputs are relevant, exclude the const tensors
1948 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001949 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001950
jimfly01c25411c2018-11-14 17:47:22 +00001951 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001952 // register the output connection slots for the layer, connections are made after all layers have been created
1953 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1954 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1955}
1956
Kevin May7d96b162021-02-03 17:38:41 +00001957void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001958{
1959 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1960
1961 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1962 CHECK_VALID_SIZE(inputs.size(), 3);
1963 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1964 CHECK_VALID_SIZE(outputs.size(), 1);
1965
1966 SliceDescriptor desc;
1967
1968 // set begin tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00001969 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
josh minorba424d22019-11-13 10:55:17 -06001970 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1971
1972 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1973 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1974
1975 // set size tensor info for slice descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00001976 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
josh minorba424d22019-11-13 10:55:17 -06001977 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1978
Cathal Corbettde33dda2022-09-20 16:40:09 +01001979 std::vector<int> signedSize(sizeTensorInfo.GetNumElements(), 1);
1980
1981 // if size buffer data is not specified, all contents of size vector remain as values of 1
1982 if (sizeBufferPtr->data.data())
1983 {
1984 ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1985 }
1986
josh minorba424d22019-11-13 10:55:17 -06001987 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
Mike Kelly377fb212023-01-10 15:55:28 +00001988 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly7ba84d62021-09-10 15:27:19 +01001989
1990 for (unsigned int i = 0; i < signedSize.size(); ++i)
1991 {
1992 int signedValue = signedSize[i];
Jim Flynnfca233e2021-09-23 12:16:53 +01001993
Mike Kelly7ba84d62021-09-10 15:27:19 +01001994 if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
1995 {
1996 throw ParseException(fmt::format("Invalid value for size {} size must be in range "
1997 "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
1998 signedValue,
1999 inputTensorInfo.GetShape()[i] - begin[i],
2000 CHECK_LOCATION().AsString()));
2001 }
2002
2003 if (signedValue == -1)
2004 {
2005 size[i] = inputTensorInfo.GetShape()[i] - begin[i];
2006 }
2007 else
2008 {
2009 size[i] = static_cast<unsigned int>(signedValue);
2010 }
2011 }
2012
josh minorba424d22019-11-13 10:55:17 -06002013 desc = SliceDescriptor(begin, size);
2014
James Ward58dec6b2020-09-11 17:32:44 +01002015 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06002016
James Conroy05102392020-06-24 15:39:55 +01002017 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
Mike Kelly377fb212023-01-10 15:55:28 +00002018
2019 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2020 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
josh minorba424d22019-11-13 10:55:17 -06002021 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2022
2023 // register the input connection slots for the layer, connections are made after all layers have been created
2024 // only the tensors for the inputs are relevant, exclude the const tensors
2025 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2026 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2027
2028 // register the output connection slots for the layer, connections are made after all layers have been created
2029 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2030 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2031}
2032
Kevin May7d96b162021-02-03 17:38:41 +00002033void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002034{
2035 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002036 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2037 const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
telsoa01c577f2c2018-08-31 09:22:23 +01002038
2039 SoftmaxDescriptor desc;
2040 desc.m_Beta = options->beta;
2041
2042 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2043 CHECK_VALID_SIZE(inputs.size(), 1);
2044 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2045 CHECK_VALID_SIZE(outputs.size(), 1);
2046
James Ward58dec6b2020-09-11 17:32:44 +01002047 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002048 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2049
Mike Kelly377fb212023-01-10 15:55:28 +00002050 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
telsoa01c577f2c2018-08-31 09:22:23 +01002051 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2052
2053 // register the input connection slots for the layer, connections are made after all layers have been created
2054 // only the tensors for the inputs are relevant, exclude the const tensors
2055 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2056 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2057
2058 // register the output connection slots for the layer, connections are made after all layers have been created
2059 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2060 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2061}
2062
Teresa Charlinfd33a692022-06-29 15:35:57 +01002063void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
2064{
2065 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2066
2067 LogSoftmaxDescriptor desc;
2068
2069 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2070 CHECK_VALID_SIZE(inputs.size(), 1);
2071 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2072 CHECK_VALID_SIZE(outputs.size(), 1);
2073
2074 auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2075 IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2076
Mike Kelly377fb212023-01-10 15:55:28 +00002077 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Teresa Charlinfd33a692022-06-29 15:35:57 +01002078 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2079
2080 // register the input connection slots for the layer, connections are made after all layers have been created
2081 // only the tensors for the inputs are relevant, exclude the const tensors
2082 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2083 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2084
2085 // register the output connection slots for the layer, connections are made after all layers have been created
2086 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2087 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2088}
2089
Kevin May7d96b162021-02-03 17:38:41 +00002090void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002091{
2092 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2093
2094 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2095 CHECK_VALID_SIZE(inputs.size(), 3);
2096
2097 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2098 CHECK_VALID_SIZE(outputs.size(), 1);
2099
Mike Kelly377fb212023-01-10 15:55:28 +00002100 armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002101 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2102
Mike Kelly377fb212023-01-10 15:55:28 +00002103 armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002104 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2105
2106 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
2107 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
2108
2109 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
2110 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
2111
2112 size_t step = 2;
2113 std::vector<std::pair<unsigned int, unsigned int>> padList;
2114 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
2115 {
2116 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2117 }
2118
2119 armnn::SpaceToBatchNdDescriptor desc;
2120 desc.m_BlockShape = blockShape;
2121 desc.m_PadList = padList;
2122 desc.m_DataLayout = armnn::DataLayout::NHWC;
2123
James Ward58dec6b2020-09-11 17:32:44 +01002124 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002125
Mike Kelly377fb212023-01-10 15:55:28 +00002126 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01002127
2128 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2129 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002130
2131 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2132 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalvesbaded142019-02-08 19:02:48 -02002133 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2134
2135 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2136 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2137
2138 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2139 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2140}
2141
Teresa Charlin2a764ad2023-02-24 18:17:31 +00002142void TfLiteParserImpl::ParseSpaceToDepth(size_t subgraphIndex, size_t operatorIndex)
2143{
2144 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2145
2146 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2147 CHECK_VALID_SIZE(inputs.size(), 1);
2148 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2149 CHECK_VALID_SIZE(outputs.size(), 1);
2150
2151 armnn::SpaceToDepthDescriptor descriptor;
2152
2153 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2154 const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2155 auto blockSize = options->block_size;
2156 if (blockSize < 2)
2157 {
2158 throw ParseException(
2159 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
2160 blockSize,
2161 CHECK_LOCATION().AsString()));
2162 }
2163 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2164
2165 auto layerName = fmt::format("SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2166 IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2167 ARMNN_ASSERT(layer != nullptr);
2168 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2169 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2170
2171 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2172 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2173
2174 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2175 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2176}
2177
Teresa Charlin3ab85482021-06-08 16:59:29 +01002178armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Mike Kelly0d77ae12022-01-07 17:42:27 +00002179 const armnn::TensorInfo& inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01002180{
Teresa Charlin3ab85482021-06-08 16:59:29 +01002181 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01002182 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2183
2184 if (inputTensorInfo.GetNumDimensions() > 4)
2185 {
2186 std::stringstream ss;
2187 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2188 << " shape:" << inputTensorInfo.GetShape() << " "
2189 << CHECK_LOCATION().AsString();
2190 throw ParseException(ss.str());
2191 }
2192
2193 if (squeezeDims.empty())
2194 {
2195 squeezeDims.assign(dimensionSequence,
2196 dimensionSequence+inputTensorInfo.GetNumDimensions());
2197 }
2198
2199 std::vector<uint32_t> outputDims;
2200 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2201 {
2202 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2203 auto currentDimension = inputTensorInfo.GetShape()[i];
2204 if (skipSqueeze || currentDimension != 1)
2205 {
2206 outputDims.push_back(currentDimension);
2207 }
2208 }
2209
2210 if (outputDims.size() > 4)
2211 {
2212 std::stringstream ss;
2213 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2214 << " shape:" << inputTensorInfo.GetShape() << " "
2215 << CHECK_LOCATION().AsString();
2216 throw ParseException(ss.str());
2217 }
2218
2219 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2220 outputDims.data());
2221
2222 // we need to preserve the tensor type and the quantization data as well
2223 TensorInfo outTensorInfo = inputTensorInfo;
2224 outTensorInfo.SetShape(outShape);
2225
2226 return outTensorInfo;
2227}
2228
Keith Davis0176fd82021-06-01 17:36:32 +01002229void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2230{
2231 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2232
2233 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2234 CHECK_VALID_SIZE(inputs.size(), 1);
2235 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2236 CHECK_VALID_SIZE(outputs.size(), 1);
2237
2238 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2239
2240 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2241 ARMNN_ASSERT(layer != nullptr);
2242
Mike Kelly377fb212023-01-10 15:55:28 +00002243 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Keith Davis0176fd82021-06-01 17:36:32 +01002244 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2245
2246 // Check if output tensor type is Signed32 or Signed64
2247 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2248 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2249 {
2250 throw ParseException(
2251 fmt::format(
2252 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2253 CHECK_LOCATION().AsString()));
2254 }
2255
2256 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2257 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2258
2259 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2260 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2261}
2262
Kevin May7d96b162021-02-03 17:38:41 +00002263void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01002264{
2265 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2266
2267 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2268 CHECK_VALID_SIZE(inputs.size(), 1);
2269
2270 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2271 CHECK_VALID_SIZE(outputs.size(), 1);
2272
2273 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2274 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002275 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01002276
Mike Kelly377fb212023-01-10 15:55:28 +00002277 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Teresa Charlin3ab85482021-06-08 16:59:29 +01002278
2279 std::vector<uint32_t> squeezeDim;
2280 // A single negative dim index is interpreted as a negative index in python
2281 // Meaning the index will be the shape size plus the negative index value
2282 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2283 {
2284 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2285 squeezeDim.push_back(static_cast<uint32_t>(dim));
2286 }
2287 else
2288 {
2289 squeezeDim = AsUnsignedVector(options->squeeze_dims);
2290 }
2291
2292 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2293
James Conroy05102392020-06-24 15:39:55 +01002294 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01002295
2296 ReshapeDescriptor reshapeDesc;
2297 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2298
Mike Kellyb2293702023-02-14 17:16:12 +00002299 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
2300 m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2301
telsoa01c577f2c2018-08-31 09:22:23 +01002302 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002303 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01002304 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2305
2306 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2307 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2308
2309 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2310 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2311}
2312
Kevin May7d96b162021-02-03 17:38:41 +00002313void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002314{
2315 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2316
2317 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2318 CHECK_VALID_SIZE(inputs.size(), 4);
2319
2320 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2321 CHECK_VALID_SIZE(outputs.size(), 1);
2322
Mike Kelly0d77ae12022-01-07 17:42:27 +00002323 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2324 const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002325
2326 StridedSliceDescriptor desc;
2327 desc.m_BeginMask = options->begin_mask;
2328 desc.m_EllipsisMask = options->ellipsis_mask;
2329 desc.m_EndMask = options->end_mask;
2330 desc.m_NewAxisMask = options->new_axis_mask;
2331 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2332 desc.m_DataLayout = armnn::DataLayout::NHWC;
2333
Mike Kelly377fb212023-01-10 15:55:28 +00002334 armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002335 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2336
2337 std::vector<int> begin(beginTensorInfo.GetNumElements());
2338 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2339
Mike Kelly377fb212023-01-10 15:55:28 +00002340 armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002341 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2342
2343 std::vector<int> end(endTensorInfo.GetNumElements());
2344 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2345
Mike Kelly377fb212023-01-10 15:55:28 +00002346 armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002347 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2348
2349 std::vector<int> stride(strideTensorInfo.GetNumElements());
2350 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2351
2352 desc.m_Begin = begin;
2353 desc.m_End = end;
2354 desc.m_Stride = stride;
2355
James Ward58dec6b2020-09-11 17:32:44 +01002356 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002357 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002358 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002359
Mike Kelly377fb212023-01-10 15:55:28 +00002360 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves451d95b2019-02-12 22:59:22 -02002361 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2362
2363 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2364 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2365
2366 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2367 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2368}
2369
Kevin May7d96b162021-02-03 17:38:41 +00002370void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002371{
2372 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2373
Mike Kelly0d77ae12022-01-07 17:42:27 +00002374 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2375 const auto* options = operatorPtr->builtin_options.AsSubOptions();
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002376
2377 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2378 CHECK_VALID_SIZE(inputs.size(), 2);
2379
2380 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2381 CHECK_VALID_SIZE(outputs.size(), 1);
2382
Mike Kelly377fb212023-01-10 15:55:28 +00002383 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2384 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002385
James Ward58dec6b2020-09-11 17:32:44 +01002386 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002387 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002388 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002389
Mike Kelly377fb212023-01-10 15:55:28 +00002390 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002391 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2392
2393 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002394 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02002395
2396 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2397
2398 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2399 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2400}
2401
Kevin May7d96b162021-02-03 17:38:41 +00002402void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302403{
2404 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2405
Mike Kelly0d77ae12022-01-07 17:42:27 +00002406 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2407 const auto* options = operatorPtr->builtin_options.AsDivOptions();
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302408
2409 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2410 CHECK_VALID_SIZE(inputs.size(), 2);
2411
2412 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2413 CHECK_VALID_SIZE(outputs.size(), 1);
2414
Mike Kelly377fb212023-01-10 15:55:28 +00002415 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2416 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302417
James Ward58dec6b2020-09-11 17:32:44 +01002418 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302419 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002420 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302421
Mike Kelly377fb212023-01-10 15:55:28 +00002422 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302423 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2424
2425 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002426 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05302427 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2428
2429 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2430 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2431}
2432
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002433void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2434{
2435 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2436
2437 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2438 CHECK_VALID_SIZE(inputs.size(), 2);
2439
2440 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2441 CHECK_VALID_SIZE(outputs.size(), 1);
2442
Mike Kelly377fb212023-01-10 15:55:28 +00002443 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2444 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002445
2446 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2447 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
2448 ARMNN_ASSERT(layer != nullptr);
2449
Mike Kelly377fb212023-01-10 15:55:28 +00002450 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlincdbd40b2022-02-25 13:21:55 +00002451 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2452
2453 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2454 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2455 layer = AddFusedFloorLayer(layer, 0);
2456
2457 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2458 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2459}
2460
Kevin May7d96b162021-02-03 17:38:41 +00002461void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002462{
2463 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2464
Mike Kelly0d77ae12022-01-07 17:42:27 +00002465 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2466 const auto* options = operatorPtr->builtin_options.AsAddOptions();
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002467
2468 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2469 CHECK_VALID_SIZE(inputs.size(), 2);
2470
2471 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2472 CHECK_VALID_SIZE(outputs.size(), 1);
2473
Mike Kelly377fb212023-01-10 15:55:28 +00002474 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2475 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002476
James Ward58dec6b2020-09-11 17:32:44 +01002477 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002478 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002479 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002480
Mike Kelly377fb212023-01-10 15:55:28 +00002481 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002482 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2483
2484 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002485 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02002486 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2487
2488 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2489 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2490}
2491
Kevin May7d96b162021-02-03 17:38:41 +00002492void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002493{
2494 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2495
Mike Kelly0d77ae12022-01-07 17:42:27 +00002496 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2497 const auto* options = operatorPtr->builtin_options.AsMulOptions();
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002498
2499 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2500 CHECK_VALID_SIZE(inputs.size(), 2);
2501
2502 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2503 CHECK_VALID_SIZE(outputs.size(), 1);
2504
Mike Kelly377fb212023-01-10 15:55:28 +00002505 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2506 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -02002507
James Ward58dec6b2020-09-11 17:32:44 +01002508 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002509 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002510 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002511
Mike Kelly377fb212023-01-10 15:55:28 +00002512 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002513 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2514
2515 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002516 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002517 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2518
2519 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2520 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2521}
2522
Kevin May7d96b162021-02-03 17:38:41 +00002523void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002524{
2525 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2526
2527 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2528
2529 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2530 CHECK_VALID_SIZE(outputs.size(), 1);
2531
Mike Kelly377fb212023-01-10 15:55:28 +00002532 armnn::TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002533 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2534
2535 armnn::MeanDescriptor desc;
2536 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2537 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2538 desc.m_Axis = axis;
2539
Mike Kelly377fb212023-01-10 15:55:28 +00002540 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002541 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002542
2543 desc.m_KeepDims =
Mike Kelly377fb212023-01-10 15:55:28 +00002544 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002545 true : false;
2546
James Ward58dec6b2020-09-11 17:32:44 +01002547 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002548 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002549 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002550
Mike Kelly377fb212023-01-10 15:55:28 +00002551 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002552 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2553
2554 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2555 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2556
2557 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2558 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2559}
2560
Kevin May7d96b162021-02-03 17:38:41 +00002561void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002562{
2563 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2564
Kevin May7d96b162021-02-03 17:38:41 +00002565 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002566
Kevin May7d96b162021-02-03 17:38:41 +00002567 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002568 CHECK_VALID_SIZE(outputs.size(), 1);
2569
Mike Kelly377fb212023-01-10 15:55:28 +00002570 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2571 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002572
Mike Kelly0d77ae12022-01-07 17:42:27 +00002573 std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002574
2575 size_t step = 2;
2576 armnn::PadDescriptor desc;
Mike Kelly0d77ae12022-01-07 17:42:27 +00002577 auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2578
2579 if (opcode == tflite::BuiltinOperator_PAD)
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002580 {
Mike Kelly0d77ae12022-01-07 17:42:27 +00002581 CHECK_VALID_SIZE(inputs.size(), 2);
2582
2583 if (inputTensorInfo.IsQuantized())
2584 {
2585 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2586 }
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002587 }
Mike Kelly0d77ae12022-01-07 17:42:27 +00002588 else if (opcode == tflite::BuiltinOperator_PADV2)
2589 {
2590 CHECK_VALID_SIZE(inputs.size(), 3);
2591
Mike Kelly377fb212023-01-10 15:55:28 +00002592 armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002593
2594 if (padValueTensorInfo.GetNumElements() != 1)
2595 {
2596 ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2597 }
2598 BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2599
2600 // Get the pad value from the input tensor
2601 if (padValueBufferPtr->data.size() > 0)
2602 {
2603 switch (padValueTensorInfo.GetDataType())
2604 {
2605 case armnn::DataType::Float32:
2606 {
2607 std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2608 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2609 desc.m_PadValue = padValueBuffer[0];
2610 break;
2611 }
2612 case armnn::DataType::QAsymmU8:
2613 {
2614 std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2615 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2616 desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2617 padValueTensorInfo.GetQuantizationScale(),
2618 padValueTensorInfo.GetQuantizationOffset());
2619 break;
2620 }
2621 case armnn::DataType::QAsymmS8:
2622 case armnn::DataType::QSymmS8:
2623 {
2624 std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2625 ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2626 desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2627 padValueTensorInfo.GetQuantizationScale(),
2628 padValueTensorInfo.GetQuantizationOffset());
2629 break;
2630 }
2631 default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2632 }
2633 }
2634 else if (inputTensorInfo.IsQuantized())
2635 {
2636 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2637 }
2638 }
2639
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002640 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2641 {
2642 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2643 }
2644
Mike Kelly0d77ae12022-01-07 17:42:27 +00002645 auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2646 : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01002647
2648 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2649 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002650 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002651 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2652
2653 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2654 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2655
2656 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2657 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2658}
2659
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002660void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2661{
2662 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2663
2664 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2665 CHECK_VALID_SIZE(inputs.size(), 2);
2666
2667 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2668 CHECK_VALID_SIZE(outputs.size(), 1);
2669
Mike Kelly377fb212023-01-10 15:55:28 +00002670 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002671
Mike Kelly377fb212023-01-10 15:55:28 +00002672 armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002673 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2674
2675 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2676 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2677
2678 size_t step = 2;
2679 armnn::PadDescriptor desc;
2680 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2681 {
2682 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2683 }
2684
2685 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2686 const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2687
2688 if (options->mode == tflite::MirrorPadMode_REFLECT)
2689 {
2690 desc.m_PaddingMode = PaddingMode::Reflect;
2691 }
2692 else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2693 {
2694 desc.m_PaddingMode = PaddingMode::Symmetric;
2695 }
2696 else
2697 {
2698 ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2699 }
2700
2701 // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2702 // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2703 auto inputShape = inputTensorInfo.GetShape();
2704 auto padList = desc.m_PadList;
2705
2706 const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2707 for(unsigned int i = 0; i < padList.size(); ++i)
2708 {
2709 if(padList.at(i).first > (inputShape[i] - isReflect) ||
2710 padList.at(i).second > (inputShape[i] - isReflect))
2711 {
2712 ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2713 "equal (Symmetric) to the dimension size.");
2714 }
2715 }
2716
2717 auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002718
2719 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2720 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002721 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyanaf3a4ef2021-10-22 15:48:12 +01002722 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2723
2724 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2725 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2726
2727 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2728 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2729}
2730
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002731void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2732{
2733 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2734
2735 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2736 CHECK_VALID_SIZE(inputs.size(), 2);
2737
2738 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2739 CHECK_VALID_SIZE(outputs.size(), 1);
2740
2741 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2742
Mike Kelly377fb212023-01-10 15:55:28 +00002743 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2744 armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002745
2746 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2747 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00002748
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002749
2750 if (IsConstTensor(inputs[1]))
2751 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002752 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002753 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2754 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002755
Mike Kelly5880b912022-01-28 16:18:54 +00002756 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
2757 inputTensorInfo.GetDataType());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002758 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2759 IConnectableLayer* constLayer =
Mike Kelly5880b912022-01-28 16:18:54 +00002760 m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002761 ARMNN_ASSERT(constLayer != nullptr);
2762
2763 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2764 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2765 RegisterOutputSlots(subgraphIndex,
2766 VIRTUAL_OPERATOR_ID,
2767 constLayer,
2768 { inputTensorIndexes[1] });
2769 }
2770 else
2771 {
2772 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2773 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2774 }
2775
Mike Kelly377fb212023-01-10 15:55:28 +00002776 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2777 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2778 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2779
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002780 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2781 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2782}
2783
Kevin May7d96b162021-02-03 17:38:41 +00002784void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002785{
2786 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2787
2788 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2789 CHECK_VALID_SIZE(inputs.size(), 1);
2790
2791 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2792 CHECK_VALID_SIZE(outputs.size(), 1);
2793
James Ward58dec6b2020-09-11 17:32:44 +01002794 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002795
2796 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002797 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002798
Mike Kelly377fb212023-01-10 15:55:28 +00002799 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan66dedc72019-12-10 16:32:07 +00002800 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2801
2802 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2803 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2804
2805 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2806 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2807}
Finn Williamsc42c3842019-01-22 14:18:11 +00002808
Kevin May7d96b162021-02-03 17:38:41 +00002809void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002810{
Finn Williamsc42c3842019-01-22 14:18:11 +00002811 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002812}
2813
Kevin May7d96b162021-02-03 17:38:41 +00002814void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002815{
Finn Williamsc42c3842019-01-22 14:18:11 +00002816 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2817}
Sadik Armagan58f39192018-09-17 14:14:39 +01002818
Kevin May7d96b162021-02-03 17:38:41 +00002819void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002820{
Jan Eilers2f746b32020-07-28 14:00:06 +01002821 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002822}
2823
Kevin May7d96b162021-02-03 17:38:41 +00002824void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002825{
2826 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2827}
2828
Kevin May7d96b162021-02-03 17:38:41 +00002829void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002830{
2831 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2832}
2833
Kevin May7d96b162021-02-03 17:38:41 +00002834void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002835{
2836 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2837}
2838
Kevin May7d96b162021-02-03 17:38:41 +00002839void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002840{
2841 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2842}
Finn Williamsc42c3842019-01-22 14:18:11 +00002843
Kevin May7d96b162021-02-03 17:38:41 +00002844void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002845{
2846 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002847 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002848 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002849
2850 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2851 CHECK_VALID_SIZE(inputs.size(), 1);
2852
2853 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2854 CHECK_VALID_SIZE(outputs.size(), 1);
2855
James Ward58dec6b2020-09-11 17:32:44 +01002856 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002857 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002858 activationDesc.m_Function = activationType;
2859
2860 switch (activationType)
2861 {
2862 case ActivationFunction::ReLu:
2863 {
James Ward58dec6b2020-09-11 17:32:44 +01002864 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002865 break;
2866 }
2867 case ActivationFunction::BoundedReLu:
2868 {
James Ward58dec6b2020-09-11 17:32:44 +01002869 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002870 activationDesc.m_A = 6.0f;
2871 activationDesc.m_B = 0.0f;
2872 break;
2873 }
2874 case ActivationFunction::Sigmoid:
2875 {
James Ward58dec6b2020-09-11 17:32:44 +01002876 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002877 break;
2878 }
Nina Drozd99851762019-04-09 09:37:38 +01002879 case ActivationFunction::TanH:
2880 {
James Ward58dec6b2020-09-11 17:32:44 +01002881 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002882 activationDesc.m_A = 1.0f;
2883 activationDesc.m_B = 1.0f;
2884 break;
2885 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002886 case ActivationFunction::LeakyReLu:
2887 {
James Ward58dec6b2020-09-11 17:32:44 +01002888 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00002889 const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
Sadik Armagan12239e72020-05-27 11:06:17 +01002890 activationDesc.m_A = options->alpha;
2891 break;
2892 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002893 case ActivationFunction::Elu:
2894 {
2895 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2896 activationDesc.m_A = 1.0f;
2897 break;
2898 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002899 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002900 {
James Ward58dec6b2020-09-11 17:32:44 +01002901 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002902 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002903 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002904 default:
2905 {
2906 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002907 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2908 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002909 }
2910 }
2911
2912 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002913
Mike Kelly377fb212023-01-10 15:55:28 +00002914 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan58f39192018-09-17 14:14:39 +01002915 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2916
2917 // register the input connection slots for the layer, connections are made after all layers have been created
2918 // only the tensors for the inputs are relevant, exclude the const tensors
2919 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2920 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2921
2922 // register the output connection slots for the layer, connections are made after all layers have been created
2923 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2924 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2925}
Mike Kelly0d77ae12022-01-07 17:42:27 +00002926armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo& inputTensorInfo,
2927 const std::vector<int32_t>& targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002928{
2929 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2930 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2931
2932 if (stretchDim != targetDimsIn.end())
2933 {
2934 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2935 {
2936 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002937 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002938 }
2939
2940 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002941 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002942 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2943
2944 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2945 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2946 }
2947
2948 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2949
2950 TensorInfo reshapeInfo = inputTensorInfo;
2951 reshapeInfo.SetShape(outputShape);
2952
2953 return reshapeInfo;
2954}
2955
Kevin May7d96b162021-02-03 17:38:41 +00002956void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002957{
2958 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2959
2960 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002961
2962 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2963 CHECK_VALID_SIZE(outputs.size(), 1);
2964
Mike Kelly0d77ae12022-01-07 17:42:27 +00002965 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2966 const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002967 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002968
Mike Kelly377fb212023-01-10 15:55:28 +00002969 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
kevmay0171972a82018-12-17 14:28:03 +00002970 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002971 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002972
Jan Eilersbac9b352020-07-13 13:40:24 +01002973 // Extracting new shape for the output
2974 // There are two ways it can be passed
2975 // * First is to define the target shape in the operator built-in options
2976 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002977 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002978 bool targetShapeFound = false;
2979 // Check if built-in options were given
2980 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002981 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002982 // make sure the parameter is given
2983 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002984 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002985 targetShape = options->new_shape;
2986 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002987 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002988 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002989
2990 // If there is no built-in option given or if the built-in new_shape parameter was empty
2991 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002992 {
Teresa Charlin6a056a42021-12-01 10:25:43 +00002993 // Check for a second input tensor
2994 if (inputs.size() > 1 && inputs[1] != nullptr)
Jan Eilersbac9b352020-07-13 13:40:24 +01002995 {
2996 if (inputs[1]->is_variable)
2997 {
2998 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2999 }
3000
3001 if (inputs[1]->shape.size() != 1)
3002 {
3003 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
3004 }
3005
3006 if (inputs[1]->type != tflite::TensorType_INT32)
3007 {
3008 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
3009 }
3010
Teresa Charlin6a056a42021-12-01 10:25:43 +00003011 // Extract target shape from input
3012 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3013 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Cathal Corbettd2f73232021-12-10 13:38:52 +00003014 if (values)
Sadik Armagan19a1c032021-01-20 12:17:00 +00003015 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003016 for (int i = 0; i < inputs[1]->shape[0]; ++i)
3017 {
3018 targetShape.push_back(values[i]);
3019 }
Sadik Armagan19a1c032021-01-20 12:17:00 +00003020 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003021 else
Jan Eilersbac9b352020-07-13 13:40:24 +01003022 {
Cathal Corbettd2f73232021-12-10 13:38:52 +00003023 try
3024 {
3025 // We attempt to infer during Runtime.
Mike Kelly04d82292023-01-19 18:29:40 +00003026 TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
3027
3028 if (reshapeShapes[0] == actualOutputTensorInfo.GetNumDimensions())
3029 {
3030 for (unsigned int i = 0; i < actualOutputTensorInfo.GetShape().GetNumDimensions(); ++i)
3031 {
3032 targetShape.push_back(actualOutputTensorInfo.GetShape()[i]);
3033 }
3034 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003035 // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
Mike Kelly04d82292023-01-19 18:29:40 +00003036 else if (reshapeShapes[0] > 2)
Cathal Corbettd2f73232021-12-10 13:38:52 +00003037 {
3038 throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
3039 "When inferring during runtime, the parser only supports "
3040 "shape (batch, -1) or (-1) for target shape input.",
3041 reshapeShapes[0],
3042 layerName,
3043 CHECK_LOCATION().AsString()));
3044 }
Mike Kelly04d82292023-01-19 18:29:40 +00003045 else
Cathal Corbettd2f73232021-12-10 13:38:52 +00003046 {
Mike Kelly04d82292023-01-19 18:29:40 +00003047 const int32_t numInputElements = inputTensorInfo.GetNumElements();
3048 const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
3049 if (reshapeShapes[0] == 1)
3050 {
3051 targetShape = {numInputElements};
3052 }
3053 else if (reshapeShapes[0] == 2)
3054 {
3055 targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3056 }
Cathal Corbettd2f73232021-12-10 13:38:52 +00003057 }
3058 }
3059 catch (const std::exception& exc)
3060 {
3061 ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
3062 "Reshape operation. Reshape operator target shape input buffer data "
3063 "is null. " << exc.what());
3064 }
Jan Eilersbac9b352020-07-13 13:40:24 +01003065 }
3066 }
3067 else
Derek Lambertic9e52792020-03-11 11:42:26 +00003068 {
3069 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
3070 "At least one method required");
3071 }
Derek Lambertic9e52792020-03-11 11:42:26 +00003072 }
3073
kevmay0171972a82018-12-17 14:28:03 +00003074 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00003075 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01003076
kevmay0171972a82018-12-17 14:28:03 +00003077 // Check for valid input size and that reshape parameters equal output shape
Cathal Corbett2b922e22022-09-23 15:49:24 +01003078 // The output shape can be provided to us in 2 ways:
3079 // 1. through the normal 'shape' parameter given by outputs[indx]->shape
3080 // 2. through additional parameter 'shape_signature' given by outputs[indx]->buffer.
3081 // This parameter can sometimes contain -1 value not visible in the 'shape' parameter.
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00003082 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
3083 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00003084 {
Cathal Corbett2b922e22022-09-23 15:49:24 +01003085 // Attempt to extract output shape from secondary 'shape_signature'
3086 // parameter and try to CheckShape() with this param.
3087 std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3088
3089 // if outputs[0]->shape_signature contain a -1 value, we need to compute its actual value
3090 // from reshape input in order to correctly verify reshape parameters equal output shape
3091 armnn::TensorInfo secondaryReshapeOutputTensorInfo =
3092 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, secondaryOutputTargetShape);
3093
3094 if (!CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.GetShape()))
3095 {
3096 std::stringstream ss;
3097 ss << "New shape defined in reshape parameters "
3098 << reshapeOutputTensorShape
3099 << " does not equal output shape "
3100 << actualOutputTensorInfo.GetShape()
3101 << ": "
3102 << CHECK_LOCATION().AsString();
3103 throw ParseException(ss.str());
3104 }
kevmay0171972a82018-12-17 14:28:03 +00003105 }
Mike Kelly377fb212023-01-10 15:55:28 +00003106 auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
kevmay0171972a82018-12-17 14:28:03 +00003107
Sadikb94967b2018-09-19 15:30:00 +01003108 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00003109 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Mike Kelly377fb212023-01-10 15:55:28 +00003110 m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
Sadikb94967b2018-09-19 15:30:00 +01003111
Sadikb94967b2018-09-19 15:30:00 +01003112 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003113 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00003114 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01003115
3116 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3117 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3118
3119 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3120 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3121}
3122
Kevin May7d96b162021-02-03 17:38:41 +00003123void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003124{
Sadik Armagana3b31f02019-12-05 09:08:53 +00003125 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3126}
3127
Kevin May7d96b162021-02-03 17:38:41 +00003128void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003129{
3130 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3131}
3132
Kevin May7d96b162021-02-03 17:38:41 +00003133void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00003134{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003135 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3136
3137 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3138 CHECK_VALID_SIZE(inputs.size(), 2);
3139
3140 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3141 CHECK_VALID_SIZE(outputs.size(), 1);
3142
Mike Kelly377fb212023-01-10 15:55:28 +00003143 armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003144
3145 // Data for the parsed tensor args (size) must be stored locally.
3146 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
3147
3148 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3149 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
3150
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003151 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003152 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003153 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01003154 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
3155 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003156
James Ward58dec6b2020-09-11 17:32:44 +01003157 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00003158
3159 switch (resizeMethod)
3160 {
3161 case ResizeMethod::Bilinear:
3162 {
James Ward58dec6b2020-09-11 17:32:44 +01003163 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00003164
3165 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3166 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3167
David Monahan4a0c9b92020-05-30 09:48:39 +01003168 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00003169 break;
3170 }
3171 case ResizeMethod::NearestNeighbor:
3172 {
James Ward58dec6b2020-09-11 17:32:44 +01003173 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00003174 break;
3175 }
3176 default:
3177 {
3178 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003179 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
3180 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00003181 }
3182 }
3183
Mike Kelly377fb212023-01-10 15:55:28 +00003184 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
James Conroy05102392020-06-24 15:39:55 +01003185
3186 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
3187 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00003188 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3189 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02003190 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3191
3192 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3193 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3194
3195 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3196 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3197}
3198
Kevin May7d96b162021-02-03 17:38:41 +00003199void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01003200{
3201 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3202
Mike Kelly0d77ae12022-01-07 17:42:27 +00003203 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3204 const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003205
3206 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3207
3208 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3209 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003210 auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
3211
Sadik Armagan479045b2018-10-01 11:51:37 +01003212 CHECK_VALID_SIZE(outputs.size(), 1);
3213
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003214 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
Mike Kelly377fb212023-01-10 15:55:28 +00003215 uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01003216
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003217 const unsigned int concatDimInput = static_cast<unsigned int>(
Mike Kelly377fb212023-01-10 15:55:28 +00003218 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01003219
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003220 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3221 concatDescriptor.SetConcatAxis(concatDimInput);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003222 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01003223
3224 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3225 {
Mike Kelly377fb212023-01-10 15:55:28 +00003226 TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
Sadik Armagan479045b2018-10-01 11:51:37 +01003227
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003228 // This set up concatDescriptor view origin
3229 armnnUtils::ProcessConcatInputTensorInfo(
Mike Kelly377fb212023-01-10 15:55:28 +00003230 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01003231 }
3232
James Ward58dec6b2020-09-11 17:32:44 +01003233 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01003234
Jim Flynn906f9462019-05-10 13:55:21 +01003235 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003236 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00003237 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003238 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01003239
James Conroy05102392020-06-24 15:39:55 +01003240 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003241 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01003242
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00003243 // add fused activation layer
3244 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01003245
Sadik Armagan479045b2018-10-01 11:51:37 +01003246 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3247 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3248}
3249
Kevin May7d96b162021-02-03 17:38:41 +00003250void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003251{
3252 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3253
Mike Kelly0d77ae12022-01-07 17:42:27 +00003254 const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003255 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3256
3257 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3258
3259 FullyConnectedDescriptor desc;
3260 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01003261 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003262
3263 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3264 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3265 CHECK_VALID_SIZE(outputs.size(), 1);
3266
Mike Kelly377fb212023-01-10 15:55:28 +00003267 armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003268
3269 // Fully Connected Layer accepts two dimensional weights input
3270 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3271 if (weightsDimension != 2)
3272 {
3273 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003274 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3275 "Node {}",
3276 weightsDimension,
3277 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003278 }
3279
Matthew Jackson74bf7da2019-08-16 16:51:42 +01003280 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01003281 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003282
Matthew Sloyan81beae32021-07-13 19:46:11 +01003283 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3284 // Add the first input tensor to the registration list
3285 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
Mike Kelly377fb212023-01-10 15:55:28 +00003286 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003287
3288 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3289
Matthew Sloyan81beae32021-07-13 19:46:11 +01003290 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3291 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003292
Mike Kelly0506ef02023-01-03 16:29:44 +00003293 if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003294 {
3295 m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3296 }
3297
Finn Williamsd4fa5452021-03-01 12:31:41 +00003298 if (inputs.size() == 3)
3299 {
3300 desc.m_BiasEnabled = true;
Mike Kelly377fb212023-01-10 15:55:28 +00003301 armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003302
3303 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3304 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Mike Kelly5880b912022-01-28 16:18:54 +00003305
Mike Kelly0506ef02023-01-03 16:29:44 +00003306 if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
Mike Kelly5880b912022-01-28 16:18:54 +00003307 {
3308 m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3309 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003310 }
3311
Matthew Sloyan81beae32021-07-13 19:46:11 +01003312 // Filters and biases are always passed to fully connected as inputs
3313 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003314
3315 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003316
Finn Williamsd4fa5452021-03-01 12:31:41 +00003317 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003318 if (inputTensorInfo.GetNumDimensions() > 2)
3319 {
3320 // Add reshape to flatten to 2D [batch_size, input_size],
3321 // where "input_size" corresponds to the number of inputs to the layer,
3322 // matching the second dimension of weights,
3323 // and "batch_size" is calculated by dividing the number of elements by "input_size".
3324 std::vector<unsigned int> reshapedDimensions(2);
3325 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3326 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3327
3328 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3329 {
3330 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003331 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3332 reshapedDimensions[1],
3333 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003334 }
3335
Mike Kelly377fb212023-01-10 15:55:28 +00003336 armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003337 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
Mike Kelly377fb212023-01-10 15:55:28 +00003338 inputTensorInfo = reshapedTensorInfo;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003339
James Ward58dec6b2020-09-11 17:32:44 +01003340 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00003341 armnn::ReshapeDescriptor reshapeDescriptor;
3342 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
Mike Kelly04d82292023-01-19 18:29:40 +00003343 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor,
3344 reshapeLayerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003345
3346 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3347 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3348
3349 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00003350 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3351 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3352 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003353 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00003354
3355 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003356
Mike Kelly377fb212023-01-10 15:55:28 +00003357 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3358 { inputTensorInfo.GetShape(),
3359 filterTensorInfo.GetShape() });
Mike Kelly04d82292023-01-19 18:29:40 +00003360
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003361 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3362
Mike Kelly04d82292023-01-19 18:29:40 +00003363 if (outputTensorInfo.GetNumDimensions() > 2)
3364 {
3365 // Calculate reshape to flatten to 2D [batch_size, input_size]
3366 std::vector<unsigned int> reshapedDimensions(2);
3367 reshapedDimensions[1] = filterTensorInfo.GetShape()[0];
3368 reshapedDimensions[0] = outputTensorInfo.GetNumElements() / reshapedDimensions[1];
3369 armnn::TensorInfo reshapedOutputTensorInfo = outputTensorInfo;
3370 if (outputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3371 {
3372 throw ParseException(
3373 fmt::format("Failed to deduce output tensor shape from filter size {} {}",
3374 reshapedDimensions[1],
3375 CHECK_LOCATION().AsString()));
3376 }
3377 reshapedOutputTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3378 layer->GetOutputSlot(0).SetTensorInfo(reshapedOutputTensorInfo);
3379
3380 std::string reshapeLayerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3381 layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3382 }
3383
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003384 // we need to add the activation layer and fortunately we don't need to care about the data layout
3385 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3386 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01003387
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003388 // register the output connection slots for the layer, connections are made after all layers have been created
3389 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3390 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
Mike Kelly04d82292023-01-19 18:29:40 +00003391
3392 m_TensorInfos[outputTensorIndexes[0]] = layer->GetOutputSlot(0).GetTensorInfo();
Sadik Armagan8853c1f2018-10-22 09:04:18 +01003393}
3394
Kevin May7d96b162021-02-03 17:38:41 +00003395void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00003396{
3397 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3398
Mike Kelly0d77ae12022-01-07 17:42:27 +00003399 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
keidav011b3e2ea2019-02-21 10:07:37 +00003400
3401 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3402 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3403 CHECK_VALID_SIZE(outputs.size(), 4);
3404
3405 // Obtain custom options from flexbuffers
3406 auto custom_options = operatorPtr->custom_options;
3407 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3408
3409 // Obtain descriptor information from tf lite
3410 DetectionPostProcessDescriptor desc;
3411 desc.m_MaxDetections = m["max_detections"].AsUInt32();
3412 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3413 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3414 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3415 desc.m_NumClasses = m["num_classes"].AsUInt32();
3416 desc.m_ScaleH = m["h_scale"].AsFloat();
3417 desc.m_ScaleW = m["w_scale"].AsFloat();
3418 desc.m_ScaleX = m["x_scale"].AsFloat();
3419 desc.m_ScaleY = m["y_scale"].AsFloat();
3420
keidav0107d58c72019-02-26 11:57:39 +00003421 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00003422 {
keidav0107d58c72019-02-26 11:57:39 +00003423 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00003424 }
3425 if (!(m["detections_per_class"].IsNull()))
3426 {
3427 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3428 }
3429
3430 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3431 {
3432 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3433 "must be positive and less than or equal to 1.");
3434 }
3435
Mike Kelly377fb212023-01-10 15:55:28 +00003436 armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003437 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00003438
James Ward58dec6b2020-09-11 17:32:44 +01003439 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003440 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00003441 layerName.c_str());
3442
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003443 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00003444
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003445 // The model does not specify the output shapes.
3446 // The output shapes are calculated from the max_detection and max_classes_per_detection.
3447 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
Mike Kelly377fb212023-01-10 15:55:28 +00003448 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3449 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3450 m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3451 m_OverriddenOutputShapes.push_back({ 1 });
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003452
keidav011b3e2ea2019-02-21 10:07:37 +00003453 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3454 {
Mike Kelly377fb212023-01-10 15:55:28 +00003455 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverriddenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00003456 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3457 }
3458
3459 // Register the input connection slots for the layer, connections are made after all layers have been created
3460 // only the tensors for the inputs are relevant, exclude the const tensors
3461 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3462 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3463
3464 // Register the output connection slots for the layer, connections are made after all layers have been created
3465 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3466 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3467 outputTensorIndexes[1],
3468 outputTensorIndexes[2],
3469 outputTensorIndexes[3]});
3470}
3471
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003472/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00003473void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003474{
3475 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3476
3477 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3478 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3479 CHECK_VALID_SIZE(outputs.size(), 1);
3480
3481 if (inputs.size() < 1)
3482 {
3483 throw ParseException("Pack must have at least one input.");
3484 }
3485
3486 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3487 const auto* options = operatorPtr->builtin_options.AsPackOptions();
3488
3489 StackDescriptor desc;
3490 desc.m_Axis = static_cast<uint32_t>(options->axis);
3491 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3492
3493 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
Mike Kelly377fb212023-01-10 15:55:28 +00003494 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003495 desc.m_InputShape = inputTensorInfo.GetShape();
3496
James Ward58dec6b2020-09-11 17:32:44 +01003497 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003498 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3499
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003500 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003501
Mike Kelly377fb212023-01-10 15:55:28 +00003502 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01003503 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3504
3505 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3506 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3507
3508 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3509 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3510}
3511
Mike Kelly5880b912022-01-28 16:18:54 +00003512void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3513{
3514 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3515
3516 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3517 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3518
3519 if (inputs.size() < 2)
3520 {
3521 throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3522 }
3523
3524 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3525 const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3526 const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3527 CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
Mike Kelly377fb212023-01-10 15:55:28 +00003528 auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly5880b912022-01-28 16:18:54 +00003529 auto outputTensorInfo = ToTensorInfo(outputs[0]);
3530
3531 // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3532 // Please refer to each operand at
3533 // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3534 armnn::LstmInputParams params;
3535
3536 if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3537 {
3538 params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3539 inputTensorInfo).first;
3540 }
3541
3542 params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3543 inputTensorInfo).first;
3544 params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3545 inputTensorInfo).first;
3546 params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3547 inputTensorInfo).first;
3548
3549 // Recurrent weight tensors of size {n_cell, n_output}
3550 if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3551 {
3552 params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3553 inputTensorInfo).first;
3554 }
3555
3556 params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3557 inputTensorInfo).first;
3558 params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3559 inputTensorInfo).first;
3560 params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3561 inputTensorInfo).first;
3562
3563 // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3564 if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3565 {
3566 params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3567 inputTensorInfo).first;
3568 }
3569
3570 if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3571 {
3572 params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3573 inputTensorInfo).first;
3574 }
3575
3576 if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3577 {
3578 params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3579 inputTensorInfo).first;
3580 }
3581
3582 // Gates bias tensors of size {n_cell}
3583 if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3584 {
3585 params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3586 inputTensorInfo).first;
3587 }
3588
3589 params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3590 inputTensorInfo).first;
3591 params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3592 inputTensorInfo).first;
3593 params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3594 inputTensorInfo).first;
3595
3596 // Projection weight tensor of size {n_output, n_cell}
3597 if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3598 {
3599 params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3600 inputTensorInfo).first;
3601 }
3602 // Projection bias tensor of size {n_output}
3603 if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3604 {
3605 params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3606 inputTensorInfo).first;
3607 }
3608
3609 // These state tensors are defined as variable tensors, and will be modified by this op.
3610 armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3611 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3612 armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3613 m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3614
3615 // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3616 if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3617 {
3618 params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3619 inputTensorInfo).first;
3620 }
3621
3622 if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3623 {
3624 params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3625 inputTensorInfo).first;
3626 }
3627
3628 if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3629 {
3630 params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3631 inputTensorInfo).first;
3632 }
3633
3634 if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3635 {
3636 params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3637 inputTensorInfo).first;
3638 }
3639
3640 // set the layer descriptor
3641 armnn::UnidirectionalSequenceLstmDescriptor desc;
3642 desc.m_ActivationFunc = nodeParams->fused_activation_function;
3643 desc.m_ClippingThresCell = nodeParams->cell_clip;
3644 desc.m_ClippingThresProj = nodeParams->proj_clip;
3645 desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
3646 || params.m_RecurrentToInputWeights == nullptr
3647 || params.m_InputGateBias == nullptr);
3648 desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
3649 desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
3650 desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
3651 || params.m_ForgetLayerNormWeights != nullptr
3652 || params.m_CellLayerNormWeights != nullptr
3653 || params.m_OutputLayerNormWeights != nullptr);
3654 desc.m_TimeMajor = nodeParams->time_major;
3655
Mike Kellyc0800a32022-06-15 10:57:52 +01003656 if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
Mike Kelly5880b912022-01-28 16:18:54 +00003657 {
3658 auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
3659 inputTensorInfo).first;
3660 auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
3661 desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
3662
3663 auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
3664 inputTensorInfo).first;
3665 auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
3666 desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
3667
3668 auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
3669 inputTensorInfo).first;
3670 auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
3671 desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
3672
3673 auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
3674 inputTensorInfo).first;
3675 auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
3676 desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
3677 }
3678 else
3679 {
3680 float defaultIntermediate = std::pow(2, -12);
3681 desc.m_InputIntermediateScale = defaultIntermediate;
3682 desc.m_ForgetIntermediateScale = defaultIntermediate;
3683 desc.m_CellIntermediateScale = defaultIntermediate;
3684 desc.m_OutputIntermediateScale = defaultIntermediate;
3685 }
3686
Mike Kellyc0800a32022-06-15 10:57:52 +01003687 if (operatorPtr->intermediates.size() > 4)
3688 {
3689 auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
3690 inputTensorInfo).first;
Mike Kelly5880b912022-01-28 16:18:54 +00003691
Mike Kellyc0800a32022-06-15 10:57:52 +01003692 desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
3693 desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
3694 }
Mike Kelly5880b912022-01-28 16:18:54 +00003695 unsigned int batchSize = inputTensorInfo.GetShape()[0];
3696 unsigned int outputSize = outputTensorInfo.GetShape()[2];
3697 unsigned int numUnits = cellStateInInfo.GetShape()[1];
3698
3699 armnn::DataType dataType = inputTensorInfo.GetDataType();
3700 float qScale = inputTensorInfo.GetQuantizationScale();
3701 float qOffset = inputTensorInfo.GetQuantizationOffset();
3702
3703 armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
3704 if (!desc.m_CifgEnabled)
3705 {
3706 scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
3707 }
3708 armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
3709 cellStateInInfo.GetDataType(),
3710 cellStateInInfo.GetQuantizationScale(),
3711 cellStateInInfo.GetQuantizationOffset());
3712 armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
3713
3714 armnn::LstmInputParamsInfo paramsInfo;
3715 paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
3716 paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
3717 paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
3718 paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
3719 paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
3720 paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
3721 paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
3722 paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
3723 paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
3724
3725 if (!desc.m_CifgEnabled)
3726 {
3727 paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
3728 paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
3729 if (params.m_CellToInputWeights != nullptr)
3730 {
3731 paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
3732 }
3733 paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
3734 }
3735
3736 if (desc.m_ProjectionEnabled)
3737 {
3738 paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
3739 if (params.m_ProjectionBias != nullptr)
3740 {
3741 paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
3742 }
3743 }
3744
3745 if (desc.m_PeepholeEnabled)
3746 {
3747 paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
3748 paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
3749 }
3750
3751 if (desc.m_LayerNormEnabled)
3752 {
3753 if(!desc.m_CifgEnabled)
3754 {
3755 paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
3756 }
3757 paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
3758 paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
3759 paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
3760 }
3761
3762 auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
3763 armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
3764 ARMNN_ASSERT(layer != nullptr);
3765
3766 // register the input connection slots for the layer, connections are made after all layers have been created
3767 // only the tensors for the inputs are relevant, exclude the const tensors
3768 auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
3769 operatorPtr->inputs[18],
3770 operatorPtr->inputs[19]});
3771 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
3772 inputTensorIndexes[1],
3773 inputTensorIndexes[2]});
3774
3775 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3776
3777 layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
3778 layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
3779 layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
3780
3781 unsigned int tensorIndex = outputTensorIndexes[0];
3782 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
3783 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3784}
3785
Kevin May7d96b162021-02-03 17:38:41 +00003786void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01003787{
3788 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3789
Mike Kelly0d77ae12022-01-07 17:42:27 +00003790 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3791 const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
Nina Drozd200e3802019-04-15 09:47:39 +01003792
3793 // This unpackAxis indicates the axis to unpack
3794 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
3795
3796 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3797 CHECK_VALID_SIZE(inputs.size(), 1);
3798
Mike Kelly377fb212023-01-10 15:55:28 +00003799 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003800
3801 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
3802 {
3803 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003804 fmt::format("The unpack axis: {} cannot be greater than or equal to "
3805 "the number of input dimension {} {}",
3806 unpackAxis,
3807 inputTensorInfo.GetNumDimensions(),
3808 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003809 }
3810
Nina Drozd200e3802019-04-15 09:47:39 +01003811 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
3812 // If num is not defined, automatically infer from the length of the dimension axis.
3813 if(unpackNum == 0)
3814 {
3815 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
3816 }
3817
3818 // If unpack number cannot be inferred and is still zero, throw ParseException.
3819 if(unpackNum == 0)
3820 {
3821 throw ParseException("Number to unpack must greater than zero.");
3822 }
3823
3824 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3825 CHECK_VALID_SIZE(outputs.size(), unpackNum);
3826
3827 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3828 std::vector<unsigned int> unpackDimSizes(inputDimSize);
3829
3830 // Add current input shape to unpackDimSizes
3831 for (unsigned int i = 0; i < inputDimSize; ++i)
3832 {
3833 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
3834 }
3835
3836 if (unpackDimSizes[unpackAxis] != unpackNum)
3837 {
3838 throw ParseException("Number to unpack must be the same as length of the dimension to "
3839 "unpack along.");
3840 }
3841
3842 unpackDimSizes[unpackAxis] /= unpackNum;
3843
3844 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
3845 for (unsigned int j = 0; j < unpackNum; ++j)
3846 {
3847 // Set the size of the views.
3848 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
3849 {
3850 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
3851 }
3852 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
3853 }
3854
James Ward58dec6b2020-09-11 17:32:44 +01003855 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01003856 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003857 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01003858
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003859 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
3860 unpackDimSizes.data());
3861
Nina Drozd200e3802019-04-15 09:47:39 +01003862 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3863 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3864
Finn Williamsb49ed182021-06-29 15:50:08 +01003865 std::vector<unsigned int> reshapeDims;
3866 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
3867 {
3868 if (axis != unpackAxis)
3869 {
3870 reshapeDims.push_back(splitOutShape[axis]);
3871 }
3872 }
3873
3874 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
3875
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003876 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
3877 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3878 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003879 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01003880 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003881 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01003882 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003883 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
3884
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003885 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
3886 outputTensorInfo.GetDataType(),
3887 outputTensorInfo.GetQuantizationScale(),
3888 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003889 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
3890
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01003891 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01003892
3893 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
3894 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
3895 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
3896 }
Nina Drozd200e3802019-04-15 09:47:39 +01003897}
3898
Kevin May7d96b162021-02-03 17:38:41 +00003899void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01003900{
3901 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3902
Mike Kelly0d77ae12022-01-07 17:42:27 +00003903 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3904 const auto* options = operatorPtr->builtin_options.AsSplitOptions();
Nina Drozd0324f482019-04-08 10:52:10 +01003905
3906 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
3907
Nina Drozd200e3802019-04-15 09:47:39 +01003908 // If number of splits cannot be inferred and is zero, throw ParseException.
3909 if(numSplits == 0)
3910 {
3911 throw ParseException("Number to splits must greater than zero.");
3912 }
3913
Nina Drozd0324f482019-04-08 10:52:10 +01003914 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3915 CHECK_VALID_SIZE(inputs.size(), 2);
3916 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3917 CHECK_VALID_SIZE(outputs.size(), numSplits);
3918
Mike Kelly377fb212023-01-10 15:55:28 +00003919 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3920 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003921 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01003922
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003923 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003924 if (axisBufferPtr == nullptr)
3925 {
3926 throw ParseException(
3927 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3928 CHECK_LOCATION().AsString()));
3929 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003930
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003931 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3932 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3933 int32_t axis = axisData[0];
3934
3935 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3936 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3937 {
3938 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3939 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3940 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3941 throw ParseException(
3942 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3943 axis,
3944 CHECK_LOCATION().AsString()));
3945 }
3946
3947 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01003948
Nina Drozd0324f482019-04-08 10:52:10 +01003949 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003950 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01003951 {
3952 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003953 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
3954 inputTensorInfo.GetNumDimensions(),
3955 MaxNumOfTensorDimensions,
3956 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01003957 }
3958
3959 std::vector<unsigned int> splitterDimSizes(inputDimSize);
3960
3961 // Add current input shape to splitterDimSizes
3962 for (unsigned int i = 0; i < inputDimSize; ++i)
3963 {
3964 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
3965 }
3966
3967 if (splitterDimSizes[splitDim] % numSplits != 0)
3968 {
3969 throw ParseException("Number of splits must evenly divide the dimension");
3970 }
3971 splitterDimSizes[splitDim] /= numSplits;
3972
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003973 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01003974 for (unsigned int j = 0; j < numSplits; ++j)
3975 {
3976 // Set the size of the views.
3977 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
3978 {
3979 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
3980 }
3981 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
3982 }
3983
James Ward58dec6b2020-09-11 17:32:44 +01003984 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01003985 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003986 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01003987
3988 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01003989 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01003990
Nina Drozd0324f482019-04-08 10:52:10 +01003991 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3992 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003993 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01003994 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01003995 }
3996
3997 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3998 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3999}
4000
Derek Lambertif0176992020-04-28 13:37:49 +01004001unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
4002{
4003 int numDims = armnn::numeric_cast<int>(numDimsIn);
4004 int v = idx < 0 ? numDims + idx : idx;
4005 ARMNN_ASSERT(v >= 0);
4006 ARMNN_ASSERT(v < numDims);
4007
4008 return static_cast<unsigned int>(v);
4009}
4010
Kevin May7d96b162021-02-03 17:38:41 +00004011void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01004012{
4013 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4014
Mike Kelly0d77ae12022-01-07 17:42:27 +00004015 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4016 const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01004017
4018 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4019 CHECK_VALID_SIZE(inputs.size(), 3);
4020
4021 auto& inputTensor = inputs[0];
4022 auto& splitsTensor = inputs[1];
4023 auto& axisTensor = inputs[2];
4024
4025 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
4026 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
4027 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
4028 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
4029
4030 // Inputs
4031 auto inputDimSize = inputTensorInfo.GetNumDimensions();
4032 if (inputDimSize > MaxNumOfTensorDimensions)
4033 {
4034 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004035 fmt::format("The number of dimensions: {} for input tensors of the "
4036 "SplitV op cannot be greater than {} {}",
4037 inputTensorInfo.GetNumDimensions(),
4038 MaxNumOfTensorDimensions,
4039 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01004040 }
4041
4042 // Get split axis
4043 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004044 if (axisBufferPtr == nullptr)
4045 {
4046 throw ParseException(
4047 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4048 CHECK_LOCATION().AsString()));
4049 }
4050
Derek Lambertif0176992020-04-28 13:37:49 +01004051 std::vector<int> axisData(axisTensorInfo.GetNumElements());
4052 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004053 int32_t axis = axisData[0];
4054
4055 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4056 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4057 {
4058 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4059 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4060 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4061 throw ParseException(
4062 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4063 axis,
4064 CHECK_LOCATION().AsString()));
4065 }
4066 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01004067
Derek Lambertif0176992020-04-28 13:37:49 +01004068 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01004069 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01004070 unsigned int numSplits{0};
4071
4072 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01004073 {
4074 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01004075 }
4076 else
4077 {
Ryan OShea86704732020-05-26 11:41:04 +01004078 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01004079 }
4080
4081 if (numSplits <=0)
4082 {
4083 throw ParseException("SplitV has invalid number of splits");
4084 }
4085
Jan Eilersc0761e92020-06-29 16:48:44 +01004086 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01004087 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01004088 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01004089
Jan Eilersc0761e92020-06-29 16:48:44 +01004090 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01004091 int numInferred{0};
4092 unsigned int inferIdx{0};
4093 int splitSum{0};
4094 for (auto split : splitsData)
4095 {
4096 if (split < 0)
4097 {
4098 numInferred++;
4099 inferIdx = idx;
4100 }
4101 else
4102 {
4103 splitSum += split;
4104 }
4105 idx++;
4106 }
4107 // Check for inferred Axis
4108 if (numInferred == 0)
4109 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004110 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01004111 {
4112 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
4113 }
4114 }
4115 else if (numInferred == 1)
4116 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004117 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01004118 }
4119 else
4120 {
4121 throw ParseException("Cannot infer split size for more than one split");
4122 }
4123
Derek Lambertif0176992020-04-28 13:37:49 +01004124 //Ouput size validation
4125 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4126 CHECK_VALID_SIZE(outputs.size(), numSplits);
4127
4128 // Setup Armnn descriptor
4129 SplitterDescriptor splitDesc(numSplits, inputDimSize);
4130 unsigned int accumSplit = 0;
4131 for (unsigned int j = 0; j < numSplits; ++j)
4132 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01004133 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01004134
4135 // Set the size of the views.
4136 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
4137 {
4138 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
4139 if (dimIdx == splitDim)
4140 {
4141 dimSize = splitSize;
4142 }
4143 splitDesc.SetViewSize(j, dimIdx, dimSize);
4144 }
4145
4146 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4147 accumSplit += splitSize;
4148 }
4149
James Ward58dec6b2020-09-11 17:32:44 +01004150 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01004151 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01004152 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01004153
4154 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4155 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4156
4157 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4158 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01004159 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01004160 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4161 }
4162
4163 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4164 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4165}
4166
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004167void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
4168{
4169 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
4170}
4171
Kevin May7d96b162021-02-03 17:38:41 +00004172void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09004173{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004174 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
4175}
4176
4177void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
4178{
Inki Daed4619e22020-09-10 15:33:54 +09004179 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4180 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4181 CHECK_VALID_SIZE(inputs.size(), 2);
4182
4183 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4184 CHECK_VALID_SIZE(outputs.size(), 1);
4185
Mike Kelly377fb212023-01-10 15:55:28 +00004186 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4187 armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Inki Daed4619e22020-09-10 15:33:54 +09004188 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004189 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004190
4191 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01004192 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
4193 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
4194 {
4195 throw ParseException(
4196 fmt::format(
4197 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4198 CHECK_LOCATION().AsString()));
4199 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01004200
4201 // Get const axis value from model and set it to descriptor.
4202 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4203 if (axisBufferPtr == nullptr)
4204 {
4205 throw ParseException(
4206 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4207 CHECK_LOCATION().AsString()));
4208 }
4209
4210 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4211 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4212 int32_t axis = axisData.front();
4213
4214 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4215 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4216 {
4217 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4218 // E.g. Rank 4 tensor can have axis in range [-4, 3)
4219 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4220 throw ParseException(
4221 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4222 axis,
4223 CHECK_LOCATION().AsString()));
4224 }
4225
4226 ArgMinMaxDescriptor desc;
4227 desc.m_Axis = axis;
4228 desc.m_Function = argMinMaxFunction;
4229
4230 // Register a ArgMin/ArgMax layer.
4231 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
4232 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4233 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4234 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004235 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Inki Daed4619e22020-09-10 15:33:54 +09004236 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4237
4238 // Register input tensor to the layer.
4239 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4240 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4241
4242 // Register output tensor to the layer.
4243 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4244 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4245}
4246
Kevin May7d96b162021-02-03 17:38:41 +00004247void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004248{
4249 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4250
Kevin May7d96b162021-02-03 17:38:41 +00004251 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004252 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00004253 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004254 CHECK_VALID_SIZE(outputs.size(), 1);
4255
Mike Kelly377fb212023-01-10 15:55:28 +00004256 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4257 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4258 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Sadik Armagan26868492021-01-22 14:25:31 +00004259
4260 armnn::GatherDescriptor gatherDescriptor;
4261
Mike Kelly0d77ae12022-01-07 17:42:27 +00004262 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4263 const auto* options = operatorPtr->builtin_options.AsGatherOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004264 auto axis = options->axis;
4265
Mike Kelly377fb212023-01-10 15:55:28 +00004266 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4267
Sadik Armagan26868492021-01-22 14:25:31 +00004268 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4269 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4270 auto outputDimensions = outputTensorInfo.GetNumDimensions();
4271 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4272 {
4273 throw ParseException(
4274 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4275 axis,
4276 inputDimensions, inputDimensions,
4277 CHECK_LOCATION().AsString()));
4278 }
4279 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4280 {
4281 throw ParseException(
4282 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4283 outputDimensions,
4284 inputDimensions, indicesDimensions,
4285 CHECK_LOCATION().AsString()));
4286 }
4287
4288 gatherDescriptor.m_Axis = axis;
4289
Sadik Armagan26868492021-01-22 14:25:31 +00004290 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4291 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004292 outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Sadik Armagan26868492021-01-22 14:25:31 +00004293 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4294
4295 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4296 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4297
4298 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4299 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4300}
4301
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004302void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4303{
4304 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4305
4306 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4307 CHECK_VALID_SIZE(inputs.size(), 2);
4308 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4309 CHECK_VALID_SIZE(outputs.size(), 1);
4310
Mike Kelly377fb212023-01-10 15:55:28 +00004311 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4312 armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004313
4314 auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4315 IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4316 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004317 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Teresa Charlin91a53ea2022-04-25 15:47:29 +01004318 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4319
4320 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4321 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4322
4323 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4324 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4325}
4326
Kevin May7d96b162021-02-03 17:38:41 +00004327void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00004328{
4329 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4330
Kevin May7d96b162021-02-03 17:38:41 +00004331 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004332 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00004333 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00004334 CHECK_VALID_SIZE(outputs.size(), 1);
4335
4336 armnn::DepthToSpaceDescriptor descriptor;
4337
Mike Kelly0d77ae12022-01-07 17:42:27 +00004338 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4339 const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
Sadik Armagan26868492021-01-22 14:25:31 +00004340 auto blockSize = options->block_size;
4341 if (blockSize < 2)
4342 {
4343 throw ParseException(
4344 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4345 blockSize,
4346 CHECK_LOCATION().AsString()));
4347 }
4348 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4349
4350 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4351 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4352 ARMNN_ASSERT(layer != nullptr);
Mike Kelly377fb212023-01-10 15:55:28 +00004353 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan26868492021-01-22 14:25:31 +00004354 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4355
4356 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4357 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4358
4359 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4360 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4361}
4362
Kevin May7d96b162021-02-03 17:38:41 +00004363void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004364{
Sadik Armagana2747482021-02-09 10:28:54 +00004365 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4366}
4367
Teresa Charlin4e3e8312021-08-05 12:34:37 +01004368void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4369{
4370 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4371}
4372
Sadik Armagana2747482021-02-09 10:28:54 +00004373void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4374{
4375 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4376}
4377
4378void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4379{
4380 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4381}
4382
4383void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4384{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004385 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4386
Mike Kelly0d77ae12022-01-07 17:42:27 +00004387 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4388 const auto* options = operatorPtr->builtin_options.AsReducerOptions();
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004389
4390 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4391 CHECK_VALID_SIZE(inputs.size(), 2);
4392
4393 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4394 CHECK_VALID_SIZE(outputs.size(), 1);
4395
Sadik Armagana2747482021-02-09 10:28:54 +00004396 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004397
Mike Kelly377fb212023-01-10 15:55:28 +00004398 armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4399 armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004400
4401 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004402 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4403 // Get const axis value from model and set it to descriptor.
4404 if (axisBufferPtr != nullptr)
4405 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00004406 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4407 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4408
4409 // Convert the axis to unsigned int and remove duplicates.
4410 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4411 std::set<unsigned int> uniqueAxis;
4412 std::transform(axisData.begin(),
4413 axisData.end(),
4414 std::inserter(uniqueAxis, uniqueAxis.begin()),
4415 [rank](int i)->unsigned int{
4416 return static_cast<uint32_t>(((i + rank) % rank)); });
4417 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004418 }
Sadik Armagana2747482021-02-09 10:28:54 +00004419 else
4420 {
4421 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4422 {
4423 desc.m_vAxis.push_back(i);
4424 }
4425 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004426
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004427 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00004428 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004429
4430 // Register a new layer object, Sum.
Mike Kelly0d77ae12022-01-07 17:42:27 +00004431 IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004432
Mike Kelly377fb212023-01-10 15:55:28 +00004433 armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00004434 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4435
4436 // Register input tensor to the layer.
4437 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4438 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4439
4440 // Register output tensor to the layer.
4441 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4442 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4443}
4444
Mike Kelly31dce2b2021-09-01 21:22:37 +01004445void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4446{
4447 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4448
4449 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4450 CHECK_VALID_SIZE(inputs.size(), 1);
4451
4452 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4453 CHECK_VALID_SIZE(outputs.size(), 1);
4454
4455 auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4456 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4457
Mike Kelly377fb212023-01-10 15:55:28 +00004458 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
Mike Kelly31dce2b2021-09-01 21:22:37 +01004459
4460 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4461 const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4462
4463 armnn::NormalizationDescriptor descriptor;
4464 descriptor.m_DataLayout = armnn::DataLayout::NHWC;
4465 descriptor.m_NormChannelType = armnn::NormalizationAlgorithmChannel::Across;
4466 descriptor.m_NormMethodType = armnn::NormalizationAlgorithmMethod::LocalBrightness;
4467 descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4468 descriptor.m_K = options->bias;
4469 descriptor.m_Alpha = options->alpha;
4470 descriptor.m_Beta = options->beta;
4471
4472 // ArmNN expects normSize to be the full size of the normalization
4473 // window rather than the radius as in TfLite.
4474 descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4475
4476 IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4477 ARMNN_ASSERT(layer != nullptr);
4478
Mike Kelly377fb212023-01-10 15:55:28 +00004479 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Mike Kelly31dce2b2021-09-01 21:22:37 +01004480 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4481
4482 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4483 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4484
4485 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4486 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4487}
4488
Teresa Charlin28aa6692022-07-12 11:18:44 +01004489void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4490{
4491 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4492}
4493
4494void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4495{
4496 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4497}
4498
4499void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
4500{
4501 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
4502}
4503
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004504void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4505{
4506 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4507}
4508
4509void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4510{
4511 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4512}
4513
4514void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4515{
4516 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4517}
4518
Teresa Charlin28aa6692022-07-12 11:18:44 +01004519void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
4520{
4521 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
4522}
4523
Teresa Charlinf0fce5b2022-05-04 17:24:43 +01004524void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4525{
4526 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4527}
4528
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004529void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
4530{
4531 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4532
4533 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4534 CHECK_VALID_SIZE(inputs.size(), 1);
4535
4536 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4537 CHECK_VALID_SIZE(outputs.size(), 1);
4538
4539 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
4540 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4541
4542 ElementwiseUnaryDescriptor desc;
4543 desc.m_Operation = unaryOperation;
4544 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
4545 ARMNN_ASSERT(layer != nullptr);
4546
Mike Kelly377fb212023-01-10 15:55:28 +00004547 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
Matthew Sloyaned7fce42021-04-15 20:46:24 +01004548 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4549
4550 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4551 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4552
4553 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4554 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4555}
4556
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004557void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
4558{
4559 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
4560}
4561
4562void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
4563{
4564 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
4565}
4566
4567void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
4568{
4569 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
4570}
4571
4572void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
4573{
4574 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
4575}
4576
4577void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
4578{
4579 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
4580}
4581
4582void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
4583{
4584 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
4585}
4586
4587void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
4588 ComparisonOperation comparisonOperation)
4589{
4590 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4591
4592 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4593 CHECK_VALID_SIZE(inputs.size(), 2);
4594
4595 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4596 CHECK_VALID_SIZE(outputs.size(), 1);
4597
4598 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
4599 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4600
Mike Kelly377fb212023-01-10 15:55:28 +00004601 armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4602 armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004603 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
4604
4605 ComparisonDescriptor desc;
4606 desc.m_Operation = comparisonOperation;
4607 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
4608 ARMNN_ASSERT(layer != nullptr);
4609
Mike Kelly377fb212023-01-10 15:55:28 +00004610 TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03004611 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4612
4613 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4614 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4615
4616 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4617 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4618}
4619
Mike Kelly04d82292023-01-19 18:29:40 +00004620armnn::IConnectableLayer* TfLiteParserImpl::AddReshapeLayer(armnn::IConnectableLayer* layer,
4621 unsigned int outputSlot,
4622 std::string reshapeLayerName,
4623 armnn::TensorInfo outputShape)
4624{
4625 ReshapeDescriptor desc;
4626 desc.m_TargetShape = outputShape.GetShape();
4627
4628 IConnectableLayer* reshapeLayer =
4629 m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
4630
4631 auto & prevOutputSlot = layer->GetOutputSlot(outputSlot);
4632 prevOutputSlot.Connect(reshapeLayer->GetInputSlot(0));
4633 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputShape);
4634 return reshapeLayer;
4635}
4636
Kevin May7d96b162021-02-03 17:38:41 +00004637armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
4638 unsigned int outputSlot,
4639 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01004640{
4641 ActivationDescriptor activationDesc;
4642 std::string layerName = prevLayer->GetName();
4643
4644 switch(activationType)
4645 {
4646 case tflite::ActivationFunctionType_NONE:
4647 {
4648 // this is a no-op: return previous layer
4649 return prevLayer;
4650 }
4651 case tflite::ActivationFunctionType_RELU:
4652 {
4653 activationDesc.m_Function = ActivationFunction::ReLu;
4654 layerName += ":RELU";
4655 break;
4656 }
4657 case tflite::ActivationFunctionType_RELU6:
4658 {
4659 activationDesc.m_Function = ActivationFunction::BoundedReLu;
4660 activationDesc.m_A = 6.0f;
4661 activationDesc.m_B = 0.0f;
4662 layerName += ":RELU6";
4663 break;
4664 }
4665 case tflite::ActivationFunctionType_TANH:
4666 {
4667 activationDesc.m_Function = ActivationFunction::TanH;
4668 activationDesc.m_A = 1.0f;
4669 activationDesc.m_B = 1.0f;
4670 layerName += ":TANH";
4671 break;
4672 }
4673
4674 // I only put these here as a reminder what others we could support
4675 case tflite::ActivationFunctionType_RELU_N1_TO_1:
4676 case tflite::ActivationFunctionType_SIGN_BIT:
4677 default:
4678 {
4679 throw ParseException(
Mike Kelly377fb212023-01-10 15:55:28 +00004680 fmt::format("TfLite parser doesn't support fused activation: "
James Ward58dec6b2020-09-11 17:32:44 +01004681 "{}/{} {} ",
4682 activationType,
4683 tflite::EnumNameActivationFunctionType(activationType),
4684 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004685
4686 }
4687 }
4688
4689 IConnectableLayer* activationLayer =
4690 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
4691
4692 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4693 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
4694 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
4695 return activationLayer;
4696}
4697
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004698armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
4699 unsigned int outputSlot)
4700{
Teresa Charlin725728e2022-05-05 13:33:33 +01004701
4702 auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
4703 DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
4704
4705 if (dataType == DataType::Signed32)
4706 {
4707 return prevLayer;
4708 }
4709
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004710 std::string layerName = prevLayer->GetName();
4711 IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
4712
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004713 prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
4714 floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
Teresa Charlin725728e2022-05-05 13:33:33 +01004715
Teresa Charlincdbd40b2022-02-25 13:21:55 +00004716 return floorLayer;
4717}
4718
Mike Kelly0d77ae12022-01-07 17:42:27 +00004719TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char* fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01004720{
4721 if (fileName == nullptr)
4722 {
James Ward58dec6b2020-09-11 17:32:44 +01004723 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004724 CHECK_LOCATION().AsString()));
4725 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01004726 std::error_code errorCode;
4727 fs::path pathToFile(fileName);
4728 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01004729 {
James Ward58dec6b2020-09-11 17:32:44 +01004730 //fmt::format() could not be used here (format error)
4731 std::stringstream msg;
4732 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
4733 << " " << CHECK_LOCATION().AsString();
4734
4735 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01004736 }
4737 std::ifstream file(fileName, std::ios::binary);
4738 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
4739 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
4740 fileContent.size());
4741}
4742
Mike Kelly0d77ae12022-01-07 17:42:27 +00004743TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t* binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01004744{
4745 if (binaryContent == nullptr)
4746 {
James Ward58dec6b2020-09-11 17:32:44 +01004747 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01004748 CHECK_LOCATION().AsString()));
4749 }
4750 flatbuffers::Verifier verifier(binaryContent, len);
4751 if (verifier.VerifyBuffer<tflite::Model>() == false)
4752 {
4753 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004754 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
4755 "flatbuffers format. size:{} {}",
4756 len,
4757 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004758 }
4759 return tflite::UnPackModel(binaryContent);
4760}
4761
Mike Kelly0d77ae12022-01-07 17:42:27 +00004762TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004763 size_t subgraphIndex,
4764 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004765{
4766 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4767
Mike Kelly0d77ae12022-01-07 17:42:27 +00004768 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4769 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004770
4771 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01004772 TensorRawPtrVector result;
Mike Kelly0d77ae12022-01-07 17:42:27 +00004773 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004774 {
mathad01c21025d2021-04-26 10:09:37 +01004775 // If the input location is -1 then assume input is turned off.
4776 if (operatorPtr->inputs[i] == -1)
4777 {
4778 continue;
4779 }
4780 else
4781 {
4782 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
4783 result.push_back(subgraphPtr->tensors[inputId].get());
4784 }
telsoa01c577f2c2018-08-31 09:22:23 +01004785 }
4786 return result;
4787}
4788
Mike Kelly0d77ae12022-01-07 17:42:27 +00004789TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004790 size_t subgraphIndex,
4791 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004792{
4793 CHECK_MODEL(model, subgraphIndex, operatorIndex);
4794
Mike Kelly0d77ae12022-01-07 17:42:27 +00004795 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4796 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004797
4798 size_t outputCount = operatorPtr->outputs.size();
4799 TensorRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004800 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004801 {
4802 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
4803 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004804 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01004805 }
4806 return result;
4807}
4808
Mike Kelly0d77ae12022-01-07 17:42:27 +00004809TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004810 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004811{
4812 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004813 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004814
Derek Lambertiff05cc52019-04-26 13:05:17 +01004815 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004816 TensorIdRawPtrVector result(inputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004817 for (size_t i = 0; i < inputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004818 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004819 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01004820 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01004821 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004822 }
4823 return result;
4824}
4825
Mike Kelly0d77ae12022-01-07 17:42:27 +00004826TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr& model,
Kevin May7d96b162021-02-03 17:38:41 +00004827 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004828{
4829 CHECK_SUBGRAPH(model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004830 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004831
Derek Lambertiff05cc52019-04-26 13:05:17 +01004832 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01004833 TensorIdRawPtrVector result(outputCount);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004834 for (size_t i = 0; i < outputCount; ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01004835 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01004836 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
4837 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01004838 }
4839 return result;
4840}
4841
Kevin May7d96b162021-02-03 17:38:41 +00004842std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
4843 size_t subgraphIndex,
4844 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004845{
4846 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004847 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4848 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004849 return operatorPtr->inputs;
4850}
4851
Kevin May7d96b162021-02-03 17:38:41 +00004852std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
4853 size_t subgraphIndex,
4854 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004855{
4856 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004857 const auto& subgraphPtr = model->subgraphs[subgraphIndex];
4858 const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01004859 return operatorPtr->outputs;
4860}
4861
Kevin May7d96b162021-02-03 17:38:41 +00004862void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
4863 size_t operatorIndex,
4864 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00004865 const std::vector<unsigned int>& tensorIndexes,
4866 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004867{
4868 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004869 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01004870
Finn Williamsd4fa5452021-03-01 12:31:41 +00004871 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01004872 {
4873 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004874 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
4875 " for subgraph:{} operator index:{} {}",
4876 tensorIndexes.size(),
4877 layer->GetNumInputSlots(),
4878 subgraphIndex,
4879 operatorIndex,
4880 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004881 }
4882
Finn Williamsd4fa5452021-03-01 12:31:41 +00004883 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01004884 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00004885 unsigned int tensorIndex = tensorIndexes[index];
4886 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01004887 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
4888 }
4889}
4890
Kevin May7d96b162021-02-03 17:38:41 +00004891void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
4892 size_t operatorIndex,
4893 IConnectableLayer* layer,
4894 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01004895{
4896 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01004897 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01004898 if (tensorIndexes.size() != layer->GetNumOutputSlots())
4899 {
4900 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004901 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
4902 " for subgraph:{} operator index:{} {}",
4903 tensorIndexes.size(),
4904 layer->GetNumOutputSlots(),
4905 subgraphIndex,
4906 operatorIndex,
4907 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004908 }
4909
4910 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
4911 {
4912 unsigned int tensorIndex = tensorIndexes[slotIndex];
4913 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
4914 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4915 }
4916}
4917
Mike Kelly377fb212023-01-10 15:55:28 +00004918void TfLiteParserImpl::SetupInputLayerTensorInfos(size_t subgraphIndex)
4919{
4920 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4921
4922 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
4923 for (auto const& tensorIdAndPtr : inputs)
4924 {
4925 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4926 m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
4927 }
4928}
4929
Kevin May7d96b162021-02-03 17:38:41 +00004930void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004931{
4932 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4933
4934 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004935 for (auto const& tensorIdAndPtr : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004936 {
4937 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4938 IConnectableLayer* layer =
4939 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4940
4941 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
4942 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
4943
4944 RegisterOutputSlots(subgraphIndex,
4945 VIRTUAL_OPERATOR_ID,
4946 layer,
4947 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4948 }
4949}
4950
Kevin May7d96b162021-02-03 17:38:41 +00004951void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01004952{
4953 CHECK_SUBGRAPH(m_Model, subgraphIndex);
4954
4955 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
Mike Kelly0d77ae12022-01-07 17:42:27 +00004956 for (auto const& tensorIdAndPtr : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01004957 {
4958 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
4959 IConnectableLayer* layer =
4960 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
4961
4962 RegisterInputSlots(subgraphIndex,
4963 VIRTUAL_OPERATOR_ID,
4964 layer,
4965 { static_cast<uint32_t>(tensorIdAndPtr.first) });
4966 }
4967}
4968
Mike Kelly377fb212023-01-10 15:55:28 +00004969void TfLiteParserImpl::SetupConstantLayerTensorInfos(size_t subgraph)
4970{
4971 CHECK_SUBGRAPH(m_Model, subgraph);
4972
4973 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
4974 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4975 {
4976 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4977 {
4978 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
4979 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
4980 {
4981 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
4982
4983 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
4984
4985 m_TensorInfos.insert({tensorIndex, tensorInfo});
4986 }
4987 }
4988 }
4989}
4990
Mike Kelly5880b912022-01-28 16:18:54 +00004991void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004992{
Mike Kelly5880b912022-01-28 16:18:54 +00004993 CHECK_SUBGRAPH(m_Model, subgraph);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004994
Mike Kelly5880b912022-01-28 16:18:54 +00004995 const auto & subgraphPtr = m_Model->subgraphs[subgraph];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02004996 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
4997 {
4998 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
4999 {
5000 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5001 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5002 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01005003 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005004
Mike Kelly5880b912022-01-28 16:18:54 +00005005 if (IsConstTensor(tensorPtr))
Matthew Sloyan81beae32021-07-13 19:46:11 +01005006 {
5007 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Mike Kelly5880b912022-01-28 16:18:54 +00005008 armnn::DataType dataType = tensorInfo.GetDataType();
5009
5010 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5011 != m_ConstantsToDequantize.end())
5012 {
5013 dataType = DataType::Float32;
5014 }
5015 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5016
5017 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5018 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5019
5020 layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
5021 RegisterOutputSlots(subgraphIndex,
5022 VIRTUAL_OPERATOR_ID,
5023 layer,
5024 { tensorIndex });
5025 }
5026 else if (ShouldConstantTensorBeCreated(tensorIndex))
5027 {
5028 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5029 armnn::DataType dataType = tensorInfo.GetDataType();
5030
5031 if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5032 != m_ConstantsToDequantize.end())
5033 {
5034 dataType = DataType::Float32;
5035 }
5036 // Make sure isConstant flag is set.
5037 tensorInfo.SetConstant();
5038 tensorInfo.SetDataType(dataType);
5039
5040 auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005041
Matthew Sloyan81beae32021-07-13 19:46:11 +01005042 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005043 IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005044
Matthew Sloyan81beae32021-07-13 19:46:11 +01005045 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5046 RegisterOutputSlots(subgraphIndex,
5047 VIRTUAL_OPERATOR_ID,
5048 layer,
Mike Kelly5880b912022-01-28 16:18:54 +00005049 {tensorIndex});
Matthew Sloyan81beae32021-07-13 19:46:11 +01005050 }
5051 else
5052 {
5053 throw ParseException(
5054 fmt::format("Invalid Tensor: Tensor should be constant. {}",
5055 CHECK_LOCATION().AsString()));
5056 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02005057 }
5058 }
5059 }
5060}
5061
telsoa01c577f2c2018-08-31 09:22:23 +01005062// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00005063TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01005064{
5065 CHECK_BUFFER(model, bufferIndex);
5066 return model->buffers[bufferIndex].get();
5067}
5068
Matteo Martincigh747ef822018-12-18 09:26:39 +00005069template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00005070std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5071TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
5072 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00005073 armnn::TensorInfo& tensorInfo,
5074 armnn::Optional<armnn::PermutationVector&> permutationVector)
5075{
Matthew Sloyan81beae32021-07-13 19:46:11 +01005076 // Make sure isConstant flag is set.
5077 tensorInfo.SetConstant();
5078
Matteo Martincigh747ef822018-12-18 09:26:39 +00005079 auto constData = CreateConstTensorImpl<T>(bufferPtr,
5080 tensorPtr,
5081 tensorInfo,
5082 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00005083 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00005084 return std::make_pair(constData.first, std::move(storage));
5085}
5086
Mike Kelly5880b912022-01-28 16:18:54 +00005087bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
5088{
5089 // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
5090 return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5091 != m_ConstantsToBeCreated.end());
5092}
5093
Finn Williamsd4fa5452021-03-01 12:31:41 +00005094bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
5095{
5096 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01005097 bool isConst = true;
5098
5099 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
5100 if (buffer->data.size() == 0)
5101 {
5102 isConst = false;
5103 }
5104
5105 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00005106}
5107
Kevin May7d96b162021-02-03 17:38:41 +00005108std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00005109TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
5110 armnn::TensorInfo& tensorInfo,
5111 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01005112{
5113 CHECK_TENSOR_PTR(tensorPtr);
5114 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5115 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5116
Matthew Sloyan81beae32021-07-13 19:46:11 +01005117 // Make sure isConstant flag is set.
5118 tensorInfo.SetConstant();
5119
telsoa01c577f2c2018-08-31 09:22:23 +01005120 switch (tensorInfo.GetDataType())
5121 {
5122 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005123 return CreateConstTensorAndStoreData<float>(bufferPtr,
5124 tensorPtr,
5125 tensorInfo,
5126 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00005127 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005128 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5129 tensorPtr,
5130 tensorInfo,
5131 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00005132 case armnn::DataType::QSymmS8:
5133 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5134 tensorPtr,
5135 tensorInfo,
5136 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00005137 case armnn::DataType::QAsymmS8:
5138 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5139 tensorPtr,
5140 tensorInfo,
5141 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005142 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00005143 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5144 tensorPtr,
5145 tensorInfo,
5146 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01005147 default:
5148 {
5149 std::stringstream errString;
5150 errString << "Unexpected datatype when creating const tensor: "
5151 << armnn::GetDataTypeName(tensorInfo.GetDataType())
5152 << " shape:" << tensorInfo.GetShape()
5153 << CHECK_LOCATION().AsString();
5154 throw ParseException(errString.str());
5155 }
5156 }
5157}
5158
Finn Williamsd4fa5452021-03-01 12:31:41 +00005159armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5160 armnn::TensorInfo& tensorInfo)
5161{
5162 CHECK_TENSOR_PTR(tensorPtr);
5163 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5164 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5165
Matthew Sloyan81beae32021-07-13 19:46:11 +01005166 // Make sure isConstant flag is set.
5167 tensorInfo.SetConstant();
5168
Finn Williamsd4fa5452021-03-01 12:31:41 +00005169 return ConstTensor(tensorInfo, bufferPtr->data.data());
5170}
5171
Mike Kelly5880b912022-01-28 16:18:54 +00005172std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5173TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5174 armnn::TensorInfo& tensorInfo,
5175 armnn::DataType inputDataType)
5176{
5177 CHECK_TENSOR_PTR(tensorPtr);
5178 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5179 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5180
5181 // Make sure isConstant flag is set.
5182 tensorInfo.SetConstant();
5183
Mike Kelly0506ef02023-01-03 16:29:44 +00005184 if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
Mike Kelly5880b912022-01-28 16:18:54 +00005185 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005186 try
5187 {
5188 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5189 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5190 return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
5191 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005192 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005193 {
5194 throw ParseException(
5195 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5196 GetDataTypeName(DataType::Float32),
5197 GetDataTypeName(tensorInfo.GetDataType()),
5198 CHECK_LOCATION().AsString()));
5199 }
Mike Kelly5880b912022-01-28 16:18:54 +00005200 }
5201 else
5202 {
5203 return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5204 }
5205}
5206
5207std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5208TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
5209{
5210 CHECK_TENSOR_PTR(tensorPtr);
5211 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5212 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5213 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5214
5215 // Make sure isConstant flag is set.
5216 tensorInfo.SetConstant();
5217
5218 if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5219 {
Mike Kelly0506ef02023-01-03 16:29:44 +00005220 try
5221 {
5222 TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5223 std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5224 return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
5225 }
Cathal Corbett9c843c32023-01-09 17:51:37 +00005226 catch (InvalidArgumentException&)
Mike Kelly0506ef02023-01-03 16:29:44 +00005227 {
5228 throw ParseException(
5229 fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5230 GetDataTypeName(DataType::Float32),
5231 GetDataTypeName(tensorInfo.GetDataType()),
5232 CHECK_LOCATION().AsString()));
5233 }
Mike Kelly5880b912022-01-28 16:18:54 +00005234 }
5235 else
5236 {
5237 return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5238 }
5239}
5240
Kevin May7d96b162021-02-03 17:38:41 +00005241BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
5242 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005243{
5244 CHECK_SUBGRAPH(m_Model, subgraphId);
5245 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
Mike Kelly0d77ae12022-01-07 17:42:27 +00005246 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005247 {
5248 if (input.second->name == name)
5249 {
5250 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
Colm Donelan4bc993b2021-11-09 20:39:10 +00005251 auto inputTensorInfo = ToTensorInfo(input.second);
5252 // Input tensors are always treated as constant tensors during network execution.
5253 inputTensorInfo.SetConstant(true);
5254 return std::make_pair(bindingId, inputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01005255 }
5256 }
5257
5258 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005259 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005260 {
5261 bindings << "'" << input.second->name << "' ";
5262 }
5263
5264 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005265 fmt::format("No input binding found for subgraph:{} and name:{}. "
5266 "Possible inputs are: [{}] {}",
5267 subgraphId,
5268 name,
5269 bindings.str(),
5270 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005271}
5272
Kevin May7d96b162021-02-03 17:38:41 +00005273BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
5274 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01005275{
5276 CHECK_SUBGRAPH(m_Model, subgraphId);
5277 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005278 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01005279 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005280 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01005281 if (output.second->name == name)
5282 {
5283 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Mike Kelly377fb212023-01-10 15:55:28 +00005284 std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5285 m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00005286 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01005287 }
5288 }
5289
5290 std::stringstream bindings;
Mike Kelly0d77ae12022-01-07 17:42:27 +00005291 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005292 {
5293 bindings << "'" << output.second->name << "' ";
5294 }
5295
5296 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01005297 fmt::format("No output binding found for subgraph:{} and name:{}. "
5298 "Possible outputs are: [{}] {}",
5299 subgraphId,
5300 name,
5301 bindings.str(),
5302 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01005303}
5304
Kevin May7d96b162021-02-03 17:38:41 +00005305size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01005306{
5307 return m_Model->subgraphs.size();
5308}
5309
Kevin May7d96b162021-02-03 17:38:41 +00005310std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005311{
5312 CHECK_SUBGRAPH(m_Model, subgraphId);
5313 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5314 std::vector<std::string> result;
5315 result.reserve(inputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005316 for (auto const& input : inputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005317 {
5318 result.push_back(input.second->name);
5319 }
5320 return result;
5321}
5322
Kevin May7d96b162021-02-03 17:38:41 +00005323std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01005324{
5325 CHECK_SUBGRAPH(m_Model, subgraphId);
5326 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5327 std::vector<std::string> result;
5328 result.reserve(outputs.size());
Mike Kelly0d77ae12022-01-07 17:42:27 +00005329 for (auto const& output : outputs)
telsoa01c577f2c2018-08-31 09:22:23 +01005330 {
5331 result.push_back(output.second->name);
5332 }
5333 return result;
5334}
5335
Matthew Sloyanac001ee2021-02-03 10:43:04 +00005336const std::string TfLiteParserImpl::GetVersion()
5337{
5338 return TFLITE_PARSER_VERSION;
5339}
5340
Mike Kelly0d77ae12022-01-07 17:42:27 +00005341TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005342: m_FloatData(std::move(data))
5343, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005344, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005345, m_Int32Data(nullptr)
5346{
5347}
5348
Mike Kelly0d77ae12022-01-07 17:42:27 +00005349TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005350: m_FloatData(nullptr)
5351, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00005352, m_Int8Data(nullptr)
5353, m_Int32Data(nullptr)
5354{
5355}
5356
Mike Kelly0d77ae12022-01-07 17:42:27 +00005357TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
Keith Davisd305e1a2020-01-22 11:57:54 +00005358: m_FloatData(nullptr)
5359, m_Uint8Data(nullptr)
5360, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01005361, m_Int32Data(nullptr)
5362{
5363}
5364
Mike Kelly0d77ae12022-01-07 17:42:27 +00005365TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
telsoa01c577f2c2018-08-31 09:22:23 +01005366: m_FloatData(nullptr)
5367, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00005368, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01005369, m_Int32Data(std::move(data))
5370{
5371}
5372
5373} // armnnTfLiteParser