ArmNN
 21.05
TfLiteParser.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "TfLiteParser.hpp"
7 
9 
10 #include <armnn/BackendOptions.hpp>
11 #include <armnn/Descriptors.hpp>
12 #include <armnn/Exceptions.hpp>
13 #include <armnn/Logging.hpp>
14 #include <armnn/Tensor.hpp>
16 #include <armnn/TypesUtils.hpp>
17 #include <armnn/utility/Assert.hpp>
20 
21 // armnnUtils:
22 #include <armnnUtils/Permute.hpp>
23 #include <Filesystem.hpp>
24 
25 #include <ParserHelper.hpp>
26 #include <VerificationHelpers.hpp>
27 
28 // The generated code based on the Tf Lite schema:
29 #include <schema_generated.h>
30 
31 #include <flatbuffers/flexbuffers.h>
32 
33 #include <fmt/format.h>
34 
35 #include <algorithm>
36 #include <fstream>
37 #include <iostream>
38 #include <limits>
39 #include <numeric>
40 #include <sstream>
41 
42 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \
43  { \
44  throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
45  << ": " \
46  << CHECK_LOCATION().AsString()).str()); \
47  }
48 
49 using namespace armnn;
51 namespace armnnTfLiteParser
52 {
53 
54 ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
55  pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
56 
57 ITfLiteParser::~ITfLiteParser() = default;
58 
59 ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
60 {
61  return new ITfLiteParser(options);
62 }
63 
64 ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
65 {
66  return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
67 }
68 
69 void ITfLiteParser::Destroy(ITfLiteParser* parser)
70 {
71  delete parser;
72 }
73 
74 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
75 {
76  return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
77 }
78 
79 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
80 {
81  return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
82 }
83 
84 BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
85  const std::string& name) const
86 {
87  return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
88 }
89 
90 BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
91  const std::string& name) const
92 {
93  return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
94 }
95 
96 size_t ITfLiteParser::GetSubgraphCount() const
97 {
98  return pTfLiteParserImpl->GetSubgraphCount();
99 }
100 
101 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
102 {
103  return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
104 }
105 
106 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
107 {
108  return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
109 }
110 
111 namespace
112 {
113 
114 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
115 
116 void CheckSubgraph(const TfLiteParserImpl::ModelPtr & model,
117  size_t subgraphIndex,
118  const CheckLocation & location)
119 {
120  if (model.get() == nullptr)
121  {
122  throw ParseException(
123  fmt::format("{} was called with invalid (null) model. "
124  "Possible reason is that the model is not yet loaded and Unpack(ed). "
125  "subgraph:{} at {}",
126  location.m_Function,
127  subgraphIndex,
128  location.FileLine()));
129  }
130  else if (subgraphIndex >= model->subgraphs.size())
131  {
132  throw ParseException(
133  fmt::format("{} was called with an invalid subgraph index. "
134  "subgraph:{} at {}",
135  location.m_Function,
136  subgraphIndex,
137  location.FileLine()));
138  }
139 }
140 
141 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
142  CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
143 
144 void CheckModel(const TfLiteParserImpl::ModelPtr & model,
145  size_t subgraphIndex,
146  size_t operatorIndex,
147  const CheckLocation & location)
148 {
149  if (model.get() == nullptr)
150  {
151  throw ParseException(
152  fmt::format("{} was called with invalid (null) model. "
153  "Possible reason is that the model is not yet loaded and Unpack(ed). "
154  "subgraph:{} operator:{} at {}",
155  location.m_Function,
156  subgraphIndex,
157  operatorIndex,
158  location.FileLine()));
159  }
160  else if (subgraphIndex >= model->subgraphs.size())
161  {
162  throw ParseException(
163  fmt::format("{} was called with an invalid subgraph index. "
164  "subgraph:{} operator:{} at {}",
165  location.m_Function,
166  subgraphIndex,
167  operatorIndex,
168  location.FileLine()));
169  }
170  else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
171  operatorIndex != VIRTUAL_OPERATOR_ID)
172  {
173  throw ParseException(
174  fmt::format("{} was called with an invalid operator index. "
175  "subgraph:{} operator:{} at {}",
176  location.m_Function,
177  subgraphIndex,
178  operatorIndex,
179  location.FileLine()));
180  }
181 }
182 
183 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
184  CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
185 
186 void CheckTensor(const TfLiteParserImpl::ModelPtr & model,
187  size_t subgraphIndex,
188  size_t tensorIndex,
189  const CheckLocation & location)
190 {
191  // not checking model, because I assume CHECK_MODEL already run
192  // and checked that. An assert would do.
193  ARMNN_ASSERT_MSG(model.get() != nullptr, "Expecting a valid model in this function");
194 
195  // also subgraph index should be checked by CHECK_MODEL so
196  // I only add an assert here
197  ARMNN_ASSERT_MSG(subgraphIndex < model->subgraphs.size(), "Expecting a valid subgraph index");
198 
199  // the tensor index is the only one to check here
200  if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
201  {
202  throw ParseException(
203  fmt::format("{} was called with an invalid tensor index. "
204  "subgraph:{} tensor:{} at {}",
205  location.m_Function,
206  subgraphIndex,
207  tensorIndex,
208  location.FileLine()));
209  }
210 }
211 
212 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
213  CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
214 
215 void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
216  const CheckLocation & location)
217 {
218  if (rawPtr == nullptr)
219  {
220  throw ParseException(
221  fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
222  }
223 }
224 
225 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
226  CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
227 
228 void CheckBuffer(const TfLiteParserImpl::ModelPtr & model,
229  size_t bufferIndex,
230  const CheckLocation & location)
231 {
232  if (model.get() == nullptr)
233  {
234  throw ParseException(
235  fmt::format("{} was called with invalid (null) model. "
236  "Possible reason is that the model is not yet loaded and Unpack(ed). "
237  "buffer:{} at {}",
238  location.m_Function,
239  bufferIndex,
240  location.FileLine()));
241  }
242  else if (bufferIndex >= model->buffers.size())
243  {
244  throw ParseException(
245  fmt::format("{} was called with an invalid buffer index. "
246  "buffer index:{} at {}",
247  location.m_Function,
248  bufferIndex,
249  location.FileLine()));
250  }
251  else if (model->buffers[bufferIndex].get() == nullptr)
252  {
253  throw ParseException(
254  fmt::format("The buffer #{} is null. {}",
255  bufferIndex,
256  location.AsString()));
257  }
258 }
259 
260 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
261  CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
262 
263 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
264  const armnn::TensorInfo & tensorInfo,
265  uint32_t bufferId,
266  const CheckLocation & location)
267 {
268  if (bufferPtr == nullptr)
269  {
270  throw ParseException(
271  fmt::format("BufferPtr is null for buffer:{}. {}",
272  bufferId,
273  location.AsString()));
274  }
275  else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
276  tensorInfo.GetNumBytes() > bufferPtr->data.size())
277  {
278  std::stringstream ss;
279  ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
280  << "For tensor: " << tensorInfo.GetShape()
281  << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
282  << tensorInfo.GetNumElements() << " elements. " << location.AsString();
283  throw ParseException(ss.str());
284  }
285 }
286 
287 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
288  CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
289 
290 bool IsActivationSupported(tflite::ActivationFunctionType activationType)
291 {
292  switch(activationType)
293  {
294  case tflite::ActivationFunctionType_NONE:
295  case tflite::ActivationFunctionType_RELU:
296  case tflite::ActivationFunctionType_RELU6:
297  case tflite::ActivationFunctionType_TANH:
298  {
299  return true;
300  }
301  default:
302  {
303  return false;
304  }
305  }
306 }
307 
308 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
309  do { \
310  if (IsActivationSupported(OPTION->fused_activation_function) == false) \
311  { \
312  throw ParseException( \
313  fmt::format("TfLite parser doesn't suppport fused activation: " \
314  "{}/{} in {} subgraph:{} operator:{} at {}", \
315  OPTION->fused_activation_function, \
316  tflite::EnumNameActivationFunctionType(\
317  OPTION->fused_activation_function), \
318  __func__, \
319  SUBGRAPH_INDEX, \
320  OPERATOR_INDEX, \
321  CHECK_LOCATION().FileLine())); \
322  } \
323  } while(false)
324 
325 
326 std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t> & in)
327 {
328  std::vector<unsigned int> result;
329  result.reserve(in.size());
330  for (auto & i : in)
331  {
332  // If the location of the input data is -1 then the input should be ignored.
333  if (i == -1)
334  {
335  continue;
336  }
337  result.push_back(CHECKED_NON_NEGATIVE(i));
338  }
339  return result;
340 }
341 
342 void CalcPadding(uint32_t inputSize,
343  uint32_t filterSize,
344  uint32_t stride,
345  uint32_t dilation,
346  uint32_t& paddingFront,
347  uint32_t& paddingBack,
348  tflite::Padding padding)
349 {
350  paddingFront = 0;
351  paddingBack = 0;
352  if (padding == tflite::Padding_SAME)
353  {
354  uint32_t outputSize = (inputSize + stride - 1) / stride;
355  uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
356  uint32_t temp = (outputSize - 1) * stride + dilatedSize;
357  if (temp > inputSize)
358  {
359  paddingFront = (temp - inputSize) / 2;
360  paddingBack = (temp - inputSize) - paddingFront;
361  }
362  }
363 }
364 
366  const std::vector<unsigned int>& shapes,
367  const bool outputTensor = false)
368 {
369  armnn::DataType type;
370  CHECK_TENSOR_PTR(tensorPtr);
371 
372  switch (tensorPtr->type)
373  {
374  case tflite::TensorType_UINT8:
376  break;
377  case tflite::TensorType_FLOAT32:
379  break;
380  case tflite::TensorType_INT8:
381  if (tensorPtr->quantization->zero_point.size() == 1)
382  {
383  // Per-tensor
385  }
386  else
387  {
388  // Per-channel
390  }
391  break;
392  case tflite::TensorType_INT16:
394  break;
395  case tflite::TensorType_INT32:
397  break;
398  case tflite::TensorType_INT64:
400  break;
401  case tflite::TensorType_BOOL:
403  break;
404  default:
405  {
406  CheckLocation location = CHECK_LOCATION();
407  throw ParseException(
408  fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
409  tensorPtr->type,
410  tflite::EnumNameTensorType(tensorPtr->type),
411  tensorPtr->name,
412  location.AsString()));
413  }
414  }
415  std::vector<unsigned int> safeShape = shapes;
416  bool isDynamic = false;
417  if (safeShape.size() == 0)
418  {
419  safeShape.push_back(1);
420  if (outputTensor)
421  {
422  isDynamic = true;
423  }
424  }
425 
426  float quantizationScale = 0.0f;
427  int32_t quantizationOffset = 0;
428 
429  if (tensorPtr->quantization.get())
430  {
431  if (tensorPtr->quantization->scale.size() <= 1)
432  {
433  CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
434  CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
435 
436  if (tensorPtr->quantization->scale.size() == 1)
437  {
438  quantizationScale = tensorPtr->quantization->scale[0];
439  }
440  if (tensorPtr->quantization->zero_point.size() == 1)
441  {
442  // NOTE: we lose precision here when converting from 64 bit to 32
443  // but this is what we support at the moment in ArmNN
444  quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
445  }
446 
447  TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
448  safeShape.data());
449  if (isDynamic)
450  {
451  tensorShape = TensorShape(1, false);
452  }
453  armnn::TensorInfo result(tensorShape,
454  type,
455  quantizationScale,
456  quantizationOffset);
457  return result;
458  }
459  else
460  {
461  std::vector<float> quantizationScales;
462  std::vector<int32_t> quantizationOffsets;
463 
464  // Scale
465  std::copy(tensorPtr->quantization->scale.begin(),
466  tensorPtr->quantization->scale.end(),
467  std::back_inserter(quantizationScales));
468 
469  // QSymmS8 Per-axis
470  TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
471  safeShape.data());
472  if (isDynamic)
473  {
474  tensorShape = TensorShape(1, false);
475  }
476  armnn::TensorInfo result(tensorShape,
477  type,
478  quantizationScales,
479  armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
480  return result;
481  }
482  }
483  else
484  {
485  TensorShape tensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()),
486  safeShape.data());
487  if (isDynamic)
488  {
489  tensorShape = TensorShape(1, false);
490  }
491  armnn::TensorInfo result(tensorShape,
492  type,
493  quantizationScale,
494  quantizationOffset);
495  return result;
496  }
497 }
498 
500 {
501  auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
502  return ToTensorInfo(tensorPtr, dimensions);
503 }
504 
506  const bool outputTensor)
507 {
508  auto const & dimensions = AsUnsignedVector(tensorPtr->shape);
509  return ToTensorInfo(tensorPtr, dimensions, outputTensor);
510 }
511 
512 template<typename T>
513 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
514 CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
516  armnn::TensorInfo& tensorInfo,
518 {
519  IgnoreUnused(tensorPtr);
520  ARMNN_ASSERT_MSG(tensorPtr != nullptr, "tensorPtr is null");
521  ARMNN_ASSERT_MSG(bufferPtr != nullptr,
522  fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
523 
524  std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
525 
526  if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
527  {
528  tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
529  armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
530  reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
531  }
532  else
533  {
534  ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
535  }
536 
537  return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
538 }
539 
540 armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
541 {
542  // generate the binding id by shifting the tensor id by 8 bit
543  // and add the subgraph id, which allows 256 subgraphs
544  return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
545 }
546 
547 bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
548 {
549  const unsigned int actualSize = actual.GetNumDimensions();
550  if (actualSize != expected.size())
551  {
552  return false;
553  }
554 
555  for (unsigned int i = 0u; i < actualSize; i++)
556  {
557  if (expected[i] < 0 ||
558  actual[i] != static_cast<unsigned int>(expected[i]))
559  {
560  return false;
561  }
562  }
563 
564  return true;
565 }
566 
567 void CheckMatchingQuantization(const TensorInfo& first,
568  const TensorInfo& second,
569  const std::string& descName,
570  std::string const& firstName,
571  std::string const& secondName)
572 {
573  if (!first.IsQuantized() ||
574  !second.IsQuantized())
575  {
576  // Not a quantized type, ignore the validation
577  return;
578  }
579 
580  DataType firstDataType = first.GetDataType();
581  DataType secondDataType = second.GetDataType();
582 
583  if (firstDataType != secondDataType)
584  {
585  throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
586  " must be of the same quantized type, " +
587  firstName + " is " + GetDataTypeName(firstDataType) + ", " +
588  secondName + " is " + GetDataTypeName(secondDataType));
589  }
590 
591  if (!first.IsTypeSpaceMatch(second))
592  {
593  throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
594  " must have the same quantization space, " +
595  firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
596  " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
597  secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
598  " and scale " + std::to_string(second.GetQuantizationScale()));
599  }
600 }
601 
602 } // <anonymous>
603 
604 TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
605 : m_Options(options)
606 , m_Network(nullptr, nullptr)
607 , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
608 {
609  // register supported operators
610  m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
611  m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
612  m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
613  m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
614  m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
615  m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
616  m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
617  m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
618  m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
619  m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
620  m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
621  m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
622  m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
623  m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
624  m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
625  m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
626  m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
627  m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
628  m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
629  m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
630  m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
631  m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
632  m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
633  m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
634  m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
635  m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
636  m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
637  m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
638  m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
639  m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
640  m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
641  m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
642  m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
643  m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
644  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
645  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
646  m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
647  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
648  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
649  m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
650  m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
651  m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
652  m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
653  m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
654  m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
655  m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
656  m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
657  m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
658  m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
659  m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
660  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
661  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
662  m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
663 
664  // register supported custom operators
665  m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
666 }
667 
668 void TfLiteParserImpl::ResetParser()
669 {
670  m_Network = armnn::INetworkPtr(nullptr, nullptr);
671  m_Model = nullptr;
672  m_SubgraphConnections.clear();
673 }
674 
676 {
677  ResetParser();
678  m_Model = LoadModelFromFile(graphFile);
679  return CreateNetworkFromModel();
680 }
681 
682 INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t> & binaryContent)
683 {
684  ResetParser();
685  m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
686  return CreateNetworkFromModel();
687 }
688 
689 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
690 {
691 
692  using NetworkOptions = std::vector<BackendOptions>;
693  NetworkOptions networkOptions = {};
694  if (m_Options && m_Options.value().m_InferAndValidate)
695  {
696  BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
697  {
698  { "InferAndValidate", true }
699  });
700 
701  networkOptions.push_back(shapeInferenceMethodOption);
702  }
703 
704  m_Network = INetwork::Create(networkOptions);
705  ARMNN_ASSERT(m_Model.get() != nullptr);
706 
707  if (m_Model->subgraphs.size() != 1)
708  {
709  throw ParseException(
710  fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
711  m_Model->subgraphs.size(),
712  CHECK_LOCATION().AsString()));
713  }
714 
715  size_t subgraphIndex = 0;
716  size_t operatorIndex = 0;
717  try
718  {
719  for (SubgraphPtr const& subgraph : m_Model->subgraphs)
720  {
721  m_SubgraphConnections.emplace_back(subgraph->tensors.size());
722  for (OperatorPtr const& op : subgraph->operators)
723  {
724  auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
725  auto builtinCode = opCodePtr->builtin_code;
726 
727  if (builtinCode > tflite::BuiltinOperator_MAX)
728  {
729  throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
730  "subgraph:{} operator idx:{}. {}",
731  builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
732  operatorIndex, CHECK_LOCATION().AsString()));
733  }
734 
735  // lookup and call the parser function
736  auto& parserFunction = m_ParserFunctions[builtinCode];
737  (this->*parserFunction)(subgraphIndex, operatorIndex);
738  ++operatorIndex;
739  }
740 
741  SetupInputLayers(subgraphIndex);
742  SetupOutputLayers(subgraphIndex);
743  SetupConstantLayers(subgraphIndex);
744 
745  ++subgraphIndex;
746  operatorIndex = 0;
747  }
748  }
749  catch (const ParseException& e)
750  {
751  std::stringstream errorString;
752  errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
753  << subgraphIndex << " error: " << e.what();
754  ARMNN_LOG(error) << errorString.str();
755  std::stringstream errors;
756  errors << errorString.str() << "\n";
757  throw ParseException(errors.str());
758  }
759 
760  // establish the connections from the layer outputs to the inputs of the subsequent layers
761  for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
762  {
763  for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
764  {
765  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
766  {
767  for (size_t inputSlotIdx = 0;
768  inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
769  ++inputSlotIdx)
770  {
771  m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
772  *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
773  }
774  }
775  }
776  }
777 
778  return std::move(m_Network);
779 }
780 
781 void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
782  size_t tensorIndex,
783  armnn::IOutputSlot* slot)
784 {
785  CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
786  ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
787  ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
788 
789  TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
790 
791  // assuming there is only one producer for that tensor
792  if (tensorSlots.outputSlot != nullptr)
793  {
794  throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
795  "subgraph:{} tensor:{} {}",
796  subgraphIndex,
797  tensorIndex,
798  CHECK_LOCATION().AsString()));
799  }
800 
801  tensorSlots.outputSlot = slot;
802 }
803 
804 void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
805  size_t tensorIndex,
806  armnn::IInputSlot* slot)
807 {
808  CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
809  ARMNN_ASSERT(m_SubgraphConnections.size() > subgraphIndex);
810  ARMNN_ASSERT(m_SubgraphConnections[subgraphIndex].size() > tensorIndex);
811 
812  TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
813  tensorSlots.inputSlots.push_back(slot);
814 }
815 
816 void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
817 {
818  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
819 
820  // NOTE: By default we presume the custom operator is not supported
821  auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
822 
823  // Identify custom code defined for custom operator
824  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
825  const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
826 
827  // Find parser function that correspondes to custom code (if any)
828  auto iterator = m_CustomParserFunctions.find(customCode);
829  if (iterator != m_CustomParserFunctions.end())
830  {
831  customParserFunction = iterator->second;
832  }
833 
834  // Run parser function
835  (this->*customParserFunction)(subgraphIndex, operatorIndex);
836 }
837 
838 void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
839 {
840  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
841 
842  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
843 
844  auto opcodeIndex = operatorPtr->opcode_index;
845  auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
846 
847  if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
848  {
849  // Do not add StandInLayer, throw ParseException instead
850  throw ParseException(
851  fmt::format("Operator not supported. "
852  "subgraph:{} operator:{} "
853  "opcode_index:{} opcode:{} / {} {}",
854  subgraphIndex,
855  operatorIndex,
856  opcodeIndex,
857  opcode,
858  tflite::EnumNameBuiltinOperator(opcode),
859  CHECK_LOCATION().AsString()));
860  }
861 
862  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
863  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
864 
865  const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
866  const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
867 
868  StandInDescriptor descriptor(numInputs, numOutputs);
869  auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
870 
871  // Add a non-executable StandInLayer as a placeholder for any unsupported operator
872  IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
873  ARMNN_ASSERT(layer != nullptr);
874 
875  for (unsigned int i = 0u; i < numOutputs; ++i)
876  {
877  layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[i], true));
878  }
879 
880  auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
881  auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
882 
883  RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
884  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
885 }
886 
887 void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
888 {
889  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
890 
891  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
892  CHECK_VALID_SIZE(inputs.size(), 1);
893  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
894  CHECK_VALID_SIZE(outputs.size(), 1);
895 
896  auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
897 
898  IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
899  ARMNN_ASSERT(layer != nullptr);
900 
901  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
902  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
903 
904  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
905  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
906 
907  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
908  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
909 }
910 
911 void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
912 {
913  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
914 
915  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
916  const auto * options = operatorPtr->builtin_options.AsConv2DOptions();
917 
918  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
919 
921  desc.m_BiasEnabled = false;
922  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
923  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
925  desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
926  desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
927 
928  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
929  CHECK_VALID_SIZE(inputs.size(), 2, 3);
930 
931  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
932  CHECK_VALID_SIZE(outputs.size(), 1);
933 
934  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
935  armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
936 
937  // assuming input is NHWC
938  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
939  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
940 
941  // assuming the filter is OHWI : Output, H, W, Input
942  // which is essentially the same as NHWC
943  unsigned int filterHeight = filterTensorInfo.GetShape()[1];
944  unsigned int filterWidth = filterTensorInfo.GetShape()[2];
945 
946  CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
947  desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
948  CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
949  desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
950 
951  auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
952  armnn::IConnectableLayer* layer = nullptr;
953 
954  auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
955 
956  if (inputs.size() == 3)
957  {
958  desc.m_BiasEnabled = true;
959  armnn::TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
960  auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
961  layer = m_Network->AddConvolution2dLayer(desc,
962  filterTensorAndData,
963  Optional<ConstTensor>(biasTensorAndData),
964  layerName.c_str());
965  }
966  else
967  {
968  layer = m_Network->AddConvolution2dLayer(desc,
969  filterTensorAndData,
970  EmptyOptional(),
971  layerName.c_str());
972  }
973 
974  ARMNN_ASSERT(layer != nullptr);
975 
976  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
977  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
978 
979  // register the input connection slots for the layer, connections are made after all layers have been created
980  // only the tensors for the inputs are relevant, exclude the const tensors
981  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
982  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
983 
984  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
985  // register the output connection slots for the layer, connections are made after all layers have been created
986  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
987  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
988 }
989 
990 void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
991 {
992  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
993 
994  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
995  const auto * options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
996 
997  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
998 
1000  desc.m_BiasEnabled = false;
1001  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1002  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1004  CHECKED_NON_NEGATIVE(options->depth_multiplier);
1005 
1006  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1007  CHECK_VALID_SIZE(inputs.size(), 2, 3);
1008  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1009  CHECK_VALID_SIZE(outputs.size(), 1);
1010  desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1011  desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1012 
1013  // Mappings from TensorflowLite filter tensors to the ArmNN filter tensors (ArmNN weights have to be [M, I, H, W])
1014  PermutationVector permutationVector{ 2, 3, 1, 0 }; // [H, W, I, M] -> [M, I, H, W]
1015 
1016  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1017  armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1018 
1019  // Assuming input is NHWC
1020  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1021  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1022 
1023  // TensorflowLite weights come in the format [1, H, W, I * M]
1024  unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1025  unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1026 
1027  // Reshape weights as [ H, W, I, M ]
1028  filterTensorInfo.SetShape({ filterHeight,
1029  filterWidth,
1030  inputTensorInfo.GetShape()[3],
1031  filterTensorInfo.GetShape()[3] / inputTensorInfo.GetShape()[3] });
1032 
1033  CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1034  desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1035  CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1036  desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1037 
1038  auto filterTensorAndData = CreateConstTensorPermuted(inputs[1], filterTensorInfo, permutationVector);
1039  armnn::IConnectableLayer* layer = nullptr;
1040  auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1041 
1042  if (inputs.size() == 3)
1043  {
1044  desc.m_BiasEnabled = true;
1045  TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
1046  auto biasTensorAndData = CreateConstTensorNonPermuted(inputs[2], biasTensorInfo);
1047  layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1048  filterTensorAndData.first,
1049  Optional<ConstTensor>(biasTensorAndData),
1050  layerName.c_str());
1051  }
1052  else
1053  {
1054  layer = m_Network->AddDepthwiseConvolution2dLayer(desc,
1055  filterTensorAndData.first,
1056  EmptyOptional(),
1057  layerName.c_str());
1058  }
1059  ARMNN_ASSERT(layer != nullptr);
1060 
1061  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1062  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1063 
1064  // register the input connection slots for the layer, connections are made after all layers have been created
1065  // only the tensors for the inputs are relevant, exclude the const tensors
1066  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1067  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1068 
1069  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1070  // register the output connection slots for the layer, connections are made after all layers have been created
1071  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1072  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1073 }
1074 
1075 void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
1076 {
1077  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1078 
1079  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1080  CHECK_VALID_SIZE(inputs.size(), 1);
1081 
1082  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1083  CHECK_VALID_SIZE(outputs.size(), 1);
1084 
1085  auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
1086 
1087  IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
1088  ARMNN_ASSERT(layer != nullptr);
1089 
1090  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1091  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1092 
1093  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1094  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1095 
1096  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1097  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1098 }
1099 
1100 void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
1101 {
1102  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1103 
1104  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1105  CHECK_VALID_SIZE(inputs.size(), 1, 2);
1106 
1107  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1108  CHECK_VALID_SIZE(outputs.size(), 1);
1109 
1110  auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
1111  TransposeDescriptor desc;
1112 
1113  if (inputs.size() == 2)
1114  {
1115  armnn::TensorInfo permuteTensorInfo = ToTensorInfo(inputs[1]);
1116  BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1117  auto numPermVecElements = permuteTensorInfo.GetNumElements();
1118  std::vector<unsigned int> permuteShape(numPermVecElements);
1119  ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
1120  PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
1121 
1122  desc = TransposeDescriptor(permutationVector);
1123  }
1124 
1125  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1126  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1127  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1128 
1129  IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1130  ARMNN_ASSERT(layer != nullptr);
1131  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1132 
1133  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1134  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1135 
1136  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1137  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1138 }
1139 
1140 void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
1141 {
1142  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1143 
1144  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1145  const auto * options = operatorPtr->builtin_options.AsTransposeConvOptions();
1146 
1148  desc.m_BiasEnabled = false;
1149  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1150  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1152 
1153  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1154  if (inputs.size() == 4)
1155  {
1156  desc.m_BiasEnabled = true;
1157  }
1158  else
1159  {
1160  CHECK_VALID_SIZE(inputs.size(), 3);
1161  }
1162 
1163  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1164  CHECK_VALID_SIZE(outputs.size(), 1);
1165 
1166  if (inputs[0])
1167  {
1168  armnn::TensorInfo tensorInfo = ToTensorInfo(inputs[0]);
1169  std::vector<int> output_shape(tensorInfo.GetNumElements());
1170  if (tensorInfo.GetDataType() == DataType::Signed32)
1171  {
1172  ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1173  }
1174  if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1175  {
1176  for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1177  {
1178  output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1179  }
1180  }
1181  // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1182  for (int dimension : output_shape)
1183  {
1184  desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1185  }
1186  desc.m_OutputShapeEnabled = true;
1187  }
1188  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[2]);
1189  armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
1190 
1191  // TfLite uses NHWC tensors
1192  const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1193  const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1194 
1195  const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1196  const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1197 
1198  CalcPadding(inputHeight,
1199  filterHeight,
1200  desc.m_StrideY,
1201  1, // DilationY
1202  desc.m_PadTop,
1203  desc.m_PadBottom,
1204  options->padding);
1205 
1206  CalcPadding(inputWidth,
1207  filterWidth,
1208  desc.m_StrideX,
1209  1, // DilationX
1210  desc.m_PadLeft,
1211  desc.m_PadRight,
1212  options->padding);
1213 
1214  auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo);
1215 
1216  armnn::IConnectableLayer* layer = nullptr;
1217  auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1218 
1219  if (desc.m_BiasEnabled)
1220  {
1221  auto biasTensorInfo = ToTensorInfo(inputs[3]);
1222  auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo);
1223  layer = m_Network->AddTransposeConvolution2dLayer(desc,
1224  filterTensorAndData,
1225  biasConstTensor,
1226  layerName.c_str());
1227  }
1228  else
1229  {
1230  layer = m_Network->AddTransposeConvolution2dLayer(desc,
1231  filterTensorAndData,
1232  EmptyOptional(),
1233  layerName.c_str());
1234  }
1235 
1236  ARMNN_ASSERT(layer != nullptr);
1237 
1238  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1239  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1240 
1241  // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1242  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1243  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1244 
1245  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1246  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1247 }
1248 
1249 void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
1250 {
1251  ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1252 }
1253 
1254 void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
1255 {
1256  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1257 
1258  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1259  CHECK_VALID_SIZE(inputs.size(), 3);
1260 
1261  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1262  CHECK_VALID_SIZE(outputs.size(), 1);
1263 
1264  armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1265  BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1266 
1267  armnn::TensorInfo cropsTensorInfo = ToTensorInfo(inputs[2]);
1268  BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1269 
1270  std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1271  ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1272 
1273  std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1274  ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1275 
1276  size_t step = 2;
1277  std::vector<std::pair<unsigned int, unsigned int>> crops;
1278  for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1279  {
1280  crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1281  }
1282 
1284  desc.m_BlockShape = blockShape;
1285  desc.m_Crops = crops;
1287 
1288  auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1289 
1290  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1291  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1292  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1293 
1294  IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1295  ARMNN_ASSERT(layer != nullptr);
1296  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1297 
1298  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1299  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1300 
1301  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1302  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1303 }
1304 
1305 void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
1306 {
1307  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1308 
1309  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1310  CHECK_VALID_SIZE(inputs.size(), 1);
1311 
1312  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1313  CHECK_VALID_SIZE(outputs.size(), 1);
1314 
1317  auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1318  IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1319 
1320  ARMNN_ASSERT(layer != nullptr);
1321 
1322  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1323  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1324 
1325  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1326  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1327 
1328  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1329  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1330 }
1331 
1332 void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
1333 {
1334  ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1335 }
1336 
1337 void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
1338 {
1339  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1340 
1341  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1342  CHECK_VALID_SIZE(inputs.size(), 2);
1343 
1344  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1345  CHECK_VALID_SIZE(outputs.size(), 1);
1346 
1347  auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
1348 
1349  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1350  TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1351  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
1352 
1353  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1354  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1355 
1356  IConnectableLayer* layer = m_Network->AddMaximumLayer(layerName.c_str());
1357  ARMNN_ASSERT(layer != nullptr);
1358  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1359 
1360  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1361  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1362 
1363  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1364  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1365 }
1366 
1367 void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
1368 {
1369  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1370 
1371  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1372  CHECK_VALID_SIZE(inputs.size(), 2);
1373 
1374  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1375  CHECK_VALID_SIZE(outputs.size(), 1);
1376 
1377  auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
1378 
1379  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1380  TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1381  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
1382 
1383  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1384  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1385 
1386  IConnectableLayer* layer = m_Network->AddMinimumLayer(layerName.c_str());
1387  ARMNN_ASSERT(layer != nullptr);
1388  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1389 
1390  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1391  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1392 
1393  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1394  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1395 }
1396 
1397 void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
1398  size_t operatorIndex,
1399  PoolingAlgorithm algorithm)
1400 {
1401  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1402 
1403  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1404  const auto * options = operatorPtr->builtin_options.AsPool2DOptions();
1405 
1406  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1407 
1408  std::string layerName;
1409 
1410  switch (algorithm)
1411  {
1412  case PoolingAlgorithm::Average:
1413  layerName =
1414  fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
1415  break;
1416  case PoolingAlgorithm::Max:
1417  layerName =
1418  fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
1419  break;
1420  default:
1421  ARMNN_ASSERT_MSG(false, "Unsupported Pooling Algorithm");
1422  }
1423 
1424  Pooling2dDescriptor desc;
1425 
1426  desc.m_PoolType = algorithm;
1427  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1428  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1429  desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
1430  desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
1431  desc.m_PaddingMethod = PaddingMethod::Exclude;
1432  desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
1434 
1435  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1436  CHECK_VALID_SIZE(inputs.size(), 1);
1437  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1438 
1439  // assuming input is NHWC
1440  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1441  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1442 
1443  CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
1444  desc.m_PadTop, desc.m_PadBottom, options->padding);
1445  CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
1446  desc.m_PadLeft, desc.m_PadRight, options->padding);
1447 
1448  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1449  CHECK_VALID_SIZE(outputs.size(), 1);
1450 
1451  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1452  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1453 
1454  IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
1455  ARMNN_ASSERT(layer != nullptr);
1456  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1457 
1458  // register the input connection slots for the layer, connections are made after all layers have been created
1459  // only the tensors for the inputs are relevant, exclude the const tensors
1460  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1461  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1462 
1463  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1464  // register the output connection slots for the layer, connections are made after all layers have been created
1465  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1466  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1467 }
1468 
1469 void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
1470 {
1471  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1472 
1473  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1474  CHECK_VALID_SIZE(inputs.size(), 3);
1475  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1476  CHECK_VALID_SIZE(outputs.size(), 1);
1477 
1478  SliceDescriptor desc;
1479 
1480  // set begin tensor info for slice descriptor
1481  armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1482  BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1483 
1484  std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
1485  ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1486 
1487  // set size tensor info for slice descriptor
1488  armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[2]);
1489  BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1490 
1491  std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
1492  ::memcpy(size.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
1493  desc = SliceDescriptor(begin, size);
1494 
1495  auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
1496 
1497  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1498  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1499  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1500 
1501  IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
1502  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1503 
1504  // register the input connection slots for the layer, connections are made after all layers have been created
1505  // only the tensors for the inputs are relevant, exclude the const tensors
1506  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1507  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1508 
1509  // register the output connection slots for the layer, connections are made after all layers have been created
1510  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1511  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1512 }
1513 
1514 void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
1515 {
1516  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1517  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1518  const auto * options = operatorPtr->builtin_options.AsSoftmaxOptions();
1519 
1520  SoftmaxDescriptor desc;
1521  desc.m_Beta = options->beta;
1522 
1523  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1524  CHECK_VALID_SIZE(inputs.size(), 1);
1525  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1526  CHECK_VALID_SIZE(outputs.size(), 1);
1527 
1528  auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
1529  IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
1530 
1531  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1532  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1533 
1534  // register the input connection slots for the layer, connections are made after all layers have been created
1535  // only the tensors for the inputs are relevant, exclude the const tensors
1536  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1537  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1538 
1539  // register the output connection slots for the layer, connections are made after all layers have been created
1540  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1541  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1542 }
1543 
1544 void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
1545 {
1546  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1547 
1548  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1549  CHECK_VALID_SIZE(inputs.size(), 3);
1550 
1551  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1552  CHECK_VALID_SIZE(outputs.size(), 1);
1553 
1554  armnn::TensorInfo blockShapeTensorInfo = ToTensorInfo(inputs[1]);
1555  BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1556 
1557  armnn::TensorInfo padListTensorInfo = ToTensorInfo(inputs[2]);
1558  BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1559 
1560  std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1561  ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1562 
1563  std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
1564  ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
1565 
1566  size_t step = 2;
1567  std::vector<std::pair<unsigned int, unsigned int>> padList;
1568  for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
1569  {
1570  padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
1571  }
1572 
1574  desc.m_BlockShape = blockShape;
1575  desc.m_PadList = padList;
1577 
1578  auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
1579 
1580  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1581  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1582  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1583 
1584  IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
1585  ARMNN_ASSERT(layer != nullptr);
1586  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1587 
1588  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1589  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1590 
1591  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1592  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1593 }
1594 
1595 armnn::TensorInfo TfLiteParserImpl::OutputShapeOfSqueeze(const std::vector<uint32_t> & squeezeDimsIn,
1596  const armnn::TensorInfo & inputTensorInfo)
1597 {
1598  CHECK_VALID_SIZE(squeezeDimsIn.size(), 0, 1, 2, 3, 4);
1599  std::vector<uint32_t> squeezeDims = squeezeDimsIn;
1600  static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
1601 
1602  if (inputTensorInfo.GetNumDimensions() > 4)
1603  {
1604  std::stringstream ss;
1605  ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1606  << " shape:" << inputTensorInfo.GetShape() << " "
1607  << CHECK_LOCATION().AsString();
1608  throw ParseException(ss.str());
1609  }
1610 
1611  if (squeezeDims.empty())
1612  {
1613  squeezeDims.assign(dimensionSequence,
1614  dimensionSequence+inputTensorInfo.GetNumDimensions());
1615  }
1616 
1617  std::vector<uint32_t> outputDims;
1618  for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
1619  {
1620  bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
1621  auto currentDimension = inputTensorInfo.GetShape()[i];
1622  if (skipSqueeze || currentDimension != 1)
1623  {
1624  outputDims.push_back(currentDimension);
1625  }
1626  }
1627 
1628  if (outputDims.size() > 4)
1629  {
1630  std::stringstream ss;
1631  ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
1632  << " shape:" << inputTensorInfo.GetShape() << " "
1633  << CHECK_LOCATION().AsString();
1634  throw ParseException(ss.str());
1635  }
1636 
1637  TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
1638  outputDims.data());
1639 
1640  // we need to preserve the tensor type and the quantization data as well
1641  TensorInfo outTensorInfo = inputTensorInfo;
1642  outTensorInfo.SetShape(outShape);
1643 
1644  return outTensorInfo;
1645 }
1646 
1647 void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
1648 {
1649  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1650 
1651  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1652  CHECK_VALID_SIZE(inputs.size(), 1);
1653 
1654  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1655  CHECK_VALID_SIZE(outputs.size(), 1);
1656 
1657  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1658  const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
1659  auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
1660 
1661  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1662  armnn::TensorInfo outputTensorInfo =
1663  TfLiteParserImpl::OutputShapeOfSqueeze(AsUnsignedVector(options->squeeze_dims),
1664  inputTensorInfo);
1665  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1666 
1667  ReshapeDescriptor reshapeDesc;
1668  reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
1669 
1670  IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1671  ARMNN_ASSERT(layer != nullptr);
1672  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1673 
1674  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1675  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1676 
1677  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1678  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1679 }
1680 
1681 void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
1682 {
1683  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1684 
1685  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1686  CHECK_VALID_SIZE(inputs.size(), 4);
1687 
1688  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1689  CHECK_VALID_SIZE(outputs.size(), 1);
1690 
1691  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1692  const auto * options = operatorPtr->builtin_options.AsStridedSliceOptions();
1693 
1695  desc.m_BeginMask = options->begin_mask;
1696  desc.m_EllipsisMask = options->ellipsis_mask;
1697  desc.m_EndMask = options->end_mask;
1698  desc.m_NewAxisMask = options->new_axis_mask;
1699  desc.m_ShrinkAxisMask = options->shrink_axis_mask;
1701 
1702  armnn::TensorInfo beginTensorInfo = ToTensorInfo(inputs[1]);
1703  BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1704 
1705  std::vector<int> begin(beginTensorInfo.GetNumElements());
1706  ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
1707 
1708  armnn::TensorInfo endTensorInfo = ToTensorInfo(inputs[2]);
1709  BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1710 
1711  std::vector<int> end(endTensorInfo.GetNumElements());
1712  ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
1713 
1714  armnn::TensorInfo strideTensorInfo = ToTensorInfo(inputs[3]);
1715  BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
1716 
1717  std::vector<int> stride(strideTensorInfo.GetNumElements());
1718  ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
1719 
1720  desc.m_Begin = begin;
1721  desc.m_End = end;
1722  desc.m_Stride = stride;
1723 
1724  auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
1725  IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
1726  ARMNN_ASSERT(layer != nullptr);
1727 
1728  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1729  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1730 
1731  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1732  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1733 
1734  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1735  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1736 }
1737 
1738 void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
1739 {
1740  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1741 
1742  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1743  const auto * options = operatorPtr->builtin_options.AsSubOptions();
1744 
1745  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1746  CHECK_VALID_SIZE(inputs.size(), 2);
1747 
1748  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1749  CHECK_VALID_SIZE(outputs.size(), 1);
1750 
1751  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1752  armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1753 
1754  auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
1755  IConnectableLayer* layer = m_Network->AddSubtractionLayer(layerName.c_str());
1756  ARMNN_ASSERT(layer != nullptr);
1757 
1758  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1759  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1760 
1761  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1762  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1763 
1764  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1765 
1766  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1767  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1768 }
1769 
1770 void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
1771 {
1772  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1773 
1774  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1775  const auto * options = operatorPtr->builtin_options.AsDivOptions();
1776 
1777  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1778  CHECK_VALID_SIZE(inputs.size(), 2);
1779 
1780  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1781  CHECK_VALID_SIZE(outputs.size(), 1);
1782 
1783  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1784  armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1785 
1786  auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
1787  IConnectableLayer* layer = m_Network->AddDivisionLayer(layerName.c_str());
1788  ARMNN_ASSERT(layer != nullptr);
1789 
1790  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1791  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1792 
1793  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1794  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1795  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1796 
1797  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1798  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1799 }
1800 
1801 void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
1802 {
1803  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1804 
1805  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1806  const auto * options = operatorPtr->builtin_options.AsAddOptions();
1807 
1808  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1809  CHECK_VALID_SIZE(inputs.size(), 2);
1810 
1811  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1812  CHECK_VALID_SIZE(outputs.size(), 1);
1813 
1814  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1815  armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1816 
1817  auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
1818  IConnectableLayer* layer = m_Network->AddAdditionLayer(layerName.c_str());
1819  ARMNN_ASSERT(layer != nullptr);
1820 
1821  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1822  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1823 
1824  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1825  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1826  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1827 
1828  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1829  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1830 }
1831 
1832 void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
1833 {
1834  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1835 
1836  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1837  const auto * options = operatorPtr->builtin_options.AsMulOptions();
1838 
1839  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1840  CHECK_VALID_SIZE(inputs.size(), 2);
1841 
1842  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1843  CHECK_VALID_SIZE(outputs.size(), 1);
1844 
1845  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1846  armnn::TensorInfo input1TensorInfo = ToTensorInfo(inputs[1]);
1847 
1848  auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
1849  IConnectableLayer* layer = m_Network->AddMultiplicationLayer(layerName.c_str());
1850  ARMNN_ASSERT(layer != nullptr);
1851 
1852  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1853  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1854 
1855  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1856  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1857  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1858 
1859  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1860  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1861 }
1862 
1863 void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
1864 {
1865  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1866 
1867  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1868 
1869  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1870  CHECK_VALID_SIZE(outputs.size(), 1);
1871 
1872  armnn::TensorInfo dimTensorInfo = ToTensorInfo(inputs[1]);
1873  BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1874 
1875  armnn::MeanDescriptor desc;
1876  std::vector<unsigned int> axis(dimTensorInfo.GetNumElements());
1877  ::memcpy(axis.data(), bufferPtr->data.data(), dimTensorInfo.GetNumBytes());
1878  desc.m_Axis = axis;
1879 
1880  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1881  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1882 
1883  desc.m_KeepDims =
1884  inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ?
1885  true : false;
1886 
1887  auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
1888  IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
1889  ARMNN_ASSERT(layer != nullptr);
1890 
1891  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1892 
1893  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1894  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1895 
1896  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1897  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1898 }
1899 
1900 void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
1901 {
1902  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1903 
1904  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1905 
1906  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1907  CHECK_VALID_SIZE(outputs.size(), 1);
1908 
1909  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
1910 
1911  armnn::TensorInfo padTensorInfo = ToTensorInfo(inputs[1]);
1912  BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1913 
1914  std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
1915  ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
1916 
1917  size_t step = 2;
1918  armnn::PadDescriptor desc;
1919  if (inputTensorInfo.IsQuantized())
1920  {
1921  desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
1922  }
1923  for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
1924  {
1925  desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
1926  }
1927 
1928  auto layerName = fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex);
1929  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1930 
1931  IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
1932  ARMNN_ASSERT(layer != nullptr);
1933  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1934 
1935  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1936  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1937 
1938  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1939  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1940 }
1941 
1942 void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
1943 {
1944  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1945 
1946  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1947  CHECK_VALID_SIZE(inputs.size(), 1);
1948 
1949  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1950  CHECK_VALID_SIZE(outputs.size(), 1);
1951 
1952  auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
1953 
1954  IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
1955  ARMNN_ASSERT(layer != nullptr);
1956 
1957  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1958  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1959 
1960  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1961  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1962 
1963  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1964  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1965 }
1966 
1967 void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
1968 {
1969  ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
1970 }
1971 
1972 void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
1973 {
1974  ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
1975 }
1976 
1977 void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
1978 {
1979  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
1980 }
1981 
1982 void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
1983 {
1984  ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
1985 }
1986 
1987 void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
1988 {
1989  ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
1990 }
1991 
1992 void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
1993 {
1994  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
1995 }
1996 
1997 void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
1998 {
1999  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
2000 }
2001 
2002 void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
2003 {
2004  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2005  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2006  IgnoreUnused(operatorPtr);
2007 
2008  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2009  CHECK_VALID_SIZE(inputs.size(), 1);
2010 
2011  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2012  CHECK_VALID_SIZE(outputs.size(), 1);
2013 
2014  auto layerName = fmt::format("Activation:");
2015  ActivationDescriptor activationDesc;
2016  activationDesc.m_Function = activationType;
2017 
2018  switch (activationType)
2019  {
2020  case ActivationFunction::ReLu:
2021  {
2022  layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
2023  break;
2024  }
2025  case ActivationFunction::BoundedReLu:
2026  {
2027  layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
2028  activationDesc.m_A = 6.0f;
2029  activationDesc.m_B = 0.0f;
2030  break;
2031  }
2032  case ActivationFunction::Sigmoid:
2033  {
2034  layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
2035  break;
2036  }
2037  case ActivationFunction::TanH:
2038  {
2039  layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
2040  activationDesc.m_A = 1.0f;
2041  activationDesc.m_B = 1.0f;
2042  break;
2043  }
2044  case ActivationFunction::LeakyReLu:
2045  {
2046  layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
2047  const auto * options = operatorPtr->builtin_options.AsLeakyReluOptions();
2048  activationDesc.m_A = options->alpha;
2049  break;
2050  }
2051  case ActivationFunction::Elu:
2052  {
2053  layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
2054  activationDesc.m_A = 1.0f;
2055  break;
2056  }
2057  case ActivationFunction::HardSwish:
2058  {
2059  layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
2060  break;
2061  }
2062  default:
2063  {
2064  throw ParseException(
2065  fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
2066  static_cast<int>(activationType), CHECK_LOCATION().AsString()));
2067  }
2068  }
2069 
2070  IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
2071 
2072  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2073  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2074 
2075  // register the input connection slots for the layer, connections are made after all layers have been created
2076  // only the tensors for the inputs are relevant, exclude the const tensors
2077  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2078  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2079 
2080  // register the output connection slots for the layer, connections are made after all layers have been created
2081  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2082  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2083 }
2085  const std::vector<int32_t> & targetDimsIn)
2086 {
2087  std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
2088  const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
2089 
2090  if (stretchDim != targetDimsIn.end())
2091  {
2092  if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
2093  {
2094  throw ParseException(
2095  fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
2096  }
2097 
2098  auto targetNumElements =
2099  armnn::numeric_cast<unsigned int>(
2100  std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
2101 
2102  auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
2103  outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
2104  }
2105 
2106  TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
2107 
2108  TensorInfo reshapeInfo = inputTensorInfo;
2109  reshapeInfo.SetShape(outputShape);
2110 
2111  return reshapeInfo;
2112 }
2113 
2114 void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
2115 {
2116  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2117 
2118  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2119 
2120  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2121  CHECK_VALID_SIZE(outputs.size(), 1);
2122 
2123  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2124  const auto * options = operatorPtr->builtin_options.AsReshapeOptions();
2125  auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
2126 
2127  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2128  armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
2129  CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
2130 
2131  // Extracting new shape for the output
2132  // There are two ways it can be passed
2133  // * First is to define the target shape in the operator built-in options
2134  // * Second is to pass it as a second input tensor
2135  std::vector<int32_t> targetShape;
2136  bool targetShapeFound = false;
2137  // Check if built-in options were given
2138  if (options != nullptr)
2139  {
2140  // make sure the parameter is given
2141  if (options->new_shape.empty() == false)
2142  {
2143  targetShape = options->new_shape;
2144  targetShapeFound = true;
2145  }
2146  }
2147 
2148  // If there is no built-in option given or if the built-in new_shape parameter was empty
2149  if (!targetShapeFound)
2150  {
2151  // Check for a second input tensor
2152  if (inputs.size() > 1 && inputs[1] != nullptr)
2153  {
2154  if (inputs[1]->is_variable)
2155  {
2156  ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
2157  }
2158 
2159  if (inputs[1]->shape.size() != 1)
2160  {
2161  ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
2162  }
2163 
2164  if (inputs[1]->type != tflite::TensorType_INT32)
2165  {
2166  ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
2167  }
2168 
2169  // Extract target shape from input
2170  auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2171  auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
2172  if (!values)
2173  {
2174  ARMNN_THROW_PARSE_EXCEPTION("Reshape operator target shape input buffer data is null");
2175  }
2176  for (int i=0; i < inputs[1]->shape[0]; ++i)
2177  {
2178  targetShape.push_back(values[i]);
2179  }
2180  }
2181  else
2182  {
2183  ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
2184  "At least one method required");
2185  }
2186  }
2187 
2188  armnn::TensorInfo reshapeOutputTensorInfo =
2189  TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
2190 
2191  // Check for valid input size and that reshape parameters equal output shape
2192  const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
2193  if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
2194  {
2195  std::stringstream ss;
2196  ss << "New shape defined in reshape parameters "
2197  << reshapeOutputTensorShape
2198  << " does not equal output shape "
2199  << actualOutputTensorInfo.GetShape()
2200  << ": "
2201  << CHECK_LOCATION().AsString();
2202  throw ParseException(ss.str());
2203  }
2204 
2205  ReshapeDescriptor reshapeDesc;
2206  reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
2207 
2208  IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2209  ARMNN_ASSERT(layer != nullptr);
2210  layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
2211 
2212  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2213  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2214 
2215  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2216  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2217 }
2218 
2219 void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
2220 {
2221  ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
2222 }
2223 
2224 void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
2225 {
2226  ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
2227 }
2228 
2229 void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
2230 {
2231  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2232 
2233  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2234  CHECK_VALID_SIZE(inputs.size(), 2);
2235 
2236  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2237  CHECK_VALID_SIZE(outputs.size(), 1);
2238 
2239  armnn::TensorInfo sizeTensorInfo = ToTensorInfo(inputs[1]);
2240 
2241  // Data for the parsed tensor args (size) must be stored locally.
2242  std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
2243 
2244  BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2245  ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2246 
2247  ResizeDescriptor desc;
2248  desc.m_Method = resizeMethod;
2249  desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
2250  desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
2251  desc.m_DataLayout = armnn::DataLayout::NHWC;
2252 
2253  auto layerName = fmt::format("Resize:");
2254 
2255  switch (resizeMethod)
2256  {
2257  case ResizeMethod::Bilinear:
2258  {
2259  layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
2260 
2261  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2262  const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
2263 
2264  desc.m_AlignCorners = options->align_corners;
2265  break;
2266  }
2267  case ResizeMethod::NearestNeighbor:
2268  {
2269  layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
2270  break;
2271  }
2272  default:
2273  {
2274  throw ParseException(
2275  fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
2276  static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
2277  }
2278  }
2279 
2280  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2281  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2282  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2283 
2284  IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
2285  ARMNN_ASSERT(layer != nullptr);
2286  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2287 
2288  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2289  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2290 
2291  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2292  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2293 }
2294 
2295 void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
2296 {
2297  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2298 
2299  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2300  const auto * options = operatorPtr->builtin_options.AsConcatenationOptions();
2301 
2302  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2303 
2304  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2305  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2306  CHECK_VALID_SIZE(outputs.size(), 1);
2307 
2308  unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
2309  uint32_t inputRank = ToTensorInfo(inputs[0]).GetNumDimensions();
2310 
2311  const unsigned int concatDimInput = static_cast<unsigned int>(
2312  (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
2313 
2314  OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
2315  concatDescriptor.SetConcatAxis(concatDimInput);
2316 
2317  unsigned int mergeDimOrigin = 0;
2318 
2319  for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
2320  {
2321  TensorInfo inputTensorInfo = ToTensorInfo(inputs[viewIndex]);
2322 
2323  // This set up concatDescriptor view origin
2325  inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
2326  }
2327 
2328  auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
2329  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2330 
2331  IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
2332  ARMNN_ASSERT(layer != nullptr);
2333  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2334 
2335  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2336  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2337 
2338  // add fused activation layer
2339  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2340 
2341  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2342  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2343 }
2344 
2345 void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
2346 {
2347  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2348 
2349  const auto & operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2350  const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
2351 
2352  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2353 
2355  desc.m_BiasEnabled = false;
2356  desc.m_TransposeWeightMatrix = true;
2357 
2358  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2359  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2360  CHECK_VALID_SIZE(outputs.size(), 1);
2361 
2362  armnn::TensorInfo filterTensorInfo = ToTensorInfo(inputs[1]);
2363 
2364  // Fully Connected Layer accepts two dimensional weights input
2365  int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
2366  if (weightsDimension != 2)
2367  {
2368  throw ParseException(
2369  fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
2370  "Node {}",
2371  weightsDimension,
2372  CHECK_LOCATION().AsString()));
2373  }
2374 
2375  armnn::IConnectableLayer* layer = nullptr;
2376  auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
2377 
2378  Optional<ConstTensor> filterOptionalConstTensor;
2379 
2380  desc.m_ConstantWeights = IsConstTensor(inputs[1]);
2381 
2382  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2383  std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
2384  if (desc.m_ConstantWeights)
2385  {
2386  filterOptionalConstTensor = Optional<ConstTensor>(CreateConstTensorNonPermuted(inputs[1], filterTensorInfo));
2387  }
2388  else
2389  {
2390  // Non const weights will need to be registered as inputs
2391  tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
2392  }
2393 
2394  Optional<ConstTensor> biasOptionalConstTensor;
2395  if (inputs.size() == 3)
2396  {
2397  desc.m_BiasEnabled = true;
2398  if (desc.m_ConstantWeights)
2399  {
2400  TensorInfo biasTensorInfo = ToTensorInfo(inputs[2]);
2401  biasOptionalConstTensor = Optional<ConstTensor>(CreateConstTensorNonPermuted(inputs[2], biasTensorInfo));
2402  }
2403  else
2404  {
2405  // Non const biases will need to be registered as inputs
2406  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
2407  }
2408  }
2409 
2410  layer = m_Network->AddFullyConnectedLayer(desc,
2411  filterOptionalConstTensor,
2412  biasOptionalConstTensor,
2413  layerName.c_str());
2414 
2415  ARMNN_ASSERT(layer != nullptr);
2416  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2417 
2418  unsigned int startingSlotIndex = 0;
2419  if (inputTensorInfo.GetNumDimensions() > 2)
2420  {
2421  // Add reshape to flatten to 2D [batch_size, input_size],
2422  // where "input_size" corresponds to the number of inputs to the layer,
2423  // matching the second dimension of weights,
2424  // and "batch_size" is calculated by dividing the number of elements by "input_size".
2425  std::vector<unsigned int> reshapedDimensions(2);
2426  reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
2427  reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
2428 
2429  if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
2430  {
2431  throw ParseException(
2432  fmt::format("Failed to deduce input tensor shape from filter size {} {}",
2433  reshapedDimensions[1],
2434  CHECK_LOCATION().AsString()));
2435  }
2436 
2437  armnn::TensorInfo reshapedTensorInfo = ToTensorInfo(inputs[0]);
2438  reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
2439 
2440  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
2441  armnn::ReshapeDescriptor reshapeDescriptor;
2442  reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
2443  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor, layerName.c_str());
2444 
2445  reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
2446  reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
2447 
2448  RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
2449  // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
2450  tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
2451  startingSlotIndex = 1;
2452  }
2453 
2454  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
2455 
2456  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2457  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2458 
2459  // we need to add the activation layer and fortunately we don't need to care about the data layout
2460  armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
2461  options->fused_activation_function);
2462 
2463  // register the output connection slots for the layer, connections are made after all layers have been created
2464  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2465  RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
2466 }
2467 
2468 void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
2469 {
2470  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2471 
2472  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2473 
2474  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2475  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2476  CHECK_VALID_SIZE(outputs.size(), 4);
2477 
2478  // Obtain custom options from flexbuffers
2479  auto custom_options = operatorPtr->custom_options;
2480  const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
2481 
2482  // Obtain descriptor information from tf lite
2484  desc.m_MaxDetections = m["max_detections"].AsUInt32();
2485  desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
2486  desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
2487  desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
2488  desc.m_NumClasses = m["num_classes"].AsUInt32();
2489  desc.m_ScaleH = m["h_scale"].AsFloat();
2490  desc.m_ScaleW = m["w_scale"].AsFloat();
2491  desc.m_ScaleX = m["x_scale"].AsFloat();
2492  desc.m_ScaleY = m["y_scale"].AsFloat();
2493 
2494  if (!(m["use_regular_nms"].IsNull()))
2495  {
2496  desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
2497  }
2498  if (!(m["detections_per_class"].IsNull()))
2499  {
2500  desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
2501  }
2502 
2503  if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
2504  {
2505  throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
2506  "must be positive and less than or equal to 1.");
2507  }
2508 
2509  armnn::TensorInfo anchorTensorInfo = ToTensorInfo(inputs[2]);
2510  auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
2511 
2512  auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
2513  IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
2514  layerName.c_str());
2515 
2516  ARMNN_ASSERT(layer != nullptr);
2517 
2518  // The model does not specify the output shapes.
2519  // The output shapes are calculated from the max_detection and max_classes_per_detection.
2520  unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
2521  m_OverridenOutputShapes.push_back({ 1, numDetectedBox, 4 });
2522  m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2523  m_OverridenOutputShapes.push_back({ 1, numDetectedBox });
2524  m_OverridenOutputShapes.push_back({ 1 });
2525 
2526  for (unsigned int i = 0 ; i < outputs.size() ; ++i)
2527  {
2528  armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverridenOutputShapes[i]);
2529  layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
2530  }
2531 
2532  // Register the input connection slots for the layer, connections are made after all layers have been created
2533  // only the tensors for the inputs are relevant, exclude the const tensors
2534  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2535  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2536 
2537  // Register the output connection slots for the layer, connections are made after all layers have been created
2538  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2539  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
2540  outputTensorIndexes[1],
2541  outputTensorIndexes[2],
2542  outputTensorIndexes[3]});
2543 }
2544 
2545 /// The TfLite Pack operator is equivalent to the ArmNN Stack operator
2546 void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
2547 {
2548  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2549 
2550  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2551  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2552  CHECK_VALID_SIZE(outputs.size(), 1);
2553 
2554  if (inputs.size() < 1)
2555  {
2556  throw ParseException("Pack must have at least one input.");
2557  }
2558 
2559  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2560  const auto* options = operatorPtr->builtin_options.AsPackOptions();
2561 
2562  StackDescriptor desc;
2563  desc.m_Axis = static_cast<uint32_t>(options->axis);
2564  desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
2565 
2566  // Use the tensor shape of the first input as the "correct" input shape in the descriptor
2567  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2568  desc.m_InputShape = inputTensorInfo.GetShape();
2569 
2570  auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
2571  IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
2572 
2573  ARMNN_ASSERT(layer != nullptr);
2574 
2575  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2576  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2577 
2578  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2579  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
2580 
2581  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2582  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2583 }
2584 
2585 void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
2586 {
2587  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2588 
2589  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2590  const auto * options = operatorPtr->builtin_options.AsUnpackOptions();
2591 
2592  // This unpackAxis indicates the axis to unpack
2593  const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
2594 
2595  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2596  CHECK_VALID_SIZE(inputs.size(), 1);
2597 
2598  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2599 
2600  if (unpackAxis >= inputTensorInfo.GetNumDimensions())
2601  {
2602  throw ParseException(
2603  fmt::format("The unpack axis: {} cannot be greater than or equal to "
2604  "the number of input dimension {} {}",
2605  unpackAxis,
2606  inputTensorInfo.GetNumDimensions(),
2607  CHECK_LOCATION().AsString()));
2608  }
2609 
2610  unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
2611  // If num is not defined, automatically infer from the length of the dimension axis.
2612  if(unpackNum == 0)
2613  {
2614  unpackNum = inputTensorInfo.GetShape()[unpackAxis];
2615  }
2616 
2617  // If unpack number cannot be inferred and is still zero, throw ParseException.
2618  if(unpackNum == 0)
2619  {
2620  throw ParseException("Number to unpack must greater than zero.");
2621  }
2622 
2623  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2624  CHECK_VALID_SIZE(outputs.size(), unpackNum);
2625 
2626  auto inputDimSize = inputTensorInfo.GetNumDimensions();
2627  std::vector<unsigned int> unpackDimSizes(inputDimSize);
2628 
2629  // Add current input shape to unpackDimSizes
2630  for (unsigned int i = 0; i < inputDimSize; ++i)
2631  {
2632  unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
2633  }
2634 
2635  if (unpackDimSizes[unpackAxis] != unpackNum)
2636  {
2637  throw ParseException("Number to unpack must be the same as length of the dimension to "
2638  "unpack along.");
2639  }
2640 
2641  unpackDimSizes[unpackAxis] /= unpackNum;
2642 
2643  SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
2644  for (unsigned int j = 0; j < unpackNum; ++j)
2645  {
2646  // Set the size of the views.
2647  for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
2648  {
2649  splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
2650  }
2651  splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
2652  }
2653 
2654  auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
2655  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2656  ARMNN_ASSERT(layer != nullptr);
2657 
2658  TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
2659  unpackDimSizes.data());
2660 
2661  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2662  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2663 
2664  // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
2665  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2666  {
2667  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
2668  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
2670  desc.m_TargetShape = outputTensorInfo.GetShape();
2671  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
2672 
2673  layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
2674  outputTensorInfo.GetDataType(),
2675  outputTensorInfo.GetQuantizationScale(),
2676  outputTensorInfo.GetQuantizationOffset()));
2677  layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
2678 
2679  reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2680 
2681  uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
2682  armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
2683  RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
2684  }
2685 }
2686 
2687 void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
2688 {
2689  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2690 
2691  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2692  const auto * options = operatorPtr->builtin_options.AsSplitOptions();
2693 
2694  const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2695 
2696  // If number of splits cannot be inferred and is zero, throw ParseException.
2697  if(numSplits == 0)
2698  {
2699  throw ParseException("Number to splits must greater than zero.");
2700  }
2701 
2702  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2703  CHECK_VALID_SIZE(inputs.size(), 2);
2704  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2705  CHECK_VALID_SIZE(outputs.size(), numSplits);
2706 
2707  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[1]);
2708  armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[0]);
2709  ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2710 
2711  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
2712  if (axisBufferPtr == nullptr)
2713  {
2714  throw ParseException(
2715  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2716  CHECK_LOCATION().AsString()));
2717  }
2718 
2719  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
2720  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2721  int32_t axis = axisData[0];
2722 
2723  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2724  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2725  {
2726  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
2727  // E.g. Rank 4 tensor can have axis in range [-4, 3)
2728  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
2729  throw ParseException(
2730  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2731  axis,
2732  CHECK_LOCATION().AsString()));
2733  }
2734 
2735  const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
2736 
2737  auto inputDimSize = inputTensorInfo.GetNumDimensions();
2738  if (inputDimSize > MaxNumOfTensorDimensions)
2739  {
2740  throw ParseException(
2741  fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
2742  inputTensorInfo.GetNumDimensions(),
2744  CHECK_LOCATION().AsString()));
2745  }
2746 
2747  std::vector<unsigned int> splitterDimSizes(inputDimSize);
2748 
2749  // Add current input shape to splitterDimSizes
2750  for (unsigned int i = 0; i < inputDimSize; ++i)
2751  {
2752  splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
2753  }
2754 
2755  if (splitterDimSizes[splitDim] % numSplits != 0)
2756  {
2757  throw ParseException("Number of splits must evenly divide the dimension");
2758  }
2759  splitterDimSizes[splitDim] /= numSplits;
2760 
2761  SplitterDescriptor splitDesc(numSplits, inputDimSize);
2762  for (unsigned int j = 0; j < numSplits; ++j)
2763  {
2764  // Set the size of the views.
2765  for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
2766  {
2767  splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
2768  }
2769  splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
2770  }
2771 
2772  auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
2773  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2774  ARMNN_ASSERT(layer != nullptr);
2775 
2776  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2777  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
2778 
2779  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2780  {
2781  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
2782  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
2783  }
2784 
2785  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2786  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2787 }
2788 
2789 unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
2790 {
2791  int numDims = armnn::numeric_cast<int>(numDimsIn);
2792  int v = idx < 0 ? numDims + idx : idx;
2793  ARMNN_ASSERT(v >= 0);
2794  ARMNN_ASSERT(v < numDims);
2795 
2796  return static_cast<unsigned int>(v);
2797 }
2798 
2799 void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
2800 {
2801  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2802 
2803  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2804  const auto * options = operatorPtr->builtin_options.AsSplitVOptions();
2805 
2806  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2807  CHECK_VALID_SIZE(inputs.size(), 3);
2808 
2809  auto& inputTensor = inputs[0];
2810  auto& splitsTensor = inputs[1];
2811  auto& axisTensor = inputs[2];
2812 
2813  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
2814  armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
2815  armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
2816  ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2817 
2818  // Inputs
2819  auto inputDimSize = inputTensorInfo.GetNumDimensions();
2820  if (inputDimSize > MaxNumOfTensorDimensions)
2821  {
2822  throw ParseException(
2823  fmt::format("The number of dimensions: {} for input tensors of the "
2824  "SplitV op cannot be greater than {} {}",
2825  inputTensorInfo.GetNumDimensions(),
2827  CHECK_LOCATION().AsString()));
2828  }
2829 
2830  // Get split axis
2831  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
2832  if (axisBufferPtr == nullptr)
2833  {
2834  throw ParseException(
2835  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2836  CHECK_LOCATION().AsString()));
2837  }
2838 
2839  std::vector<int> axisData(axisTensorInfo.GetNumElements());
2840  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
2841  int32_t axis = axisData[0];
2842 
2843  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2844  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
2845  {
2846  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
2847  // E.g. Rank 4 tensor can have axis in range [-4, 3)
2848  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
2849  throw ParseException(
2850  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
2851  axis,
2852  CHECK_LOCATION().AsString()));
2853  }
2854  const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
2855 
2856  // Set split sizes
2857  CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
2858  unsigned int numSplits{0};
2859 
2860  if(options)
2861  {
2862  numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
2863  }
2864  else
2865  {
2866  numSplits = splitsInfo.GetNumElements();
2867  }
2868 
2869  if (numSplits <=0)
2870  {
2871  throw ParseException("SplitV has invalid number of splits");
2872  }
2873 
2874  std::vector<int> splitsData(numSplits);
2875  BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
2876  ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
2877 
2878  unsigned int idx = 0;
2879  int numInferred{0};
2880  unsigned int inferIdx{0};
2881  int splitSum{0};
2882  for (auto split : splitsData)
2883  {
2884  if (split < 0)
2885  {
2886  numInferred++;
2887  inferIdx = idx;
2888  }
2889  else
2890  {
2891  splitSum += split;
2892  }
2893  idx++;
2894  }
2895  // Check for inferred Axis
2896  if (numInferred == 0)
2897  {
2898  if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
2899  {
2900  throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
2901  }
2902  }
2903  else if (numInferred == 1)
2904  {
2905  splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
2906  }
2907  else
2908  {
2909  throw ParseException("Cannot infer split size for more than one split");
2910  }
2911 
2912  //Ouput size validation
2913  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2914  CHECK_VALID_SIZE(outputs.size(), numSplits);
2915 
2916  // Setup Armnn descriptor
2917  SplitterDescriptor splitDesc(numSplits, inputDimSize);
2918  unsigned int accumSplit = 0;
2919  for (unsigned int j = 0; j < numSplits; ++j)
2920  {
2921  unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
2922 
2923  // Set the size of the views.
2924  for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
2925  {
2926  unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
2927  if (dimIdx == splitDim)
2928  {
2929  dimSize = splitSize;
2930  }
2931  splitDesc.SetViewSize(j, dimIdx, dimSize);
2932  }
2933 
2934  splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
2935  accumSplit += splitSize;
2936  }
2937 
2938  auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
2939  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
2940  ARMNN_ASSERT(layer != nullptr);
2941 
2942  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2943  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2944 
2945  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
2946  {
2947  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
2948  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
2949  }
2950 
2951  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2952  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2953 }
2954 
2955 void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
2956 {
2957  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
2958 }
2959 
2960 void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
2961 {
2962  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
2963 }
2964 
2965 void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
2966 {
2967  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2968  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2969  CHECK_VALID_SIZE(inputs.size(), 2);
2970 
2971  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2972  CHECK_VALID_SIZE(outputs.size(), 1);
2973 
2974  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
2975  armnn::TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
2976  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
2977  ARMNN_ASSERT(axisTensorInfo.GetNumElements() == 1);
2978 
2979  // Check if output tensor type is Signed32 or Signed64
2980  if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2981  outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2982  {
2983  throw ParseException(
2984  fmt::format(
2985  "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2986  CHECK_LOCATION().AsString()));
2987  }
2988 
2989  // Get const axis value from model and set it to descriptor.
2990  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2991  if (axisBufferPtr == nullptr)
2992  {
2993  throw ParseException(
2994  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
2995  CHECK_LOCATION().AsString()));
2996  }
2997 
2998  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
2999  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
3000  int32_t axis = axisData.front();
3001 
3002  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3003  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3004  {
3005  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
3006  // E.g. Rank 4 tensor can have axis in range [-4, 3)
3007  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
3008  throw ParseException(
3009  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
3010  axis,
3011  CHECK_LOCATION().AsString()));
3012  }
3013 
3014  ArgMinMaxDescriptor desc;
3015  desc.m_Axis = axis;
3016  desc.m_Function = argMinMaxFunction;
3017 
3018  // Register a ArgMin/ArgMax layer.
3019  auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
3020  auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3021  IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
3022  ARMNN_ASSERT(layer != nullptr);
3023  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3024 
3025  // Register input tensor to the layer.
3026  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3027  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3028 
3029  // Register output tensor to the layer.
3030  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3031  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3032 }
3033 
3034 void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
3035 {
3036  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3037 
3038  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3039  CHECK_VALID_SIZE(inputs.size(), 2);
3040  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3041  CHECK_VALID_SIZE(outputs.size(), 1);
3042 
3043  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3044  armnn::TensorInfo indicesTensorInfo = ToTensorInfo(inputs[1]);
3045  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3046 
3047  armnn::GatherDescriptor gatherDescriptor;
3048 
3049  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3050  const auto * options = operatorPtr->builtin_options.AsGatherOptions();
3051  auto axis = options->axis;
3052 
3053  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
3054  auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
3055  auto outputDimensions = outputTensorInfo.GetNumDimensions();
3056  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
3057  {
3058  throw ParseException(
3059  fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
3060  axis,
3061  inputDimensions, inputDimensions,
3062  CHECK_LOCATION().AsString()));
3063  }
3064  if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
3065  {
3066  throw ParseException(
3067  fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
3068  outputDimensions,
3069  inputDimensions, indicesDimensions,
3070  CHECK_LOCATION().AsString()));
3071  }
3072 
3073  gatherDescriptor.m_Axis = axis;
3074 
3075  auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
3076  IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
3077  ARMNN_ASSERT(layer != nullptr);
3078  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3079 
3080  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3081  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3082 
3083  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3084  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3085 }
3086 
3087 void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
3088 {
3089  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3090 
3091  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3092  CHECK_VALID_SIZE(inputs.size(), 1);
3093  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3094  CHECK_VALID_SIZE(outputs.size(), 1);
3095 
3096  armnn::DepthToSpaceDescriptor descriptor;
3097 
3098  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3099  const auto * options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
3100  auto blockSize = options->block_size;
3101  if (blockSize < 2)
3102  {
3103  throw ParseException(
3104  fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
3105  blockSize,
3106  CHECK_LOCATION().AsString()));
3107  }
3108  descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
3109 
3110  auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
3111  IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
3112  ARMNN_ASSERT(layer != nullptr);
3113  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3114  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3115 
3116  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3117  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3118 
3119  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3120  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3121 }
3122 
3123 void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
3124 {
3125  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
3126 }
3127 
3128 void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
3129 {
3130  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
3131 }
3132 
3133 void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
3134 {
3135  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
3136 }
3137 
3138 void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
3139 {
3140  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3141 
3142  const auto &operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3143  const auto *options = operatorPtr->builtin_options.AsReducerOptions();
3144 
3145  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3146  CHECK_VALID_SIZE(inputs.size(), 2);
3147 
3148  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3149  CHECK_VALID_SIZE(outputs.size(), 1);
3150 
3151  auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
3152 
3153  armnn::TensorInfo inputTensorInfo0 = ToTensorInfo(inputs[0]);
3154  armnn::TensorInfo inputTensorInfo1 = ToTensorInfo(inputs[1]);
3155 
3156  ReduceDescriptor desc;
3157  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3158  // Get const axis value from model and set it to descriptor.
3159  if (axisBufferPtr != nullptr)
3160  {
3161  std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
3162  ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
3163 
3164  // Convert the axis to unsigned int and remove duplicates.
3165  auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
3166  std::set<unsigned int> uniqueAxis;
3167  std::transform(axisData.begin(),
3168  axisData.end(),
3169  std::inserter(uniqueAxis, uniqueAxis.begin()),
3170  [rank](int i)->unsigned int{
3171  return static_cast<uint32_t>(((i + rank) % rank)); });
3172  desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
3173  }
3174  else
3175  {
3176  for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
3177  {
3178  desc.m_vAxis.push_back(i);
3179  }
3180  }
3181 
3182  desc.m_KeepDims = options->keep_dims;
3183  desc.m_ReduceOperation = reduceOperation;
3184 
3185  // Register a new layer object, Sum.
3186  IConnectableLayer *layer = m_Network->AddReduceLayer(desc, layerName.c_str());
3187 
3188  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3189  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3190 
3191  // Register input tensor to the layer.
3192  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3193  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3194 
3195  // Register output tensor to the layer.
3196  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3197  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3198 }
3199 
3200 void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
3201 {
3202  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
3203 }
3204 
3205 void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
3206 {
3207  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
3208 }
3209 
3210 void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
3211 {
3212  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
3213 }
3214 
3215 void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
3216 {
3217  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
3218 }
3219 
3220 void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
3221 {
3222  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
3223 }
3224 
3225 void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
3226 {
3227  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3228 
3229  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3230  CHECK_VALID_SIZE(inputs.size(), 1);
3231 
3232  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3233  CHECK_VALID_SIZE(outputs.size(), 1);
3234 
3235  std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
3236  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
3237 
3239  desc.m_Operation = unaryOperation;
3240  IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
3241  ARMNN_ASSERT(layer != nullptr);
3242 
3243  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
3244  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3245 
3246  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3247  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3248 
3249  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3250  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3251 }
3252 
3253 armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
3254  unsigned int outputSlot,
3255  tflite::ActivationFunctionType activationType)
3256 {
3257  ActivationDescriptor activationDesc;
3258  std::string layerName = prevLayer->GetName();
3259 
3260  switch(activationType)
3261  {
3262  case tflite::ActivationFunctionType_NONE:
3263  {
3264  // this is a no-op: return previous layer
3265  return prevLayer;
3266  }
3267  case tflite::ActivationFunctionType_RELU:
3268  {
3269  activationDesc.m_Function = ActivationFunction::ReLu;
3270  layerName += ":RELU";
3271  break;
3272  }
3273  case tflite::ActivationFunctionType_RELU6:
3274  {
3275  activationDesc.m_Function = ActivationFunction::BoundedReLu;
3276  activationDesc.m_A = 6.0f;
3277  activationDesc.m_B = 0.0f;
3278  layerName += ":RELU6";
3279  break;
3280  }
3281  case tflite::ActivationFunctionType_TANH:
3282  {
3283  activationDesc.m_Function = ActivationFunction::TanH;
3284  activationDesc.m_A = 1.0f;
3285  activationDesc.m_B = 1.0f;
3286  layerName += ":TANH";
3287  break;
3288  }
3289 
3290  // I only put these here as a reminder what others we could support
3291  case tflite::ActivationFunctionType_RELU_N1_TO_1:
3292  case tflite::ActivationFunctionType_SIGN_BIT:
3293  default:
3294  {
3295  throw ParseException(
3296  fmt::format("TfLite parser doesn't suppport fused activation: "
3297  "{}/{} {} ",
3298  activationType,
3299  tflite::EnumNameActivationFunctionType(activationType),
3300  CHECK_LOCATION().AsString()));
3301 
3302  }
3303  }
3304 
3305  IConnectableLayer* activationLayer =
3306  m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3307 
3308  auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
3309  prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
3310  activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
3311  return activationLayer;
3312 }
3313 
3315 {
3316  if (fileName == nullptr)
3317  {
3318  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
3319  CHECK_LOCATION().AsString()));
3320  }
3321  std::error_code errorCode;
3322  fs::path pathToFile(fileName);
3323  if (!fs::exists(pathToFile, errorCode))
3324  {
3325  //fmt::format() could not be used here (format error)
3326  std::stringstream msg;
3327  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
3328  << " " << CHECK_LOCATION().AsString();
3329 
3330  throw FileNotFoundException(msg.str());
3331  }
3332  std::ifstream file(fileName, std::ios::binary);
3333  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
3334  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
3335  fileContent.size());
3336 }
3337 
3338 TfLiteParserImpl::ModelPtr TfLiteParserImpl::LoadModelFromBinary(const uint8_t * binaryContent, size_t len)
3339 {
3340  if (binaryContent == nullptr)
3341  {
3342  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
3343  CHECK_LOCATION().AsString()));
3344  }
3345  flatbuffers::Verifier verifier(binaryContent, len);
3346  if (verifier.VerifyBuffer<tflite::Model>() == false)
3347  {
3348  throw ParseException(
3349  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
3350  "flatbuffers format. size:{} {}",
3351  len,
3352  CHECK_LOCATION().AsString()));
3353  }
3354  return tflite::UnPackModel(binaryContent);
3355 }
3356 
3358  size_t subgraphIndex,
3359  size_t operatorIndex)
3360 {
3361  CHECK_MODEL(model, subgraphIndex, operatorIndex);
3362 
3363  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3364  const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3365 
3366  size_t inputCount = operatorPtr->inputs.size();
3367  TensorRawPtrVector result;
3368  for (size_t i=0; i<inputCount; ++i)
3369  {
3370  // If the input location is -1 then assume input is turned off.
3371  if (operatorPtr->inputs[i] == -1)
3372  {
3373  continue;
3374  }
3375  else
3376  {
3377  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
3378  result.push_back(subgraphPtr->tensors[inputId].get());
3379  }
3380  }
3381  return result;
3382 }
3383 
3385  size_t subgraphIndex,
3386  size_t operatorIndex)
3387 {
3388  CHECK_MODEL(model, subgraphIndex, operatorIndex);
3389 
3390  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3391  const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3392 
3393  size_t outputCount = operatorPtr->outputs.size();
3394  TensorRawPtrVector result(outputCount);
3395  for (size_t i=0; i<outputCount; ++i)
3396  {
3397  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
3398  CHECK_TENSOR(model, subgraphIndex, outputId);
3399  result[i] = subgraphPtr->tensors[outputId].get();
3400  }
3401  return result;
3402 }
3403 
3405  size_t subgraphIndex)
3406 {
3407  CHECK_SUBGRAPH(model, subgraphIndex);
3408  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3409 
3410  size_t inputCount = subgraphPtr->inputs.size();
3411  TensorIdRawPtrVector result(inputCount);
3412  for (size_t i=0; i<inputCount; ++i)
3413  {
3414  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
3415  CHECK_TENSOR(model, subgraphIndex, inputId);
3416  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
3417  }
3418  return result;
3419 }
3420 
3422  size_t subgraphIndex)
3423 {
3424  CHECK_SUBGRAPH(model, subgraphIndex);
3425  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3426 
3427  size_t outputCount = subgraphPtr->outputs.size();
3428  TensorIdRawPtrVector result(outputCount);
3429  for (size_t i=0; i<outputCount; ++i)
3430  {
3431  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
3432  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
3433  }
3434  return result;
3435 }
3436 
3437 std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
3438  size_t subgraphIndex,
3439  size_t operatorIndex)
3440 {
3441  CHECK_MODEL(model, subgraphIndex, operatorIndex);
3442  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3443  const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3444  return operatorPtr->inputs;
3445 }
3446 
3447 std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
3448  size_t subgraphIndex,
3449  size_t operatorIndex)
3450 {
3451  CHECK_MODEL(model, subgraphIndex, operatorIndex);
3452  const auto & subgraphPtr = model->subgraphs[subgraphIndex];
3453  const auto & operatorPtr = subgraphPtr->operators[operatorIndex];
3454  return operatorPtr->outputs;
3455 }
3456 
3457 void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
3458  size_t operatorIndex,
3459  IConnectableLayer* layer,
3460  const std::vector<unsigned int>& tensorIndexes,
3461  unsigned int startingSlotIndex)
3462 {
3463  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3464  ARMNN_ASSERT(layer != nullptr);
3465  if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
3466  {
3467  throw ParseException(
3468  fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
3469  " for subgraph:{} operator index:{} {}",
3470  tensorIndexes.size(),
3471  layer->GetNumInputSlots(),
3472  subgraphIndex,
3473  operatorIndex,
3474  CHECK_LOCATION().AsString()));
3475  }
3476 
3477  for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
3478  {
3479  unsigned int tensorIndex = tensorIndexes[index];
3480  armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
3481  RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
3482  }
3483 }
3484 
3485 void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
3486  size_t operatorIndex,
3487  IConnectableLayer* layer,
3488  const std::vector<unsigned int>& tensorIndexes)
3489 {
3490  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3491  ARMNN_ASSERT(layer != nullptr);
3492  if (tensorIndexes.size() != layer->GetNumOutputSlots())
3493  {
3494  throw ParseException(
3495  fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
3496  " for subgraph:{} operator index:{} {}",
3497  tensorIndexes.size(),
3498  layer->GetNumOutputSlots(),
3499  subgraphIndex,
3500  operatorIndex,
3501  CHECK_LOCATION().AsString()));
3502  }
3503 
3504  for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
3505  {
3506  unsigned int tensorIndex = tensorIndexes[slotIndex];
3507  armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
3508  RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
3509  }
3510 }
3511 
3512 void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
3513 {
3514  CHECK_SUBGRAPH(m_Model, subgraphIndex);
3515 
3516  auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
3517  for (auto const & tensorIdAndPtr : inputs)
3518  {
3519  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3520  IConnectableLayer* layer =
3521  m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3522 
3523  auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
3524  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3525 
3526  RegisterOutputSlots(subgraphIndex,
3527  VIRTUAL_OPERATOR_ID,
3528  layer,
3529  { static_cast<uint32_t>(tensorIdAndPtr.first) });
3530  }
3531 }
3532 
3533 void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
3534 {
3535  CHECK_SUBGRAPH(m_Model, subgraphIndex);
3536 
3537  auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
3538  for (auto const & tensorIdAndPtr : outputs)
3539  {
3540  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
3541  IConnectableLayer* layer =
3542  m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
3543 
3544  RegisterInputSlots(subgraphIndex,
3545  VIRTUAL_OPERATOR_ID,
3546  layer,
3547  { static_cast<uint32_t>(tensorIdAndPtr.first) });
3548  }
3549 }
3550 
3551 void TfLiteParserImpl::SetupConstantLayers(size_t subgraphIndex)
3552 {
3553  CHECK_SUBGRAPH(m_Model, subgraphIndex);
3554 
3555  const auto & subgraphPtr = m_Model->subgraphs[subgraphIndex];
3556  for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
3557  {
3558  for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
3559  {
3560  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
3561  m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
3562  {
3563  TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
3564  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
3565  auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo);
3566 
3567  std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
3568  IConnectableLayer *layer =
3569  m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
3570 
3571  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
3572  RegisterOutputSlots(subgraphIndex,
3573  VIRTUAL_OPERATOR_ID,
3574  layer,
3575  { tensorIndex });
3576 
3577  }
3578  }
3579  }
3580 }
3581 
3582 // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
3584 {
3585  CHECK_BUFFER(model, bufferIndex);
3586  return model->buffers[bufferIndex].get();
3587 }
3588 
3589 template<typename T>
3590 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3591 TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
3593  armnn::TensorInfo& tensorInfo,
3595 {
3596  auto constData = CreateConstTensorImpl<T>(bufferPtr,
3597  tensorPtr,
3598  tensorInfo,
3599  permutationVector);
3600  TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
3601  return std::make_pair(constData.first, std::move(storage));
3602 }
3603 
3604 bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
3605 {
3606  CHECK_TENSOR_PTR(tensorPtr);
3607  bool isConst = true;
3608 
3609  auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
3610  if (buffer->data.size() == 0)
3611  {
3612  isConst = false;
3613  }
3614 
3615  return isConst;
3616 }
3617 
3618 
3619 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
3620 TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
3621  armnn::TensorInfo& tensorInfo,
3623 {
3624  CHECK_TENSOR_PTR(tensorPtr);
3625  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3626  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3627 
3628  switch (tensorInfo.GetDataType())
3629  {
3631  return CreateConstTensorAndStoreData<float>(bufferPtr,
3632  tensorPtr,
3633  tensorInfo,
3634  permutationVector);
3636  return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
3637  tensorPtr,
3638  tensorInfo,
3639  permutationVector);
3641  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3642  tensorPtr,
3643  tensorInfo,
3644  permutationVector);
3646  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
3647  tensorPtr,
3648  tensorInfo,
3649  permutationVector);
3651  return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
3652  tensorPtr,
3653  tensorInfo,
3654  permutationVector);
3655  default:
3656  {
3657  std::stringstream errString;
3658  errString << "Unexpected datatype when creating const tensor: "
3659  << armnn::GetDataTypeName(tensorInfo.GetDataType())
3660  << " shape:" << tensorInfo.GetShape()
3661  << CHECK_LOCATION().AsString();
3662  throw ParseException(errString.str());
3663  }
3664  }
3665 }
3666 
3667 armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
3668  armnn::TensorInfo& tensorInfo)
3669 {
3670  CHECK_TENSOR_PTR(tensorPtr);
3671  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
3672  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
3673 
3674  return ConstTensor(tensorInfo, bufferPtr->data.data());
3675 }
3676 
3678  const std::string& name) const
3679 {
3680  CHECK_SUBGRAPH(m_Model, subgraphId);
3681  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3682  for (auto const & input : inputs)
3683  {
3684  if (input.second->name == name)
3685  {
3686  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
3687  return std::make_pair(bindingId, ToTensorInfo(input.second));
3688  }
3689  }
3690 
3691  std::stringstream bindings;
3692  for (auto const & input : inputs)
3693  {
3694  bindings << "'" << input.second->name << "' ";
3695  }
3696 
3697  throw ParseException(
3698  fmt::format("No input binding found for subgraph:{} and name:{}. "
3699  "Possible inputs are: [{}] {}",
3700  subgraphId,
3701  name,
3702  bindings.str(),
3703  CHECK_LOCATION().AsString()));
3704 }
3705 
3707  const std::string& name) const
3708 {
3709  CHECK_SUBGRAPH(m_Model, subgraphId);
3710  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
3711  for (unsigned int i = 0; i < outputs.size(); ++i)
3712  {
3713  auto const output = outputs[i];
3714  if (output.second->name == name)
3715  {
3716  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
3717  std::vector<unsigned int> shape = m_OverridenOutputShapes.size() > 0 ?
3718  m_OverridenOutputShapes[i] : AsUnsignedVector(output.second->shape);
3719  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
3720  }
3721  }
3722 
3723  std::stringstream bindings;
3724  for (auto const & output : outputs)
3725  {
3726  bindings << "'" << output.second->name << "' ";
3727  }
3728 
3729  throw ParseException(
3730  fmt::format("No output binding found for subgraph:{} and name:{}. "
3731  "Possible outputs are: [{}] {}",
3732  subgraphId,
3733  name,
3734  bindings.str(),
3735  CHECK_LOCATION().AsString()));
3736 }
3737 
3739 {
3740  return m_Model->subgraphs.size();
3741 }
3742 
3743 std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
3744 {
3745  CHECK_SUBGRAPH(m_Model, subgraphId);
3746  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
3747  std::vector<std::string> result;
3748  result.reserve(inputs.size());
3749  for (auto const & input : inputs)
3750  {
3751  result.push_back(input.second->name);
3752  }
3753  return result;
3754 }
3755 
3756 std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
3757 {
3758  CHECK_SUBGRAPH(m_Model, subgraphId);
3759  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
3760  std::vector<std::string> result;
3761  result.reserve(outputs.size());
3762  for (auto const & output : outputs)
3763  {
3764  result.push_back(output.second->name);
3765  }
3766  return result;
3767 }
3768 
3769 const std::string TfLiteParserImpl::GetVersion()
3770 {
3771  return TFLITE_PARSER_VERSION;
3772 }
3773 
3774 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]> && data)
3775 : m_FloatData(std::move(data))
3776 , m_Uint8Data(nullptr)
3777 , m_Int8Data(nullptr)
3778 , m_Int32Data(nullptr)
3779 {
3780 }
3781 
3782 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]> && data)
3783 : m_FloatData(nullptr)
3784 , m_Uint8Data(std::move(data))
3785 , m_Int8Data(nullptr)
3786 , m_Int32Data(nullptr)
3787 {
3788 }
3789 
3790 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]> && data)
3791 : m_FloatData(nullptr)
3792 , m_Uint8Data(nullptr)
3793 , m_Int8Data(std::move(data))
3794 , m_Int32Data(nullptr)
3795 {
3796 }
3797 
3798 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]> && data)
3799 : m_FloatData(nullptr)
3800 , m_Uint8Data(nullptr)
3801 , m_Int8Data(nullptr)
3802 , m_Int32Data(std::move(data))
3803 {
3804 }
3805 
3806 } // armnnTfLiteParser
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
std::unique_ptr< tflite::ModelT > ModelPtr
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
uint32_t m_Axis
0-based axis along which to stack the input tensors.
A ViewsDescriptor for the SplitterLayer.
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:62
float m_ScaleW
Center size encoding scale weight.
bool IsTypeSpaceMatch(const TensorInfo &other) const
Check that the types are the same and, if quantize, that the quantization parameters are the same...
Definition: Tensor.cpp:423
uint32_t m_PadBottom
Padding bottom value in the height dimension.
bool m_BiasEnabled
Enable/disable bias.
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
#define ARMNN_THROW_PARSE_EXCEPTION(msg)
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
uint32_t m_PadBottom
Padding bottom value in the height dimension.
uint32_t m_PadLeft
Padding left value in the width dimension.
const tflite::TensorT * TensorRawPtr
std::string AsString() const
Definition: Exceptions.hpp:29
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1...
A ReshapeDescriptor for the ReshapeLayer.
std::vector< int > m_Begin
Begin values for the input that will be sliced.
const tflite::BufferT * BufferRawPtr
float m_PadValue
Optional value to use for padding, defaults to 0.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
float m_ScaleX
Center size encoding scale x.
TensorShape m_InputShape
Required shape of all input tensors.
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
uint32_t m_PoolWidth
Pooling width value.
A Convolution2dDescriptor for the Convolution2dLayer.
uint32_t m_PadLeft
Padding left value in the width dimension.
bool m_KeepDims
if true then output shape has no change.
bool m_BiasEnabled
Enable/disable bias.
std::vector< unsigned int > m_OutputShape
unsigned int GetNumBytes() const
Definition: Tensor.cpp:418
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
float m_Beta
Exponentiation value.
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network output identified by the given layer...
ArgMinMaxFunction m_Function
Specify if the function is to find Min or Max.
Definition: Descriptors.hpp:70
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
bool m_OutputShapeEnabled
Output shape if it has been specified.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
#define ARMNN_LOG(severity)
Definition: Logging.hpp:202
uint32_t m_PadTop
Padding top value in the height dimension.
std::vector< BackendOptions > NetworkOptions
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
uint32_t m_PadRight
Padding right value in the width dimension.
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
std::unique_ptr< ITfLiteParser, void(*)(ITfLiteParser *parser)> ITfLiteParserPtr
std::unique_ptr< tflite::OperatorT > OperatorPtr
unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
Copyright (c) 2021 ARM Limited and Contributors.
void IgnoreUnused(Ts &&...)
uint32_t m_PadBottom
Padding bottom value in the height dimension.
int32_t m_BeginMask
Begin mask value.
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
SizeType GetSize() const
Definition: Types.hpp:274
uint32_t m_DilationY
Dilation along y axis.
int32_t m_EndMask
End mask value.
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
PoolingAlgorithm
Definition: Types.hpp:115
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left, right}.
uint32_t m_DilationY
Dilation factor value for height dimension.
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:243
#define TFLITE_PARSER_VERSION
TFLITE_PARSER_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch ver...
Definition: Version.hpp:25
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:191
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:189
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)
Create the network from a flatbuffers binary.
A ResizeDescriptor for the ResizeLayer.
static BufferRawPtr GetBuffer(const ModelPtr &model, size_t bufferIndex)
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
A StackDescriptor for the StackLayer.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
constexpr char const * GetUnaryOperationAsCString(UnaryOperation operation)
Definition: TypesUtils.hpp:71
TensorShape m_TargetShape
Target shape value.
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
uint32_t m_PoolHeight
Pooling height value.
uint32_t m_PadTop
Padding top value in the height dimension.
uint32_t m_MaxDetections
Maximum numbers of detections.
A PadDescriptor for the PadLayer.
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
ReduceOperation
Definition: Types.hpp:122
void CheckTensor(const ConstTensor &t)
Definition: TensorTest.cpp:165
BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network input identified by the given layer ...
bool CheckShape(const armnn::TensorShape &actual, const std::vector< uint32_t > &expected)
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
DataType
Definition: Types.hpp:36
static armnn::TensorInfo OutputShapeOfSqueeze(const std::vector< uint32_t > &squeezeDims, const armnn::TensorInfo &inputTensorInfo)
float m_NmsIouThreshold
Intersection over union threshold.
uint32_t m_PadRight
Padding right value in the width dimension.
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
uint32_t m_DilationX
Dilation factor value for width dimension.
uint32_t m_PadTop
Padding top value in the height dimension.
std::string FileLine() const
Definition: Exceptions.hpp:37
Status SetViewSize(uint32_t view, uint32_t coord, uint32_t value)
Set the size of the views.
#define ARMNN_ASSERT_MSG(COND, MSG)
Definition: Assert.hpp:15
int32_t m_NewAxisMask
New axis mask value.
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept...
static std::vector< int32_t > & GetInputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
std::vector< unsigned int > m_BlockShape
Block shape values.
An output connection slot for a layer.
Definition: INetwork.hpp:38
A L2NormalizationDescriptor for the L2NormalizationLayer.
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:469
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:56
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
float GetQuantizationScale() const
Definition: Tensor.cpp:452
DataType GetDataType() const
Definition: Tensor.hpp:194
An OriginsDescriptor for the ConcatLayer.
A ReduceDescriptor for the REDUCE operators.
bool has_value() const noexcept
Definition: Optional.hpp:53
A FullyConnectedDescriptor for the FullyConnectedLayer.
int32_t m_EllipsisMask
Ellipsis mask value.
bool m_BiasEnabled
Enable/disable bias.
static ModelPtr LoadModelFromFile(const char *fileName)
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:314
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
A GatherDescriptor for the GatherLayer.
#define CHECK_VALID_SIZE(ACTUAL,...)
uint32_t m_NumClasses
Number of classes.
#define CHECKED_NON_NEGATIVE(VALUE)
std::vector< TensorRawPtr > TensorRawPtrVector
size_t GetSubgraphCount() const
Return the number of subgraphs in the parsed model.
uint32_t m_PadTop
Padding top value in the height dimension.
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
A StandInDescriptor for the StandIn layer.
bool m_UseRegularNms
Use Regular NMS.
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
std::vector< unsigned int > m_BlockShape
Block shape value.
std::vector< int > m_Stride
Stride values for the input that will be sliced.
bool IsActivationSupported(const BackendId &backend, const TensorInfo &input, const TensorInfo &output, const ActivationDescriptor &descriptor, char *reasonIfUnsupported=nullptr, size_t reasonIfUnsupportedMaxLength=1024)
Deprecated in favor of IBackend and ILayerSupport interfaces.
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:25
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197
uint32_t m_NumInputs
Number of input tensors.
A SliceDescriptor for the SliceLayer.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
#define CHECK_TENSOR_PTR(TENSOR_PTR)
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
float m_ScaleH
Center size encoding scale height.
std::vector< int > m_End
End values for the input that will be sliced.
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Struct for the users to pass backend specific options.
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH, Elu).
Definition: Descriptors.hpp:50
static TensorRawPtrVector GetInputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
uint32_t m_DilationX
Dilation along x axis.
const armnnSerializer::TensorInfo * TensorRawPtr
static TensorRawPtrVector GetOutputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
uint32_t m_PadLeft
Padding left value in the width dimension.
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
static std::vector< int32_t > & GetOutputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX)
int32_t m_Axis
The axis in params to gather indices from.
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:98
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
ArgMinMaxFunction
Definition: Types.hpp:83
OutputShapeRounding m_OutputShapeRounding
The rounding method for the output shape. (Floor, Ceiling).
void SetConcatAxis(unsigned int concatAxis)
Set the concatenation axis value.
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
ResizeMethod
Definition: Types.hpp:130
A MeanDescriptor for the MeanLayer.
UnaryOperation
Definition: Types.hpp:105
armnn::BindingPointInfo BindingPointInfo
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:35
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
uint32_t m_PadRight
Padding right value in the width dimension.
A TransposeDescriptor for the TransposeLayer.
A StridedSliceDescriptor for the StridedSliceLayer.
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
int m_Axis
Axis to reduce across the input tensor.
Definition: Descriptors.hpp:72
virtual const char * GetName() const =0
Returns the name of the layer.
float m_ScaleY
Center size encoding scale y.
float m_NmsScoreThreshold
NMS score threshold.
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173
virtual int Connect(IInputSlot &destination)=0
const char * m_Function
Definition: Exceptions.hpp:16
A Pooling2dDescriptor for the Pooling2dLayer.
std::vector< std::string > GetSubgraphInputTensorNames(size_t subgraphId) const
Return the input tensor names for a given subgraph.
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191
#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID)
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
Definition: Descriptors.hpp:52
bool IsQuantized() const
Definition: Tensor.cpp:495
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98
A SoftmaxDescriptor for the SoftmaxLayer.
DataLayout::NCHW DataLayout::NCHW DataLayout::NHWC DataLayout::NHWC true
Status SetViewOriginCoord(uint32_t view, uint32_t coord, uint32_t value)
Set the view origin coordinates.
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu, LeakyReLu, Abs, Sqrt, Square, Elu).
Definition: Descriptors.hpp:48
An input connection slot for a layer.
Definition: INetwork.hpp:25
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:19
uint32_t m_PadLeft
Padding left value in the width dimension.
unsigned int GetNumElements() const
Definition: Tensor.hpp:192
ActivationFunction
Definition: Types.hpp:67
uint32_t m_PadRight
Padding right value in the width dimension.
bool m_ConstantWeights
Enable/disable constant weights and biases.