blob: 26c44a9f35a0ce6230229e79aec798121b27135f [file] [log] [blame]
telsoa01c577f2c2018-08-31 09:22:23 +01001//
Mike Kellyc5789ca2020-07-06 19:24:15 +01002// Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
Matteo Martincighe011d202019-11-28 11:35:47 +00005
telsoa01c577f2c2018-08-31 09:22:23 +01006#include "TfLiteParser.hpp"
7
Matthew Sloyanac001ee2021-02-03 10:43:04 +00008#include "armnnTfLiteParser/Version.hpp"
9
Sadik Armagand109a4d2020-07-28 10:42:13 +010010#include <armnn/BackendOptions.hpp>
Matthew Bentham39ef3e52020-01-20 10:09:09 +000011#include <armnn/Descriptors.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010012#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000013#include <armnn/Logging.hpp>
James Conroy05102392020-06-24 15:39:55 +010014#include <armnn/Tensor.hpp>
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +000015#include <armnnUtils/TensorUtils.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010016#include <armnn/TypesUtils.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010017#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000018#include <armnn/utility/IgnoreUnused.hpp>
Derek Lambertif0176992020-04-28 13:37:49 +010019#include <armnn/utility/NumericCast.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010020
21// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000022#include <armnnUtils/Permute.hpp>
Francis Murtagh532a29d2020-06-29 11:50:01 +010023#include <Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000024
Sadik Armagan479045b2018-10-01 11:51:37 +010025#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010026#include <VerificationHelpers.hpp>
27
28// The generated code based on the Tf Lite schema:
29#include <schema_generated.h>
30
Matteo Martincighe011d202019-11-28 11:35:47 +000031#include <flatbuffers/flexbuffers.h>
32
James Ward58dec6b2020-09-11 17:32:44 +010033#include <fmt/format.h>
telsoa01c577f2c2018-08-31 09:22:23 +010034
telsoa01c577f2c2018-08-31 09:22:23 +010035#include <algorithm>
Matthew Sloyanac001ee2021-02-03 10:43:04 +000036#include <fstream>
37#include <iostream>
telsoa01c577f2c2018-08-31 09:22:23 +010038#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010039#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000040#include <sstream>
41
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
79armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
80{
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
Kevin May7d96b162021-02-03 17:38:41 +0000116void CheckSubgraph(const TfLiteParserImpl::ModelPtr & model,
telsoa01c577f2c2018-08-31 09:22:23 +0100117 size_t subgraphIndex,
118 const CheckLocation & location)
119{
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
Kevin May7d96b162021-02-03 17:38:41 +0000144void CheckModel(const TfLiteParserImpl::ModelPtr & model,
telsoa01c577f2c2018-08-31 09:22:23 +0100145 size_t subgraphIndex,
146 size_t operatorIndex,
147 const CheckLocation & location)
148{
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
Kevin May7d96b162021-02-03 17:38:41 +0000186void CheckTensor(const TfLiteParserImpl::ModelPtr & model,
telsoa01c577f2c2018-08-31 09:22:23 +0100187 size_t subgraphIndex,
188 size_t tensorIndex,
189 const CheckLocation & location)
190{
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,
telsoa01c577f2c2018-08-31 09:22:23 +0100216 const CheckLocation & location)
217{
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
Kevin May7d96b162021-02-03 17:38:41 +0000228void CheckBuffer(const TfLiteParserImpl::ModelPtr & model,
telsoa01c577f2c2018-08-31 09:22:23 +0100229 size_t bufferIndex,
230 const CheckLocation & location)
231{
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,
telsoa01c577f2c2018-08-31 09:22:23 +0100264 const armnn::TensorInfo & tensorInfo,
265 uint32_t bufferId,
266 const CheckLocation & location)
267{
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
287#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
288 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
289
290bool IsActivationSupported(tflite::ActivationFunctionType activationType)
291{
292 switch(activationType)
293 {
294 case tflite::ActivationFunctionType_NONE:
295 case tflite::ActivationFunctionType_RELU:
296 case tflite::ActivationFunctionType_RELU6:
297 case tflite::ActivationFunctionType_TANH:
298 {
299 return true;
300 }
301 default:
302 {
303 return false;
304 }
305 }
306}
307
308#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
309 do { \
310 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
311 { \
312 throw ParseException( \
James Ward58dec6b2020-09-11 17:32:44 +0100313 fmt::format("TfLite parser doesn't suppport fused activation: " \
314 "{}/{} in {} subgraph:{} operator:{} at {}", \
315 OPTION->fused_activation_function, \
316 tflite::EnumNameActivationFunctionType(\
317 OPTION->fused_activation_function), \
318 __func__, \
319 SUBGRAPH_INDEX, \
320 OPERATOR_INDEX, \
321 CHECK_LOCATION().FileLine())); \
telsoa01c577f2c2018-08-31 09:22:23 +0100322 } \
323 } while(false)
324
325
326std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in)
327{
328 std::vector<unsigned int> result;
329 result.reserve(in.size());
330 for (auto & i : in)
331 {
mathad01c21025d2021-04-26 10:09:37 +0100332 // If the location of the input data is -1 then the input should be ignored.
333 if (i == -1)
334 {
335 continue;
336 }
telsoa01c577f2c2018-08-31 09:22:23 +0100337 result.push_back(CHECKED_NON_NEGATIVE(i));
338 }
339 return result;
340}
341
342void CalcPadding(uint32_t inputSize,
343 uint32_t filterSize,
344 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100345 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100346 uint32_t& paddingFront,
347 uint32_t& paddingBack,
348 tflite::Padding padding)
349{
350 paddingFront = 0;
351 paddingBack = 0;
352 if (padding == tflite::Padding_SAME)
353 {
354 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100355 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
356 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100357 if (temp > inputSize)
358 {
359 paddingFront = (temp - inputSize) / 2;
360 paddingBack = (temp - inputSize) - paddingFront;
361 }
362 }
363}
364
Kevin May7d96b162021-02-03 17:38:41 +0000365armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100366 const std::vector<unsigned int>& shapes,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100367 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100368{
369 armnn::DataType type;
370 CHECK_TENSOR_PTR(tensorPtr);
371
372 switch (tensorPtr->type)
373 {
374 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000375 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100376 break;
377 case tflite::TensorType_FLOAT32:
378 type = armnn::DataType::Float32;
379 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000380 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000381 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000382 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000383 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000384 type = armnn::DataType::QAsymmS8;
385 }
386 else
387 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000388 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000389 type = armnn::DataType::QSymmS8;
390 }
Finn Williamsed66d142019-12-06 09:55:55 +0000391 break;
392 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000393 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000394 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100395 case tflite::TensorType_INT32:
396 type = armnn::DataType::Signed32;
397 break;
Inki Daed4619e22020-09-10 15:33:54 +0900398 case tflite::TensorType_INT64:
399 type = armnn::DataType::Signed64;
400 break;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100401 case tflite::TensorType_BOOL:
402 type = armnn::DataType::Boolean;
403 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100404 default:
405 {
406 CheckLocation location = CHECK_LOCATION();
407 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100408 fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
409 tensorPtr->type,
410 tflite::EnumNameTensorType(tensorPtr->type),
411 tensorPtr->name,
412 location.AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100413 }
414 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100415 std::vector<unsigned int> safeShape = shapes;
Sadik Armagand109a4d2020-07-28 10:42:13 +0100416 bool isDynamic = false;
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100417 if (safeShape.size() == 0)
418 {
419 safeShape.push_back(1);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100420 if (outputTensor)
421 {
422 isDynamic = true;
423 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100424 }
425
Keith Davisd305e1a2020-01-22 11:57:54 +0000426 float quantizationScale = 0.0f;
427 int32_t quantizationOffset = 0;
428
429 if (tensorPtr->quantization.get())
430 {
431 if (tensorPtr->quantization->scale.size() <= 1)
432 {
433 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
434 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
435
436 if (tensorPtr->quantization->scale.size() == 1)
437 {
438 quantizationScale = tensorPtr->quantization->scale[0];
439 }
440 if (tensorPtr->quantization->zero_point.size() == 1)
441 {
442 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000443 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100444 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000445 }
446
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100447 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100448 safeShape.data());
449 if (isDynamic)
450 {
451 tensorShape = TensorShape(1, false);
452 }
453 armnn::TensorInfo result(tensorShape,
454 type,
455 quantizationScale,
456 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000457 return result;
458 }
459 else
460 {
461 std::vector<float> quantizationScales;
462 std::vector<int32_t> quantizationOffsets;
463
464 // Scale
465 std::copy(tensorPtr->quantization->scale.begin(),
466 tensorPtr->quantization->scale.end(),
467 std::back_inserter(quantizationScales));
468
Keith Davis0c2eeac2020-02-11 16:51:50 +0000469 // QSymmS8 Per-axis
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100470 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100471 safeShape.data());
472 if (isDynamic)
473 {
474 tensorShape = TensorShape(1, false);
475 }
476 armnn::TensorInfo result(tensorShape,
477 type,
478 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100479 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000480 return result;
481 }
482 }
483 else
484 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100485 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100486 safeShape.data());
487 if (isDynamic)
488 {
489 tensorShape = TensorShape(1, false);
490 }
491 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000492 type,
493 quantizationScale,
494 quantizationOffset);
495 return result;
496 }
telsoa01c577f2c2018-08-31 09:22:23 +0100497}
498
Jan Eilers7612bd62021-04-06 17:29:03 +0100499armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr)
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000500{
501 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100502 return ToTensorInfo(tensorPtr, dimensions);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000503}
504
Kevin May7d96b162021-02-03 17:38:41 +0000505armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100506 const bool outputTensor)
507{
508 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100509 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100510}
511
telsoa01c577f2c2018-08-31 09:22:23 +0100512template<typename T>
513std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000514CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
515 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000516 armnn::TensorInfo& tensorInfo,
517 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100518{
Jan Eilers8eb25602020-03-09 12:13:48 +0000519 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100520 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
521 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100522 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100523
524 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000525
526 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
527 {
528 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000529 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
530 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000531 }
532 else
533 {
534 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
535 }
536
telsoa01c577f2c2018-08-31 09:22:23 +0100537 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
538}
539
telsoa01c577f2c2018-08-31 09:22:23 +0100540armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
541{
542 // generate the binding id by shifting the tensor id by 8 bit
543 // and add the subgraph id, which allows 256 subgraphs
544 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
545}
546
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000547bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
548{
549 const unsigned int actualSize = actual.GetNumDimensions();
550 if (actualSize != expected.size())
551 {
552 return false;
553 }
554
555 for (unsigned int i = 0u; i < actualSize; i++)
556 {
557 if (expected[i] < 0 ||
558 actual[i] != static_cast<unsigned int>(expected[i]))
559 {
560 return false;
561 }
562 }
563
564 return true;
565}
566
James Conroy05102392020-06-24 15:39:55 +0100567void CheckMatchingQuantization(const TensorInfo& first,
568 const TensorInfo& second,
569 const std::string& descName,
570 std::string const& firstName,
571 std::string const& secondName)
572{
573 if (!first.IsQuantized() ||
574 !second.IsQuantized())
575 {
576 // Not a quantized type, ignore the validation
577 return;
578 }
579
580 DataType firstDataType = first.GetDataType();
581 DataType secondDataType = second.GetDataType();
582
583 if (firstDataType != secondDataType)
584 {
585 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
586 " must be of the same quantized type, " +
587 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
588 secondName + " is " + GetDataTypeName(secondDataType));
589 }
590
591 if (!first.IsTypeSpaceMatch(second))
592 {
593 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
594 " must have the same quantization space, " +
595 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
596 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
597 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
598 " and scale " + std::to_string(second.GetQuantizationScale()));
599 }
600}
601
telsoa01c577f2c2018-08-31 09:22:23 +0100602} // <anonymous>
603
Kevin May7d96b162021-02-03 17:38:41 +0000604TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100605: m_Options(options)
606, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000607, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100608{
609 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100610 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000611 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100612 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
613 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000614 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
615 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
mathad01b392e982021-04-07 12:07:30 +0100616 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000617 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
618 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
619 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
620 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
621 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
622 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100623 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000624 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
625 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
626 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
627 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
628 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
629 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100630 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000631 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
632 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
633 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
634 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
635 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
636 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
637 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
638 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
639 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
640 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100641 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000642 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
643 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
644 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000645 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
646 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Kevin May7d96b162021-02-03 17:38:41 +0000647 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
648 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
649 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100650 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Kevin May7d96b162021-02-03 17:38:41 +0000651 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
652 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
653 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
654 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
655 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
656 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
657 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
658 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
659 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
660 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
661 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
662 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
663 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100664
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100665 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000666 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100667}
668
Kevin May7d96b162021-02-03 17:38:41 +0000669void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100670{
671 m_Network = armnn::INetworkPtr(nullptr, nullptr);
672 m_Model = nullptr;
673 m_SubgraphConnections.clear();
674}
675
Kevin May7d96b162021-02-03 17:38:41 +0000676INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100677{
678 ResetParser();
679 m_Model = LoadModelFromFile(graphFile);
680 return CreateNetworkFromModel();
681}
682
Kevin May7d96b162021-02-03 17:38:41 +0000683INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100684{
685 ResetParser();
686 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
687 return CreateNetworkFromModel();
688}
689
Kevin May7d96b162021-02-03 17:38:41 +0000690INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100691{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100692
693 using NetworkOptions = std::vector<BackendOptions>;
694 NetworkOptions networkOptions = {};
695 if (m_Options && m_Options.value().m_InferAndValidate)
696 {
697 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
698 {
699 { "InferAndValidate", true }
700 });
701
702 networkOptions.push_back(shapeInferenceMethodOption);
703 }
704
705 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100706 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100707
telsoa01c577f2c2018-08-31 09:22:23 +0100708 if (m_Model->subgraphs.size() != 1)
709 {
710 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100711 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
712 m_Model->subgraphs.size(),
713 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100714 }
715
716 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100717 size_t operatorIndex = 0;
718 try
telsoa01c577f2c2018-08-31 09:22:23 +0100719 {
Colm Donelan6350d272020-06-09 16:56:25 +0100720 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100721 {
Colm Donelan6350d272020-06-09 16:56:25 +0100722 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
723 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100724 {
Colm Donelan6350d272020-06-09 16:56:25 +0100725 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
telsoa01c577f2c2018-08-31 09:22:23 +0100726 auto builtinCode = opCodePtr->builtin_code;
727
728 if (builtinCode > tflite::BuiltinOperator_MAX)
729 {
James Ward58dec6b2020-09-11 17:32:44 +0100730 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
731 "subgraph:{} operator idx:{}. {}",
732 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
733 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100734 }
735
736 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100737 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100738 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100739 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100740 }
telsoa01c577f2c2018-08-31 09:22:23 +0100741
Colm Donelan6350d272020-06-09 16:56:25 +0100742 SetupInputLayers(subgraphIndex);
743 SetupOutputLayers(subgraphIndex);
744 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100745
Colm Donelan6350d272020-06-09 16:56:25 +0100746 ++subgraphIndex;
747 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100748 }
telsoa01c577f2c2018-08-31 09:22:23 +0100749 }
Colm Donelan6350d272020-06-09 16:56:25 +0100750 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100751 {
Colm Donelan6350d272020-06-09 16:56:25 +0100752 std::stringstream errorString;
753 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
754 << subgraphIndex << " error: " << e.what();
755 ARMNN_LOG(error) << errorString.str();
756 std::stringstream errors;
757 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100758 throw ParseException(errors.str());
759 }
760
761 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100762 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100763 {
764 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
765 {
766 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
767 {
768 for (size_t inputSlotIdx = 0;
769 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
770 ++inputSlotIdx)
771 {
772 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
773 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
774 }
775 }
776 }
777 }
778
779 return std::move(m_Network);
780}
781
Kevin May7d96b162021-02-03 17:38:41 +0000782void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
783 size_t tensorIndex,
784 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100785{
786 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100787 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
788 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100789
790 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
791
792 // assuming there is only one producer for that tensor
793 if (tensorSlots.outputSlot != nullptr)
794 {
James Ward58dec6b2020-09-11 17:32:44 +0100795 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
796 "subgraph:{} tensor:{} {}",
797 subgraphIndex,
798 tensorIndex,
799 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100800 }
801
802 tensorSlots.outputSlot = slot;
803}
804
Kevin May7d96b162021-02-03 17:38:41 +0000805void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
806 size_t tensorIndex,
807 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100808{
809 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100810 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
811 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100812
Finn Williamsd4fa5452021-03-01 12:31:41 +0000813 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100814 tensorSlots.inputSlots.push_back(slot);
815}
816
Kevin May7d96b162021-02-03 17:38:41 +0000817void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100818{
819 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
820
821 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000822 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100823
824 // Identify custom code defined for custom operator
825 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
826 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
827
828 // Find parser function that correspondes to custom code (if any)
829 auto iterator = m_CustomParserFunctions.find(customCode);
830 if (iterator != m_CustomParserFunctions.end())
831 {
832 customParserFunction = iterator->second;
833 }
834
835 // Run parser function
836 (this->*customParserFunction)(subgraphIndex, operatorIndex);
837}
838
Kevin May7d96b162021-02-03 17:38:41 +0000839void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100840{
841 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100842
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100843 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
844
845 auto opcodeIndex = operatorPtr->opcode_index;
846 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
847
848 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
849 {
850 // Do not add StandInLayer, throw ParseException instead
851 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100852 fmt::format("Operator not supported. "
853 "subgraph:{} operator:{} "
854 "opcode_index:{} opcode:{} / {} {}",
855 subgraphIndex,
856 operatorIndex,
857 opcodeIndex,
858 opcode,
859 tflite::EnumNameBuiltinOperator(opcode),
860 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100861 }
862
863 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
864 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
865
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100866 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
867 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100868
869 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +0100870 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100871
872 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
873 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +0100874 ARMNN_ASSERT(layer != nullptr);
875
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100876 for (unsigned int i = 0u; i < numOutputs; ++i)
877 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100878 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100879 }
880
881 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
882 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
883
884 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
885 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +0100886}
887
mathad01b392e982021-04-07 12:07:30 +0100888void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
889{
890 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
891
892 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
893 CHECK_VALID_SIZE(inputs.size(), 1);
894 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
895 CHECK_VALID_SIZE(outputs.size(), 1);
896
897 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
898
899 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
900 ARMNN_ASSERT(layer != nullptr);
901
902 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
903 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
904
905 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
906 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
907
908 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
909 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
910}
911
Kevin May7d96b162021-02-03 17:38:41 +0000912void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100913{
914 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
915
916 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
917 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
918
919 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
920
921 Convolution2dDescriptor desc;
922 desc.m_BiasEnabled = false;
923 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
924 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000925 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100926 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
927 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000928
telsoa01c577f2c2018-08-31 09:22:23 +0100929 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
930 CHECK_VALID_SIZE(inputs.size(), 2, 3);
931
932 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
933 CHECK_VALID_SIZE(outputs.size(), 1);
934
935 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
936 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
937
938 // assuming input is NHWC
939 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
940 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
941
942 // assuming the filter is OHWI : Output, H, W, Input
943 // which is essentially the same as NHWC
944 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
945 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
946
Pablo Tellof0bd6832019-04-26 17:58:13 +0100947 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
948 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
949 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
950 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +0100951
Finn Williamsd4fa5452021-03-01 12:31:41 +0000952 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +0100953 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100954
James Ward58dec6b2020-09-11 17:32:44 +0100955 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100956
957 if (inputs.size() == 3)
958 {
959 desc.m_BiasEnabled = true;
960 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +0000961 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +0100962 layer = m_Network->AddConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +0000963 filterTensorAndData,
964 Optional<ConstTensor>(biasTensorAndData),
telsoa01c577f2c2018-08-31 09:22:23 +0100965 layerName.c_str());
966 }
967 else
968 {
969 layer = m_Network->AddConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +0000970 filterTensorAndData,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100971 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +0100972 layerName.c_str());
973 }
974
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100975 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100976
Sadik Armagand109a4d2020-07-28 10:42:13 +0100977 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +0000978 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +0100979
980 // register the input connection slots for the layer, connections are made after all layers have been created
981 // only the tensors for the inputs are relevant, exclude the const tensors
982 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +0000983 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +0100984
jimfly01c25411c2018-11-14 17:47:22 +0000985 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +0100986 // register the output connection slots for the layer, connections are made after all layers have been created
987 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
988 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
989}
990
Kevin May7d96b162021-02-03 17:38:41 +0000991void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100992{
993 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
994
995 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
996 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
997
998 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
999
1000 DepthwiseConvolution2dDescriptor desc;
1001 desc.m_BiasEnabled = false;
1002 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1003 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001004 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001005 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001006
1007 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1008 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1009 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1010 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001011 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1012 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001013
telsoa01c577f2c2018-08-31 09:22:23 +01001014 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001015 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001016
Matteo Martincigh747ef822018-12-18 09:26:39 +00001017 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001018 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1019 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001020
1021 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001022 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1023 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1024
Pablo Tellof0bd6832019-04-26 17:58:13 +01001025 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1026 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1027 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1028 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001029
Jan Eilers53ef7952021-06-02 12:01:25 +01001030 // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
1031 auto filterTensor = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001032 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001033 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001034
1035 if (inputs.size() == 3)
1036 {
1037 desc.m_BiasEnabled = true;
1038 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00001039 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001040 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
Jan Eilers53ef7952021-06-02 12:01:25 +01001041 filterTensor,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001042 Optional<ConstTensor>(biasTensorAndData),
telsoa01c577f2c2018-08-31 09:22:23 +01001043 layerName.c_str());
1044 }
1045 else
1046 {
1047 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
Jan Eilers53ef7952021-06-02 12:01:25 +01001048 filterTensor,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001049 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001050 layerName.c_str());
1051 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001052 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001053
Sadik Armagand109a4d2020-07-28 10:42:13 +01001054 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001055 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001056
1057 // register the input connection slots for the layer, connections are made after all layers have been created
1058 // only the tensors for the inputs are relevant, exclude the const tensors
1059 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001060 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +01001061
jimfly01c25411c2018-11-14 17:47:22 +00001062 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001063 // register the output connection slots for the layer, connections are made after all layers have been created
1064 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1065 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1066}
1067
Kevin May7d96b162021-02-03 17:38:41 +00001068void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001069{
1070 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1071
1072 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1073 CHECK_VALID_SIZE(inputs.size(), 1);
1074
1075 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1076 CHECK_VALID_SIZE(outputs.size(), 1);
1077
James Ward58dec6b2020-09-11 17:32:44 +01001078 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001079
1080 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001081 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001082
Sadik Armagand109a4d2020-07-28 10:42:13 +01001083 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001084 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1085
1086 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1087 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1088
1089 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1090 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1091}
1092
Kevin May7d96b162021-02-03 17:38:41 +00001093void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001094{
1095 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1096
1097 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001098 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001099
1100 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1101 CHECK_VALID_SIZE(outputs.size(), 1);
1102
James Ward58dec6b2020-09-11 17:32:44 +01001103 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001104 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001105
josh minorba424d22019-11-13 10:55:17 -06001106 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001107 {
1108 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1109 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001110 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1111 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001112 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001113 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001114
Mike Kelly08759e22020-03-02 11:41:31 +00001115 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001116 }
1117
James Conroy05102392020-06-24 15:39:55 +01001118 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001119 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001120 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001121
James Conroy05102392020-06-24 15:39:55 +01001122 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001123 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001124 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1125
1126 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1127 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1128
1129 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1130 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1131}
1132
Kevin May7d96b162021-02-03 17:38:41 +00001133void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001134{
1135 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1136
1137 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1138 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1139
1140 TransposeConvolution2dDescriptor desc;
1141 desc.m_BiasEnabled = false;
1142 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1143 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1144 desc.m_DataLayout = armnn::DataLayout::NHWC;
1145
1146 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001147 if (inputs.size() == 4)
1148 {
1149 desc.m_BiasEnabled = true;
1150 }
1151 else
1152 {
1153 CHECK_VALID_SIZE(inputs.size(), 3);
1154 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001155
1156 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1157 CHECK_VALID_SIZE(outputs.size(), 1);
1158
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001159 if (inputs[0])
1160 {
1161 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1162 std::vector<int> output_shape(tensorInfo.GetNumElements());
1163 if (tensorInfo.GetDataType() == DataType::Signed32)
1164 {
1165 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1166 }
1167 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1168 {
1169 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1170 {
1171 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1172 }
1173 }
1174 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1175 for (int dimension : output_shape)
1176 {
1177 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1178 }
1179 desc.m_OutputShapeEnabled = true;
1180 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001181 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001182 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1183
1184 // TfLite uses NHWC tensors
1185 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1186 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1187
1188 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1189 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1190
1191 CalcPadding(inputHeight,
1192 filterHeight,
1193 desc.m_StrideY,
1194 1, // DilationY
1195 desc.m_PadTop,
1196 desc.m_PadBottom,
1197 options->padding);
1198
1199 CalcPadding(inputWidth,
1200 filterWidth,
1201 desc.m_StrideX,
1202 1, // DilationX
1203 desc.m_PadLeft,
1204 desc.m_PadRight,
1205 options->padding);
1206
Finn Williamsd4fa5452021-03-01 12:31:41 +00001207 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001208
1209 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001210 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001211
David Monahan61683802021-01-12 09:11:07 +00001212 if (desc.m_BiasEnabled)
1213 {
1214 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00001215 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo);
David Monahan61683802021-01-12 09:11:07 +00001216 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001217 filterTensorAndData,
1218 biasConstTensor,
David Monahan61683802021-01-12 09:11:07 +00001219 layerName.c_str());
1220 }
1221 else
1222 {
1223 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001224 filterTensorAndData,
David Monahan61683802021-01-12 09:11:07 +00001225 EmptyOptional(),
1226 layerName.c_str());
1227 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001228
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001229 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001230
Sadik Armagand109a4d2020-07-28 10:42:13 +01001231 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001232 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1233
1234 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1235 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001236 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001237
1238 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1239 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1240}
1241
Kevin May7d96b162021-02-03 17:38:41 +00001242void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001243{
1244 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1245}
1246
Kevin May7d96b162021-02-03 17:38:41 +00001247void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001248{
1249 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1250
1251 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1252 CHECK_VALID_SIZE(inputs.size(), 3);
1253
1254 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1255 CHECK_VALID_SIZE(outputs.size(), 1);
1256
1257 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1258 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1259
1260 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1261 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1262
1263 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1264 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1265
1266 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1267 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1268
1269 size_t step = 2;
1270 std::vector<std::pair<unsigned int, unsigned int>> crops;
1271 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1272 {
1273 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1274 }
1275
1276 armnn::BatchToSpaceNdDescriptor desc;
1277 desc.m_BlockShape = blockShape;
1278 desc.m_Crops = crops;
1279 desc.m_DataLayout = armnn::DataLayout::NHWC;
1280
James Ward58dec6b2020-09-11 17:32:44 +01001281 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001282
James Conroy05102392020-06-24 15:39:55 +01001283 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001284 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001285 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1286
1287 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1288 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001289 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1290
1291 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1292 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1293
1294 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1295 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1296}
1297
Kevin May7d96b162021-02-03 17:38:41 +00001298void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001299{
1300 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1301
1302 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1303 CHECK_VALID_SIZE(inputs.size(), 1);
1304
1305 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1306 CHECK_VALID_SIZE(outputs.size(), 1);
1307
1308 L2NormalizationDescriptor desc;
1309 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001310 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001311 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1312
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001313 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001314
Sadik Armagand109a4d2020-07-28 10:42:13 +01001315 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001316 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1317
1318 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1319 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1320
1321 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1322 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1323}
1324
Kevin May7d96b162021-02-03 17:38:41 +00001325void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001326{
1327 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1328}
1329
Kevin May7d96b162021-02-03 17:38:41 +00001330void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001331{
1332 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1333
1334 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1335 CHECK_VALID_SIZE(inputs.size(), 2);
1336
1337 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1338 CHECK_VALID_SIZE(outputs.size(), 1);
1339
James Ward58dec6b2020-09-11 17:32:44 +01001340 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001341
1342 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1343 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1344 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001345
Sadik Armagand109a4d2020-07-28 10:42:13 +01001346 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001347 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1348
1349 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1350 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001351 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1352
1353 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001354 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001355
1356 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1357 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1358}
1359
Kevin May7d96b162021-02-03 17:38:41 +00001360void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001361{
1362 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1363
1364 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1365 CHECK_VALID_SIZE(inputs.size(), 2);
1366
1367 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1368 CHECK_VALID_SIZE(outputs.size(), 1);
1369
James Ward58dec6b2020-09-11 17:32:44 +01001370 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001371
1372 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1373 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1374 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001375
Sadik Armagand109a4d2020-07-28 10:42:13 +01001376 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001377 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1378
1379 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1380 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001381 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1382
1383 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001384 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001385
1386 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1387 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1388}
1389
Kevin May7d96b162021-02-03 17:38:41 +00001390void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1391 size_t operatorIndex,
1392 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001393{
1394 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1395
1396 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1397 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1398
1399 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1400
1401 std::string layerName;
1402
1403 switch (algorithm)
1404 {
1405 case PoolingAlgorithm::Average:
1406 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001407 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001408 break;
1409 case PoolingAlgorithm::Max:
1410 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001411 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001412 break;
1413 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001414 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001415 }
1416
1417 Pooling2dDescriptor desc;
1418
1419 desc.m_PoolType = algorithm;
1420 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1421 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1422 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1423 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1424 desc.m_PaddingMethod = PaddingMethod::Exclude;
1425 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001426 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001427
1428 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1429 CHECK_VALID_SIZE(inputs.size(), 1);
1430 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1431
1432 // assuming input is NHWC
1433 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1434 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1435
Pablo Tellof0bd6832019-04-26 17:58:13 +01001436 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1437 desc.m_PadTop, desc.m_PadBottom, options->padding);
1438 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1439 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001440
1441 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1442 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001443
Sadik Armagand109a4d2020-07-28 10:42:13 +01001444 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001445 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1446
1447 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1448 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001449 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001450
1451 // register the input connection slots for the layer, connections are made after all layers have been created
1452 // only the tensors for the inputs are relevant, exclude the const tensors
1453 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001454 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001455
jimfly01c25411c2018-11-14 17:47:22 +00001456 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001457 // register the output connection slots for the layer, connections are made after all layers have been created
1458 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1459 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1460}
1461
Kevin May7d96b162021-02-03 17:38:41 +00001462void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001463{
1464 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1465
1466 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1467 CHECK_VALID_SIZE(inputs.size(), 3);
1468 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1469 CHECK_VALID_SIZE(outputs.size(), 1);
1470
1471 SliceDescriptor desc;
1472
1473 // set begin tensor info for slice descriptor
1474 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1475 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1476
1477 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1478 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1479
1480 // set size tensor info for slice descriptor
1481 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1482 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1483
1484 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1485 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1486 desc = SliceDescriptor(begin, size);
1487
James Ward58dec6b2020-09-11 17:32:44 +01001488 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001489
James Conroy05102392020-06-24 15:39:55 +01001490 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001491 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001492 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1493
1494 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001495 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1496
1497 // register the input connection slots for the layer, connections are made after all layers have been created
1498 // only the tensors for the inputs are relevant, exclude the const tensors
1499 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1500 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1501
1502 // register the output connection slots for the layer, connections are made after all layers have been created
1503 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1504 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1505}
1506
Kevin May7d96b162021-02-03 17:38:41 +00001507void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001508{
1509 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1510 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1511 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1512
1513 SoftmaxDescriptor desc;
1514 desc.m_Beta = options->beta;
1515
1516 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1517 CHECK_VALID_SIZE(inputs.size(), 1);
1518 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1519 CHECK_VALID_SIZE(outputs.size(), 1);
1520
James Ward58dec6b2020-09-11 17:32:44 +01001521 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001522 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1523
Sadik Armagand109a4d2020-07-28 10:42:13 +01001524 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001525 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1526
1527 // register the input connection slots for the layer, connections are made after all layers have been created
1528 // only the tensors for the inputs are relevant, exclude the const tensors
1529 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1530 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1531
1532 // register the output connection slots for the layer, connections are made after all layers have been created
1533 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1534 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1535}
1536
Kevin May7d96b162021-02-03 17:38:41 +00001537void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001538{
1539 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1540
1541 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1542 CHECK_VALID_SIZE(inputs.size(), 3);
1543
1544 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1545 CHECK_VALID_SIZE(outputs.size(), 1);
1546
1547 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1548 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1549
1550 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1551 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1552
1553 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1554 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1555
1556 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1557 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1558
1559 size_t step = 2;
1560 std::vector<std::pair<unsigned int, unsigned int>> padList;
1561 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1562 {
1563 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1564 }
1565
1566 armnn::SpaceToBatchNdDescriptor desc;
1567 desc.m_BlockShape = blockShape;
1568 desc.m_PadList = padList;
1569 desc.m_DataLayout = armnn::DataLayout::NHWC;
1570
James Ward58dec6b2020-09-11 17:32:44 +01001571 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001572
James Conroy05102392020-06-24 15:39:55 +01001573 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001574 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001575 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1576
1577 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1578 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001579 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1580
1581 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1582 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1583
1584 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1585 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1586}
1587
Kevin May7d96b162021-02-03 17:38:41 +00001588armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn,
1589 const armnn::TensorInfo & inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001590{
1591 CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4);
1592 std::vector<uint32_t> squeezeDims = squeezeDimsIn;
1593 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1594
1595 if (inputTensorInfo.GetNumDimensions() > 4)
1596 {
1597 std::stringstream ss;
1598 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1599 << " shape:" << inputTensorInfo.GetShape() << " "
1600 << CHECK_LOCATION().AsString();
1601 throw ParseException(ss.str());
1602 }
1603
1604 if (squeezeDims.empty())
1605 {
1606 squeezeDims.assign(dimensionSequence,
1607 dimensionSequence+inputTensorInfo.GetNumDimensions());
1608 }
1609
1610 std::vector<uint32_t> outputDims;
1611 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1612 {
1613 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1614 auto currentDimension = inputTensorInfo.GetShape()[i];
1615 if (skipSqueeze || currentDimension != 1)
1616 {
1617 outputDims.push_back(currentDimension);
1618 }
1619 }
1620
1621 if (outputDims.size() > 4)
1622 {
1623 std::stringstream ss;
1624 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1625 << " shape:" << inputTensorInfo.GetShape() << " "
1626 << CHECK_LOCATION().AsString();
1627 throw ParseException(ss.str());
1628 }
1629
1630 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1631 outputDims.data());
1632
1633 // we need to preserve the tensor type and the quantization data as well
1634 TensorInfo outTensorInfo = inputTensorInfo;
1635 outTensorInfo.SetShape(outShape);
1636
1637 return outTensorInfo;
1638}
1639
Kevin May7d96b162021-02-03 17:38:41 +00001640void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001641{
1642 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1643
1644 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1645 CHECK_VALID_SIZE(inputs.size(), 1);
1646
1647 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1648 CHECK_VALID_SIZE(outputs.size(), 1);
1649
1650 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1651 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01001652 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001653
1654 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1655 armnn::TensorInfo outputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00001656 TfLiteParserImpl::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims),
telsoa01c577f2c2018-08-31 09:22:23 +01001657 inputTensorInfo);
James Conroy05102392020-06-24 15:39:55 +01001658 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01001659
1660 ReshapeDescriptor reshapeDesc;
1661 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1662
telsoa01c577f2c2018-08-31 09:22:23 +01001663 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001664 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001665 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1666
1667 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1668 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1669
1670 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1671 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1672}
1673
Kevin May7d96b162021-02-03 17:38:41 +00001674void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001675{
1676 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1677
1678 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1679 CHECK_VALID_SIZE(inputs.size(), 4);
1680
1681 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1682 CHECK_VALID_SIZE(outputs.size(), 1);
1683
1684 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1685 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1686
1687 StridedSliceDescriptor desc;
1688 desc.m_BeginMask = options->begin_mask;
1689 desc.m_EllipsisMask = options->ellipsis_mask;
1690 desc.m_EndMask = options->end_mask;
1691 desc.m_NewAxisMask = options->new_axis_mask;
1692 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
1693 desc.m_DataLayout = armnn::DataLayout::NHWC;
1694
1695 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1696 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1697
1698 std::vector<int> begin(beginTensorInfo.GetNumElements());
1699 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1700
1701 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
1702 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1703
1704 std::vector<int> end(endTensorInfo.GetNumElements());
1705 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1706
1707 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
1708 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
1709
1710 std::vector<int> stride(strideTensorInfo.GetNumElements());
1711 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1712
1713 desc.m_Begin = begin;
1714 desc.m_End = end;
1715 desc.m_Stride = stride;
1716
James Ward58dec6b2020-09-11 17:32:44 +01001717 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001718 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001719 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001720
Sadik Armagand109a4d2020-07-28 10:42:13 +01001721 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001722 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1723
1724 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1725 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1726
1727 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1728 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1729}
1730
Kevin May7d96b162021-02-03 17:38:41 +00001731void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001732{
1733 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1734
1735 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1736 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1737
1738 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1739 CHECK_VALID_SIZE(inputs.size(), 2);
1740
1741 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1742 CHECK_VALID_SIZE(outputs.size(), 1);
1743
1744 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1745 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1746
James Ward58dec6b2020-09-11 17:32:44 +01001747 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001748 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001749 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001750
Sadik Armagand109a4d2020-07-28 10:42:13 +01001751 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001752 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1753
1754 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001755 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001756
1757 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1758
1759 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1760 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1761}
1762
Kevin May7d96b162021-02-03 17:38:41 +00001763void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301764{
1765 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1766
1767 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1768 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1769
1770 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1771 CHECK_VALID_SIZE(inputs.size(), 2);
1772
1773 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1774 CHECK_VALID_SIZE(outputs.size(), 1);
1775
1776 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1777 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1778
James Ward58dec6b2020-09-11 17:32:44 +01001779 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301780 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001781 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301782
Sadik Armagand109a4d2020-07-28 10:42:13 +01001783 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301784 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1785
1786 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001787 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301788 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1789
1790 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1791 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1792}
1793
Kevin May7d96b162021-02-03 17:38:41 +00001794void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001795{
1796 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1797
1798 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1799 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1800
1801 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1802 CHECK_VALID_SIZE(inputs.size(), 2);
1803
1804 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1805 CHECK_VALID_SIZE(outputs.size(), 1);
1806
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001807 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1808 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1809
James Ward58dec6b2020-09-11 17:32:44 +01001810 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001811 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001812 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001813
Sadik Armagand109a4d2020-07-28 10:42:13 +01001814 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001815 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1816
1817 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001818 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001819 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1820
1821 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1822 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1823}
1824
Kevin May7d96b162021-02-03 17:38:41 +00001825void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001826{
1827 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1828
1829 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1830 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1831
1832 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1833 CHECK_VALID_SIZE(inputs.size(), 2);
1834
1835 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1836 CHECK_VALID_SIZE(outputs.size(), 1);
1837
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001838 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1839 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1840
James Ward58dec6b2020-09-11 17:32:44 +01001841 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001842 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001843 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001844
Sadik Armagand109a4d2020-07-28 10:42:13 +01001845 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -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 Goncalvesf803f782018-12-18 13:40:30 -02001850 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1851
1852 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1853 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1854}
1855
Kevin May7d96b162021-02-03 17:38:41 +00001856void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001857{
1858 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1859
1860 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1861
1862 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1863 CHECK_VALID_SIZE(outputs.size(), 1);
1864
1865 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
1866 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1867
1868 armnn::MeanDescriptor desc;
1869 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
1870 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
1871 desc.m_Axis = axis;
1872
1873 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001874 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001875
1876 desc.m_KeepDims =
1877 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
1878 true : false;
1879
James Ward58dec6b2020-09-11 17:32:44 +01001880 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001881 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001882 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001883
1884 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1885
1886 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1887 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1888
1889 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1890 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1891}
1892
Kevin May7d96b162021-02-03 17:38:41 +00001893void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001894{
1895 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1896
Kevin May7d96b162021-02-03 17:38:41 +00001897 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001898
Kevin May7d96b162021-02-03 17:38:41 +00001899 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001900 CHECK_VALID_SIZE(outputs.size(), 1);
1901
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00001902 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1903
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001904 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
1905 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1906
1907 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
1908 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
1909
1910 size_t step = 2;
1911 armnn::PadDescriptor desc;
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00001912 if (inputTensorInfo.IsQuantized())
1913 {
1914 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
1915 }
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001916 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
1917 {
1918 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
1919 }
1920
James Ward58dec6b2020-09-11 17:32:44 +01001921 auto layerName = fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001922 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001923
1924 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
1925 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001926 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1927
1928 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1929 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1930
1931 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1932 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1933}
1934
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01001935void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
1936{
1937 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1938
1939 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1940 CHECK_VALID_SIZE(inputs.size(), 2);
1941
1942 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1943 CHECK_VALID_SIZE(outputs.size(), 1);
1944
1945 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
1946
1947 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1948 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
1949 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1950 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1951
1952 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
1953 ARMNN_ASSERT(layer != nullptr);
1954 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1955
1956 if (IsConstTensor(inputs[1]))
1957 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01001958 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01001959 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
1960 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01001961
1962 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo);
1963 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
1964 IConnectableLayer* constLayer =
1965 m_Network->AddConstantLayer(alphaTensorAndData, constLayerName.c_str());
1966 ARMNN_ASSERT(constLayer != nullptr);
1967
1968 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
1969 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
1970 RegisterOutputSlots(subgraphIndex,
1971 VIRTUAL_OPERATOR_ID,
1972 constLayer,
1973 { inputTensorIndexes[1] });
1974 }
1975 else
1976 {
1977 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1978 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
1979 }
1980
1981 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1982 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1983}
1984
Kevin May7d96b162021-02-03 17:38:41 +00001985void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00001986{
1987 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1988
1989 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1990 CHECK_VALID_SIZE(inputs.size(), 1);
1991
1992 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1993 CHECK_VALID_SIZE(outputs.size(), 1);
1994
James Ward58dec6b2020-09-11 17:32:44 +01001995 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00001996
1997 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001998 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00001999
Sadik Armagand109a4d2020-07-28 10:42:13 +01002000 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002001 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2002
2003 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2004 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2005
2006 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2007 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2008}
Finn Williamsc42c3842019-01-22 14:18:11 +00002009
Kevin May7d96b162021-02-03 17:38:41 +00002010void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002011{
Finn Williamsc42c3842019-01-22 14:18:11 +00002012 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002013}
2014
Kevin May7d96b162021-02-03 17:38:41 +00002015void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002016{
Finn Williamsc42c3842019-01-22 14:18:11 +00002017 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2018}
Sadik Armagan58f39192018-09-17 14:14:39 +01002019
Kevin May7d96b162021-02-03 17:38:41 +00002020void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002021{
Jan Eilers2f746b32020-07-28 14:00:06 +01002022 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002023}
2024
Kevin May7d96b162021-02-03 17:38:41 +00002025void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002026{
2027 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2028}
2029
Kevin May7d96b162021-02-03 17:38:41 +00002030void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002031{
2032 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2033}
2034
Kevin May7d96b162021-02-03 17:38:41 +00002035void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002036{
2037 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2038}
2039
Kevin May7d96b162021-02-03 17:38:41 +00002040void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002041{
2042 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2043}
Finn Williamsc42c3842019-01-22 14:18:11 +00002044
Kevin May7d96b162021-02-03 17:38:41 +00002045void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002046{
2047 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan58f39192018-09-17 14:14:39 +01002048 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002049 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002050
2051 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2052 CHECK_VALID_SIZE(inputs.size(), 1);
2053
2054 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2055 CHECK_VALID_SIZE(outputs.size(), 1);
2056
James Ward58dec6b2020-09-11 17:32:44 +01002057 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002058 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002059 activationDesc.m_Function = activationType;
2060
2061 switch (activationType)
2062 {
2063 case ActivationFunction::ReLu:
2064 {
James Ward58dec6b2020-09-11 17:32:44 +01002065 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002066 break;
2067 }
2068 case ActivationFunction::BoundedReLu:
2069 {
James Ward58dec6b2020-09-11 17:32:44 +01002070 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002071 activationDesc.m_A = 6.0f;
2072 activationDesc.m_B = 0.0f;
2073 break;
2074 }
2075 case ActivationFunction::Sigmoid:
2076 {
James Ward58dec6b2020-09-11 17:32:44 +01002077 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002078 break;
2079 }
Nina Drozd99851762019-04-09 09:37:38 +01002080 case ActivationFunction::TanH:
2081 {
James Ward58dec6b2020-09-11 17:32:44 +01002082 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002083 activationDesc.m_A = 1.0f;
2084 activationDesc.m_B = 1.0f;
2085 break;
2086 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002087 case ActivationFunction::LeakyReLu:
2088 {
James Ward58dec6b2020-09-11 17:32:44 +01002089 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan12239e72020-05-27 11:06:17 +01002090 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2091 activationDesc.m_A = options->alpha;
2092 break;
2093 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002094 case ActivationFunction::Elu:
2095 {
2096 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2097 activationDesc.m_A = 1.0f;
2098 break;
2099 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002100 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002101 {
James Ward58dec6b2020-09-11 17:32:44 +01002102 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002103 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002104 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002105 default:
2106 {
2107 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002108 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2109 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002110 }
2111 }
2112
2113 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002114
Sadik Armagand109a4d2020-07-28 10:42:13 +01002115 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002116 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2117
2118 // register the input connection slots for the layer, connections are made after all layers have been created
2119 // only the tensors for the inputs are relevant, exclude the const tensors
2120 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2121 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2122
2123 // register the output connection slots for the layer, connections are made after all layers have been created
2124 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2125 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2126}
Kevin May7d96b162021-02-03 17:38:41 +00002127armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
2128 const std::vector<int32_t> & targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002129{
2130 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2131 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2132
2133 if (stretchDim != targetDimsIn.end())
2134 {
2135 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2136 {
2137 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002138 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002139 }
2140
2141 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002142 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002143 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2144
2145 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2146 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2147 }
2148
2149 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2150
2151 TensorInfo reshapeInfo = inputTensorInfo;
2152 reshapeInfo.SetShape(outputShape);
2153
2154 return reshapeInfo;
2155}
2156
Kevin May7d96b162021-02-03 17:38:41 +00002157void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002158{
2159 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2160
2161 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002162
2163 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2164 CHECK_VALID_SIZE(outputs.size(), 1);
2165
2166 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2167 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002168 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002169
2170 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002171 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002172 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002173
Jan Eilersbac9b352020-07-13 13:40:24 +01002174 // Extracting new shape for the output
2175 // There are two ways it can be passed
2176 // * First is to define the target shape in the operator built-in options
2177 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002178 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002179 bool targetShapeFound = false;
2180 // Check if built-in options were given
2181 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002182 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002183 // make sure the parameter is given
2184 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002185 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002186 targetShape = options->new_shape;
2187 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002188 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002189 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002190
2191 // If there is no built-in option given or if the built-in new_shape parameter was empty
2192 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002193 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002194 // Check for a second input tensor
2195 if (inputs.size() > 1 && inputs[1] != nullptr)
2196 {
2197 if (inputs[1]->is_variable)
2198 {
2199 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2200 }
2201
2202 if (inputs[1]->shape.size() != 1)
2203 {
2204 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2205 }
2206
2207 if (inputs[1]->type != tflite::TensorType_INT32)
2208 {
2209 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2210 }
2211
2212 // Extract target shape from input
2213 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2214 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Sadik Armagan19a1c032021-01-20 12:17:00 +00002215 if (!values)
2216 {
2217 ARMNN_THROW_PARSE_EXCEPTION("Reshape operator target shape input buffer data is null");
2218 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002219 for (int i=0; i < inputs[1]->shape[0]; ++i)
2220 {
2221 targetShape.push_back(values[i]);
2222 }
2223 }
2224 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002225 {
2226 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2227 "At least one method required");
2228 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002229 }
2230
kevmay0171972a82018-12-17 14:28:03 +00002231 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002232 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002233
kevmay0171972a82018-12-17 14:28:03 +00002234 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002235 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2236 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002237 {
2238 std::stringstream ss;
2239 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002240 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002241 << " does not equal output shape "
2242 << actualOutputTensorInfo.GetShape()
2243 << ": "
2244 << CHECK_LOCATION().AsString();
2245 throw ParseException(ss.str());
2246 }
2247
Sadikb94967b2018-09-19 15:30:00 +01002248 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002249 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002250
Sadikb94967b2018-09-19 15:30:00 +01002251 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002252 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002253 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002254
2255 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2256 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2257
2258 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2259 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2260}
2261
Kevin May7d96b162021-02-03 17:38:41 +00002262void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002263{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002264 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2265}
2266
Kevin May7d96b162021-02-03 17:38:41 +00002267void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002268{
2269 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2270}
2271
Kevin May7d96b162021-02-03 17:38:41 +00002272void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002273{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002274 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2275
2276 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2277 CHECK_VALID_SIZE(inputs.size(), 2);
2278
2279 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2280 CHECK_VALID_SIZE(outputs.size(), 1);
2281
2282 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2283
2284 // Data for the parsed tensor args (size) must be stored locally.
2285 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2286
2287 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2288 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2289
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002290 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002291 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002292 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002293 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2294 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002295
James Ward58dec6b2020-09-11 17:32:44 +01002296 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002297
2298 switch (resizeMethod)
2299 {
2300 case ResizeMethod::Bilinear:
2301 {
James Ward58dec6b2020-09-11 17:32:44 +01002302 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002303
2304 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2305 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2306
David Monahan4a0c9b92020-05-30 09:48:39 +01002307 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002308 break;
2309 }
2310 case ResizeMethod::NearestNeighbor:
2311 {
James Ward58dec6b2020-09-11 17:32:44 +01002312 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002313 break;
2314 }
2315 default:
2316 {
2317 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002318 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2319 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002320 }
2321 }
2322
James Conroy05102392020-06-24 15:39:55 +01002323 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002324 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002325 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2326
2327 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2328 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002329 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2330
2331 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2332 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2333
2334 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2335 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2336}
2337
Kevin May7d96b162021-02-03 17:38:41 +00002338void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002339{
2340 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2341
2342 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2343 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2344
2345 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2346
2347 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2348 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2349 CHECK_VALID_SIZE(outputs.size(), 1);
2350
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002351 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2352 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002353
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002354 const unsigned int concatDimInput = static_cast<unsigned int>(
2355 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002356
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002357 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2358 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002359
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002360 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002361
2362 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2363 {
2364 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2365
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002366 // This set up concatDescriptor view origin
2367 armnnUtils::ProcessConcatInputTensorInfo(
2368 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002369 }
2370
James Ward58dec6b2020-09-11 17:32:44 +01002371 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002372 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002373
Jim Flynn906f9462019-05-10 13:55:21 +01002374 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002375 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002376 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002377
James Conroy05102392020-06-24 15:39:55 +01002378 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002379 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002380
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002381 // add fused activation layer
2382 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002383
Sadik Armagan479045b2018-10-01 11:51:37 +01002384 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2385 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2386}
2387
Kevin May7d96b162021-02-03 17:38:41 +00002388void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002389{
2390 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2391
2392 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2393 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2394
2395 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2396
2397 FullyConnectedDescriptor desc;
2398 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002399 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002400
2401 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2402 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2403 CHECK_VALID_SIZE(outputs.size(), 1);
2404
2405 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2406
2407 // Fully Connected Layer accepts two dimensional weights input
2408 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2409 if (weightsDimension != 2)
2410 {
2411 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002412 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2413 "Node {}",
2414 weightsDimension,
2415 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002416 }
2417
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002418 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01002419 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002420
Finn Williamsd4fa5452021-03-01 12:31:41 +00002421 Optional<ConstTensor> filterOptionalConstTensor;
2422
2423 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
2424
Finn Williamsd4fa5452021-03-01 12:31:41 +00002425 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2426 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2427 if (desc.m_ConstantWeights)
2428 {
2429 filterOptionalConstTensor = Optional<ConstTensor>(CreateConstTensorNonPermuted(inputs[1], filterTensorInfo));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002430 }
2431 else
2432 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00002433 // Non const weights will need to be registered as inputs
2434 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002435 }
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002436
Finn Williamsd4fa5452021-03-01 12:31:41 +00002437 Optional<ConstTensor> biasOptionalConstTensor;
2438 if (inputs.size() == 3)
2439 {
2440 desc.m_BiasEnabled = true;
2441 if (desc.m_ConstantWeights)
2442 {
2443 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
2444 biasOptionalConstTensor = Optional<ConstTensor>(CreateConstTensorNonPermuted(inputs[2], biasTensorInfo));
2445 }
2446 else
2447 {
2448 // Non const biases will need to be registered as inputs
2449 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
2450 }
2451 }
2452
2453 layer = m_Network->AddFullyConnectedLayer(desc,
2454 filterOptionalConstTensor,
2455 biasOptionalConstTensor,
2456 layerName.c_str());
2457
2458 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002459 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2460
Finn Williamsd4fa5452021-03-01 12:31:41 +00002461 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002462 if (inputTensorInfo.GetNumDimensions() > 2)
2463 {
2464 // Add reshape to flatten to 2D [batch_size, input_size],
2465 // where "input_size" corresponds to the number of inputs to the layer,
2466 // matching the second dimension of weights,
2467 // and "batch_size" is calculated by dividing the number of elements by "input_size".
2468 std::vector<unsigned int> reshapedDimensions(2);
2469 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2470 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
2471
2472 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
2473 {
2474 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002475 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
2476 reshapedDimensions[1],
2477 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002478 }
2479
2480 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
2481 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
2482
James Ward58dec6b2020-09-11 17:32:44 +01002483 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00002484 armnn::ReshapeDescriptor reshapeDescriptor;
2485 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
2486 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002487
2488 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
2489 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
2490
2491 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00002492 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
2493 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
2494 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002495 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00002496
2497 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002498
Sadik Armagand109a4d2020-07-28 10:42:13 +01002499 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002500 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2501
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002502 // we need to add the activation layer and fortunately we don't need to care about the data layout
2503 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
2504 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002505
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002506 // register the output connection slots for the layer, connections are made after all layers have been created
2507 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2508 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2509}
2510
Kevin May7d96b162021-02-03 17:38:41 +00002511void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00002512{
2513 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2514
2515 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2516
2517 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2518 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2519 CHECK_VALID_SIZE(outputs.size(), 4);
2520
2521 // Obtain custom options from flexbuffers
2522 auto custom_options = operatorPtr->custom_options;
2523 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2524
2525 // Obtain descriptor information from tf lite
2526 DetectionPostProcessDescriptor desc;
2527 desc.m_MaxDetections = m["max_detections"].AsUInt32();
2528 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
2529 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
2530 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
2531 desc.m_NumClasses = m["num_classes"].AsUInt32();
2532 desc.m_ScaleH = m["h_scale"].AsFloat();
2533 desc.m_ScaleW = m["w_scale"].AsFloat();
2534 desc.m_ScaleX = m["x_scale"].AsFloat();
2535 desc.m_ScaleY = m["y_scale"].AsFloat();
2536
keidav0107d58c72019-02-26 11:57:39 +00002537 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00002538 {
keidav0107d58c72019-02-26 11:57:39 +00002539 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00002540 }
2541 if (!(m["detections_per_class"].IsNull()))
2542 {
2543 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
2544 }
2545
2546 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
2547 {
2548 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
2549 "must be positive and less than or equal to 1.");
2550 }
2551
2552 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002553 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00002554
James Ward58dec6b2020-09-11 17:32:44 +01002555 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002556 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00002557 layerName.c_str());
2558
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002559 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00002560
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002561 // The model does not specify the output shapes.
2562 // The output shapes are calculated from the max_detection and max_classes_per_detection.
2563 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
2564 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2565 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2566 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2567 m_OverridenOutputShapes.push_back({ 1 });
2568
keidav011b3e2ea2019-02-21 10:07:37 +00002569 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
2570 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002571 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00002572 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
2573 }
2574
2575 // Register the input connection slots for the layer, connections are made after all layers have been created
2576 // only the tensors for the inputs are relevant, exclude the const tensors
2577 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2578 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2579
2580 // Register the output connection slots for the layer, connections are made after all layers have been created
2581 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2582 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2583 outputTensorIndexes[1],
2584 outputTensorIndexes[2],
2585 outputTensorIndexes[3]});
2586}
2587
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002588/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00002589void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002590{
2591 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2592
2593 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2594 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2595 CHECK_VALID_SIZE(outputs.size(), 1);
2596
2597 if (inputs.size() < 1)
2598 {
2599 throw ParseException("Pack must have at least one input.");
2600 }
2601
2602 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2603 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2604
2605 StackDescriptor desc;
2606 desc.m_Axis = static_cast<uint32_t>(options->axis);
2607 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
2608
2609 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
2610 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2611 desc.m_InputShape = inputTensorInfo.GetShape();
2612
James Ward58dec6b2020-09-11 17:32:44 +01002613 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002614 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
2615
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002616 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002617
Sadik Armagand109a4d2020-07-28 10:42:13 +01002618 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002619 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2620
2621 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2622 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2623
2624 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2625 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2626}
2627
Kevin May7d96b162021-02-03 17:38:41 +00002628void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01002629{
2630 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2631
2632 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2633 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2634
2635 // This unpackAxis indicates the axis to unpack
2636 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
2637
2638 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2639 CHECK_VALID_SIZE(inputs.size(), 1);
2640
2641 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002642
2643 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2644 {
2645 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002646 fmt::format("The unpack axis: {} cannot be greater than or equal to "
2647 "the number of input dimension {} {}",
2648 unpackAxis,
2649 inputTensorInfo.GetNumDimensions(),
2650 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002651 }
2652
Nina Drozd200e3802019-04-15 09:47:39 +01002653 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
2654 // If num is not defined, automatically infer from the length of the dimension axis.
2655 if(unpackNum == 0)
2656 {
2657 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2658 }
2659
2660 // If unpack number cannot be inferred and is still zero, throw ParseException.
2661 if(unpackNum == 0)
2662 {
2663 throw ParseException("Number to unpack must greater than zero.");
2664 }
2665
2666 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2667 CHECK_VALID_SIZE(outputs.size(), unpackNum);
2668
2669 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2670 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2671
2672 // Add current input shape to unpackDimSizes
2673 for (unsigned int i = 0; i < inputDimSize; ++i)
2674 {
2675 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2676 }
2677
2678 if (unpackDimSizes[unpackAxis] != unpackNum)
2679 {
2680 throw ParseException("Number to unpack must be the same as length of the dimension to "
2681 "unpack along.");
2682 }
2683
2684 unpackDimSizes[unpackAxis] /= unpackNum;
2685
2686 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2687 for (unsigned int j = 0; j < unpackNum; ++j)
2688 {
2689 // Set the size of the views.
2690 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2691 {
2692 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2693 }
2694 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
2695 }
2696
James Ward58dec6b2020-09-11 17:32:44 +01002697 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01002698 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002699 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01002700
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002701 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
2702 unpackDimSizes.data());
2703
Nina Drozd200e3802019-04-15 09:47:39 +01002704 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2705 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2706
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002707 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
2708 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2709 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002710 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01002711 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002712 armnn::ReshapeDescriptor desc;
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002713 desc.m_TargetShape = outputTensorInfo.GetShape();
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002714 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2715
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002716 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
2717 outputTensorInfo.GetDataType(),
2718 outputTensorInfo.GetQuantizationScale(),
2719 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002720 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
2721
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002722 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002723
2724 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
2725 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
2726 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2727 }
Nina Drozd200e3802019-04-15 09:47:39 +01002728}
2729
Kevin May7d96b162021-02-03 17:38:41 +00002730void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01002731{
2732 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2733
2734 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2735 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2736
2737 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2738
Nina Drozd200e3802019-04-15 09:47:39 +01002739 // If number of splits cannot be inferred and is zero, throw ParseException.
2740 if(numSplits == 0)
2741 {
2742 throw ParseException("Number to splits must greater than zero.");
2743 }
2744
Nina Drozd0324f482019-04-08 10:52:10 +01002745 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2746 CHECK_VALID_SIZE(inputs.size(), 2);
2747 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2748 CHECK_VALID_SIZE(outputs.size(), numSplits);
2749
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002750 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
2751 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
2752 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01002753
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002754 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002755 if (axisBufferPtr == nullptr)
2756 {
2757 throw ParseException(
2758 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2759 CHECK_LOCATION().AsString()));
2760 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002761
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002762 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
2763 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2764 int32_t axis = axisData[0];
2765
2766 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2767 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2768 {
2769 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
2770 // E.g. Rank 4 tensor can have axis in range [-4, 3)
2771 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
2772 throw ParseException(
2773 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2774 axis,
2775 CHECK_LOCATION().AsString()));
2776 }
2777
2778 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01002779
Nina Drozd0324f482019-04-08 10:52:10 +01002780 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002781 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01002782 {
2783 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002784 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
2785 inputTensorInfo.GetNumDimensions(),
2786 MaxNumOfTensorDimensions,
2787 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01002788 }
2789
2790 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2791
2792 // Add current input shape to splitterDimSizes
2793 for (unsigned int i = 0; i < inputDimSize; ++i)
2794 {
2795 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2796 }
2797
2798 if (splitterDimSizes[splitDim] % numSplits != 0)
2799 {
2800 throw ParseException("Number of splits must evenly divide the dimension");
2801 }
2802 splitterDimSizes[splitDim] /= numSplits;
2803
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002804 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01002805 for (unsigned int j = 0; j < numSplits; ++j)
2806 {
2807 // Set the size of the views.
2808 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2809 {
2810 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2811 }
2812 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
2813 }
2814
James Ward58dec6b2020-09-11 17:32:44 +01002815 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01002816 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002817 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01002818
2819 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002820 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01002821
Nina Drozd0324f482019-04-08 10:52:10 +01002822 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2823 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002824 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01002825 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01002826 }
2827
2828 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2829 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2830}
2831
Derek Lambertif0176992020-04-28 13:37:49 +01002832unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
2833{
2834 int numDims = armnn::numeric_cast<int>(numDimsIn);
2835 int v = idx < 0 ? numDims + idx : idx;
2836 ARMNN_ASSERT(v >= 0);
2837 ARMNN_ASSERT(v < numDims);
2838
2839 return static_cast<unsigned int>(v);
2840}
2841
Kevin May7d96b162021-02-03 17:38:41 +00002842void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01002843{
2844 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2845
2846 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Ryan OShea86704732020-05-26 11:41:04 +01002847 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01002848
2849 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2850 CHECK_VALID_SIZE(inputs.size(), 3);
2851
2852 auto& inputTensor = inputs[0];
2853 auto& splitsTensor = inputs[1];
2854 auto& axisTensor = inputs[2];
2855
2856 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
2857 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
2858 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
2859 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2860
2861 // Inputs
2862 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2863 if (inputDimSize > MaxNumOfTensorDimensions)
2864 {
2865 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002866 fmt::format("The number of dimensions: {} for input tensors of the "
2867 "SplitV op cannot be greater than {} {}",
2868 inputTensorInfo.GetNumDimensions(),
2869 MaxNumOfTensorDimensions,
2870 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01002871 }
2872
2873 // Get split axis
2874 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002875 if (axisBufferPtr == nullptr)
2876 {
2877 throw ParseException(
2878 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2879 CHECK_LOCATION().AsString()));
2880 }
2881
Derek Lambertif0176992020-04-28 13:37:49 +01002882 std::vector<int> axisData(axisTensorInfo.GetNumElements());
2883 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002884 int32_t axis = axisData[0];
2885
2886 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2887 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2888 {
2889 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
2890 // E.g. Rank 4 tensor can have axis in range [-4, 3)
2891 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
2892 throw ParseException(
2893 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2894 axis,
2895 CHECK_LOCATION().AsString()));
2896 }
2897 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01002898
Derek Lambertif0176992020-04-28 13:37:49 +01002899 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01002900 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01002901 unsigned int numSplits{0};
2902
2903 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01002904 {
2905 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01002906 }
2907 else
2908 {
Ryan OShea86704732020-05-26 11:41:04 +01002909 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01002910 }
2911
2912 if (numSplits <=0)
2913 {
2914 throw ParseException("SplitV has invalid number of splits");
2915 }
2916
Jan Eilersc0761e92020-06-29 16:48:44 +01002917 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01002918 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01002919 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01002920
Jan Eilersc0761e92020-06-29 16:48:44 +01002921 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01002922 int numInferred{0};
2923 unsigned int inferIdx{0};
2924 int splitSum{0};
2925 for (auto split : splitsData)
2926 {
2927 if (split < 0)
2928 {
2929 numInferred++;
2930 inferIdx = idx;
2931 }
2932 else
2933 {
2934 splitSum += split;
2935 }
2936 idx++;
2937 }
2938 // Check for inferred Axis
2939 if (numInferred == 0)
2940 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002941 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01002942 {
2943 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
2944 }
2945 }
2946 else if (numInferred == 1)
2947 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002948 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01002949 }
2950 else
2951 {
2952 throw ParseException("Cannot infer split size for more than one split");
2953 }
2954
Derek Lambertif0176992020-04-28 13:37:49 +01002955 //Ouput size validation
2956 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2957 CHECK_VALID_SIZE(outputs.size(), numSplits);
2958
2959 // Setup Armnn descriptor
2960 SplitterDescriptor splitDesc(numSplits, inputDimSize);
2961 unsigned int accumSplit = 0;
2962 for (unsigned int j = 0; j < numSplits; ++j)
2963 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002964 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01002965
2966 // Set the size of the views.
2967 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
2968 {
2969 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
2970 if (dimIdx == splitDim)
2971 {
2972 dimSize = splitSize;
2973 }
2974 splitDesc.SetViewSize(j, dimIdx, dimSize);
2975 }
2976
2977 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
2978 accumSplit += splitSize;
2979 }
2980
James Ward58dec6b2020-09-11 17:32:44 +01002981 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01002982 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002983 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01002984
2985 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2986 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2987
2988 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2989 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002990 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01002991 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
2992 }
2993
2994 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2995 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2996}
2997
Matthew Sloyan28f177c2021-04-09 14:38:52 +01002998void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
2999{
3000 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3001}
3002
Kevin May7d96b162021-02-03 17:38:41 +00003003void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003004{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003005 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3006}
3007
3008void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3009{
Inki Daed4619e22020-09-10 15:33:54 +09003010 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3011 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3012 CHECK_VALID_SIZE(inputs.size(), 2);
3013
3014 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3015 CHECK_VALID_SIZE(outputs.size(), 1);
3016
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003017 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3018 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003019 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003020 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003021
3022 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003023 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3024 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3025 {
3026 throw ParseException(
3027 fmt::format(
3028 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3029 CHECK_LOCATION().AsString()));
3030 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003031
3032 // Get const axis value from model and set it to descriptor.
3033 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3034 if (axisBufferPtr == nullptr)
3035 {
3036 throw ParseException(
3037 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3038 CHECK_LOCATION().AsString()));
3039 }
3040
3041 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3042 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3043 int32_t axis = axisData.front();
3044
3045 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3046 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3047 {
3048 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3049 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3050 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3051 throw ParseException(
3052 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3053 axis,
3054 CHECK_LOCATION().AsString()));
3055 }
3056
3057 ArgMinMaxDescriptor desc;
3058 desc.m_Axis = axis;
3059 desc.m_Function = argMinMaxFunction;
3060
3061 // Register a ArgMin/ArgMax layer.
3062 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3063 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3064 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3065 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003066 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3067
3068 // Register input tensor to the layer.
3069 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3070 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3071
3072 // Register output tensor to the layer.
3073 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3074 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3075}
3076
Kevin May7d96b162021-02-03 17:38:41 +00003077void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003078{
3079 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3080
Kevin May7d96b162021-02-03 17:38:41 +00003081 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003082 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003083 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003084 CHECK_VALID_SIZE(outputs.size(), 1);
3085
3086 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3087 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3088 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3089
3090 armnn::GatherDescriptor gatherDescriptor;
3091
3092 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3093 const auto * options = operatorPtr->builtin_options.AsGatherOptions();
3094 auto axis = options->axis;
3095
3096 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3097 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3098 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3099 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3100 {
3101 throw ParseException(
3102 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3103 axis,
3104 inputDimensions, inputDimensions,
3105 CHECK_LOCATION().AsString()));
3106 }
3107 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3108 {
3109 throw ParseException(
3110 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3111 outputDimensions,
3112 inputDimensions, indicesDimensions,
3113 CHECK_LOCATION().AsString()));
3114 }
3115
3116 gatherDescriptor.m_Axis = axis;
3117
3118 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3119 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3120 ARMNN_ASSERT(layer != nullptr);
3121 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3122
3123 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3124 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3125
3126 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3127 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3128}
3129
Kevin May7d96b162021-02-03 17:38:41 +00003130void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003131{
3132 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3133
Kevin May7d96b162021-02-03 17:38:41 +00003134 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003135 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00003136 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003137 CHECK_VALID_SIZE(outputs.size(), 1);
3138
3139 armnn::DepthToSpaceDescriptor descriptor;
3140
3141 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3142 const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
3143 auto blockSize = options->block_size;
3144 if (blockSize < 2)
3145 {
3146 throw ParseException(
3147 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
3148 blockSize,
3149 CHECK_LOCATION().AsString()));
3150 }
3151 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
3152
3153 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
3154 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
3155 ARMNN_ASSERT(layer != nullptr);
3156 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3157 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3158
3159 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3160 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3161
3162 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3163 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3164}
3165
Kevin May7d96b162021-02-03 17:38:41 +00003166void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003167{
Sadik Armagana2747482021-02-09 10:28:54 +00003168 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
3169}
3170
3171void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
3172{
3173 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
3174}
3175
3176void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
3177{
3178 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
3179}
3180
3181void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
3182{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003183 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3184
3185 const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3186 const auto *options = operatorPtr->builtin_options.AsReducerOptions();
3187
3188 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3189 CHECK_VALID_SIZE(inputs.size(), 2);
3190
3191 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3192 CHECK_VALID_SIZE(outputs.size(), 1);
3193
Sadik Armagana2747482021-02-09 10:28:54 +00003194 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003195
3196 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
3197 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003198
3199 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003200 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3201 // Get const axis value from model and set it to descriptor.
3202 if (axisBufferPtr != nullptr)
3203 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00003204 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
3205 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
3206
3207 // Convert the axis to unsigned int and remove duplicates.
3208 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
3209 std::set<unsigned int> uniqueAxis;
3210 std::transform(axisData.begin(),
3211 axisData.end(),
3212 std::inserter(uniqueAxis, uniqueAxis.begin()),
3213 [rank](int i)->unsigned int{
3214 return static_cast<uint32_t>(((i + rank) % rank)); });
3215 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003216 }
Sadik Armagana2747482021-02-09 10:28:54 +00003217 else
3218 {
3219 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
3220 {
3221 desc.m_vAxis.push_back(i);
3222 }
3223 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003224
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003225 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00003226 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003227
3228 // Register a new layer object, Sum.
3229 IConnectableLayer *layer = m_Network->AddReduceLayer(desc, layerName.c_str());
3230
3231 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3232 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3233
3234 // Register input tensor to the layer.
3235 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3236 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3237
3238 // Register output tensor to the layer.
3239 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3240 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3241}
3242
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003243void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
3244{
3245 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
3246}
3247
3248void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
3249{
3250 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
3251}
3252
3253void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
3254{
3255 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
3256}
3257
3258void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
3259{
3260 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
3261}
3262
3263void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
3264{
3265 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
3266}
3267
3268void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
3269{
3270 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3271
3272 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3273 CHECK_VALID_SIZE(inputs.size(), 1);
3274
3275 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3276 CHECK_VALID_SIZE(outputs.size(), 1);
3277
3278 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
3279 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3280
3281 ElementwiseUnaryDescriptor desc;
3282 desc.m_Operation = unaryOperation;
3283 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
3284 ARMNN_ASSERT(layer != nullptr);
3285
3286 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3287 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3288
3289 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3290 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3291
3292 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3293 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3294}
3295
Kevin May7d96b162021-02-03 17:38:41 +00003296armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
3297 unsigned int outputSlot,
3298 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01003299{
3300 ActivationDescriptor activationDesc;
3301 std::string layerName = prevLayer->GetName();
3302
3303 switch(activationType)
3304 {
3305 case tflite::ActivationFunctionType_NONE:
3306 {
3307 // this is a no-op: return previous layer
3308 return prevLayer;
3309 }
3310 case tflite::ActivationFunctionType_RELU:
3311 {
3312 activationDesc.m_Function = ActivationFunction::ReLu;
3313 layerName += ":RELU";
3314 break;
3315 }
3316 case tflite::ActivationFunctionType_RELU6:
3317 {
3318 activationDesc.m_Function = ActivationFunction::BoundedReLu;
3319 activationDesc.m_A = 6.0f;
3320 activationDesc.m_B = 0.0f;
3321 layerName += ":RELU6";
3322 break;
3323 }
3324 case tflite::ActivationFunctionType_TANH:
3325 {
3326 activationDesc.m_Function = ActivationFunction::TanH;
3327 activationDesc.m_A = 1.0f;
3328 activationDesc.m_B = 1.0f;
3329 layerName += ":TANH";
3330 break;
3331 }
3332
3333 // I only put these here as a reminder what others we could support
3334 case tflite::ActivationFunctionType_RELU_N1_TO_1:
3335 case tflite::ActivationFunctionType_SIGN_BIT:
3336 default:
3337 {
3338 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003339 fmt::format("TfLite parser doesn't suppport fused activation: "
3340 "{}/{} {} ",
3341 activationType,
3342 tflite::EnumNameActivationFunctionType(activationType),
3343 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003344
3345 }
3346 }
3347
3348 IConnectableLayer* activationLayer =
3349 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3350
3351 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
3352 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
3353 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
3354 return activationLayer;
3355}
3356
Kevin May7d96b162021-02-03 17:38:41 +00003357TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char * fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01003358{
3359 if (fileName == nullptr)
3360 {
James Ward58dec6b2020-09-11 17:32:44 +01003361 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01003362 CHECK_LOCATION().AsString()));
3363 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01003364 std::error_code errorCode;
3365 fs::path pathToFile(fileName);
3366 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01003367 {
James Ward58dec6b2020-09-11 17:32:44 +01003368 //fmt::format() could not be used here (format error)
3369 std::stringstream msg;
3370 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
3371 << " " << CHECK_LOCATION().AsString();
3372
3373 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01003374 }
3375 std::ifstream file(fileName, std::ios::binary);
3376 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3377 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
3378 fileContent.size());
3379}
3380
Kevin May7d96b162021-02-03 17:38:41 +00003381TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t * binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01003382{
3383 if (binaryContent == nullptr)
3384 {
James Ward58dec6b2020-09-11 17:32:44 +01003385 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01003386 CHECK_LOCATION().AsString()));
3387 }
3388 flatbuffers::Verifier verifier(binaryContent, len);
3389 if (verifier.VerifyBuffer<tflite::Model>() == false)
3390 {
3391 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003392 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
3393 "flatbuffers format. size:{} {}",
3394 len,
3395 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003396 }
3397 return tflite::UnPackModel(binaryContent);
3398}
3399
Kevin May7d96b162021-02-03 17:38:41 +00003400TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr & model,
3401 size_t subgraphIndex,
3402 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003403{
3404 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3405
Derek Lambertiff05cc52019-04-26 13:05:17 +01003406 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3407 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003408
3409 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01003410 TensorRawPtrVector result;
telsoa01c577f2c2018-08-31 09:22:23 +01003411 for (size_t i=0; i<inputCount; ++i)
3412 {
mathad01c21025d2021-04-26 10:09:37 +01003413 // If the input location is -1 then assume input is turned off.
3414 if (operatorPtr->inputs[i] == -1)
3415 {
3416 continue;
3417 }
3418 else
3419 {
3420 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
3421 result.push_back(subgraphPtr->tensors[inputId].get());
3422 }
telsoa01c577f2c2018-08-31 09:22:23 +01003423 }
3424 return result;
3425}
3426
Kevin May7d96b162021-02-03 17:38:41 +00003427TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr & model,
3428 size_t subgraphIndex,
3429 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003430{
3431 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3432
Derek Lambertiff05cc52019-04-26 13:05:17 +01003433 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3434 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003435
3436 size_t outputCount = operatorPtr->outputs.size();
3437 TensorRawPtrVector result(outputCount);
3438 for (size_t i=0; i<outputCount; ++i)
3439 {
3440 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
3441 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003442 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01003443 }
3444 return result;
3445}
3446
Kevin May7d96b162021-02-03 17:38:41 +00003447TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr & model,
3448 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003449{
3450 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003451 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003452
Derek Lambertiff05cc52019-04-26 13:05:17 +01003453 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003454 TensorIdRawPtrVector result(inputCount);
3455 for (size_t i=0; i<inputCount; ++i)
3456 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003457 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01003458 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003459 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003460 }
3461 return result;
3462}
3463
Kevin May7d96b162021-02-03 17:38:41 +00003464TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr & model,
3465 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003466{
3467 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003468 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003469
Derek Lambertiff05cc52019-04-26 13:05:17 +01003470 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003471 TensorIdRawPtrVector result(outputCount);
3472 for (size_t i=0; i<outputCount; ++i)
3473 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003474 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
3475 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003476 }
3477 return result;
3478}
3479
Kevin May7d96b162021-02-03 17:38:41 +00003480std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
3481 size_t subgraphIndex,
3482 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003483{
3484 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003485 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3486 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003487 return operatorPtr->inputs;
3488}
3489
Kevin May7d96b162021-02-03 17:38:41 +00003490std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
3491 size_t subgraphIndex,
3492 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003493{
3494 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003495 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3496 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003497 return operatorPtr->outputs;
3498}
3499
Kevin May7d96b162021-02-03 17:38:41 +00003500void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
3501 size_t operatorIndex,
3502 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00003503 const std::vector<unsigned int>& tensorIndexes,
3504 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003505{
3506 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003507 ARMNN_ASSERT(layer != nullptr);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003508 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01003509 {
3510 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003511 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
3512 " for subgraph:{} operator index:{} {}",
3513 tensorIndexes.size(),
3514 layer->GetNumInputSlots(),
3515 subgraphIndex,
3516 operatorIndex,
3517 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003518 }
3519
Finn Williamsd4fa5452021-03-01 12:31:41 +00003520 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01003521 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00003522 unsigned int tensorIndex = tensorIndexes[index];
3523 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01003524 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3525 }
3526}
3527
Kevin May7d96b162021-02-03 17:38:41 +00003528void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
3529 size_t operatorIndex,
3530 IConnectableLayer* layer,
3531 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01003532{
3533 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003534 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003535 if (tensorIndexes.size() != layer->GetNumOutputSlots())
3536 {
3537 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003538 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
3539 " for subgraph:{} operator index:{} {}",
3540 tensorIndexes.size(),
3541 layer->GetNumOutputSlots(),
3542 subgraphIndex,
3543 operatorIndex,
3544 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003545 }
3546
3547 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
3548 {
3549 unsigned int tensorIndex = tensorIndexes[slotIndex];
3550 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
3551 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3552 }
3553}
3554
Kevin May7d96b162021-02-03 17:38:41 +00003555void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003556{
3557 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3558
3559 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
3560 for (auto const & tensorIdAndPtr : inputs)
3561 {
3562 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3563 IConnectableLayer* layer =
3564 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3565
3566 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
3567 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3568
3569 RegisterOutputSlots(subgraphIndex,
3570 VIRTUAL_OPERATOR_ID,
3571 layer,
3572 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3573 }
3574}
3575
Kevin May7d96b162021-02-03 17:38:41 +00003576void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003577{
3578 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3579
3580 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
3581 for (auto const & tensorIdAndPtr : outputs)
3582 {
3583 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3584 IConnectableLayer* layer =
3585 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3586
3587 RegisterInputSlots(subgraphIndex,
3588 VIRTUAL_OPERATOR_ID,
3589 layer,
3590 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3591 }
3592}
3593
Kevin May7d96b162021-02-03 17:38:41 +00003594void TfLiteParserImpl::SetupConstantLayers(size_t subgraphIndex)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003595{
3596 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3597
Derek Lambertiff05cc52019-04-26 13:05:17 +01003598 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003599 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3600 {
3601 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3602 {
3603 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
3604 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3605 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003606 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003607 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
Finn Williamsd4fa5452021-03-01 12:31:41 +00003608 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003609
James Ward58dec6b2020-09-11 17:32:44 +01003610 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003611 IConnectableLayer *layer =
Finn Williamsd4fa5452021-03-01 12:31:41 +00003612 m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003613
3614 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3615 RegisterOutputSlots(subgraphIndex,
3616 VIRTUAL_OPERATOR_ID,
3617 layer,
3618 { tensorIndex });
3619
3620 }
3621 }
3622 }
3623}
3624
telsoa01c577f2c2018-08-31 09:22:23 +01003625// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00003626TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003627{
3628 CHECK_BUFFER(model, bufferIndex);
3629 return model->buffers[bufferIndex].get();
3630}
3631
Matteo Martincigh747ef822018-12-18 09:26:39 +00003632template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00003633std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3634TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
3635 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00003636 armnn::TensorInfo& tensorInfo,
3637 armnn::Optional<armnn::PermutationVector&> permutationVector)
3638{
3639 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3640 tensorPtr,
3641 tensorInfo,
3642 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00003643 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00003644 return std::make_pair(constData.first, std::move(storage));
3645}
3646
Finn Williamsd4fa5452021-03-01 12:31:41 +00003647bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
3648{
3649 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01003650 bool isConst = true;
3651
3652 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
3653 if (buffer->data.size() == 0)
3654 {
3655 isConst = false;
3656 }
3657
3658 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00003659}
3660
3661
Kevin May7d96b162021-02-03 17:38:41 +00003662std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00003663TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
3664 armnn::TensorInfo& tensorInfo,
3665 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01003666{
3667 CHECK_TENSOR_PTR(tensorPtr);
3668 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3669 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3670
3671 switch (tensorInfo.GetDataType())
3672 {
3673 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003674 return CreateConstTensorAndStoreData<float>(bufferPtr,
3675 tensorPtr,
3676 tensorInfo,
3677 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00003678 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003679 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3680 tensorPtr,
3681 tensorInfo,
3682 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00003683 case armnn::DataType::QSymmS8:
3684 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3685 tensorPtr,
3686 tensorInfo,
3687 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00003688 case armnn::DataType::QAsymmS8:
3689 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3690 tensorPtr,
3691 tensorInfo,
3692 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003693 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003694 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3695 tensorPtr,
3696 tensorInfo,
3697 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003698 default:
3699 {
3700 std::stringstream errString;
3701 errString << "Unexpected datatype when creating const tensor: "
3702 << armnn::GetDataTypeName(tensorInfo.GetDataType())
3703 << " shape:" << tensorInfo.GetShape()
3704 << CHECK_LOCATION().AsString();
3705 throw ParseException(errString.str());
3706 }
3707 }
3708}
3709
Finn Williamsd4fa5452021-03-01 12:31:41 +00003710armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
3711 armnn::TensorInfo& tensorInfo)
3712{
3713 CHECK_TENSOR_PTR(tensorPtr);
3714 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3715 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3716
3717 return ConstTensor(tensorInfo, bufferPtr->data.data());
3718}
3719
Kevin May7d96b162021-02-03 17:38:41 +00003720BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
3721 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01003722{
3723 CHECK_SUBGRAPH(m_Model, subgraphId);
3724 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3725 for (auto const & input : inputs)
3726 {
3727 if (input.second->name == name)
3728 {
3729 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3730 return std::make_pair(bindingId, ToTensorInfo(input.second));
3731 }
3732 }
3733
3734 std::stringstream bindings;
3735 for (auto const & input : inputs)
3736 {
3737 bindings << "'" << input.second->name << "' ";
3738 }
3739
3740 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003741 fmt::format("No input binding found for subgraph:{} and name:{}. "
3742 "Possible inputs are: [{}] {}",
3743 subgraphId,
3744 name,
3745 bindings.str(),
3746 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003747}
3748
Kevin May7d96b162021-02-03 17:38:41 +00003749BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
3750 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01003751{
3752 CHECK_SUBGRAPH(m_Model, subgraphId);
3753 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003754 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01003755 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003756 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01003757 if (output.second->name == name)
3758 {
3759 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003760 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3761 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3762 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01003763 }
3764 }
3765
3766 std::stringstream bindings;
3767 for (auto const & output : outputs)
3768 {
3769 bindings << "'" << output.second->name << "' ";
3770 }
3771
3772 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003773 fmt::format("No output binding found for subgraph:{} and name:{}. "
3774 "Possible outputs are: [{}] {}",
3775 subgraphId,
3776 name,
3777 bindings.str(),
3778 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003779}
3780
Kevin May7d96b162021-02-03 17:38:41 +00003781size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01003782{
3783 return m_Model->subgraphs.size();
3784}
3785
Kevin May7d96b162021-02-03 17:38:41 +00003786std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01003787{
3788 CHECK_SUBGRAPH(m_Model, subgraphId);
3789 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3790 std::vector<std::string> result;
3791 result.reserve(inputs.size());
3792 for (auto const & input : inputs)
3793 {
3794 result.push_back(input.second->name);
3795 }
3796 return result;
3797}
3798
Kevin May7d96b162021-02-03 17:38:41 +00003799std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01003800{
3801 CHECK_SUBGRAPH(m_Model, subgraphId);
3802 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
3803 std::vector<std::string> result;
3804 result.reserve(outputs.size());
3805 for (auto const & output : outputs)
3806 {
3807 result.push_back(output.second->name);
3808 }
3809 return result;
3810}
3811
Matthew Sloyanac001ee2021-02-03 10:43:04 +00003812const std::string TfLiteParserImpl::GetVersion()
3813{
3814 return TFLITE_PARSER_VERSION;
3815}
3816
Kevin May7d96b162021-02-03 17:38:41 +00003817TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01003818: m_FloatData(std::move(data))
3819, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003820, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003821, m_Int32Data(nullptr)
3822{
3823}
3824
Kevin May7d96b162021-02-03 17:38:41 +00003825TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01003826: m_FloatData(nullptr)
3827, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00003828, m_Int8Data(nullptr)
3829, m_Int32Data(nullptr)
3830{
3831}
3832
Kevin May7d96b162021-02-03 17:38:41 +00003833TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
Keith Davisd305e1a2020-01-22 11:57:54 +00003834: m_FloatData(nullptr)
3835, m_Uint8Data(nullptr)
3836, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01003837, m_Int32Data(nullptr)
3838{
3839}
3840
Kevin May7d96b162021-02-03 17:38:41 +00003841TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01003842: m_FloatData(nullptr)
3843, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003844, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003845, m_Int32Data(std::move(data))
3846{
3847}
3848
3849} // armnnTfLiteParser