blob: 6143f4af6a0929d72875c0097f1f87e87851867b [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
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +0100446armnn::TensorInfo ToTensorInfo(TfLiteParser::TensorRawPtr tensorPtr,
Keith Davis0c2eeac2020-02-11 16:51:50 +0000447 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
612INetworkPtr TfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
613{
614 ResetParser();
615 m_Model = LoadModelFromFile(graphFile);
616 return CreateNetworkFromModel();
617}
618
619INetworkPtr TfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
620{
621 ResetParser();
622 m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
623 return CreateNetworkFromModel();
624}
625
626INetworkPtr TfLiteParser::CreateNetworkFromModel()
627{
Sadik Armagand109a4d2020-07-28 10:42:13 +0100628
629 using NetworkOptions = std::vector<BackendOptions>;
630 NetworkOptions networkOptions = {};
631 if (m_Options && m_Options.value().m_InferAndValidate)
632 {
633 BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
634 {
635 { "InferAndValidate", true }
636 });
637
638 networkOptions.push_back(shapeInferenceMethodOption);
639 }
640
641 m_Network = INetwork::Create(networkOptions);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100642 ARMNN_ASSERT(m_Model.get() != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100643
telsoa01c577f2c2018-08-31 09:22:23 +0100644 if (m_Model->subgraphs.size() != 1)
645 {
646 throw ParseException(
647 boost::str(
648 boost::format("Current TfLite parser only supports 1 subgraph. Current one has: %1% %2%") %
649 m_Model->subgraphs.size() %
650 CHECK_LOCATION().AsString()));
651 }
652
653 size_t subgraphIndex = 0;
Colm Donelan6350d272020-06-09 16:56:25 +0100654 size_t operatorIndex = 0;
655 try
telsoa01c577f2c2018-08-31 09:22:23 +0100656 {
Colm Donelan6350d272020-06-09 16:56:25 +0100657 for (SubgraphPtr const& subgraph : m_Model->subgraphs)
telsoa01c577f2c2018-08-31 09:22:23 +0100658 {
Colm Donelan6350d272020-06-09 16:56:25 +0100659 m_SubgraphConnections.emplace_back(subgraph->tensors.size());
660 for (OperatorPtr const& op : subgraph->operators)
telsoa01c577f2c2018-08-31 09:22:23 +0100661 {
Colm Donelan6350d272020-06-09 16:56:25 +0100662 auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
telsoa01c577f2c2018-08-31 09:22:23 +0100663 auto builtinCode = opCodePtr->builtin_code;
664
665 if (builtinCode > tflite::BuiltinOperator_MAX)
666 {
Colm Donelan6350d272020-06-09 16:56:25 +0100667 throw ParseException(boost::str(boost::format("Operator code %1% is out of range 0-%2%. "
668 "subgraph:%3% operator idx:%4%. %5%") %
669 builtinCode % tflite::BuiltinOperator_MAX % subgraphIndex %
670 operatorIndex % CHECK_LOCATION().AsString()));
telsoa01c577f2c2018-08-31 09:22:23 +0100671 }
672
673 // lookup and call the parser function
Colm Donelan6350d272020-06-09 16:56:25 +0100674 auto& parserFunction = m_ParserFunctions[builtinCode];
telsoa01c577f2c2018-08-31 09:22:23 +0100675 (this->*parserFunction)(subgraphIndex, operatorIndex);
Colm Donelan6350d272020-06-09 16:56:25 +0100676 ++operatorIndex;
telsoa01c577f2c2018-08-31 09:22:23 +0100677 }
telsoa01c577f2c2018-08-31 09:22:23 +0100678
Colm Donelan6350d272020-06-09 16:56:25 +0100679 SetupInputLayers(subgraphIndex);
680 SetupOutputLayers(subgraphIndex);
681 SetupConstantLayers(subgraphIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100682
Colm Donelan6350d272020-06-09 16:56:25 +0100683 ++subgraphIndex;
684 operatorIndex = 0;
telsoa01c577f2c2018-08-31 09:22:23 +0100685 }
telsoa01c577f2c2018-08-31 09:22:23 +0100686 }
Colm Donelan6350d272020-06-09 16:56:25 +0100687 catch (const ParseException& e)
telsoa01c577f2c2018-08-31 09:22:23 +0100688 {
Colm Donelan6350d272020-06-09 16:56:25 +0100689 std::stringstream errorString;
690 errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
691 << subgraphIndex << " error: " << e.what();
692 ARMNN_LOG(error) << errorString.str();
693 std::stringstream errors;
694 errors << errorString.str() << "\n";
telsoa01c577f2c2018-08-31 09:22:23 +0100695 throw ParseException(errors.str());
696 }
697
698 // establish the connections from the layer outputs to the inputs of the subsequent layers
Colm Donelan6350d272020-06-09 16:56:25 +0100699 for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
telsoa01c577f2c2018-08-31 09:22:23 +0100700 {
701 for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
702 {
703 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
704 {
705 for (size_t inputSlotIdx = 0;
706 inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
707 ++inputSlotIdx)
708 {
709 m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
710 *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
711 }
712 }
713 }
714 }
715
716 return std::move(m_Network);
717}
718
719void TfLiteParser::RegisterProducerOfTensor(size_t subgraphIndex,
720 size_t tensorIndex,
721 armnn::IOutputSlot* slot)
722{
723 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100724 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
725 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100726
727 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
728
729 // assuming there is only one producer for that tensor
730 if (tensorSlots.outputSlot != nullptr)
731 {
732 throw ParseException(boost::str(
733 boost::format("Another layer has already registered itself as the producer of "
734 "subgraph:%1% tensor:%2% %3%") %
735 subgraphIndex %
736 tensorIndex %
737 CHECK_LOCATION().AsString()));
738 }
739
740 tensorSlots.outputSlot = slot;
741}
742
743void TfLiteParser::RegisterConsumerOfTensor(size_t subgraphIndex,
744 size_t tensorIndex,
745 armnn::IInputSlot* slot)
746{
747 CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100748 ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
749 ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100750
751 TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
752 tensorSlots.inputSlots.push_back(slot);
753}
754
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100755void TfLiteParser::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
756{
757 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
758
759 // NOTE: By default we presume the custom operator is not supported
760 auto customParserFunction = &TfLiteParser::ParseUnsupportedOperator;
761
762 // Identify custom code defined for custom operator
763 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
764 const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
765
766 // Find parser function that correspondes to custom code (if any)
767 auto iterator = m_CustomParserFunctions.find(customCode);
768 if (iterator != m_CustomParserFunctions.end())
769 {
770 customParserFunction = iterator->second;
771 }
772
773 // Run parser function
774 (this->*customParserFunction)(subgraphIndex, operatorIndex);
775}
776
telsoa01c577f2c2018-08-31 09:22:23 +0100777void TfLiteParser::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
778{
779 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +0100780
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100781 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
782
783 auto opcodeIndex = operatorPtr->opcode_index;
784 auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
785
786 if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
787 {
788 // Do not add StandInLayer, throw ParseException instead
789 throw ParseException(
790 boost::str(
791 boost::format("Operator not supported. "
792 "subgraph:%1% operator:%2% "
793 "opcode_index:%3% opcode:%4% / %5% %6%") %
794 subgraphIndex %
795 operatorIndex %
796 opcodeIndex %
797 opcode %
798 tflite::EnumNameBuiltinOperator(opcode) %
799 CHECK_LOCATION().AsString()));
800 }
801
802 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
803 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
804
Matthew Sloyan589e3e82020-09-11 16:17:48 +0100805 const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
806 const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100807
808 StandInDescriptor descriptor(numInputs, numOutputs);
809 auto layerName = boost::str(boost::format("StandIn:%1%:%2%:%3%") % subgraphIndex % operatorIndex % opcode);
810
811 // Add a non-executable StandInLayer as a placeholder for any unsupported operator
812 IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +0100813 ARMNN_ASSERT(layer != nullptr);
814
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100815 for (unsigned int i = 0u; i < numOutputs; ++i)
816 {
Sadik Armagand109a4d2020-07-28 10:42:13 +0100817 layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
Aron Virginas-Tarc975f922019-10-23 17:38:17 +0100818 }
819
820 auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
821 auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
822
823 RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
824 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
telsoa01c577f2c2018-08-31 09:22:23 +0100825}
826
telsoa01c577f2c2018-08-31 09:22:23 +0100827void TfLiteParser::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
828{
829 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
830
831 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
832 const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
833
834 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
835
836 Convolution2dDescriptor desc;
837 desc.m_BiasEnabled = false;
838 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
839 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000840 desc.m_DataLayout = armnn::DataLayout::NHWC;
Pablo Tellof0bd6832019-04-26 17:58:13 +0100841 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
842 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000843
telsoa01c577f2c2018-08-31 09:22:23 +0100844 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
845 CHECK_VALID_SIZE(inputs.size(), 2, 3);
846
847 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
848 CHECK_VALID_SIZE(outputs.size(), 1);
849
850 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
851 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
852
853 // assuming input is NHWC
854 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
855 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
856
857 // assuming the filter is OHWI : Output, H, W, Input
858 // which is essentially the same as NHWC
859 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
860 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
861
Pablo Tellof0bd6832019-04-26 17:58:13 +0100862 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
863 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
864 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
865 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +0100866
Matteo Martincigh747ef822018-12-18 09:26:39 +0000867 auto filterTensorAndData = CreateConstTensor(inputs[1],
868 filterTensorInfo,
869 armnn::Optional<armnn::PermutationVector&>());
Matthew Jackson74bf7da2019-08-16 16:51:42 +0100870 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100871
872 auto layerName = boost::str(boost::format("Conv2D:%1%:%2%") % subgraphIndex % operatorIndex);
873
874 if (inputs.size() == 3)
875 {
876 desc.m_BiasEnabled = true;
877 armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000878 auto biasTensorAndData = CreateConstTensor(inputs[2],
879 biasTensorInfo,
880 armnn::Optional<armnn::PermutationVector&>());
telsoa01c577f2c2018-08-31 09:22:23 +0100881 layer = m_Network->AddConvolution2dLayer(desc,
882 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100883 Optional<ConstTensor>(biasTensorAndData.first),
telsoa01c577f2c2018-08-31 09:22:23 +0100884 layerName.c_str());
885 }
886 else
887 {
888 layer = m_Network->AddConvolution2dLayer(desc,
889 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100890 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +0100891 layerName.c_str());
892 }
893
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100894 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100895
Sadik Armagand109a4d2020-07-28 10:42:13 +0100896 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +0000897 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +0100898
899 // register the input connection slots for the layer, connections are made after all layers have been created
900 // only the tensors for the inputs are relevant, exclude the const tensors
901 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +0000902 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +0100903
jimfly01c25411c2018-11-14 17:47:22 +0000904 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +0100905 // register the output connection slots for the layer, connections are made after all layers have been created
906 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
907 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
908}
909
910void TfLiteParser::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
911{
912 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
913
914 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
915 const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
916
917 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
918
919 DepthwiseConvolution2dDescriptor desc;
920 desc.m_BiasEnabled = false;
921 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
922 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
jimfly01c25411c2018-11-14 17:47:22 +0000923 desc.m_DataLayout = armnn::DataLayout::NHWC;
Matthew Jacksond6a9dee2019-07-22 13:53:24 +0100924 CHECKED_NON_NEGATIVE(options->depth_multiplier);
telsoa01c577f2c2018-08-31 09:22:23 +0100925
926 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
927 CHECK_VALID_SIZE(inputs.size(), 2, 3);
928 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
929 CHECK_VALID_SIZE(outputs.size(), 1);
Pablo Tellof0bd6832019-04-26 17:58:13 +0100930 desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
931 desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
Kevin May83add212019-03-26 11:39:19 +0000932
Keith Davis0c2eeac2020-02-11 16:51:50 +0000933 // Mappings from TensorflowLite filter tensors to the ArmNN filter tensors (ArmNN weights have to be [M, I, H, W])
934 PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, H, W]
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +0100935
telsoa01c577f2c2018-08-31 09:22:23 +0100936 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Keith Davis0c2eeac2020-02-11 16:51:50 +0000937 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1], permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +0100938
Matteo Martincigh747ef822018-12-18 09:26:39 +0000939 // Assuming input is NHWC
telsoa01c577f2c2018-08-31 09:22:23 +0100940 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
941 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
Matteo Martincigh747ef822018-12-18 09:26:39 +0000942
943 // TensorflowLite weights come in the format [1, H, W, I * M]
telsoa01c577f2c2018-08-31 09:22:23 +0100944 unsigned int filterHeight = filterTensorInfo.GetShape()[1];
945 unsigned int filterWidth = filterTensorInfo.GetShape()[2];
946
Matteo Martincigh747ef822018-12-18 09:26:39 +0000947 // Reshape weights as [ H, W, I, M ]
948 filterTensorInfo.SetShape({ filterHeight,
949 filterWidth,
950 inputTensorInfo.GetShape()[3],
951 filterTensorInfo.GetShape()[3] / inputTensorInfo.GetShape()[3] });
952
Pablo Tellof0bd6832019-04-26 17:58:13 +0100953 CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
954 desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
955 CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
956 desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
telsoa01c577f2c2018-08-31 09:22:23 +0100957
Matteo Martincigh747ef822018-12-18 09:26:39 +0000958 auto filterTensorAndData = CreateConstTensor(inputs[1], filterTensorInfo, permutationVector);
Matthew Jackson74bf7da2019-08-16 16:51:42 +0100959 armnn::IConnectableLayer* layer = nullptr;
telsoa01c577f2c2018-08-31 09:22:23 +0100960 auto layerName = boost::str(boost::format("DepthwiseConv2D:%1%:%2%") % subgraphIndex % operatorIndex);
961
962 if (inputs.size() == 3)
963 {
964 desc.m_BiasEnabled = true;
965 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +0000966 auto biasTensorAndData = CreateConstTensor(inputs[2],
967 biasTensorInfo,
968 armnn::Optional<armnn::PermutationVector&>());
telsoa01c577f2c2018-08-31 09:22:23 +0100969 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
970 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100971 Optional<ConstTensor>(biasTensorAndData.first),
telsoa01c577f2c2018-08-31 09:22:23 +0100972 layerName.c_str());
973 }
974 else
975 {
976 layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
977 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +0100978 EmptyOptional(),
telsoa01c577f2c2018-08-31 09:22:23 +0100979 layerName.c_str());
980 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +0100981 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +0100982
Sadik Armagand109a4d2020-07-28 10:42:13 +0100983 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
jimfly01c25411c2018-11-14 17:47:22 +0000984 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
telsoa01c577f2c2018-08-31 09:22:23 +0100985
986 // register the input connection slots for the layer, connections are made after all layers have been created
987 // only the tensors for the inputs are relevant, exclude the const tensors
988 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +0000989 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
telsoa01c577f2c2018-08-31 09:22:23 +0100990
jimfly01c25411c2018-11-14 17:47:22 +0000991 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
telsoa01c577f2c2018-08-31 09:22:23 +0100992 // register the output connection slots for the layer, connections are made after all layers have been created
993 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
994 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
995}
996
Finn Williamsed66d142019-12-06 09:55:55 +0000997void TfLiteParser::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
998{
999 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1000
1001 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1002 CHECK_VALID_SIZE(inputs.size(), 1);
1003
1004 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1005 CHECK_VALID_SIZE(outputs.size(), 1);
1006
1007 auto layerName = boost::str(boost::format("Dequantize:%1%:%2%") % subgraphIndex % operatorIndex);
1008
1009 IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001010 ARMNN_ASSERT(layer != nullptr);
Finn Williamsed66d142019-12-06 09:55:55 +00001011
Sadik Armagand109a4d2020-07-28 10:42:13 +01001012 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Finn Williamsed66d142019-12-06 09:55:55 +00001013 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1014
1015 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1016 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1017
1018 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1019 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1020}
1021
Derek Lambertif0176992020-04-28 13:37:49 +01001022void TfLiteParser::ParseExp(size_t subgraphIndex, size_t operatorIndex)
1023{
1024 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1025
1026 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1027 CHECK_VALID_SIZE(inputs.size(), 1);
1028
1029 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1030 CHECK_VALID_SIZE(outputs.size(), 1);
1031
1032 auto layerName = boost::str(boost::format("Exp:%1%:%2%") % subgraphIndex % operatorIndex);
1033
1034 ElementwiseUnaryDescriptor desc;
1035 desc.m_Operation = UnaryOperation::Exp;
1036 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerName.c_str());
1037 ARMNN_ASSERT(layer != nullptr);
1038
Sadik Armagand109a4d2020-07-28 10:42:13 +01001039 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Derek Lambertif0176992020-04-28 13:37:49 +01001040 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1041
1042 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1043 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1044
1045 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1046 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1047}
1048
Keith Davis4cd29a02019-09-09 14:49:20 +01001049void TfLiteParser::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
1050{
1051 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1052
1053 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Kevin May85d92602019-09-27 17:21:06 +01001054 CHECK_VALID_SIZE(inputs.size(), 1, 2);
Keith Davis4cd29a02019-09-09 14:49:20 +01001055
1056 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1057 CHECK_VALID_SIZE(outputs.size(), 1);
1058
Keith Davis4cd29a02019-09-09 14:49:20 +01001059 auto layerName = boost::str(boost::format("Transpose:%1%:%2%") % subgraphIndex % operatorIndex);
Mike Kelly08759e22020-03-02 11:41:31 +00001060 TransposeDescriptor desc;
Keith Davis4cd29a02019-09-09 14:49:20 +01001061
josh minorba424d22019-11-13 10:55:17 -06001062 if (inputs.size() == 2)
Kevin May85d92602019-09-27 17:21:06 +01001063 {
1064 armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1065 BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
josh minorba424d22019-11-13 10:55:17 -06001066 auto numPermVecElements = permuteTensorInfo.GetNumElements();
1067 std::vector<unsigned int> permuteShape(numPermVecElements);
Kevin May85d92602019-09-27 17:21:06 +01001068 ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
Mike Kelly08759e22020-03-02 11:41:31 +00001069 PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
Kevin May85d92602019-09-27 17:21:06 +01001070
Mike Kelly08759e22020-03-02 11:41:31 +00001071 desc = TransposeDescriptor(permutationVector);
Kevin May85d92602019-09-27 17:21:06 +01001072 }
1073
James Conroy05102392020-06-24 15:39:55 +01001074 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001075 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001076 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
Keith Davis4cd29a02019-09-09 14:49:20 +01001077
James Conroy05102392020-06-24 15:39:55 +01001078 IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001079 ARMNN_ASSERT(layer != nullptr);
Keith Davis4cd29a02019-09-09 14:49:20 +01001080 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1081
1082 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1083 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1084
1085 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1086 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1087}
1088
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001089void TfLiteParser::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
1090{
1091 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1092
1093 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1094 const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1095
1096 TransposeConvolution2dDescriptor desc;
1097 desc.m_BiasEnabled = false;
1098 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1099 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1100 desc.m_DataLayout = armnn::DataLayout::NHWC;
1101
1102 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001103 CHECK_VALID_SIZE(inputs.size(), 3);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001104
1105 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1106 CHECK_VALID_SIZE(outputs.size(), 1);
1107
Colm Donelan0ad3ef12020-07-03 15:54:28 +01001108 if (inputs[0])
1109 {
1110 armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1111 std::vector<int> output_shape(tensorInfo.GetNumElements());
1112 if (tensorInfo.GetDataType() == DataType::Signed32)
1113 {
1114 ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1115 }
1116 if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1117 {
1118 for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1119 {
1120 output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1121 }
1122 }
1123 // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1124 for (int dimension : output_shape)
1125 {
1126 desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1127 }
1128 desc.m_OutputShapeEnabled = true;
1129 }
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001130 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001131 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1132
1133 // TfLite uses NHWC tensors
1134 const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1135 const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1136
1137 const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1138 const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1139
1140 CalcPadding(inputHeight,
1141 filterHeight,
1142 desc.m_StrideY,
1143 1, // DilationY
1144 desc.m_PadTop,
1145 desc.m_PadBottom,
1146 options->padding);
1147
1148 CalcPadding(inputWidth,
1149 filterWidth,
1150 desc.m_StrideX,
1151 1, // DilationX
1152 desc.m_PadLeft,
1153 desc.m_PadRight,
1154 options->padding);
1155
1156 auto filterTensorAndData = CreateConstTensor(inputs[1],
1157 filterTensorInfo,
1158 armnn::Optional<armnn::PermutationVector&>());
1159
1160 armnn::IConnectableLayer* layer = nullptr;
1161 auto layerName = boost::str(boost::format("TransposeConv:%1%:%2%") % subgraphIndex % operatorIndex);
1162
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001163 layer = m_Network->AddTransposeConvolution2dLayer(desc,
1164 filterTensorAndData.first,
1165 EmptyOptional(),
1166 layerName.c_str());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001167
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001168 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001169
Sadik Armagand109a4d2020-07-28 10:42:13 +01001170 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001171 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1172
1173 // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1174 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Matthew Jacksonccb25ea2019-08-20 17:18:33 +01001175 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
Matthew Jackson74bf7da2019-08-16 16:51:42 +01001176
1177 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1178 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1179}
1180
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001181void TfLiteParser::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
1182{
1183 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1184}
1185
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001186void TfLiteParser::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
1187{
1188 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1189
1190 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1191 CHECK_VALID_SIZE(inputs.size(), 3);
1192
1193 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1194 CHECK_VALID_SIZE(outputs.size(), 1);
1195
1196 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1197 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1198
1199 armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1200 BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1201
1202 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1203 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1204
1205 std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1206 ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1207
1208 size_t step = 2;
1209 std::vector<std::pair<unsigned int, unsigned int>> crops;
1210 for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1211 {
1212 crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1213 }
1214
1215 armnn::BatchToSpaceNdDescriptor desc;
1216 desc.m_BlockShape = blockShape;
1217 desc.m_Crops = crops;
1218 desc.m_DataLayout = armnn::DataLayout::NHWC;
1219
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001220 auto layerName = boost::str(boost::format("BatchToSpaceND:%1%:%2%") % subgraphIndex % operatorIndex);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001221
James Conroy05102392020-06-24 15:39:55 +01001222 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001223 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001224 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1225
1226 IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1227 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesdb947e22019-02-08 18:52:21 -02001228 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1229
1230 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1231 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1232
1233 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1234 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1235}
1236
Matthew Jackson28c94572019-07-18 10:47:03 +01001237void TfLiteParser::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
1238{
1239 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1240
1241 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1242 CHECK_VALID_SIZE(inputs.size(), 1);
1243
1244 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1245 CHECK_VALID_SIZE(outputs.size(), 1);
1246
1247 L2NormalizationDescriptor desc;
1248 desc.m_DataLayout = armnn::DataLayout::NHWC;
1249 auto layerName = boost::str(boost::format("L2Normalization:%1%:%2%") % subgraphIndex % operatorIndex);
1250 IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1251
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001252 ARMNN_ASSERT(layer != nullptr);
Matthew Jackson28c94572019-07-18 10:47:03 +01001253
Sadik Armagand109a4d2020-07-28 10:42:13 +01001254 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jackson28c94572019-07-18 10:47:03 +01001255 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1256
1257 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1258 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1259
1260 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1261 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1262}
1263
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001264void TfLiteParser::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
1265{
1266 ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1267}
1268
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001269void TfLiteParser::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
1270{
1271 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1272
1273 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1274 CHECK_VALID_SIZE(inputs.size(), 2);
1275
1276 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1277 CHECK_VALID_SIZE(outputs.size(), 1);
1278
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001279 auto layerName = boost::str(boost::format("Maximum:%1%:%2%") % subgraphIndex % operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001280
1281 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1282 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1283 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001284
Sadik Armagand109a4d2020-07-28 10:42:13 +01001285 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001286 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1287
1288 IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1289 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001290 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1291
1292 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001293 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesb8d805e2019-02-12 22:57:13 -02001294
1295 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1296 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1297}
1298
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001299void TfLiteParser::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
1300{
1301 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1302
1303 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1304 CHECK_VALID_SIZE(inputs.size(), 2);
1305
1306 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1307 CHECK_VALID_SIZE(outputs.size(), 1);
1308
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001309 auto layerName = boost::str(boost::format("Minimum:%1%:%2%") % subgraphIndex % operatorIndex);
James Conroy05102392020-06-24 15:39:55 +01001310
1311 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1312 TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1313 CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001314
Sadik Armagand109a4d2020-07-28 10:42:13 +01001315 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001316 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1317
1318 IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1319 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001320 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1321
1322 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001323 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalves8f6d7a72019-02-12 22:58:18 -02001324
1325 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1326 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1327}
1328
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001329void TfLiteParser::ParsePool(size_t subgraphIndex,
1330 size_t operatorIndex,
1331 PoolingAlgorithm algorithm)
1332{
1333 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1334
1335 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1336 const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1337
1338 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1339
1340 std::string layerName;
1341
1342 switch (algorithm)
1343 {
1344 case PoolingAlgorithm::Average:
1345 layerName =
1346 boost::str(boost::format("AveragePool2D:%1%:%2%") % subgraphIndex % operatorIndex);
1347 break;
1348 case PoolingAlgorithm::Max:
1349 layerName =
1350 boost::str(boost::format("MaxPool2D:%1%:%2%") % subgraphIndex % operatorIndex);
1351 break;
1352 default:
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001353 ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001354 }
1355
1356 Pooling2dDescriptor desc;
1357
1358 desc.m_PoolType = algorithm;
1359 desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1360 desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1361 desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1362 desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1363 desc.m_PaddingMethod = PaddingMethod::Exclude;
1364 desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
jimfly01c25411c2018-11-14 17:47:22 +00001365 desc.m_DataLayout = armnn::DataLayout::NHWC;
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001366
1367 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1368 CHECK_VALID_SIZE(inputs.size(), 1);
1369 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1370
1371 // assuming input is NHWC
1372 unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1373 unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1374
Pablo Tellof0bd6832019-04-26 17:58:13 +01001375 CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1376 desc.m_PadTop, desc.m_PadBottom, options->padding);
1377 CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1378 desc.m_PadLeft, desc.m_PadRight, options->padding);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001379
1380 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1381 CHECK_VALID_SIZE(outputs.size(), 1);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001382
Sadik Armagand109a4d2020-07-28 10:42:13 +01001383 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001384 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1385
1386 IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1387 ARMNN_ASSERT(layer != nullptr);
jimfly01c25411c2018-11-14 17:47:22 +00001388 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001389
1390 // register the input connection slots for the layer, connections are made after all layers have been created
1391 // only the tensors for the inputs are relevant, exclude the const tensors
1392 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
jimfly01c25411c2018-11-14 17:47:22 +00001393 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001394
jimfly01c25411c2018-11-14 17:47:22 +00001395 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Nattapat Chaimanowongb66504b2018-10-17 15:19:14 +01001396 // register the output connection slots for the layer, connections are made after all layers have been created
1397 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1398 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1399}
1400
josh minorba424d22019-11-13 10:55:17 -06001401void TfLiteParser::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
1402{
1403 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1404
1405 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1406 CHECK_VALID_SIZE(inputs.size(), 3);
1407 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1408 CHECK_VALID_SIZE(outputs.size(), 1);
1409
1410 SliceDescriptor desc;
1411
1412 // set begin tensor info for slice descriptor
1413 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1414 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1415
1416 std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1417 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1418
1419 // set size tensor info for slice descriptor
1420 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1421 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1422
1423 std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1424 ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1425 desc = SliceDescriptor(begin, size);
1426
1427 auto layerName = boost::str(boost::format("Slice:%1%:%2%") % subgraphIndex % operatorIndex);
josh minorba424d22019-11-13 10:55:17 -06001428
James Conroy05102392020-06-24 15:39:55 +01001429 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001430 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001431 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1432
1433 IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
josh minorba424d22019-11-13 10:55:17 -06001434 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1435
1436 // register the input connection slots for the layer, connections are made after all layers have been created
1437 // only the tensors for the inputs are relevant, exclude the const tensors
1438 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1439 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1440
1441 // register the output connection slots for the layer, connections are made after all layers have been created
1442 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1443 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1444}
1445
telsoa01c577f2c2018-08-31 09:22:23 +01001446void TfLiteParser::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
1447{
1448 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1449 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1450 const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1451
1452 SoftmaxDescriptor desc;
1453 desc.m_Beta = options->beta;
1454
1455 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1456 CHECK_VALID_SIZE(inputs.size(), 1);
1457 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1458 CHECK_VALID_SIZE(outputs.size(), 1);
1459
1460 auto layerName = boost::str(boost::format("Softmax:%1%:%2%") % subgraphIndex % operatorIndex);
1461 IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1462
Sadik Armagand109a4d2020-07-28 10:42:13 +01001463 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
telsoa01c577f2c2018-08-31 09:22:23 +01001464 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1465
1466 // register the input connection slots for the layer, connections are made after all layers have been created
1467 // only the tensors for the inputs are relevant, exclude the const tensors
1468 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1469 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1470
1471 // register the output connection slots for the layer, connections are made after all layers have been created
1472 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1473 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1474}
1475
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001476void TfLiteParser::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
1477{
1478 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1479
1480 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1481 CHECK_VALID_SIZE(inputs.size(), 3);
1482
1483 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1484 CHECK_VALID_SIZE(outputs.size(), 1);
1485
1486 armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1487 BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1488
1489 armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1490 BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1491
1492 std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1493 ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1494
1495 std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1496 ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1497
1498 size_t step = 2;
1499 std::vector<std::pair<unsigned int, unsigned int>> padList;
1500 for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1501 {
1502 padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1503 }
1504
1505 armnn::SpaceToBatchNdDescriptor desc;
1506 desc.m_BlockShape = blockShape;
1507 desc.m_PadList = padList;
1508 desc.m_DataLayout = armnn::DataLayout::NHWC;
1509
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001510 auto layerName = boost::str(boost::format("SpaceToBatchND:%1%:%2%") % subgraphIndex % operatorIndex);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001511
James Conroy05102392020-06-24 15:39:55 +01001512 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001513 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001514 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1515
1516 IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1517 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbaded142019-02-08 19:02:48 -02001518 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1519
1520 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1521 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1522
1523 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1524 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1525}
1526
telsoa01c577f2c2018-08-31 09:22:23 +01001527armnn::TensorInfo TfLiteParser::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn,
1528 const armnn::TensorInfo & inputTensorInfo)
1529{
1530 CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4);
1531 std::vector<uint32_t> squeezeDims = squeezeDimsIn;
1532 static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1533
1534 if (inputTensorInfo.GetNumDimensions() > 4)
1535 {
1536 std::stringstream ss;
1537 ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1538 << " shape:" << inputTensorInfo.GetShape() << " "
1539 << CHECK_LOCATION().AsString();
1540 throw ParseException(ss.str());
1541 }
1542
1543 if (squeezeDims.empty())
1544 {
1545 squeezeDims.assign(dimensionSequence,
1546 dimensionSequence+inputTensorInfo.GetNumDimensions());
1547 }
1548
1549 std::vector<uint32_t> outputDims;
1550 for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1551 {
1552 bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1553 auto currentDimension = inputTensorInfo.GetShape()[i];
1554 if (skipSqueeze || currentDimension != 1)
1555 {
1556 outputDims.push_back(currentDimension);
1557 }
1558 }
1559
1560 if (outputDims.size() > 4)
1561 {
1562 std::stringstream ss;
1563 ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1564 << " shape:" << inputTensorInfo.GetShape() << " "
1565 << CHECK_LOCATION().AsString();
1566 throw ParseException(ss.str());
1567 }
1568
1569 TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1570 outputDims.data());
1571
1572 // we need to preserve the tensor type and the quantization data as well
1573 TensorInfo outTensorInfo = inputTensorInfo;
1574 outTensorInfo.SetShape(outShape);
1575
1576 return outTensorInfo;
1577}
1578
1579void TfLiteParser::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
1580{
1581 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1582
1583 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1584 CHECK_VALID_SIZE(inputs.size(), 1);
1585
1586 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1587 CHECK_VALID_SIZE(outputs.size(), 1);
1588
1589 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1590 const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
James Conroy05102392020-06-24 15:39:55 +01001591 auto layerName = boost::str(boost::format("Squeeze:%1%:%2%") % subgraphIndex % operatorIndex);
telsoa01c577f2c2018-08-31 09:22:23 +01001592
1593 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1594 armnn::TensorInfo outputTensorInfo =
1595 TfLiteParser::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims),
1596 inputTensorInfo);
James Conroy05102392020-06-24 15:39:55 +01001597 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
telsoa01c577f2c2018-08-31 09:22:23 +01001598
1599 ReshapeDescriptor reshapeDesc;
1600 reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1601
telsoa01c577f2c2018-08-31 09:22:23 +01001602 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001603 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01001604 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1605
1606 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1607 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1608
1609 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1610 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1611}
1612
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001613void TfLiteParser::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
1614{
1615 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1616
1617 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1618 CHECK_VALID_SIZE(inputs.size(), 4);
1619
1620 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1621 CHECK_VALID_SIZE(outputs.size(), 1);
1622
1623 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1624 const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1625
1626 StridedSliceDescriptor desc;
1627 desc.m_BeginMask = options->begin_mask;
1628 desc.m_EllipsisMask = options->ellipsis_mask;
1629 desc.m_EndMask = options->end_mask;
1630 desc.m_NewAxisMask = options->new_axis_mask;
1631 desc.m_ShrinkAxisMask = options->shrink_axis_mask;
1632 desc.m_DataLayout = armnn::DataLayout::NHWC;
1633
1634 armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1635 BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1636
1637 std::vector<int> begin(beginTensorInfo.GetNumElements());
1638 ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1639
1640 armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
1641 BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1642
1643 std::vector<int> end(endTensorInfo.GetNumElements());
1644 ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1645
1646 armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
1647 BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
1648
1649 std::vector<int> stride(strideTensorInfo.GetNumElements());
1650 ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1651
1652 desc.m_Begin = begin;
1653 desc.m_End = end;
1654 desc.m_Stride = stride;
1655
1656 auto layerName = boost::str(boost::format("StridedSlice:%1%:%2%") % subgraphIndex % operatorIndex);
1657 IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001658 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001659
Sadik Armagand109a4d2020-07-28 10:42:13 +01001660 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves451d95b2019-02-12 22:59:22 -02001661 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1662
1663 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1664 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1665
1666 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1667 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1668}
1669
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001670void TfLiteParser::ParseSub(size_t subgraphIndex, size_t operatorIndex)
1671{
1672 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1673
1674 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1675 const auto * options = operatorPtr->builtin_options.AsSubOptions();
1676
1677 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1678 CHECK_VALID_SIZE(inputs.size(), 2);
1679
1680 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1681 CHECK_VALID_SIZE(outputs.size(), 1);
1682
1683 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1684 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1685
1686 auto layerName = boost::str(boost::format("Sub:%1%:%2%") % subgraphIndex % operatorIndex);
1687 IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001688 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001689
Sadik Armagand109a4d2020-07-28 10:42:13 +01001690 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001691 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1692
1693 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001694 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesbbeae262019-02-07 18:37:39 -02001695
1696 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1697
1698 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1699 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1700}
1701
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301702void TfLiteParser::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
1703{
1704 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1705
1706 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1707 const auto * options = operatorPtr->builtin_options.AsDivOptions();
1708
1709 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1710 CHECK_VALID_SIZE(inputs.size(), 2);
1711
1712 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1713 CHECK_VALID_SIZE(outputs.size(), 1);
1714
1715 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1716 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1717
1718 auto layerName = boost::str(boost::format("Div:%1%:%2%") % subgraphIndex % operatorIndex);
1719 IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001720 ARMNN_ASSERT(layer != nullptr);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301721
Sadik Armagand109a4d2020-07-28 10:42:13 +01001722 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301723 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1724
1725 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001726 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Darshan Patel42b3d7d2020-05-25 22:30:07 +05301727 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1728
1729 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1730 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1731}
1732
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001733void TfLiteParser::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
1734{
1735 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1736
1737 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1738 const auto * options = operatorPtr->builtin_options.AsAddOptions();
1739
1740 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1741 CHECK_VALID_SIZE(inputs.size(), 2);
1742
1743 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1744 CHECK_VALID_SIZE(outputs.size(), 1);
1745
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001746 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1747 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1748
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001749 auto layerName = boost::str(boost::format("Add:%1%:%2%") % subgraphIndex % operatorIndex);
1750 IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001751 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001752
Sadik Armagand109a4d2020-07-28 10:42:13 +01001753 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001754 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1755
1756 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001757 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesd4ac6a42018-12-18 12:56:22 -02001758 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1759
1760 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1761 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1762}
1763
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001764void TfLiteParser::ParseMul(size_t subgraphIndex, size_t operatorIndex)
1765{
1766 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1767
1768 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1769 const auto * options = operatorPtr->builtin_options.AsMulOptions();
1770
1771 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1772 CHECK_VALID_SIZE(inputs.size(), 2);
1773
1774 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1775 CHECK_VALID_SIZE(outputs.size(), 1);
1776
Bruno Goncalves9c761a62018-12-27 14:20:35 -02001777 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1778 armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1779
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001780 auto layerName = boost::str(boost::format("Mul:%1%:%2%") % subgraphIndex % operatorIndex);
1781 IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001782 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001783
Sadik Armagand109a4d2020-07-28 10:42:13 +01001784 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001785 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1786
1787 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat16f82f92020-09-14 16:12:44 +01001788 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
Bruno Goncalvesf803f782018-12-18 13:40:30 -02001789 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1790
1791 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1792 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1793}
1794
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001795void TfLiteParser::ParseMean(size_t subgraphIndex, size_t operatorIndex)
1796{
1797 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1798
1799 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1800
1801 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1802 CHECK_VALID_SIZE(outputs.size(), 1);
1803
1804 armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
1805 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1806
1807 armnn::MeanDescriptor desc;
1808 std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
1809 ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
1810 desc.m_Axis = axis;
1811
1812 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001813 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001814
1815 desc.m_KeepDims =
1816 inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
1817 true : false;
1818
1819 auto layerName = boost::str(boost::format("Mean:%1%:%2%") % subgraphIndex % operatorIndex);
1820 IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01001821 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves2235cee2018-12-19 12:51:45 -02001822
1823 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1824
1825 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1826 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1827
1828 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1829 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1830}
1831
Darshan Patel83fcf982020-05-26 22:22:42 +05301832void TfLiteParser::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
1833{
1834 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1835
1836 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1837 CHECK_VALID_SIZE(inputs.size(), 1);
1838
1839 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1840 CHECK_VALID_SIZE(outputs.size(), 1);
1841
1842 auto layerName = boost::str(boost::format("Neg:%1%:%2%") % subgraphIndex % operatorIndex);
1843 armnn::ElementwiseUnaryDescriptor descriptor(armnn::UnaryOperation::Neg);
1844 IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(descriptor, layerName.c_str());
1845 ARMNN_ASSERT(layer != nullptr);
1846
Sadik Armagand109a4d2020-07-28 10:42:13 +01001847 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Darshan Patel83fcf982020-05-26 22:22:42 +05301848 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1849
1850 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1851 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1852
1853 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1854 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1855}
1856
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001857void TfLiteParser::ParsePad(size_t subgraphIndex, size_t operatorIndex)
1858{
1859 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1860
1861 TfLiteParser::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1862
1863 TfLiteParser::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1864 CHECK_VALID_SIZE(outputs.size(), 1);
1865
1866 armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
1867 BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1868
1869 std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
1870 ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
1871
1872 size_t step = 2;
1873 armnn::PadDescriptor desc;
1874 for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
1875 {
1876 desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
1877 }
1878
1879 auto layerName = boost::str(boost::format("Pad:%1%:%2%") % subgraphIndex % operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01001880 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01001881
1882 IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
1883 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves6c2355b2018-12-19 12:52:01 -02001884 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1885
1886 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1887 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1888
1889 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1890 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1891}
1892
Sadik Armagan66dedc72019-12-10 16:32:07 +00001893void TfLiteParser::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
1894{
1895 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1896
1897 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1898 CHECK_VALID_SIZE(inputs.size(), 1);
1899
1900 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1901 CHECK_VALID_SIZE(outputs.size(), 1);
1902
1903 auto layerName = boost::str(boost::format("Quantize:%1%:%2%") % subgraphIndex % operatorIndex);
1904
1905 IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01001906 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan66dedc72019-12-10 16:32:07 +00001907
Sadik Armagand109a4d2020-07-28 10:42:13 +01001908 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan66dedc72019-12-10 16:32:07 +00001909 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1910
1911 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1912 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1913
1914 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1915 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1916}
Finn Williamsc42c3842019-01-22 14:18:11 +00001917
Sadik Armagan58f39192018-09-17 14:14:39 +01001918void TfLiteParser::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
1919{
Finn Williamsc42c3842019-01-22 14:18:11 +00001920 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
Sadik Armagan58f39192018-09-17 14:14:39 +01001921}
1922
1923void TfLiteParser::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
1924{
Finn Williamsc42c3842019-01-22 14:18:11 +00001925 ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
1926}
Sadik Armagan58f39192018-09-17 14:14:39 +01001927
Sadik Armagan12239e72020-05-27 11:06:17 +01001928void TfLiteParser::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
1929{
Jan Eilers2f746b32020-07-28 14:00:06 +01001930 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
Sadik Armagan12239e72020-05-27 11:06:17 +01001931}
1932
Finn Williamsc42c3842019-01-22 14:18:11 +00001933void TfLiteParser::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
1934{
1935 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
1936}
1937
Nina Drozd99851762019-04-09 09:37:38 +01001938void TfLiteParser::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
1939{
1940 ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
1941}
1942
Jan Eilers2f746b32020-07-28 14:00:06 +01001943void TfLiteParser::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
1944{
1945 ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
1946}
Finn Williamsc42c3842019-01-22 14:18:11 +00001947
1948void TfLiteParser::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
1949{
1950 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Sadik Armagan58f39192018-09-17 14:14:39 +01001951 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Jan Eilers8eb25602020-03-09 12:13:48 +00001952 IgnoreUnused(operatorPtr);
Sadik Armagan58f39192018-09-17 14:14:39 +01001953
1954 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1955 CHECK_VALID_SIZE(inputs.size(), 1);
1956
1957 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1958 CHECK_VALID_SIZE(outputs.size(), 1);
1959
Finn Williamsc42c3842019-01-22 14:18:11 +00001960 auto layerName = str(boost::format("Activation:"));
Sadik Armagan58f39192018-09-17 14:14:39 +01001961 ActivationDescriptor activationDesc;
Finn Williamsc42c3842019-01-22 14:18:11 +00001962 activationDesc.m_Function = activationType;
1963
1964 switch (activationType)
1965 {
1966 case ActivationFunction::ReLu:
1967 {
1968 layerName += str(boost::format("RELU:%1%:%2%") % subgraphIndex % operatorIndex);
1969 break;
1970 }
1971 case ActivationFunction::BoundedReLu:
1972 {
1973 layerName += str(boost::format("RELU6:%1%:%2%") % subgraphIndex % operatorIndex);
1974 activationDesc.m_A = 6.0f;
1975 activationDesc.m_B = 0.0f;
1976 break;
1977 }
1978 case ActivationFunction::Sigmoid:
1979 {
1980 layerName += str(boost::format("SIGMOID:%1%:%2%") % subgraphIndex % operatorIndex);
1981 break;
1982 }
Nina Drozd99851762019-04-09 09:37:38 +01001983 case ActivationFunction::TanH:
1984 {
1985 layerName += str(boost::format("TANH:%1%:%2%") % subgraphIndex % operatorIndex);
1986 activationDesc.m_A = 1.0f;
1987 activationDesc.m_B = 1.0f;
1988 break;
1989 }
Sadik Armagan12239e72020-05-27 11:06:17 +01001990 case ActivationFunction::LeakyReLu:
1991 {
1992 layerName += str(boost::format("LEAKYRELU:%1%:%2%") % subgraphIndex % operatorIndex);
1993 const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
1994 activationDesc.m_A = options->alpha;
1995 break;
1996 }
Jan Eilers2f746b32020-07-28 14:00:06 +01001997 case ActivationFunction::HardSwish:
1998 layerName += str(boost::format("HARDSWISH:%1%:%2%") % subgraphIndex % operatorIndex);
1999 break;
Finn Williamsc42c3842019-01-22 14:18:11 +00002000 default:
2001 {
2002 throw ParseException(
2003 boost::str(boost::format("Unexpected ActivationFunction[%1%] when creating layerName "
2004 " %2% ") %static_cast<int>(activationType)% CHECK_LOCATION().AsString()));
2005 }
2006 }
2007
2008 IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
Sadik Armagan58f39192018-09-17 14:14:39 +01002009
Sadik Armagand109a4d2020-07-28 10:42:13 +01002010 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan58f39192018-09-17 14:14:39 +01002011 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2012
2013 // register the input connection slots for the layer, connections are made after all layers have been created
2014 // only the tensors for the inputs are relevant, exclude the const tensors
2015 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2016 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2017
2018 // register the output connection slots for the layer, connections are made after all layers have been created
2019 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2020 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2021}
Sadikb94967b2018-09-19 15:30:00 +01002022armnn::TensorInfo TfLiteParser::OutputShapeOfReshape(const armnn::TensorInfo & inputTensorInfo,
2023 const std::vector<int32_t> & targetDimsIn)
2024{
2025 std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2026 const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2027
2028 if (stretchDim != targetDimsIn.end())
2029 {
2030 if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2031 {
2032 throw ParseException(
2033 boost::str(
2034 boost::format("At most one component of shape can be -1 %1%") % CHECK_LOCATION().AsString()));
2035 }
2036
2037 auto targetNumElements =
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002038 armnn::numeric_cast<unsigned int>(
Sadikb94967b2018-09-19 15:30:00 +01002039 std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2040
2041 auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2042 outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2043 }
2044
2045 TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2046
2047 TensorInfo reshapeInfo = inputTensorInfo;
2048 reshapeInfo.SetShape(outputShape);
2049
2050 return reshapeInfo;
2051}
2052
2053void TfLiteParser::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
2054{
2055 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2056
2057 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002058
2059 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2060 CHECK_VALID_SIZE(outputs.size(), 1);
2061
2062 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2063 const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
James Conroy05102392020-06-24 15:39:55 +01002064 auto layerName = boost::str(boost::format("Reshape:%1%:%2%") % subgraphIndex % operatorIndex);
Sadikb94967b2018-09-19 15:30:00 +01002065
2066 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
kevmay0171972a82018-12-17 14:28:03 +00002067 armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
James Conroy05102392020-06-24 15:39:55 +01002068 CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
Derek Lambertic9e52792020-03-11 11:42:26 +00002069
Jan Eilersbac9b352020-07-13 13:40:24 +01002070 // Extracting new shape for the output
2071 // There are two ways it can be passed
2072 // * First is to define the target shape in the operator built-in options
2073 // * Second is to pass it as a second input tensor
Derek Lambertic9e52792020-03-11 11:42:26 +00002074 std::vector<int32_t> targetShape;
Jan Eilersbac9b352020-07-13 13:40:24 +01002075 bool targetShapeFound = false;
2076 // Check if built-in options were given
2077 if (options != nullptr)
Derek Lambertic9e52792020-03-11 11:42:26 +00002078 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002079 // make sure the parameter is given
2080 if (options->new_shape.empty() == false)
Derek Lambertic9e52792020-03-11 11:42:26 +00002081 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002082 targetShape = options->new_shape;
2083 targetShapeFound = true;
Derek Lambertif4a953f2020-03-17 14:25:57 +00002084 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002085 }
Jan Eilersbac9b352020-07-13 13:40:24 +01002086
2087 // If there is no built-in option given or if the built-in new_shape parameter was empty
2088 if (!targetShapeFound)
Derek Lambertic9e52792020-03-11 11:42:26 +00002089 {
Jan Eilersbac9b352020-07-13 13:40:24 +01002090 // Check for a second input tensor
2091 if (inputs.size() > 1 && inputs[1] != nullptr)
2092 {
2093 if (inputs[1]->is_variable)
2094 {
2095 ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2096 }
2097
2098 if (inputs[1]->shape.size() != 1)
2099 {
2100 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2101 }
2102
2103 if (inputs[1]->type != tflite::TensorType_INT32)
2104 {
2105 ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2106 }
2107
2108 // Extract target shape from input
2109 auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2110 auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
2111 for (int i=0; i < inputs[1]->shape[0]; ++i)
2112 {
2113 targetShape.push_back(values[i]);
2114 }
2115 }
2116 else
Derek Lambertic9e52792020-03-11 11:42:26 +00002117 {
2118 ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2119 "At least one method required");
2120 }
Derek Lambertic9e52792020-03-11 11:42:26 +00002121 }
2122
kevmay0171972a82018-12-17 14:28:03 +00002123 armnn::TensorInfo reshapeOutputTensorInfo =
Derek Lambertic9e52792020-03-11 11:42:26 +00002124 TfLiteParser::OutputShapeOfReshape(inputTensorInfo, targetShape);
Sadikb94967b2018-09-19 15:30:00 +01002125
kevmay0171972a82018-12-17 14:28:03 +00002126 // Check for valid input size and that reshape parameters equal output shape
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002127 const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2128 if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
kevmay0171972a82018-12-17 14:28:03 +00002129 {
2130 std::stringstream ss;
2131 ss << "New shape defined in reshape parameters "
Aron Virginas-Tar70672f62019-01-23 14:00:00 +00002132 << reshapeOutputTensorShape
kevmay0171972a82018-12-17 14:28:03 +00002133 << " does not equal output shape "
2134 << actualOutputTensorInfo.GetShape()
2135 << ": "
2136 << CHECK_LOCATION().AsString();
2137 throw ParseException(ss.str());
2138 }
2139
Sadikb94967b2018-09-19 15:30:00 +01002140 ReshapeDescriptor reshapeDesc;
kevmay0171972a82018-12-17 14:28:03 +00002141 reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
Sadikb94967b2018-09-19 15:30:00 +01002142
Sadikb94967b2018-09-19 15:30:00 +01002143 IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002144 ARMNN_ASSERT(layer != nullptr);
kevmay0171972a82018-12-17 14:28:03 +00002145 layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
Sadikb94967b2018-09-19 15:30:00 +01002146
2147 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2148 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2149
2150 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2151 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2152}
2153
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002154void TfLiteParser::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
2155{
Sadik Armagana3b31f02019-12-05 09:08:53 +00002156 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2157}
2158
2159void TfLiteParser::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
2160{
2161 ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2162}
2163
2164void TfLiteParser::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
2165{
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002166 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2167
2168 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2169 CHECK_VALID_SIZE(inputs.size(), 2);
2170
2171 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2172 CHECK_VALID_SIZE(outputs.size(), 1);
2173
2174 armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2175
2176 // Data for the parsed tensor args (size) must be stored locally.
2177 std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2178
2179 BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2180 ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2181
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002182 ResizeDescriptor desc;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002183 desc.m_Method = resizeMethod;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002184 desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
Aron Virginas-Tar169d2f12019-07-01 19:01:44 +01002185 desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2186 desc.m_DataLayout = armnn::DataLayout::NHWC;
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002187
Sadik Armagana3b31f02019-12-05 09:08:53 +00002188 auto layerName = str(boost::format("Resize:"));
2189
2190 switch (resizeMethod)
2191 {
2192 case ResizeMethod::Bilinear:
2193 {
2194 layerName += str(boost::format("BILINEAR:%1%:%2%") % subgraphIndex % operatorIndex);
Sang-Hoon Park820eb142020-01-08 10:25:24 +00002195
2196 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2197 const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2198
David Monahan4a0c9b92020-05-30 09:48:39 +01002199 desc.m_AlignCorners = options->align_corners;
Sadik Armagana3b31f02019-12-05 09:08:53 +00002200 break;
2201 }
2202 case ResizeMethod::NearestNeighbor:
2203 {
2204 layerName += str(boost::format("NEARESTNEIGHBOR:%1%:%2%") % subgraphIndex % operatorIndex);
2205 break;
2206 }
2207 default:
2208 {
2209 throw ParseException(
2210 boost::str(boost::format("Unexpected ResizeMethod[%1%] when creating layerName "
2211 " %2% ") %static_cast<int>(resizeMethod)% CHECK_LOCATION().AsString()));
2212 }
2213 }
2214
James Conroy05102392020-06-24 15:39:55 +01002215 TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002216 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002217 CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2218
2219 IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2220 ARMNN_ASSERT(layer != nullptr);
Bruno Goncalves3f58ddb2019-02-07 18:40:11 -02002221 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2222
2223 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2224 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2225
2226 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2227 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2228}
2229
Sadik Armagan479045b2018-10-01 11:51:37 +01002230void TfLiteParser::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
2231{
2232 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2233
2234 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2235 const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2236
2237 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2238
2239 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2240 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2241 CHECK_VALID_SIZE(outputs.size(), 1);
2242
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002243 unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2244 uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
Sadik Armagan479045b2018-10-01 11:51:37 +01002245
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002246 const unsigned int concatDimInput = static_cast<unsigned int>(
2247 (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
Sadik Armagan479045b2018-10-01 11:51:37 +01002248
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002249 OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2250 concatDescriptor.SetConcatAxis(concatDimInput);
Sadik Armagan479045b2018-10-01 11:51:37 +01002251
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002252 unsigned int mergeDimOrigin = 0;
Sadik Armagan479045b2018-10-01 11:51:37 +01002253
2254 for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2255 {
2256 TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2257
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002258 // This set up concatDescriptor view origin
2259 armnnUtils::ProcessConcatInputTensorInfo(
2260 inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
Sadik Armagan479045b2018-10-01 11:51:37 +01002261 }
2262
2263 auto layerName = boost::str(boost::format("Concatenation:%1%:%2%") % subgraphIndex % operatorIndex);
Sadik Armagand109a4d2020-07-28 10:42:13 +01002264 TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
James Conroy05102392020-06-24 15:39:55 +01002265
Jim Flynn906f9462019-05-10 13:55:21 +01002266 IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002267 ARMNN_ASSERT(layer != nullptr);
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002268 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Sadik Armagan479045b2018-10-01 11:51:37 +01002269
James Conroy05102392020-06-24 15:39:55 +01002270 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002271 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
Sadik Armagan479045b2018-10-01 11:51:37 +01002272
Nattapat Chaimanowong5e9d2982019-01-25 13:20:39 +00002273 // add fused activation layer
2274 layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
Sadik Armagan479045b2018-10-01 11:51:37 +01002275
Sadik Armagan479045b2018-10-01 11:51:37 +01002276 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2277 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2278}
2279
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002280void TfLiteParser::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
2281{
2282 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2283
2284 const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2285 const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2286
2287 CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2288
2289 FullyConnectedDescriptor desc;
2290 desc.m_BiasEnabled = false;
Nattapat Chaimanowongd8eee592018-10-26 10:24:14 +01002291 desc.m_TransposeWeightMatrix = true;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002292
2293 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2294 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2295 CHECK_VALID_SIZE(outputs.size(), 1);
2296
2297 armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2298
2299 // Fully Connected Layer accepts two dimensional weights input
2300 int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2301 if (weightsDimension != 2)
2302 {
2303 throw ParseException(
2304 boost::str(
2305 boost::format(
2306 "Dimension %1% for Fully Connected weights is not supported by Armnn. "
2307 "Node %2%")
2308 % weightsDimension
2309 % CHECK_LOCATION().AsString()));
2310 }
2311
Matteo Martincigh747ef822018-12-18 09:26:39 +00002312 auto filterTensorAndData = CreateConstTensor(inputs[1],
2313 filterTensorInfo,
2314 armnn::Optional<armnn::PermutationVector&>());
Matthew Jackson74bf7da2019-08-16 16:51:42 +01002315 armnn::IConnectableLayer* layer = nullptr;
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002316 auto layerName = boost::str(boost::format("FullyConnected:%1%:%2%") % subgraphIndex % operatorIndex);
2317
2318 if (inputs.size() == 3)
2319 {
2320 desc.m_BiasEnabled = true;
2321 TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
Matteo Martincigh747ef822018-12-18 09:26:39 +00002322 auto biasTensorAndData = CreateConstTensor(inputs[2],
2323 biasTensorInfo,
2324 armnn::Optional<armnn::PermutationVector&>());
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002325 layer = m_Network->AddFullyConnectedLayer(desc,
2326 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01002327 Optional<ConstTensor>(biasTensorAndData.first),
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002328 layerName.c_str());
2329 }
2330 else
2331 {
2332 layer = m_Network->AddFullyConnectedLayer(desc,
2333 filterTensorAndData.first,
Matteo Martincighfc598e12019-05-14 10:36:13 +01002334 EmptyOptional(),
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002335 layerName.c_str());
2336 }
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002337 ARMNN_ASSERT(layer != nullptr);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002338
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002339 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2340
2341 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2342
2343 if (inputTensorInfo.GetNumDimensions() > 2)
2344 {
2345 // Add reshape to flatten to 2D [batch_size, input_size],
2346 // where "input_size" corresponds to the number of inputs to the layer,
2347 // matching the second dimension of weights,
2348 // and "batch_size" is calculated by dividing the number of elements by "input_size".
2349 std::vector<unsigned int> reshapedDimensions(2);
2350 reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2351 reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
2352
2353 if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
2354 {
2355 throw ParseException(
2356 boost::str(
2357 boost::format(
2358 "Failed to deduce input tensor shape from filter size %1%")
2359 % reshapedDimensions[1]
2360 % CHECK_LOCATION().AsString()));
2361 }
2362
2363 armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
2364 reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
2365
2366 std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName());
2367 armnn::ReshapeDescriptor desc;
2368 desc.m_TargetShape = reshapedTensorInfo.GetShape();
2369 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2370
2371 reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
2372 reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
2373
2374 RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
2375 }
2376 else
2377 {
2378 // register the input connection slot for the layer
2379 // only the tensors for the inputs are relevant, exclude the const tensors
2380 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2381 }
2382
Sadik Armagand109a4d2020-07-28 10:42:13 +01002383 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002384 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2385
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002386 // we need to add the activation layer and fortunately we don't need to care about the data layout
2387 armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
2388 options->fused_activation_function);
Narumol Prangnawarat501f4d42019-04-24 15:52:20 +01002389
Sadik Armagan8853c1f2018-10-22 09:04:18 +01002390 // register the output connection slots for the layer, connections are made after all layers have been created
2391 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2392 RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2393}
2394
keidav011b3e2ea2019-02-21 10:07:37 +00002395void TfLiteParser::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
2396{
2397 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2398
2399 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2400
2401 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2402 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2403 CHECK_VALID_SIZE(outputs.size(), 4);
2404
2405 // Obtain custom options from flexbuffers
2406 auto custom_options = operatorPtr->custom_options;
2407 const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2408
2409 // Obtain descriptor information from tf lite
2410 DetectionPostProcessDescriptor desc;
2411 desc.m_MaxDetections = m["max_detections"].AsUInt32();
2412 desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
2413 desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
2414 desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
2415 desc.m_NumClasses = m["num_classes"].AsUInt32();
2416 desc.m_ScaleH = m["h_scale"].AsFloat();
2417 desc.m_ScaleW = m["w_scale"].AsFloat();
2418 desc.m_ScaleX = m["x_scale"].AsFloat();
2419 desc.m_ScaleY = m["y_scale"].AsFloat();
2420
keidav0107d58c72019-02-26 11:57:39 +00002421 if (!(m["use_regular_nms"].IsNull()))
keidav011b3e2ea2019-02-21 10:07:37 +00002422 {
keidav0107d58c72019-02-26 11:57:39 +00002423 desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
keidav011b3e2ea2019-02-21 10:07:37 +00002424 }
2425 if (!(m["detections_per_class"].IsNull()))
2426 {
2427 desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
2428 }
2429
2430 if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
2431 {
2432 throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
2433 "must be positive and less than or equal to 1.");
2434 }
2435
2436 armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
2437 auto anchorTensorAndData = CreateConstTensor(inputs[2], anchorTensorInfo,
2438 armnn::Optional<armnn::PermutationVector&>());
2439
2440 auto layerName = boost::str(boost::format("DetectionPostProcess:%1%:%2%") % subgraphIndex % operatorIndex);
2441 IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData.first,
2442 layerName.c_str());
2443
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002444 ARMNN_ASSERT(layer != nullptr);
keidav011b3e2ea2019-02-21 10:07:37 +00002445
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002446 // The model does not specify the output shapes.
2447 // The output shapes are calculated from the max_detection and max_classes_per_detection.
2448 unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
2449 m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2450 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2451 m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2452 m_OverridenOutputShapes.push_back({ 1 });
2453
keidav011b3e2ea2019-02-21 10:07:37 +00002454 for (unsigned int i = 0 ; i < outputs.size() ; ++i)
2455 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00002456 armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
keidav011b3e2ea2019-02-21 10:07:37 +00002457 layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
2458 }
2459
2460 // Register the input connection slots for the layer, connections are made after all layers have been created
2461 // only the tensors for the inputs are relevant, exclude the const tensors
2462 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2463 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2464
2465 // Register the output connection slots for the layer, connections are made after all layers have been created
2466 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2467 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2468 outputTensorIndexes[1],
2469 outputTensorIndexes[2],
2470 outputTensorIndexes[3]});
2471}
2472
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002473/// The TfLite Pack operator is equivalent to the ArmNN Stack operator
2474void TfLiteParser::ParsePack(size_t subgraphIndex, size_t operatorIndex)
2475{
2476 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2477
2478 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2479 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2480 CHECK_VALID_SIZE(outputs.size(), 1);
2481
2482 if (inputs.size() < 1)
2483 {
2484 throw ParseException("Pack must have at least one input.");
2485 }
2486
2487 const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2488 const auto* options = operatorPtr->builtin_options.AsPackOptions();
2489
2490 StackDescriptor desc;
2491 desc.m_Axis = static_cast<uint32_t>(options->axis);
2492 desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
2493
2494 // Use the tensor shape of the first input as the "correct" input shape in the descriptor
2495 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2496 desc.m_InputShape = inputTensorInfo.GetShape();
2497
2498 auto layerName = boost::str(boost::format("Pack:%1%:%2%") % subgraphIndex % operatorIndex);
2499 IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
2500
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002501 ARMNN_ASSERT(layer != nullptr);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002502
Sadik Armagand109a4d2020-07-28 10:42:13 +01002503 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
Matthew Jacksonbcca1f42019-07-16 11:39:21 +01002504 layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2505
2506 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2507 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2508
2509 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2510 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2511}
2512
Nina Drozd200e3802019-04-15 09:47:39 +01002513void TfLiteParser::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
2514{
2515 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2516
2517 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2518 const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2519
2520 // This unpackAxis indicates the axis to unpack
2521 const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
2522
2523 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2524 CHECK_VALID_SIZE(inputs.size(), 1);
2525
2526 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002527
2528 if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2529 {
2530 throw ParseException(
2531 boost::str(
2532 boost::format(
2533 "The unpack axis: %1% cannot be greater than or equal to "
2534 "the number of input dimension %2% %3%")
2535 % unpackAxis
2536 % inputTensorInfo.GetNumDimensions()
2537 % CHECK_LOCATION().AsString()));
2538 }
2539
Nina Drozd200e3802019-04-15 09:47:39 +01002540 unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
2541 // If num is not defined, automatically infer from the length of the dimension axis.
2542 if(unpackNum == 0)
2543 {
2544 unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2545 }
2546
2547 // If unpack number cannot be inferred and is still zero, throw ParseException.
2548 if(unpackNum == 0)
2549 {
2550 throw ParseException("Number to unpack must greater than zero.");
2551 }
2552
2553 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2554 CHECK_VALID_SIZE(outputs.size(), unpackNum);
2555
2556 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2557 std::vector<unsigned int> unpackDimSizes(inputDimSize);
2558
2559 // Add current input shape to unpackDimSizes
2560 for (unsigned int i = 0; i < inputDimSize; ++i)
2561 {
2562 unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2563 }
2564
2565 if (unpackDimSizes[unpackAxis] != unpackNum)
2566 {
2567 throw ParseException("Number to unpack must be the same as length of the dimension to "
2568 "unpack along.");
2569 }
2570
2571 unpackDimSizes[unpackAxis] /= unpackNum;
2572
2573 SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2574 for (unsigned int j = 0; j < unpackNum; ++j)
2575 {
2576 // Set the size of the views.
2577 for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2578 {
2579 splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2580 }
2581 splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
2582 }
2583
2584 auto layerName = boost::str(boost::format("Unpack:%1%:%2%") % subgraphIndex % operatorIndex);
2585 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002586 ARMNN_ASSERT(layer != nullptr);
Nina Drozd200e3802019-04-15 09:47:39 +01002587
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002588 TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
2589 unpackDimSizes.data());
2590
Nina Drozd200e3802019-04-15 09:47:39 +01002591 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2592 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2593
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002594 // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
2595 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2596 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002597 armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002598 std::string reshapeLayerName = boost::str(boost::format("Reshape_for:%1%") % layer->GetName());
2599 armnn::ReshapeDescriptor desc;
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002600 desc.m_TargetShape = outputTensorInfo.GetShape();
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002601 armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2602
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002603 layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
2604 outputTensorInfo.GetDataType(),
2605 outputTensorInfo.GetQuantizationScale(),
2606 outputTensorInfo.GetQuantizationOffset()));
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002607 layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
2608
Narumol Prangnawarat2c526462019-10-21 14:58:26 +01002609 reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
Narumol Prangnawarat672de572019-04-23 15:28:06 +01002610
2611 uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
2612 armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
2613 RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2614 }
Nina Drozd200e3802019-04-15 09:47:39 +01002615}
2616
Nina Drozd0324f482019-04-08 10:52:10 +01002617void TfLiteParser::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
2618{
2619 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2620
2621 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2622 const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2623
2624 const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2625
Nina Drozd200e3802019-04-15 09:47:39 +01002626 // If number of splits cannot be inferred and is zero, throw ParseException.
2627 if(numSplits == 0)
2628 {
2629 throw ParseException("Number to splits must greater than zero.");
2630 }
2631
Nina Drozd0324f482019-04-08 10:52:10 +01002632 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2633 CHECK_VALID_SIZE(inputs.size(), 2);
2634 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2635 CHECK_VALID_SIZE(outputs.size(), numSplits);
2636
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002637 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
2638 armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
Nina Drozd0324f482019-04-08 10:52:10 +01002639
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002640 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
2641 std::vector<unsigned int> axisData(axisTensorInfo.GetNumElements());
2642 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2643
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01002644 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002645 const unsigned int splitDim = axisData[0];
Nina Drozd0324f482019-04-08 10:52:10 +01002646
Nina Drozd0324f482019-04-08 10:52:10 +01002647 auto inputDimSize = inputTensorInfo.GetNumDimensions();
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002648 if (inputDimSize > MaxNumOfTensorDimensions)
Nina Drozd0324f482019-04-08 10:52:10 +01002649 {
2650 throw ParseException(
2651 boost::str(
2652 boost::format(
2653 "The number of dimensions: %1% for input tensors of the "
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002654 "split op cannot be greater than %2% %3%")
Nina Drozd0324f482019-04-08 10:52:10 +01002655 % inputTensorInfo.GetNumDimensions()
2656 % MaxNumOfTensorDimensions
2657 % CHECK_LOCATION().AsString()));
2658 }
2659
2660 std::vector<unsigned int> splitterDimSizes(inputDimSize);
2661
2662 // Add current input shape to splitterDimSizes
2663 for (unsigned int i = 0; i < inputDimSize; ++i)
2664 {
2665 splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2666 }
2667
2668 if (splitterDimSizes[splitDim] % numSplits != 0)
2669 {
2670 throw ParseException("Number of splits must evenly divide the dimension");
2671 }
2672 splitterDimSizes[splitDim] /= numSplits;
2673
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002674 SplitterDescriptor splitDesc(numSplits, inputDimSize);
Nina Drozd0324f482019-04-08 10:52:10 +01002675 for (unsigned int j = 0; j < numSplits; ++j)
2676 {
2677 // Set the size of the views.
2678 for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2679 {
2680 splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2681 }
2682 splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
2683 }
2684
2685 auto layerName = boost::str(boost::format("Split:%1%:%2%") % subgraphIndex % operatorIndex);
2686 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002687 ARMNN_ASSERT(layer != nullptr);
Nina Drozd0324f482019-04-08 10:52:10 +01002688
2689 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
Narumol Prangnawarat17660e62019-04-18 16:56:19 +01002690 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
Nina Drozd0324f482019-04-08 10:52:10 +01002691
Nina Drozd0324f482019-04-08 10:52:10 +01002692 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2693 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002694 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Francis Murtagh98d6b3d2019-10-21 10:52:54 +01002695 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
Nina Drozd0324f482019-04-08 10:52:10 +01002696 }
2697
2698 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2699 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2700}
2701
Derek Lambertif0176992020-04-28 13:37:49 +01002702unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
2703{
2704 int numDims = armnn::numeric_cast<int>(numDimsIn);
2705 int v = idx < 0 ? numDims + idx : idx;
2706 ARMNN_ASSERT(v >= 0);
2707 ARMNN_ASSERT(v < numDims);
2708
2709 return static_cast<unsigned int>(v);
2710}
2711
2712void TfLiteParser::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
2713{
2714 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2715
2716 const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
Ryan OShea86704732020-05-26 11:41:04 +01002717 const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
Derek Lambertif0176992020-04-28 13:37:49 +01002718
2719 auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2720 CHECK_VALID_SIZE(inputs.size(), 3);
2721
2722 auto& inputTensor = inputs[0];
2723 auto& splitsTensor = inputs[1];
2724 auto& axisTensor = inputs[2];
2725
2726 armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
2727 armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
2728 armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
2729 ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2730
2731 // Inputs
2732 auto inputDimSize = inputTensorInfo.GetNumDimensions();
2733 if (inputDimSize > MaxNumOfTensorDimensions)
2734 {
2735 throw ParseException(
2736 boost::str(
2737 boost::format(
2738 "The number of dimensions: %1% for input tensors of the "
Jan Eilersc0761e92020-06-29 16:48:44 +01002739 "SplitV op cannot be greater than %2% %3%")
Derek Lambertif0176992020-04-28 13:37:49 +01002740 % inputTensorInfo.GetNumDimensions()
2741 % MaxNumOfTensorDimensions
2742 % CHECK_LOCATION().AsString()));
2743 }
2744
2745 // Get split axis
2746 BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
2747 std::vector<int> axisData(axisTensorInfo.GetNumElements());
2748 ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2749 const unsigned int splitDim = ComputeWrappedIndex(axisData[0], inputTensorInfo.GetNumDimensions());
2750
Derek Lambertif0176992020-04-28 13:37:49 +01002751 // Set split sizes
Derek Lambertif0176992020-04-28 13:37:49 +01002752 CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
Ryan OShea86704732020-05-26 11:41:04 +01002753 unsigned int numSplits{0};
2754
2755 if(options)
Derek Lambertif0176992020-04-28 13:37:49 +01002756 {
2757 numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
Derek Lambertif0176992020-04-28 13:37:49 +01002758 }
2759 else
2760 {
Ryan OShea86704732020-05-26 11:41:04 +01002761 numSplits = splitsInfo.GetNumElements();
Derek Lambertif0176992020-04-28 13:37:49 +01002762 }
2763
2764 if (numSplits <=0)
2765 {
2766 throw ParseException("SplitV has invalid number of splits");
2767 }
2768
Jan Eilersc0761e92020-06-29 16:48:44 +01002769 std::vector<int> splitsData(numSplits);
Ryan OShea86704732020-05-26 11:41:04 +01002770 BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
Jan Eilersc0761e92020-06-29 16:48:44 +01002771 ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
Ryan OShea86704732020-05-26 11:41:04 +01002772
Jan Eilersc0761e92020-06-29 16:48:44 +01002773 unsigned int idx = 0;
Ryan OShea86704732020-05-26 11:41:04 +01002774 int numInferred{0};
2775 unsigned int inferIdx{0};
2776 int splitSum{0};
2777 for (auto split : splitsData)
2778 {
2779 if (split < 0)
2780 {
2781 numInferred++;
2782 inferIdx = idx;
2783 }
2784 else
2785 {
2786 splitSum += split;
2787 }
2788 idx++;
2789 }
2790 // Check for inferred Axis
2791 if (numInferred == 0)
2792 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002793 if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
Ryan OShea86704732020-05-26 11:41:04 +01002794 {
2795 throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
2796 }
2797 }
2798 else if (numInferred == 1)
2799 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002800 splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
Ryan OShea86704732020-05-26 11:41:04 +01002801 }
2802 else
2803 {
2804 throw ParseException("Cannot infer split size for more than one split");
2805 }
2806
Derek Lambertif0176992020-04-28 13:37:49 +01002807 //Ouput size validation
2808 auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2809 CHECK_VALID_SIZE(outputs.size(), numSplits);
2810
2811 // Setup Armnn descriptor
2812 SplitterDescriptor splitDesc(numSplits, inputDimSize);
2813 unsigned int accumSplit = 0;
2814 for (unsigned int j = 0; j < numSplits; ++j)
2815 {
Matthew Sloyan589e3e82020-09-11 16:17:48 +01002816 unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
Derek Lambertif0176992020-04-28 13:37:49 +01002817
2818 // Set the size of the views.
2819 for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
2820 {
2821 unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
2822 if (dimIdx == splitDim)
2823 {
2824 dimSize = splitSize;
2825 }
2826 splitDesc.SetViewSize(j, dimIdx, dimSize);
2827 }
2828
2829 splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
2830 accumSplit += splitSize;
2831 }
2832
Ryan OShea86704732020-05-26 11:41:04 +01002833 auto layerName = boost::str(boost::format("SplitV:%1%:%2%") % subgraphIndex % operatorIndex);
Derek Lambertif0176992020-04-28 13:37:49 +01002834 IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
James Conroy05102392020-06-24 15:39:55 +01002835 ARMNN_ASSERT(layer != nullptr);
Derek Lambertif0176992020-04-28 13:37:49 +01002836
2837 auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2838 RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2839
2840 for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2841 {
Sadik Armagand109a4d2020-07-28 10:42:13 +01002842 armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
Derek Lambertif0176992020-04-28 13:37:49 +01002843 layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
2844 }
2845
2846 auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2847 RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2848}
2849
Sadik Armagan58f39192018-09-17 14:14:39 +01002850armnn::IConnectableLayer* TfLiteParser::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
2851 unsigned int outputSlot,
2852 tflite::ActivationFunctionType activationType)
telsoa01c577f2c2018-08-31 09:22:23 +01002853{
2854 ActivationDescriptor activationDesc;
2855 std::string layerName = prevLayer->GetName();
2856
2857 switch(activationType)
2858 {
2859 case tflite::ActivationFunctionType_NONE:
2860 {
2861 // this is a no-op: return previous layer
2862 return prevLayer;
2863 }
2864 case tflite::ActivationFunctionType_RELU:
2865 {
2866 activationDesc.m_Function = ActivationFunction::ReLu;
2867 layerName += ":RELU";
2868 break;
2869 }
2870 case tflite::ActivationFunctionType_RELU6:
2871 {
2872 activationDesc.m_Function = ActivationFunction::BoundedReLu;
2873 activationDesc.m_A = 6.0f;
2874 activationDesc.m_B = 0.0f;
2875 layerName += ":RELU6";
2876 break;
2877 }
2878 case tflite::ActivationFunctionType_TANH:
2879 {
2880 activationDesc.m_Function = ActivationFunction::TanH;
2881 activationDesc.m_A = 1.0f;
2882 activationDesc.m_B = 1.0f;
2883 layerName += ":TANH";
2884 break;
2885 }
2886
2887 // I only put these here as a reminder what others we could support
2888 case tflite::ActivationFunctionType_RELU_N1_TO_1:
2889 case tflite::ActivationFunctionType_SIGN_BIT:
2890 default:
2891 {
2892 throw ParseException(
2893 boost::str(
2894 boost::format("TfLite parser doesn't suppport fused activation: "
2895 "%1%/%2% %3% ") %
2896 activationType %
2897 tflite::EnumNameActivationFunctionType(activationType) %
2898 CHECK_LOCATION().AsString()));
2899
2900 }
2901 }
2902
2903 IConnectableLayer* activationLayer =
2904 m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2905
2906 auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
2907 prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
2908 activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
2909 return activationLayer;
2910}
2911
2912TfLiteParser::ModelPtr TfLiteParser::LoadModelFromFile(const char * fileName)
2913{
2914 if (fileName == nullptr)
2915 {
2916 throw InvalidArgumentException(boost::str(boost::format("Invalid (null) file name %1%") %
2917 CHECK_LOCATION().AsString()));
2918 }
Francis Murtagh532a29d2020-06-29 11:50:01 +01002919 std::error_code errorCode;
2920 fs::path pathToFile(fileName);
2921 if (!fs::exists(pathToFile, errorCode))
telsoa01c577f2c2018-08-31 09:22:23 +01002922 {
Derek Lambertic9e52792020-03-11 11:42:26 +00002923 std::string locationString = CHECK_LOCATION().AsString();
2924 std::string msg = boost::str(boost::format("Cannot find the file (%1%) errorCode: %2% %3%") %
2925 fileName %
2926 errorCode %
2927 locationString);
2928 throw FileNotFoundException(msg);
telsoa01c577f2c2018-08-31 09:22:23 +01002929 }
2930 std::ifstream file(fileName, std::ios::binary);
2931 std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
2932 return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
2933 fileContent.size());
2934}
2935
2936TfLiteParser::ModelPtr TfLiteParser::LoadModelFromBinary(const uint8_t * binaryContent, size_t len)
2937{
2938 if (binaryContent == nullptr)
2939 {
2940 throw InvalidArgumentException(boost::str(boost::format("Invalid (null) binary content %1%") %
2941 CHECK_LOCATION().AsString()));
2942 }
2943 flatbuffers::Verifier verifier(binaryContent, len);
2944 if (verifier.VerifyBuffer<tflite::Model>() == false)
2945 {
2946 throw ParseException(
2947 boost::str(boost::format("Buffer doesn't conform to the expected Tensorflow Lite "
2948 "flatbuffers format. size:%1% %2%") %
2949 len %
2950 CHECK_LOCATION().AsString()));
2951 }
2952 return tflite::UnPackModel(binaryContent);
2953}
2954
2955TfLiteParser::TensorRawPtrVector TfLiteParser::GetInputs(const ModelPtr & model,
2956 size_t subgraphIndex,
2957 size_t operatorIndex)
2958{
2959 CHECK_MODEL(model, subgraphIndex, operatorIndex);
2960
Derek Lambertiff05cc52019-04-26 13:05:17 +01002961 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
2962 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01002963
2964 size_t inputCount = operatorPtr->inputs.size();
2965 TensorRawPtrVector result(inputCount);
2966 for (size_t i=0; i<inputCount; ++i)
2967 {
2968 uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
Derek Lambertiff05cc52019-04-26 13:05:17 +01002969 result[i] = subgraphPtr->tensors[inputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01002970 }
2971 return result;
2972}
2973
2974TfLiteParser::TensorRawPtrVector TfLiteParser::GetOutputs(const ModelPtr & model,
2975 size_t subgraphIndex,
2976 size_t operatorIndex)
2977{
2978 CHECK_MODEL(model, subgraphIndex, operatorIndex);
2979
Derek Lambertiff05cc52019-04-26 13:05:17 +01002980 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
2981 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01002982
2983 size_t outputCount = operatorPtr->outputs.size();
2984 TensorRawPtrVector result(outputCount);
2985 for (size_t i=0; i<outputCount; ++i)
2986 {
2987 uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
2988 CHECK_TENSOR(model, subgraphIndex, outputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01002989 result[i] = subgraphPtr->tensors[outputId].get();
telsoa01c577f2c2018-08-31 09:22:23 +01002990 }
2991 return result;
2992}
2993
2994TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphInputs(const ModelPtr & model,
2995 size_t subgraphIndex)
2996{
2997 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01002998 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01002999
Derek Lambertiff05cc52019-04-26 13:05:17 +01003000 size_t inputCount = subgraphPtr->inputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003001 TensorIdRawPtrVector result(inputCount);
3002 for (size_t i=0; i<inputCount; ++i)
3003 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003004 uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
telsoa01c577f2c2018-08-31 09:22:23 +01003005 CHECK_TENSOR(model, subgraphIndex, inputId);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003006 result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003007 }
3008 return result;
3009}
3010
3011TfLiteParser::TensorIdRawPtrVector TfLiteParser::GetSubgraphOutputs(const ModelPtr & model,
3012 size_t subgraphIndex)
3013{
3014 CHECK_SUBGRAPH(model, subgraphIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003015 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003016
Derek Lambertiff05cc52019-04-26 13:05:17 +01003017 size_t outputCount = subgraphPtr->outputs.size();
telsoa01c577f2c2018-08-31 09:22:23 +01003018 TensorIdRawPtrVector result(outputCount);
3019 for (size_t i=0; i<outputCount; ++i)
3020 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003021 uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
3022 result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
telsoa01c577f2c2018-08-31 09:22:23 +01003023 }
3024 return result;
3025}
3026
3027std::vector<int32_t>& TfLiteParser::GetInputTensorIds(const ModelPtr& model,
3028 size_t subgraphIndex,
3029 size_t operatorIndex)
3030{
3031 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003032 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3033 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003034 return operatorPtr->inputs;
3035}
3036
3037std::vector<int32_t>& TfLiteParser::GetOutputTensorIds(const ModelPtr& model,
3038 size_t subgraphIndex,
3039 size_t operatorIndex)
3040{
3041 CHECK_MODEL(model, subgraphIndex, operatorIndex);
Derek Lambertiff05cc52019-04-26 13:05:17 +01003042 const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3043 const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
telsoa01c577f2c2018-08-31 09:22:23 +01003044 return operatorPtr->outputs;
3045}
3046
3047void TfLiteParser::RegisterInputSlots(size_t subgraphIndex,
3048 size_t operatorIndex,
3049 IConnectableLayer* layer,
3050 const std::vector<unsigned int>& tensorIndexes)
3051{
3052 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003053 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003054 if (tensorIndexes.size() != layer->GetNumInputSlots())
3055 {
3056 throw ParseException(
3057 boost::str(boost::format("The number of tensor inputs (%1%) does not match the number expected (%2%)"
3058 " for subgraph:%3% operator index:%4% %5%") %
3059 tensorIndexes.size() %
3060 layer->GetNumInputSlots() %
3061 subgraphIndex %
3062 operatorIndex %
3063 CHECK_LOCATION().AsString()));
3064 }
3065
3066 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumInputSlots(); ++slotIndex)
3067 {
3068 unsigned int tensorIndex = tensorIndexes[slotIndex];
3069 armnn::IInputSlot* slot = &(layer->GetInputSlot(slotIndex));
3070 RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3071 }
3072}
3073
3074void TfLiteParser::RegisterOutputSlots(size_t subgraphIndex,
3075 size_t operatorIndex,
3076 IConnectableLayer* layer,
3077 const std::vector<unsigned int>& tensorIndexes)
3078{
3079 CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
Narumol Prangnawaratac2770a2020-04-01 16:51:23 +01003080 ARMNN_ASSERT(layer != nullptr);
telsoa01c577f2c2018-08-31 09:22:23 +01003081 if (tensorIndexes.size() != layer->GetNumOutputSlots())
3082 {
3083 throw ParseException(
3084 boost::str(boost::format("The number of tensor outputs (%1%) does not match the number expected (%2%)"
3085 " for subgraph:%3% operator index:%4% %5%") %
3086 tensorIndexes.size() %
3087 layer->GetNumOutputSlots() %
3088 subgraphIndex %
3089 operatorIndex %
3090 CHECK_LOCATION().AsString()));
3091 }
3092
3093 for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
3094 {
3095 unsigned int tensorIndex = tensorIndexes[slotIndex];
3096 armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
3097 RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3098 }
3099}
3100
3101void TfLiteParser::SetupInputLayers(size_t subgraphIndex)
3102{
3103 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3104
3105 auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
3106 for (auto const & tensorIdAndPtr : inputs)
3107 {
3108 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3109 IConnectableLayer* layer =
3110 m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3111
3112 auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
3113 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3114
3115 RegisterOutputSlots(subgraphIndex,
3116 VIRTUAL_OPERATOR_ID,
3117 layer,
3118 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3119 }
3120}
3121
3122void TfLiteParser::SetupOutputLayers(size_t subgraphIndex)
3123{
3124 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3125
3126 auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
3127 for (auto const & tensorIdAndPtr : outputs)
3128 {
3129 auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3130 IConnectableLayer* layer =
3131 m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3132
3133 RegisterInputSlots(subgraphIndex,
3134 VIRTUAL_OPERATOR_ID,
3135 layer,
3136 { static_cast<uint32_t>(tensorIdAndPtr.first) });
3137 }
3138}
3139
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003140void TfLiteParser::SetupConstantLayers(size_t subgraphIndex)
3141{
3142 CHECK_SUBGRAPH(m_Model, subgraphIndex);
3143
Derek Lambertiff05cc52019-04-26 13:05:17 +01003144 const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003145 for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3146 {
3147 for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3148 {
3149 if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
3150 m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3151 {
Derek Lambertiff05cc52019-04-26 13:05:17 +01003152 TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
Bruno Goncalves3d7efe92018-12-27 14:21:43 -02003153 armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
3154 auto tensorAndData = CreateConstTensor(tensorPtr,
3155 tensorInfo,
3156 armnn::Optional<armnn::PermutationVector&>());
3157
3158 std::string layerName = boost::str(boost::format("Constant:%1%") % tensorPtr->name);
3159 IConnectableLayer *layer =
3160 m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
3161
3162 layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3163 RegisterOutputSlots(subgraphIndex,
3164 VIRTUAL_OPERATOR_ID,
3165 layer,
3166 { tensorIndex });
3167
3168 }
3169 }
3170 }
3171}
3172
telsoa01c577f2c2018-08-31 09:22:23 +01003173// example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
3174TfLiteParser::BufferRawPtr TfLiteParser::GetBuffer(const ModelPtr& model, size_t bufferIndex)
3175{
3176 CHECK_BUFFER(model, bufferIndex);
3177 return model->buffers[bufferIndex].get();
3178}
3179
Matteo Martincigh747ef822018-12-18 09:26:39 +00003180template<typename T>
3181std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage>
3182TfLiteParser::CreateConstTensorAndStoreData(TfLiteParser::BufferRawPtr bufferPtr,
3183 TfLiteParser::TensorRawPtr tensorPtr,
3184 armnn::TensorInfo& tensorInfo,
3185 armnn::Optional<armnn::PermutationVector&> permutationVector)
3186{
3187 auto constData = CreateConstTensorImpl<T>(bufferPtr,
3188 tensorPtr,
3189 tensorInfo,
3190 permutationVector);
3191 TfLiteParser::SupportedDataStorage storage(std::move(constData.second));
3192 return std::make_pair(constData.first, std::move(storage));
3193}
3194
telsoa01c577f2c2018-08-31 09:22:23 +01003195std::pair<armnn::ConstTensor, TfLiteParser::SupportedDataStorage>
3196TfLiteParser::CreateConstTensor(TensorRawPtr tensorPtr,
Matteo Martincigh747ef822018-12-18 09:26:39 +00003197 armnn::TensorInfo& tensorInfo,
3198 armnn::Optional<armnn::PermutationVector&> permutationVector)
telsoa01c577f2c2018-08-31 09:22:23 +01003199{
3200 CHECK_TENSOR_PTR(tensorPtr);
3201 auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3202 CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3203
3204 switch (tensorInfo.GetDataType())
3205 {
3206 case armnn::DataType::Float32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003207 return CreateConstTensorAndStoreData<float>(bufferPtr,
3208 tensorPtr,
3209 tensorInfo,
3210 permutationVector);
Derek Lambertif90c56d2020-01-10 17:14:08 +00003211 case armnn::DataType::QAsymmU8:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003212 return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3213 tensorPtr,
3214 tensorInfo,
3215 permutationVector);
Keith Davisd305e1a2020-01-22 11:57:54 +00003216 case armnn::DataType::QSymmS8:
3217 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3218 tensorPtr,
3219 tensorInfo,
3220 permutationVector);
Keith Davis67e6c542020-02-19 10:08:33 +00003221 case armnn::DataType::QAsymmS8:
3222 return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3223 tensorPtr,
3224 tensorInfo,
3225 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003226 case armnn::DataType::Signed32:
Matteo Martincigh747ef822018-12-18 09:26:39 +00003227 return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3228 tensorPtr,
3229 tensorInfo,
3230 permutationVector);
telsoa01c577f2c2018-08-31 09:22:23 +01003231 default:
3232 {
3233 std::stringstream errString;
3234 errString << "Unexpected datatype when creating const tensor: "
3235 << armnn::GetDataTypeName(tensorInfo.GetDataType())
3236 << " shape:" << tensorInfo.GetShape()
3237 << CHECK_LOCATION().AsString();
3238 throw ParseException(errString.str());
3239 }
3240 }
3241}
3242
3243BindingPointInfo TfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
3244 const std::string& name) const
3245{
3246 CHECK_SUBGRAPH(m_Model, subgraphId);
3247 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3248 for (auto const & input : inputs)
3249 {
3250 if (input.second->name == name)
3251 {
3252 auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3253 return std::make_pair(bindingId, ToTensorInfo(input.second));
3254 }
3255 }
3256
3257 std::stringstream bindings;
3258 for (auto const & input : inputs)
3259 {
3260 bindings << "'" << input.second->name << "' ";
3261 }
3262
3263 throw ParseException(
3264 boost::str(
3265 boost::format("No input binding found for subgraph:%1% and name:%2%. "
3266 "Possible inputs are: [%3%] %4%") %
3267 subgraphId %
3268 name %
3269 bindings.str() %
3270 CHECK_LOCATION().AsString()));
3271}
3272
3273BindingPointInfo TfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
3274 const std::string& name) const
3275{
3276 CHECK_SUBGRAPH(m_Model, subgraphId);
3277 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003278 for (unsigned int i = 0; i < outputs.size(); ++i)
telsoa01c577f2c2018-08-31 09:22:23 +01003279 {
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003280 auto const output = outputs[i];
telsoa01c577f2c2018-08-31 09:22:23 +01003281 if (output.second->name == name)
3282 {
3283 auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
Narumol Prangnawarat4628d052019-02-25 17:26:05 +00003284 std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3285 m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3286 return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
telsoa01c577f2c2018-08-31 09:22:23 +01003287 }
3288 }
3289
3290 std::stringstream bindings;
3291 for (auto const & output : outputs)
3292 {
3293 bindings << "'" << output.second->name << "' ";
3294 }
3295
3296 throw ParseException(
3297 boost::str(
3298 boost::format("No output binding found for subgraph:%1% and name:%2%. "
3299 "Possible outputs are: [%3%] %4%") %
3300 subgraphId %
3301 name %
3302 bindings.str() %
3303 CHECK_LOCATION().AsString()));
3304}
3305
3306size_t TfLiteParser::GetSubgraphCount() const
3307{
3308 return m_Model->subgraphs.size();
3309}
3310
3311std::vector<std::string> TfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
3312{
3313 CHECK_SUBGRAPH(m_Model, subgraphId);
3314 auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3315 std::vector<std::string> result;
3316 result.reserve(inputs.size());
3317 for (auto const & input : inputs)
3318 {
3319 result.push_back(input.second->name);
3320 }
3321 return result;
3322}
3323
3324std::vector<std::string> TfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
3325{
3326 CHECK_SUBGRAPH(m_Model, subgraphId);
3327 auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
3328 std::vector<std::string> result;
3329 result.reserve(outputs.size());
3330 for (auto const & output : outputs)
3331 {
3332 result.push_back(output.second->name);
3333 }
3334 return result;
3335}
3336
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003337ITfLiteParser* ITfLiteParser::CreateRaw(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
telsoa01c577f2c2018-08-31 09:22:23 +01003338{
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003339 return new TfLiteParser(options);
telsoa01c577f2c2018-08-31 09:22:23 +01003340}
3341
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003342ITfLiteParserPtr ITfLiteParser::Create(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
telsoa01c577f2c2018-08-31 09:22:23 +01003343{
Aron Virginas-Tarc975f922019-10-23 17:38:17 +01003344 return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
telsoa01c577f2c2018-08-31 09:22:23 +01003345}
3346
3347void ITfLiteParser::Destroy(ITfLiteParser* parser)
3348{
3349 delete parser;
3350}
3351
3352TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data)
3353: m_FloatData(std::move(data))
3354, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003355, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003356, m_Int32Data(nullptr)
3357{
3358}
3359
3360TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
3361: m_FloatData(nullptr)
3362, m_Uint8Data(std::move(data))
Keith Davisd305e1a2020-01-22 11:57:54 +00003363, m_Int8Data(nullptr)
3364, m_Int32Data(nullptr)
3365{
3366}
3367
3368TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
3369: m_FloatData(nullptr)
3370, m_Uint8Data(nullptr)
3371, m_Int8Data(std::move(data))
telsoa01c577f2c2018-08-31 09:22:23 +01003372, m_Int32Data(nullptr)
3373{
3374}
3375
3376TfLiteParser::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
3377: m_FloatData(nullptr)
3378, m_Uint8Data(nullptr)
Keith Davisd305e1a2020-01-22 11:57:54 +00003379, m_Int8Data(nullptr)
telsoa01c577f2c2018-08-31 09:22:23 +01003380, m_Int32Data(std::move(data))
3381{
3382}
3383
3384} // armnnTfLiteParser