blob: 109c2c2be10c6b18127dc46600cb8d416d7003be [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
Sadik Armagand109a4d2020-07-28 10:42:13 +01008#include <armnn/BackendOptions.hpp>
Matthew Bentham39ef3e52020-01-20 10:09:09 +00009#include <armnn/Descriptors.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010010#include <armnn/Exceptions.hpp>
Derek Lamberti08446972019-11-26 16:38:31 +000011#include <armnn/Logging.hpp>
James Conroy05102392020-06-24 15:39:55 +010012#include <armnn/Tensor.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010013#include <armnn/TypesUtils.hpp>
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +010014#include <armnn/utility/Assert.hpp>
Jan Eilers8eb25602020-03-09 12:13:48 +000015#include <armnn/utility/IgnoreUnused.hpp>
Derek Lambertif0176992020-04-28 13:37:49 +010016#include <armnn/utility/NumericCast.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010017
18// armnnUtils:
Matteo Martincighe011d202019-11-28 11:35:47 +000019#include <armnnUtils/Permute.hpp>
Francis Murtagh532a29d2020-06-29 11:50:01 +010020#include <Filesystem.hpp>
Matteo Martincighe011d202019-11-28 11:35:47 +000021
Sadik Armagan479045b2018-10-01 11:51:37 +010022#include <ParserHelper.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010023#include <VerificationHelpers.hpp>
24
25// The generated code based on the Tf Lite schema:
26#include <schema_generated.h>
27
Matteo Martincighe011d202019-11-28 11:35:47 +000028#include <flatbuffers/flexbuffers.h>
29
telsoa01c577f2c2018-08-31 09:22:23 +010030#include <boost/format.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +010031
32#include <fstream>
33#include <algorithm>
34#include <limits>
Sadikb94967b2018-09-19 15:30:00 +010035#include <numeric>
Derek Lambertic9e52792020-03-11 11:42:26 +000036#include <sstream>
37
38#define ARMNN_THROW_PARSE_EXCEPTION(msg) \
39 { \
40 throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
41 << ": " \
42 << CHECK_LOCATION().AsString()).str()); \
43 }
telsoa01c577f2c2018-08-31 09:22:23 +010044
45using namespace armnn;
46using armnn::CheckLocation;
47namespace armnnTfLiteParser
48{
49namespace
50{
jimfly01c25411c2018-11-14 17:47:22 +000051
telsoa01c577f2c2018-08-31 09:22:23 +010052const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
53
54void CheckSubgraph(const TfLiteParser::ModelPtr & model,
55 size_t subgraphIndex,
56 const CheckLocation & location)
57{
58 if (model.get() == nullptr)
59 {
60 throw ParseException(
61 boost::str(
62 boost::format("%1% was called with invalid (null) model. "
63 "Possible reason is that the model is not yet loaded and Unpack(ed). "
64 "subgraph:%2% at %3%") %
65 location.m_Function %
66 subgraphIndex %
67 location.FileLine()));
68 }
69 else if (subgraphIndex >= model->subgraphs.size())
70 {
71 throw ParseException(
72 boost::str(
73 boost::format("%1% was called with an invalid subgraph index. "
74 "subgraph:%2% at %3%") %
75 location.m_Function %
76 subgraphIndex %
77 location.FileLine()));
78 }
79}
80
81#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
82 CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
83
84void CheckModel(const TfLiteParser::ModelPtr & model,
85 size_t subgraphIndex,
86 size_t operatorIndex,
87 const CheckLocation & location)
88{
89 if (model.get() == nullptr)
90 {
91 throw ParseException(
92 boost::str(
93 boost::format("%1% was called with invalid (null) model. "
94 "Possible reason is that the model is not yet loaded and Unpack(ed). "
95 "subgraph:%2% operator:%3% at %4%") %
96 location.m_Function %
97 subgraphIndex %
98 operatorIndex %
99 location.FileLine()));
100 }
101 else if (subgraphIndex >= model->subgraphs.size())
102 {
103 throw ParseException(
104 boost::str(
105 boost::format("%1% was called with an invalid subgraph index. "
106 "subgraph:%2% operator:%3% at %4%") %
107 location.m_Function %
108 subgraphIndex %
109 operatorIndex %
110 location.FileLine()));
111 }
112 else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
113 operatorIndex != VIRTUAL_OPERATOR_ID)
114 {
115 throw ParseException(
116 boost::str(
117 boost::format("%1% was called with an invalid operator index. "
118 "subgraph:%2% operator:%3% at %4%") %
119 location.m_Function %
120 subgraphIndex %
121 operatorIndex %
122 location.FileLine()));
123 }
124}
125
126#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
127 CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
128
129void CheckTensor(const TfLiteParser::ModelPtr & model,
130 size_t subgraphIndex,
131 size_t tensorIndex,
132 const CheckLocation & location)
133{
134 // not checking model, because I assume CHECK_MODEL already run
135 // and checked that. An assert would do.
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100136 ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
telsoa01c577f2c2018-08-31 09:22:23 +0100137
138 // also subgraph index should be checked by CHECK_MODEL so
139 // I only add an assert here
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100140 ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
telsoa01c577f2c2018-08-31 09:22:23 +0100141
142 // the tensor index is the only one to check here
143 if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
144 {
145 throw ParseException(
146 boost::str(
147 boost::format("%1% was called with an invalid tensor index. "
148 "subgraph:%2% tensor:%3% at %4%") %
149 location.m_Function %
150 subgraphIndex %
151 tensorIndex %
152 location.FileLine()));
153 }
154}
155
156#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
157 CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
158
159void CheckTensorPtr(TfLiteParser::TensorRawPtr rawPtr,
160 const CheckLocation & location)
161{
162 if (rawPtr == nullptr)
163 {
164 throw ParseException(
165 boost::str(
166 boost::format("%1% was called with a null tensor pointer. "
167 "at %2%") %
168 location.m_Function %
169 location.FileLine()));
170
171 }
172}
173
174#define CHECK_TENSOR_PTR(TENSOR_PTR) \
175 CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
176
177void CheckBuffer(const TfLiteParser::ModelPtr & model,
178 size_t bufferIndex,
179 const CheckLocation & location)
180{
181 if (model.get() == nullptr)
182 {
183 throw ParseException(
184 boost::str(
185 boost::format("%1% was called with invalid (null) model. "
186 "Possible reason is that the model is not yet loaded and Unpack(ed). "
187 "buffer:%2% at %3%") %
188 location.m_Function %
189 bufferIndex %
190 location.FileLine()));
191 }
192 else if (bufferIndex >= model->buffers.size())
193 {
194 throw ParseException(
195 boost::str(
196 boost::format("%1% was called with an invalid buffer index. "
197 "buffer index:%2% at %3%") %
198 location.m_Function %
199 bufferIndex %
200 location.FileLine()));
201 }
202 else if (model->buffers[bufferIndex].get() == nullptr)
203 {
204 throw ParseException(
205 boost::str(
206 boost::format("The buffer #%1% is null. %3%") %
207 bufferIndex %
208 location.AsString()));
209 }
210}
211
212#define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
213 CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
214
215void CheckBufferSize(TfLiteParser::BufferRawPtr bufferPtr,
216 const armnn::TensorInfo & tensorInfo,
217 uint32_t bufferId,
218 const CheckLocation & location)
219{
220 if (bufferPtr == nullptr)
221 {
222 throw ParseException(
223 boost::str(
224 boost::format("BufferPtr is null for buffer:%1%. %2%") %
225 bufferId %
226 location.AsString()));
227 }
228 else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
229 tensorInfo.GetNumBytes() > bufferPtr->data.size())
230 {
231 std::stringstream ss;
232 ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
233 << "For tensor: " << tensorInfo.GetShape()
234 << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
235 << tensorInfo.GetNumElements() << " elements. " << location.AsString();
236 throw ParseException(ss.str());
237 }
238}
239
240#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
241 CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
242
243bool IsActivationSupported(tflite::ActivationFunctionType activationType)
244{
245 switch(activationType)
246 {
247 case tflite::ActivationFunctionType_NONE:
248 case tflite::ActivationFunctionType_RELU:
249 case tflite::ActivationFunctionType_RELU6:
250 case tflite::ActivationFunctionType_TANH:
251 {
252 return true;
253 }
254 default:
255 {
256 return false;
257 }
258 }
259}
260
261#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
262 do { \
263 if (IsActivationSupported(OPTION->fused_activation_function) == false) \
264 { \
265 throw ParseException( \
266 boost::str( \
267 boost::format("TfLite parser doesn't suppport fused activation: " \
268 "%1%/%2% in %3% subgraph:%4% operator:%5% at %6%") % \
269 OPTION->fused_activation_function % \
270 tflite::EnumNameActivationFunctionType(\
271 OPTION->fused_activation_function) % \
272 __func__ % \
273 SUBGRAPH_INDEX % \
274 OPERATOR_INDEX % \
275 CHECK_LOCATION().FileLine())); \
276 } \
277 } while(false)
278
279
280std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in)
281{
282 std::vector<unsigned int> result;
283 result.reserve(in.size());
284 for (auto & i : in)
285 {
286 result.push_back(CHECKED_NON_NEGATIVE(i));
287 }
288 return result;
289}
290
291void CalcPadding(uint32_t inputSize,
292 uint32_t filterSize,
293 uint32_t stride,
Pablo Tellof0bd6832019-04-26 17:58:13 +0100294 uint32_t dilation,
telsoa01c577f2c2018-08-31 09:22:23 +0100295 uint32_t& paddingFront,
296 uint32_t& paddingBack,
297 tflite::Padding padding)
298{
299 paddingFront = 0;
300 paddingBack = 0;
301 if (padding == tflite::Padding_SAME)
302 {
303 uint32_t outputSize = (inputSize + stride - 1) / stride;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100304 uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
305 uint32_t temp = (outputSize - 1) * stride + dilatedSize;
telsoa01c577f2c2018-08-31 09:22:23 +0100306 if (temp > inputSize)
307 {
308 paddingFront = (temp - inputSize) / 2;
309 paddingBack = (temp - inputSize) - paddingFront;
310 }
311 }
312}
313
Sadik Armagand109a4d2020-07-28 10:42:13 +0100314armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr,
315 const std::vector<unsigned int>& shapes,
316 const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3},
317 const bool outputTensor = false)
telsoa01c577f2c2018-08-31 09:22:23 +0100318{
319 armnn::DataType type;
320 CHECK_TENSOR_PTR(tensorPtr);
321
322 switch (tensorPtr->type)
323 {
324 case tflite::TensorType_UINT8:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000325 type = armnn::DataType::QAsymmU8;
telsoa01c577f2c2018-08-31 09:22:23 +0100326 break;
327 case tflite::TensorType_FLOAT32:
328 type = armnn::DataType::Float32;
329 break;
Finn Williamsed66d142019-12-06 09:55:55 +0000330 case tflite::TensorType_INT8:
Keith Davis67e6c542020-02-19 10:08:33 +0000331 if (tensorPtr->quantization->zero_point.size() == 1)
Ryan OShea03181ff2020-02-07 17:22:22 +0000332 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000333 // Per-tensor
Ryan OShea03181ff2020-02-07 17:22:22 +0000334 type = armnn::DataType::QAsymmS8;
335 }
336 else
337 {
Keith Davis0c2eeac2020-02-11 16:51:50 +0000338 // Per-channel
Ryan OShea03181ff2020-02-07 17:22:22 +0000339 type = armnn::DataType::QSymmS8;
340 }
Finn Williamsed66d142019-12-06 09:55:55 +0000341 break;
342 case tflite::TensorType_INT16:
Derek Lambertif90c56d2020-01-10 17:14:08 +0000343 type = armnn::DataType::QSymmS16;
Finn Williamsed66d142019-12-06 09:55:55 +0000344 break;
telsoa01c577f2c2018-08-31 09:22:23 +0100345 case tflite::TensorType_INT32:
346 type = armnn::DataType::Signed32;
347 break;
348
349 default:
350 {
351 CheckLocation location = CHECK_LOCATION();
352 throw ParseException(
353 boost::str(
354 boost::format("Unsupported data type %1% = %2% for tensor: %3%. %4%") %
355 tensorPtr->type %
356 tflite::EnumNameTensorType(tensorPtr->type) %
357 tensorPtr->name %
358 location.AsString()));
359 }
360 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100361 std::vector<unsigned int> safeShape = shapes;
Sadik Armagand109a4d2020-07-28 10:42:13 +0100362 bool isDynamic = false;
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100363 if (safeShape.size() == 0)
364 {
365 safeShape.push_back(1);
Sadik Armagand109a4d2020-07-28 10:42:13 +0100366 if (outputTensor)
367 {
368 isDynamic = true;
369 }
Narumol Prangnawarat4818d462019-04-17 11:22:38 +0100370 }
371
Keith Davisd305e1a2020-01-22 11:57:54 +0000372 float quantizationScale = 0.0f;
373 int32_t quantizationOffset = 0;
374
375 if (tensorPtr->quantization.get())
376 {
377 if (tensorPtr->quantization->scale.size() <= 1)
378 {
379 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
380 CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
381
382 if (tensorPtr->quantization->scale.size() == 1)
383 {
384 quantizationScale = tensorPtr->quantization->scale[0];
385 }
386 if (tensorPtr->quantization->zero_point.size() == 1)
387 {
388 // NOTE: we lose precision here when converting from 64 bit to 32
Ryan OShea03181ff2020-02-07 17:22:22 +0000389 // but this is what we support at the moment in ArmNN
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100390 quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000391 }
392
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100393 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100394 safeShape.data());
395 if (isDynamic)
396 {
397 tensorShape = TensorShape(1, false);
398 }
399 armnn::TensorInfo result(tensorShape,
400 type,
401 quantizationScale,
402 quantizationOffset);
Keith Davisd305e1a2020-01-22 11:57:54 +0000403 return result;
404 }
405 else
406 {
407 std::vector<float> quantizationScales;
408 std::vector<int32_t> quantizationOffsets;
409
410 // Scale
411 std::copy(tensorPtr->quantization->scale.begin(),
412 tensorPtr->quantization->scale.end(),
413 std::back_inserter(quantizationScales));
414
Keith Davis0c2eeac2020-02-11 16:51:50 +0000415 // QSymmS8 Per-axis
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100416 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100417 safeShape.data());
418 if (isDynamic)
419 {
420 tensorShape = TensorShape(1, false);
421 }
422 armnn::TensorInfo result(tensorShape,
423 type,
424 quantizationScales,
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100425 dimensionMappings[armnn::numeric_cast<unsigned int>(
Sadik Armagand109a4d2020-07-28 10:42:13 +0100426 tensorPtr->quantization->quantized_dimension)]);
Keith Davisd305e1a2020-01-22 11:57:54 +0000427 return result;
428 }
429 }
430 else
431 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100432 TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
Sadik Armagand109a4d2020-07-28 10:42:13 +0100433 safeShape.data());
434 if (isDynamic)
435 {
436 tensorShape = TensorShape(1, false);
437 }
438 armnn::TensorInfo result(tensorShape,
Keith Davisd305e1a2020-01-22 11:57:54 +0000439 type,
440 quantizationScale,
441 quantizationOffset);
442 return result;
443 }
telsoa01c577f2c2018-08-31 09:22:23 +0100444}
445
Keith Davis0c2eeac2020-02-11 16:51:50 +0000446armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr,
447 const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3})
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000448{
449 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
Keith Davis0c2eeac2020-02-11 16:51:50 +0000450 return ToTensorInfo(tensorPtr, dimensions, dimensionMappings);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +0000451}
452
Sadik Armagand109a4d2020-07-28 10:42:13 +0100453armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr,
454 const bool outputTensor)
455{
456 auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
457 const armnn::PermutationVector& dimensionMappings = {0, 1, 2, 3};
458 return ToTensorInfo(tensorPtr, dimensions, dimensionMappings, outputTensor);
459}
460
telsoa01c577f2c2018-08-31 09:22:23 +0100461template<typename T>
462std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
463CreateConstTensorImpl(TfLiteParser::BufferRawPtr bufferPtr,
464 TfLiteParser::TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +0000465 armnn::TensorInfo& tensorInfo,
466 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +0100467{
Jan Eilers8eb25602020-03-09 12:13:48 +0000468 IgnoreUnused(tensorPtr);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100469 ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
470 ARMNN_ASSERT_MSG(bufferPtr != nullptr,
telsoa01c577f2c2018-08-31 09:22:23 +0100471 boost::str(
472 boost::format("Buffer for buffer:%1% is null") % tensorPtr->buffer).c_str());
473
474 std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000475
476 if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
477 {
478 tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
Matteo Martincighd5b9e642019-01-04 18:01:21 +0000479 armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
480 reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
Matteo Martincigh747ef822018-12-18 09:26:39 +0000481 }
482 else
483 {
484 ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
485 }
486
telsoa01c577f2c2018-08-31 09:22:23 +0100487 return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
488}
489
telsoa01c577f2c2018-08-31 09:22:23 +0100490armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
491{
492 // generate the binding id by shifting the tensor id by 8 bit
493 // and add the subgraph id, which allows 256 subgraphs
494 return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
495}
496
Aron Virginas-Tar70672f62019-01-23 14:00:00 +0000497bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
498{
499 const unsigned int actualSize = actual.GetNumDimensions();
500 if (actualSize != expected.size())
501 {
502 return false;
503 }
504
505 for (unsigned int i = 0u; i < actualSize; i++)
506 {
507 if (expected[i] < 0 ||
508 actual[i] != static_cast<unsigned int>(expected[i]))
509 {
510 return false;
511 }
512 }
513
514 return true;
515}
516
James Conroy05102392020-06-24 15:39:55 +0100517void CheckMatchingQuantization(const TensorInfo& first,
518 const TensorInfo& second,
519 const std::string& descName,
520 std::string const& firstName,
521 std::string const& secondName)
522{
523 if (!first.IsQuantized() ||
524 !second.IsQuantized())
525 {
526 // Not a quantized type, ignore the validation
527 return;
528 }
529
530 DataType firstDataType = first.GetDataType();
531 DataType secondDataType = second.GetDataType();
532
533 if (firstDataType != secondDataType)
534 {
535 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
536 " must be of the same quantized type, " +
537 firstName + " is " + GetDataTypeName(firstDataType) + ", " +
538 secondName + " is " + GetDataTypeName(secondDataType));
539 }
540
541 if (!first.IsTypeSpaceMatch(second))
542 {
543 throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
544 " must have the same quantization space, " +
545 firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
546 " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
547 secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
548 " and scale " + std::to_string(second.GetQuantizationScale()));
549 }
550}
551
telsoa01c577f2c2018-08-31 09:22:23 +0100552} // <anonymous>
553
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100554TfLiteParser::TfLiteParser(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
555: m_Options(options)
556, m_Network(nullptr, nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +0100557, m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParser::ParseUnsupportedOperator)
558{
559 // register supported operators
Sadik Armagan66dedc72019-12-10 16:32:07 +0000560 m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParser::ParseAdd;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000561 m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParser::ParseAveragePool2D;
562 m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParser::ParseBatchToSpaceND;
563 m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParser::ParseConcatenation;
564 m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParser::ParseConv2D;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000565 m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParser::ParseCustomOperator;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000566 m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParser::ParseDepthwiseConv2D;
Finn Williamsed66d142019-12-06 09:55:55 +0000567 m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParser::ParseDequantize;
Derek Lambertif0176992020-04-28 13:37:49 +0100568 m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParser::ParseExp;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000569 m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParser::ParseFullyConnected;
Jan Eilers2f746b32020-07-28 14:00:06 +0100570 m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParser::ParseHardSwish;
Sadik Armagan12239e72020-05-27 11:06:17 +0100571 m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParser::ParseLeakyRelu;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000572 m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParser::ParseLogistic;
573 m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParser::ParseL2Normalization;
574 m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParser::ParseMaxPool2D;
575 m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParser::ParseMaximum;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000576 m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParser::ParseMean;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000577 m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParser::ParseMinimum;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000578 m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParser::ParseMul;
Darshan Patel83fcf982020-05-26 22:22:42 +0530579 m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParser::ParseNeg;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000580 m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParser::ParsePack;
581 m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParser::ParsePad;
582 m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParser::ParseQuantize;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000583 m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParser::ParseRelu;
584 m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParser::ParseRelu6;
585 m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParser::ParseReshape;
586 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParser::ParseResizeBilinear;
587 m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParser::ParseResizeNearestNeighbor;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000588 m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParser::ParseSlice;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000589 m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParser::ParseSoftmax;
590 m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParser::ParseSpaceToBatchND;
Sadik Armagan66dedc72019-12-10 16:32:07 +0000591 m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParser::ParseSplit;
Derek Lambertif0176992020-04-28 13:37:49 +0100592 m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParser::ParseSplitV;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000593 m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParser::ParseSqueeze;
594 m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParser::ParseStridedSlice;
595 m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParser::ParseSub;
Sadik Armagana3b31f02019-12-05 09:08:53 +0000596 m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParser::ParseTanH;
597 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParser::ParseTranspose;
598 m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParser::ParseTransposeConv;
599 m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParser::ParseUnpack;
Darshan Patel42b3d7d2020-05-25 22:30:07 +0530600 m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParser::ParseDiv;
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100601 // register supported custom operators
602 m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParser::ParseDetectionPostProcess;
telsoa01c577f2c2018-08-31 09:22:23 +0100603}
604
605void TfLiteParser::ResetParser()
606{
607 m_Network = armnn::INetworkPtr(nullptr, nullptr);
608 m_Model = nullptr;
609 m_SubgraphConnections.clear();
610}
611
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200612void TfLiteParser::AddBroadcastReshapeLayer(size_t subgraphIndex,
613 size_t operatorIndex,
614 IConnectableLayer *layer)
615{
616 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100617 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200618
Derek Lambertiff05cc52019-04-26 13:05:17 +0100619 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
620 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200621
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100622 ARMNN_ASSERT(operatorPtr->inputs.size() > 1);
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200623
624 uint32_t reshapedInputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[0]);
Derek Lambertiff05cc52019-04-26 13:05:17 +0100625 TensorRawPtr tensorPtr = subgraphPtr->tensors[reshapedInputId].get();
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200626 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[1]);
Derek Lambertiff05cc52019-04-26 13:05:17 +0100627 TensorRawPtr tensorPtr1 = subgraphPtr->tensors[inputId].get();
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200628
629 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(tensorPtr);
630 armnn::TensorInfo inputTensorInfo = ToTensorInfo(tensorPtr1);
631
Mike Kellyc5789ca2020-07-06 19:24:15 +0100632 uint32_t inputSlotId = 1;
633 uint32_t reshapeSlotId = 0;
634
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200635 if (inputTensorInfo.GetNumDimensions() < reshapedTensorInfo.GetNumDimensions())
636 {
637 uint32_t id = reshapedInputId;
638 reshapedInputId = inputId;
639 inputId = id;
640
641 reshapedTensorInfo = ToTensorInfo(tensorPtr1);
642 inputTensorInfo = ToTensorInfo(tensorPtr);
Mike Kellyc5789ca2020-07-06 19:24:15 +0100643
644 inputSlotId = 0;
645 reshapeSlotId = 1;
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200646 }
647
648 uint32_t numDimensions = inputTensorInfo.GetNumDimensions();
649
650 std::vector<unsigned> reshapedDim;
651 for (unsigned int i = 0; i < reshapedTensorInfo.GetNumDimensions(); ++i)
652 {
653 reshapedDim.push_back(reshapedTensorInfo.GetShape()[i]);
654 }
655
656 std::vector<unsigned int> reshapedDimensions(numDimensions, 1);
657 std::copy_backward (reshapedDim.begin(), reshapedDim.end(), reshapedDimensions.end());
658
659 reshapedTensorInfo.SetShape(armnn::TensorShape{ numDimensions, reshapedDimensions.data() });
660
661 std::string layerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName());
662 armnn::ReshapeDescriptor desc;
663 desc.m_TargetShape = reshapedTensorInfo.GetShape();
664 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
665
666 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
Mike Kellyc5789ca2020-07-06 19:24:15 +0100667 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(reshapeSlotId));
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200668
669 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {reshapedInputId});
670
Mike Kellyc5789ca2020-07-06 19:24:15 +0100671 armnn::IInputSlot* input1Slot = &(layer->GetInputSlot(inputSlotId));
Bruno Goncalves9c761a62018-12-27 14:20:35 -0200672 RegisterConsumerOfTensor(subgraphIndex, inputId, input1Slot);
673}
674
telsoa01c577f2c2018-08-31 09:22:23 +0100675INetworkPtr TfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
676{
677 ResetParser();
678 m_Model = LoadModelFromFile(graphFile);
679 return CreateNetworkFromModel();
680}
681
682INetworkPtr TfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
683{
684 ResetParser();
685 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
686 return CreateNetworkFromModel();
687}
688
689INetworkPtr TfLiteParser::CreateNetworkFromModel()
690{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100691
692 using NetworkOptions = std::vector<BackendOptions>;
693 NetworkOptions networkOptions = {};
694 if (m_Options && m_Options.value().m_InferAndValidate)
695 {
696 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
697 {
698 { "InferAndValidate", true }
699 });
700
701 networkOptions.push_back(shapeInferenceMethodOption);
702 }
703
704 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100705 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100706
telsoa01c577f2c2018-08-31 09:22:23 +0100707 if (m_Model->subgraphs.size() != 1)
708 {
709 throw ParseException(
710 boost::str(
711 boost::format("Current TfLite parser only supports 1 subgraph. Current one has: %1% %2%") %
712 m_Model->subgraphs.size() %
713 CHECK_LOCATION().AsString()));
714 }
715
716 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100717 size_t operatorIndex = 0;
718 try
telsoa01c577f2c2018-08-31 09:22:23 +0100719 {
Colm Donelan6350d272020-06-09 16:56:25 +0100720 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100721 {
Colm Donelan6350d272020-06-09 16:56:25 +0100722 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
723 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100724 {
Colm Donelan6350d272020-06-09 16:56:25 +0100725 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
telsoa01c577f2c2018-08-31 09:22:23 +0100726 auto builtinCode = opCodePtr->builtin_code;
727
728 if (builtinCode > tflite::BuiltinOperator_MAX)
729 {
Colm Donelan6350d272020-06-09 16:56:25 +0100730 throw ParseException(boost::str(boost::format("Operator code %1% is out of range 0-%2%. "
731 "subgraph:%3% operator idx:%4%. %5%") %
732 builtinCode % tflite::BuiltinOperator_MAX % subgraphIndex %
733 operatorIndex % CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100734 }
735
736 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100737 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100738 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100739 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100740 }
telsoa01c577f2c2018-08-31 09:22:23 +0100741
Colm Donelan6350d272020-06-09 16:56:25 +0100742 SetupInputLayers(subgraphIndex);
743 SetupOutputLayers(subgraphIndex);
744 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100745
Colm Donelan6350d272020-06-09 16:56:25 +0100746 ++subgraphIndex;
747 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100748 }
telsoa01c577f2c2018-08-31 09:22:23 +0100749 }
Colm Donelan6350d272020-06-09 16:56:25 +0100750 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100751 {
Colm Donelan6350d272020-06-09 16:56:25 +0100752 std::stringstream errorString;
753 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
754 << subgraphIndex << " error: " << e.what();
755 ARMNN_LOG(error) << errorString.str();
756 std::stringstream errors;
757 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100758 throw ParseException(errors.str());
759 }
760
761 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100762 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100763 {
764 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
765 {
766 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
767 {
768 for (size_t inputSlotIdx = 0;
769 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
770 ++inputSlotIdx)
771 {
772 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
773 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
774 }
775 }
776 }
777 }
778
779 return std::move(m_Network);
780}
781
782void TfLiteParser::RegisterProducerOfTensor(size_t subgraphIndex,
783 size_t tensorIndex,
784 armnn::IOutputSlot* slot)
785{
786 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100787 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
788 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100789
790 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
791
792 // assuming there is only one producer for that tensor
793 if (tensorSlots.outputSlot != nullptr)
794 {
795 throw ParseException(boost::str(
796 boost::format("Another layer has already registered itself as the producer of "
797 "subgraph:%1% tensor:%2% %3%") %
798 subgraphIndex %
799 tensorIndex %
800 CHECK_LOCATION().AsString()));
801 }
802
803 tensorSlots.outputSlot = slot;
804}
805
806void TfLiteParser::RegisterConsumerOfTensor(size_t subgraphIndex,
807 size_t tensorIndex,
808 armnn::IInputSlot* slot)
809{
810 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100811 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
812 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100813
814 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
815 tensorSlots.inputSlots.push_back(slot);
816}
817
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100818void TfLiteParser::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
819{
820 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
821
822 // NOTE: By default we presume the custom operator is not supported
823 auto customParserFunction = &TfLiteParser::ParseUnsupportedOperator;
824
825 // Identify custom code defined for custom operator
826 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
827 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
828
829 // Find parser function that correspondes to custom code (if any)
830 auto iterator = m_CustomParserFunctions.find(customCode);
831 if (iterator != m_CustomParserFunctions.end())
832 {
833 customParserFunction = iterator->second;
834 }
835
836 // Run parser function
837 (this->*customParserFunction)(subgraphIndex, operatorIndex);
838}
839
telsoa01c577f2c2018-08-31 09:22:23 +0100840void TfLiteParser::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
841{
842 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100843
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100844 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
845
846 auto opcodeIndex = operatorPtr->opcode_index;
847 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
848
849 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
850 {
851 // Do not add StandInLayer, throw ParseException instead
852 throw ParseException(
853 boost::str(
854 boost::format("Operator not supported. "
855 "subgraph:%1% operator:%2% "
856 "opcode_index:%3% opcode:%4% / %5% %6%") %
857 subgraphIndex %
858 operatorIndex %
859 opcodeIndex %
860 opcode %
861 tflite::EnumNameBuiltinOperator(opcode) %
862 CHECK_LOCATION().AsString()));
863 }
864
865 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
866 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
867
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100868 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
869 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100870
871 StandInDescriptor descriptor(numInputs, numOutputs);
872 auto layerName = boost::str(boost::format("StandIn:%1%:%2%:%3%") % subgraphIndex % operatorIndex % opcode);
873
874 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
875 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +0100876 ARMNN_ASSERT(layer != nullptr);
877
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100878 for (unsigned int i = 0u; i < numOutputs; ++i)
879 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100880 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100881 }
882
883 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
884 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
885
886 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
887 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +0100888}
889
telsoa01c577f2c2018-08-31 09:22:23 +0100890void TfLiteParser::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
891{
892 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
893
894 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
895 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
896
897 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
898
899 Convolution2dDescriptor desc;
900 desc.m_BiasEnabled = false;
901 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
902 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000903 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100904 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
905 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000906
telsoa01c577f2c2018-08-31 09:22:23 +0100907 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
908 CHECK_VALID_SIZE(inputs.size(), 2, 3);
909
910 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
911 CHECK_VALID_SIZE(outputs.size(), 1);
912
913 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
914 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
915
916 // assuming input is NHWC
917 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
918 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
919
920 // assuming the filter is OHWI : Output, H, W, Input
921 // which is essentially the same as NHWC
922 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
923 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
924
Pablo Tellof0bd6832019-04-26 17:58:13 +0100925 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
926 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
927 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
928 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +0100929
Matteo Martincigh747ef822018-12-18 09:26:39 +0000930 auto filterTensorAndData = CreateConstTensor(inputs[1],
931 filterTensorInfo,
932 armnn::Optional<armnn::PermutationVector&>());
Matthew Jackson74bf7da2019-08-16 16:51:42 +0100933 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100934
935 auto layerName = boost::str(boost::format("Conv2D:%1%:%2%") % subgraphIndex % operatorIndex);
936
937 if (inputs.size() == 3)
938 {
939 desc.m_BiasEnabled = true;
940 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000941 auto biasTensorAndData = CreateConstTensor(inputs[2],
942 biasTensorInfo,
943 armnn::Optional<armnn::PermutationVector&>());
telsoa01c577f2c2018-08-31 09:22:23 +0100944 layer = m_Network->AddConvolution2dLayer(desc,
945 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100946 Optional<ConstTensor>(biasTensorAndData.first),
telsoa01c577f2c2018-08-31 09:22:23 +0100947 layerName.c_str());
948 }
949 else
950 {
951 layer = m_Network->AddConvolution2dLayer(desc,
952 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100953 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +0100954 layerName.c_str());
955 }
956
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100957 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100958
Sadik Armagand109a4d2020-07-28 10:42:13 +0100959 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +0000960 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +0100961
962 // register the input connection slots for the layer, connections are made after all layers have been created
963 // only the tensors for the inputs are relevant, exclude the const tensors
964 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +0000965 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +0100966
jimfly01c25411c2018-11-14 17:47:22 +0000967 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +0100968 // register the output connection slots for the layer, connections are made after all layers have been created
969 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
970 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
971}
972
973void TfLiteParser::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
974{
975 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
976
977 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
978 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
979
980 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
981
982 DepthwiseConvolution2dDescriptor desc;
983 desc.m_BiasEnabled = false;
984 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
985 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000986 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +0100987 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +0100988
989 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
990 CHECK_VALID_SIZE(inputs.size(), 2, 3);
991 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
992 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +0100993 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
994 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000995
Keith Davis0c2eeac2020-02-11 16:51:50 +0000996 // Mappings from TensorflowLite filter tensors to the ArmNN filter tensors (ArmNN weights have to be [M, I, H, W])
997 PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, H, W]
998
telsoa01c577f2c2018-08-31 09:22:23 +0100999 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Keith Davis0c2eeac2020-02-11 16:51:50 +00001000 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1], permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01001001
Matteo Martincigh747ef822018-12-18 09:26:39 +00001002 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +01001003 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1004 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +00001005
1006 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +01001007 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1008 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1009
Matteo Martincigh747ef822018-12-18 09:26:39 +00001010 // Reshape weights as [ H, W, I, M ]
1011 filterTensorInfo.SetShape({ filterHeight,
1012 filterWidth,
1013 inputTensorInfo.GetShape()[3],
1014 filterTensorInfo.GetShape()[3] / inputTensorInfo.GetShape()[3] });
1015
Pablo Tellof0bd6832019-04-26 17:58:13 +01001016 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1017 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1018 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1019 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +01001020
Matteo Martincigh747ef822018-12-18 09:26:39 +00001021 auto filterTensorAndData = CreateConstTensor(inputs[1], filterTensorInfo, permutationVector);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001022 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +01001023 auto layerName = boost::str(boost::format("DepthwiseConv2D:%1%:%2%") % subgraphIndex % operatorIndex);
1024
1025 if (inputs.size() == 3)
1026 {
1027 desc.m_BiasEnabled = true;
1028 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +00001029 auto biasTensorAndData = CreateConstTensor(inputs[2],
1030 biasTensorInfo,
1031 armnn::Optional<armnn::PermutationVector&>());
telsoa01c577f2c2018-08-31 09:22:23 +01001032 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1033 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001034 Optional<ConstTensor>(biasTensorAndData.first),
telsoa01c577f2c2018-08-31 09:22:23 +01001035 layerName.c_str());
1036 }
1037 else
1038 {
1039 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1040 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01001041 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +01001042 layerName.c_str());
1043 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001044 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001045
Sadik Armagand109a4d2020-07-28 10:42:13 +01001046 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +00001047 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +01001048
1049 // register the input connection slots for the layer, connections are made after all layers have been created
1050 // only the tensors for the inputs are relevant, exclude the const tensors
1051 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001052 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +01001053
jimfly01c25411c2018-11-14 17:47:22 +00001054 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +01001055 // register the output connection slots for the layer, connections are made after all layers have been created
1056 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1057 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1058}
1059
Finn Williamsed66d142019-12-06 09:55:55 +00001060void TfLiteParser::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
1061{
1062 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1063
1064 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1065 CHECK_VALID_SIZE(inputs.size(), 1);
1066
1067 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1068 CHECK_VALID_SIZE(outputs.size(), 1);
1069
1070 auto layerName = boost::str(boost::format("Dequantize:%1%:%2%") % subgraphIndex % operatorIndex);
1071
1072 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001073 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001074
Sadik Armagand109a4d2020-07-28 10:42:13 +01001075 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001076 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1077
1078 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1079 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1080
1081 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1082 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1083}
1084
Derek Lambertif0176992020-04-28 13:37:49 +01001085void TfLiteParser::ParseExp(size_t subgraphIndex, size_t operatorIndex)
1086{
1087 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1088
1089 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1090 CHECK_VALID_SIZE(inputs.size(), 1);
1091
1092 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1093 CHECK_VALID_SIZE(outputs.size(), 1);
1094
1095 auto layerName = boost::str(boost::format("Exp:%1%:%2%") % subgraphIndex % operatorIndex);
1096
1097 ElementwiseUnaryDescriptor desc;
1098 desc.m_Operation = UnaryOperation::Exp;
1099 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerName.c_str());
1100 ARMNN_ASSERT(layer != nullptr);
1101
Sadik Armagand109a4d2020-07-28 10:42:13 +01001102 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Derek Lambertif0176992020-04-28 13:37:49 +01001103 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1104
1105 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1106 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1107
1108 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1109 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1110}
1111
Keith Davis4cd29a02019-09-09 14:49:20 +01001112void TfLiteParser::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
1113{
1114 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1115
1116 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001117 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001118
1119 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1120 CHECK_VALID_SIZE(outputs.size(), 1);
1121
Keith Davis4cd29a02019-09-09 14:49:20 +01001122 auto layerName = boost::str(boost::format("Transpose:%1%:%2%") % subgraphIndex % operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001123 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001124
josh minorba424d22019-11-13 10:55:17 -06001125 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001126 {
1127 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1128 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001129 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1130 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001131 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001132 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001133
Mike Kelly08759e22020-03-02 11:41:31 +00001134 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001135 }
1136
James Conroy05102392020-06-24 15:39:55 +01001137 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001138 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001139 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001140
James Conroy05102392020-06-24 15:39:55 +01001141 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001142 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001143 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1144
1145 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1146 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1147
1148 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1149 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1150}
1151
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001152void TfLiteParser::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
1153{
1154 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1155
1156 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1157 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1158
1159 TransposeConvolution2dDescriptor desc;
1160 desc.m_BiasEnabled = false;
1161 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1162 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1163 desc.m_DataLayout = armnn::DataLayout::NHWC;
1164
1165 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001166 CHECK_VALID_SIZE(inputs.size(), 3);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001167
1168 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1169 CHECK_VALID_SIZE(outputs.size(), 1);
1170
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001171 if (inputs[0])
1172 {
1173 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1174 std::vector<int> output_shape(tensorInfo.GetNumElements());
1175 if (tensorInfo.GetDataType() == DataType::Signed32)
1176 {
1177 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1178 }
1179 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1180 {
1181 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1182 {
1183 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1184 }
1185 }
1186 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1187 for (int dimension : output_shape)
1188 {
1189 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1190 }
1191 desc.m_OutputShapeEnabled = true;
1192 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001193 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001194 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1195
1196 // TfLite uses NHWC tensors
1197 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1198 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1199
1200 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1201 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1202
1203 CalcPadding(inputHeight,
1204 filterHeight,
1205 desc.m_StrideY,
1206 1, // DilationY
1207 desc.m_PadTop,
1208 desc.m_PadBottom,
1209 options->padding);
1210
1211 CalcPadding(inputWidth,
1212 filterWidth,
1213 desc.m_StrideX,
1214 1, // DilationX
1215 desc.m_PadLeft,
1216 desc.m_PadRight,
1217 options->padding);
1218
1219 auto filterTensorAndData = CreateConstTensor(inputs[1],
1220 filterTensorInfo,
1221 armnn::Optional<armnn::PermutationVector&>());
1222
1223 armnn::IConnectableLayer* layer = nullptr;
1224 auto layerName = boost::str(boost::format("TransposeConv:%1%:%2%") % subgraphIndex % operatorIndex);
1225
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001226 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1227 filterTensorAndData.first,
1228 EmptyOptional(),
1229 layerName.c_str());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001230
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001231 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001232
Sadik Armagand109a4d2020-07-28 10:42:13 +01001233 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001234 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1235
1236 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1237 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001238 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001239
1240 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1241 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1242}
1243
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001244void TfLiteParser::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
1245{
1246 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1247}
1248
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001249void TfLiteParser::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
1250{
1251 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1252
1253 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1254 CHECK_VALID_SIZE(inputs.size(), 3);
1255
1256 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1257 CHECK_VALID_SIZE(outputs.size(), 1);
1258
1259 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1260 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1261
1262 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1263 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1264
1265 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1266 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1267
1268 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1269 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1270
1271 size_t step = 2;
1272 std::vector<std::pair<unsigned int, unsigned int>> crops;
1273 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1274 {
1275 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1276 }
1277
1278 armnn::BatchToSpaceNdDescriptor desc;
1279 desc.m_BlockShape = blockShape;
1280 desc.m_Crops = crops;
1281 desc.m_DataLayout = armnn::DataLayout::NHWC;
1282
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001283 auto layerName = boost::str(boost::format("BatchToSpaceND:%1%:%2%") % subgraphIndex % operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001284
James Conroy05102392020-06-24 15:39:55 +01001285 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001286 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001287 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1288
1289 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1290 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001291 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1292
1293 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1294 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1295
1296 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1297 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1298}
1299
Matthew Jackson28c94572019-07-18 10:47:03 +01001300void TfLiteParser::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
1301{
1302 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1303
1304 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1305 CHECK_VALID_SIZE(inputs.size(), 1);
1306
1307 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1308 CHECK_VALID_SIZE(outputs.size(), 1);
1309
1310 L2NormalizationDescriptor desc;
1311 desc.m_DataLayout = armnn::DataLayout::NHWC;
1312 auto layerName = boost::str(boost::format("L2Normalization:%1%:%2%") % subgraphIndex % operatorIndex);
1313 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1314
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001315 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001316
Sadik Armagand109a4d2020-07-28 10:42:13 +01001317 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001318 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1319
1320 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1321 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1322
1323 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1324 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1325}
1326
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001327void TfLiteParser::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
1328{
1329 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1330}
1331
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001332void TfLiteParser::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
1333{
1334 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1335
1336 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1337 CHECK_VALID_SIZE(inputs.size(), 2);
1338
1339 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1340 CHECK_VALID_SIZE(outputs.size(), 1);
1341
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001342 auto layerName = boost::str(boost::format("Maximum:%1%:%2%") % subgraphIndex % operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001343
1344 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1345 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1346 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001347
Sadik Armagand109a4d2020-07-28 10:42:13 +01001348 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001349 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1350
1351 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1352 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001353 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1354
1355 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1356 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1357 {
1358 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1359 }
1360 else
1361 {
1362 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1363 }
1364
1365 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1366 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1367}
1368
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001369void TfLiteParser::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
1370{
1371 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1372
1373 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1374 CHECK_VALID_SIZE(inputs.size(), 2);
1375
1376 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1377 CHECK_VALID_SIZE(outputs.size(), 1);
1378
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001379 auto layerName = boost::str(boost::format("Minimum:%1%:%2%") % subgraphIndex % operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001380
1381 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1382 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1383 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001384
Sadik Armagand109a4d2020-07-28 10:42:13 +01001385 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001386 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1387
1388 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1389 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001390 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1391
1392 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1393 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1394 {
1395 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1396 }
1397 else
1398 {
1399 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1400 }
1401
1402 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1403 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1404}
1405
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001406void TfLiteParser::ParsePool(size_t subgraphIndex,
1407 size_t operatorIndex,
1408 PoolingAlgorithm algorithm)
1409{
1410 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1411
1412 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1413 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1414
1415 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1416
1417 std::string layerName;
1418
1419 switch (algorithm)
1420 {
1421 case PoolingAlgorithm::Average:
1422 layerName =
1423 boost::str(boost::format("AveragePool2D:%1%:%2%") % subgraphIndex % operatorIndex);
1424 break;
1425 case PoolingAlgorithm::Max:
1426 layerName =
1427 boost::str(boost::format("MaxPool2D:%1%:%2%") % subgraphIndex % operatorIndex);
1428 break;
1429 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001430 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001431 }
1432
1433 Pooling2dDescriptor desc;
1434
1435 desc.m_PoolType = algorithm;
1436 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1437 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1438 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1439 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1440 desc.m_PaddingMethod = PaddingMethod::Exclude;
1441 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001442 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001443
1444 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1445 CHECK_VALID_SIZE(inputs.size(), 1);
1446 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1447
1448 // assuming input is NHWC
1449 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1450 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1451
Pablo Tellof0bd6832019-04-26 17:58:13 +01001452 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1453 desc.m_PadTop, desc.m_PadBottom, options->padding);
1454 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1455 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001456
1457 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1458 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001459
Sadik Armagand109a4d2020-07-28 10:42:13 +01001460 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001461 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1462
1463 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1464 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001465 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001466
1467 // register the input connection slots for the layer, connections are made after all layers have been created
1468 // only the tensors for the inputs are relevant, exclude the const tensors
1469 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001470 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001471
jimfly01c25411c2018-11-14 17:47:22 +00001472 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001473 // register the output connection slots for the layer, connections are made after all layers have been created
1474 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1475 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1476}
1477
josh minorba424d22019-11-13 10:55:17 -06001478void TfLiteParser::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
1479{
1480 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1481
1482 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1483 CHECK_VALID_SIZE(inputs.size(), 3);
1484 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1485 CHECK_VALID_SIZE(outputs.size(), 1);
1486
1487 SliceDescriptor desc;
1488
1489 // set begin tensor info for slice descriptor
1490 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1491 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1492
1493 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1494 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1495
1496 // set size tensor info for slice descriptor
1497 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1498 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1499
1500 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1501 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1502 desc = SliceDescriptor(begin, size);
1503
1504 auto layerName = boost::str(boost::format("Slice:%1%:%2%") % subgraphIndex % operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001505
James Conroy05102392020-06-24 15:39:55 +01001506 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001507 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001508 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1509
1510 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001511 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1512
1513 // register the input connection slots for the layer, connections are made after all layers have been created
1514 // only the tensors for the inputs are relevant, exclude the const tensors
1515 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1516 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1517
1518 // register the output connection slots for the layer, connections are made after all layers have been created
1519 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1520 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1521}
1522
telsoa01c577f2c2018-08-31 09:22:23 +01001523void TfLiteParser::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
1524{
1525 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1526 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1527 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1528
1529 SoftmaxDescriptor desc;
1530 desc.m_Beta = options->beta;
1531
1532 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1533 CHECK_VALID_SIZE(inputs.size(), 1);
1534 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1535 CHECK_VALID_SIZE(outputs.size(), 1);
1536
1537 auto layerName = boost::str(boost::format("Softmax:%1%:%2%") % subgraphIndex % operatorIndex);
1538 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1539
Sadik Armagand109a4d2020-07-28 10:42:13 +01001540 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001541 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1542
1543 // register the input connection slots for the layer, connections are made after all layers have been created
1544 // only the tensors for the inputs are relevant, exclude the const tensors
1545 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1546 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1547
1548 // register the output connection slots for the layer, connections are made after all layers have been created
1549 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1550 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1551}
1552
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001553void TfLiteParser::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
1554{
1555 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1556
1557 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1558 CHECK_VALID_SIZE(inputs.size(), 3);
1559
1560 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1561 CHECK_VALID_SIZE(outputs.size(), 1);
1562
1563 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1564 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1565
1566 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1567 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1568
1569 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1570 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1571
1572 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1573 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1574
1575 size_t step = 2;
1576 std::vector<std::pair<unsigned int, unsigned int>> padList;
1577 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1578 {
1579 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1580 }
1581
1582 armnn::SpaceToBatchNdDescriptor desc;
1583 desc.m_BlockShape = blockShape;
1584 desc.m_PadList = padList;
1585 desc.m_DataLayout = armnn::DataLayout::NHWC;
1586
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001587 auto layerName = boost::str(boost::format("SpaceToBatchND:%1%:%2%") % subgraphIndex % operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001588
James Conroy05102392020-06-24 15:39:55 +01001589 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001590 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001591 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1592
1593 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1594 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001595 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1596
1597 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1598 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1599
1600 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1601 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1602}
1603
telsoa01c577f2c2018-08-31 09:22:23 +01001604armnn::TensorInfo TfLiteParser::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn,
1605 const armnn::TensorInfo & inputTensorInfo)
1606{
1607 CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4);
1608 std::vector<uint32_t> squeezeDims = squeezeDimsIn;
1609 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1610
1611 if (inputTensorInfo.GetNumDimensions() > 4)
1612 {
1613 std::stringstream ss;
1614 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1615 << " shape:" << inputTensorInfo.GetShape() << " "
1616 << CHECK_LOCATION().AsString();
1617 throw ParseException(ss.str());
1618 }
1619
1620 if (squeezeDims.empty())
1621 {
1622 squeezeDims.assign(dimensionSequence,
1623 dimensionSequence+inputTensorInfo.GetNumDimensions());
1624 }
1625
1626 std::vector<uint32_t> outputDims;
1627 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1628 {
1629 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1630 auto currentDimension = inputTensorInfo.GetShape()[i];
1631 if (skipSqueeze || currentDimension != 1)
1632 {
1633 outputDims.push_back(currentDimension);
1634 }
1635 }
1636
1637 if (outputDims.size() > 4)
1638 {
1639 std::stringstream ss;
1640 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1641 << " shape:" << inputTensorInfo.GetShape() << " "
1642 << CHECK_LOCATION().AsString();
1643 throw ParseException(ss.str());
1644 }
1645
1646 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1647 outputDims.data());
1648
1649 // we need to preserve the tensor type and the quantization data as well
1650 TensorInfo outTensorInfo = inputTensorInfo;
1651 outTensorInfo.SetShape(outShape);
1652
1653 return outTensorInfo;
1654}
1655
1656void TfLiteParser::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
1657{
1658 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1659
1660 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1661 CHECK_VALID_SIZE(inputs.size(), 1);
1662
1663 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1664 CHECK_VALID_SIZE(outputs.size(), 1);
1665
1666 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1667 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Conroy05102392020-06-24 15:39:55 +01001668 auto layerName = boost::str(boost::format("Squeeze:%1%:%2%") % subgraphIndex % operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001669
1670 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1671 armnn::TensorInfo outputTensorInfo =
1672 TfLiteParser::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims),
1673 inputTensorInfo);
James Conroy05102392020-06-24 15:39:55 +01001674 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01001675
1676 ReshapeDescriptor reshapeDesc;
1677 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1678
telsoa01c577f2c2018-08-31 09:22:23 +01001679 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001680 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001681 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1682
1683 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1684 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1685
1686 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1687 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1688}
1689
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001690void TfLiteParser::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
1691{
1692 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1693
1694 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1695 CHECK_VALID_SIZE(inputs.size(), 4);
1696
1697 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1698 CHECK_VALID_SIZE(outputs.size(), 1);
1699
1700 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1701 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1702
1703 StridedSliceDescriptor desc;
1704 desc.m_BeginMask = options->begin_mask;
1705 desc.m_EllipsisMask = options->ellipsis_mask;
1706 desc.m_EndMask = options->end_mask;
1707 desc.m_NewAxisMask = options->new_axis_mask;
1708 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
1709 desc.m_DataLayout = armnn::DataLayout::NHWC;
1710
1711 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1712 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1713
1714 std::vector<int> begin(beginTensorInfo.GetNumElements());
1715 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1716
1717 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
1718 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1719
1720 std::vector<int> end(endTensorInfo.GetNumElements());
1721 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1722
1723 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
1724 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
1725
1726 std::vector<int> stride(strideTensorInfo.GetNumElements());
1727 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1728
1729 desc.m_Begin = begin;
1730 desc.m_End = end;
1731 desc.m_Stride = stride;
1732
1733 auto layerName = boost::str(boost::format("StridedSlice:%1%:%2%") % subgraphIndex % operatorIndex);
1734 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001735 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001736
Sadik Armagand109a4d2020-07-28 10:42:13 +01001737 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001738 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1739
1740 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1741 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1742
1743 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1744 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1745}
1746
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001747void TfLiteParser::ParseSub(size_t subgraphIndex, size_t operatorIndex)
1748{
1749 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1750
1751 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1752 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1753
1754 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1755 CHECK_VALID_SIZE(inputs.size(), 2);
1756
1757 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1758 CHECK_VALID_SIZE(outputs.size(), 1);
1759
1760 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1761 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1762
1763 auto layerName = boost::str(boost::format("Sub:%1%:%2%") % subgraphIndex % operatorIndex);
1764 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001765 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001766
Sadik Armagand109a4d2020-07-28 10:42:13 +01001767 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001768 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1769
1770 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1771 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1772 {
1773 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1774 }
1775 else
1776 {
1777 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1778 }
1779
1780 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1781
1782 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1783 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1784}
1785
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301786void TfLiteParser::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
1787{
1788 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1789
1790 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1791 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1792
1793 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1794 CHECK_VALID_SIZE(inputs.size(), 2);
1795
1796 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1797 CHECK_VALID_SIZE(outputs.size(), 1);
1798
1799 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1800 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1801
1802 auto layerName = boost::str(boost::format("Div:%1%:%2%") % subgraphIndex % operatorIndex);
1803 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001804 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301805
Sadik Armagand109a4d2020-07-28 10:42:13 +01001806 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301807 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1808
1809 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1810 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1811 {
1812 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1813 }
1814 else
1815 {
1816 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1817 }
1818
1819 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1820
1821 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1822 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1823}
1824
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001825void TfLiteParser::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
1826{
1827 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1828
1829 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1830 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1831
1832 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1833 CHECK_VALID_SIZE(inputs.size(), 2);
1834
1835 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1836 CHECK_VALID_SIZE(outputs.size(), 1);
1837
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001838 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1839 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1840
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001841 auto layerName = boost::str(boost::format("Add:%1%:%2%") % subgraphIndex % operatorIndex);
1842 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001843 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001844
Sadik Armagand109a4d2020-07-28 10:42:13 +01001845 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001846 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1847
1848 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001849 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1850 {
1851 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1852 }
1853 else
1854 {
1855 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1856 }
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001857
1858 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1859
1860 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1861 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1862}
1863
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001864void TfLiteParser::ParseMul(size_t subgraphIndex, size_t operatorIndex)
1865{
1866 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1867
1868 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1869 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1870
1871 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1872 CHECK_VALID_SIZE(inputs.size(), 2);
1873
1874 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1875 CHECK_VALID_SIZE(outputs.size(), 1);
1876
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001877 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1878 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1879
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001880 auto layerName = boost::str(boost::format("Mul:%1%:%2%") % subgraphIndex % operatorIndex);
1881 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001882 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001883
Sadik Armagand109a4d2020-07-28 10:42:13 +01001884 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001885 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1886
1887 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001888 if (inputTensorInfo.GetNumDimensions() != input1TensorInfo.GetNumDimensions())
1889 {
1890 AddBroadcastReshapeLayer(subgraphIndex, operatorIndex, layer);
1891 }
1892 else
1893 {
1894 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1895 }
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001896
1897 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1898
1899 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1900 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1901}
1902
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001903void TfLiteParser::ParseMean(size_t subgraphIndex, size_t operatorIndex)
1904{
1905 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1906
1907 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1908
1909 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1910 CHECK_VALID_SIZE(outputs.size(), 1);
1911
1912 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
1913 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1914
1915 armnn::MeanDescriptor desc;
1916 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
1917 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
1918 desc.m_Axis = axis;
1919
1920 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001921 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001922
1923 desc.m_KeepDims =
1924 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
1925 true : false;
1926
1927 auto layerName = boost::str(boost::format("Mean:%1%:%2%") % subgraphIndex % operatorIndex);
1928 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001929 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001930
1931 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1932
1933 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1934 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1935
1936 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1937 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1938}
1939
Darshan Patel83fcf982020-05-26 22:22:42 +05301940void TfLiteParser::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
1941{
1942 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1943
1944 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1945 CHECK_VALID_SIZE(inputs.size(), 1);
1946
1947 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1948 CHECK_VALID_SIZE(outputs.size(), 1);
1949
1950 auto layerName = boost::str(boost::format("Neg:%1%:%2%") % subgraphIndex % operatorIndex);
1951 armnn::ElementwiseUnaryDescriptor descriptor(armnn::UnaryOperation::Neg);
1952 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1953 ARMNN_ASSERT(layer != nullptr);
1954
Sadik Armagand109a4d2020-07-28 10:42:13 +01001955 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel83fcf982020-05-26 22:22:42 +05301956 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1957
1958 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1959 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1960
1961 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1962 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1963}
1964
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001965void TfLiteParser::ParsePad(size_t subgraphIndex, size_t operatorIndex)
1966{
1967 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1968
1969 TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1970
1971 TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1972 CHECK_VALID_SIZE(outputs.size(), 1);
1973
1974 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
1975 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1976
1977 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
1978 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
1979
1980 size_t step = 2;
1981 armnn::PadDescriptor desc;
1982 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
1983 {
1984 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
1985 }
1986
1987 auto layerName = boost::str(boost::format("Pad:%1%:%2%") % subgraphIndex % operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001988 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001989
1990 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
1991 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001992 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1993
1994 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1995 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1996
1997 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1998 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1999}
2000
Sadik Armagan66dedc72019-12-10 16:32:07 +00002001void TfLiteParser::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
2002{
2003 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2004
2005 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2006 CHECK_VALID_SIZE(inputs.size(), 1);
2007
2008 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2009 CHECK_VALID_SIZE(outputs.size(), 1);
2010
2011 auto layerName = boost::str(boost::format("Quantize:%1%:%2%") % subgraphIndex % operatorIndex);
2012
2013 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002014 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002015
Sadik Armagand109a4d2020-07-28 10:42:13 +01002016 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00002017 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2018
2019 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2020 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2021
2022 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2023 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2024}
Finn Williamsc42c3842019-01-22 14:18:11 +00002025
Sadik Armagan58f39192018-09-17 14:14:39 +01002026void TfLiteParser::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
2027{
Finn Williamsc42c3842019-01-22 14:18:11 +00002028 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01002029}
2030
2031void TfLiteParser::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
2032{
Finn Williamsc42c3842019-01-22 14:18:11 +00002033 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
2034}
Sadik Armagan58f39192018-09-17 14:14:39 +01002035
Sadik Armagan12239e72020-05-27 11:06:17 +01002036void TfLiteParser::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
2037{
Jan Eilers2f746b32020-07-28 14:00:06 +01002038 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01002039}
2040
Finn Williamsc42c3842019-01-22 14:18:11 +00002041void TfLiteParser::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
2042{
2043 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
2044}
2045
Nina Drozd99851762019-04-09 09:37:38 +01002046void TfLiteParser::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
2047{
2048 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
2049}
2050
Jan Eilers2f746b32020-07-28 14:00:06 +01002051void TfLiteParser::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
2052{
2053 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2054}
Finn Williamsc42c3842019-01-22 14:18:11 +00002055
2056void TfLiteParser::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
2057{
2058 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan58f39192018-09-17 14:14:39 +01002059 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00002060 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01002061
2062 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2063 CHECK_VALID_SIZE(inputs.size(), 1);
2064
2065 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2066 CHECK_VALID_SIZE(outputs.size(), 1);
2067
Finn Williamsc42c3842019-01-22 14:18:11 +00002068 auto layerName = str(boost::format("Activation:"));
Sadik Armagan58f39192018-09-17 14:14:39 +01002069 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00002070 activationDesc.m_Function = activationType;
2071
2072 switch (activationType)
2073 {
2074 case ActivationFunction::ReLu:
2075 {
2076 layerName += str(boost::format("RELU:%1%:%2%") % subgraphIndex % operatorIndex);
2077 break;
2078 }
2079 case ActivationFunction::BoundedReLu:
2080 {
2081 layerName += str(boost::format("RELU6:%1%:%2%") % subgraphIndex % operatorIndex);
2082 activationDesc.m_A = 6.0f;
2083 activationDesc.m_B = 0.0f;
2084 break;
2085 }
2086 case ActivationFunction::Sigmoid:
2087 {
2088 layerName += str(boost::format("SIGMOID:%1%:%2%") % subgraphIndex % operatorIndex);
2089 break;
2090 }
Nina Drozd99851762019-04-09 09:37:38 +01002091 case ActivationFunction::TanH:
2092 {
2093 layerName += str(boost::format("TANH:%1%:%2%") % subgraphIndex % operatorIndex);
2094 activationDesc.m_A = 1.0f;
2095 activationDesc.m_B = 1.0f;
2096 break;
2097 }
Sadik Armagan12239e72020-05-27 11:06:17 +01002098 case ActivationFunction::LeakyReLu:
2099 {
2100 layerName += str(boost::format("LEAKYRELU:%1%:%2%") % subgraphIndex % operatorIndex);
2101 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2102 activationDesc.m_A = options->alpha;
2103 break;
2104 }
Jan Eilers2f746b32020-07-28 14:00:06 +01002105 case ActivationFunction::HardSwish:
2106 layerName += str(boost::format("HARDSWISH:%1%:%2%") % subgraphIndex % operatorIndex);
2107 break;
Finn Williamsc42c3842019-01-22 14:18:11 +00002108 default:
2109 {
2110 throw ParseException(
2111 boost::str(boost::format("Unexpected ActivationFunction[%1%] when creating layerName "
2112 " %2% ") %static_cast<int>(activationType)% CHECK_LOCATION().AsString()));
2113 }
2114 }
2115
2116 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002117
Sadik Armagand109a4d2020-07-28 10:42:13 +01002118 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002119 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2120
2121 // register the input connection slots for the layer, connections are made after all layers have been created
2122 // only the tensors for the inputs are relevant, exclude the const tensors
2123 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2124 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2125
2126 // register the output connection slots for the layer, connections are made after all layers have been created
2127 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2128 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2129}
Sadikb94967b2018-09-19 15:30:00 +01002130armnn::TensorInfo TfLiteParser::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
2131 const std::vector<int32_t> & targetDimsIn)
2132{
2133 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2134 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2135
2136 if (stretchDim != targetDimsIn.end())
2137 {
2138 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2139 {
2140 throw ParseException(
2141 boost::str(
2142 boost::format("At most one component of shape can be -1 %1%") % CHECK_LOCATION().AsString()));
2143 }
2144
2145 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002146 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002147 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2148
2149 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2150 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2151 }
2152
2153 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2154
2155 TensorInfo reshapeInfo = inputTensorInfo;
2156 reshapeInfo.SetShape(outputShape);
2157
2158 return reshapeInfo;
2159}
2160
2161void TfLiteParser::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
2162{
2163 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2164
2165 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002166
2167 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2168 CHECK_VALID_SIZE(outputs.size(), 1);
2169
2170 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2171 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
James Conroy05102392020-06-24 15:39:55 +01002172 auto layerName = boost::str(boost::format("Reshape:%1%:%2%") % subgraphIndex % operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002173
2174 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002175 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002176 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002177
Jan Eilersbac9b352020-07-13 13:40:24 +01002178 // Extracting new shape for the output
2179 // There are two ways it can be passed
2180 // * First is to define the target shape in the operator built-in options
2181 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002182 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002183 bool targetShapeFound = false;
2184 // Check if built-in options were given
2185 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002186 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002187 // make sure the parameter is given
2188 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002189 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002190 targetShape = options->new_shape;
2191 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002192 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002193 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002194
2195 // If there is no built-in option given or if the built-in new_shape parameter was empty
2196 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002197 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002198 // Check for a second input tensor
2199 if (inputs.size() > 1 && inputs[1] != nullptr)
2200 {
2201 if (inputs[1]->is_variable)
2202 {
2203 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2204 }
2205
2206 if (inputs[1]->shape.size() != 1)
2207 {
2208 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2209 }
2210
2211 if (inputs[1]->type != tflite::TensorType_INT32)
2212 {
2213 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2214 }
2215
2216 // Extract target shape from input
2217 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2218 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
2219 for (int i=0; i < inputs[1]->shape[0]; ++i)
2220 {
2221 targetShape.push_back(values[i]);
2222 }
2223 }
2224 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002225 {
2226 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2227 "At least one method required");
2228 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002229 }
2230
kevmay0171972a82018-12-17 14:28:03 +00002231 armnn::TensorInfo reshapeOutputTensorInfo =
Derek Lambertic9e52792020-03-11 11:42:26 +00002232 TfLiteParser::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002233
kevmay0171972a82018-12-17 14:28:03 +00002234 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002235 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2236 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002237 {
2238 std::stringstream ss;
2239 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002240 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002241 << " does not equal output shape "
2242 << actualOutputTensorInfo.GetShape()
2243 << ": "
2244 << CHECK_LOCATION().AsString();
2245 throw ParseException(ss.str());
2246 }
2247
Sadikb94967b2018-09-19 15:30:00 +01002248 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002249 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002250
Sadikb94967b2018-09-19 15:30:00 +01002251 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002252 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002253 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002254
2255 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2256 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2257
2258 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2259 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2260}
2261
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002262void TfLiteParser::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
2263{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002264 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2265}
2266
2267void TfLiteParser::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
2268{
2269 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2270}
2271
2272void TfLiteParser::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
2273{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002274 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2275
2276 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2277 CHECK_VALID_SIZE(inputs.size(), 2);
2278
2279 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2280 CHECK_VALID_SIZE(outputs.size(), 1);
2281
2282 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2283
2284 // Data for the parsed tensor args (size) must be stored locally.
2285 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2286
2287 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2288 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2289
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002290 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002291 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002292 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002293 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2294 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002295
Sadik Armagana3b31f02019-12-05 09:08:53 +00002296 auto layerName = str(boost::format("Resize:"));
2297
2298 switch (resizeMethod)
2299 {
2300 case ResizeMethod::Bilinear:
2301 {
2302 layerName += str(boost::format("BILINEAR:%1%:%2%") % subgraphIndex % operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002303
2304 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2305 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2306
David Monahan4a0c9b92020-05-30 09:48:39 +01002307 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002308 break;
2309 }
2310 case ResizeMethod::NearestNeighbor:
2311 {
2312 layerName += str(boost::format("NEARESTNEIGHBOR:%1%:%2%") % subgraphIndex % operatorIndex);
2313 break;
2314 }
2315 default:
2316 {
2317 throw ParseException(
2318 boost::str(boost::format("Unexpected ResizeMethod[%1%] when creating layerName "
2319 " %2% ") %static_cast<int>(resizeMethod)% CHECK_LOCATION().AsString()));
2320 }
2321 }
2322
James Conroy05102392020-06-24 15:39:55 +01002323 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002324 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002325 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2326
2327 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2328 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002329 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2330
2331 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2332 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2333
2334 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2335 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2336}
2337
Sadik Armagan479045b2018-10-01 11:51:37 +01002338void TfLiteParser::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
2339{
2340 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2341
2342 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2343 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2344
2345 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2346
2347 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2348 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2349 CHECK_VALID_SIZE(outputs.size(), 1);
2350
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002351 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2352 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002353
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002354 const unsigned int concatDimInput = static_cast<unsigned int>(
2355 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002356
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002357 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2358 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002359
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002360 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002361
2362 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2363 {
2364 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2365
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002366 // This set up concatDescriptor view origin
2367 armnnUtils::ProcessConcatInputTensorInfo(
2368 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002369 }
2370
2371 auto layerName = boost::str(boost::format("Concatenation:%1%:%2%") % subgraphIndex % operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002372 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002373
Jim Flynn906f9462019-05-10 13:55:21 +01002374 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002375 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002376 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002377
James Conroy05102392020-06-24 15:39:55 +01002378 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002379 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002380
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002381 // add fused activation layer
2382 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002383
Sadik Armagan479045b2018-10-01 11:51:37 +01002384 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2385 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2386}
2387
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002388void TfLiteParser::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
2389{
2390 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2391
2392 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2393 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2394
2395 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2396
2397 FullyConnectedDescriptor desc;
2398 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002399 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002400
2401 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2402 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2403 CHECK_VALID_SIZE(outputs.size(), 1);
2404
2405 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2406
2407 // Fully Connected Layer accepts two dimensional weights input
2408 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2409 if (weightsDimension != 2)
2410 {
2411 throw ParseException(
2412 boost::str(
2413 boost::format(
2414 "Dimension %1% for Fully Connected weights is not supported by Armnn. "
2415 "Node %2%")
2416 % weightsDimension
2417 % CHECK_LOCATION().AsString()));
2418 }
2419
Matteo Martincigh747ef822018-12-18 09:26:39 +00002420 auto filterTensorAndData = CreateConstTensor(inputs[1],
2421 filterTensorInfo,
2422 armnn::Optional<armnn::PermutationVector&>());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002423 armnn::IConnectableLayer* layer = nullptr;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002424 auto layerName = boost::str(boost::format("FullyConnected:%1%:%2%") % subgraphIndex % operatorIndex);
2425
2426 if (inputs.size() == 3)
2427 {
2428 desc.m_BiasEnabled = true;
2429 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +00002430 auto biasTensorAndData = CreateConstTensor(inputs[2],
2431 biasTensorInfo,
2432 armnn::Optional<armnn::PermutationVector&>());
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002433 layer = m_Network->AddFullyConnectedLayer(desc,
2434 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01002435 Optional<ConstTensor>(biasTensorAndData.first),
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002436 layerName.c_str());
2437 }
2438 else
2439 {
2440 layer = m_Network->AddFullyConnectedLayer(desc,
2441 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01002442 EmptyOptional(),
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002443 layerName.c_str());
2444 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002445 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002446
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002447 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2448
2449 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2450
2451 if (inputTensorInfo.GetNumDimensions() > 2)
2452 {
2453 // Add reshape to flatten to 2D [batch_size, input_size],
2454 // where "input_size" corresponds to the number of inputs to the layer,
2455 // matching the second dimension of weights,
2456 // and "batch_size" is calculated by dividing the number of elements by "input_size".
2457 std::vector<unsigned int> reshapedDimensions(2);
2458 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2459 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
2460
2461 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
2462 {
2463 throw ParseException(
2464 boost::str(
2465 boost::format(
2466 "Failed to deduce input tensor shape from filter size %1%")
2467 % reshapedDimensions[1]
2468 % CHECK_LOCATION().AsString()));
2469 }
2470
2471 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
2472 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
2473
2474 std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName());
2475 armnn::ReshapeDescriptor desc;
2476 desc.m_TargetShape = reshapedTensorInfo.GetShape();
2477 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2478
2479 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
2480 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
2481
2482 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
2483 }
2484 else
2485 {
2486 // register the input connection slot for the layer
2487 // only the tensors for the inputs are relevant, exclude the const tensors
2488 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2489 }
2490
Sadik Armagand109a4d2020-07-28 10:42:13 +01002491 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002492 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2493
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002494 // we need to add the activation layer and fortunately we don't need to care about the data layout
2495 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
2496 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002497
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002498 // register the output connection slots for the layer, connections are made after all layers have been created
2499 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2500 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2501}
2502
keidav011b3e2ea2019-02-21 10:07:37 +00002503void TfLiteParser::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
2504{
2505 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2506
2507 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2508
2509 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2510 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2511 CHECK_VALID_SIZE(outputs.size(), 4);
2512
2513 // Obtain custom options from flexbuffers
2514 auto custom_options = operatorPtr->custom_options;
2515 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2516
2517 // Obtain descriptor information from tf lite
2518 DetectionPostProcessDescriptor desc;
2519 desc.m_MaxDetections = m["max_detections"].AsUInt32();
2520 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
2521 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
2522 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
2523 desc.m_NumClasses = m["num_classes"].AsUInt32();
2524 desc.m_ScaleH = m["h_scale"].AsFloat();
2525 desc.m_ScaleW = m["w_scale"].AsFloat();
2526 desc.m_ScaleX = m["x_scale"].AsFloat();
2527 desc.m_ScaleY = m["y_scale"].AsFloat();
2528
keidav0107d58c72019-02-26 11:57:39 +00002529 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00002530 {
keidav0107d58c72019-02-26 11:57:39 +00002531 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00002532 }
2533 if (!(m["detections_per_class"].IsNull()))
2534 {
2535 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
2536 }
2537
2538 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
2539 {
2540 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
2541 "must be positive and less than or equal to 1.");
2542 }
2543
2544 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
2545 auto anchorTensorAndData = CreateConstTensor(inputs[2], anchorTensorInfo,
2546 armnn::Optional<armnn::PermutationVector&>());
2547
2548 auto layerName = boost::str(boost::format("DetectionPostProcess:%1%:%2%") % subgraphIndex % operatorIndex);
2549 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData.first,
2550 layerName.c_str());
2551
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002552 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00002553
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002554 // The model does not specify the output shapes.
2555 // The output shapes are calculated from the max_detection and max_classes_per_detection.
2556 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
2557 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2558 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2559 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2560 m_OverridenOutputShapes.push_back({ 1 });
2561
keidav011b3e2ea2019-02-21 10:07:37 +00002562 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
2563 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002564 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00002565 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
2566 }
2567
2568 // Register the input connection slots for the layer, connections are made after all layers have been created
2569 // only the tensors for the inputs are relevant, exclude the const tensors
2570 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2571 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2572
2573 // Register the output connection slots for the layer, connections are made after all layers have been created
2574 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2575 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2576 outputTensorIndexes[1],
2577 outputTensorIndexes[2],
2578 outputTensorIndexes[3]});
2579}
2580
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002581/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
2582void TfLiteParser::ParsePack(size_t subgraphIndex, size_t operatorIndex)
2583{
2584 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2585
2586 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2587 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2588 CHECK_VALID_SIZE(outputs.size(), 1);
2589
2590 if (inputs.size() < 1)
2591 {
2592 throw ParseException("Pack must have at least one input.");
2593 }
2594
2595 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2596 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2597
2598 StackDescriptor desc;
2599 desc.m_Axis = static_cast<uint32_t>(options->axis);
2600 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
2601
2602 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
2603 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2604 desc.m_InputShape = inputTensorInfo.GetShape();
2605
2606 auto layerName = boost::str(boost::format("Pack:%1%:%2%") % subgraphIndex % operatorIndex);
2607 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
2608
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002609 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002610
Sadik Armagand109a4d2020-07-28 10:42:13 +01002611 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002612 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2613
2614 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2615 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2616
2617 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2618 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2619}
2620
Nina Drozd200e3802019-04-15 09:47:39 +01002621void TfLiteParser::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
2622{
2623 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2624
2625 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2626 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2627
2628 // This unpackAxis indicates the axis to unpack
2629 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
2630
2631 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2632 CHECK_VALID_SIZE(inputs.size(), 1);
2633
2634 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002635
2636 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2637 {
2638 throw ParseException(
2639 boost::str(
2640 boost::format(
2641 "The unpack axis: %1% cannot be greater than or equal to "
2642 "the number of input dimension %2% %3%")
2643 % unpackAxis
2644 % inputTensorInfo.GetNumDimensions()
2645 % CHECK_LOCATION().AsString()));
2646 }
2647
Nina Drozd200e3802019-04-15 09:47:39 +01002648 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
2649 // If num is not defined, automatically infer from the length of the dimension axis.
2650 if(unpackNum == 0)
2651 {
2652 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2653 }
2654
2655 // If unpack number cannot be inferred and is still zero, throw ParseException.
2656 if(unpackNum == 0)
2657 {
2658 throw ParseException("Number to unpack must greater than zero.");
2659 }
2660
2661 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2662 CHECK_VALID_SIZE(outputs.size(), unpackNum);
2663
2664 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2665 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2666
2667 // Add current input shape to unpackDimSizes
2668 for (unsigned int i = 0; i < inputDimSize; ++i)
2669 {
2670 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2671 }
2672
2673 if (unpackDimSizes[unpackAxis] != unpackNum)
2674 {
2675 throw ParseException("Number to unpack must be the same as length of the dimension to "
2676 "unpack along.");
2677 }
2678
2679 unpackDimSizes[unpackAxis] /= unpackNum;
2680
2681 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2682 for (unsigned int j = 0; j < unpackNum; ++j)
2683 {
2684 // Set the size of the views.
2685 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2686 {
2687 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2688 }
2689 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
2690 }
2691
2692 auto layerName = boost::str(boost::format("Unpack:%1%:%2%") % subgraphIndex % operatorIndex);
2693 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002694 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01002695
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002696 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
2697 unpackDimSizes.data());
2698
Nina Drozd200e3802019-04-15 09:47:39 +01002699 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2700 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2701
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002702 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
2703 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2704 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002705 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002706 std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName());
2707 armnn::ReshapeDescriptor desc;
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002708 desc.m_TargetShape = outputTensorInfo.GetShape();
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002709 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2710
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002711 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
2712 outputTensorInfo.GetDataType(),
2713 outputTensorInfo.GetQuantizationScale(),
2714 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002715 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
2716
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002717 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002718
2719 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
2720 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
2721 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2722 }
Nina Drozd200e3802019-04-15 09:47:39 +01002723}
2724
Nina Drozd0324f482019-04-08 10:52:10 +01002725void TfLiteParser::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
2726{
2727 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2728
2729 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2730 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2731
2732 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2733
Nina Drozd200e3802019-04-15 09:47:39 +01002734 // If number of splits cannot be inferred and is zero, throw ParseException.
2735 if(numSplits == 0)
2736 {
2737 throw ParseException("Number to splits must greater than zero.");
2738 }
2739
Nina Drozd0324f482019-04-08 10:52:10 +01002740 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2741 CHECK_VALID_SIZE(inputs.size(), 2);
2742 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2743 CHECK_VALID_SIZE(outputs.size(), numSplits);
2744
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002745 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
2746 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
Nina Drozd0324f482019-04-08 10:52:10 +01002747
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002748 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
2749 std::vector<unsigned int> axisData(axisTensorInfo.GetNumElements());
2750 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2751
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002752 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002753 const unsigned int splitDim = axisData[0];
Nina Drozd0324f482019-04-08 10:52:10 +01002754
Nina Drozd0324f482019-04-08 10:52:10 +01002755 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002756 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01002757 {
2758 throw ParseException(
2759 boost::str(
2760 boost::format(
2761 "The number of dimensions: %1% for input tensors of the "
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002762 "split op cannot be greater than %2% %3%")
Nina Drozd0324f482019-04-08 10:52:10 +01002763 % inputTensorInfo.GetNumDimensions()
2764 % MaxNumOfTensorDimensions
2765 % CHECK_LOCATION().AsString()));
2766 }
2767
2768 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2769
2770 // Add current input shape to splitterDimSizes
2771 for (unsigned int i = 0; i < inputDimSize; ++i)
2772 {
2773 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2774 }
2775
2776 if (splitterDimSizes[splitDim] % numSplits != 0)
2777 {
2778 throw ParseException("Number of splits must evenly divide the dimension");
2779 }
2780 splitterDimSizes[splitDim] /= numSplits;
2781
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002782 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01002783 for (unsigned int j = 0; j < numSplits; ++j)
2784 {
2785 // Set the size of the views.
2786 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2787 {
2788 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2789 }
2790 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
2791 }
2792
2793 auto layerName = boost::str(boost::format("Split:%1%:%2%") % subgraphIndex % operatorIndex);
2794 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002795 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01002796
2797 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002798 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01002799
Nina Drozd0324f482019-04-08 10:52:10 +01002800 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2801 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002802 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01002803 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01002804 }
2805
2806 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2807 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2808}
2809
Derek Lambertif0176992020-04-28 13:37:49 +01002810unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
2811{
2812 int numDims = armnn::numeric_cast<int>(numDimsIn);
2813 int v = idx < 0 ? numDims + idx : idx;
2814 ARMNN_ASSERT(v >= 0);
2815 ARMNN_ASSERT(v < numDims);
2816
2817 return static_cast<unsigned int>(v);
2818}
2819
2820void TfLiteParser::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
2821{
2822 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2823
2824 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Ryan OShea86704732020-05-26 11:41:04 +01002825 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01002826
2827 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2828 CHECK_VALID_SIZE(inputs.size(), 3);
2829
2830 auto& inputTensor = inputs[0];
2831 auto& splitsTensor = inputs[1];
2832 auto& axisTensor = inputs[2];
2833
2834 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
2835 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
2836 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
2837 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2838
2839 // Inputs
2840 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2841 if (inputDimSize > MaxNumOfTensorDimensions)
2842 {
2843 throw ParseException(
2844 boost::str(
2845 boost::format(
2846 "The number of dimensions: %1% for input tensors of the "
Jan Eilersc0761e92020-06-29 16:48:44 +01002847 "SplitV op cannot be greater than %2% %3%")
Derek Lambertif0176992020-04-28 13:37:49 +01002848 % inputTensorInfo.GetNumDimensions()
2849 % MaxNumOfTensorDimensions
2850 % CHECK_LOCATION().AsString()));
2851 }
2852
2853 // Get split axis
2854 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
2855 std::vector<int> axisData(axisTensorInfo.GetNumElements());
2856 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2857 const unsigned int splitDim = ComputeWrappedIndex(axisData[0], inputTensorInfo.GetNumDimensions());
2858
Derek Lambertif0176992020-04-28 13:37:49 +01002859 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01002860 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01002861 unsigned int numSplits{0};
2862
2863 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01002864 {
2865 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01002866 }
2867 else
2868 {
Ryan OShea86704732020-05-26 11:41:04 +01002869 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01002870 }
2871
2872 if (numSplits <=0)
2873 {
2874 throw ParseException("SplitV has invalid number of splits");
2875 }
2876
Jan Eilersc0761e92020-06-29 16:48:44 +01002877 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01002878 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01002879 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01002880
Jan Eilersc0761e92020-06-29 16:48:44 +01002881 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01002882 int numInferred{0};
2883 unsigned int inferIdx{0};
2884 int splitSum{0};
2885 for (auto split : splitsData)
2886 {
2887 if (split < 0)
2888 {
2889 numInferred++;
2890 inferIdx = idx;
2891 }
2892 else
2893 {
2894 splitSum += split;
2895 }
2896 idx++;
2897 }
2898 // Check for inferred Axis
2899 if (numInferred == 0)
2900 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002901 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01002902 {
2903 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
2904 }
2905 }
2906 else if (numInferred == 1)
2907 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002908 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01002909 }
2910 else
2911 {
2912 throw ParseException("Cannot infer split size for more than one split");
2913 }
2914
Derek Lambertif0176992020-04-28 13:37:49 +01002915 //Ouput size validation
2916 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2917 CHECK_VALID_SIZE(outputs.size(), numSplits);
2918
2919 // Setup Armnn descriptor
2920 SplitterDescriptor splitDesc(numSplits, inputDimSize);
2921 unsigned int accumSplit = 0;
2922 for (unsigned int j = 0; j < numSplits; ++j)
2923 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002924 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01002925
2926 // Set the size of the views.
2927 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
2928 {
2929 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
2930 if (dimIdx == splitDim)
2931 {
2932 dimSize = splitSize;
2933 }
2934 splitDesc.SetViewSize(j, dimIdx, dimSize);
2935 }
2936
2937 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
2938 accumSplit += splitSize;
2939 }
2940
Ryan OShea86704732020-05-26 11:41:04 +01002941 auto layerName = boost::str(boost::format("SplitV:%1%:%2%") % subgraphIndex % operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01002942 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002943 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01002944
2945 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2946 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2947
2948 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2949 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002950 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01002951 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
2952 }
2953
2954 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2955 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2956}
2957
Sadik Armagan58f39192018-09-17 14:14:39 +01002958armnn::IConnectableLayer* TfLiteParser::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
2959 unsigned int outputSlot,
2960 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01002961{
2962 ActivationDescriptor activationDesc;
2963 std::string layerName = prevLayer->GetName();
2964
2965 switch(activationType)
2966 {
2967 case tflite::ActivationFunctionType_NONE:
2968 {
2969 // this is a no-op: return previous layer
2970 return prevLayer;
2971 }
2972 case tflite::ActivationFunctionType_RELU:
2973 {
2974 activationDesc.m_Function = ActivationFunction::ReLu;
2975 layerName += ":RELU";
2976 break;
2977 }
2978 case tflite::ActivationFunctionType_RELU6:
2979 {
2980 activationDesc.m_Function = ActivationFunction::BoundedReLu;
2981 activationDesc.m_A = 6.0f;
2982 activationDesc.m_B = 0.0f;
2983 layerName += ":RELU6";
2984 break;
2985 }
2986 case tflite::ActivationFunctionType_TANH:
2987 {
2988 activationDesc.m_Function = ActivationFunction::TanH;
2989 activationDesc.m_A = 1.0f;
2990 activationDesc.m_B = 1.0f;
2991 layerName += ":TANH";
2992 break;
2993 }
2994
2995 // I only put these here as a reminder what others we could support
2996 case tflite::ActivationFunctionType_RELU_N1_TO_1:
2997 case tflite::ActivationFunctionType_SIGN_BIT:
2998 default:
2999 {
3000 throw ParseException(
3001 boost::str(
3002 boost::format("TfLite parser doesn't suppport fused activation: "
3003 "%1%/%2% %3% ") %
3004 activationType %
3005 tflite::EnumNameActivationFunctionType(activationType) %
3006 CHECK_LOCATION().AsString()));
3007
3008 }
3009 }
3010
3011 IConnectableLayer* activationLayer =
3012 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3013
3014 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
3015 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
3016 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
3017 return activationLayer;
3018}
3019
3020TfLiteParser::ModelPtr TfLiteParser::LoadModelFromFile(const char * fileName)
3021{
3022 if (fileName == nullptr)
3023 {
3024 throw InvalidArgumentException(boost::str(boost::format("Invalid (null) file name %1%") %
3025 CHECK_LOCATION().AsString()));
3026 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01003027 std::error_code errorCode;
3028 fs::path pathToFile(fileName);
3029 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01003030 {
Derek Lambertic9e52792020-03-11 11:42:26 +00003031 std::string locationString = CHECK_LOCATION().AsString();
3032 std::string msg = boost::str(boost::format("Cannot find the file (%1%) errorCode: %2% %3%") %
3033 fileName %
3034 errorCode %
3035 locationString);
3036 throw FileNotFoundException(msg);
telsoa01c577f2c2018-08-31 09:22:23 +01003037 }
3038 std::ifstream file(fileName, std::ios::binary);
3039 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3040 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
3041 fileContent.size());
3042}
3043
3044TfLiteParser::ModelPtr TfLiteParser::LoadModelFromBinary(const uint8_t * binaryContent, size_t len)
3045{
3046 if (binaryContent == nullptr)
3047 {
3048 throw InvalidArgumentException(boost::str(boost::format("Invalid (null) binary content %1%") %
3049 CHECK_LOCATION().AsString()));
3050 }
3051 flatbuffers::Verifier verifier(binaryContent, len);
3052 if (verifier.VerifyBuffer<tflite::Model>() == false)
3053 {
3054 throw ParseException(
3055 boost::str(boost::format("Buffer doesn't conform to the expected Tensorflow Lite "
3056 "flatbuffers format. size:%1% %2%") %
3057 len %
3058 CHECK_LOCATION().AsString()));
3059 }
3060 return tflite::UnPackModel(binaryContent);
3061}
3062
3063TfLiteParser::TensorRawPtrVector TfLiteParser::GetInputs(const ModelPtr & model,
3064 size_t subgraphIndex,
3065 size_t operatorIndex)
3066{
3067 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3068
Derek Lambertiff05cc52019-04-26 13:05:17 +01003069 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3070 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003071
3072 size_t inputCount = operatorPtr->inputs.size();
3073 TensorRawPtrVector result(inputCount);
3074 for (size_t i=0; i<inputCount; ++i)
3075 {
3076 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003077 result[i] = subgraphPtr->tensors[inputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01003078 }
3079 return result;
3080}
3081
3082TfLiteParser::TensorRawPtrVector TfLiteParser::GetOutputs(const ModelPtr & model,
3083 size_t subgraphIndex,
3084 size_t operatorIndex)
3085{
3086 CHECK_MODEL(model, subgraphIndex, operatorIndex);
3087
Derek Lambertiff05cc52019-04-26 13:05:17 +01003088 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3089 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003090
3091 size_t outputCount = operatorPtr->outputs.size();
3092 TensorRawPtrVector result(outputCount);
3093 for (size_t i=0; i<outputCount; ++i)
3094 {
3095 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
3096 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003097 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01003098 }
3099 return result;
3100}
3101
3102TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphInputs(const ModelPtr & model,
3103 size_t subgraphIndex)
3104{
3105 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003106 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003107
Derek Lambertiff05cc52019-04-26 13:05:17 +01003108 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003109 TensorIdRawPtrVector result(inputCount);
3110 for (size_t i=0; i<inputCount; ++i)
3111 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003112 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01003113 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003114 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003115 }
3116 return result;
3117}
3118
3119TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphOutputs(const ModelPtr & model,
3120 size_t subgraphIndex)
3121{
3122 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003123 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003124
Derek Lambertiff05cc52019-04-26 13:05:17 +01003125 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003126 TensorIdRawPtrVector result(outputCount);
3127 for (size_t i=0; i<outputCount; ++i)
3128 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003129 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
3130 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003131 }
3132 return result;
3133}
3134
3135std::vector<int32_t>& TfLiteParser::GetInputTensorIds(const ModelPtr& model,
3136 size_t subgraphIndex,
3137 size_t operatorIndex)
3138{
3139 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003140 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3141 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003142 return operatorPtr->inputs;
3143}
3144
3145std::vector<int32_t>& TfLiteParser::GetOutputTensorIds(const ModelPtr& model,
3146 size_t subgraphIndex,
3147 size_t operatorIndex)
3148{
3149 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003150 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3151 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003152 return operatorPtr->outputs;
3153}
3154
3155void TfLiteParser::RegisterInputSlots(size_t subgraphIndex,
3156 size_t operatorIndex,
3157 IConnectableLayer* layer,
3158 const std::vector<unsigned int>& tensorIndexes)
3159{
3160 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003161 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003162 if (tensorIndexes.size() != layer->GetNumInputSlots())
3163 {
3164 throw ParseException(
3165 boost::str(boost::format("The number of tensor inputs (%1%) does not match the number expected (%2%)"
3166 " for subgraph:%3% operator index:%4% %5%") %
3167 tensorIndexes.size() %
3168 layer->GetNumInputSlots() %
3169 subgraphIndex %
3170 operatorIndex %
3171 CHECK_LOCATION().AsString()));
3172 }
3173
3174 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex)
3175 {
3176 unsigned int tensorIndex = tensorIndexes[slotIndex];
3177 armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex));
3178 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3179 }
3180}
3181
3182void TfLiteParser::RegisterOutputSlots(size_t subgraphIndex,
3183 size_t operatorIndex,
3184 IConnectableLayer* layer,
3185 const std::vector<unsigned int>& tensorIndexes)
3186{
3187 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003188 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003189 if (tensorIndexes.size() != layer->GetNumOutputSlots())
3190 {
3191 throw ParseException(
3192 boost::str(boost::format("The number of tensor outputs (%1%) does not match the number expected (%2%)"
3193 " for subgraph:%3% operator index:%4% %5%") %
3194 tensorIndexes.size() %
3195 layer->GetNumOutputSlots() %
3196 subgraphIndex %
3197 operatorIndex %
3198 CHECK_LOCATION().AsString()));
3199 }
3200
3201 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
3202 {
3203 unsigned int tensorIndex = tensorIndexes[slotIndex];
3204 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
3205 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3206 }
3207}
3208
3209void TfLiteParser::SetupInputLayers(size_t subgraphIndex)
3210{
3211 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3212
3213 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
3214 for (auto const & tensorIdAndPtr : inputs)
3215 {
3216 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3217 IConnectableLayer* layer =
3218 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3219
3220 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
3221 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3222
3223 RegisterOutputSlots(subgraphIndex,
3224 VIRTUAL_OPERATOR_ID,
3225 layer,
3226 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3227 }
3228}
3229
3230void TfLiteParser::SetupOutputLayers(size_t subgraphIndex)
3231{
3232 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3233
3234 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
3235 for (auto const & tensorIdAndPtr : outputs)
3236 {
3237 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3238 IConnectableLayer* layer =
3239 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3240
3241 RegisterInputSlots(subgraphIndex,
3242 VIRTUAL_OPERATOR_ID,
3243 layer,
3244 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3245 }
3246}
3247
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003248void TfLiteParser::SetupConstantLayers(size_t subgraphIndex)
3249{
3250 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3251
Derek Lambertiff05cc52019-04-26 13:05:17 +01003252 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003253 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3254 {
3255 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3256 {
3257 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
3258 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3259 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003260 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003261 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
3262 auto tensorAndData = CreateConstTensor(tensorPtr,
3263 tensorInfo,
3264 armnn::Optional<armnn::PermutationVector&>());
3265
3266 std::string layerName = boost::str(boost::format("Constant:%1%") % tensorPtr->name);
3267 IConnectableLayer *layer =
3268 m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
3269
3270 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3271 RegisterOutputSlots(subgraphIndex,
3272 VIRTUAL_OPERATOR_ID,
3273 layer,
3274 { tensorIndex });
3275
3276 }
3277 }
3278 }
3279}
3280
telsoa01c577f2c2018-08-31 09:22:23 +01003281// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
3282TfLiteParser::BufferRawPtr TfLiteParser::GetBuffer(const ModelPtr& model, size_t bufferIndex)
3283{
3284 CHECK_BUFFER(model, bufferIndex);
3285 return model->buffers[bufferIndex].get();
3286}
3287
Matteo Martincigh747ef822018-12-18 09:26:39 +00003288template<typename T>
3289std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage>
3290TfLiteParser::CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr,
3291 TfLiteParser::TensorRawPtr tensorPtr,
3292 armnn::TensorInfo& tensorInfo,
3293 armnn::Optional<armnn::PermutationVector&> permutationVector)
3294{
3295 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3296 tensorPtr,
3297 tensorInfo,
3298 permutationVector);
3299 TfLiteParser::SupportedDataStorage storage(std::move(constData.second));
3300 return std::make_pair(constData.first, std::move(storage));
3301}
3302
telsoa01c577f2c2018-08-31 09:22:23 +01003303std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage>
3304TfLiteParser::CreateConstTensor(TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00003305 armnn::TensorInfo& tensorInfo,
3306 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01003307{
3308 CHECK_TENSOR_PTR(tensorPtr);
3309 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3310 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3311
3312 switch (tensorInfo.GetDataType())
3313 {
3314 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003315 return CreateConstTensorAndStoreData<float>(bufferPtr,
3316 tensorPtr,
3317 tensorInfo,
3318 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00003319 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003320 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3321 tensorPtr,
3322 tensorInfo,
3323 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00003324 case armnn::DataType::QSymmS8:
3325 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3326 tensorPtr,
3327 tensorInfo,
3328 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00003329 case armnn::DataType::QAsymmS8:
3330 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3331 tensorPtr,
3332 tensorInfo,
3333 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003334 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003335 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3336 tensorPtr,
3337 tensorInfo,
3338 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003339 default:
3340 {
3341 std::stringstream errString;
3342 errString << "Unexpected datatype when creating const tensor: "
3343 << armnn::GetDataTypeName(tensorInfo.GetDataType())
3344 << " shape:" << tensorInfo.GetShape()
3345 << CHECK_LOCATION().AsString();
3346 throw ParseException(errString.str());
3347 }
3348 }
3349}
3350
3351BindingPointInfo TfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
3352 const std::string& name) const
3353{
3354 CHECK_SUBGRAPH(m_Model, subgraphId);
3355 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3356 for (auto const & input : inputs)
3357 {
3358 if (input.second->name == name)
3359 {
3360 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3361 return std::make_pair(bindingId, ToTensorInfo(input.second));
3362 }
3363 }
3364
3365 std::stringstream bindings;
3366 for (auto const & input : inputs)
3367 {
3368 bindings << "'" << input.second->name << "' ";
3369 }
3370
3371 throw ParseException(
3372 boost::str(
3373 boost::format("No input binding found for subgraph:%1% and name:%2%. "
3374 "Possible inputs are: [%3%] %4%") %
3375 subgraphId %
3376 name %
3377 bindings.str() %
3378 CHECK_LOCATION().AsString()));
3379}
3380
3381BindingPointInfo TfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
3382 const std::string& name) const
3383{
3384 CHECK_SUBGRAPH(m_Model, subgraphId);
3385 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003386 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01003387 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003388 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01003389 if (output.second->name == name)
3390 {
3391 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003392 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3393 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3394 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01003395 }
3396 }
3397
3398 std::stringstream bindings;
3399 for (auto const & output : outputs)
3400 {
3401 bindings << "'" << output.second->name << "' ";
3402 }
3403
3404 throw ParseException(
3405 boost::str(
3406 boost::format("No output binding found for subgraph:%1% and name:%2%. "
3407 "Possible outputs are: [%3%] %4%") %
3408 subgraphId %
3409 name %
3410 bindings.str() %
3411 CHECK_LOCATION().AsString()));
3412}
3413
3414size_t TfLiteParser::GetSubgraphCount() const
3415{
3416 return m_Model->subgraphs.size();
3417}
3418
3419std::vector<std::string> TfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
3420{
3421 CHECK_SUBGRAPH(m_Model, subgraphId);
3422 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3423 std::vector<std::string> result;
3424 result.reserve(inputs.size());
3425 for (auto const & input : inputs)
3426 {
3427 result.push_back(input.second->name);
3428 }
3429 return result;
3430}
3431
3432std::vector<std::string> TfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
3433{
3434 CHECK_SUBGRAPH(m_Model, subgraphId);
3435 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
3436 std::vector<std::string> result;
3437 result.reserve(outputs.size());
3438 for (auto const & output : outputs)
3439 {
3440 result.push_back(output.second->name);
3441 }
3442 return result;
3443}
3444
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003445ITfLiteParser* ITfLiteParser::CreateRaw(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
telsoa01c577f2c2018-08-31 09:22:23 +01003446{
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003447 return new TfLiteParser(options);
telsoa01c577f2c2018-08-31 09:22:23 +01003448}
3449
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003450ITfLiteParserPtr ITfLiteParser::Create(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
telsoa01c577f2c2018-08-31 09:22:23 +01003451{
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003452 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
telsoa01c577f2c2018-08-31 09:22:23 +01003453}
3454
3455void ITfLiteParser::Destroy(ITfLiteParser* parser)
3456{
3457 delete parser;
3458}
3459
3460TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data)
3461: m_FloatData(std::move(data))
3462, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003463, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003464, m_Int32Data(nullptr)
3465{
3466}
3467
3468TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
3469: m_FloatData(nullptr)
3470, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00003471, m_Int8Data(nullptr)
3472, m_Int32Data(nullptr)
3473{
3474}
3475
3476TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
3477: m_FloatData(nullptr)
3478, m_Uint8Data(nullptr)
3479, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01003480, m_Int32Data(nullptr)
3481{
3482}
3483
3484TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
3485: m_FloatData(nullptr)
3486, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003487, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003488, m_Int32Data(std::move(data))
3489{
3490}
3491
3492} // armnnTfLiteParser