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