blob: 2ac325c08ce096a640454aa9be86cee997e8bcfb [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>
Rob Hughes9542f902021-07-14 09:48:54 +010023#include <armnnUtils/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,
Finn Williamsb49ed182021-06-29 15:50:08 +0100366 const std::vector<unsigned int>& shape,
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 }
Finn Williamsb49ed182021-06-29 15:50:08 +0100415 TensorShape tensorShape;
416
417 std::vector<unsigned int> safeShape = shape;
418 if (shape.size() == 0)
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100419 {
420 safeShape.push_back(1);
Finn Williamsb49ed182021-06-29 15:50:08 +0100421 }
422
423 if (!outputTensor)
424 {
425 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
426 }
427 else
428 {
Rob Hughesd812a312021-08-06 13:10:53 +0100429 size_t shapeSignatureSize = tensorPtr->shape_signature.size();
Finn Williamsb49ed182021-06-29 15:50:08 +0100430
431 // If a shape signature exists we will use that to infer dynamic tensors
432 if (shapeSignatureSize != 0)
Sadik Armagand109a4d2020-07-28 10:42:13 +0100433 {
Finn Williamsb49ed182021-06-29 15:50:08 +0100434 // If the shape is incompatible with the shape signature override the shape
435 if (shapeSignatureSize != shape.size())
436 {
437 safeShape = {};
438
439 for (unsigned int i = 0; i < shapeSignatureSize; ++i)
440 {
441 unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
442 static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
443 safeShape.push_back(dim);
444 }
445 }
446
Rob Hughesd812a312021-08-06 13:10:53 +0100447 std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
Finn Williamsb49ed182021-06-29 15:50:08 +0100448 for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
449 {
450 dimMask[i] = tensorPtr->shape_signature[i] == -1 ? false : true;
451 }
Rob Hughesd812a312021-08-06 13:10:53 +0100452 tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
Finn Williamsb49ed182021-06-29 15:50:08 +0100453 }
454 // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
455 else if (shape.size() == 0)
456 {
457 tensorShape = TensorShape(1, false);
458 }
459 else
460 {
461 tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
Sadik Armagand109a4d2020-07-28 10:42:13 +0100462 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100463 }
464
Keith Davisd305e1a2020-01-22 11:57:54 +0000465 float quantizationScale = 0.0f;
466 int32_t quantizationOffset = 0;
467
468 if (tensorPtr->quantization.get())
469 {
470 if (tensorPtr->quantization->scale.size() <= 1)
471 {
472 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
473 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
474
475 if (tensorPtr->quantization->scale.size() == 1)
476 {
477 quantizationScale = tensorPtr->quantization->scale[0];
478 }
479 if (tensorPtr->quantization->zero_point.size() == 1)
480 {
481 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000482 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100483 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000484 }
485
Sadik Armagand109a4d2020-07-28 10:42:13 +0100486 armnn::TensorInfo result(tensorShape,
487 type,
488 quantizationScale,
489 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000490 return result;
491 }
492 else
493 {
494 std::vector<float> quantizationScales;
495 std::vector<int32_t> quantizationOffsets;
496
497 // Scale
498 std::copy(tensorPtr->quantization->scale.begin(),
499 tensorPtr->quantization->scale.end(),
500 std::back_inserter(quantizationScales));
501
Keith Davis0c2eeac2020-02-11 16:51:50 +0000502 // QSymmS8 Per-axis
Sadik Armagand109a4d2020-07-28 10:42:13 +0100503 armnn::TensorInfo result(tensorShape,
504 type,
505 quantizationScales,
Jan Eilers7612bd62021-04-06 17:29:03 +0100506 armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
Keith Davisd305e1a2020-01-22 11:57:54 +0000507 return result;
508 }
509 }
510 else
511 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100512 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000513 type,
514 quantizationScale,
515 quantizationOffset);
516 return result;
517 }
telsoa01c577f2c2018-08-31 09:22:23 +0100518}
519
Jan Eilers7612bd62021-04-06 17:29:03 +0100520armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr)
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000521{
522 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100523 return ToTensorInfo(tensorPtr, dimensions);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000524}
525
Kevin May7d96b162021-02-03 17:38:41 +0000526armnn::TensorInfo ToTensorInfo(TfLiteParserImpl::TensorRawPtr tensorPtr,
Sadik Armagand109a4d2020-07-28 10:42:13 +0100527 const bool outputTensor)
528{
529 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
Jan Eilers7612bd62021-04-06 17:29:03 +0100530 return ToTensorInfo(tensorPtr, dimensions, outputTensor);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100531}
532
telsoa01c577f2c2018-08-31 09:22:23 +0100533template<typename T>
534std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
Kevin May7d96b162021-02-03 17:38:41 +0000535CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
536 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000537 armnn::TensorInfo& tensorInfo,
538 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100539{
Jan Eilers8eb25602020-03-09 12:13:48 +0000540 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100541 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
542 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
James Ward58dec6b2020-09-11 17:32:44 +0100543 fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
telsoa01c577f2c2018-08-31 09:22:23 +0100544
545 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000546
547 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
548 {
549 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000550 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
551 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000552 }
553 else
554 {
555 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
556 }
557
Matthew Sloyan81beae32021-07-13 19:46:11 +0100558 // Make sure isConstant flag is set.
559 tensorInfo.SetConstant();
560
telsoa01c577f2c2018-08-31 09:22:23 +0100561 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
562}
563
telsoa01c577f2c2018-08-31 09:22:23 +0100564armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
565{
566 // generate the binding id by shifting the tensor id by 8 bit
567 // and add the subgraph id, which allows 256 subgraphs
568 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
569}
570
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000571bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
572{
573 const unsigned int actualSize = actual.GetNumDimensions();
574 if (actualSize != expected.size())
575 {
576 return false;
577 }
578
579 for (unsigned int i = 0u; i < actualSize; i++)
580 {
581 if (expected[i] < 0 ||
582 actual[i] != static_cast<unsigned int>(expected[i]))
583 {
584 return false;
585 }
586 }
587
588 return true;
589}
590
James Conroy05102392020-06-24 15:39:55 +0100591void CheckMatchingQuantization(const TensorInfo& first,
592 const TensorInfo& second,
593 const std::string& descName,
594 std::string const& firstName,
595 std::string const& secondName)
596{
597 if (!first.IsQuantized() ||
598 !second.IsQuantized())
599 {
600 // Not a quantized type, ignore the validation
601 return;
602 }
603
604 DataType firstDataType = first.GetDataType();
605 DataType secondDataType = second.GetDataType();
606
607 if (firstDataType != secondDataType)
608 {
609 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
610 " must be of the same quantized type, " +
611 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
612 secondName + " is " + GetDataTypeName(secondDataType));
613 }
614
615 if (!first.IsTypeSpaceMatch(second))
616 {
617 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
618 " must have the same quantization space, " +
619 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
620 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
621 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
622 " and scale " + std::to_string(second.GetQuantizationScale()));
623 }
624}
625
telsoa01c577f2c2018-08-31 09:22:23 +0100626} // <anonymous>
627
Kevin May7d96b162021-02-03 17:38:41 +0000628TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100629: m_Options(options)
630, m_Network(nullptr, nullptr)
Kevin May7d96b162021-02-03 17:38:41 +0000631, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
telsoa01c577f2c2018-08-31 09:22:23 +0100632{
633 // register supported operators
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100634 m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
Kevin May7d96b162021-02-03 17:38:41 +0000635 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100636 m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
637 m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
Kevin May7d96b162021-02-03 17:38:41 +0000638 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
639 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
mathad01b392e982021-04-07 12:07:30 +0100640 m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
Kevin May7d96b162021-02-03 17:38:41 +0000641 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
642 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
643 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
644 m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
645 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
646 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100647 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
Kevin May7d96b162021-02-03 17:38:41 +0000648 m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300649 m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000650 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
Teresa Charlin3ab85482021-06-08 16:59:29 +0100651 m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
Kevin May7d96b162021-02-03 17:38:41 +0000652 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
653 m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300654 m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
655 m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000656 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
657 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300658 m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
659 m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100660 m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
Kevin May7d96b162021-02-03 17:38:41 +0000661 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
662 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
663 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
664 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
665 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
666 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
667 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
668 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
Bruno Goncalves2d0eb862021-07-11 14:10:15 -0300669 m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
Kevin May7d96b162021-02-03 17:38:41 +0000670 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
671 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +0100672 m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
Kevin May7d96b162021-02-03 17:38:41 +0000673 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
674 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
675 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
Sadik Armagana2747482021-02-09 10:28:54 +0000676 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
677 m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
Kevin May7d96b162021-02-03 17:38:41 +0000678 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
679 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
680 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
Matthew Sloyaned7fce42021-04-15 20:46:24 +0100681 m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
Keith Davis0176fd82021-06-01 17:36:32 +0100682 m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
Kevin May7d96b162021-02-03 17:38:41 +0000683 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
684 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
685 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
686 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
687 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
688 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
689 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
690 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
691 m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
692 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
693 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
694 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
695 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
Matthew Sloyan28f177c2021-04-09 14:38:52 +0100696
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100697 // register supported custom operators
Kevin May7d96b162021-02-03 17:38:41 +0000698 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100699}
700
Kevin May7d96b162021-02-03 17:38:41 +0000701void TfLiteParserImpl::ResetParser()
telsoa01c577f2c2018-08-31 09:22:23 +0100702{
703 m_Network = armnn::INetworkPtr(nullptr, nullptr);
704 m_Model = nullptr;
705 m_SubgraphConnections.clear();
706}
707
Kevin May7d96b162021-02-03 17:38:41 +0000708INetworkPtr TfLiteParserImpl::CreateNetworkFromBinaryFile(const char* graphFile)
telsoa01c577f2c2018-08-31 09:22:23 +0100709{
710 ResetParser();
711 m_Model = LoadModelFromFile(graphFile);
712 return CreateNetworkFromModel();
713}
714
Kevin May7d96b162021-02-03 17:38:41 +0000715INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
telsoa01c577f2c2018-08-31 09:22:23 +0100716{
717 ResetParser();
718 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
719 return CreateNetworkFromModel();
720}
721
Finn Williamsb49ed182021-06-29 15:50:08 +0100722
723armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
724{
725 ResetParser();
726 m_Model = std::move(model);
727
728 return CreateNetworkFromModel();
729}
730
Kevin May7d96b162021-02-03 17:38:41 +0000731INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
telsoa01c577f2c2018-08-31 09:22:23 +0100732{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100733
734 using NetworkOptions = std::vector<BackendOptions>;
735 NetworkOptions networkOptions = {};
736 if (m_Options && m_Options.value().m_InferAndValidate)
737 {
738 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
739 {
740 { "InferAndValidate", true }
741 });
742
743 networkOptions.push_back(shapeInferenceMethodOption);
744 }
745
746 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100747 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100748
telsoa01c577f2c2018-08-31 09:22:23 +0100749 if (m_Model->subgraphs.size() != 1)
750 {
751 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100752 fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
753 m_Model->subgraphs.size(),
754 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100755 }
756
757 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100758 size_t operatorIndex = 0;
759 try
telsoa01c577f2c2018-08-31 09:22:23 +0100760 {
Colm Donelan6350d272020-06-09 16:56:25 +0100761 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100762 {
Colm Donelan6350d272020-06-09 16:56:25 +0100763 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
764 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100765 {
Colm Donelan6350d272020-06-09 16:56:25 +0100766 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
telsoa01c577f2c2018-08-31 09:22:23 +0100767 auto builtinCode = opCodePtr->builtin_code;
768
769 if (builtinCode > tflite::BuiltinOperator_MAX)
770 {
James Ward58dec6b2020-09-11 17:32:44 +0100771 throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
772 "subgraph:{} operator idx:{}. {}",
773 builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
774 operatorIndex, CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100775 }
776
777 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100778 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100779 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100780 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100781 }
telsoa01c577f2c2018-08-31 09:22:23 +0100782
Colm Donelan6350d272020-06-09 16:56:25 +0100783 SetupInputLayers(subgraphIndex);
784 SetupOutputLayers(subgraphIndex);
785 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100786
Colm Donelan6350d272020-06-09 16:56:25 +0100787 ++subgraphIndex;
788 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100789 }
telsoa01c577f2c2018-08-31 09:22:23 +0100790 }
Colm Donelan6350d272020-06-09 16:56:25 +0100791 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100792 {
Colm Donelan6350d272020-06-09 16:56:25 +0100793 std::stringstream errorString;
794 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
795 << subgraphIndex << " error: " << e.what();
796 ARMNN_LOG(error) << errorString.str();
797 std::stringstream errors;
798 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100799 throw ParseException(errors.str());
800 }
801
802 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100803 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100804 {
805 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
806 {
807 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
808 {
809 for (size_t inputSlotIdx = 0;
810 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
811 ++inputSlotIdx)
812 {
813 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
814 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
815 }
816 }
817 }
818 }
819
820 return std::move(m_Network);
821}
822
Kevin May7d96b162021-02-03 17:38:41 +0000823void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
824 size_t tensorIndex,
825 armnn::IOutputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100826{
827 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100828 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
829 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100830
831 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
832
833 // assuming there is only one producer for that tensor
834 if (tensorSlots.outputSlot != nullptr)
835 {
James Ward58dec6b2020-09-11 17:32:44 +0100836 throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
837 "subgraph:{} tensor:{} {}",
838 subgraphIndex,
839 tensorIndex,
840 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100841 }
842
843 tensorSlots.outputSlot = slot;
844}
845
Kevin May7d96b162021-02-03 17:38:41 +0000846void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
847 size_t tensorIndex,
848 armnn::IInputSlot* slot)
telsoa01c577f2c2018-08-31 09:22:23 +0100849{
850 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100851 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
852 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100853
Finn Williamsd4fa5452021-03-01 12:31:41 +0000854 TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +0100855 tensorSlots.inputSlots.push_back(slot);
856}
857
Kevin May7d96b162021-02-03 17:38:41 +0000858void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100859{
860 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
861
862 // NOTE: By default we presume the custom operator is not supported
Kevin May7d96b162021-02-03 17:38:41 +0000863 auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100864
865 // Identify custom code defined for custom operator
866 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
867 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
868
869 // Find parser function that correspondes to custom code (if any)
870 auto iterator = m_CustomParserFunctions.find(customCode);
871 if (iterator != m_CustomParserFunctions.end())
872 {
873 customParserFunction = iterator->second;
874 }
875
876 // Run parser function
877 (this->*customParserFunction)(subgraphIndex, operatorIndex);
878}
879
Kevin May7d96b162021-02-03 17:38:41 +0000880void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100881{
882 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100883
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100884 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
885
886 auto opcodeIndex = operatorPtr->opcode_index;
887 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
888
889 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
890 {
891 // Do not add StandInLayer, throw ParseException instead
892 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +0100893 fmt::format("Operator not supported. "
894 "subgraph:{} operator:{} "
895 "opcode_index:{} opcode:{} / {} {}",
896 subgraphIndex,
897 operatorIndex,
898 opcodeIndex,
899 opcode,
900 tflite::EnumNameBuiltinOperator(opcode),
901 CHECK_LOCATION().AsString()));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100902 }
903
904 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
905 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
906
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100907 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
908 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100909
910 StandInDescriptor descriptor(numInputs, numOutputs);
James Ward58dec6b2020-09-11 17:32:44 +0100911 auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100912
913 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
914 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +0100915 ARMNN_ASSERT(layer != nullptr);
916
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100917 for (unsigned int i = 0u; i < numOutputs; ++i)
918 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100919 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100920 }
921
922 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
923 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
924
925 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
926 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +0100927}
928
mathad01b392e982021-04-07 12:07:30 +0100929void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
930{
931 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
932
933 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
934 CHECK_VALID_SIZE(inputs.size(), 1);
935 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
936 CHECK_VALID_SIZE(outputs.size(), 1);
937
938 auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
939
940 IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
941 ARMNN_ASSERT(layer != nullptr);
942
943 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
944 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
945
946 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
947 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
948
949 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
950 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
951}
952
Kevin May7d96b162021-02-03 17:38:41 +0000953void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100954{
955 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
956
957 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
958 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
959
960 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
961
962 Convolution2dDescriptor desc;
963 desc.m_BiasEnabled = false;
964 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
965 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000966 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100967 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
968 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000969
telsoa01c577f2c2018-08-31 09:22:23 +0100970 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
971 CHECK_VALID_SIZE(inputs.size(), 2, 3);
972
973 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
974 CHECK_VALID_SIZE(outputs.size(), 1);
975
976 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
977 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
978
979 // assuming input is NHWC
980 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
981 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
982
983 // assuming the filter is OHWI : Output, H, W, Input
984 // which is essentially the same as NHWC
985 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
986 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
987
Pablo Tellof0bd6832019-04-26 17:58:13 +0100988 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
989 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
990 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
991 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +0100992
Finn Williamsd4fa5452021-03-01 12:31:41 +0000993 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +0100994 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100995
James Ward58dec6b2020-09-11 17:32:44 +0100996 auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100997
998 if (inputs.size() == 3)
999 {
1000 desc.m_BiasEnabled = true;
1001 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00001002 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001003 layer = m_Network->AddConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001004 filterTensorAndData,
1005 Optional<ConstTensor>(biasTensorAndData),
telsoa01c577f2c2018-08-31 09:22:23 +01001006 layerName.c_str());
1007 }
1008 else
1009 {
1010 layer = m_Network->AddConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001011 filterTensorAndData,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001012 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001013 layerName.c_str());
1014 }
1015
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001016 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001017
Sadik Armagand109a4d2020-07-28 10:42:13 +01001018 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001019 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001020
1021 // register the input connection slots for the layer, connections are made after all layers have been created
1022 // only the tensors for the inputs are relevant, exclude the const tensors
1023 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001024 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +01001025
jimfly01c25411c2018-11-14 17:47:22 +00001026 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001027 // register the output connection slots for the layer, connections are made after all layers have been created
1028 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1029 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1030}
1031
Kevin May7d96b162021-02-03 17:38:41 +00001032void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001033{
1034 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1035
1036 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1037 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1038
1039 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1040
1041 DepthwiseConvolution2dDescriptor desc;
1042 desc.m_BiasEnabled = false;
1043 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1044 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +00001045 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +01001046 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +01001047
1048 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1049 CHECK_VALID_SIZE(inputs.size(), 2, 3);
1050 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1051 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +01001052 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1053 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +00001054
telsoa01c577f2c2018-08-31 09:22:23 +01001055 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Jan Eilers7612bd62021-04-06 17:29:03 +01001056 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
telsoa01c577f2c2018-08-31 09:22:23 +01001057
Matteo Martincigh747ef822018-12-18 09:26:39 +00001058 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001059 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1060 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001061
1062 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001063 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1064 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1065
Pablo Tellof0bd6832019-04-26 17:58:13 +01001066 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1067 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1068 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1069 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001070
Jan Eilers53ef7952021-06-02 12:01:25 +01001071 // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
1072 auto filterTensor = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001073 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001074 auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001075
1076 if (inputs.size() == 3)
1077 {
1078 desc.m_BiasEnabled = true;
1079 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00001080 auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001081 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
Jan Eilers53ef7952021-06-02 12:01:25 +01001082 filterTensor,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001083 Optional<ConstTensor>(biasTensorAndData),
telsoa01c577f2c2018-08-31 09:22:23 +01001084 layerName.c_str());
1085 }
1086 else
1087 {
1088 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
Jan Eilers53ef7952021-06-02 12:01:25 +01001089 filterTensor,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001090 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001091 layerName.c_str());
1092 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001093 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001094
Sadik Armagand109a4d2020-07-28 10:42:13 +01001095 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001096 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001097
1098 // register the input connection slots for the layer, connections are made after all layers have been created
1099 // only the tensors for the inputs are relevant, exclude the const tensors
1100 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001101 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +01001102
jimfly01c25411c2018-11-14 17:47:22 +00001103 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001104 // register the output connection slots for the layer, connections are made after all layers have been created
1105 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1106 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1107}
1108
Kevin May7d96b162021-02-03 17:38:41 +00001109void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsed66d142019-12-06 09:55:55 +00001110{
1111 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1112
1113 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1114 CHECK_VALID_SIZE(inputs.size(), 1);
1115
1116 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1117 CHECK_VALID_SIZE(outputs.size(), 1);
1118
James Ward58dec6b2020-09-11 17:32:44 +01001119 auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsed66d142019-12-06 09:55:55 +00001120
1121 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001122 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001123
Sadik Armagand109a4d2020-07-28 10:42:13 +01001124 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001125 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1126
1127 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1128 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1129
1130 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1131 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1132}
1133
Teresa Charlin3ab85482021-06-08 16:59:29 +01001134void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1135{
1136 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1137
1138 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1139 CHECK_VALID_SIZE(inputs.size(), 2);
1140
1141 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1142 CHECK_VALID_SIZE(outputs.size(), 1);
1143
1144 auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1145
1146 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1147 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1148
1149 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1150
1151 ReshapeDescriptor reshapeDesc;
Finn Williamsb49ed182021-06-29 15:50:08 +01001152
1153 if (outputTensorInfo.GetShape().AreAllDimensionsSpecified())
1154 {
1155 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1156 }
1157 else
1158 {
1159 int32_t axis = inputs[1]->shape[0];
1160
1161 int32_t inputDimSize = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1162
1163 if (axis > inputDimSize || axis < 0 - (inputDimSize + 1))
1164 {
1165 throw ParseException("axis must be in range [0 - (inputDimSize + 1), inputDimSize] inclusive");
1166 }
1167
1168 if(axis < 0)
1169 {
1170 axis = inputDimSize + axis + 1;
1171 }
1172
Rob Hughesd812a312021-08-06 13:10:53 +01001173 std::vector<unsigned int> shape(static_cast<unsigned int>(inputDimSize) + 1);
Finn Williamsb49ed182021-06-29 15:50:08 +01001174 unsigned int inputShapeIndex = 0;
1175 for (unsigned int i = 0; i < static_cast<unsigned int>(inputDimSize + 1); ++i)
1176 {
1177 if (i == static_cast<unsigned int>(axis))
1178 {
1179 shape[i] = 1;
1180 }
1181 else
1182 {
1183 shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1184 ++inputShapeIndex;
1185 }
1186 }
1187
Rob Hughesd812a312021-08-06 13:10:53 +01001188 reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(inputDimSize + 1), shape.data());
Finn Williamsb49ed182021-06-29 15:50:08 +01001189 }
Teresa Charlin3ab85482021-06-08 16:59:29 +01001190
1191 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1192 ARMNN_ASSERT(layer != nullptr);
1193 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1194
1195 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1196 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1197
1198 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1199 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1200}
1201
Kevin May7d96b162021-02-03 17:38:41 +00001202void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
Keith Davis4cd29a02019-09-09 14:49:20 +01001203{
1204 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1205
1206 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001207 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001208
1209 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1210 CHECK_VALID_SIZE(outputs.size(), 1);
1211
James Ward58dec6b2020-09-11 17:32:44 +01001212 auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001213 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001214
josh minorba424d22019-11-13 10:55:17 -06001215 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001216 {
1217 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1218 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001219 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1220 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001221 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001222 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001223
Mike Kelly08759e22020-03-02 11:41:31 +00001224 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001225 }
1226
James Conroy05102392020-06-24 15:39:55 +01001227 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001228 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001229 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001230
James Conroy05102392020-06-24 15:39:55 +01001231 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001232 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001233 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1234
1235 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1236 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1237
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::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001243{
1244 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1245
1246 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1247 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1248
1249 TransposeConvolution2dDescriptor desc;
1250 desc.m_BiasEnabled = false;
1251 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1252 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1253 desc.m_DataLayout = armnn::DataLayout::NHWC;
1254
1255 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
David Monahan61683802021-01-12 09:11:07 +00001256 if (inputs.size() == 4)
1257 {
1258 desc.m_BiasEnabled = true;
1259 }
1260 else
1261 {
1262 CHECK_VALID_SIZE(inputs.size(), 3);
1263 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001264
1265 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1266 CHECK_VALID_SIZE(outputs.size(), 1);
1267
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001268 if (inputs[0])
1269 {
1270 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1271 std::vector<int> output_shape(tensorInfo.GetNumElements());
1272 if (tensorInfo.GetDataType() == DataType::Signed32)
1273 {
1274 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1275 }
1276 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1277 {
1278 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1279 {
1280 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1281 }
1282 }
1283 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1284 for (int dimension : output_shape)
1285 {
1286 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1287 }
1288 desc.m_OutputShapeEnabled = true;
1289 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001290 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001291 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1292
1293 // TfLite uses NHWC tensors
1294 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1295 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1296
1297 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1298 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1299
1300 CalcPadding(inputHeight,
1301 filterHeight,
1302 desc.m_StrideY,
1303 1, // DilationY
1304 desc.m_PadTop,
1305 desc.m_PadBottom,
1306 options->padding);
1307
1308 CalcPadding(inputWidth,
1309 filterWidth,
1310 desc.m_StrideX,
1311 1, // DilationX
1312 desc.m_PadLeft,
1313 desc.m_PadRight,
1314 options->padding);
1315
Finn Williamsd4fa5452021-03-01 12:31:41 +00001316 auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001317
1318 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01001319 auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001320
David Monahan61683802021-01-12 09:11:07 +00001321 if (desc.m_BiasEnabled)
1322 {
1323 auto biasTensorInfo = ToTensorInfo(inputs[3]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00001324 auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo);
David Monahan61683802021-01-12 09:11:07 +00001325 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001326 filterTensorAndData,
1327 biasConstTensor,
David Monahan61683802021-01-12 09:11:07 +00001328 layerName.c_str());
1329 }
1330 else
1331 {
1332 layer = m_Network->AddTransposeConvolution2dLayer(desc,
Finn Williamsd4fa5452021-03-01 12:31:41 +00001333 filterTensorAndData,
David Monahan61683802021-01-12 09:11:07 +00001334 EmptyOptional(),
1335 layerName.c_str());
1336 }
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001337
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001338 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001339
Sadik Armagand109a4d2020-07-28 10:42:13 +01001340 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001341 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1342
1343 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1344 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001345 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001346
1347 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1348 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1349}
1350
Kevin May7d96b162021-02-03 17:38:41 +00001351void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001352{
1353 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1354}
1355
Kevin May7d96b162021-02-03 17:38:41 +00001356void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001357{
1358 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1359
1360 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1361 CHECK_VALID_SIZE(inputs.size(), 3);
1362
1363 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1364 CHECK_VALID_SIZE(outputs.size(), 1);
1365
1366 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1367 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1368
1369 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1370 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1371
1372 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1373 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1374
1375 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1376 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1377
1378 size_t step = 2;
1379 std::vector<std::pair<unsigned int, unsigned int>> crops;
1380 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1381 {
1382 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1383 }
1384
1385 armnn::BatchToSpaceNdDescriptor desc;
1386 desc.m_BlockShape = blockShape;
1387 desc.m_Crops = crops;
1388 desc.m_DataLayout = armnn::DataLayout::NHWC;
1389
James Ward58dec6b2020-09-11 17:32:44 +01001390 auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001391
James Conroy05102392020-06-24 15:39:55 +01001392 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001393 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001394 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1395
1396 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1397 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001398 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1399
1400 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1401 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1402
1403 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1404 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1405}
1406
Kevin May7d96b162021-02-03 17:38:41 +00001407void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
Matthew Jackson28c94572019-07-18 10:47:03 +01001408{
1409 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1410
1411 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1412 CHECK_VALID_SIZE(inputs.size(), 1);
1413
1414 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1415 CHECK_VALID_SIZE(outputs.size(), 1);
1416
1417 L2NormalizationDescriptor desc;
1418 desc.m_DataLayout = armnn::DataLayout::NHWC;
James Ward58dec6b2020-09-11 17:32:44 +01001419 auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
Matthew Jackson28c94572019-07-18 10:47:03 +01001420 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1421
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001422 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001423
Sadik Armagand109a4d2020-07-28 10:42:13 +01001424 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001425 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1426
1427 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1428 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1429
1430 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1431 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1432}
1433
Kevin May7d96b162021-02-03 17:38:41 +00001434void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001435{
1436 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1437}
1438
Kevin May7d96b162021-02-03 17:38:41 +00001439void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001440{
1441 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1442
1443 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1444 CHECK_VALID_SIZE(inputs.size(), 2);
1445
1446 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1447 CHECK_VALID_SIZE(outputs.size(), 1);
1448
James Ward58dec6b2020-09-11 17:32:44 +01001449 auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001450
1451 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1452 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1453 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001454
Sadik Armagand109a4d2020-07-28 10:42:13 +01001455 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001456 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1457
1458 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1459 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001460 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1461
1462 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001463 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001464
1465 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1466 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1467}
1468
Kevin May7d96b162021-02-03 17:38:41 +00001469void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001470{
1471 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1472
1473 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1474 CHECK_VALID_SIZE(inputs.size(), 2);
1475
1476 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1477 CHECK_VALID_SIZE(outputs.size(), 1);
1478
James Ward58dec6b2020-09-11 17:32:44 +01001479 auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001480
1481 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1482 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1483 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001484
Sadik Armagand109a4d2020-07-28 10:42:13 +01001485 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001486 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1487
1488 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1489 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001490 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1491
1492 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001493 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001494
1495 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1496 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1497}
1498
Kevin May7d96b162021-02-03 17:38:41 +00001499void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1500 size_t operatorIndex,
1501 PoolingAlgorithm algorithm)
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001502{
1503 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1504
1505 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1506 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1507
1508 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1509
1510 std::string layerName;
1511
1512 switch (algorithm)
1513 {
1514 case PoolingAlgorithm::Average:
1515 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001516 fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001517 break;
1518 case PoolingAlgorithm::Max:
1519 layerName =
James Ward58dec6b2020-09-11 17:32:44 +01001520 fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001521 break;
1522 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001523 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001524 }
1525
1526 Pooling2dDescriptor desc;
1527
1528 desc.m_PoolType = algorithm;
1529 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1530 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1531 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1532 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1533 desc.m_PaddingMethod = PaddingMethod::Exclude;
1534 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001535 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001536
1537 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1538 CHECK_VALID_SIZE(inputs.size(), 1);
1539 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1540
1541 // assuming input is NHWC
1542 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1543 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1544
Pablo Tellof0bd6832019-04-26 17:58:13 +01001545 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1546 desc.m_PadTop, desc.m_PadBottom, options->padding);
1547 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1548 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001549
1550 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1551 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001552
Sadik Armagand109a4d2020-07-28 10:42:13 +01001553 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001554 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1555
1556 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1557 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001558 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001559
1560 // register the input connection slots for the layer, connections are made after all layers have been created
1561 // only the tensors for the inputs are relevant, exclude the const tensors
1562 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001563 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001564
jimfly01c25411c2018-11-14 17:47:22 +00001565 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001566 // register the output connection slots for the layer, connections are made after all layers have been created
1567 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1568 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1569}
1570
Kevin May7d96b162021-02-03 17:38:41 +00001571void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
josh minorba424d22019-11-13 10:55:17 -06001572{
1573 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1574
1575 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1576 CHECK_VALID_SIZE(inputs.size(), 3);
1577 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1578 CHECK_VALID_SIZE(outputs.size(), 1);
1579
1580 SliceDescriptor desc;
1581
1582 // set begin tensor info for slice descriptor
1583 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1584 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1585
1586 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1587 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1588
1589 // set size tensor info for slice descriptor
1590 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1591 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1592
1593 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1594 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1595 desc = SliceDescriptor(begin, size);
1596
James Ward58dec6b2020-09-11 17:32:44 +01001597 auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001598
James Conroy05102392020-06-24 15:39:55 +01001599 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001600 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001601 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1602
1603 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001604 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1605
1606 // register the input connection slots for the layer, connections are made after all layers have been created
1607 // only the tensors for the inputs are relevant, exclude the const tensors
1608 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1609 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1610
1611 // register the output connection slots for the layer, connections are made after all layers have been created
1612 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1613 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1614}
1615
Kevin May7d96b162021-02-03 17:38:41 +00001616void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001617{
1618 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1619 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1620 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1621
1622 SoftmaxDescriptor desc;
1623 desc.m_Beta = options->beta;
1624
1625 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1626 CHECK_VALID_SIZE(inputs.size(), 1);
1627 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1628 CHECK_VALID_SIZE(outputs.size(), 1);
1629
James Ward58dec6b2020-09-11 17:32:44 +01001630 auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001631 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1632
Sadik Armagand109a4d2020-07-28 10:42:13 +01001633 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001634 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1635
1636 // register the input connection slots for the layer, connections are made after all layers have been created
1637 // only the tensors for the inputs are relevant, exclude the const tensors
1638 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1639 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1640
1641 // register the output connection slots for the layer, connections are made after all layers have been created
1642 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1643 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1644}
1645
Kevin May7d96b162021-02-03 17:38:41 +00001646void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001647{
1648 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1649
1650 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1651 CHECK_VALID_SIZE(inputs.size(), 3);
1652
1653 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1654 CHECK_VALID_SIZE(outputs.size(), 1);
1655
1656 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1657 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1658
1659 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1660 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1661
1662 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1663 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1664
1665 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1666 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1667
1668 size_t step = 2;
1669 std::vector<std::pair<unsigned int, unsigned int>> padList;
1670 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1671 {
1672 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1673 }
1674
1675 armnn::SpaceToBatchNdDescriptor desc;
1676 desc.m_BlockShape = blockShape;
1677 desc.m_PadList = padList;
1678 desc.m_DataLayout = armnn::DataLayout::NHWC;
1679
James Ward58dec6b2020-09-11 17:32:44 +01001680 auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001681
James Conroy05102392020-06-24 15:39:55 +01001682 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001683 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001684 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1685
1686 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1687 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001688 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1689
1690 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1691 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1692
1693 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1694 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1695}
1696
Teresa Charlin3ab85482021-06-08 16:59:29 +01001697armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(std::vector<uint32_t> squeezeDims,
Kevin May7d96b162021-02-03 17:38:41 +00001698 const armnn::TensorInfo & inputTensorInfo)
telsoa01c577f2c2018-08-31 09:22:23 +01001699{
Teresa Charlin3ab85482021-06-08 16:59:29 +01001700 CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
telsoa01c577f2c2018-08-31 09:22:23 +01001701 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1702
1703 if (inputTensorInfo.GetNumDimensions() > 4)
1704 {
1705 std::stringstream ss;
1706 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1707 << " shape:" << inputTensorInfo.GetShape() << " "
1708 << CHECK_LOCATION().AsString();
1709 throw ParseException(ss.str());
1710 }
1711
1712 if (squeezeDims.empty())
1713 {
1714 squeezeDims.assign(dimensionSequence,
1715 dimensionSequence+inputTensorInfo.GetNumDimensions());
1716 }
1717
1718 std::vector<uint32_t> outputDims;
1719 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1720 {
1721 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1722 auto currentDimension = inputTensorInfo.GetShape()[i];
1723 if (skipSqueeze || currentDimension != 1)
1724 {
1725 outputDims.push_back(currentDimension);
1726 }
1727 }
1728
1729 if (outputDims.size() > 4)
1730 {
1731 std::stringstream ss;
1732 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1733 << " shape:" << inputTensorInfo.GetShape() << " "
1734 << CHECK_LOCATION().AsString();
1735 throw ParseException(ss.str());
1736 }
1737
1738 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1739 outputDims.data());
1740
1741 // we need to preserve the tensor type and the quantization data as well
1742 TensorInfo outTensorInfo = inputTensorInfo;
1743 outTensorInfo.SetShape(outShape);
1744
1745 return outTensorInfo;
1746}
1747
Keith Davis0176fd82021-06-01 17:36:32 +01001748void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
1749{
1750 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1751
1752 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1753 CHECK_VALID_SIZE(inputs.size(), 1);
1754 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1755 CHECK_VALID_SIZE(outputs.size(), 1);
1756
1757 auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
1758
1759 IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
1760 ARMNN_ASSERT(layer != nullptr);
1761
1762
1763 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1764 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1765
1766 // Check if output tensor type is Signed32 or Signed64
1767 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
1768 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
1769 {
1770 throw ParseException(
1771 fmt::format(
1772 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
1773 CHECK_LOCATION().AsString()));
1774 }
1775
1776 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1777 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1778
1779 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1780 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1781}
1782
Kevin May7d96b162021-02-03 17:38:41 +00001783void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01001784{
1785 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1786
1787 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1788 CHECK_VALID_SIZE(inputs.size(), 1);
1789
1790 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1791 CHECK_VALID_SIZE(outputs.size(), 1);
1792
1793 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1794 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01001795 auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001796
1797 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Teresa Charlin3ab85482021-06-08 16:59:29 +01001798
1799 std::vector<uint32_t> squeezeDim;
1800 // A single negative dim index is interpreted as a negative index in python
1801 // Meaning the index will be the shape size plus the negative index value
1802 if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
1803 {
1804 int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
1805 squeezeDim.push_back(static_cast<uint32_t>(dim));
1806 }
1807 else
1808 {
1809 squeezeDim = AsUnsignedVector(options->squeeze_dims);
1810 }
1811
1812 armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
1813
James Conroy05102392020-06-24 15:39:55 +01001814 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01001815
1816 ReshapeDescriptor reshapeDesc;
1817 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1818
telsoa01c577f2c2018-08-31 09:22:23 +01001819 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001820 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001821 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1822
1823 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1824 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1825
1826 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1827 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1828}
1829
Kevin May7d96b162021-02-03 17:38:41 +00001830void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001831{
1832 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1833
1834 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1835 CHECK_VALID_SIZE(inputs.size(), 4);
1836
1837 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1838 CHECK_VALID_SIZE(outputs.size(), 1);
1839
1840 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1841 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1842
1843 StridedSliceDescriptor desc;
1844 desc.m_BeginMask = options->begin_mask;
1845 desc.m_EllipsisMask = options->ellipsis_mask;
1846 desc.m_EndMask = options->end_mask;
1847 desc.m_NewAxisMask = options->new_axis_mask;
1848 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
1849 desc.m_DataLayout = armnn::DataLayout::NHWC;
1850
1851 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1852 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1853
1854 std::vector<int> begin(beginTensorInfo.GetNumElements());
1855 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1856
1857 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
1858 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1859
1860 std::vector<int> end(endTensorInfo.GetNumElements());
1861 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1862
1863 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
1864 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
1865
1866 std::vector<int> stride(strideTensorInfo.GetNumElements());
1867 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1868
1869 desc.m_Begin = begin;
1870 desc.m_End = end;
1871 desc.m_Stride = stride;
1872
James Ward58dec6b2020-09-11 17:32:44 +01001873 auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001874 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001875 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001876
Sadik Armagand109a4d2020-07-28 10:42:13 +01001877 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001878 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1879
1880 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1881 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1882
1883 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1884 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1885}
1886
Kevin May7d96b162021-02-03 17:38:41 +00001887void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001888{
1889 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1890
1891 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1892 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1893
1894 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1895 CHECK_VALID_SIZE(inputs.size(), 2);
1896
1897 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1898 CHECK_VALID_SIZE(outputs.size(), 1);
1899
1900 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1901 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1902
James Ward58dec6b2020-09-11 17:32:44 +01001903 auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001904 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001905 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001906
Sadik Armagand109a4d2020-07-28 10:42:13 +01001907 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001908 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1909
1910 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001911 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001912
1913 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1914
1915 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1916 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1917}
1918
Kevin May7d96b162021-02-03 17:38:41 +00001919void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301920{
1921 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1922
1923 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1924 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1925
1926 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1927 CHECK_VALID_SIZE(inputs.size(), 2);
1928
1929 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1930 CHECK_VALID_SIZE(outputs.size(), 1);
1931
1932 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1933 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1934
James Ward58dec6b2020-09-11 17:32:44 +01001935 auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301936 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001937 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301938
Sadik Armagand109a4d2020-07-28 10:42:13 +01001939 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301940 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1941
1942 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001943 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301944 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1945
1946 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1947 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1948}
1949
Kevin May7d96b162021-02-03 17:38:41 +00001950void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001951{
1952 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1953
1954 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1955 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1956
1957 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1958 CHECK_VALID_SIZE(inputs.size(), 2);
1959
1960 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1961 CHECK_VALID_SIZE(outputs.size(), 1);
1962
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001963 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1964 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1965
James Ward58dec6b2020-09-11 17:32:44 +01001966 auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001967 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001968 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001969
Sadik Armagand109a4d2020-07-28 10:42:13 +01001970 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001971 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1972
1973 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001974 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001975 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1976
1977 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1978 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1979}
1980
Kevin May7d96b162021-02-03 17:38:41 +00001981void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001982{
1983 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1984
1985 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1986 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1987
1988 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1989 CHECK_VALID_SIZE(inputs.size(), 2);
1990
1991 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1992 CHECK_VALID_SIZE(outputs.size(), 1);
1993
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001994 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1995 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1996
James Ward58dec6b2020-09-11 17:32:44 +01001997 auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001998 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001999 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002000
Sadik Armagand109a4d2020-07-28 10:42:13 +01002001 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002002 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2003
2004 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01002005 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02002006 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2007
2008 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2009 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2010}
2011
Kevin May7d96b162021-02-03 17:38:41 +00002012void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002013{
2014 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2015
2016 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2017
2018 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2019 CHECK_VALID_SIZE(outputs.size(), 1);
2020
2021 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
2022 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2023
2024 armnn::MeanDescriptor desc;
2025 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
2026 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2027 desc.m_Axis = axis;
2028
2029 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002030 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002031
2032 desc.m_KeepDims =
2033 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
2034 true : false;
2035
James Ward58dec6b2020-09-11 17:32:44 +01002036 auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002037 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002038 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02002039
2040 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2041
2042 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2043 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2044
2045 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2046 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2047}
2048
Kevin May7d96b162021-02-03 17:38:41 +00002049void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002050{
2051 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2052
Kevin May7d96b162021-02-03 17:38:41 +00002053 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002054
Kevin May7d96b162021-02-03 17:38:41 +00002055 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002056 CHECK_VALID_SIZE(outputs.size(), 1);
2057
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002058 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2059
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002060 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
2061 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2062
2063 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2064 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2065
2066 size_t step = 2;
2067 armnn::PadDescriptor desc;
Narumol Prangnawarat8719d222020-11-27 16:57:56 +00002068 if (inputTensorInfo.IsQuantized())
2069 {
2070 desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2071 }
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002072 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2073 {
2074 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2075 }
2076
James Ward58dec6b2020-09-11 17:32:44 +01002077 auto layerName = fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002078 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002079
2080 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2081 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02002082 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2083
2084 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2085 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2086
2087 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2088 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2089}
2090
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002091void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2092{
2093 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2094
2095 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2096 CHECK_VALID_SIZE(inputs.size(), 2);
2097
2098 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2099 CHECK_VALID_SIZE(outputs.size(), 1);
2100
2101 auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2102
2103 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2104 armnn::TensorInfo alphaTensorInfo = ToTensorInfo(inputs[1]);
2105 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2106 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2107
2108 IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2109 ARMNN_ASSERT(layer != nullptr);
2110 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2111
2112 if (IsConstTensor(inputs[1]))
2113 {
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002114 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawaratbf99b5f2021-05-27 09:55:43 +01002115 armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
2116 RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
Narumol Prangnawaratbfaee6b2021-05-24 18:50:24 +01002117
2118 auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo);
2119 std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
2120 IConnectableLayer* constLayer =
2121 m_Network->AddConstantLayer(alphaTensorAndData, constLayerName.c_str());
2122 ARMNN_ASSERT(constLayer != nullptr);
2123
2124 constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
2125 constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
2126 RegisterOutputSlots(subgraphIndex,
2127 VIRTUAL_OPERATOR_ID,
2128 constLayer,
2129 { inputTensorIndexes[1] });
2130 }
2131 else
2132 {
2133 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2134 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
2135 }
2136
2137 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2138 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2139}
2140
Kevin May7d96b162021-02-03 17:38:41 +00002141void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan66dedc72019-12-10 16:32:07 +00002142{
2143 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2144
2145 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2146 CHECK_VALID_SIZE(inputs.size(), 1);
2147
2148 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2149 CHECK_VALID_SIZE(outputs.size(), 1);
2150
James Ward58dec6b2020-09-11 17:32:44 +01002151 auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002152
2153 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002154 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002155
Sadik Armagand109a4d2020-07-28 10:42:13 +01002156 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002157 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2158
2159 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2160 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2161
2162 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2163 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2164}
Finn Williamsc42c3842019-01-22 14:18:11 +00002165
Kevin May7d96b162021-02-03 17:38:41 +00002166void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002167{
Finn Williamsc42c3842019-01-22 14:18:11 +00002168 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002169}
2170
Kevin May7d96b162021-02-03 17:38:41 +00002171void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan58f39192018-09-17 14:14:39 +01002172{
Finn Williamsc42c3842019-01-22 14:18:11 +00002173 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2174}
Sadik Armagan58f39192018-09-17 14:14:39 +01002175
Kevin May7d96b162021-02-03 17:38:41 +00002176void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan12239e72020-05-27 11:06:17 +01002177{
Jan Eilers2f746b32020-07-28 14:00:06 +01002178 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002179}
2180
Kevin May7d96b162021-02-03 17:38:41 +00002181void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
Finn Williamsc42c3842019-01-22 14:18:11 +00002182{
2183 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2184}
2185
Kevin May7d96b162021-02-03 17:38:41 +00002186void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd99851762019-04-09 09:37:38 +01002187{
2188 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2189}
2190
Kevin May7d96b162021-02-03 17:38:41 +00002191void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
Matthew Sloyan7515d072020-12-16 12:50:01 +00002192{
2193 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
2194}
2195
Kevin May7d96b162021-02-03 17:38:41 +00002196void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
Jan Eilers2f746b32020-07-28 14:00:06 +01002197{
2198 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2199}
Finn Williamsc42c3842019-01-22 14:18:11 +00002200
Kevin May7d96b162021-02-03 17:38:41 +00002201void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
Finn Williamsc42c3842019-01-22 14:18:11 +00002202{
2203 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan58f39192018-09-17 14:14:39 +01002204 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002205 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002206
2207 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2208 CHECK_VALID_SIZE(inputs.size(), 1);
2209
2210 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2211 CHECK_VALID_SIZE(outputs.size(), 1);
2212
James Ward58dec6b2020-09-11 17:32:44 +01002213 auto layerName = fmt::format("Activation:");
Sadik Armagan58f39192018-09-17 14:14:39 +01002214 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002215 activationDesc.m_Function = activationType;
2216
2217 switch (activationType)
2218 {
2219 case ActivationFunction::ReLu:
2220 {
James Ward58dec6b2020-09-11 17:32:44 +01002221 layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002222 break;
2223 }
2224 case ActivationFunction::BoundedReLu:
2225 {
James Ward58dec6b2020-09-11 17:32:44 +01002226 layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002227 activationDesc.m_A = 6.0f;
2228 activationDesc.m_B = 0.0f;
2229 break;
2230 }
2231 case ActivationFunction::Sigmoid:
2232 {
James Ward58dec6b2020-09-11 17:32:44 +01002233 layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsc42c3842019-01-22 14:18:11 +00002234 break;
2235 }
Nina Drozd99851762019-04-09 09:37:38 +01002236 case ActivationFunction::TanH:
2237 {
James Ward58dec6b2020-09-11 17:32:44 +01002238 layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd99851762019-04-09 09:37:38 +01002239 activationDesc.m_A = 1.0f;
2240 activationDesc.m_B = 1.0f;
2241 break;
2242 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002243 case ActivationFunction::LeakyReLu:
2244 {
James Ward58dec6b2020-09-11 17:32:44 +01002245 layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan12239e72020-05-27 11:06:17 +01002246 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2247 activationDesc.m_A = options->alpha;
2248 break;
2249 }
Matthew Sloyan7515d072020-12-16 12:50:01 +00002250 case ActivationFunction::Elu:
2251 {
2252 layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2253 activationDesc.m_A = 1.0f;
2254 break;
2255 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002256 case ActivationFunction::HardSwish:
Matthew Sloyan7515d072020-12-16 12:50:01 +00002257 {
James Ward58dec6b2020-09-11 17:32:44 +01002258 layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
Jan Eilers2f746b32020-07-28 14:00:06 +01002259 break;
Matthew Sloyan7515d072020-12-16 12:50:01 +00002260 }
Finn Williamsc42c3842019-01-22 14:18:11 +00002261 default:
2262 {
2263 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002264 fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2265 static_cast<int>(activationType), CHECK_LOCATION().AsString()));
Finn Williamsc42c3842019-01-22 14:18:11 +00002266 }
2267 }
2268
2269 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002270
Sadik Armagand109a4d2020-07-28 10:42:13 +01002271 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002272 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2273
2274 // register the input connection slots for the layer, connections are made after all layers have been created
2275 // only the tensors for the inputs are relevant, exclude the const tensors
2276 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2277 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2278
2279 // register the output connection slots for the layer, connections are made after all layers have been created
2280 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2281 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2282}
Kevin May7d96b162021-02-03 17:38:41 +00002283armnn::TensorInfo TfLiteParserImpl::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
2284 const std::vector<int32_t> & targetDimsIn)
Sadikb94967b2018-09-19 15:30:00 +01002285{
2286 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2287 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2288
2289 if (stretchDim != targetDimsIn.end())
2290 {
2291 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2292 {
2293 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002294 fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
Sadikb94967b2018-09-19 15:30:00 +01002295 }
2296
2297 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002298 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002299 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2300
2301 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2302 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2303 }
2304
2305 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2306
2307 TensorInfo reshapeInfo = inputTensorInfo;
2308 reshapeInfo.SetShape(outputShape);
2309
2310 return reshapeInfo;
2311}
2312
Kevin May7d96b162021-02-03 17:38:41 +00002313void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
Sadikb94967b2018-09-19 15:30:00 +01002314{
2315 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2316
2317 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002318
2319 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2320 CHECK_VALID_SIZE(outputs.size(), 1);
2321
2322 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2323 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
James Ward58dec6b2020-09-11 17:32:44 +01002324 auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002325
2326 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002327 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002328 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002329
Jan Eilersbac9b352020-07-13 13:40:24 +01002330 // Extracting new shape for the output
2331 // There are two ways it can be passed
2332 // * First is to define the target shape in the operator built-in options
2333 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002334 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002335 bool targetShapeFound = false;
2336 // Check if built-in options were given
2337 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002338 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002339 // make sure the parameter is given
2340 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002341 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002342 targetShape = options->new_shape;
2343 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002344 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002345 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002346
2347 // If there is no built-in option given or if the built-in new_shape parameter was empty
2348 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002349 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002350 // Check for a second input tensor
2351 if (inputs.size() > 1 && inputs[1] != nullptr)
2352 {
2353 if (inputs[1]->is_variable)
2354 {
2355 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2356 }
2357
2358 if (inputs[1]->shape.size() != 1)
2359 {
2360 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2361 }
2362
2363 if (inputs[1]->type != tflite::TensorType_INT32)
2364 {
2365 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2366 }
2367
2368 // Extract target shape from input
2369 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2370 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
Sadik Armagan19a1c032021-01-20 12:17:00 +00002371 if (!values)
2372 {
2373 ARMNN_THROW_PARSE_EXCEPTION("Reshape operator target shape input buffer data is null");
2374 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002375 for (int i=0; i < inputs[1]->shape[0]; ++i)
2376 {
2377 targetShape.push_back(values[i]);
2378 }
2379 }
2380 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002381 {
2382 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2383 "At least one method required");
2384 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002385 }
2386
kevmay0171972a82018-12-17 14:28:03 +00002387 armnn::TensorInfo reshapeOutputTensorInfo =
Kevin May7d96b162021-02-03 17:38:41 +00002388 TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002389
kevmay0171972a82018-12-17 14:28:03 +00002390 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002391 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2392 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002393 {
2394 std::stringstream ss;
2395 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002396 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002397 << " does not equal output shape "
2398 << actualOutputTensorInfo.GetShape()
2399 << ": "
2400 << CHECK_LOCATION().AsString();
2401 throw ParseException(ss.str());
2402 }
2403
Sadikb94967b2018-09-19 15:30:00 +01002404 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002405 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002406
Sadikb94967b2018-09-19 15:30:00 +01002407 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002408 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002409 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002410
2411 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2412 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2413
2414 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2415 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2416}
2417
Kevin May7d96b162021-02-03 17:38:41 +00002418void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002419{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002420 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2421}
2422
Kevin May7d96b162021-02-03 17:38:41 +00002423void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002424{
2425 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2426}
2427
Kevin May7d96b162021-02-03 17:38:41 +00002428void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
Sadik Armagana3b31f02019-12-05 09:08:53 +00002429{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002430 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2431
2432 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2433 CHECK_VALID_SIZE(inputs.size(), 2);
2434
2435 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2436 CHECK_VALID_SIZE(outputs.size(), 1);
2437
2438 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2439
2440 // Data for the parsed tensor args (size) must be stored locally.
2441 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2442
2443 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2444 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2445
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002446 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002447 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002448 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002449 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2450 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002451
James Ward58dec6b2020-09-11 17:32:44 +01002452 auto layerName = fmt::format("Resize:");
Sadik Armagana3b31f02019-12-05 09:08:53 +00002453
2454 switch (resizeMethod)
2455 {
2456 case ResizeMethod::Bilinear:
2457 {
James Ward58dec6b2020-09-11 17:32:44 +01002458 layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002459
2460 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2461 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2462
David Monahan4a0c9b92020-05-30 09:48:39 +01002463 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002464 break;
2465 }
2466 case ResizeMethod::NearestNeighbor:
2467 {
James Ward58dec6b2020-09-11 17:32:44 +01002468 layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagana3b31f02019-12-05 09:08:53 +00002469 break;
2470 }
2471 default:
2472 {
2473 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002474 fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2475 static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
Sadik Armagana3b31f02019-12-05 09:08:53 +00002476 }
2477 }
2478
James Conroy05102392020-06-24 15:39:55 +01002479 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002480 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002481 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2482
2483 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2484 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002485 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2486
2487 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2488 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2489
2490 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2491 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2492}
2493
Kevin May7d96b162021-02-03 17:38:41 +00002494void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan479045b2018-10-01 11:51:37 +01002495{
2496 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2497
2498 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2499 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2500
2501 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2502
2503 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2504 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2505 CHECK_VALID_SIZE(outputs.size(), 1);
2506
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002507 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2508 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002509
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002510 const unsigned int concatDimInput = static_cast<unsigned int>(
2511 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002512
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002513 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2514 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002515
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002516 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002517
2518 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2519 {
2520 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2521
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002522 // This set up concatDescriptor view origin
2523 armnnUtils::ProcessConcatInputTensorInfo(
2524 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002525 }
2526
James Ward58dec6b2020-09-11 17:32:44 +01002527 auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002528 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002529
Jim Flynn906f9462019-05-10 13:55:21 +01002530 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002531 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002532 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002533
James Conroy05102392020-06-24 15:39:55 +01002534 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002535 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002536
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002537 // add fused activation layer
2538 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002539
Sadik Armagan479045b2018-10-01 11:51:37 +01002540 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2541 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2542}
2543
Kevin May7d96b162021-02-03 17:38:41 +00002544void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002545{
2546 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2547
2548 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2549 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2550
2551 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2552
2553 FullyConnectedDescriptor desc;
2554 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002555 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002556
2557 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2558 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2559 CHECK_VALID_SIZE(outputs.size(), 1);
2560
2561 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2562
2563 // Fully Connected Layer accepts two dimensional weights input
2564 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2565 if (weightsDimension != 2)
2566 {
2567 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002568 fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2569 "Node {}",
2570 weightsDimension,
2571 CHECK_LOCATION().AsString()));
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002572 }
2573
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002574 armnn::IConnectableLayer* layer = nullptr;
James Ward58dec6b2020-09-11 17:32:44 +01002575 auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002576
Matthew Sloyan81beae32021-07-13 19:46:11 +01002577 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2578 // Add the first input tensor to the registration list
2579 std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2580 std::vector<unsigned int> ignoreInputWhenRegister = {};
Finn Williamsd4fa5452021-03-01 12:31:41 +00002581
2582 desc.m_ConstantWeights = IsConstTensor(inputs[1]);
2583
Matthew Sloyan81beae32021-07-13 19:46:11 +01002584 // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
2585 tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002586
Finn Williamsd4fa5452021-03-01 12:31:41 +00002587 if (inputs.size() == 3)
2588 {
2589 desc.m_BiasEnabled = true;
Matthew Sloyan81beae32021-07-13 19:46:11 +01002590
2591 // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
2592 tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002593 }
2594
Matthew Sloyan81beae32021-07-13 19:46:11 +01002595 // Filters and biases are always passed to fully connected as inputs
2596 layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
Finn Williamsd4fa5452021-03-01 12:31:41 +00002597
2598 ARMNN_ASSERT(layer != nullptr);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002599 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2600
Finn Williamsd4fa5452021-03-01 12:31:41 +00002601 unsigned int startingSlotIndex = 0;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002602 if (inputTensorInfo.GetNumDimensions() > 2)
2603 {
2604 // Add reshape to flatten to 2D [batch_size, input_size],
2605 // where "input_size" corresponds to the number of inputs to the layer,
2606 // matching the second dimension of weights,
2607 // and "batch_size" is calculated by dividing the number of elements by "input_size".
2608 std::vector<unsigned int> reshapedDimensions(2);
2609 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2610 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
2611
2612 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
2613 {
2614 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002615 fmt::format("Failed to deduce input tensor shape from filter size {} {}",
2616 reshapedDimensions[1],
2617 CHECK_LOCATION().AsString()));
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002618 }
2619
2620 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
2621 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
2622
James Ward58dec6b2020-09-11 17:32:44 +01002623 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Finn Williamsd4fa5452021-03-01 12:31:41 +00002624 armnn::ReshapeDescriptor reshapeDescriptor;
2625 reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
2626 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002627
2628 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
2629 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
2630
2631 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
Finn Williamsd4fa5452021-03-01 12:31:41 +00002632 // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
2633 tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
2634 startingSlotIndex = 1;
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002635 }
Finn Williamsd4fa5452021-03-01 12:31:41 +00002636
2637 RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002638
Sadik Armagand109a4d2020-07-28 10:42:13 +01002639 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002640 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2641
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002642 // we need to add the activation layer and fortunately we don't need to care about the data layout
2643 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
2644 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002645
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002646 // register the output connection slots for the layer, connections are made after all layers have been created
2647 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2648 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2649}
2650
Kevin May7d96b162021-02-03 17:38:41 +00002651void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
keidav011b3e2ea2019-02-21 10:07:37 +00002652{
2653 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2654
2655 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2656
2657 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2658 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2659 CHECK_VALID_SIZE(outputs.size(), 4);
2660
2661 // Obtain custom options from flexbuffers
2662 auto custom_options = operatorPtr->custom_options;
2663 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2664
2665 // Obtain descriptor information from tf lite
2666 DetectionPostProcessDescriptor desc;
2667 desc.m_MaxDetections = m["max_detections"].AsUInt32();
2668 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
2669 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
2670 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
2671 desc.m_NumClasses = m["num_classes"].AsUInt32();
2672 desc.m_ScaleH = m["h_scale"].AsFloat();
2673 desc.m_ScaleW = m["w_scale"].AsFloat();
2674 desc.m_ScaleX = m["x_scale"].AsFloat();
2675 desc.m_ScaleY = m["y_scale"].AsFloat();
2676
keidav0107d58c72019-02-26 11:57:39 +00002677 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00002678 {
keidav0107d58c72019-02-26 11:57:39 +00002679 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00002680 }
2681 if (!(m["detections_per_class"].IsNull()))
2682 {
2683 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
2684 }
2685
2686 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
2687 {
2688 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
2689 "must be positive and less than or equal to 1.");
2690 }
2691
2692 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002693 auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
keidav011b3e2ea2019-02-21 10:07:37 +00002694
James Ward58dec6b2020-09-11 17:32:44 +01002695 auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
Finn Williamsd4fa5452021-03-01 12:31:41 +00002696 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
keidav011b3e2ea2019-02-21 10:07:37 +00002697 layerName.c_str());
2698
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002699 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00002700
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002701 // The model does not specify the output shapes.
2702 // The output shapes are calculated from the max_detection and max_classes_per_detection.
2703 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
2704 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2705 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2706 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2707 m_OverridenOutputShapes.push_back({ 1 });
2708
keidav011b3e2ea2019-02-21 10:07:37 +00002709 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
2710 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002711 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00002712 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
2713 }
2714
2715 // Register the input connection slots for the layer, connections are made after all layers have been created
2716 // only the tensors for the inputs are relevant, exclude the const tensors
2717 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2718 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2719
2720 // Register the output connection slots for the layer, connections are made after all layers have been created
2721 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2722 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2723 outputTensorIndexes[1],
2724 outputTensorIndexes[2],
2725 outputTensorIndexes[3]});
2726}
2727
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002728/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
Kevin May7d96b162021-02-03 17:38:41 +00002729void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002730{
2731 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2732
2733 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2734 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2735 CHECK_VALID_SIZE(outputs.size(), 1);
2736
2737 if (inputs.size() < 1)
2738 {
2739 throw ParseException("Pack must have at least one input.");
2740 }
2741
2742 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2743 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2744
2745 StackDescriptor desc;
2746 desc.m_Axis = static_cast<uint32_t>(options->axis);
2747 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
2748
2749 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
2750 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2751 desc.m_InputShape = inputTensorInfo.GetShape();
2752
James Ward58dec6b2020-09-11 17:32:44 +01002753 auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002754 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
2755
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002756 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002757
Sadik Armagand109a4d2020-07-28 10:42:13 +01002758 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002759 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2760
2761 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2762 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2763
2764 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2765 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2766}
2767
Kevin May7d96b162021-02-03 17:38:41 +00002768void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd200e3802019-04-15 09:47:39 +01002769{
2770 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2771
2772 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2773 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2774
2775 // This unpackAxis indicates the axis to unpack
2776 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
2777
2778 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2779 CHECK_VALID_SIZE(inputs.size(), 1);
2780
2781 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002782
2783 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2784 {
2785 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002786 fmt::format("The unpack axis: {} cannot be greater than or equal to "
2787 "the number of input dimension {} {}",
2788 unpackAxis,
2789 inputTensorInfo.GetNumDimensions(),
2790 CHECK_LOCATION().AsString()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002791 }
2792
Nina Drozd200e3802019-04-15 09:47:39 +01002793 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
2794 // If num is not defined, automatically infer from the length of the dimension axis.
2795 if(unpackNum == 0)
2796 {
2797 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2798 }
2799
2800 // If unpack number cannot be inferred and is still zero, throw ParseException.
2801 if(unpackNum == 0)
2802 {
2803 throw ParseException("Number to unpack must greater than zero.");
2804 }
2805
2806 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2807 CHECK_VALID_SIZE(outputs.size(), unpackNum);
2808
2809 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2810 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2811
2812 // Add current input shape to unpackDimSizes
2813 for (unsigned int i = 0; i < inputDimSize; ++i)
2814 {
2815 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2816 }
2817
2818 if (unpackDimSizes[unpackAxis] != unpackNum)
2819 {
2820 throw ParseException("Number to unpack must be the same as length of the dimension to "
2821 "unpack along.");
2822 }
2823
2824 unpackDimSizes[unpackAxis] /= unpackNum;
2825
2826 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2827 for (unsigned int j = 0; j < unpackNum; ++j)
2828 {
2829 // Set the size of the views.
2830 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2831 {
2832 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2833 }
2834 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
2835 }
2836
James Ward58dec6b2020-09-11 17:32:44 +01002837 auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd200e3802019-04-15 09:47:39 +01002838 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002839 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01002840
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002841 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
2842 unpackDimSizes.data());
2843
Nina Drozd200e3802019-04-15 09:47:39 +01002844 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2845 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2846
Finn Williamsb49ed182021-06-29 15:50:08 +01002847 std::vector<unsigned int> reshapeDims;
2848 for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
2849 {
2850 if (axis != unpackAxis)
2851 {
2852 reshapeDims.push_back(splitOutShape[axis]);
2853 }
2854 }
2855
2856 TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
2857
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002858 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
2859 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2860 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002861 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
James Ward58dec6b2020-09-11 17:32:44 +01002862 std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002863 armnn::ReshapeDescriptor desc;
Finn Williamsb49ed182021-06-29 15:50:08 +01002864 desc.m_TargetShape = reshapeOutputShape;
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002865 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2866
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002867 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
2868 outputTensorInfo.GetDataType(),
2869 outputTensorInfo.GetQuantizationScale(),
2870 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002871 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
2872
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002873 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002874
2875 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
2876 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
2877 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2878 }
Nina Drozd200e3802019-04-15 09:47:39 +01002879}
2880
Kevin May7d96b162021-02-03 17:38:41 +00002881void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
Nina Drozd0324f482019-04-08 10:52:10 +01002882{
2883 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2884
2885 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2886 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2887
2888 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2889
Nina Drozd200e3802019-04-15 09:47:39 +01002890 // If number of splits cannot be inferred and is zero, throw ParseException.
2891 if(numSplits == 0)
2892 {
2893 throw ParseException("Number to splits must greater than zero.");
2894 }
2895
Nina Drozd0324f482019-04-08 10:52:10 +01002896 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2897 CHECK_VALID_SIZE(inputs.size(), 2);
2898 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2899 CHECK_VALID_SIZE(outputs.size(), numSplits);
2900
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002901 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
2902 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
2903 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Nina Drozd0324f482019-04-08 10:52:10 +01002904
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002905 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002906 if (axisBufferPtr == nullptr)
2907 {
2908 throw ParseException(
2909 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2910 CHECK_LOCATION().AsString()));
2911 }
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002912
Matthew Sloyaned7fce42021-04-15 20:46:24 +01002913 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
2914 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2915 int32_t axis = axisData[0];
2916
2917 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2918 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2919 {
2920 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
2921 // E.g. Rank 4 tensor can have axis in range [-4, 3)
2922 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
2923 throw ParseException(
2924 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2925 axis,
2926 CHECK_LOCATION().AsString()));
2927 }
2928
2929 const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
Nina Drozd0324f482019-04-08 10:52:10 +01002930
Nina Drozd0324f482019-04-08 10:52:10 +01002931 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002932 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01002933 {
2934 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01002935 fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
2936 inputTensorInfo.GetNumDimensions(),
2937 MaxNumOfTensorDimensions,
2938 CHECK_LOCATION().AsString()));
Nina Drozd0324f482019-04-08 10:52:10 +01002939 }
2940
2941 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2942
2943 // Add current input shape to splitterDimSizes
2944 for (unsigned int i = 0; i < inputDimSize; ++i)
2945 {
2946 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2947 }
2948
2949 if (splitterDimSizes[splitDim] % numSplits != 0)
2950 {
2951 throw ParseException("Number of splits must evenly divide the dimension");
2952 }
2953 splitterDimSizes[splitDim] /= numSplits;
2954
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002955 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01002956 for (unsigned int j = 0; j < numSplits; ++j)
2957 {
2958 // Set the size of the views.
2959 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2960 {
2961 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2962 }
2963 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
2964 }
2965
James Ward58dec6b2020-09-11 17:32:44 +01002966 auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
Nina Drozd0324f482019-04-08 10:52:10 +01002967 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002968 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01002969
2970 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002971 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01002972
Nina Drozd0324f482019-04-08 10:52:10 +01002973 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2974 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002975 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01002976 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01002977 }
2978
2979 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2980 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2981}
2982
Derek Lambertif0176992020-04-28 13:37:49 +01002983unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
2984{
2985 int numDims = armnn::numeric_cast<int>(numDimsIn);
2986 int v = idx < 0 ? numDims + idx : idx;
2987 ARMNN_ASSERT(v >= 0);
2988 ARMNN_ASSERT(v < numDims);
2989
2990 return static_cast<unsigned int>(v);
2991}
2992
Kevin May7d96b162021-02-03 17:38:41 +00002993void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
Derek Lambertif0176992020-04-28 13:37:49 +01002994{
2995 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2996
2997 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Ryan OShea86704732020-05-26 11:41:04 +01002998 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01002999
3000 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3001 CHECK_VALID_SIZE(inputs.size(), 3);
3002
3003 auto& inputTensor = inputs[0];
3004 auto& splitsTensor = inputs[1];
3005 auto& axisTensor = inputs[2];
3006
3007 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
3008 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
3009 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
3010 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
3011
3012 // Inputs
3013 auto inputDimSize = inputTensorInfo.GetNumDimensions();
3014 if (inputDimSize > MaxNumOfTensorDimensions)
3015 {
3016 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003017 fmt::format("The number of dimensions: {} for input tensors of the "
3018 "SplitV op cannot be greater than {} {}",
3019 inputTensorInfo.GetNumDimensions(),
3020 MaxNumOfTensorDimensions,
3021 CHECK_LOCATION().AsString()));
Derek Lambertif0176992020-04-28 13:37:49 +01003022 }
3023
3024 // Get split axis
3025 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003026 if (axisBufferPtr == nullptr)
3027 {
3028 throw ParseException(
3029 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3030 CHECK_LOCATION().AsString()));
3031 }
3032
Derek Lambertif0176992020-04-28 13:37:49 +01003033 std::vector<int> axisData(axisTensorInfo.GetNumElements());
3034 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003035 int32_t axis = axisData[0];
3036
3037 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3038 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3039 {
3040 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3041 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3042 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3043 throw ParseException(
3044 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3045 axis,
3046 CHECK_LOCATION().AsString()));
3047 }
3048 const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
Derek Lambertif0176992020-04-28 13:37:49 +01003049
Derek Lambertif0176992020-04-28 13:37:49 +01003050 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01003051 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01003052 unsigned int numSplits{0};
3053
3054 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01003055 {
3056 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01003057 }
3058 else
3059 {
Ryan OShea86704732020-05-26 11:41:04 +01003060 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01003061 }
3062
3063 if (numSplits <=0)
3064 {
3065 throw ParseException("SplitV has invalid number of splits");
3066 }
3067
Jan Eilersc0761e92020-06-29 16:48:44 +01003068 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01003069 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01003070 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01003071
Jan Eilersc0761e92020-06-29 16:48:44 +01003072 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01003073 int numInferred{0};
3074 unsigned int inferIdx{0};
3075 int splitSum{0};
3076 for (auto split : splitsData)
3077 {
3078 if (split < 0)
3079 {
3080 numInferred++;
3081 inferIdx = idx;
3082 }
3083 else
3084 {
3085 splitSum += split;
3086 }
3087 idx++;
3088 }
3089 // Check for inferred Axis
3090 if (numInferred == 0)
3091 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003092 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01003093 {
3094 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
3095 }
3096 }
3097 else if (numInferred == 1)
3098 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003099 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01003100 }
3101 else
3102 {
3103 throw ParseException("Cannot infer split size for more than one split");
3104 }
3105
Derek Lambertif0176992020-04-28 13:37:49 +01003106 //Ouput size validation
3107 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3108 CHECK_VALID_SIZE(outputs.size(), numSplits);
3109
3110 // Setup Armnn descriptor
3111 SplitterDescriptor splitDesc(numSplits, inputDimSize);
3112 unsigned int accumSplit = 0;
3113 for (unsigned int j = 0; j < numSplits; ++j)
3114 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01003115 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01003116
3117 // Set the size of the views.
3118 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
3119 {
3120 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
3121 if (dimIdx == splitDim)
3122 {
3123 dimSize = splitSize;
3124 }
3125 splitDesc.SetViewSize(j, dimIdx, dimSize);
3126 }
3127
3128 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
3129 accumSplit += splitSize;
3130 }
3131
James Ward58dec6b2020-09-11 17:32:44 +01003132 auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01003133 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01003134 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01003135
3136 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3137 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3138
3139 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
3140 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01003141 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01003142 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
3143 }
3144
3145 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3146 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3147}
3148
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003149void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
3150{
3151 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
3152}
3153
Kevin May7d96b162021-02-03 17:38:41 +00003154void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
Inki Daed4619e22020-09-10 15:33:54 +09003155{
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003156 ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
3157}
3158
3159void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
3160{
Inki Daed4619e22020-09-10 15:33:54 +09003161 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3162 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3163 CHECK_VALID_SIZE(inputs.size(), 2);
3164
3165 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3166 CHECK_VALID_SIZE(outputs.size(), 1);
3167
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003168 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3169 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
Inki Daed4619e22020-09-10 15:33:54 +09003170 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003171 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003172
3173 // Check if output tensor type is Signed32 or Signed64
Mike Kelly1f140f72021-04-06 12:25:55 +01003174 if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
3175 outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
3176 {
3177 throw ParseException(
3178 fmt::format(
3179 "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
3180 CHECK_LOCATION().AsString()));
3181 }
Matthew Sloyan28f177c2021-04-09 14:38:52 +01003182
3183 // Get const axis value from model and set it to descriptor.
3184 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3185 if (axisBufferPtr == nullptr)
3186 {
3187 throw ParseException(
3188 fmt::format("Operation has invalid inputs. Failed to read axis. {}",
3189 CHECK_LOCATION().AsString()));
3190 }
3191
3192 std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
3193 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3194 int32_t axis = axisData.front();
3195
3196 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3197 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3198 {
3199 // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3200 // E.g. Rank 4 tensor can have axis in range [-4, 3)
3201 // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3202 throw ParseException(
3203 fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3204 axis,
3205 CHECK_LOCATION().AsString()));
3206 }
3207
3208 ArgMinMaxDescriptor desc;
3209 desc.m_Axis = axis;
3210 desc.m_Function = argMinMaxFunction;
3211
3212 // Register a ArgMin/ArgMax layer.
3213 auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3214 auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3215 IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3216 ARMNN_ASSERT(layer != nullptr);
Inki Daed4619e22020-09-10 15:33:54 +09003217 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3218
3219 // Register input tensor to the layer.
3220 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3221 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3222
3223 // Register output tensor to the layer.
3224 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3225 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3226}
3227
Kevin May7d96b162021-02-03 17:38:41 +00003228void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003229{
3230 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3231
Kevin May7d96b162021-02-03 17:38:41 +00003232 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003233 CHECK_VALID_SIZE(inputs.size(), 2);
Kevin May7d96b162021-02-03 17:38:41 +00003234 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003235 CHECK_VALID_SIZE(outputs.size(), 1);
3236
3237 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3238 armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3239 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3240
3241 armnn::GatherDescriptor gatherDescriptor;
3242
3243 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3244 const auto * options = operatorPtr->builtin_options.AsGatherOptions();
3245 auto axis = options->axis;
3246
3247 auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3248 auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3249 auto outputDimensions = outputTensorInfo.GetNumDimensions();
3250 if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3251 {
3252 throw ParseException(
3253 fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3254 axis,
3255 inputDimensions, inputDimensions,
3256 CHECK_LOCATION().AsString()));
3257 }
3258 if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3259 {
3260 throw ParseException(
3261 fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3262 outputDimensions,
3263 inputDimensions, indicesDimensions,
3264 CHECK_LOCATION().AsString()));
3265 }
3266
3267 gatherDescriptor.m_Axis = axis;
3268
3269 auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3270 IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3271 ARMNN_ASSERT(layer != nullptr);
3272 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3273
3274 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3275 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3276
3277 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3278 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3279}
3280
Kevin May7d96b162021-02-03 17:38:41 +00003281void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan26868492021-01-22 14:25:31 +00003282{
3283 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3284
Kevin May7d96b162021-02-03 17:38:41 +00003285 TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003286 CHECK_VALID_SIZE(inputs.size(), 1);
Kevin May7d96b162021-02-03 17:38:41 +00003287 TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan26868492021-01-22 14:25:31 +00003288 CHECK_VALID_SIZE(outputs.size(), 1);
3289
3290 armnn::DepthToSpaceDescriptor descriptor;
3291
3292 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3293 const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
3294 auto blockSize = options->block_size;
3295 if (blockSize < 2)
3296 {
3297 throw ParseException(
3298 fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
3299 blockSize,
3300 CHECK_LOCATION().AsString()));
3301 }
3302 descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
3303
3304 auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
3305 IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
3306 ARMNN_ASSERT(layer != nullptr);
3307 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3308 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3309
3310 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3311 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3312
3313 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3314 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3315}
3316
Kevin May7d96b162021-02-03 17:38:41 +00003317void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003318{
Sadik Armagana2747482021-02-09 10:28:54 +00003319 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
3320}
3321
3322void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
3323{
3324 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
3325}
3326
3327void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
3328{
3329 ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
3330}
3331
3332void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
3333{
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003334 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3335
3336 const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3337 const auto *options = operatorPtr->builtin_options.AsReducerOptions();
3338
3339 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3340 CHECK_VALID_SIZE(inputs.size(), 2);
3341
3342 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3343 CHECK_VALID_SIZE(outputs.size(), 1);
3344
Sadik Armagana2747482021-02-09 10:28:54 +00003345 auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003346
3347 armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
3348 armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003349
3350 ReduceDescriptor desc;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003351 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3352 // Get const axis value from model and set it to descriptor.
3353 if (axisBufferPtr != nullptr)
3354 {
Sadik Armagan49bdb792021-02-11 13:57:07 +00003355 std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
3356 ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
3357
3358 // Convert the axis to unsigned int and remove duplicates.
3359 auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
3360 std::set<unsigned int> uniqueAxis;
3361 std::transform(axisData.begin(),
3362 axisData.end(),
3363 std::inserter(uniqueAxis, uniqueAxis.begin()),
3364 [rank](int i)->unsigned int{
3365 return static_cast<uint32_t>(((i + rank) % rank)); });
3366 desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003367 }
Sadik Armagana2747482021-02-09 10:28:54 +00003368 else
3369 {
3370 for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
3371 {
3372 desc.m_vAxis.push_back(i);
3373 }
3374 }
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003375
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003376 desc.m_KeepDims = options->keep_dims;
Sadik Armagana2747482021-02-09 10:28:54 +00003377 desc.m_ReduceOperation = reduceOperation;
Sadik Armagan0c3ea5b2021-02-03 09:29:30 +00003378
3379 // Register a new layer object, Sum.
3380 IConnectableLayer *layer = m_Network->AddReduceLayer(desc, layerName.c_str());
3381
3382 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3383 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3384
3385 // Register input tensor to the layer.
3386 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3387 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3388
3389 // Register output tensor to the layer.
3390 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3391 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3392}
3393
Matthew Sloyaned7fce42021-04-15 20:46:24 +01003394void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
3395{
3396 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
3397}
3398
3399void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
3400{
3401 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
3402}
3403
3404void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
3405{
3406 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
3407}
3408
3409void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
3410{
3411 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
3412}
3413
3414void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
3415{
3416 ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
3417}
3418
3419void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
3420{
3421 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3422
3423 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3424 CHECK_VALID_SIZE(inputs.size(), 1);
3425
3426 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3427 CHECK_VALID_SIZE(outputs.size(), 1);
3428
3429 std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
3430 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3431
3432 ElementwiseUnaryDescriptor desc;
3433 desc.m_Operation = unaryOperation;
3434 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
3435 ARMNN_ASSERT(layer != nullptr);
3436
3437 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3438 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3439
3440 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3441 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3442
3443 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3444 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3445}
3446
Bruno Goncalves2d0eb862021-07-11 14:10:15 -03003447void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
3448{
3449 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
3450}
3451
3452void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
3453{
3454 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
3455}
3456
3457void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
3458{
3459 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
3460}
3461
3462void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
3463{
3464 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
3465}
3466
3467void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
3468{
3469 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
3470}
3471
3472void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
3473{
3474 ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
3475}
3476
3477void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
3478 ComparisonOperation comparisonOperation)
3479{
3480 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3481
3482 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3483 CHECK_VALID_SIZE(inputs.size(), 2);
3484
3485 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3486 CHECK_VALID_SIZE(outputs.size(), 1);
3487
3488 auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
3489 std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3490
3491 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3492 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
3493 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
3494
3495 ComparisonDescriptor desc;
3496 desc.m_Operation = comparisonOperation;
3497 IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
3498 ARMNN_ASSERT(layer != nullptr);
3499
3500 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3501 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3502
3503 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3504 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3505
3506 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3507 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3508}
3509
Kevin May7d96b162021-02-03 17:38:41 +00003510armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
3511 unsigned int outputSlot,
3512 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01003513{
3514 ActivationDescriptor activationDesc;
3515 std::string layerName = prevLayer->GetName();
3516
3517 switch(activationType)
3518 {
3519 case tflite::ActivationFunctionType_NONE:
3520 {
3521 // this is a no-op: return previous layer
3522 return prevLayer;
3523 }
3524 case tflite::ActivationFunctionType_RELU:
3525 {
3526 activationDesc.m_Function = ActivationFunction::ReLu;
3527 layerName += ":RELU";
3528 break;
3529 }
3530 case tflite::ActivationFunctionType_RELU6:
3531 {
3532 activationDesc.m_Function = ActivationFunction::BoundedReLu;
3533 activationDesc.m_A = 6.0f;
3534 activationDesc.m_B = 0.0f;
3535 layerName += ":RELU6";
3536 break;
3537 }
3538 case tflite::ActivationFunctionType_TANH:
3539 {
3540 activationDesc.m_Function = ActivationFunction::TanH;
3541 activationDesc.m_A = 1.0f;
3542 activationDesc.m_B = 1.0f;
3543 layerName += ":TANH";
3544 break;
3545 }
3546
3547 // I only put these here as a reminder what others we could support
3548 case tflite::ActivationFunctionType_RELU_N1_TO_1:
3549 case tflite::ActivationFunctionType_SIGN_BIT:
3550 default:
3551 {
3552 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003553 fmt::format("TfLite parser doesn't suppport fused activation: "
3554 "{}/{} {} ",
3555 activationType,
3556 tflite::EnumNameActivationFunctionType(activationType),
3557 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003558
3559 }
3560 }
3561
3562 IConnectableLayer* activationLayer =
3563 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3564
3565 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
3566 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
3567 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
3568 return activationLayer;
3569}
3570
Kevin May7d96b162021-02-03 17:38:41 +00003571TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromFile(const char * fileName)
telsoa01c577f2c2018-08-31 09:22:23 +01003572{
3573 if (fileName == nullptr)
3574 {
James Ward58dec6b2020-09-11 17:32:44 +01003575 throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
telsoa01c577f2c2018-08-31 09:22:23 +01003576 CHECK_LOCATION().AsString()));
3577 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01003578 std::error_code errorCode;
3579 fs::path pathToFile(fileName);
3580 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01003581 {
James Ward58dec6b2020-09-11 17:32:44 +01003582 //fmt::format() could not be used here (format error)
3583 std::stringstream msg;
3584 msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
3585 << " " << CHECK_LOCATION().AsString();
3586
3587 throw FileNotFoundException(msg.str());
telsoa01c577f2c2018-08-31 09:22:23 +01003588 }
3589 std::ifstream file(fileName, std::ios::binary);
3590 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3591 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
3592 fileContent.size());
3593}
3594
Kevin May7d96b162021-02-03 17:38:41 +00003595TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t * binaryContent, size_t len)
telsoa01c577f2c2018-08-31 09:22:23 +01003596{
3597 if (binaryContent == nullptr)
3598 {
James Ward58dec6b2020-09-11 17:32:44 +01003599 throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
telsoa01c577f2c2018-08-31 09:22:23 +01003600 CHECK_LOCATION().AsString()));
3601 }
3602 flatbuffers::Verifier verifier(binaryContent, len);
3603 if (verifier.VerifyBuffer<tflite::Model>() == false)
3604 {
3605 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003606 fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
3607 "flatbuffers format. size:{} {}",
3608 len,
3609 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003610 }
3611 return tflite::UnPackModel(binaryContent);
3612}
3613
Kevin May7d96b162021-02-03 17:38:41 +00003614TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetInputs(const ModelPtr & model,
3615 size_t subgraphIndex,
3616 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003617{
3618 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3619
Derek Lambertiff05cc52019-04-26 13:05:17 +01003620 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3621 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003622
3623 size_t inputCount = operatorPtr->inputs.size();
mathad01c21025d2021-04-26 10:09:37 +01003624 TensorRawPtrVector result;
telsoa01c577f2c2018-08-31 09:22:23 +01003625 for (size_t i=0; i<inputCount; ++i)
3626 {
mathad01c21025d2021-04-26 10:09:37 +01003627 // If the input location is -1 then assume input is turned off.
3628 if (operatorPtr->inputs[i] == -1)
3629 {
3630 continue;
3631 }
3632 else
3633 {
3634 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
3635 result.push_back(subgraphPtr->tensors[inputId].get());
3636 }
telsoa01c577f2c2018-08-31 09:22:23 +01003637 }
3638 return result;
3639}
3640
Kevin May7d96b162021-02-03 17:38:41 +00003641TfLiteParserImpl::TensorRawPtrVector TfLiteParserImpl::GetOutputs(const ModelPtr & model,
3642 size_t subgraphIndex,
3643 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003644{
3645 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3646
Derek Lambertiff05cc52019-04-26 13:05:17 +01003647 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3648 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003649
3650 size_t outputCount = operatorPtr->outputs.size();
3651 TensorRawPtrVector result(outputCount);
3652 for (size_t i=0; i<outputCount; ++i)
3653 {
3654 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
3655 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003656 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01003657 }
3658 return result;
3659}
3660
Kevin May7d96b162021-02-03 17:38:41 +00003661TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphInputs(const ModelPtr & model,
3662 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003663{
3664 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003665 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003666
Derek Lambertiff05cc52019-04-26 13:05:17 +01003667 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003668 TensorIdRawPtrVector result(inputCount);
3669 for (size_t i=0; i<inputCount; ++i)
3670 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003671 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01003672 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003673 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003674 }
3675 return result;
3676}
3677
Kevin May7d96b162021-02-03 17:38:41 +00003678TfLiteParserImpl::TensorIdRawPtrVector TfLiteParserImpl::GetSubgraphOutputs(const ModelPtr & model,
3679 size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003680{
3681 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003682 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003683
Derek Lambertiff05cc52019-04-26 13:05:17 +01003684 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003685 TensorIdRawPtrVector result(outputCount);
3686 for (size_t i=0; i<outputCount; ++i)
3687 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003688 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
3689 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003690 }
3691 return result;
3692}
3693
Kevin May7d96b162021-02-03 17:38:41 +00003694std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
3695 size_t subgraphIndex,
3696 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003697{
3698 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003699 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3700 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003701 return operatorPtr->inputs;
3702}
3703
Kevin May7d96b162021-02-03 17:38:41 +00003704std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
3705 size_t subgraphIndex,
3706 size_t operatorIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003707{
3708 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003709 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3710 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003711 return operatorPtr->outputs;
3712}
3713
Kevin May7d96b162021-02-03 17:38:41 +00003714void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
3715 size_t operatorIndex,
3716 IConnectableLayer* layer,
Finn Williamsd4fa5452021-03-01 12:31:41 +00003717 const std::vector<unsigned int>& tensorIndexes,
3718 unsigned int startingSlotIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003719{
3720 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003721 ARMNN_ASSERT(layer != nullptr);
Matthew Sloyan81beae32021-07-13 19:46:11 +01003722
Finn Williamsd4fa5452021-03-01 12:31:41 +00003723 if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
telsoa01c577f2c2018-08-31 09:22:23 +01003724 {
3725 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003726 fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
3727 " for subgraph:{} operator index:{} {}",
3728 tensorIndexes.size(),
3729 layer->GetNumInputSlots(),
3730 subgraphIndex,
3731 operatorIndex,
3732 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003733 }
3734
Finn Williamsd4fa5452021-03-01 12:31:41 +00003735 for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
telsoa01c577f2c2018-08-31 09:22:23 +01003736 {
Finn Williamsd4fa5452021-03-01 12:31:41 +00003737 unsigned int tensorIndex = tensorIndexes[index];
3738 armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
telsoa01c577f2c2018-08-31 09:22:23 +01003739 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3740 }
3741}
3742
Kevin May7d96b162021-02-03 17:38:41 +00003743void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
3744 size_t operatorIndex,
3745 IConnectableLayer* layer,
3746 const std::vector<unsigned int>& tensorIndexes)
telsoa01c577f2c2018-08-31 09:22:23 +01003747{
3748 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003749 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003750 if (tensorIndexes.size() != layer->GetNumOutputSlots())
3751 {
3752 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003753 fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
3754 " for subgraph:{} operator index:{} {}",
3755 tensorIndexes.size(),
3756 layer->GetNumOutputSlots(),
3757 subgraphIndex,
3758 operatorIndex,
3759 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003760 }
3761
3762 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
3763 {
3764 unsigned int tensorIndex = tensorIndexes[slotIndex];
3765 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
3766 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3767 }
3768}
3769
Kevin May7d96b162021-02-03 17:38:41 +00003770void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003771{
3772 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3773
3774 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
3775 for (auto const & tensorIdAndPtr : inputs)
3776 {
3777 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3778 IConnectableLayer* layer =
3779 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3780
3781 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
3782 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3783
3784 RegisterOutputSlots(subgraphIndex,
3785 VIRTUAL_OPERATOR_ID,
3786 layer,
3787 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3788 }
3789}
3790
Kevin May7d96b162021-02-03 17:38:41 +00003791void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003792{
3793 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3794
3795 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
3796 for (auto const & tensorIdAndPtr : outputs)
3797 {
3798 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3799 IConnectableLayer* layer =
3800 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3801
3802 RegisterInputSlots(subgraphIndex,
3803 VIRTUAL_OPERATOR_ID,
3804 layer,
3805 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3806 }
3807}
3808
Kevin May7d96b162021-02-03 17:38:41 +00003809void TfLiteParserImpl::SetupConstantLayers(size_t subgraphIndex)
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003810{
3811 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3812
Derek Lambertiff05cc52019-04-26 13:05:17 +01003813 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003814 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3815 {
3816 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3817 {
3818 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
3819 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3820 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003821 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003822
Matthew Sloyan81beae32021-07-13 19:46:11 +01003823 if(IsConstTensor(tensorPtr))
3824 {
3825 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
3826 auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo);
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003827
Matthew Sloyan81beae32021-07-13 19:46:11 +01003828 std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
3829 IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003830
Matthew Sloyan81beae32021-07-13 19:46:11 +01003831 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3832 RegisterOutputSlots(subgraphIndex,
3833 VIRTUAL_OPERATOR_ID,
3834 layer,
3835 { tensorIndex });
3836 }
3837 else
3838 {
3839 throw ParseException(
3840 fmt::format("Invalid Tensor: Tensor should be constant. {}",
3841 CHECK_LOCATION().AsString()));
3842 }
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003843 }
3844 }
3845 }
3846}
3847
telsoa01c577f2c2018-08-31 09:22:23 +01003848// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
Kevin May7d96b162021-02-03 17:38:41 +00003849TfLiteParserImpl::BufferRawPtr TfLiteParserImpl::GetBuffer(const ModelPtr& model, size_t bufferIndex)
telsoa01c577f2c2018-08-31 09:22:23 +01003850{
3851 CHECK_BUFFER(model, bufferIndex);
3852 return model->buffers[bufferIndex].get();
3853}
3854
Matteo Martincigh747ef822018-12-18 09:26:39 +00003855template<typename T>
Kevin May7d96b162021-02-03 17:38:41 +00003856std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3857TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
3858 TfLiteParserImpl::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00003859 armnn::TensorInfo& tensorInfo,
3860 armnn::Optional<armnn::PermutationVector&> permutationVector)
3861{
Matthew Sloyan81beae32021-07-13 19:46:11 +01003862 // Make sure isConstant flag is set.
3863 tensorInfo.SetConstant();
3864
Matteo Martincigh747ef822018-12-18 09:26:39 +00003865 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3866 tensorPtr,
3867 tensorInfo,
3868 permutationVector);
Kevin May7d96b162021-02-03 17:38:41 +00003869 TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
Matteo Martincigh747ef822018-12-18 09:26:39 +00003870 return std::make_pair(constData.first, std::move(storage));
3871}
3872
Finn Williamsd4fa5452021-03-01 12:31:41 +00003873bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
3874{
3875 CHECK_TENSOR_PTR(tensorPtr);
mathad01bf7edb62021-04-20 16:12:45 +01003876 bool isConst = true;
3877
3878 auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
3879 if (buffer->data.size() == 0)
3880 {
3881 isConst = false;
3882 }
3883
3884 return isConst;
Finn Williamsd4fa5452021-03-01 12:31:41 +00003885}
3886
Kevin May7d96b162021-02-03 17:38:41 +00003887std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
Finn Williamsd4fa5452021-03-01 12:31:41 +00003888TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
3889 armnn::TensorInfo& tensorInfo,
3890 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01003891{
3892 CHECK_TENSOR_PTR(tensorPtr);
3893 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3894 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3895
Matthew Sloyan81beae32021-07-13 19:46:11 +01003896 // Make sure isConstant flag is set.
3897 tensorInfo.SetConstant();
3898
telsoa01c577f2c2018-08-31 09:22:23 +01003899 switch (tensorInfo.GetDataType())
3900 {
3901 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003902 return CreateConstTensorAndStoreData<float>(bufferPtr,
3903 tensorPtr,
3904 tensorInfo,
3905 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00003906 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003907 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3908 tensorPtr,
3909 tensorInfo,
3910 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00003911 case armnn::DataType::QSymmS8:
3912 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3913 tensorPtr,
3914 tensorInfo,
3915 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00003916 case armnn::DataType::QAsymmS8:
3917 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3918 tensorPtr,
3919 tensorInfo,
3920 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003921 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003922 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3923 tensorPtr,
3924 tensorInfo,
3925 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003926 default:
3927 {
3928 std::stringstream errString;
3929 errString << "Unexpected datatype when creating const tensor: "
3930 << armnn::GetDataTypeName(tensorInfo.GetDataType())
3931 << " shape:" << tensorInfo.GetShape()
3932 << CHECK_LOCATION().AsString();
3933 throw ParseException(errString.str());
3934 }
3935 }
3936}
3937
Finn Williamsd4fa5452021-03-01 12:31:41 +00003938armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
3939 armnn::TensorInfo& tensorInfo)
3940{
3941 CHECK_TENSOR_PTR(tensorPtr);
3942 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3943 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3944
Matthew Sloyan81beae32021-07-13 19:46:11 +01003945 // Make sure isConstant flag is set.
3946 tensorInfo.SetConstant();
3947
Finn Williamsd4fa5452021-03-01 12:31:41 +00003948 return ConstTensor(tensorInfo, bufferPtr->data.data());
3949}
3950
Kevin May7d96b162021-02-03 17:38:41 +00003951BindingPointInfo TfLiteParserImpl::GetNetworkInputBindingInfo(size_t subgraphId,
3952 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01003953{
3954 CHECK_SUBGRAPH(m_Model, subgraphId);
3955 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3956 for (auto const & input : inputs)
3957 {
3958 if (input.second->name == name)
3959 {
3960 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3961 return std::make_pair(bindingId, ToTensorInfo(input.second));
3962 }
3963 }
3964
3965 std::stringstream bindings;
3966 for (auto const & input : inputs)
3967 {
3968 bindings << "'" << input.second->name << "' ";
3969 }
3970
3971 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01003972 fmt::format("No input binding found for subgraph:{} and name:{}. "
3973 "Possible inputs are: [{}] {}",
3974 subgraphId,
3975 name,
3976 bindings.str(),
3977 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01003978}
3979
Kevin May7d96b162021-02-03 17:38:41 +00003980BindingPointInfo TfLiteParserImpl::GetNetworkOutputBindingInfo(size_t subgraphId,
3981 const std::string& name) const
telsoa01c577f2c2018-08-31 09:22:23 +01003982{
3983 CHECK_SUBGRAPH(m_Model, subgraphId);
3984 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003985 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01003986 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003987 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01003988 if (output.second->name == name)
3989 {
3990 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003991 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3992 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3993 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01003994 }
3995 }
3996
3997 std::stringstream bindings;
3998 for (auto const & output : outputs)
3999 {
4000 bindings << "'" << output.second->name << "' ";
4001 }
4002
4003 throw ParseException(
James Ward58dec6b2020-09-11 17:32:44 +01004004 fmt::format("No output binding found for subgraph:{} and name:{}. "
4005 "Possible outputs are: [{}] {}",
4006 subgraphId,
4007 name,
4008 bindings.str(),
4009 CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +01004010}
4011
Kevin May7d96b162021-02-03 17:38:41 +00004012size_t TfLiteParserImpl::GetSubgraphCount() const
telsoa01c577f2c2018-08-31 09:22:23 +01004013{
4014 return m_Model->subgraphs.size();
4015}
4016
Kevin May7d96b162021-02-03 17:38:41 +00004017std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004018{
4019 CHECK_SUBGRAPH(m_Model, subgraphId);
4020 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
4021 std::vector<std::string> result;
4022 result.reserve(inputs.size());
4023 for (auto const & input : inputs)
4024 {
4025 result.push_back(input.second->name);
4026 }
4027 return result;
4028}
4029
Kevin May7d96b162021-02-03 17:38:41 +00004030std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
telsoa01c577f2c2018-08-31 09:22:23 +01004031{
4032 CHECK_SUBGRAPH(m_Model, subgraphId);
4033 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
4034 std::vector<std::string> result;
4035 result.reserve(outputs.size());
4036 for (auto const & output : outputs)
4037 {
4038 result.push_back(output.second->name);
4039 }
4040 return result;
4041}
4042
Matthew Sloyanac001ee2021-02-03 10:43:04 +00004043const std::string TfLiteParserImpl::GetVersion()
4044{
4045 return TFLITE_PARSER_VERSION;
4046}
4047
Kevin May7d96b162021-02-03 17:38:41 +00004048TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01004049: m_FloatData(std::move(data))
4050, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004051, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004052, m_Int32Data(nullptr)
4053{
4054}
4055
Kevin May7d96b162021-02-03 17:38:41 +00004056TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01004057: m_FloatData(nullptr)
4058, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00004059, m_Int8Data(nullptr)
4060, m_Int32Data(nullptr)
4061{
4062}
4063
Kevin May7d96b162021-02-03 17:38:41 +00004064TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
Keith Davisd305e1a2020-01-22 11:57:54 +00004065: m_FloatData(nullptr)
4066, m_Uint8Data(nullptr)
4067, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01004068, m_Int32Data(nullptr)
4069{
4070}
4071
Kevin May7d96b162021-02-03 17:38:41 +00004072TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
telsoa01c577f2c2018-08-31 09:22:23 +01004073: m_FloatData(nullptr)
4074, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00004075, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01004076, m_Int32Data(std::move(data))
4077{
4078}
4079
4080} // armnnTfLiteParser