ArmNN
 23.08
TfLiteParser.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017-2023 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #include "TfLiteParser.hpp"
7 
9 #include "armnn/LstmParams.hpp"
10 
11 #include <armnn/BackendOptions.hpp>
12 #include <armnn/Descriptors.hpp>
13 #include <armnn/Exceptions.hpp>
14 #include <armnn/Logging.hpp>
15 #include <armnn/Tensor.hpp>
17 #include <armnn/TypesUtils.hpp>
18 #include <armnn/utility/Assert.hpp>
21 
22 // armnnUtils:
23 #include <armnnUtils/Permute.hpp>
25 
26 #include <ParserHelper.hpp>
27 #include <VerificationHelpers.hpp>
28 
29 // The generated code based on the Tf Lite schema:
30 #include <schema_generated.h>
31 
32 #include <flatbuffers/flexbuffers.h>
33 
34 #include <fmt/format.h>
35 
36 #include <algorithm>
37 #include <iostream>
38 #include <limits>
39 #include <numeric>
40 
41 #define ARMNN_THROW_PARSE_EXCEPTION(msg) \
42  { \
43  throw armnn::ParseException( static_cast<const std::stringstream&>( std::stringstream() << msg \
44  << ": " \
45  << CHECK_LOCATION().AsString()).str()); \
46  }
47 
48 using namespace armnn;
50 namespace armnnTfLiteParser
51 {
52 
53 ITfLiteParser::ITfLiteParser(const armnn::Optional<TfLiteParserOptions>& options) :
54  pTfLiteParserImpl(new TfLiteParserImpl(options)) {}
55 
56 ITfLiteParser::~ITfLiteParser() = default;
57 
58 ITfLiteParser* ITfLiteParser::CreateRaw(const armnn::Optional<TfLiteParserOptions>& options)
59 {
60  return new ITfLiteParser(options);
61 }
62 
63 ITfLiteParserPtr ITfLiteParser::Create(const armnn::Optional<TfLiteParserOptions>& options)
64 {
65  return ITfLiteParserPtr(CreateRaw(options), &ITfLiteParser::Destroy);
66 }
67 
68 void ITfLiteParser::Destroy(ITfLiteParser* parser)
69 {
70  delete parser;
71 }
72 
73 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinaryFile(const char* graphFile)
74 {
75  return pTfLiteParserImpl->CreateNetworkFromBinaryFile(graphFile);
76 }
77 
78 armnn::INetworkPtr ITfLiteParser::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
79 {
80  return pTfLiteParserImpl->CreateNetworkFromBinary(binaryContent);
81 }
82 
83 BindingPointInfo ITfLiteParser::GetNetworkInputBindingInfo(size_t subgraphId,
84  const std::string& name) const
85 {
86  return pTfLiteParserImpl->GetNetworkInputBindingInfo(subgraphId, name);
87 }
88 
89 BindingPointInfo ITfLiteParser::GetNetworkOutputBindingInfo(size_t subgraphId,
90  const std::string& name) const
91 {
92  return pTfLiteParserImpl->GetNetworkOutputBindingInfo(subgraphId, name);
93 }
94 
95 size_t ITfLiteParser::GetSubgraphCount() const
96 {
97  return pTfLiteParserImpl->GetSubgraphCount();
98 }
99 
100 std::vector<std::string> ITfLiteParser::GetSubgraphInputTensorNames(size_t subgraphId) const
101 {
102  return pTfLiteParserImpl->GetSubgraphInputTensorNames(subgraphId);
103 }
104 
105 std::vector<std::string> ITfLiteParser::GetSubgraphOutputTensorNames(size_t subgraphId) const
106 {
107  return pTfLiteParserImpl->GetSubgraphOutputTensorNames(subgraphId);
108 }
109 
110 namespace
111 {
112 
113 const uint32_t VIRTUAL_OPERATOR_ID = std::numeric_limits<uint32_t>::max();
114 
115 void CheckSubgraph(const TfLiteParserImpl::ModelPtr& model,
116  size_t subgraphIndex,
117  const CheckLocation& location)
118 {
119  if (model.get() == nullptr)
120  {
121  throw ParseException(
122  fmt::format("{} was called with invalid (null) model. "
123  "Possible reason is that the model is not yet loaded and Unpack(ed). "
124  "subgraph:{} at {}",
125  location.m_Function,
126  subgraphIndex,
127  location.FileLine()));
128  }
129  else if (subgraphIndex >= model->subgraphs.size())
130  {
131  throw ParseException(
132  fmt::format("{} was called with an invalid subgraph index. "
133  "subgraph:{} at {}",
134  location.m_Function,
135  subgraphIndex,
136  location.FileLine()));
137  }
138 }
139 
140 #define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX) \
141  CheckSubgraph(MODEL, SUBGRAPH_INDEX, CHECK_LOCATION())
142 
143 void CheckModel(const TfLiteParserImpl::ModelPtr& model,
144  size_t subgraphIndex,
145  size_t operatorIndex,
146  const CheckLocation& location)
147 {
148  if (model.get() == nullptr)
149  {
150  throw ParseException(
151  fmt::format("{} was called with invalid (null) model. "
152  "Possible reason is that the model is not yet loaded and Unpack(ed). "
153  "subgraph:{} operator:{} at {}",
154  location.m_Function,
155  subgraphIndex,
156  operatorIndex,
157  location.FileLine()));
158  }
159  else if (subgraphIndex >= model->subgraphs.size())
160  {
161  throw ParseException(
162  fmt::format("{} was called with an invalid subgraph index. "
163  "subgraph:{} operator:{} at {}",
164  location.m_Function,
165  subgraphIndex,
166  operatorIndex,
167  location.FileLine()));
168  }
169  else if (operatorIndex >= model->subgraphs[subgraphIndex]->operators.size() &&
170  operatorIndex != VIRTUAL_OPERATOR_ID)
171  {
172  throw ParseException(
173  fmt::format("{} was called with an invalid operator index. "
174  "subgraph:{} operator:{} at {}",
175  location.m_Function,
176  subgraphIndex,
177  operatorIndex,
178  location.FileLine()));
179  }
180 }
181 
182 #define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX) \
183  CheckModel(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX, CHECK_LOCATION())
184 
185 void CheckTensor(const TfLiteParserImpl::ModelPtr& model,
186  size_t subgraphIndex,
187  size_t tensorIndex,
188  const CheckLocation& location)
189 {
190  // the tensor index is the only one to check here
191  if (tensorIndex >= model->subgraphs[subgraphIndex]->tensors.size())
192  {
193  throw ParseException(
194  fmt::format("{} was called with an invalid tensor index. "
195  "subgraph:{} tensor:{} at {}",
196  location.m_Function,
197  subgraphIndex,
198  tensorIndex,
199  location.FileLine()));
200  }
201 }
202 
203 #define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX) \
204  CheckTensor(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX, CHECK_LOCATION())
205 
206 void CheckTensorPtr(TfLiteParserImpl::TensorRawPtr rawPtr,
207  const CheckLocation& location)
208 {
209  if (rawPtr == nullptr)
210  {
211  throw ParseException(
212  fmt::format("{} was called with a null tensor pointer at {}", location.m_Function, location.FileLine()));
213  }
214 }
215 
216 #define CHECK_TENSOR_PTR(TENSOR_PTR) \
217  CheckTensorPtr(TENSOR_PTR, CHECK_LOCATION())
218 
219 void CheckBuffer(const TfLiteParserImpl::ModelPtr& model,
220  size_t bufferIndex,
221  const CheckLocation& location)
222 {
223  if (model.get() == nullptr)
224  {
225  throw ParseException(
226  fmt::format("{} was called with invalid (null) model. "
227  "Possible reason is that the model is not yet loaded and Unpack(ed). "
228  "buffer:{} at {}",
229  location.m_Function,
230  bufferIndex,
231  location.FileLine()));
232  }
233  else if (bufferIndex >= model->buffers.size())
234  {
235  throw ParseException(
236  fmt::format("{} was called with an invalid buffer index. "
237  "buffer index:{} at {}",
238  location.m_Function,
239  bufferIndex,
240  location.FileLine()));
241  }
242  else if (model->buffers[bufferIndex].get() == nullptr)
243  {
244  throw ParseException(
245  fmt::format("The buffer #{} is null. {}",
246  bufferIndex,
247  location.AsString()));
248  }
249 }
250 
251 #define CHECK_BUFFER(MODEL, BUFFER_INDEX) \
252  CheckBuffer(MODEL, BUFFER_INDEX, CHECK_LOCATION())
253 
254 void CheckBufferSize(TfLiteParserImpl::BufferRawPtr bufferPtr,
255  const armnn::TensorInfo& tensorInfo,
256  uint32_t bufferId,
257  const CheckLocation& location)
258 {
259  if (bufferPtr == nullptr)
260  {
261  throw ParseException(
262  fmt::format("BufferPtr is null for buffer:{}. {}",
263  bufferId,
264  location.AsString()));
265  }
266  else if(tensorInfo.GetNumElements() > bufferPtr->data.size() ||
267  tensorInfo.GetNumBytes() > bufferPtr->data.size())
268  {
269  std::stringstream ss;
270  ss << "Buffer #" << bufferId << " has " << bufferPtr->data.size() << " bytes. "
271  << "For tensor: " << tensorInfo.GetShape()
272  << " expecting: " << tensorInfo.GetNumBytes() << " bytes and "
273  << tensorInfo.GetNumElements() << " elements. " << location.AsString();
274  throw ParseException(ss.str());
275  }
276 }
277 
278 
279 tflite::BuiltinOperator GetOpCode(const TfLiteParserImpl::ModelPtr& model, size_t subgraphIndex, size_t operatorIndex)
280 {
281  const auto& operatorPtr = model->subgraphs[subgraphIndex]->operators[operatorIndex];
282  auto opcodeIndex = operatorPtr->opcode_index;
283 
284 // work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
285 #if defined(ARMNN_POST_TFLITE_2_3)
286  auto opcode = std::max(model->operator_codes[opcodeIndex]->builtin_code,
287  static_cast<tflite::BuiltinOperator>(model->operator_codes[opcodeIndex]->deprecated_builtin_code));
288 #else
289  auto opcode = model->operator_codes[opcodeIndex]->builtin_code;
290 #endif
291  return opcode;
292 }
293 
294 std::vector<unsigned int> GetUIntBuffer(armnn::TensorInfo info,
295  const TfLiteParserImpl::ModelPtr& model,
296  size_t bufferIndex)
297 {
298  TfLiteParserImpl::BufferRawPtr bufferPtr = TfLiteParserImpl::GetBuffer(model, bufferIndex);
299  std::vector<unsigned int> buffer(info.GetNumElements());
300 
301  if (info.GetDataType() == DataType::Signed32)
302  {
303  ::memcpy(buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
304  }
305  else if (info.GetDataType() == DataType::Signed64)
306  {
307  std::vector<uint64_t> uint64Buffer(info.GetNumElements());
308  ::memcpy(uint64Buffer.data(), bufferPtr->data.data(), bufferPtr->data.size());
309  buffer.assign(std::begin(uint64Buffer), std::end(uint64Buffer));
310  }
311  else
312  {
313  CheckLocation location = CHECK_LOCATION();
314  throw ParseException(
315  fmt::format("Unsupported data type for uint buffer {}, only Signed 32 or Signed 64 are supported. {}",
316  GetDataTypeName(info.GetDataType()),
317  location.AsString()));
318  }
319  return buffer;
320 }
321 
322 #define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID) \
323  CheckBufferSize(BUFFER_PTR, TENSOR_INFO, BUFFER_ID, CHECK_LOCATION())
324 
325 bool IsActivationSupported(tflite::ActivationFunctionType activationType)
326 {
327  switch(activationType)
328  {
329  case tflite::ActivationFunctionType_NONE:
330  case tflite::ActivationFunctionType_RELU:
331  case tflite::ActivationFunctionType_RELU6:
332  case tflite::ActivationFunctionType_TANH:
333  {
334  return true;
335  }
336  default:
337  {
338  return false;
339  }
340  }
341 }
342 
343 #define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX) \
344  do { \
345  if (IsActivationSupported(OPTION->fused_activation_function) == false) \
346  { \
347  throw ParseException( \
348  fmt::format("TfLite parser doesn't support fused activation: " \
349  "{}/{} in {} subgraph:{} operator:{} at {}", \
350  OPTION->fused_activation_function, \
351  tflite::EnumNameActivationFunctionType(\
352  OPTION->fused_activation_function), \
353  __func__, \
354  SUBGRAPH_INDEX, \
355  OPERATOR_INDEX, \
356  CHECK_LOCATION().FileLine())); \
357  } \
358  } while(false)
359 
360 
361 std::vector<unsigned int> AsUnsignedVector(const std::vector<int32_t>& in)
362 {
363  std::vector<unsigned int> result;
364  result.reserve(in.size());
365  for (auto& i : in)
366  {
367  // If the location of the input data is -1 then the input should be ignored.
368  if (i == -1)
369  {
370  continue;
371  }
372  result.push_back(CHECKED_NON_NEGATIVE(i));
373  }
374  return result;
375 }
376 
377 bool IsOptionalOperandPresent(int input)
378 {
379  return (input >= 0);
380 }
381 
382 void CalcPadding(uint32_t inputSize,
383  uint32_t filterSize,
384  uint32_t stride,
385  uint32_t dilation,
386  uint32_t& paddingFront,
387  uint32_t& paddingBack,
388  tflite::Padding padding)
389 {
390  paddingFront = 0;
391  paddingBack = 0;
392  if (padding == tflite::Padding_SAME)
393  {
394  uint32_t outputSize = (inputSize + stride - 1) / stride;
395  uint32_t dilatedSize = filterSize + (dilation - 1) * (filterSize - 1);
396  uint32_t temp = (outputSize - 1) * stride + dilatedSize;
397  if (temp > inputSize)
398  {
399  paddingFront = (temp - inputSize) / 2;
400  paddingBack = (temp - inputSize) - paddingFront;
401  }
402  }
403 }
404 
405 // Function that calculates explicit padding when the output shape is known.
406 // At the moment the output is only given as an input parameter in Transpose Convolution,
407 // not in Convolution and Depthwise Convolution
408 void CalcPadding(uint32_t inputSize,
409  uint32_t filterSize,
410  uint32_t stride,
411  uint32_t dilation,
412  uint32_t& paddingFront,
413  uint32_t& paddingBack,
414  tflite::Padding padding,
415  uint32_t outputSize)
416 {
417  IgnoreUnused(dilation);
418  paddingFront = 0;
419  paddingBack = 0;
420  if (padding == tflite::Padding_SAME)
421  {
422  uint32_t totalPadding = (inputSize - 1) * stride + filterSize - outputSize;
423  paddingFront = totalPadding / 2;
424  paddingBack = totalPadding - paddingFront;
425  }
426 }
427 
429  const std::vector<unsigned int>& shape,
430  const bool outputTensor = false)
431 {
432  armnn::DataType type;
433  CHECK_TENSOR_PTR(tensorPtr);
434 
435  switch (tensorPtr->type)
436  {
437  case tflite::TensorType_UINT8:
439  break;
440  case tflite::TensorType_FLOAT32:
442  break;
443  case tflite::TensorType_FLOAT16:
445  break;
446  case tflite::TensorType_INT8:
447  if (tensorPtr->quantization->zero_point.size() == 1)
448  {
449  // Per-tensor
451  }
452  else
453  {
454  // Per-channel
456  }
457  break;
458  case tflite::TensorType_INT16:
460  break;
461  case tflite::TensorType_INT32:
463  break;
464  case tflite::TensorType_INT64:
466  break;
467  case tflite::TensorType_BOOL:
469  break;
470  default:
471  {
472  CheckLocation location = CHECK_LOCATION();
473  throw ParseException(
474  fmt::format("Unsupported data type {} = {} for tensor: {}. {}",
475  tensorPtr->type,
476  tflite::EnumNameTensorType(tensorPtr->type),
477  tensorPtr->name,
478  location.AsString()));
479  }
480  }
481  TensorShape tensorShape;
482 
483  std::vector<unsigned int> safeShape = shape;
484  if (shape.size() == 0)
485  {
486  safeShape.push_back(1);
487  }
488 
489  if (!outputTensor)
490  {
491  tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(safeShape.size()), safeShape.data());
492  }
493  else
494  {
495  size_t shapeSignatureSize = tensorPtr->shape_signature.size();
496 
497  // If a shape signature exists we will use that to infer dynamic tensors
498  if (shapeSignatureSize != 0)
499  {
500  // If the shape is incompatible with the shape signature override the shape
501  if (shapeSignatureSize != shape.size())
502  {
503  safeShape = {};
504 
505  for (unsigned int i = 0; i < shapeSignatureSize; ++i)
506  {
507  unsigned int dim = tensorPtr->shape_signature[i] > -1 ?
508  static_cast<unsigned int>(tensorPtr->shape_signature[i]) : 0;
509  safeShape.push_back(dim);
510  }
511  }
512 
513  std::unique_ptr<bool[]> dimMask = std::make_unique<bool[]>(tensorPtr->shape_signature.size());
514  bool batchOnly = true;
515  for (unsigned int i = 0; i < tensorPtr->shape_signature.size(); ++i)
516  {
517  dimMask[i] = tensorPtr->shape_signature[i] != -1;
518 
519  if (i > 0 && !dimMask[i])
520  {
521  batchOnly = false;
522  }
523  }
524  if (batchOnly)
525  {
526  dimMask[0] = true;
527  }
528  tensorShape = TensorShape(static_cast<unsigned int>(safeShape.size()), safeShape.data(), dimMask.get());
529  }
530  // If there is no shape signature treat the tensor as dynamic if the shape has a size of zero
531  else if (shape.size() == 0)
532  {
533  tensorShape = TensorShape(1, false);
534  }
535  else
536  {
537  tensorShape = TensorShape(armnn::numeric_cast<unsigned int>(shape.size()), shape.data());
538  }
539  }
540 
541  float quantizationScale = 1.0f;
542  int32_t quantizationOffset = 0;
543 
544  if (tensorPtr->quantization.get())
545  {
546  if (tensorPtr->quantization->scale.size() <= 1)
547  {
548  CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
549  CHECK_VALID_SIZE(tensorPtr->quantization->zero_point.size(), 0, 1);
550 
551  if (tensorPtr->quantization->scale.size() == 1)
552  {
553  quantizationScale = tensorPtr->quantization->scale[0];
554  }
555  if (tensorPtr->quantization->zero_point.size() == 1)
556  {
557  // NOTE: we lose precision here when converting from 64 bit to 32
558  // but this is what we support at the moment in ArmNN
559  quantizationOffset = armnn::numeric_cast<int32_t>(tensorPtr->quantization->zero_point[0]);
560  }
561 
562  armnn::TensorInfo result(tensorShape,
563  type,
564  quantizationScale,
565  quantizationOffset);
566  return result;
567  }
568  else
569  {
570  std::vector<float> quantizationScales;
571  std::vector<int32_t> quantizationOffsets;
572 
573  // Scale
574  std::copy(tensorPtr->quantization->scale.begin(),
575  tensorPtr->quantization->scale.end(),
576  std::back_inserter(quantizationScales));
577 
578  // QSymmS8 Per-axis
579  armnn::TensorInfo result(tensorShape,
580  type,
581  quantizationScales,
582  armnn::numeric_cast<unsigned int>(tensorPtr->quantization->quantized_dimension));
583  return result;
584  }
585  }
586  else
587  {
588  armnn::TensorInfo result(tensorShape,
589  type,
590  quantizationScale,
591  quantizationOffset);
592  return result;
593  }
594 }
595 
597  const bool outputTensor = false)
598 {
599  auto const& dimensions = AsUnsignedVector(tensorPtr->shape);
600  return ToTensorInfo(tensorPtr, dimensions, outputTensor);
601 }
602 
603 template<typename T>
604 std::pair<armnn::ConstTensor, std::unique_ptr<T[]>>
605 CreateConstTensorImpl(TfLiteParserImpl::BufferRawPtr bufferPtr,
607  armnn::TensorInfo& tensorInfo,
609 {
610  IgnoreUnused(tensorPtr);
611 
612  if (!tensorPtr)
613  {
614  throw armnn::ParseException(fmt::format("Tensor pointer is null {}", CHECK_LOCATION().AsString()));
615  }
616 
617  if (!bufferPtr)
618  {
619  throw armnn::ParseException(fmt::format("Buffer for buffer:{} is null", tensorPtr->buffer).c_str());
620  }
621 
622  std::unique_ptr<T[]> data(new T[tensorInfo.GetNumElements()]);
623 
624  if (permutationVector.has_value() && permutationVector.value().GetSize() > 0)
625  {
626  tensorInfo = armnnUtils::Permuted(tensorInfo, permutationVector.value());
627  armnnUtils::Permute(tensorInfo.GetShape(), permutationVector.value(),
628  reinterpret_cast<const T*>(bufferPtr->data.data()), data.get(), sizeof(T));
629  }
630  else
631  {
632  ::memcpy(data.get(), bufferPtr->data.data(), tensorInfo.GetNumBytes());
633  }
634 
635  // Make sure isConstant flag is set.
636  tensorInfo.SetConstant();
637 
638  return std::make_pair(ConstTensor(tensorInfo, data.get()), std::move(data));
639 }
640 
641 armnn::LayerBindingId GenerateLayerBindingId(size_t subgraphIndex, size_t tensorIndex)
642 {
643  // generate the binding id by shifting the tensor id by 8 bit
644  // and add the subgraph id, which allows 256 subgraphs
645  return static_cast<armnn::LayerBindingId>((tensorIndex<<8)+subgraphIndex);
646 }
647 
648 bool CheckShape(const armnn::TensorShape& actual, const std::vector<int32_t>& expected)
649 {
650  const unsigned int actualSize = actual.GetNumDimensions();
651  if (actualSize != expected.size())
652  {
653  return false;
654  }
655 
656  for (unsigned int i = 0u; i < actualSize; i++)
657  {
658  if (expected[i] < 0 ||
659  actual[i] != static_cast<unsigned int>(expected[i]))
660  {
661  return false;
662  }
663  }
664 
665  return true;
666 }
667 
668 bool CheckShape(const armnn::TensorShape& actual, const armnn::TensorShape& expected)
669 {
670  std::vector<int32_t> expectedVec;
671  for (uint32_t i = 0; i < expected.GetNumDimensions(); i++)
672  {
673  expectedVec.push_back(expected[i]);
674  }
675  return CheckShape(actual, expectedVec);
676 }
677 
678 void CheckMatchingQuantization(const TensorInfo& first,
679  const TensorInfo& second,
680  const std::string& descName,
681  std::string const& firstName,
682  std::string const& secondName)
683 {
684  if (!first.IsQuantized() ||
685  !second.IsQuantized())
686  {
687  // Not a quantized type, ignore the validation
688  return;
689  }
690 
691  DataType firstDataType = first.GetDataType();
692  DataType secondDataType = second.GetDataType();
693 
694  if (firstDataType != secondDataType)
695  {
696  throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
697  " must be of the same quantized type, " +
698  firstName + " is " + GetDataTypeName(firstDataType) + ", " +
699  secondName + " is " + GetDataTypeName(secondDataType));
700  }
701 
702  if (!first.IsTypeSpaceMatch(second))
703  {
704  throw InvalidArgumentException(descName + ": " + firstName + " and " + secondName +
705  " must have the same quantization space, " +
706  firstName + " has offset " + std::to_string(first.GetQuantizationOffset()) +
707  " and scale " + std::to_string(first.GetQuantizationScale()) + ", " +
708  secondName + " has offset " + std::to_string(second.GetQuantizationOffset()) +
709  " and scale " + std::to_string(second.GetQuantizationScale()));
710  }
711 }
712 
713 bool IsDynamic(TfLiteParserImpl::TensorRawPtr tensorPtr)
714 {
715  auto shape = tensorPtr->shape;
716 
717  if (shape.empty())
718  {
719  return true;
720  }
721  auto shapeSig = tensorPtr->shape_signature;
722 
723  if (shapeSig.empty())
724  {
725  return false;
726  }
727 
728  for (unsigned int i = 0; i < shapeSig.size() ; ++i)
729  {
730  if (shapeSig[i] == -1)
731  {
732  return true;
733  }
734  }
735  return false;
736 }
737 
738 } // <anonymous>
739 
740 TfLiteParserImpl::TfLiteParserImpl(const Optional<ITfLiteParser::TfLiteParserOptions>& options)
741 : m_Options(options)
742 , m_Network(nullptr, nullptr)
743 , m_ParserFunctions(tflite::BuiltinOperator_MAX+1, &TfLiteParserImpl::ParseUnsupportedOperator)
744 {
745  // register supported operators
746  m_ParserFunctions[tflite::BuiltinOperator_ABS] = &TfLiteParserImpl::ParseAbs;
747  m_ParserFunctions[tflite::BuiltinOperator_ADD] = &TfLiteParserImpl::ParseAdd;
748  m_ParserFunctions[tflite::BuiltinOperator_ARG_MIN] = &TfLiteParserImpl::ParseArgMin;
749  m_ParserFunctions[tflite::BuiltinOperator_ARG_MAX] = &TfLiteParserImpl::ParseArgMax;
750  m_ParserFunctions[tflite::BuiltinOperator_AVERAGE_POOL_2D] = &TfLiteParserImpl::ParseAveragePool2D;
751  m_ParserFunctions[tflite::BuiltinOperator_BATCH_TO_SPACE_ND] = &TfLiteParserImpl::ParseBatchToSpaceND;
752  m_ParserFunctions[tflite::BuiltinOperator_BATCH_MATMUL] = &TfLiteParserImpl::ParseBatchMatMul;
753  m_ParserFunctions[tflite::BuiltinOperator_CEIL] = &TfLiteParserImpl::ParseCeil;
754  m_ParserFunctions[tflite::BuiltinOperator_CAST] = &TfLiteParserImpl::ParseCast;
755  m_ParserFunctions[tflite::BuiltinOperator_CONCATENATION] = &TfLiteParserImpl::ParseConcatenation;
756  m_ParserFunctions[tflite::BuiltinOperator_CONV_2D] = &TfLiteParserImpl::ParseConv2D;
757  // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
758  #if defined(ARMNN_POST_TFLITE_2_4)
759  m_ParserFunctions[tflite::BuiltinOperator_CONV_3D] = &TfLiteParserImpl::ParseConv3D;
760  #endif
761  m_ParserFunctions[tflite::BuiltinOperator_CUSTOM] = &TfLiteParserImpl::ParseCustomOperator;
762  m_ParserFunctions[tflite::BuiltinOperator_DEPTH_TO_SPACE] = &TfLiteParserImpl::ParseDepthToSpace;
763  m_ParserFunctions[tflite::BuiltinOperator_DEPTHWISE_CONV_2D] = &TfLiteParserImpl::ParseDepthwiseConv2D;
764  m_ParserFunctions[tflite::BuiltinOperator_DEQUANTIZE] = &TfLiteParserImpl::ParseDequantize;
765  m_ParserFunctions[tflite::BuiltinOperator_DIV] = &TfLiteParserImpl::ParseDiv;
766  m_ParserFunctions[tflite::BuiltinOperator_ELU] = &TfLiteParserImpl::ParseElu;
767  m_ParserFunctions[tflite::BuiltinOperator_EQUAL] = &TfLiteParserImpl::ParseEqual;
768  m_ParserFunctions[tflite::BuiltinOperator_EXP] = &TfLiteParserImpl::ParseExp;
769  m_ParserFunctions[tflite::BuiltinOperator_EXPAND_DIMS] = &TfLiteParserImpl::ParseExpandDims;
770  m_ParserFunctions[tflite::BuiltinOperator_FLOOR_DIV] = &TfLiteParserImpl::ParseFloorDiv;
771  m_ParserFunctions[tflite::BuiltinOperator_FULLY_CONNECTED] = &TfLiteParserImpl::ParseFullyConnected;
772  m_ParserFunctions[tflite::BuiltinOperator_GATHER] = &TfLiteParserImpl::ParseGather;
773  m_ParserFunctions[tflite::BuiltinOperator_GATHER_ND] = &TfLiteParserImpl::ParseGatherNd;
774  m_ParserFunctions[tflite::BuiltinOperator_GREATER] = &TfLiteParserImpl::ParseGreater;
775  m_ParserFunctions[tflite::BuiltinOperator_GREATER_EQUAL] = &TfLiteParserImpl::ParseGreaterOrEqual;
776  m_ParserFunctions[tflite::BuiltinOperator_HARD_SWISH] = &TfLiteParserImpl::ParseHardSwish;
777  m_ParserFunctions[tflite::BuiltinOperator_LEAKY_RELU] = &TfLiteParserImpl::ParseLeakyRelu;
778  m_ParserFunctions[tflite::BuiltinOperator_LESS] = &TfLiteParserImpl::ParseLess;
779  m_ParserFunctions[tflite::BuiltinOperator_LESS_EQUAL] = &TfLiteParserImpl::ParseLessOrEqual;
780  m_ParserFunctions[tflite::BuiltinOperator_LOCAL_RESPONSE_NORMALIZATION]
781  = &TfLiteParserImpl::ParseLocalResponseNormalization;
782  m_ParserFunctions[tflite::BuiltinOperator_LOG] = &TfLiteParserImpl::ParseLog;
783  m_ParserFunctions[tflite::BuiltinOperator_LOGICAL_NOT] = &TfLiteParserImpl::ParseLogicalNot;
784  m_ParserFunctions[tflite::BuiltinOperator_LOGISTIC] = &TfLiteParserImpl::ParseLogistic;
785  m_ParserFunctions[tflite::BuiltinOperator_LOG_SOFTMAX] = &TfLiteParserImpl::ParseLogSoftmax;
786  m_ParserFunctions[tflite::BuiltinOperator_L2_NORMALIZATION] = &TfLiteParserImpl::ParseL2Normalization;
787  m_ParserFunctions[tflite::BuiltinOperator_MAX_POOL_2D] = &TfLiteParserImpl::ParseMaxPool2D;
788  m_ParserFunctions[tflite::BuiltinOperator_MAXIMUM] = &TfLiteParserImpl::ParseMaximum;
789  m_ParserFunctions[tflite::BuiltinOperator_MEAN] = &TfLiteParserImpl::ParseMean;
790  m_ParserFunctions[tflite::BuiltinOperator_MINIMUM] = &TfLiteParserImpl::ParseMinimum;
791  m_ParserFunctions[tflite::BuiltinOperator_MIRROR_PAD] = &TfLiteParserImpl::ParseMirrorPad;
792  m_ParserFunctions[tflite::BuiltinOperator_MUL] = &TfLiteParserImpl::ParseMul;
793  m_ParserFunctions[tflite::BuiltinOperator_NEG] = &TfLiteParserImpl::ParseNeg;
794  m_ParserFunctions[tflite::BuiltinOperator_NOT_EQUAL] = &TfLiteParserImpl::ParseNotEqual;
795  m_ParserFunctions[tflite::BuiltinOperator_PACK] = &TfLiteParserImpl::ParsePack;
796  m_ParserFunctions[tflite::BuiltinOperator_PAD] = &TfLiteParserImpl::ParsePad;
797  m_ParserFunctions[tflite::BuiltinOperator_PADV2] = &TfLiteParserImpl::ParsePad;
798  m_ParserFunctions[tflite::BuiltinOperator_POW] = &TfLiteParserImpl::ParsePower;
799  m_ParserFunctions[tflite::BuiltinOperator_PRELU] = &TfLiteParserImpl::ParsePrelu;
800  m_ParserFunctions[tflite::BuiltinOperator_QUANTIZE] = &TfLiteParserImpl::ParseQuantize;
801  m_ParserFunctions[tflite::BuiltinOperator_RELU] = &TfLiteParserImpl::ParseRelu;
802  m_ParserFunctions[tflite::BuiltinOperator_RELU6] = &TfLiteParserImpl::ParseRelu6;
803  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MAX] = &TfLiteParserImpl::ParseReduceMax;
804  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_MIN] = &TfLiteParserImpl::ParseReduceMin;
805  m_ParserFunctions[tflite::BuiltinOperator_REDUCE_PROD] = &TfLiteParserImpl::ParseReduceProd;
806  m_ParserFunctions[tflite::BuiltinOperator_RESHAPE] = &TfLiteParserImpl::ParseReshape;
807  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_BILINEAR] = &TfLiteParserImpl::ParseResizeBilinear;
808  m_ParserFunctions[tflite::BuiltinOperator_RESIZE_NEAREST_NEIGHBOR] = &TfLiteParserImpl::ParseResizeNearestNeighbor;
809  m_ParserFunctions[tflite::BuiltinOperator_REVERSE_V2] = &TfLiteParserImpl::ParseReverseV2;
810  m_ParserFunctions[tflite::BuiltinOperator_RSQRT] = &TfLiteParserImpl::ParseRsqrt;
811  m_ParserFunctions[tflite::BuiltinOperator_SQRT] = &TfLiteParserImpl::ParseSqrt;
812  m_ParserFunctions[tflite::BuiltinOperator_SHAPE] = &TfLiteParserImpl::ParseShape;
813  m_ParserFunctions[tflite::BuiltinOperator_SIN] = &TfLiteParserImpl::ParseSin;
814  m_ParserFunctions[tflite::BuiltinOperator_SLICE] = &TfLiteParserImpl::ParseSlice;
815  m_ParserFunctions[tflite::BuiltinOperator_SOFTMAX] = &TfLiteParserImpl::ParseSoftmax;
816  m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_BATCH_ND] = &TfLiteParserImpl::ParseSpaceToBatchND;
817  m_ParserFunctions[tflite::BuiltinOperator_SPACE_TO_DEPTH] = &TfLiteParserImpl::ParseSpaceToDepth;
818  m_ParserFunctions[tflite::BuiltinOperator_SPLIT] = &TfLiteParserImpl::ParseSplit;
819  m_ParserFunctions[tflite::BuiltinOperator_SPLIT_V] = &TfLiteParserImpl::ParseSplitV;
820  m_ParserFunctions[tflite::BuiltinOperator_SQUEEZE] = &TfLiteParserImpl::ParseSqueeze;
821  m_ParserFunctions[tflite::BuiltinOperator_SQUARE] = &TfLiteParserImpl::ParseSquare;
822  m_ParserFunctions[tflite::BuiltinOperator_SQUARED_DIFFERENCE] = &TfLiteParserImpl::ParseSquaredDifference;
823  m_ParserFunctions[tflite::BuiltinOperator_STRIDED_SLICE] = &TfLiteParserImpl::ParseStridedSlice;
824  m_ParserFunctions[tflite::BuiltinOperator_SUB] = &TfLiteParserImpl::ParseSub;
825  m_ParserFunctions[tflite::BuiltinOperator_SUM] = &TfLiteParserImpl::ParseSum;
826  m_ParserFunctions[tflite::BuiltinOperator_TANH] = &TfLiteParserImpl::ParseTanH;
827  m_ParserFunctions[tflite::BuiltinOperator_TILE] = &TfLiteParserImpl::ParseTile;
828  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE] = &TfLiteParserImpl::ParseTranspose;
829  m_ParserFunctions[tflite::BuiltinOperator_TRANSPOSE_CONV] = &TfLiteParserImpl::ParseTransposeConv;
830  m_ParserFunctions[tflite::BuiltinOperator_UNIDIRECTIONAL_SEQUENCE_LSTM]
831  = &TfLiteParserImpl::ParseUnidirectionalSequenceLSTM;
832  m_ParserFunctions[tflite::BuiltinOperator_UNPACK] = &TfLiteParserImpl::ParseUnpack;
833 
834  // register supported custom operators
835  m_CustomParserFunctions["TFLite_Detection_PostProcess"] = &TfLiteParserImpl::ParseDetectionPostProcess;
836 }
837 
838 armnn::TensorInfo TfLiteParserImpl::InputTensorInfo(size_t subgraphIndex,
839  size_t operatorIndex,
840  int input)
841 {
842  const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
843  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
844 
845  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[input]);
846  auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
847 
848  if (search != m_TensorInfos.end())
849  {
850  return m_TensorInfos[inputId];
851  }
852  else
853  {
854  auto tensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
855  m_TensorInfos.insert({ inputId, tensorInfo });
856  return tensorInfo;
857  }
858 }
859 
860 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromInputs(size_t subgraphIndex,
861  size_t operatorIndex,
863  int output,
864  std::vector<int> inputs)
865 {
866  const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
867  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
868 
869  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
870 
871  auto outputSearch = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(outputId);
872 
873  if (outputSearch != m_TensorInfos.end())
874  {
875  return m_TensorInfos[outputId];
876  }
877 
878  const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
879  TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
880 
881  if (IsDynamic(outputTensorPtr))
882  {
883  if (inputs.empty())
884  {
885  for (unsigned int i = 0; i < layer->GetNumInputSlots(); ++i)
886  {
887  inputs.emplace_back(i);
888  }
889  }
890  auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
891  std::vector<armnn::TensorShape> inputShapes;
892 
893  for (unsigned int i = 0; i < inputs.size(); ++i)
894  {
895  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[inputs[i]]);
896  auto search = armnnTfLiteParser::TfLiteParserImpl::m_TensorInfos.find(inputId);
897 
898  if (search != m_TensorInfos.end())
899  {
900  auto &inputTensorInfo = m_TensorInfos[inputId];
901  inputShapes.push_back(inputTensorInfo.GetShape());
902  }
903  else
904  {
905  auto inputTensorInfo = ::armnnTfLiteParser::ToTensorInfo(subgraphPtr->tensors[inputId].get());
906  m_TensorInfos.insert({ inputId, inputTensorInfo});
907  inputShapes.push_back(inputTensorInfo.GetShape());
908  }
909  }
910  const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
911  tensor.SetShape(outputShape);
912  }
913  m_TensorInfos.insert({ outputId, tensor});
914  return tensor;
915 }
916 
917 armnn::TensorInfo TfLiteParserImpl::OutputTensorInfoFromShapes(size_t subgraphIndex,
918  size_t operatorIndex,
920  int output,
921  std::vector<armnn::TensorShape> inputShapes)
922 {
923  const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
924  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
925 
926  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[output]);
927  const auto& outputTensorPtr = subgraphPtr->tensors[outputId].get();
928  TensorInfo tensor = ::armnnTfLiteParser::ToTensorInfo(outputTensorPtr, true);
929 
930  if (IsDynamic(outputTensorPtr))
931  {
932  const auto outputShape = layer->InferOutputShapes(inputShapes)[output];
933  tensor.SetShape(outputShape);
934  }
935  m_TensorInfos.insert({ outputId, tensor});
936  return tensor;
937 }
938 
939 void TfLiteParserImpl::ResetParser()
940 {
941  m_Network = armnn::INetworkPtr(nullptr, nullptr);
942  m_Model = nullptr;
943  m_SubgraphConnections.clear();
944  m_OverriddenOutputShapes.clear();
945  m_ConstantsToDequantize.clear();
946  m_ConstantsToBeCreated.clear();
947  m_TensorInfos.clear();
948 }
949 
951 {
952  ResetParser();
953  m_Model = LoadModelFromFile(graphFile);
954  return CreateNetworkFromModel();
955 }
956 
957 INetworkPtr TfLiteParserImpl::CreateNetworkFromBinary(const std::vector<uint8_t>& binaryContent)
958 {
959  ResetParser();
960  m_Model = LoadModelFromBinary(binaryContent.data(), binaryContent.size());
961  return CreateNetworkFromModel();
962 }
963 
964 
965 armnn::INetworkPtr TfLiteParserImpl::LoadModel(std::unique_ptr<tflite::ModelT> model)
966 {
967  ResetParser();
968  m_Model = std::move(model);
969 
970  return CreateNetworkFromModel();
971 }
972 
973 INetworkPtr TfLiteParserImpl::CreateNetworkFromModel()
974 {
975 
976  using NetworkOptions = std::vector<BackendOptions>;
977  NetworkOptions networkOptions = {};
978  if (m_Options)
979  {
980  if (m_Options.value().m_InferAndValidate)
981  {
982  BackendOptions shapeInferenceMethodOption("ShapeInferenceMethod",
983  {
984  { "InferAndValidate", true }
985  });
986 
987  networkOptions.push_back(shapeInferenceMethodOption);
988  }
989  if (m_Options.value().m_AllowExpandedDims)
990  {
991  BackendOptions shapeInferenceMethodOption("AllowExpandedDims",
992  {
993  { "AllowExpandedDims", true }
994  });
995 
996  networkOptions.push_back(shapeInferenceMethodOption);
997  }
998  }
999  m_Network = INetwork::Create(networkOptions);
1000 
1001  if (m_Model.get() == nullptr)
1002  {
1003  throw ParseException(fmt::format("Tflite Model pointer is null {}", CHECK_LOCATION().AsString()));
1004  }
1005 
1006  if (m_Model->subgraphs.size() != 1)
1007  {
1008  throw ParseException(
1009  fmt::format("Current TfLite parser only supports 1 subgraph. Current one has: {} {}",
1010  m_Model->subgraphs.size(),
1011  CHECK_LOCATION().AsString()));
1012  }
1013 
1014  size_t subgraphIndex = 0;
1015  size_t operatorIndex = 0;
1016  try
1017  {
1018  for (SubgraphPtr const& subgraph : m_Model->subgraphs)
1019  {
1020  SetupInputLayerTensorInfos(subgraphIndex);
1021  SetupConstantLayerTensorInfos(subgraphIndex);
1022 
1023  m_SubgraphConnections.emplace_back(subgraph->tensors.size());
1024  for (OperatorPtr const& op : subgraph->operators)
1025  {
1026  auto const& opCodePtr = m_Model->operator_codes[op->opcode_index];
1027 
1028 // work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
1029 #if defined(ARMNN_POST_TFLITE_2_3)
1030  auto builtinCode = std::max(opCodePtr->builtin_code,
1031  static_cast<tflite::BuiltinOperator>(opCodePtr->deprecated_builtin_code));
1032 #else
1033  auto builtinCode = opCodePtr->builtin_code;
1034 #endif
1035 
1036  if (builtinCode > tflite::BuiltinOperator_MAX)
1037  {
1038  throw ParseException(fmt::format("Operator code {} is out of range 0-{}. "
1039  "subgraph:{} operator idx:{}. {}",
1040  builtinCode, tflite::BuiltinOperator_MAX, subgraphIndex,
1041  operatorIndex, CHECK_LOCATION().AsString()));
1042  }
1043 
1044  // lookup and call the parser function
1045  auto& parserFunction = m_ParserFunctions[builtinCode];
1046  (this->*parserFunction)(subgraphIndex, operatorIndex);
1047  ++operatorIndex;
1048  }
1049 
1050  SetupInputLayers(subgraphIndex);
1051  SetupOutputLayers(subgraphIndex);
1052  SetupConstantLayers(subgraphIndex);
1053 
1054  ++subgraphIndex;
1055  operatorIndex = 0;
1056  }
1057  }
1058  catch (const ParseException& e)
1059  {
1060  std::stringstream errorString;
1061  errorString << "Failed to parse operator #" << operatorIndex << " within subgraph #"
1062  << subgraphIndex << " error: " << e.what();
1063  ARMNN_LOG(error) << errorString.str();
1064  std::stringstream errors;
1065  errors << errorString.str() << "\n";
1066  throw ParseException(errors.str());
1067  }
1068 
1069  // establish the connections from the layer outputs to the inputs of the subsequent layers
1070  for (subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
1071  {
1072  for (size_t tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
1073  {
1074  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot != nullptr)
1075  {
1076  for (size_t inputSlotIdx = 0;
1077  inputSlotIdx < m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size();
1078  ++inputSlotIdx)
1079  {
1080  m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot->Connect(
1081  *(m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots[inputSlotIdx]));
1082  }
1083  }
1084  }
1085  }
1086  return std::move(m_Network);
1087 }
1088 
1089 bool TfLiteParserImpl::ShouldConstantTensorBeConverted(TfLiteParserImpl::TensorRawPtr tensorPtr,
1090  armnn::DataType inputDataType,
1091  armnn::DataType tensorDataType)
1092 {
1093  return (TfLiteParserImpl::IsConstTensor(tensorPtr) && inputDataType == DataType::Float32 &&
1094  (tensorDataType == DataType::QAsymmU8 ||
1095  tensorDataType == DataType::QAsymmS8 ||
1096  tensorDataType == DataType::QSymmS8 ||
1097  tensorDataType == DataType::Signed32 ||
1098  tensorDataType == DataType::Signed64));
1099 }
1100 
1101 void TfLiteParserImpl::RegisterProducerOfTensor(size_t subgraphIndex,
1102  size_t tensorIndex,
1103  armnn::IOutputSlot* slot)
1104 {
1105  CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
1106 
1107  TensorSlots & tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1108 
1109  if (slot->GetOwningIConnectableLayer().GetType() != LayerType::Constant)
1110  {
1111 
1112  // assuming there is only one producer for that tensor
1113  if (tensorSlots.outputSlot != nullptr)
1114  {
1115  throw ParseException(fmt::format("Another layer has already registered itself as the producer of "
1116  "subgraph:{} tensor:{} {}",
1117  subgraphIndex,
1118  tensorIndex,
1119  CHECK_LOCATION().AsString()));
1120  }
1121  }
1122  tensorSlots.outputSlot = slot;
1123 }
1124 
1125 void TfLiteParserImpl::RegisterConsumerOfTensor(size_t subgraphIndex,
1126  size_t tensorIndex,
1127  armnn::IInputSlot* slot)
1128 {
1129  CHECK_TENSOR(m_Model, subgraphIndex, tensorIndex);
1130 
1131  TensorSlots& tensorSlots = m_SubgraphConnections[subgraphIndex][tensorIndex];
1132  tensorSlots.inputSlots.push_back(slot);
1133 }
1134 
1135 void TfLiteParserImpl::ParseCustomOperator(size_t subgraphIndex, size_t operatorIndex)
1136 {
1137  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1138 
1139  // NOTE: By default we presume the custom operator is not supported
1140  auto customParserFunction = &TfLiteParserImpl::ParseUnsupportedOperator;
1141 
1142  // Identify custom code defined for custom operator
1143  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1144  const auto& customCode = m_Model->operator_codes[operatorPtr->opcode_index]->custom_code;
1145 
1146  // Find parser function that corresponds to custom code (if any)
1147  auto iterator = m_CustomParserFunctions.find(customCode);
1148  if (iterator != m_CustomParserFunctions.end())
1149  {
1150  customParserFunction = iterator->second;
1151  }
1152 
1153  // Run parser function
1154  (this->*customParserFunction)(subgraphIndex, operatorIndex);
1155 }
1156 
1157 void TfLiteParserImpl::ParseUnsupportedOperator(size_t subgraphIndex, size_t operatorIndex)
1158 {
1159  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1160 
1161  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1162 
1163  auto opcodeIndex = operatorPtr->opcode_index;
1164 
1165 // work around the introduction of the deprecated_builtin_code introduced in 2.4 in a backwards compatible manner
1166 #if defined(ARMNN_POST_TFLITE_2_3)
1167  auto opcode = std::max(m_Model->operator_codes[opcodeIndex]->builtin_code,
1168  static_cast<tflite::BuiltinOperator>(m_Model->operator_codes[opcodeIndex]->deprecated_builtin_code));
1169 #else
1170  auto opcode = m_Model->operator_codes[opcodeIndex]->builtin_code;
1171 #endif
1172 
1173  if (!m_Options || !m_Options.value().m_StandInLayerForUnsupported)
1174  {
1175  // Do not add StandInLayer, throw ParseException instead
1176  throw ParseException(
1177  fmt::format("Operator not supported. "
1178  "subgraph:{} operator:{} "
1179  "opcode_index:{} opcode:{} / {} {}",
1180  subgraphIndex,
1181  operatorIndex,
1182  opcodeIndex,
1183  opcode,
1184  tflite::EnumNameBuiltinOperator(opcode),
1185  CHECK_LOCATION().AsString()));
1186  }
1187 
1188  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1189  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1190 
1191  const unsigned int numInputs = armnn::numeric_cast<unsigned int>(inputs.size());
1192  const unsigned int numOutputs = armnn::numeric_cast<unsigned int>(outputs.size());
1193 
1194  StandInDescriptor descriptor(numInputs, numOutputs);
1195  auto layerName = fmt::format("StandIn:{}:{}:{}", subgraphIndex, operatorIndex, opcode);
1196 
1197  // Add a non-executable StandInLayer as a placeholder for any unsupported operator
1198  IConnectableLayer* layer = m_Network->AddStandInLayer(descriptor, layerName.c_str());
1199 
1200  if (!layer)
1201  {
1202  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1203  operatorIndex, CHECK_LOCATION().AsString()));
1204  }
1205 
1206  for (unsigned int i = 0u; i < numOutputs; ++i)
1207  {
1208  layer->GetOutputSlot(i).SetTensorInfo(ToTensorInfo(outputs[0], true));
1209  }
1210 
1211  auto inputTensorIds = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1212  auto outputTensorIds = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1213 
1214  RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIds);
1215  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIds);
1216 }
1217 
1218 void TfLiteParserImpl::ParseCast(size_t subgraphIndex, size_t operatorIndex)
1219 {
1220  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1221 
1222  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1223  CHECK_VALID_SIZE(inputs.size(), 1);
1224  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1225  CHECK_VALID_SIZE(outputs.size(), 1);
1226 
1227  auto layerName = fmt::format("Cast:{}:{}", subgraphIndex, operatorIndex);
1228 
1229  IConnectableLayer* layer = m_Network->AddCastLayer(layerName.c_str());
1230 
1231  if (!layer)
1232  {
1233  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1234  operatorIndex, CHECK_LOCATION().AsString()));
1235  }
1236 
1237  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1238  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1239 
1240  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1241  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1242 
1243  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1244  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1245 }
1246 
1247 void TfLiteParserImpl::ParseConv2D(size_t subgraphIndex, size_t operatorIndex)
1248 {
1249  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1250 
1251  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1252  const auto* options = operatorPtr->builtin_options.AsConv2DOptions();
1253 
1254  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1255 
1256  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1257  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1258  CHECK_VALID_SIZE(outputs.size(), 1);
1259 
1261  inputs.size() == 3 ?
1262  desc.m_BiasEnabled = true : desc.m_BiasEnabled = false;
1263  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1264  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1266  desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1267  desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1268 
1269  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1270  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1271 
1272  // assuming input is NHWC
1273  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1274  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1275 
1276  // assuming the filter is OHWI : Output, H, W, Input
1277  // which is essentially the same as NHWC
1278  unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1279  unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1280 
1281  CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1282  desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1283  CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1284  desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1285 
1286  // Add the first input and weights tensor to the registration list.
1287  // The constant weights will be added by SetupConstantLayers.
1288  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1289  std::vector<unsigned int> tensorIndexesToRegister = { inputTensorIndexes[0], inputTensorIndexes[1] };
1290 
1291  auto layerName = fmt::format("Conv2D:{}:{}", subgraphIndex, operatorIndex);
1292  armnn::IConnectableLayer* layer = m_Network->AddConvolution2dLayer(desc, layerName.c_str());
1293 
1294  if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
1295  {
1296  m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
1297  }
1298 
1299  if (desc.m_BiasEnabled)
1300  {
1301  armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1302 
1303  // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1304  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1305 
1306  if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
1307  {
1308  m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
1309  }
1310  }
1311 
1312  if (!layer)
1313  {
1314  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1315  operatorIndex, CHECK_LOCATION().AsString()));
1316  }
1317 
1318  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1319  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1320 
1321  // register the input connection slots for the layer, connections are made after all layers have been created
1322  // only the tensors for the inputs are relevant, exclude the const tensors
1323  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1324 
1325  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1326  // register the output connection slots for the layer, connections are made after all layers have been created
1327  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1328  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, { outputTensorIndexes[0] });
1329 }
1330 
1331 // Conv3D support was added in TF 2.5, so for backwards compatibility a hash define is needed.
1332 #if defined(ARMNN_POST_TFLITE_2_4)
1333 void TfLiteParserImpl::ParseConv3D(size_t subgraphIndex, size_t operatorIndex)
1334 {
1335  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1336 
1337  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1338  const auto* options = operatorPtr->builtin_options.AsConv3DOptions();
1339 
1340  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1341 
1343  desc.m_BiasEnabled = false;
1345  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1346  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1347  desc.m_StrideZ = CHECKED_NON_NEGATIVE(options->stride_d);
1348  desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1349  desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1350  desc.m_DilationZ = CHECKED_NON_NEGATIVE(options->dilation_d_factor);
1351 
1352  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1353  CHECK_VALID_SIZE(inputs.size(), 2, 3);
1354 
1355  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1356  CHECK_VALID_SIZE(outputs.size(), 1);
1357 
1358  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1359  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1360 
1361  // Assuming input is NDHWC
1362  unsigned int inputDepth = inputTensorInfo.GetShape()[1];
1363  unsigned int inputHeight = inputTensorInfo.GetShape()[2];
1364  unsigned int inputWidth = inputTensorInfo.GetShape()[3];
1365 
1366  // Assuming the filter is DHWIO : Depth, Height, Width, OutputChannels, InputChannels
1367  unsigned int filterDepth = filterTensorInfo.GetShape()[0];
1368  unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1369  unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1370 
1371  CalcPadding(inputDepth, filterDepth, desc.m_StrideZ,
1372  desc.m_DilationZ, desc.m_PadFront, desc.m_PadBack, options->padding);
1373  CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1374  desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1375  CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1376  desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1377 
1378  auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
1379 
1380  auto layerName = fmt::format("Conv3D:{}:{}", subgraphIndex, operatorIndex);
1381 
1382  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1383  // Add the first input and weights tensor to the registration list.
1384  // The constant weights will be added by SetupConstantLayers.
1385  std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1386 
1387  if (inputs.size() == 3)
1388  {
1389  desc.m_BiasEnabled = true;
1390 
1391  // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1392  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1393  }
1394 
1395  armnn::IConnectableLayer* layer = m_Network->AddConvolution3dLayer(desc, layerName.c_str());
1396 
1397  if (!layer)
1398  {
1399  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1400  operatorIndex, CHECK_LOCATION().AsString()));
1401  }
1402 
1403  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1404  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1405 
1406  // Register the input connection slots for the layer, connections are made after all layers have been created
1407  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1408 
1409  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1410  // Register the output connection slots for the layer, connections are made after all layers have been created
1411  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1412  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1413 }
1414 #endif
1415 
1416 void TfLiteParserImpl::ParseDepthwiseConv2D(size_t subgraphIndex, size_t operatorIndex)
1417 {
1418  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1419 
1420  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1421  const auto* options = operatorPtr->builtin_options.AsDepthwiseConv2DOptions();
1422 
1423  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
1424 
1426  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1427  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1429  CHECKED_NON_NEGATIVE(options->depth_multiplier);
1430 
1431  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1432  CHECK_VALID_SIZE(inputs.size(), 2, 3);
1433  if (inputs.size() == 3)
1434  {
1435  desc.m_BiasEnabled = true;
1436  }
1437 
1438  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1439  CHECK_VALID_SIZE(outputs.size(), 1);
1440  desc.m_DilationX = CHECKED_NON_NEGATIVE(options->dilation_w_factor);
1441  desc.m_DilationY = CHECKED_NON_NEGATIVE(options->dilation_h_factor);
1442 
1443  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1444  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1445 
1446  // Assuming input is NHWC
1447  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1448  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1449 
1450  // TensorflowLite weights come in the format [1, H, W, I * M]
1451  unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1452  unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1453 
1454  CalcPadding(inputHeight, filterHeight, desc.m_StrideY,
1455  desc.m_DilationY, desc.m_PadTop, desc.m_PadBottom, options->padding);
1456  CalcPadding(inputWidth, filterWidth, desc.m_StrideX,
1457  desc.m_DilationX, desc.m_PadLeft, desc.m_PadRight, options->padding);
1458 
1459  // ArmNN uses the same filter tensor layout at TfLite [1, H, W, O] no need for any permutation
1460  auto layerName = fmt::format("DepthwiseConv2D:{}:{}", subgraphIndex, operatorIndex);
1461 
1462  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1463  // Add the first input and weights tensor to the registration list.
1464  // The constant weights will be added by SetupConstantLayers.
1465  std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0], inputTensorIndexes[1]};
1466 
1467  armnn::IConnectableLayer* layer = m_Network->AddDepthwiseConvolution2dLayer(desc, layerName.c_str());
1468 
1469  if (desc.m_BiasEnabled)
1470  {
1471  desc.m_BiasEnabled = true;
1472  TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1473 
1474  // Add the biases input to the registration list, a constant layer will be added by SetupConstantLayers.
1475  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
1476  }
1477 
1478  if (!layer)
1479  {
1480  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1481  operatorIndex, CHECK_LOCATION().AsString()));
1482  }
1483 
1484  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1485  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1486 
1487  // register the input connection slots for the layer, connections are made after all layers have been created
1488  // only the tensors for the inputs are relevant, exclude the const tensors
1489  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister);
1490 
1491  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
1492  // register the output connection slots for the layer, connections are made after all layers have been created
1493  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1494  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1495 }
1496 
1497 void TfLiteParserImpl::ParseDequantize(size_t subgraphIndex, size_t operatorIndex)
1498 {
1499  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1500 
1501  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1502  CHECK_VALID_SIZE(inputs.size(), 1);
1503 
1504  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1505  CHECK_VALID_SIZE(outputs.size(), 1);
1506 
1507  auto layerName = fmt::format("Dequantize:{}:{}", subgraphIndex, operatorIndex);
1508 
1509  IConnectableLayer* layer = m_Network->AddDequantizeLayer(layerName.c_str());
1510 
1511  if (!layer)
1512  {
1513  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1514  operatorIndex, CHECK_LOCATION().AsString()));
1515  }
1516 
1517  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1518  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1519 
1520  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1521  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1522 
1523  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1524  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
1525 }
1526 
1527 void TfLiteParserImpl::ParseExpandDims(size_t subgraphIndex, size_t operatorIndex)
1528 {
1529  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1530 
1531  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1532  CHECK_VALID_SIZE(inputs.size(), 2);
1533 
1534  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1535  CHECK_VALID_SIZE(outputs.size(), 1);
1536 
1537  auto layerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
1538 
1539  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1540  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
1541  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1542 
1543  armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1544 
1545  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1546  if (axisBufferPtr == nullptr)
1547  {
1548  throw ParseException(fmt::format("{}: Operation has invalid inputs. Failed to read axis.",
1549  CHECK_LOCATION().AsString()));
1550  }
1551 
1552  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
1553  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
1554  int32_t axis = axisData[0];
1555 
1556  auto inputRank = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions());
1557  auto outputRank = inputRank + 1;
1558  if((axis < -1 * outputRank) || (outputRank <= axis))
1559  {
1560  throw ParseException(fmt::format("{}: Axis {} is not within [-{}, {}) range.",
1561  CHECK_LOCATION().AsString(), axis, outputRank, outputRank));
1562  }
1563 
1564  axis = axis < 0 ? (axis + outputRank) : axis;
1565 
1566  std::vector<unsigned int> shape(static_cast<unsigned int>(outputRank));
1567  unsigned int inputShapeIndex = 0;
1568  for (unsigned int i = 0; i < static_cast<unsigned int>(outputRank); ++i)
1569  {
1570  if (i == static_cast<unsigned int>(axis))
1571  {
1572  shape[i] = 1;
1573  }
1574  else
1575  {
1576  shape[i] = inputTensorInfo.GetShape()[inputShapeIndex];
1577  ++inputShapeIndex;
1578  }
1579  }
1580 
1581  ReshapeDescriptor reshapeDesc;
1582  reshapeDesc.m_TargetShape = TensorShape(static_cast<unsigned int>(outputRank), shape.data());
1583  outputTensorInfo.SetShape(reshapeDesc.m_TargetShape);
1584 
1585  IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
1586 
1587  if (!layer)
1588  {
1589  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1590  operatorIndex, CHECK_LOCATION().AsString()));
1591  } layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1592 
1593  auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
1594  m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
1595 
1596  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1597  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1598 
1599  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1600  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1601 }
1602 
1603 void TfLiteParserImpl::ParseTranspose(size_t subgraphIndex, size_t operatorIndex)
1604 {
1605  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1606 
1607  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1608  CHECK_VALID_SIZE(inputs.size(), 1, 2);
1609 
1610  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1611  CHECK_VALID_SIZE(outputs.size(), 1);
1612 
1613  auto layerName = fmt::format("Transpose:{}:{}", subgraphIndex, operatorIndex);
1614  TransposeDescriptor desc;
1615 
1616  if (inputs.size() == 2)
1617  {
1618  armnn::TensorInfo permuteTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1619  BufferRawPtr permuteBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1620  auto numPermVecElements = permuteTensorInfo.GetNumElements();
1621  std::vector<unsigned int> permuteShape(numPermVecElements);
1622  ::memcpy(permuteShape.data(), permuteBufferPtr->data.data(), permuteTensorInfo.GetNumBytes());
1623  PermutationVector permutationVector(permuteShape.data(), permuteTensorInfo.GetNumElements());
1624 
1625  desc = TransposeDescriptor(permutationVector);
1626  }
1627  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1628 
1629  IConnectableLayer* layer = m_Network->AddTransposeLayer(desc, layerName.c_str());
1630 
1631  if (!layer)
1632  {
1633  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1634  operatorIndex, CHECK_LOCATION().AsString()));
1635  }
1636 
1637  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1638  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1639  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1640 
1641  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1642  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1643 
1644  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1645  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1646 }
1647 
1648 void TfLiteParserImpl::ParseTransposeConv(size_t subgraphIndex, size_t operatorIndex)
1649 {
1650  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1651 
1652  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1653  const auto* options = operatorPtr->builtin_options.AsTransposeConvOptions();
1654 
1656  desc.m_BiasEnabled = false;
1657  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
1658  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
1660 
1661  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1662  if (inputs.size() == 4)
1663  {
1664  desc.m_BiasEnabled = true;
1665  }
1666  else
1667  {
1668  CHECK_VALID_SIZE(inputs.size(), 3);
1669  }
1670 
1671  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1672  CHECK_VALID_SIZE(outputs.size(), 1);
1673 
1674 
1675  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1676  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1677 
1678  // TfLite uses NHWC tensors
1679  const unsigned int inputHeight = inputTensorInfo.GetShape()[1];
1680  const unsigned int inputWidth = inputTensorInfo.GetShape()[2];
1681 
1682  const unsigned int filterHeight = filterTensorInfo.GetShape()[1];
1683  const unsigned int filterWidth = filterTensorInfo.GetShape()[2];
1684 
1685  // This block determines the output shape of the transpose convolution. If the output shape tensor ptr is not null
1686  // And the tensor is a constant, we can access the data at load time and set the output shape of the
1687  // layer. If this is not constant, We do not have access to the shape data, so we have to use
1688  // infer output shape and skip this code block.
1689  if (inputs[0] && IsConstTensor(inputs[0]))
1690  {
1691  armnn::TensorInfo tensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1692  std::vector<int> output_shape(tensorInfo.GetNumElements());
1693 
1694  if (tensorInfo.GetDataType() == DataType::Signed32)
1695  {
1696  ::memcpy(output_shape.data(), GetBuffer(m_Model, inputs[0]->buffer)->data.data(), tensorInfo.GetNumBytes());
1697  }
1698  if (tensorInfo.GetDataType() == DataType::QAsymmU8)
1699  {
1700  for(unsigned int i=0; i < tensorInfo.GetNumElements(); i++)
1701  {
1702  output_shape[i] = GetBuffer(m_Model, inputs[0]->buffer)->data.data()[i];
1703  }
1704  }
1705  // Change from signed to unsigned int to store in TransposeConvolution2dDescriptor.
1706  for (int dimension : output_shape)
1707  {
1708  desc.m_OutputShape.push_back(static_cast<unsigned int>(dimension));
1709  }
1710  desc.m_OutputShapeEnabled = true;
1711 
1712  // TfLite uses NHWC tensors
1713  const unsigned int outputHeight = desc.m_OutputShape[1];
1714  const unsigned int outputWidth = desc.m_OutputShape[2];
1715 
1716  CalcPadding(inputHeight,
1717  filterHeight,
1718  desc.m_StrideY,
1719  1, // DilationY
1720  desc.m_PadTop,
1721  desc.m_PadBottom,
1722  options->padding,
1723  outputHeight);
1724 
1725  CalcPadding(inputWidth,
1726  filterWidth,
1727  desc.m_StrideX,
1728  1, // DilationX
1729  desc.m_PadLeft,
1730  desc.m_PadRight,
1731  options->padding,
1732  outputWidth);
1733  }
1734  else
1735  {
1736  CalcPadding(inputHeight,
1737  filterHeight,
1738  desc.m_StrideY,
1739  1, // DilationY
1740  desc.m_PadTop,
1741  desc.m_PadBottom,
1742  options->padding);
1743 
1744  CalcPadding(inputWidth,
1745  filterWidth,
1746  desc.m_StrideX,
1747  1, // DilationX
1748  desc.m_PadLeft,
1749  desc.m_PadRight,
1750  options->padding);
1751  }
1752 
1753  auto filterTensorAndData = CreateConstTensorNonPermuted(inputs[1], filterTensorInfo, inputTensorInfo.GetDataType());
1754 
1755  armnn::IConnectableLayer* layer = nullptr;
1756  auto layerName = fmt::format("TransposeConv:{}:{}", subgraphIndex, operatorIndex);
1757 
1758  if (desc.m_BiasEnabled)
1759  {
1760  auto biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
1761  auto biasConstTensor = CreateConstTensorNonPermuted(inputs[3], biasTensorInfo, inputTensorInfo.GetDataType());
1762  layer = m_Network->AddTransposeConvolution2dLayer(desc,
1763  filterTensorAndData.first,
1764  biasConstTensor.first,
1765  layerName.c_str());
1766  }
1767  else
1768  {
1769  layer = m_Network->AddTransposeConvolution2dLayer(desc,
1770  filterTensorAndData.first,
1771  EmptyOptional(),
1772  layerName.c_str());
1773  }
1774 
1775  if (!layer)
1776  {
1777  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1778  operatorIndex, CHECK_LOCATION().AsString()));
1779  }
1780 
1781  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0 , { 2, 1 });
1782  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1783 
1784  // only the tensors for the inputs are relevant, exclude the const (filter) tensor
1785  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1786  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[2]});
1787 
1788  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1789  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1790 }
1791 
1792 void TfLiteParserImpl::ParseAveragePool2D(size_t subgraphIndex, size_t operatorIndex)
1793 {
1794  ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Average);
1795 }
1796 
1797 void TfLiteParserImpl::ParseBatchMatMul(size_t subgraphIndex, size_t operatorIndex)
1798 {
1799  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1800 
1801  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1802  CHECK_VALID_SIZE(inputs.size(), 2);
1803 
1804  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1805  CHECK_VALID_SIZE(outputs.size(), 1);
1806 
1807  auto layerName = fmt::format("BatchMatMul:{}:{}", subgraphIndex, operatorIndex);
1808 
1809  TensorInfo inputXTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1810  TensorInfo inputYTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1811 
1812  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
1813  const auto* options = operatorPtr->builtin_options.AsBatchMatMulOptions();
1814 
1815  // Adjoint in tensorflow lite performs transpose operation
1816  BatchMatMulDescriptor descriptor(options->adj_x,
1817  options->adj_y,
1818  false,
1819  false);
1820  // Arbitrary DataLayout
1821 
1822  IConnectableLayer* layer = m_Network->AddBatchMatMulLayer(descriptor, layerName.c_str());
1823 
1824  if (!layer)
1825  {
1826  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1827  operatorIndex, CHECK_LOCATION().AsString()));
1828  }
1829 
1830  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1831  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1832 
1833  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1834  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1835 
1836  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1837  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1838 }
1839 
1840 void TfLiteParserImpl::ParseBatchToSpaceND(size_t subgraphIndex, size_t operatorIndex)
1841 {
1842  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1843 
1844  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1845  CHECK_VALID_SIZE(inputs.size(), 3);
1846 
1847  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1848  CHECK_VALID_SIZE(outputs.size(), 1);
1849 
1850  armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1851  BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
1852 
1853  armnn::TensorInfo cropsTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
1854  BufferRawPtr cropsBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
1855 
1856  std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
1857  ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
1858 
1859  std::vector<unsigned int> cropsVector(cropsTensorInfo.GetNumElements());
1860  ::memcpy(cropsVector.data(), cropsBufferPtr->data.data(), cropsTensorInfo.GetNumBytes());
1861 
1862  size_t step = 2;
1863  std::vector<std::pair<unsigned int, unsigned int>> crops;
1864  for (unsigned int i = 0; i < cropsTensorInfo.GetNumElements() / step; ++i)
1865  {
1866  crops.emplace_back(cropsVector[i * step], cropsVector[i * step + 1]);
1867  }
1868 
1870  desc.m_BlockShape = blockShape;
1871  desc.m_Crops = crops;
1873 
1874  auto layerName = fmt::format("BatchToSpaceND:{}:{}", subgraphIndex, operatorIndex);
1875 
1876  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1877 
1878  IConnectableLayer* layer = m_Network->AddBatchToSpaceNdLayer(desc, layerName.c_str());
1879 
1880  if (!layer)
1881  {
1882  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1883  operatorIndex, CHECK_LOCATION().AsString()));
1884  }
1885 
1886  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
1887  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1888  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1889 
1890  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1891  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
1892 
1893  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1894  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1895 }
1896 
1897 void TfLiteParserImpl::ParseL2Normalization(size_t subgraphIndex, size_t operatorIndex)
1898 {
1899  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1900 
1901  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1902  CHECK_VALID_SIZE(inputs.size(), 1);
1903 
1904  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1905  CHECK_VALID_SIZE(outputs.size(), 1);
1906 
1909  auto layerName = fmt::format("L2Normalization:{}:{}", subgraphIndex, operatorIndex);
1910  IConnectableLayer* layer = m_Network->AddL2NormalizationLayer(desc, layerName.c_str());
1911 
1912  if (!layer)
1913  {
1914  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1915  operatorIndex, CHECK_LOCATION().AsString()));
1916  }
1917 
1918  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
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[0]});
1926 }
1927 
1928 void TfLiteParserImpl::ParseMaxPool2D(size_t subgraphIndex, size_t operatorIndex)
1929 {
1930  ParsePool(subgraphIndex, operatorIndex, PoolingAlgorithm::Max);
1931 }
1932 
1933 void TfLiteParserImpl::ParseMaximum(size_t subgraphIndex, size_t operatorIndex)
1934 {
1935  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1936 
1937  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1938  CHECK_VALID_SIZE(inputs.size(), 2);
1939 
1940  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1941  CHECK_VALID_SIZE(outputs.size(), 1);
1942 
1943  auto layerName = fmt::format("Maximum:{}:{}", subgraphIndex, operatorIndex);
1944 
1945  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1946  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1947  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
1948 
1949  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Maximum, layerName.c_str());
1950 
1951  if (!layer)
1952  {
1953  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1954  operatorIndex, CHECK_LOCATION().AsString()));
1955  }
1956 
1957  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1958  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1959  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1960 
1961  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1962  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1963 
1964  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
1965  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
1966 }
1967 
1968 void TfLiteParserImpl::ParseMinimum(size_t subgraphIndex, size_t operatorIndex)
1969 {
1970  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
1971 
1972  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
1973  CHECK_VALID_SIZE(inputs.size(), 2);
1974 
1975  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
1976  CHECK_VALID_SIZE(outputs.size(), 1);
1977 
1978  auto layerName = fmt::format("Minimum:{}:{}", subgraphIndex, operatorIndex);
1979 
1980  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
1981  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
1982  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
1983 
1984  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Minimum, layerName.c_str());
1985 
1986  if (!layer)
1987  {
1988  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
1989  operatorIndex, CHECK_LOCATION().AsString()));
1990  }
1991 
1992  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
1993  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
1994  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
1995 
1996  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
1997  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
1998 
1999  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2000  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2001 }
2002 
2003 void TfLiteParserImpl::ParsePool(size_t subgraphIndex,
2004  size_t operatorIndex,
2005  PoolingAlgorithm algorithm)
2006 {
2007  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2008 
2009  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2010  const auto* options = operatorPtr->builtin_options.AsPool2DOptions();
2011 
2012  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
2013 
2014  std::string layerName;
2015 
2016  switch (algorithm)
2017  {
2018  case PoolingAlgorithm::Average:
2019  layerName =
2020  fmt::format("AveragePool2D:{}:{}", subgraphIndex, operatorIndex);
2021  break;
2022  case PoolingAlgorithm::Max:
2023  layerName =
2024  fmt::format("MaxPool2D:{}:{}", subgraphIndex, operatorIndex);
2025  break;
2026  default:
2027  throw ParseException(fmt::format("Unsupported Pooling Algorithm {}", CHECK_LOCATION().AsString()));
2028  }
2029 
2030  Pooling2dDescriptor desc;
2031 
2032  desc.m_PoolType = algorithm;
2033  desc.m_StrideX = CHECKED_NON_NEGATIVE(options->stride_w);
2034  desc.m_StrideY = CHECKED_NON_NEGATIVE(options->stride_h);
2035  desc.m_PoolWidth = CHECKED_NON_NEGATIVE(options->filter_width);
2036  desc.m_PoolHeight = CHECKED_NON_NEGATIVE(options->filter_height);
2037  desc.m_PaddingMethod = PaddingMethod::Exclude;
2038  desc.m_OutputShapeRounding = OutputShapeRounding::Floor;
2040 
2041  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2042  CHECK_VALID_SIZE(inputs.size(), 1);
2043  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2044 
2045  // assuming input is NHWC
2046  unsigned int inputHeight = inputTensorInfo.GetShape()[1];
2047  unsigned int inputWidth = inputTensorInfo.GetShape()[2];
2048 
2049  CalcPadding(inputHeight, desc.m_PoolHeight, desc.m_StrideY, 1u,
2050  desc.m_PadTop, desc.m_PadBottom, options->padding);
2051  CalcPadding(inputWidth, desc.m_PoolWidth, desc.m_StrideX, 1u,
2052  desc.m_PadLeft, desc.m_PadRight, options->padding);
2053 
2054  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2055  CHECK_VALID_SIZE(outputs.size(), 1);
2056 
2057  IConnectableLayer* layer = m_Network->AddPooling2dLayer(desc, layerName.c_str());
2058 
2059  if (!layer)
2060  {
2061  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2062  operatorIndex, CHECK_LOCATION().AsString()));
2063  }
2064 
2065  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2066  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2067  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2068 
2069  // register the input connection slots for the layer, connections are made after all layers have been created
2070  // only the tensors for the inputs are relevant, exclude the const tensors
2071  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2072  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2073 
2074  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2075  // register the output connection slots for the layer, connections are made after all layers have been created
2076  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2077  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2078 }
2079 
2080 void TfLiteParserImpl::ParseSlice(size_t subgraphIndex, size_t operatorIndex)
2081 {
2082  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2083 
2084  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2085  CHECK_VALID_SIZE(inputs.size(), 3);
2086  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2087  CHECK_VALID_SIZE(outputs.size(), 1);
2088 
2089  SliceDescriptor desc;
2090 
2091  // set begin tensor info for slice descriptor
2092  armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2093  BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2094 
2095  std::vector<unsigned int> begin(beginTensorInfo.GetNumElements());
2096  ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2097 
2098  // set size tensor info for slice descriptor
2099  armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2100  BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2101 
2102  std::vector<int> signedSize(sizeTensorInfo.GetNumElements(), 1);
2103 
2104  // if size buffer data is not specified, all contents of size vector remain as values of 1
2105  if (sizeBufferPtr->data.data())
2106  {
2107  ::memcpy(signedSize.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
2108  }
2109 
2110  std::vector<unsigned int> size(sizeTensorInfo.GetNumElements());
2111  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2112 
2113  for (unsigned int i = 0; i < signedSize.size(); ++i)
2114  {
2115  int signedValue = signedSize[i];
2116 
2117  if (signedValue < -1 || signedValue > static_cast<int>(inputTensorInfo.GetShape()[i] - begin[i]))
2118  {
2119  throw ParseException(fmt::format("Invalid value for size {} size must be in range "
2120  "[-1, inputDimSize - begin] [-1, {}] inclusive {}",
2121  signedValue,
2122  inputTensorInfo.GetShape()[i] - begin[i],
2123  CHECK_LOCATION().AsString()));
2124  }
2125 
2126  if (signedValue == -1)
2127  {
2128  size[i] = inputTensorInfo.GetShape()[i] - begin[i];
2129  }
2130  else
2131  {
2132  size[i] = static_cast<unsigned int>(signedValue);
2133  }
2134  }
2135 
2136  desc = SliceDescriptor(begin, size);
2137 
2138  auto layerName = fmt::format("Slice:{}:{}", subgraphIndex, operatorIndex);
2139 
2140  IConnectableLayer* const layer = m_Network->AddSliceLayer(desc, layerName.c_str());
2141 
2142  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2143  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2144  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2145 
2146  // register the input connection slots for the layer, connections are made after all layers have been created
2147  // only the tensors for the inputs are relevant, exclude the const tensors
2148  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2149  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2150 
2151  // register the output connection slots for the layer, connections are made after all layers have been created
2152  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2153  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2154 }
2155 
2156 void TfLiteParserImpl::ParseSoftmax(size_t subgraphIndex, size_t operatorIndex)
2157 {
2158  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2159  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2160  const auto* options = operatorPtr->builtin_options.AsSoftmaxOptions();
2161 
2162  SoftmaxDescriptor desc;
2163  desc.m_Beta = options->beta;
2164 
2165  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2166  CHECK_VALID_SIZE(inputs.size(), 1);
2167  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2168  CHECK_VALID_SIZE(outputs.size(), 1);
2169 
2170  auto layerName = fmt::format("Softmax:{}:{}", subgraphIndex, operatorIndex);
2171  IConnectableLayer* const layer = m_Network->AddSoftmaxLayer(desc, layerName.c_str());
2172 
2173  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2174  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2175 
2176  // register the input connection slots for the layer, connections are made after all layers have been created
2177  // only the tensors for the inputs are relevant, exclude the const tensors
2178  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2179  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2180 
2181  // register the output connection slots for the layer, connections are made after all layers have been created
2182  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2183  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2184 }
2185 
2186 void TfLiteParserImpl::ParseLogSoftmax(size_t subgraphIndex, size_t operatorIndex)
2187 {
2188  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2189 
2190  LogSoftmaxDescriptor desc;
2191 
2192  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2193  CHECK_VALID_SIZE(inputs.size(), 1);
2194  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2195  CHECK_VALID_SIZE(outputs.size(), 1);
2196 
2197  auto layerName = fmt::format("LogSoftmax:{}:{}", subgraphIndex, operatorIndex);
2198  IConnectableLayer* const layer = m_Network->AddLogSoftmaxLayer(desc, layerName.c_str());
2199 
2200  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2201  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2202 
2203  // register the input connection slots for the layer, connections are made after all layers have been created
2204  // only the tensors for the inputs are relevant, exclude the const tensors
2205  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2206  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2207 
2208  // register the output connection slots for the layer, connections are made after all layers have been created
2209  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2210  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2211 }
2212 
2213 void TfLiteParserImpl::ParseSpaceToBatchND(size_t subgraphIndex, size_t operatorIndex)
2214 {
2215  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2216 
2217  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2218  CHECK_VALID_SIZE(inputs.size(), 3);
2219 
2220  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2221  CHECK_VALID_SIZE(outputs.size(), 1);
2222 
2223  armnn::TensorInfo blockShapeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2224  BufferRawPtr blockShapeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2225 
2226  armnn::TensorInfo padListTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2227  BufferRawPtr padListBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2228 
2229  std::vector<unsigned int> blockShape(blockShapeTensorInfo.GetNumElements());
2230  ::memcpy(blockShape.data(), blockShapeBufferPtr->data.data(), blockShapeTensorInfo.GetNumBytes());
2231 
2232  std::vector<unsigned int> padListVector(padListTensorInfo.GetNumElements());
2233  ::memcpy(padListVector.data(), padListBufferPtr->data.data(), padListTensorInfo.GetNumBytes());
2234 
2235  size_t step = 2;
2236  std::vector<std::pair<unsigned int, unsigned int>> padList;
2237  for (unsigned int i = 0; i < padListTensorInfo.GetNumElements() / step; ++i)
2238  {
2239  padList.emplace_back(padListVector[i * step], padListVector[i * step + 1]);
2240  }
2241 
2243  desc.m_BlockShape = blockShape;
2244  desc.m_PadList = padList;
2246 
2247  auto layerName = fmt::format("SpaceToBatchND:{}:{}", subgraphIndex, operatorIndex);
2248 
2249  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2250 
2251  IConnectableLayer* layer = m_Network->AddSpaceToBatchNdLayer(desc, layerName.c_str());
2252 
2253  if (!layer)
2254  {
2255  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2256  operatorIndex, CHECK_LOCATION().AsString()));
2257  }
2258 
2259  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2260  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2261  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2262 
2263  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2264  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2265 
2266  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2267  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2268 }
2269 
2270 void TfLiteParserImpl::ParseSpaceToDepth(size_t subgraphIndex, size_t operatorIndex)
2271 {
2272  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2273 
2274  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2275  CHECK_VALID_SIZE(inputs.size(), 1);
2276  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2277  CHECK_VALID_SIZE(outputs.size(), 1);
2278 
2279  armnn::SpaceToDepthDescriptor descriptor;
2280 
2281  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2282  const auto* options = operatorPtr->builtin_options.AsSpaceToDepthOptions();
2283  auto blockSize = options->block_size;
2284  if (blockSize < 2)
2285  {
2286  throw ParseException(
2287  fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
2288  blockSize,
2289  CHECK_LOCATION().AsString()));
2290  }
2291  descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
2292 
2293  auto layerName = fmt::format("SpaceToDepth:{}:{}", subgraphIndex, operatorIndex);
2294  IConnectableLayer* layer = m_Network->AddSpaceToDepthLayer(descriptor, layerName.c_str());
2295 
2296  if (!layer)
2297  {
2298  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2299  operatorIndex, CHECK_LOCATION().AsString()));
2300  }
2301 
2302  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2303  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2304 
2305  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2306  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2307 
2308  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2309  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2310 }
2311 
2313  const armnn::TensorInfo& inputTensorInfo)
2314 {
2315  CHECK_VALID_SIZE(squeezeDims.size(), 0, 1, 2, 3, 4);
2316  static const uint32_t dimensionSequence[] = { 0, 1, 2, 3 };
2317 
2318  if (inputTensorInfo.GetNumDimensions() > 4)
2319  {
2320  std::stringstream ss;
2321  ss << "Input tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2322  << " shape:" << inputTensorInfo.GetShape() << " "
2323  << CHECK_LOCATION().AsString();
2324  throw ParseException(ss.str());
2325  }
2326 
2327  if (squeezeDims.empty())
2328  {
2329  squeezeDims.assign(dimensionSequence,
2330  dimensionSequence+inputTensorInfo.GetNumDimensions());
2331  }
2332 
2333  std::vector<uint32_t> outputDims;
2334  for(unsigned int i = 0; i < inputTensorInfo.GetNumDimensions(); i++)
2335  {
2336  bool skipSqueeze = (std::find(squeezeDims.begin(), squeezeDims.end(), i) == squeezeDims.end());
2337  auto currentDimension = inputTensorInfo.GetShape()[i];
2338  if (skipSqueeze || currentDimension != 1)
2339  {
2340  outputDims.push_back(currentDimension);
2341  }
2342  }
2343 
2344  if (outputDims.size() > 4)
2345  {
2346  std::stringstream ss;
2347  ss << "Output tensor has unexpected number of dimensions:" << inputTensorInfo.GetNumDimensions()
2348  << " shape:" << inputTensorInfo.GetShape() << " "
2349  << CHECK_LOCATION().AsString();
2350  throw ParseException(ss.str());
2351  }
2352 
2353  TensorShape outShape = TensorShape(static_cast<unsigned int>(outputDims.size()),
2354  outputDims.data());
2355 
2356  // we need to preserve the tensor type and the quantization data as well
2357  TensorInfo outTensorInfo = inputTensorInfo;
2358  outTensorInfo.SetShape(outShape);
2359 
2360  return outTensorInfo;
2361 }
2362 
2363 void TfLiteParserImpl::ParseShape(size_t subgraphIndex, size_t operatorIndex)
2364 {
2365  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2366 
2367  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2368  CHECK_VALID_SIZE(inputs.size(), 1);
2369  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2370  CHECK_VALID_SIZE(outputs.size(), 1);
2371 
2372  auto layerName = fmt::format("Shape:{}:{}", subgraphIndex, operatorIndex);
2373 
2374  IConnectableLayer* layer = m_Network->AddShapeLayer(layerName.c_str());
2375 
2376  if (!layer)
2377  {
2378  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2379  operatorIndex, CHECK_LOCATION().AsString()));
2380  }
2381 
2382  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2383  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2384 
2385  // Check if output tensor type is Signed32 or Signed64
2386  if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
2387  outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
2388  {
2389  throw ParseException(
2390  fmt::format(
2391  "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
2392  CHECK_LOCATION().AsString()));
2393  }
2394 
2395  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2396  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2397 
2398  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2399  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
2400 }
2401 
2402 void TfLiteParserImpl::ParseSqueeze(size_t subgraphIndex, size_t operatorIndex)
2403 {
2404  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2405 
2406  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2407  CHECK_VALID_SIZE(inputs.size(), 1);
2408 
2409  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2410  CHECK_VALID_SIZE(outputs.size(), 1);
2411 
2412  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2413  const auto * options = operatorPtr->builtin_options.AsSqueezeOptions();
2414  auto layerName = fmt::format("Squeeze:{}:{}", subgraphIndex, operatorIndex);
2415 
2416  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2417 
2418  std::vector<uint32_t> squeezeDim;
2419  // A single negative dim index is interpreted as a negative index in python
2420  // Meaning the index will be the shape size plus the negative index value
2421  if (options->squeeze_dims.size() == 1 && options->squeeze_dims[0] < 0)
2422  {
2423  int32_t dim = static_cast<int32_t>(inputTensorInfo.GetShape().GetNumDimensions()) + options->squeeze_dims[0];
2424  squeezeDim.push_back(static_cast<uint32_t>(dim));
2425  }
2426  else
2427  {
2428  squeezeDim = AsUnsignedVector(options->squeeze_dims);
2429  }
2430 
2431  armnn::TensorInfo outputTensorInfo = TfLiteParserImpl::OutputShapeOfSqueeze(squeezeDim, inputTensorInfo);
2432 
2433  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
2434 
2435  ReshapeDescriptor reshapeDesc;
2436  reshapeDesc.m_TargetShape = outputTensorInfo.GetShape();
2437 
2438  auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
2439  m_TensorInfos[outputTensorIds[0]] = outputTensorInfo;
2440 
2441  IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
2442 
2443  if (!layer)
2444  {
2445  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2446  operatorIndex, CHECK_LOCATION().AsString()));
2447  }
2448 
2449  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2450 
2451  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2452  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2453 
2454  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2455  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2456 }
2457 
2458 void TfLiteParserImpl::ParseStridedSlice(size_t subgraphIndex, size_t operatorIndex)
2459 {
2460  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2461 
2462  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2463  CHECK_VALID_SIZE(inputs.size(), 4);
2464 
2465  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2466  CHECK_VALID_SIZE(outputs.size(), 1);
2467 
2468  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2469  const auto* options = operatorPtr->builtin_options.AsStridedSliceOptions();
2470 
2472  desc.m_BeginMask = options->begin_mask;
2473  desc.m_EllipsisMask = options->ellipsis_mask;
2474  desc.m_EndMask = options->end_mask;
2475  desc.m_NewAxisMask = options->new_axis_mask;
2476  desc.m_ShrinkAxisMask = options->shrink_axis_mask;
2478 
2479  armnn::TensorInfo beginTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2480  BufferRawPtr beginBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2481 
2482  std::vector<int> begin(beginTensorInfo.GetNumElements());
2483  if (beginBufferPtr->data.data() != nullptr)
2484  {
2485  ::memcpy(begin.data(), beginBufferPtr->data.data(), beginTensorInfo.GetNumBytes());
2486  }
2487  else
2488  {
2489  throw ParseException("ParseStridedSlice: Invalid input - the begin vector is null");
2490  }
2491 
2492  armnn::TensorInfo endTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2493  BufferRawPtr endBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2494 
2495  std::vector<int> end(endTensorInfo.GetNumElements());
2496  if (endBufferPtr->data.data() != nullptr)
2497  {
2498  ::memcpy(end.data(), endBufferPtr->data.data(), endTensorInfo.GetNumBytes());
2499  }
2500  else
2501  {
2502  throw ParseException("ParseStridedSlice: Invalid input - the end vector is null");
2503  }
2504 
2505  armnn::TensorInfo strideTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 3);
2506  BufferRawPtr strideBufferPtr = GetBuffer(m_Model, inputs[3]->buffer);
2507 
2508  std::vector<int> stride(strideTensorInfo.GetNumElements());
2509 
2510  if (strideBufferPtr->data.data() != nullptr)
2511  {
2512  ::memcpy(stride.data(), strideBufferPtr->data.data(), strideTensorInfo.GetNumBytes());
2513  }
2514  else
2515  {
2516  throw ParseException("ParseStridedSlice: Invalid input - the stride vector is null");
2517  }
2518 
2519  desc.m_Begin = begin;
2520  desc.m_End = end;
2521  desc.m_Stride = stride;
2522 
2523  auto layerName = fmt::format("StridedSlice:{}:{}", subgraphIndex, operatorIndex);
2524  IConnectableLayer* layer = m_Network->AddStridedSliceLayer(desc, layerName.c_str());
2525 
2526  if (!layer)
2527  {
2528  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2529  operatorIndex, CHECK_LOCATION().AsString()));
2530  }
2531 
2532  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2533  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2534 
2535  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2536  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2537 
2538  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2539  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2540 }
2541 
2542 void TfLiteParserImpl::ParseSub(size_t subgraphIndex, size_t operatorIndex)
2543 {
2544  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2545 
2546  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2547  const auto* options = operatorPtr->builtin_options.AsSubOptions();
2548 
2549  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2550  CHECK_VALID_SIZE(inputs.size(), 2);
2551 
2552  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2553  CHECK_VALID_SIZE(outputs.size(), 1);
2554 
2555  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2556  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2557 
2558  auto layerName = fmt::format("Sub:{}:{}", subgraphIndex, operatorIndex);
2559  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Sub, layerName.c_str());
2560 
2561  if (!layer)
2562  {
2563  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2564  operatorIndex, CHECK_LOCATION().AsString()));
2565  }
2566 
2567  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2568  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2569 
2570  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2571  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2572  if (options)
2573  {
2574  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2575  }
2576 
2577  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2578  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2579 }
2580 
2581 void TfLiteParserImpl::ParseDiv(size_t subgraphIndex, size_t operatorIndex)
2582 {
2583  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2584 
2585  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2586  const auto* options = operatorPtr->builtin_options.AsDivOptions();
2587 
2588  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2589  CHECK_VALID_SIZE(inputs.size(), 2);
2590 
2591  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2592  CHECK_VALID_SIZE(outputs.size(), 1);
2593 
2594  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2595  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2596 
2597  auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2598  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2599 
2600  if (!layer)
2601  {
2602  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2603  operatorIndex, CHECK_LOCATION().AsString()));
2604  }
2605 
2606  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2607  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2608 
2609  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2610  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2611  if (options)
2612  {
2613  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2614  }
2615 
2616  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2617  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2618 }
2619 
2620 void TfLiteParserImpl::ParseFloorDiv(size_t subgraphIndex, size_t operatorIndex)
2621 {
2622  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2623 
2624  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2625  CHECK_VALID_SIZE(inputs.size(), 2);
2626 
2627  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2628  CHECK_VALID_SIZE(outputs.size(), 1);
2629 
2630  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2631  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2632 
2633  auto layerName = fmt::format("Div:{}:{}", subgraphIndex, operatorIndex);
2634  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, layerName.c_str());
2635 
2636  if (!layer)
2637  {
2638  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2639  operatorIndex, CHECK_LOCATION().AsString()));
2640  }
2641 
2642  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2643  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2644 
2645  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2646  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2647  layer = AddFusedFloorLayer(layer, 0);
2648 
2649  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2650  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2651 }
2652 
2653 void TfLiteParserImpl::ParseAdd(size_t subgraphIndex, size_t operatorIndex)
2654 {
2655  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2656 
2657  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2658  const auto* options = operatorPtr->builtin_options.AsAddOptions();
2659 
2660  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2661  CHECK_VALID_SIZE(inputs.size(), 2);
2662 
2663  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2664  CHECK_VALID_SIZE(outputs.size(), 1);
2665 
2666  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2667  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2668 
2669  auto layerName = fmt::format("Add:{}:{}", subgraphIndex, operatorIndex);
2670  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Add, layerName.c_str());
2671 
2672  if (!layer)
2673  {
2674  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2675  operatorIndex, CHECK_LOCATION().AsString()));
2676  }
2677 
2678  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2679  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2680 
2681  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2682  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2683  if (options)
2684  {
2685  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2686  }
2687 
2688  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2689  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2690 }
2691 
2692 void TfLiteParserImpl::ParseMul(size_t subgraphIndex, size_t operatorIndex)
2693 {
2694  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2695 
2696  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2697  const auto* options = operatorPtr->builtin_options.AsMulOptions();
2698 
2699  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2700  CHECK_VALID_SIZE(inputs.size(), 2);
2701 
2702  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2703  CHECK_VALID_SIZE(outputs.size(), 1);
2704 
2705  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2706  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2707 
2708  auto layerName = fmt::format("Mul:{}:{}", subgraphIndex, operatorIndex);
2709  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
2710 
2711  if (!layer)
2712  {
2713  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2714  operatorIndex, CHECK_LOCATION().AsString()));
2715  }
2716 
2717  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
2718  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2719 
2720  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2721  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
2722  if (options)
2723  {
2724  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
2725  }
2726 
2727  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2728  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2729 }
2730 
2731 void TfLiteParserImpl::ParseMean(size_t subgraphIndex, size_t operatorIndex)
2732 {
2733  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2734 
2735  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2736 
2737  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2738  CHECK_VALID_SIZE(outputs.size(), 1);
2739 
2740  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2741  TensorInfo dimTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2742 
2743  armnn::MeanDescriptor desc;
2744  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2745  // Get const axis value from model and set it to descriptor.
2746  if (axisBufferPtr != nullptr)
2747  {
2748  std::vector<int32_t> axisData(dimTensorInfo.GetNumElements());
2749  ::memcpy(axisData.data(), axisBufferPtr->data.data(), dimTensorInfo.GetNumBytes());
2750 
2751  // Convert the axis to unsigned int and remove duplicates.
2752  auto rank = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
2753  std::set<unsigned int> uniqueAxis;
2754  std::transform(axisData.begin(),
2755  axisData.end(),
2756  std::inserter(uniqueAxis, uniqueAxis.begin()),
2757  [rank](int i)->unsigned int{
2758  return static_cast<uint32_t>(((i + rank) % rank)); });
2759  desc.m_Axis.assign(uniqueAxis.begin(), uniqueAxis.end());
2760  }
2761  else
2762  {
2763  for (uint32_t i = 0; i < inputTensorInfo.GetNumDimensions(); ++i)
2764  {
2765  desc.m_Axis.push_back(i);
2766  }
2767  }
2768 
2769  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0], true);
2770 
2771  desc.m_KeepDims = inputTensorInfo.GetNumDimensions() == outputTensorInfo.GetNumDimensions() ? true : false;
2772 
2773  auto layerName = fmt::format("Mean:{}:{}", subgraphIndex, operatorIndex);
2774  IConnectableLayer* layer = m_Network->AddMeanLayer(desc, layerName.c_str());
2775 
2776  if (!layer)
2777  {
2778  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2779  operatorIndex, CHECK_LOCATION().AsString()));
2780  }
2781 
2782  outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2783  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2784 
2785  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2786  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2787 
2788  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2789  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2790 }
2791 
2792 void TfLiteParserImpl::ParsePad(size_t subgraphIndex, size_t operatorIndex)
2793 {
2794  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2795 
2796  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2797 
2798  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2799  CHECK_VALID_SIZE(outputs.size(), 1);
2800 
2801  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2802  armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2803 
2804  std::vector<unsigned int> padBuffer = GetUIntBuffer(padTensorInfo, m_Model, inputs[1]->buffer);
2805 
2806  size_t step = 2;
2807  armnn::PadDescriptor desc;
2808  auto opcode = GetOpCode(m_Model, subgraphIndex, operatorIndex);
2809 
2810  if (opcode == tflite::BuiltinOperator_PAD)
2811  {
2812  CHECK_VALID_SIZE(inputs.size(), 2);
2813 
2814  if (inputTensorInfo.IsQuantized())
2815  {
2816  desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2817  }
2818  }
2819  else if (opcode == tflite::BuiltinOperator_PADV2)
2820  {
2821  CHECK_VALID_SIZE(inputs.size(), 3);
2822 
2823  armnn::TensorInfo padValueTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
2824 
2825  if (padValueTensorInfo.GetNumElements() != 1)
2826  {
2827  ARMNN_THROW_PARSE_EXCEPTION("Multiple padding values are not supported in PADV2");
2828  }
2829  BufferRawPtr padValueBufferPtr = GetBuffer(m_Model, inputs[2]->buffer);
2830 
2831  // Get the pad value from the input tensor
2832  if (padValueBufferPtr->data.size() > 0)
2833  {
2834  switch (padValueTensorInfo.GetDataType())
2835  {
2837  {
2838  std::vector<float> padValueBuffer(padValueTensorInfo.GetNumElements());
2839  ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2840  desc.m_PadValue = padValueBuffer[0];
2841  break;
2842  }
2844  {
2845  std::vector<uint8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2846  ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2847  desc.m_PadValue = armnn::Dequantize<uint8_t>(padValueBuffer[0],
2848  padValueTensorInfo.GetQuantizationScale(),
2849  padValueTensorInfo.GetQuantizationOffset());
2850  break;
2851  }
2854  {
2855  std::vector<int8_t> padValueBuffer(padValueTensorInfo.GetNumElements());
2856  ::memcpy(padValueBuffer.data(), padValueBufferPtr->data.data(), padValueBufferPtr->data.size());
2857  desc.m_PadValue = armnn::Dequantize<int8_t>(padValueBuffer[0],
2858  padValueTensorInfo.GetQuantizationScale(),
2859  padValueTensorInfo.GetQuantizationOffset());
2860  break;
2861  }
2862  default: ARMNN_THROW_PARSE_EXCEPTION("Unsupported DataType");
2863  }
2864  }
2865  else if (inputTensorInfo.IsQuantized())
2866  {
2867  desc.m_PadValue = static_cast<float>(inputTensorInfo.GetQuantizationOffset());
2868  }
2869  }
2870 
2871  for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2872  {
2873  desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2874  }
2875 
2876  auto layerName = (opcode == tflite::BuiltinOperator_PAD) ? fmt::format("Pad:{}:{}", subgraphIndex, operatorIndex)
2877  : fmt::format("PadV2:{}:{}", subgraphIndex, operatorIndex);
2878 
2879  IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2880 
2881  if (!layer)
2882  {
2883  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2884  operatorIndex, CHECK_LOCATION().AsString()));
2885  }
2886 
2887  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2888  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2889 
2890  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2891  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2892 
2893  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2894  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2895 }
2896 
2897 void TfLiteParserImpl::ParseMirrorPad(size_t subgraphIndex, size_t operatorIndex)
2898 {
2899  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2900 
2901  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2902  CHECK_VALID_SIZE(inputs.size(), 2);
2903 
2904  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2905  CHECK_VALID_SIZE(outputs.size(), 1);
2906 
2907  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2908 
2909  armnn::TensorInfo padTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2910  BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
2911 
2912  std::vector<unsigned int> padBuffer(padTensorInfo.GetNumElements());
2913  ::memcpy(padBuffer.data(), bufferPtr->data.data(), padTensorInfo.GetNumBytes());
2914 
2915  size_t step = 2;
2916  armnn::PadDescriptor desc;
2917  for (unsigned int i = 0; i < padTensorInfo.GetNumElements() / step; ++i)
2918  {
2919  desc.m_PadList.emplace_back(padBuffer[i * step], padBuffer[i * step + 1]);
2920  }
2921 
2922  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
2923  const auto* options = operatorPtr->builtin_options.AsMirrorPadOptions();
2924 
2925  if (options->mode == tflite::MirrorPadMode_REFLECT)
2926  {
2927  desc.m_PaddingMode = PaddingMode::Reflect;
2928  }
2929  else if (options->mode == tflite::MirrorPadMode_SYMMETRIC)
2930  {
2931  desc.m_PaddingMode = PaddingMode::Symmetric;
2932  }
2933  else
2934  {
2935  ARMNN_THROW_PARSE_EXCEPTION("PaddingMode must be either REFLECT or SYMMETRIC");
2936  }
2937 
2938  // If padding mode is Reflect then both paddings must be no greater than inputShape(i) - 1.
2939  // If padding mode is Symmetric then both paddings must be no greater than inputShape(i).
2940  auto inputShape = inputTensorInfo.GetShape();
2941  auto padList = desc.m_PadList;
2942 
2943  const unsigned int isReflect = static_cast<unsigned int>(desc.m_PaddingMode == PaddingMode::Reflect);
2944  for(unsigned int i = 0; i < padList.size(); ++i)
2945  {
2946  if(padList.at(i).first > (inputShape[i] - isReflect) ||
2947  padList.at(i).second > (inputShape[i] - isReflect))
2948  {
2949  ARMNN_THROW_PARSE_EXCEPTION("Padding values must be less (Reflect) or "
2950  "equal (Symmetric) to the dimension size.");
2951  }
2952  }
2953 
2954  auto layerName = fmt::format("MirrorPad:{}:{}", subgraphIndex, operatorIndex);
2955 
2956  IConnectableLayer* layer = m_Network->AddPadLayer(desc, layerName.c_str());
2957 
2958  if (!layer)
2959  {
2960  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2961  operatorIndex, CHECK_LOCATION().AsString()));
2962  }
2963 
2964  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
2965  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
2966 
2967  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
2968  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
2969 
2970  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
2971  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
2972 }
2973 
2974 void TfLiteParserImpl::ParsePrelu(size_t subgraphIndex, size_t operatorIndex)
2975 {
2976  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
2977 
2978  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
2979  CHECK_VALID_SIZE(inputs.size(), 2);
2980 
2981  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
2982  CHECK_VALID_SIZE(outputs.size(), 1);
2983 
2984  auto layerName = fmt::format("Prelu:{}:{}", subgraphIndex, operatorIndex);
2985 
2986  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
2987  armnn::TensorInfo alphaTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
2988 
2989  IConnectableLayer* layer = m_Network->AddPreluLayer(layerName.c_str());
2990 
2991  if (!layer)
2992  {
2993  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
2994  operatorIndex, CHECK_LOCATION().AsString()));
2995  }
2996 
2997  if (IsConstTensor(inputs[1]))
2998  {
2999  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3000  armnn::IInputSlot* slot = &(layer->GetInputSlot(0));
3001  RegisterConsumerOfTensor(subgraphIndex, inputTensorIndexes[0], slot);
3002 
3003  auto alphaTensorAndData = CreateConstTensorNonPermuted(inputs[1], alphaTensorInfo,
3004  inputTensorInfo.GetDataType());
3005  std::string constLayerName = fmt::format("Constant:{}", inputs[1]->name);
3006  IConnectableLayer* constLayer =
3007  m_Network->AddConstantLayer(alphaTensorAndData.first, constLayerName.c_str());
3008 
3009  if (!constLayer)
3010  {
3011  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3012  operatorIndex, CHECK_LOCATION().AsString()));
3013  }
3014 
3015  constLayer->GetOutputSlot(0).SetTensorInfo(alphaTensorInfo);
3016  constLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(1));
3017  RegisterOutputSlots(subgraphIndex,
3018  VIRTUAL_OPERATOR_ID,
3019  constLayer,
3020  { inputTensorIndexes[1] });
3021  }
3022  else
3023  {
3024  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3025  RegisterInputSlots(subgraphIndex, operatorIndex, layer, inputTensorIndexes);
3026  }
3027 
3028  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
3029  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
3030  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3031 
3032  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3033  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3034 }
3035 
3036 void TfLiteParserImpl::ParseQuantize(size_t subgraphIndex, size_t operatorIndex)
3037 {
3038  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3039 
3040  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3041  CHECK_VALID_SIZE(inputs.size(), 1);
3042 
3043  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3044  CHECK_VALID_SIZE(outputs.size(), 1);
3045 
3046  auto layerName = fmt::format("Quantize:{}:{}", subgraphIndex, operatorIndex);
3047 
3048  IConnectableLayer* layer = m_Network->AddQuantizeLayer(layerName.c_str());
3049 
3050  if (!layer)
3051  {
3052  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3053  operatorIndex, CHECK_LOCATION().AsString()));
3054  }
3055 
3056  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3057  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3058 
3059  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3060  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3061 
3062  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3063  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3064 }
3065 
3066 void TfLiteParserImpl::ParseRelu(size_t subgraphIndex, size_t operatorIndex)
3067 {
3068  ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::ReLu);
3069 }
3070 
3071 void TfLiteParserImpl::ParseRelu6(size_t subgraphIndex, size_t operatorIndex)
3072 {
3073  ParseActivation(subgraphIndex,operatorIndex, ActivationFunction::BoundedReLu);
3074 }
3075 
3076 void TfLiteParserImpl::ParseLeakyRelu(size_t subgraphIndex, size_t operatorIndex)
3077 {
3078  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::LeakyReLu);
3079 }
3080 
3081 void TfLiteParserImpl::ParseLogistic(size_t subgraphIndex, size_t operatorIndex)
3082 {
3083  ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::Sigmoid);
3084 }
3085 
3086 void TfLiteParserImpl::ParseTanH(size_t subgraphIndex, size_t operatorIndex)
3087 {
3088  ParseActivation(subgraphIndex,operatorIndex,ActivationFunction::TanH);
3089 }
3090 
3091 void TfLiteParserImpl::ParseElu(size_t subgraphIndex, size_t operatorIndex)
3092 {
3093  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::Elu);
3094 }
3095 
3096 void TfLiteParserImpl::ParseHardSwish(size_t subgraphIndex, size_t operatorIndex)
3097 {
3098  ParseActivation(subgraphIndex, operatorIndex, ActivationFunction::HardSwish);
3099 }
3100 
3101 void TfLiteParserImpl::ParseActivation(size_t subgraphIndex, size_t operatorIndex, ActivationFunction activationType)
3102 {
3103  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3104  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3105  IgnoreUnused(operatorPtr);
3106 
3107  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3108  CHECK_VALID_SIZE(inputs.size(), 1);
3109 
3110  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3111  CHECK_VALID_SIZE(outputs.size(), 1);
3112 
3113  auto layerName = fmt::format("Activation:");
3114  ActivationDescriptor activationDesc;
3115  activationDesc.m_Function = activationType;
3116 
3117  switch (activationType)
3118  {
3119  case ActivationFunction::ReLu:
3120  {
3121  layerName += fmt::format("RELU:{}:{}", subgraphIndex, operatorIndex);
3122  break;
3123  }
3124  case ActivationFunction::BoundedReLu:
3125  {
3126  layerName += fmt::format("RELU6:{}:{}", subgraphIndex, operatorIndex);
3127  activationDesc.m_A = 6.0f;
3128  activationDesc.m_B = 0.0f;
3129  break;
3130  }
3131  case ActivationFunction::Sigmoid:
3132  {
3133  layerName += fmt::format("SIGMOID:{}:{}", subgraphIndex, operatorIndex);
3134  break;
3135  }
3136  case ActivationFunction::TanH:
3137  {
3138  layerName += fmt::format("TANH:{}:{}", subgraphIndex, operatorIndex);
3139  activationDesc.m_A = 1.0f;
3140  activationDesc.m_B = 1.0f;
3141  break;
3142  }
3143  case ActivationFunction::LeakyReLu:
3144  {
3145  layerName += fmt::format("LEAKYRELU:{}:{}", subgraphIndex, operatorIndex);
3146  const auto* options = operatorPtr->builtin_options.AsLeakyReluOptions();
3147  activationDesc.m_A = options->alpha;
3148  break;
3149  }
3150  case ActivationFunction::Elu:
3151  {
3152  layerName += fmt::format("ELU:{}:{}", subgraphIndex, operatorIndex);
3153  activationDesc.m_A = 1.0f;
3154  break;
3155  }
3156  case ActivationFunction::HardSwish:
3157  {
3158  layerName += fmt::format("HARDSWISH:{}:{}", subgraphIndex, operatorIndex);
3159  break;
3160  }
3161  default:
3162  {
3163  throw ParseException(
3164  fmt::format("Unexpected ActivationFunction[{}] when creating layerName {} ",
3165  static_cast<int>(activationType), CHECK_LOCATION().AsString()));
3166  }
3167  }
3168 
3169  IConnectableLayer* const layer = m_Network->AddActivationLayer(activationDesc, layerName.c_str());
3170 
3171  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3172  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3173 
3174  // register the input connection slots for the layer, connections are made after all layers have been created
3175  // only the tensors for the inputs are relevant, exclude the const tensors
3176  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3177  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3178 
3179  // register the output connection slots for the layer, connections are made after all layers have been created
3180  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3181  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3182 }
3184  const std::vector<int32_t>& targetDimsIn)
3185 {
3186  std::vector<unsigned int> outputDims(targetDimsIn.begin(), targetDimsIn.end());
3187  const auto stretchDim = std::find(targetDimsIn.begin(), targetDimsIn.end(), -1);
3188 
3189  if (stretchDim != targetDimsIn.end())
3190  {
3191  if (std::find(std::next(stretchDim), targetDimsIn.end(), -1) != targetDimsIn.end())
3192  {
3193  throw ParseException(
3194  fmt::format("At most one component of shape can be -1 {}", CHECK_LOCATION().AsString()));
3195  }
3196 
3197  auto targetNumElements =
3198  armnn::numeric_cast<unsigned int>(
3199  std::accumulate(targetDimsIn.begin(), targetDimsIn.end(), -1, std::multiplies<int32_t>()));
3200 
3201  auto stretchIndex = static_cast<size_t>(std::distance(targetDimsIn.begin(), stretchDim));
3202  outputDims[stretchIndex] = inputTensorInfo.GetNumElements() / targetNumElements;
3203  }
3204 
3205  TensorShape outputShape = TensorShape(static_cast<unsigned int>(outputDims.size()), outputDims.data());
3206 
3207  TensorInfo reshapeInfo = inputTensorInfo;
3208  reshapeInfo.SetShape(outputShape);
3209 
3210  return reshapeInfo;
3211 }
3212 
3213 void TfLiteParserImpl::ParseReshape(size_t subgraphIndex, size_t operatorIndex)
3214 {
3215  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3216 
3217  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3218 
3219  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3220  CHECK_VALID_SIZE(outputs.size(), 1);
3221 
3222  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3223  const auto* options = operatorPtr->builtin_options.AsReshapeOptions();
3224  auto layerName = fmt::format("Reshape:{}:{}", subgraphIndex, operatorIndex);
3225 
3226  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3227  armnn::TensorInfo actualOutputTensorInfo = ToTensorInfo(outputs[0]);
3228  CheckMatchingQuantization(inputTensorInfo, actualOutputTensorInfo, layerName, "Input 0", "Output 0");
3229 
3230  // Extracting new shape for the output
3231  // There are two ways it can be passed
3232  // * First is to define the target shape in the operator built-in options
3233  // * Second is to pass it as a second input tensor
3234  std::vector<int32_t> targetShape;
3235  bool targetShapeFound = false;
3236  // Check if built-in options were given
3237  if (options != nullptr)
3238  {
3239  // make sure the parameter is given
3240  if (options->new_shape.empty() == false)
3241  {
3242  targetShape = options->new_shape;
3243  targetShapeFound = true;
3244  }
3245  }
3246 
3247  // If there is no built-in option given or if the built-in new_shape parameter was empty
3248  if (!targetShapeFound)
3249  {
3250  // Check for a second input tensor
3251  if (inputs.size() > 1 && inputs[1] != nullptr)
3252  {
3253  if (inputs[1]->is_variable)
3254  {
3255  ARMNN_THROW_PARSE_EXCEPTION( "Target shapes defined in non-const input tensors is not supported");
3256  }
3257 
3258  if (inputs[1]->shape.size() != 1)
3259  {
3260  ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not a 1D tensor");
3261  }
3262 
3263  if (inputs[1]->type != tflite::TensorType_INT32)
3264  {
3265  ARMNN_THROW_PARSE_EXCEPTION("Target 'shape' input is not an int32 type");
3266  }
3267 
3268  // Extract target shape from input
3269  auto bufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3270  auto values = reinterpret_cast<const int32_t*>(bufferPtr->data.data());
3271  if (values)
3272  {
3273  for (int i = 0; i < inputs[1]->shape[0]; ++i)
3274  {
3275  targetShape.push_back(values[i]);
3276  }
3277  }
3278  else
3279  {
3280  try
3281  {
3282  // We attempt to infer during Runtime.
3283  TensorShape reshapeShapes = ToTensorInfo(inputs[1]).GetShape();
3284 
3285  if (reshapeShapes[0] == actualOutputTensorInfo.GetNumDimensions())
3286  {
3287  for (unsigned int i = 0; i < actualOutputTensorInfo.GetShape().GetNumDimensions(); ++i)
3288  {
3289  targetShape.push_back(actualOutputTensorInfo.GetShape()[i]);
3290  }
3291  }
3292  // The parser only supports shape (batch, -1) or (-1) for non-constant shape input.
3293  else if (reshapeShapes[0] > 2)
3294  {
3295  throw ParseException(fmt::format("Invalid input shape '{}' in Reshape layer '{}' {}. "
3296  "When inferring during runtime, the parser only supports "
3297  "shape (batch, -1) or (-1) for target shape input.",
3298  reshapeShapes[0],
3299  layerName,
3300  CHECK_LOCATION().AsString()));
3301  }
3302  else
3303  {
3304  const int32_t numInputElements = inputTensorInfo.GetNumElements();
3305  const int32_t inputTensorShape = inputTensorInfo.GetShape()[0];
3306  if (reshapeShapes[0] == 1)
3307  {
3308  targetShape = {numInputElements};
3309  }
3310  else if (reshapeShapes[0] == 2)
3311  {
3312  targetShape = {inputTensorShape, numInputElements / inputTensorShape};
3313  }
3314  }
3315  }
3316  catch (const std::exception& exc)
3317  {
3318  ARMNN_THROW_PARSE_EXCEPTION("Failed attempt to infer during runtime the target shape input for "
3319  "Reshape operation. Reshape operator target shape input buffer data "
3320  "is null. " << exc.what());
3321  }
3322  }
3323  }
3324  else
3325  {
3326  ARMNN_THROW_PARSE_EXCEPTION("Target shape not defined in reshape parameters or input tensor. "
3327  "At least one method required");
3328  }
3329  }
3330 
3331  armnn::TensorInfo reshapeOutputTensorInfo =
3332  TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
3333 
3334  // Check for valid input size and that reshape parameters equal output shape
3335  // The output shape can be provided to us in 2 ways:
3336  // 1. through the normal 'shape' parameter given by outputs[indx]->shape
3337  // 2. through additional parameter 'shape_signature' given by outputs[indx]->buffer.
3338  // This parameter can sometimes contain -1 value not visible in the 'shape' parameter.
3339  const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
3340  if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
3341  {
3342  // Attempt to extract output shape from secondary 'shape_signature'
3343  // parameter and try to CheckShape() with this param.
3344  std::vector<int32_t> secondaryOutputTargetShape = outputs[0]->shape_signature;
3345 
3346  // if outputs[0]->shape_signature contain a -1 value, we need to compute its actual value
3347  // from reshape input in order to correctly verify reshape parameters equal output shape
3348  armnn::TensorInfo secondaryReshapeOutputTensorInfo =
3349  TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, secondaryOutputTargetShape);
3350 
3351  if (!CheckShape(reshapeOutputTensorShape, secondaryReshapeOutputTensorInfo.GetShape()))
3352  {
3353  std::stringstream ss;
3354  ss << "New shape defined in reshape parameters "
3355  << reshapeOutputTensorShape
3356  << " does not equal output shape "
3357  << actualOutputTensorInfo.GetShape()
3358  << ": "
3359  << CHECK_LOCATION().AsString();
3360  throw ParseException(ss.str());
3361  }
3362  }
3363  auto outputTensorIds = GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex);
3364 
3365  ReshapeDescriptor reshapeDesc;
3366  reshapeDesc.m_TargetShape = reshapeOutputTensorInfo.GetShape();
3367  m_TensorInfos[outputTensorIds[0]] = reshapeOutputTensorInfo;
3368 
3369  IConnectableLayer* layer = m_Network->AddReshapeLayer(reshapeDesc, layerName.c_str());
3370 
3371  if (!layer)
3372  {
3373  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3374  operatorIndex, CHECK_LOCATION().AsString()));
3375  }
3376 
3377  layer->GetOutputSlot(0).SetTensorInfo(reshapeOutputTensorInfo);
3378 
3379  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3380  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3381 
3382  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3383  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3384 }
3385 
3386 void TfLiteParserImpl::ParseResizeBilinear(size_t subgraphIndex, size_t operatorIndex)
3387 {
3388  ParseResize(subgraphIndex, operatorIndex, ResizeMethod::Bilinear);
3389 }
3390 
3391 void TfLiteParserImpl::ParseResizeNearestNeighbor(size_t subgraphIndex, size_t operatorIndex)
3392 {
3393  ParseResize(subgraphIndex, operatorIndex, ResizeMethod::NearestNeighbor);
3394 }
3395 
3396 void TfLiteParserImpl::ParseResize(size_t subgraphIndex, size_t operatorIndex, ResizeMethod resizeMethod)
3397 {
3398  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3399 
3400  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3401  CHECK_VALID_SIZE(inputs.size(), 2);
3402 
3403  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3404  CHECK_VALID_SIZE(outputs.size(), 1);
3405 
3406  armnn::TensorInfo sizeTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3407 
3408  // Data for the parsed tensor args (size) must be stored locally.
3409  std::vector<int32_t> sizeTensorData(sizeTensorInfo.GetNumElements());
3410 
3411  BufferRawPtr sizeBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3412  ::memcpy(sizeTensorData.data(), sizeBufferPtr->data.data(), sizeTensorInfo.GetNumBytes());
3413 
3414  ResizeDescriptor desc;
3415  desc.m_Method = resizeMethod;
3416  desc.m_TargetHeight = static_cast<uint32_t> (sizeTensorData[0]);
3417  desc.m_TargetWidth = static_cast<uint32_t> (sizeTensorData[1]);
3419 
3420  auto layerName = fmt::format("Resize:");
3421 
3422  switch (resizeMethod)
3423  {
3424  case ResizeMethod::Bilinear:
3425  {
3426  layerName += fmt::format("BILINEAR:{}:{}", subgraphIndex, operatorIndex);
3427 
3428  const auto & operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3429  const auto * options = operatorPtr->builtin_options.AsResizeBilinearOptions();
3430 
3431  desc.m_AlignCorners = options->align_corners;
3432  break;
3433  }
3434  case ResizeMethod::NearestNeighbor:
3435  {
3436  layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
3437  break;
3438  }
3439  default:
3440  {
3441  throw ParseException(
3442  fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
3443  static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
3444  }
3445  }
3446 
3447  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3448 
3449  IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
3450 
3451  if (!layer)
3452  {
3453  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3454  operatorIndex, CHECK_LOCATION().AsString()));
3455  }
3456 
3457  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3458  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
3459  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3460 
3461  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3462  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3463 
3464  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3465  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3466 }
3467 
3468 void TfLiteParserImpl::ParseReverseV2(size_t subgraphIndex, size_t operatorIndex)
3469 {
3470  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3471 
3472  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3473  CHECK_VALID_SIZE(inputs.size(), 2);
3474 
3475  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3476  CHECK_VALID_SIZE(outputs.size(), 1);
3477 
3478  auto layerName = fmt::format("ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3479 
3480  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3481  TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
3482  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3483 
3484  IConnectableLayer* layer = m_Network->AddReverseV2Layer(layerName.c_str());
3485  ARMNN_ASSERT(layer != nullptr);
3486 
3487  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3488 
3489  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3490  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3491 
3492  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3493  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3494 }
3495 
3496 void TfLiteParserImpl::ParseTile(size_t subgraphIndex, size_t operatorIndex)
3497 {
3498  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3499 
3500  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3501  CHECK_VALID_SIZE(inputs.size(), 2);
3502 
3503  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3504  CHECK_VALID_SIZE(outputs.size(), 1);
3505 
3506  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3507  TensorInfo multiplesTensorInfo = ToTensorInfo(inputs[1]);
3508  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3509 
3510  auto layerName = fmt::format("Tile:{}:{}", subgraphIndex, operatorIndex);
3511 
3512  TileDescriptor descriptor;
3513 
3514  BufferRawPtr multiplesBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3515  if (multiplesBufferPtr != nullptr)
3516  {
3517  std::vector<int32_t> multiplesData(multiplesTensorInfo.GetNumElements());
3518  ::memcpy(multiplesData.data(), multiplesBufferPtr->data.data(), multiplesTensorInfo.GetNumBytes());
3519  descriptor.m_Multiples.assign(multiplesData.begin(), multiplesData.end());
3520  }
3521  else
3522  {
3523  ARMNN_THROW_PARSE_EXCEPTION("For Tile layer, Multiples data was not found in the buffer.");
3524  }
3525 
3526  IConnectableLayer* layer = m_Network->AddTileLayer(descriptor, layerName.c_str());
3527  ARMNN_ASSERT(layer != nullptr);
3528 
3529  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3530 
3531  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3532  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3533 
3534  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3535  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3536 }
3537 
3538 void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
3539 {
3540  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3541 
3542  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3543  const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
3544 
3545  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3546 
3547  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3548  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3549  auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
3550 
3551  CHECK_VALID_SIZE(outputs.size(), 1);
3552 
3553  unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
3554  uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).GetNumDimensions();
3555 
3556  const unsigned int concatDimInput = static_cast<unsigned int>(
3557  (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
3558 
3559  OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3560  concatDescriptor.SetConcatAxis(concatDimInput);
3561  unsigned int mergeDimOrigin = 0;
3562 
3563  for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3564  {
3565  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
3566 
3567  // This set up concatDescriptor view origin
3569  inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
3570  }
3571 
3572  auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
3573 
3574  IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
3575 
3576  if (!layer)
3577  {
3578  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3579  operatorIndex, CHECK_LOCATION().AsString()));
3580  }
3581 
3582  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3583  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3584 
3585  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3586  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3587 
3588  // add fused activation layer
3589  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
3590 
3591  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3592  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3593 }
3594 
3595 void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
3596 {
3597  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3598 
3599  const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3600  const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3601 
3602  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3603 
3605  desc.m_BiasEnabled = false;
3606  desc.m_TransposeWeightMatrix = true;
3607 
3608  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3609  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3610  CHECK_VALID_SIZE(outputs.size(), 1);
3611 
3612  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3613 
3614  // Fully Connected Layer accepts two dimensional weights input
3615  int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3616  if (weightsDimension != 2)
3617  {
3618  throw ParseException(
3619  fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3620  "Node {}",
3621  weightsDimension,
3622  CHECK_LOCATION().AsString()));
3623  }
3624 
3625  armnn::IConnectableLayer* layer = nullptr;
3626  auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3627 
3628  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3629  // Add the first input tensor to the registration list
3630  std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3631  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3632 
3633  desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3634 
3635  // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3636  tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3637 
3638  if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
3639  {
3640  m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3641  }
3642 
3643  if (inputs.size() == 3)
3644  {
3645  desc.m_BiasEnabled = true;
3646  armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3647 
3648  // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3649  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3650 
3651  if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
3652  {
3653  m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3654  }
3655  }
3656 
3657  // Filters and biases are always passed to fully connected as inputs
3658  layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3659 
3660  if (!layer)
3661  {
3662  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3663  operatorIndex, CHECK_LOCATION().AsString()));
3664  }
3665 
3666  unsigned int startingSlotIndex = 0;
3667  if (inputTensorInfo.GetNumDimensions() > 2)
3668  {
3669  // Add reshape to flatten to 2D [batch_size, input_size],
3670  // where "input_size" corresponds to the number of inputs to the layer,
3671  // matching the second dimension of weights,
3672  // and "batch_size" is calculated by dividing the number of elements by "input_size".
3673  std::vector<unsigned int> reshapedDimensions(2);
3674  reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3675  reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3676 
3677  if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3678  {
3679  throw ParseException(
3680  fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3681  reshapedDimensions[1],
3682  CHECK_LOCATION().AsString()));
3683  }
3684 
3685  armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3686  reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3687  inputTensorInfo = reshapedTensorInfo;
3688 
3689  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
3690  armnn::ReshapeDescriptor reshapeDescriptor;
3691  reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3692  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor,
3693  reshapeLayerName.c_str());
3694 
3695  reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3696  reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3697 
3698  RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3699  // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3700  tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3701  startingSlotIndex = 1;
3702  }
3703 
3704  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3705 
3706  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3707  { inputTensorInfo.GetShape(),
3708  filterTensorInfo.GetShape() });
3709 
3710  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3711 
3712  if (outputTensorInfo.GetNumDimensions() > 2)
3713  {
3714  // Calculate reshape to flatten to 2D [batch_size, input_size]
3715  std::vector<unsigned int> reshapedDimensions(2);
3716  reshapedDimensions[1] = filterTensorInfo.GetShape()[0];
3717  reshapedDimensions[0] = outputTensorInfo.GetNumElements() / reshapedDimensions[1];
3718  armnn::TensorInfo reshapedOutputTensorInfo = outputTensorInfo;
3719  if (outputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3720  {
3721  throw ParseException(
3722  fmt::format("Failed to deduce output tensor shape from filter size {} {}",
3723  reshapedDimensions[1],
3724  CHECK_LOCATION().AsString()));
3725  }
3726  reshapedOutputTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3727  layer->GetOutputSlot(0).SetTensorInfo(reshapedOutputTensorInfo);
3728 
3729  std::string reshapeLayerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3730  layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3731  }
3732 
3733  // we need to add the activation layer and fortunately we don't need to care about the data layout
3734  armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3735  options->fused_activation_function);
3736 
3737  // register the output connection slots for the layer, connections are made after all layers have been created
3738  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3739  RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3740 
3741  m_TensorInfos[outputTensorIndexes[0]] = layer->GetOutputSlot(0).GetTensorInfo();
3742 }
3743 
3744 void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
3745 {
3746  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3747 
3748  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3749 
3750  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3751  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3752  CHECK_VALID_SIZE(outputs.size(), 4);
3753 
3754  // Obtain custom options from flexbuffers
3755  auto custom_options = operatorPtr->custom_options;
3756  const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3757 
3758  // Obtain descriptor information from tf lite
3760  desc.m_MaxDetections = m["max_detections"].AsUInt32();
3761  desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3762  desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3763  desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3764  desc.m_NumClasses = m["num_classes"].AsUInt32();
3765  desc.m_ScaleH = m["h_scale"].AsFloat();
3766  desc.m_ScaleW = m["w_scale"].AsFloat();
3767  desc.m_ScaleX = m["x_scale"].AsFloat();
3768  desc.m_ScaleY = m["y_scale"].AsFloat();
3769 
3770  if (!(m["use_regular_nms"].IsNull()))
3771  {
3772  desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
3773  }
3774  if (!(m["detections_per_class"].IsNull()))
3775  {
3776  desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3777  }
3778 
3779  if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3780  {
3781  throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3782  "must be positive and less than or equal to 1.");
3783  }
3784 
3785  armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3786  auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3787 
3788  auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3789  IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3790  layerName.c_str());
3791 
3792  if (!layer)
3793  {
3794  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3795  operatorIndex, CHECK_LOCATION().AsString()));
3796  }
3797 
3798  // The model does not specify the output shapes.
3799  // The output shapes are calculated from the max_detection and max_classes_per_detection.
3800  unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3801  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3802  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3803  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3804  m_OverriddenOutputShapes.push_back({ 1 });
3805 
3806  for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3807  {
3808  armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverriddenOutputShapes[i]);
3809  layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3810  }
3811 
3812  // Register the input connection slots for the layer, connections are made after all layers have been created
3813  // only the tensors for the inputs are relevant, exclude the const tensors
3814  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3815  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3816 
3817  // Register the output connection slots for the layer, connections are made after all layers have been created
3818  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3819  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3820  outputTensorIndexes[1],
3821  outputTensorIndexes[2],
3822  outputTensorIndexes[3]});
3823 }
3824 
3825 /// The TfLite Pack operator is equivalent to the ArmNN Stack operator
3826 void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
3827 {
3828  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3829 
3830  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3831  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3832  CHECK_VALID_SIZE(outputs.size(), 1);
3833 
3834  if (inputs.size() < 1)
3835  {
3836  throw ParseException("Pack must have at least one input.");
3837  }
3838 
3839  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3840  const auto* options = operatorPtr->builtin_options.AsPackOptions();
3841 
3842  StackDescriptor desc;
3843  desc.m_Axis = static_cast<uint32_t>(options->axis);
3844  desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3845 
3846  // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3847  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3848  desc.m_InputShape = inputTensorInfo.GetShape();
3849 
3850  auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
3851  IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3852 
3853  if (!layer)
3854  {
3855  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3856  operatorIndex, CHECK_LOCATION().AsString()));
3857  }
3858 
3859  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3860  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3861 
3862  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3863  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3864 
3865  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3866  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3867 }
3868 
3869 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
3870 {
3871  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3872 
3873  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3874  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3875 
3876  if (inputs.size() < 2)
3877  {
3878  throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
3879  }
3880 
3881  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3882  const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
3883  const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
3884  CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
3885  auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3886  auto outputTensorInfo = ToTensorInfo(outputs[0]);
3887 
3888  // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
3889  // Please refer to each operand at
3890  // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
3891  armnn::LstmInputParams params;
3892 
3893  if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
3894  {
3895  params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
3896  inputTensorInfo).first;
3897  }
3898 
3899  params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
3900  inputTensorInfo).first;
3901  params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
3902  inputTensorInfo).first;
3903  params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
3904  inputTensorInfo).first;
3905 
3906  // Recurrent weight tensors of size {n_cell, n_output}
3907  if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
3908  {
3909  params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
3910  inputTensorInfo).first;
3911  }
3912 
3913  params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
3914  inputTensorInfo).first;
3915  params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
3916  inputTensorInfo).first;
3917  params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
3918  inputTensorInfo).first;
3919 
3920  // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
3921  if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
3922  {
3923  params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
3924  inputTensorInfo).first;
3925  }
3926 
3927  if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
3928  {
3929  params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
3930  inputTensorInfo).first;
3931  }
3932 
3933  if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
3934  {
3935  params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
3936  inputTensorInfo).first;
3937  }
3938 
3939  // Gates bias tensors of size {n_cell}
3940  if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
3941  {
3942  params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
3943  inputTensorInfo).first;
3944  }
3945 
3946  params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
3947  inputTensorInfo).first;
3948  params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
3949  inputTensorInfo).first;
3950  params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
3951  inputTensorInfo).first;
3952 
3953  // Projection weight tensor of size {n_output, n_cell}
3954  if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
3955  {
3956  params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
3957  inputTensorInfo).first;
3958  }
3959  // Projection bias tensor of size {n_output}
3960  if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
3961  {
3962  params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
3963  inputTensorInfo).first;
3964  }
3965 
3966  // These state tensors are defined as variable tensors, and will be modified by this op.
3967  armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
3968  m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
3969  armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
3970  m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
3971 
3972  // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
3973  if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
3974  {
3975  params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
3976  inputTensorInfo).first;
3977  }
3978 
3979  if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
3980  {
3981  params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
3982  inputTensorInfo).first;
3983  }
3984 
3985  if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
3986  {
3987  params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
3988  inputTensorInfo).first;
3989  }
3990 
3991  if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
3992  {
3993  params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
3994  inputTensorInfo).first;
3995  }
3996 
3997  // set the layer descriptor
3999  desc.m_ActivationFunc = nodeParams->fused_activation_function;
4000  desc.m_ClippingThresCell = nodeParams->cell_clip;
4001  desc.m_ClippingThresProj = nodeParams->proj_clip;
4002  desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
4003  || params.m_RecurrentToInputWeights == nullptr
4004  || params.m_InputGateBias == nullptr);
4005  desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
4006  desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
4007  desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
4008  || params.m_ForgetLayerNormWeights != nullptr
4009  || params.m_CellLayerNormWeights != nullptr
4010  || params.m_OutputLayerNormWeights != nullptr);
4011  desc.m_TimeMajor = nodeParams->time_major;
4012 
4013  if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
4014  {
4015  auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
4016  inputTensorInfo).first;
4017  auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
4018  desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
4019 
4020  auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
4021  inputTensorInfo).first;
4022  auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
4023  desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
4024 
4025  auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
4026  inputTensorInfo).first;
4027  auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
4028  desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
4029 
4030  auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
4031  inputTensorInfo).first;
4032  auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
4033  desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
4034  }
4035  else
4036  {
4037  float defaultIntermediate = std::pow(2, -12);
4038  desc.m_InputIntermediateScale = defaultIntermediate;
4039  desc.m_ForgetIntermediateScale = defaultIntermediate;
4040  desc.m_CellIntermediateScale = defaultIntermediate;
4041  desc.m_OutputIntermediateScale = defaultIntermediate;
4042  }
4043 
4044  if (operatorPtr->intermediates.size() > 4)
4045  {
4046  auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
4047  inputTensorInfo).first;
4048 
4049  desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
4050  desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
4051  }
4052  unsigned int batchSize = inputTensorInfo.GetShape()[0];
4053  unsigned int outputSize = outputTensorInfo.GetShape()[2];
4054  unsigned int numUnits = cellStateInInfo.GetShape()[1];
4055 
4056  armnn::DataType dataType = inputTensorInfo.GetDataType();
4057  float qScale = inputTensorInfo.GetQuantizationScale();
4058  float qOffset = inputTensorInfo.GetQuantizationOffset();
4059 
4060  armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
4061  if (!desc.m_CifgEnabled)
4062  {
4063  scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
4064  }
4065  armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
4066  cellStateInInfo.GetDataType(),
4067  cellStateInInfo.GetQuantizationScale(),
4068  cellStateInInfo.GetQuantizationOffset());
4069  armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
4070 
4071  armnn::LstmInputParamsInfo paramsInfo;
4072  paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
4073  paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
4074  paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
4075  paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
4076  paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
4077  paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
4078  paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
4079  paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
4080  paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
4081 
4082  if (!desc.m_CifgEnabled)
4083  {
4084  paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
4085  paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
4086  if (params.m_CellToInputWeights != nullptr)
4087  {
4088  paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
4089  }
4090  paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
4091  }
4092 
4093  if (desc.m_ProjectionEnabled)
4094  {
4095  paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
4096  if (params.m_ProjectionBias != nullptr)
4097  {
4098  paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
4099  }
4100  }
4101 
4102  if (desc.m_PeepholeEnabled)
4103  {
4104  paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
4105  paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
4106  }
4107 
4108  if (desc.m_LayerNormEnabled)
4109  {
4110  if(!desc.m_CifgEnabled)
4111  {
4112  paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
4113  }
4114  paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
4115  paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
4116  paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
4117  }
4118 
4119  auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
4120  armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
4121 
4122  if (!layer)
4123  {
4124  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4125  operatorIndex, CHECK_LOCATION().AsString()));
4126  }
4127 
4128  // register the input connection slots for the layer, connections are made after all layers have been created
4129  // only the tensors for the inputs are relevant, exclude the const tensors
4130  auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
4131  operatorPtr->inputs[18],
4132  operatorPtr->inputs[19]});
4133  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
4134  inputTensorIndexes[1],
4135  inputTensorIndexes[2]});
4136 
4137  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4138 
4139  layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
4140  layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
4141  layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
4142 
4143  unsigned int tensorIndex = outputTensorIndexes[0];
4144  armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
4145  RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4146 }
4147 
4148 void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
4149 {
4150  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4151 
4152  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4153  const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
4154 
4155  // This unpackAxis indicates the axis to unpack
4156  const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
4157 
4158  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4159  CHECK_VALID_SIZE(inputs.size(), 1);
4160 
4161  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4162 
4163  if (unpackAxis >= inputTensorInfo.GetNumDimensions())
4164  {
4165  throw ParseException(
4166  fmt::format("The unpack axis: {} cannot be greater than or equal to "
4167  "the number of input dimension {} {}",
4168  unpackAxis,
4169  inputTensorInfo.GetNumDimensions(),
4170  CHECK_LOCATION().AsString()));
4171  }
4172 
4173  unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
4174  // If num is not defined, automatically infer from the length of the dimension axis.
4175  if(unpackNum == 0)
4176  {
4177  unpackNum = inputTensorInfo.GetShape()[unpackAxis];
4178  }
4179 
4180  // If unpack number cannot be inferred and is still zero, throw ParseException.
4181  if(unpackNum == 0)
4182  {
4183  throw ParseException("Number to unpack must greater than zero.");
4184  }
4185 
4186  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4187  CHECK_VALID_SIZE(outputs.size(), unpackNum);
4188 
4189  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4190  std::vector<unsigned int> unpackDimSizes(inputDimSize);
4191 
4192  // Add current input shape to unpackDimSizes
4193  for (unsigned int i = 0; i < inputDimSize; ++i)
4194  {
4195  unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
4196  }
4197 
4198  if (unpackDimSizes[unpackAxis] != unpackNum)
4199  {
4200  throw ParseException("Number to unpack must be the same as length of the dimension to "
4201  "unpack along.");
4202  }
4203 
4204  unpackDimSizes[unpackAxis] /= unpackNum;
4205 
4206  SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
4207  for (unsigned int j = 0; j < unpackNum; ++j)
4208  {
4209  // Set the size of the views.
4210  for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
4211  {
4212  splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
4213  }
4214  splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
4215  }
4216 
4217  auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
4218  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4219 
4220  if (!layer)
4221  {
4222  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4223  operatorIndex, CHECK_LOCATION().AsString()));
4224  }
4225 
4226  TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
4227  unpackDimSizes.data());
4228 
4229  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4230  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4231 
4232  std::vector<unsigned int> reshapeDims;
4233  for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
4234  {
4235  if (axis != unpackAxis)
4236  {
4237  reshapeDims.push_back(splitOutShape[axis]);
4238  }
4239  }
4240 
4241  TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
4242 
4243  // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
4244  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4245  {
4246  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
4247  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
4249  desc.m_TargetShape = reshapeOutputShape;
4250  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
4251 
4252  layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
4253  outputTensorInfo.GetDataType(),
4254  outputTensorInfo.GetQuantizationScale(),
4255  outputTensorInfo.GetQuantizationOffset()));
4256  layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
4257 
4258  reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4259 
4260  uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
4261  armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
4262  RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4263  }
4264 }
4265 
4266 void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
4267 {
4268  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4269 
4270  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4271  const auto* options = operatorPtr->builtin_options.AsSplitOptions();
4272 
4273  const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4274 
4275  // If number of splits cannot be inferred and is zero, throw ParseException.
4276  if(numSplits == 0)
4277  {
4278  throw ParseException("Number to splits must greater than zero.");
4279  }
4280 
4281  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4282  CHECK_VALID_SIZE(inputs.size(), 2);
4283  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4284  CHECK_VALID_SIZE(outputs.size(), numSplits);
4285 
4286  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4287  armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4288 
4289  if (axisTensorInfo.GetNumElements() != 1)
4290  {
4291  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4292  CHECK_LOCATION().AsString()));
4293  }
4294 
4295  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
4296  if (axisBufferPtr == nullptr)
4297  {
4298  throw ParseException(
4299  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4300  CHECK_LOCATION().AsString()));
4301  }
4302 
4303  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4304  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4305  int32_t axis = axisData[0];
4306 
4307  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4308  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4309  {
4310  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4311  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4312  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4313  throw ParseException(
4314  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4315  axis,
4316  CHECK_LOCATION().AsString()));
4317  }
4318 
4319  const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
4320 
4321  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4322  if (inputDimSize > MaxNumOfTensorDimensions)
4323  {
4324  throw ParseException(
4325  fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4326  inputTensorInfo.GetNumDimensions(),
4328  CHECK_LOCATION().AsString()));
4329  }
4330 
4331  std::vector<unsigned int> splitterDimSizes(inputDimSize);
4332 
4333  // Add current input shape to splitterDimSizes
4334  for (unsigned int i = 0; i < inputDimSize; ++i)
4335  {
4336  splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
4337  }
4338 
4339  if (splitterDimSizes[splitDim] % numSplits != 0)
4340  {
4341  throw ParseException("Number of splits must evenly divide the dimension");
4342  }
4343  splitterDimSizes[splitDim] /= numSplits;
4344 
4345  SplitterDescriptor splitDesc(numSplits, inputDimSize);
4346  for (unsigned int j = 0; j < numSplits; ++j)
4347  {
4348  // Set the size of the views.
4349  for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4350  {
4351  splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4352  }
4353  splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4354  }
4355 
4356  auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
4357  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4358 
4359  if (!layer)
4360  {
4361  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4362  operatorIndex, CHECK_LOCATION().AsString()));
4363  }
4364 
4365  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4366  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
4367 
4368  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4369  {
4370  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
4371  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4372  }
4373 
4374  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4375  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4376 }
4377 
4378 unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
4379 {
4380  int numDims = armnn::numeric_cast<int>(numDimsIn);
4381  int v = idx < 0 ? numDims + idx : idx;
4382 
4383  if (v < 0 || v > numDims)
4384  {
4385  throw ParseException(fmt::format("Unable to compute index {}", CHECK_LOCATION().AsString()));
4386  }
4387 
4388  return static_cast<unsigned int>(v);
4389 }
4390 
4391 void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
4392 {
4393  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4394 
4395  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4396  const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
4397 
4398  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4399  CHECK_VALID_SIZE(inputs.size(), 3);
4400 
4401  auto& inputTensor = inputs[0];
4402  auto& splitsTensor = inputs[1];
4403  auto& axisTensor = inputs[2];
4404 
4405  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
4406  armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
4407  armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
4408 
4409  if (axisTensorInfo.GetNumElements() != 1)
4410  {
4411  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4412  CHECK_LOCATION().AsString()));
4413  }
4414 
4415  // Inputs
4416  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4417  if (inputDimSize > MaxNumOfTensorDimensions)
4418  {
4419  throw ParseException(
4420  fmt::format("The number of dimensions: {} for input tensors of the "
4421  "SplitV op cannot be greater than {} {}",
4422  inputTensorInfo.GetNumDimensions(),
4424  CHECK_LOCATION().AsString()));
4425  }
4426 
4427  // Get split axis
4428  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
4429  if (axisBufferPtr == nullptr)
4430  {
4431  throw ParseException(
4432  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4433  CHECK_LOCATION().AsString()));
4434  }
4435 
4436  std::vector<int> axisData(axisTensorInfo.GetNumElements());
4437  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4438  int32_t axis = axisData[0];
4439 
4440  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4441  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4442  {
4443  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4444  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4445  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4446  throw ParseException(
4447  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4448  axis,
4449  CHECK_LOCATION().AsString()));
4450  }
4451  const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
4452 
4453  // Set split sizes
4454  CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
4455  unsigned int numSplits{0};
4456 
4457  if(options)
4458  {
4459  numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4460  }
4461  else
4462  {
4463  numSplits = splitsInfo.GetNumElements();
4464  }
4465 
4466  if (numSplits <=0)
4467  {
4468  throw ParseException("SplitV has invalid number of splits");
4469  }
4470 
4471  std::vector<int> splitsData(numSplits);
4472  BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
4473  ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
4474 
4475  unsigned int idx = 0;
4476  int numInferred{0};
4477  unsigned int inferIdx{0};
4478  int splitSum{0};
4479  for (auto split : splitsData)
4480  {
4481  if (split < 0)
4482  {
4483  numInferred++;
4484  inferIdx = idx;
4485  }
4486  else
4487  {
4488  splitSum += split;
4489  }
4490  idx++;
4491  }
4492  // Check for inferred Axis
4493  if (numInferred == 0)
4494  {
4495  if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
4496  {
4497  throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
4498  }
4499  }
4500  else if (numInferred == 1)
4501  {
4502  splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
4503  }
4504  else
4505  {
4506  throw ParseException("Cannot infer split size for more than one split");
4507  }
4508 
4509  //Ouput size validation
4510  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4511  CHECK_VALID_SIZE(outputs.size(), numSplits);
4512 
4513  // Setup Armnn descriptor
4514  SplitterDescriptor splitDesc(numSplits, inputDimSize);
4515  unsigned int accumSplit = 0;
4516  for (unsigned int j = 0; j < numSplits; ++j)
4517  {
4518  unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
4519 
4520  // Set the size of the views.
4521  for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
4522  {
4523  unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
4524  if (dimIdx == splitDim)
4525  {
4526  dimSize = splitSize;
4527  }
4528  splitDesc.SetViewSize(j, dimIdx, dimSize);
4529  }
4530 
4531  splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4532  accumSplit += splitSize;
4533  }
4534 
4535  auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
4536  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4537 
4538  if (!layer)
4539  {
4540  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4541  operatorIndex, CHECK_LOCATION().AsString()));
4542  }
4543 
4544  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4545  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4546 
4547  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4548  {
4549  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
4550  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4551  }
4552 
4553  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4554  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4555 }
4556 
4557 void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
4558 {
4559  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
4560 }
4561 
4562 void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
4563 {
4564  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
4565 }
4566 
4567 void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
4568 {
4569  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4570  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4571  CHECK_VALID_SIZE(inputs.size(), 2);
4572 
4573  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4574  CHECK_VALID_SIZE(outputs.size(), 1);
4575 
4576  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4577  armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4578  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4579 
4580  if (axisTensorInfo.GetNumElements() != 1)
4581  {
4582  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4583  CHECK_LOCATION().AsString()));
4584  }
4585 
4586  // Check if output tensor type is Signed32 or Signed64
4587  if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
4588  outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
4589  {
4590  throw ParseException(
4591  fmt::format(
4592  "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4593  CHECK_LOCATION().AsString()));
4594  }
4595 
4596  // Get const axis value from model and set it to descriptor.
4597  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4598  if (axisBufferPtr == nullptr)
4599  {
4600  throw ParseException(
4601  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4602  CHECK_LOCATION().AsString()));
4603  }
4604 
4605  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4606  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4607  int32_t axis = axisData.front();
4608 
4609  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4610  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4611  {
4612  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4613  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4614  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4615  throw ParseException(
4616  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4617  axis,
4618  CHECK_LOCATION().AsString()));
4619  }
4620 
4621  ArgMinMaxDescriptor desc;
4622  desc.m_Axis = axis;
4623  desc.m_Function = argMinMaxFunction;
4624 
4625  // Register a ArgMin/ArgMax layer.
4626  auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
4627  auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4628  IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4629 
4630  if (!layer)
4631  {
4632  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4633  operatorIndex, CHECK_LOCATION().AsString()));
4634  }
4635 
4636  outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4637  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4638 
4639  // Register input tensor to the layer.
4640  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4641  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4642 
4643  // Register output tensor to the layer.
4644  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4645  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4646 }
4647 
4648 void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
4649 {
4650  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4651 
4652  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4653  CHECK_VALID_SIZE(inputs.size(), 2);
4654  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4655  CHECK_VALID_SIZE(outputs.size(), 1);
4656 
4657  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4658  armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4659  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4660 
4661  armnn::GatherDescriptor gatherDescriptor;
4662 
4663  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4664  const auto* options = operatorPtr->builtin_options.AsGatherOptions();
4665  auto axis = options->axis;
4666 
4667  auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4668 
4669  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4670  auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4671  auto outputDimensions = outputTensorInfo.GetNumDimensions();
4672  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4673  {
4674  throw ParseException(
4675  fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4676  axis,
4677  inputDimensions, inputDimensions,
4678  CHECK_LOCATION().AsString()));
4679  }
4680  if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4681  {
4682  throw ParseException(
4683  fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4684  outputDimensions,
4685  inputDimensions, indicesDimensions,
4686  CHECK_LOCATION().AsString()));
4687  }
4688 
4689  gatherDescriptor.m_Axis = axis;
4690 
4691  IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4692 
4693  if (!layer)
4694  {
4695  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4696  operatorIndex, CHECK_LOCATION().AsString()));
4697  }
4698 
4699  outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4700  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4701 
4702  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4703  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4704 
4705  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4706  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4707 }
4708 
4709 void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4710 {
4711  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4712 
4713  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4714  CHECK_VALID_SIZE(inputs.size(), 2);
4715  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4716  CHECK_VALID_SIZE(outputs.size(), 1);
4717 
4718  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4719  armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4720 
4721  auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4722  IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4723 
4724  if (!layer)
4725  {
4726  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4727  operatorIndex, CHECK_LOCATION().AsString()));
4728  }
4729 
4730  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4731  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4732 
4733  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4734  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4735 
4736  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4737  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4738 }
4739 
4740 void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
4741 {
4742  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4743 
4744  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4745  CHECK_VALID_SIZE(inputs.size(), 1);
4746  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4747  CHECK_VALID_SIZE(outputs.size(), 1);
4748 
4749  armnn::DepthToSpaceDescriptor descriptor;
4750 
4751  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4752  const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4753  auto blockSize = options->block_size;
4754  if (blockSize < 2)
4755  {
4756  throw ParseException(
4757  fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4758  blockSize,
4759  CHECK_LOCATION().AsString()));
4760  }
4761  descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4762 
4763  auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4764  IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4765 
4766  if (!layer)
4767  {
4768  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4769  operatorIndex, CHECK_LOCATION().AsString()));
4770  }
4771 
4772  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4773  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4774 
4775  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4776  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4777 
4778  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4779  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4780 }
4781 
4782 void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
4783 {
4784  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4785 }
4786 
4787 void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4788 {
4789  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4790 }
4791 
4792 void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4793 {
4794  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4795 }
4796 
4797 void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4798 {
4799  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4800 }
4801 
4802 void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4803 {
4804  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4805 
4806  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4807  const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4808 
4809  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4810  CHECK_VALID_SIZE(inputs.size(), 2);
4811 
4812  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4813  CHECK_VALID_SIZE(outputs.size(), 1);
4814 
4815  auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
4816 
4817  armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4818  armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4819 
4820  ReduceDescriptor desc;
4821  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4822  // Get const axis value from model and set it to descriptor.
4823  if (axisBufferPtr != nullptr)
4824  {
4825  std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4826  ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4827 
4828  // Convert the axis to unsigned int and remove duplicates.
4829  auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4830  std::set<unsigned int> uniqueAxis;
4831  std::transform(axisData.begin(),
4832  axisData.end(),
4833  std::inserter(uniqueAxis, uniqueAxis.begin()),
4834  [rank](int i)->unsigned int{
4835  return static_cast<uint32_t>(((i + rank) % rank)); });
4836  desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4837  }
4838  else
4839  {
4840  for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4841  {
4842  desc.m_vAxis.push_back(i);
4843  }
4844  }
4845 
4846  desc.m_KeepDims = options->keep_dims;
4847  desc.m_ReduceOperation = reduceOperation;
4848 
4849  // Register a new layer object, Sum.
4850  IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
4851 
4852  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4853  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4854 
4855  // Register input tensor to the layer.
4856  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4857  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4858 
4859  // Register output tensor to the layer.
4860  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4861  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4862 }
4863 
4864 void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
4865 {
4866  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4867 
4868  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4869  CHECK_VALID_SIZE(inputs.size(), 1);
4870 
4871  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4872  CHECK_VALID_SIZE(outputs.size(), 1);
4873 
4874  auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
4875  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4876 
4877  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4878 
4879  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4880  const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
4881 
4882  armnn::NormalizationDescriptor descriptor;
4886  descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
4887  descriptor.m_K = options->bias;
4888  descriptor.m_Alpha = options->alpha;
4889  descriptor.m_Beta = options->beta;
4890 
4891  // ArmNN expects normSize to be the full size of the normalization
4892  // window rather than the radius as in TfLite.
4893  descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
4894 
4895  IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
4896 
4897  if (!layer)
4898  {
4899  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4900  operatorIndex, CHECK_LOCATION().AsString()));
4901  }
4902 
4903  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4904  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4905 
4906  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4907  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4908 
4909  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4910  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4911 }
4912 
4913 void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
4914 {
4915  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
4916 }
4917 
4918 void TfLiteParserImpl::ParseCeil(size_t subgraphIndex, size_t operatorIndex)
4919 {
4920  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Ceil);
4921 }
4922 
4923 void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
4924 {
4925  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
4926 }
4927 
4928 void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
4929 {
4930  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
4931 }
4932 
4933 void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
4934 {
4935  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
4936 }
4937 
4938 void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
4939 {
4940  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
4941 }
4942 
4943 void TfLiteParserImpl::ParsePower(size_t subgraphIndex, size_t operatorIndex)
4944 {
4945  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4946 
4947  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4948  CHECK_VALID_SIZE(inputs.size(), 2);
4949 
4950  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4951  CHECK_VALID_SIZE(outputs.size(), 1);
4952 
4953  auto layerName = fmt::format("Power:{}:{}", subgraphIndex, operatorIndex);
4954 
4955  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4956  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4957  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
4958 
4959  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
4960 
4961  if (!layer)
4962  {
4963  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4964  operatorIndex, CHECK_LOCATION().AsString()));
4965  }
4966 
4967  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4968  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
4969  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4970 
4971  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4972  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4973 
4974  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4975  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4976 }
4977 
4978 void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
4979 {
4980  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
4981 }
4982 
4983 void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
4984 {
4985  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
4986 }
4987 
4988 void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
4989 {
4990  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
4991 }
4992 
4993 void TfLiteParserImpl::ParseSquare(size_t subgraphIndex, size_t operatorIndex)
4994 {
4995  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4996 
4997  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4998  CHECK_VALID_SIZE(inputs.size(), 1);
4999 
5000  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5001  CHECK_VALID_SIZE(outputs.size(), 1);
5002 
5003  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5004 
5005  auto layerName = fmt::format("Square:{}:{}", subgraphIndex, operatorIndex);
5006  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
5007  ARMNN_ASSERT(layer != nullptr);
5008 
5009  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 0});
5010  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5011  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5012 
5013  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5014  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[0]});
5015 
5016  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5017  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5018 }
5019 
5020 void TfLiteParserImpl::ParseSquaredDifference(size_t subgraphIndex, size_t operatorIndex)
5021 {
5022  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5023 
5024  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5025  CHECK_VALID_SIZE(inputs.size(), 2);
5026 
5027  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5028  CHECK_VALID_SIZE(outputs.size(), 1);
5029 
5030  auto layerName = fmt::format("SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
5031 
5032  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5033  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5034  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
5035 
5036  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
5037 
5038  if (!layer)
5039  {
5040  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5041  operatorIndex, CHECK_LOCATION().AsString()));
5042  }
5043 
5044  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5045  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5046  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5047 
5048  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5049  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5050 
5051  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5052  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5053 }
5054 
5055 void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
5056 {
5057  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5058 
5059  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5060  CHECK_VALID_SIZE(inputs.size(), 1);
5061 
5062  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5063  CHECK_VALID_SIZE(outputs.size(), 1);
5064 
5065  std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
5066  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5067 
5069  desc.m_Operation = unaryOperation;
5070  IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
5071 
5072  if (!layer)
5073  {
5074  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5075  operatorIndex, CHECK_LOCATION().AsString()));
5076  }
5077 
5078  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5079  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5080 
5081  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5082  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5083 
5084  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5085  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5086 }
5087 
5088 void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
5089 {
5090  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
5091 }
5092 
5093 void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
5094 {
5095  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
5096 }
5097 
5098 void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
5099 {
5100  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
5101 }
5102 
5103 void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
5104 {
5105  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
5106 }
5107 
5108 void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
5109 {
5110  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
5111 }
5112 
5113 void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
5114 {
5115  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
5116 }
5117 
5118 void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
5119  ComparisonOperation comparisonOperation)
5120 {
5121  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5122 
5123  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5124  CHECK_VALID_SIZE(inputs.size(), 2);
5125 
5126  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5127  CHECK_VALID_SIZE(outputs.size(), 1);
5128 
5129  auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
5130  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5131 
5132  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5133  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5134  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
5135 
5136  ComparisonDescriptor desc;
5137  desc.m_Operation = comparisonOperation;
5138  IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
5139 
5140  if (!layer)
5141  {
5142  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5143  operatorIndex, CHECK_LOCATION().AsString()));
5144  }
5145 
5146  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5147  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5148 
5149  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5150  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5151 
5152  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5153  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5154 }
5155 
5156 armnn::IConnectableLayer* TfLiteParserImpl::AddReshapeLayer(armnn::IConnectableLayer* layer,
5157  unsigned int outputSlot,
5158  std::string reshapeLayerName,
5159  armnn::TensorInfo outputShape)
5160 {
5161  ReshapeDescriptor desc;
5162  desc.m_TargetShape = outputShape.GetShape();
5163 
5164  IConnectableLayer* reshapeLayer =
5165  m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
5166 
5167  auto & prevOutputSlot = layer->GetOutputSlot(outputSlot);
5168  prevOutputSlot.Connect(reshapeLayer->GetInputSlot(0));
5169  reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputShape);
5170  return reshapeLayer;
5171 }
5172 
5173 armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
5174  unsigned int outputSlot,
5175  tflite::ActivationFunctionType activationType)
5176 {
5177  ActivationDescriptor activationDesc;
5178  std::string layerName = prevLayer->GetName();
5179 
5180  switch(activationType)
5181  {
5182  case tflite::ActivationFunctionType_NONE:
5183  {
5184  // this is a no-op: return previous layer
5185  return prevLayer;
5186  }
5187  case tflite::ActivationFunctionType_RELU:
5188  {
5189  activationDesc.m_Function = ActivationFunction::ReLu;
5190  layerName += ":RELU";
5191  break;
5192  }
5193  case tflite::ActivationFunctionType_RELU6:
5194  {
5195  activationDesc.m_Function = ActivationFunction::BoundedReLu;
5196  activationDesc.m_A = 6.0f;
5197  activationDesc.m_B = 0.0f;
5198  layerName += ":RELU6";
5199  break;
5200  }
5201  case tflite::ActivationFunctionType_TANH:
5202  {
5203  activationDesc.m_Function = ActivationFunction::TanH;
5204  activationDesc.m_A = 1.0f;
5205  activationDesc.m_B = 1.0f;
5206  layerName += ":TANH";
5207  break;
5208  }
5209 
5210  // I only put these here as a reminder what others we could support
5211  case tflite::ActivationFunctionType_RELU_N1_TO_1:
5212  case tflite::ActivationFunctionType_SIGN_BIT:
5213  default:
5214  {
5215  throw ParseException(
5216  fmt::format("TfLite parser doesn't support fused activation: "
5217  "{}/{} {} ",
5218  activationType,
5219  tflite::EnumNameActivationFunctionType(activationType),
5220  CHECK_LOCATION().AsString()));
5221 
5222  }
5223  }
5224 
5225  IConnectableLayer* activationLayer =
5226  m_Network->AddActivationLayer(activationDesc, layerName.c_str());
5227 
5228  auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5229  prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
5230  activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
5231  return activationLayer;
5232 }
5233 
5234 armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
5235  unsigned int outputSlot)
5236 {
5237 
5238  auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5239  DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
5240 
5241  if (dataType == DataType::Signed32)
5242  {
5243  return prevLayer;
5244  }
5245 
5246  std::string layerName = prevLayer->GetName();
5247  IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
5248 
5249  prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
5250  floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
5251 
5252  return floorLayer;
5253 }
5254 
5256 {
5257  if (fileName == nullptr)
5258  {
5259  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
5260  CHECK_LOCATION().AsString()));
5261  }
5262  std::error_code errorCode;
5263  fs::path pathToFile(fileName);
5264  if (!fs::exists(pathToFile, errorCode))
5265  {
5266  //fmt::format() could not be used here (format error)
5267  std::stringstream msg;
5268  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
5269  << " " << CHECK_LOCATION().AsString();
5270  throw FileNotFoundException(msg.str());
5271  }
5272  if (!fs::is_regular_file(pathToFile))
5273  {
5274  // Exclude non regular files.
5275  throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
5276  pathToFile.c_str()));
5277  }
5278 
5279  std::ifstream file(fileName, std::ios::binary);
5280  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5281  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
5282  fileContent.size());
5283 }
5284 
5286 {
5287  if (binaryContent == nullptr)
5288  {
5289  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
5290  CHECK_LOCATION().AsString()));
5291  }
5292  flatbuffers::Verifier verifier(binaryContent, len);
5293  if (verifier.VerifyBuffer<tflite::Model>() == false)
5294  {
5295  throw ParseException(
5296  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
5297  "flatbuffers format. size:{} {}",
5298  len,
5299  CHECK_LOCATION().AsString()));
5300  }
5301  return tflite::UnPackModel(binaryContent);
5302 }
5303 
5305  size_t subgraphIndex,
5306  size_t operatorIndex)
5307 {
5308  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5309 
5310  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5311  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5312 
5313  size_t inputCount = operatorPtr->inputs.size();
5314  TensorRawPtrVector result;
5315  for (size_t i = 0; i < inputCount; ++i)
5316  {
5317  // If the input location is -1 then assume input is turned off.
5318  if (operatorPtr->inputs[i] == -1)
5319  {
5320  continue;
5321  }
5322  else
5323  {
5324  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
5325  result.push_back(subgraphPtr->tensors[inputId].get());
5326  }
5327  }
5328  return result;
5329 }
5330 
5332  size_t subgraphIndex,
5333  size_t operatorIndex)
5334 {
5335  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5336 
5337  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5338  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5339 
5340  size_t outputCount = operatorPtr->outputs.size();
5341  TensorRawPtrVector result(outputCount);
5342  for (size_t i = 0; i < outputCount; ++i)
5343  {
5344  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5345  CHECK_TENSOR(model, subgraphIndex, outputId);
5346  result[i] = subgraphPtr->tensors[outputId].get();
5347  }
5348  return result;
5349 }
5350 
5352  size_t subgraphIndex)
5353 {
5354  CHECK_SUBGRAPH(model, subgraphIndex);
5355  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5356 
5357  size_t inputCount = subgraphPtr->inputs.size();
5358  TensorIdRawPtrVector result(inputCount);
5359  for (size_t i = 0; i < inputCount; ++i)
5360  {
5361  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
5362  CHECK_TENSOR(model, subgraphIndex, inputId);
5363  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5364  }
5365  return result;
5366 }
5367 
5369  size_t subgraphIndex)
5370 {
5371  CHECK_SUBGRAPH(model, subgraphIndex);
5372  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5373 
5374  size_t outputCount = subgraphPtr->outputs.size();
5375  TensorIdRawPtrVector result(outputCount);
5376  for (size_t i = 0; i < outputCount; ++i)
5377  {
5378  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5379  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5380  }
5381  return result;
5382 }
5383 
5384 std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
5385  size_t subgraphIndex,
5386  size_t operatorIndex)
5387 {
5388  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5389  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5390  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5391  return operatorPtr->inputs;
5392 }
5393 
5394 std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
5395  size_t subgraphIndex,
5396  size_t operatorIndex)
5397 {
5398  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5399  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5400  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5401  return operatorPtr->outputs;
5402 }
5403 
5404 void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
5405  size_t operatorIndex,
5406  IConnectableLayer* layer,
5407  const std::vector<unsigned int>& tensorIndexes,
5408  unsigned int startingSlotIndex)
5409 {
5410  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5411 
5412  if (!layer)
5413  {
5414  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5415  operatorIndex, CHECK_LOCATION().AsString()));
5416  }
5417 
5418  if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
5419  {
5420  throw ParseException(
5421  fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
5422  " for subgraph:{} operator index:{} {}",
5423  tensorIndexes.size(),
5424  layer->GetNumInputSlots(),
5425  subgraphIndex,
5426  operatorIndex,
5427  CHECK_LOCATION().AsString()));
5428  }
5429 
5430  for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
5431  {
5432  unsigned int tensorIndex = tensorIndexes[index];
5433  armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
5434  RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5435  }
5436 }
5437 
5438 void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
5439  size_t operatorIndex,
5440  IConnectableLayer* layer,
5441  const std::vector<unsigned int>& tensorIndexes)
5442 {
5443  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5444 
5445  if (!layer)
5446  {
5447  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5448  operatorIndex, CHECK_LOCATION().AsString()));
5449  }
5450 
5451  if (tensorIndexes.size() != layer->GetNumOutputSlots())
5452  {
5453  throw ParseException(
5454  fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
5455  " for subgraph:{} operator index:{} {}",
5456  tensorIndexes.size(),
5457  layer->GetNumOutputSlots(),
5458  subgraphIndex,
5459  operatorIndex,
5460  CHECK_LOCATION().AsString()));
5461  }
5462 
5463  for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
5464  {
5465  unsigned int tensorIndex = tensorIndexes[slotIndex];
5466  armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
5467  RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5468  }
5469 }
5470 
5471 void TfLiteParserImpl::SetupInputLayerTensorInfos(size_t subgraphIndex)
5472 {
5473  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5474 
5475  auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5476  for (auto const& tensorIdAndPtr : inputs)
5477  {
5478  auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5479  m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5480  }
5481 }
5482 
5483 void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
5484 {
5485  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5486 
5487  auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5488  for (auto const& tensorIdAndPtr : inputs)
5489  {
5490  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5491  IConnectableLayer* layer =
5492  m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5493 
5494  auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5495  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5496 
5497  RegisterOutputSlots(subgraphIndex,
5498  VIRTUAL_OPERATOR_ID,
5499  layer,
5500  { static_cast<uint32_t>(tensorIdAndPtr.first) });
5501  }
5502 }
5503 
5504 void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
5505 {
5506  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5507 
5508  auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
5509  for (auto const& tensorIdAndPtr : outputs)
5510  {
5511  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5512  IConnectableLayer* layer =
5513  m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5514 
5515  RegisterInputSlots(subgraphIndex,
5516  VIRTUAL_OPERATOR_ID,
5517  layer,
5518  { static_cast<uint32_t>(tensorIdAndPtr.first) });
5519  }
5520 }
5521 
5522 void TfLiteParserImpl::SetupConstantLayerTensorInfos(size_t subgraph)
5523 {
5524  CHECK_SUBGRAPH(m_Model, subgraph);
5525 
5526  const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5527  for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5528  {
5529  for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5530  {
5531  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5532  m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5533  {
5534  TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5535 
5536  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5537 
5538  m_TensorInfos.insert({tensorIndex, tensorInfo});
5539  }
5540  }
5541  }
5542 }
5543 
5544 void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
5545 {
5546  CHECK_SUBGRAPH(m_Model, subgraph);
5547 
5548  const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5549  for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5550  {
5551  for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5552  {
5553  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5554  m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5555  {
5556  TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5557 
5558  if (IsConstTensor(tensorPtr))
5559  {
5560  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5561  armnn::DataType dataType = tensorInfo.GetDataType();
5562 
5563  if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5564  != m_ConstantsToDequantize.end())
5565  {
5566  dataType = DataType::Float32;
5567  }
5568  auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5569 
5570  std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5571  IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5572 
5573  layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
5574  RegisterOutputSlots(subgraphIndex,
5575  VIRTUAL_OPERATOR_ID,
5576  layer,
5577  { tensorIndex });
5578  }
5579  else if (ShouldConstantTensorBeCreated(tensorIndex))
5580  {
5581  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5582  armnn::DataType dataType = tensorInfo.GetDataType();
5583 
5584  if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5585  != m_ConstantsToDequantize.end())
5586  {
5587  dataType = DataType::Float32;
5588  }
5589  // Make sure isConstant flag is set.
5590  tensorInfo.SetConstant();
5591  tensorInfo.SetDataType(dataType);
5592 
5593  auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
5594 
5595  std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5596  IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
5597 
5598  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5599  RegisterOutputSlots(subgraphIndex,
5600  VIRTUAL_OPERATOR_ID,
5601  layer,
5602  {tensorIndex});
5603  }
5604  else
5605  {
5606  throw ParseException(
5607  fmt::format("Invalid Tensor: Tensor should be constant. {}",
5608  CHECK_LOCATION().AsString()));
5609  }
5610  }
5611  }
5612  }
5613 }
5614 
5615 // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
5617 {
5618  CHECK_BUFFER(model, bufferIndex);
5619  return model->buffers[bufferIndex].get();
5620 }
5621 
5622 template<typename T>
5623 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5624 TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
5626  armnn::TensorInfo& tensorInfo,
5628 {
5629  // Make sure isConstant flag is set.
5630  tensorInfo.SetConstant();
5631 
5632  auto constData = CreateConstTensorImpl<T>(bufferPtr,
5633  tensorPtr,
5634  tensorInfo,
5635  permutationVector);
5636  TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
5637  return std::make_pair(constData.first, std::move(storage));
5638 }
5639 
5640 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
5641 {
5642  // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
5643  return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5644  != m_ConstantsToBeCreated.end());
5645 }
5646 
5647 bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
5648 {
5649  CHECK_TENSOR_PTR(tensorPtr);
5650  bool isConst = true;
5651 
5652  auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
5653  if (buffer->data.size() == 0)
5654  {
5655  isConst = false;
5656  }
5657 
5658  return isConst;
5659 }
5660 
5661 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5662 TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
5663  armnn::TensorInfo& tensorInfo,
5665 {
5666  CHECK_TENSOR_PTR(tensorPtr);
5667  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5668  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5669 
5670  // Make sure isConstant flag is set.
5671  tensorInfo.SetConstant();
5672 
5673  switch (tensorInfo.GetDataType())
5674  {
5676  return CreateConstTensorAndStoreData<float>(bufferPtr,
5677  tensorPtr,
5678  tensorInfo,
5679  permutationVector);
5681  return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5682  tensorPtr,
5683  tensorInfo,
5684  permutationVector);
5686  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5687  tensorPtr,
5688  tensorInfo,
5689  permutationVector);
5691  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5692  tensorPtr,
5693  tensorInfo,
5694  permutationVector);
5696  return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5697  tensorPtr,
5698  tensorInfo,
5699  permutationVector);
5700  default:
5701  {
5702  std::stringstream errString;
5703  errString << "Unexpected datatype when creating const tensor: "
5704  << armnn::GetDataTypeName(tensorInfo.GetDataType())
5705  << " shape:" << tensorInfo.GetShape()
5706  << CHECK_LOCATION().AsString();
5707  throw ParseException(errString.str());
5708  }
5709  }
5710 }
5711 
5712 armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5713  armnn::TensorInfo& tensorInfo)
5714 {
5715  CHECK_TENSOR_PTR(tensorPtr);
5716  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5717  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5718 
5719  // Make sure isConstant flag is set.
5720  tensorInfo.SetConstant();
5721 
5722  return ConstTensor(tensorInfo, bufferPtr->data.data());
5723 }
5724 
5725 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5726 TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5727  armnn::TensorInfo& tensorInfo,
5728  armnn::DataType inputDataType)
5729 {
5730  CHECK_TENSOR_PTR(tensorPtr);
5731  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5732  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5733 
5734  // Make sure isConstant flag is set.
5735  tensorInfo.SetConstant();
5736 
5737  if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5738  {
5739  try
5740  {
5741  TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5742  std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5743  return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
5744  }
5745  catch (InvalidArgumentException&)
5746  {
5747  throw ParseException(
5748  fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5749  GetDataTypeName(DataType::Float32),
5750  GetDataTypeName(tensorInfo.GetDataType()),
5751  CHECK_LOCATION().AsString()));
5752  }
5753  }
5754  else
5755  {
5756  return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5757  }
5758 }
5759 
5760 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5761 TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
5762 {
5763  CHECK_TENSOR_PTR(tensorPtr);
5764  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5765  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5766  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5767 
5768  // Make sure isConstant flag is set.
5769  tensorInfo.SetConstant();
5770 
5771  if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5772  {
5773  try
5774  {
5775  TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5776  std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5777  return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
5778  }
5779  catch (InvalidArgumentException&)
5780  {
5781  throw ParseException(
5782  fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5783  GetDataTypeName(DataType::Float32),
5784  GetDataTypeName(tensorInfo.GetDataType()),
5785  CHECK_LOCATION().AsString()));
5786  }
5787  }
5788  else
5789  {
5790  return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5791  }
5792 }
5793 
5795  const std::string& name) const
5796 {
5797  CHECK_SUBGRAPH(m_Model, subgraphId);
5798  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5799  for (auto const& input : inputs)
5800  {
5801  if (input.second->name == name)
5802  {
5803  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5804  auto inputTensorInfo = ToTensorInfo(input.second);
5805  // Input tensors are always treated as constant tensors during network execution.
5806  inputTensorInfo.SetConstant(true);
5807  return std::make_pair(bindingId, inputTensorInfo);
5808  }
5809  }
5810 
5811  std::stringstream bindings;
5812  for (auto const& input : inputs)
5813  {
5814  bindings << "'" << input.second->name << "' ";
5815  }
5816 
5817  throw ParseException(
5818  fmt::format("No input binding found for subgraph:{} and name:{}. "
5819  "Possible inputs are: [{}] {}",
5820  subgraphId,
5821  name,
5822  bindings.str(),
5823  CHECK_LOCATION().AsString()));
5824 }
5825 
5827  const std::string& name) const
5828 {
5829  CHECK_SUBGRAPH(m_Model, subgraphId);
5830  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5831  for (unsigned int i = 0; i < outputs.size(); ++i)
5832  {
5833  auto const output = outputs[i];
5834  if (output.second->name == name)
5835  {
5836  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5837  std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5838  m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5839  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
5840  }
5841  }
5842 
5843  std::stringstream bindings;
5844  for (auto const& output : outputs)
5845  {
5846  bindings << "'" << output.second->name << "' ";
5847  }
5848 
5849  throw ParseException(
5850  fmt::format("No output binding found for subgraph:{} and name:{}. "
5851  "Possible outputs are: [{}] {}",
5852  subgraphId,
5853  name,
5854  bindings.str(),
5855  CHECK_LOCATION().AsString()));
5856 }
5857 
5859 {
5860  return m_Model->subgraphs.size();
5861 }
5862 
5863 std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
5864 {
5865  CHECK_SUBGRAPH(m_Model, subgraphId);
5866  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5867  std::vector<std::string> result;
5868  result.reserve(inputs.size());
5869  for (auto const& input : inputs)
5870  {
5871  result.push_back(input.second->name);
5872  }
5873  return result;
5874 }
5875 
5876 std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
5877 {
5878  CHECK_SUBGRAPH(m_Model, subgraphId);
5879  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5880  std::vector<std::string> result;
5881  result.reserve(outputs.size());
5882  for (auto const& output : outputs)
5883  {
5884  result.push_back(output.second->name);
5885  }
5886  return result;
5887 }
5888 
5889 const std::string TfLiteParserImpl::GetVersion()
5890 {
5891  return TFLITE_PARSER_VERSION;
5892 }
5893 
5894 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
5895 : m_FloatData(std::move(data))
5896 , m_Uint8Data(nullptr)
5897 , m_Int8Data(nullptr)
5898 , m_Int32Data(nullptr)
5899 {
5900 }
5901 
5902 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
5903 : m_FloatData(nullptr)
5904 , m_Uint8Data(std::move(data))
5905 , m_Int8Data(nullptr)
5906 , m_Int32Data(nullptr)
5907 {
5908 }
5909 
5910 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
5911 : m_FloatData(nullptr)
5912 , m_Uint8Data(nullptr)
5913 , m_Int8Data(std::move(data))
5914 , m_Int32Data(nullptr)
5915 {
5916 }
5917 
5918 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
5919 : m_FloatData(nullptr)
5920 , m_Uint8Data(nullptr)
5921 , m_Int8Data(nullptr)
5922 , m_Int32Data(std::move(data))
5923 {
5924 }
5925 
5926 } // armnnTfLiteParser
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::Convolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:570
armnn::ArgMinMaxFunction::Max
@ Max
armnn::INetworkPtr
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:339
armnnTfLiteParser::TfLiteParserImpl::GetInputs
static TensorRawPtrVector GetInputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5304
armnn::LstmInputParams::m_RecurrentToForgetWeights
const ConstTensor * m_RecurrentToForgetWeights
Definition: LstmParams.hpp:45
armnnTfLiteParser::TfLiteParserImpl::GetOutputs
static TensorRawPtrVector GetOutputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5331
armnn::DataType::Boolean
@ Boolean
armnn::FullyConnectedDescriptor::m_ConstantWeights
bool m_ConstantWeights
Enable/disable constant weights and biases.
Definition: Descriptors.hpp:530
armnn::Pooling2dDescriptor::m_PaddingMethod
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
Definition: Descriptors.hpp:425
armnn::ViewsDescriptor
A ViewsDescriptor for the SplitterLayer.
Definition: Descriptors.hpp:244
armnn::TensorInfo::GetNumElements
unsigned int GetNumElements() const
Definition: Tensor.hpp:196
armnn::LstmInputParams::m_OutputLayerNormWeights
const ConstTensor * m_OutputLayerNormWeights
Definition: LstmParams.hpp:60
armnn::DetectionPostProcessDescriptor::m_NmsScoreThreshold
float m_NmsScoreThreshold
NMS score threshold.
Definition: Descriptors.hpp:751
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::TransposeConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:1448
armnnDeserializer::TensorRawPtr
const armnnSerializer::TensorInfo * TensorRawPtr
Definition: Deserializer.hpp:20
CHECK_BUFFER_SIZE
#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID)
Definition: TfLiteParser.cpp:322
armnn::FullyConnectedDescriptor
A FullyConnectedDescriptor for the FullyConnectedLayer.
Definition: Descriptors.hpp:507
armnn::DetectionPostProcessDescriptor::m_ScaleX
float m_ScaleX
Center size encoding scale x.
Definition: Descriptors.hpp:759
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtrVector
std::vector< TensorRawPtr > TensorRawPtrVector
Definition: TfLiteParser.hpp:36
armnn::ComparisonOperation::LessOrEqual
@ LessOrEqual
armnnDeserializer::CheckShape
bool CheckShape(const armnn::TensorShape &actual, const std::vector< uint32_t > &expected)
Definition: Deserializer.cpp:188
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
armnn::TransposeConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:1456
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtr
const tflite::TensorT * TensorRawPtr
Definition: TfLiteParser.hpp:35
armnnTfLiteParser::TfLiteParserImpl::CreateNetworkFromBinaryFile
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
Definition: TfLiteParser.cpp:950
armnn::Optional
Definition: Optional.hpp:270
armnn::IConnectableLayer::GetNumInputSlots
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
armnn::LstmDescriptor::m_TimeMajor
bool m_TimeMajor
Enable/disable time major.
Definition: Descriptors.hpp:1133
armnn::ResizeMethod
ResizeMethod
Definition: Types.hpp:165
Descriptors.hpp
armnn::SpaceToBatchNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1050
armnn::FullyConnectedDescriptor::m_TransposeWeightMatrix
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
Definition: Descriptors.hpp:528
armnn::ResizeDescriptor::m_TargetHeight
uint32_t m_TargetHeight
Target height value.
Definition: Descriptors.hpp:988
armnn::DepthwiseConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:708
armnn::Pooling2dDescriptor::m_PoolHeight
uint32_t m_PoolHeight
Pooling height value.
Definition: Descriptors.hpp:417
armnn::StridedSliceDescriptor::m_Begin
std::vector< int > m_Begin
Begin values for the input that will be sliced.
Definition: Descriptors.hpp:1321
armnn::LstmInputParams::m_ProjectionBias
const ConstTensor * m_ProjectionBias
Definition: LstmParams.hpp:56
armnn::DetectionPostProcessDescriptor::m_ScaleY
float m_ScaleY
Center size encoding scale y.
Definition: Descriptors.hpp:761
armnn::DetectionPostProcessDescriptor::m_MaxDetections
uint32_t m_MaxDetections
Maximum numbers of detections.
Definition: Descriptors.hpp:745
armnn::LstmInputParams::m_RecurrentToCellWeights
const ConstTensor * m_RecurrentToCellWeights
Definition: LstmParams.hpp:46
armnn::DataLayout::NHWC
@ NHWC
armnnTfLiteParser::TfLiteParserImpl::CreateNetworkFromBinary
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)
Create the network from a flatbuffers binary.
Definition: TfLiteParser.cpp:957
armnn::LstmInputParams::m_CellBias
const ConstTensor * m_CellBias
Definition: LstmParams.hpp:53
armnn::Convolution3dDescriptor::m_PadFront
uint32_t m_PadFront
Padding front value in the depth dimension.
Definition: Descriptors.hpp:637
armnn::ResizeDescriptor
A ResizeDescriptor for the ResizeLayer.
Definition: Descriptors.hpp:964
armnn::ArgMinMaxDescriptor
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:67
armnn::IConnectableLayer::GetName
virtual const char * GetName() const =0
Returns the name of the layer.
armnnTfLiteParser::TfLiteParserImpl::SubgraphPtr
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
Definition: TfLiteParser.hpp:31
armnn::ActivationDescriptor::m_A
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH,...
Definition: Descriptors.hpp:61
armnn::CheckLocation::m_Function
const char * m_Function
Definition: Exceptions.hpp:16
armnn::StridedSliceDescriptor::m_EllipsisMask
int32_t m_EllipsisMask
Ellipsis mask value.
Definition: Descriptors.hpp:1336
armnnUtils::GetUnsignedAxis
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
Definition: TensorUtils.cpp:221
armnn::SoftmaxDescriptor::m_Beta
float m_Beta
Exponentiation value.
Definition: Descriptors.hpp:190
armnn::GatherDescriptor
A GatherDescriptor for the GatherLayer.
Definition: Descriptors.hpp:944
armnn::TensorInfo::GetQuantizationScale
float GetQuantizationScale() const
Definition: Tensor.cpp:461
TypesUtils.hpp
armnn::DepthwiseConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:710
armnn::L2NormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:824
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::L2NormalizationDescriptor
A L2NormalizationDescriptor for the L2NormalizationLayer.
Definition: Descriptors.hpp:809
armnn::NormalizationAlgorithmMethod::LocalBrightness
@ LocalBrightness
Krichevsky 2012: Local Brightness Normalization.
armnn::NormalizationDescriptor::m_Beta
float m_Beta
Beta value for the normalization equation.
Definition: Descriptors.hpp:801
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputs
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5368
armnn::GetDataTypeName
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:223
armnn::TensorInfo::SetDataType
void SetDataType(DataType type)
Definition: Tensor.hpp:199
armnn::NormalizationDescriptor
A NormalizationDescriptor for the NormalizationLayer.
Definition: Descriptors.hpp:769
armnn::Pooling2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:421
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:195
armnn::BatchToSpaceNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape values.
Definition: Descriptors.hpp:898
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::DataType::Float32
@ Float32
armnn::ResizeDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:993
Version.hpp
armnn::DepthwiseConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:692
armnn::Convolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:576
armnn::Pooling2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:411
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputTensorNames
std::vector< std::string > GetSubgraphInputTensorNames(size_t subgraphId) const
Return the input tensor names for a given subgraph.
Definition: TfLiteParser.cpp:5863
armnn::Convolution3dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:633
armnnTfLiteParser::TfLiteParserImpl::GetOutputTensorIds
static std::vector< int32_t > & GetOutputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5394
armnn::ArgMinMaxDescriptor::m_Function
ArgMinMaxFunction m_Function
Specify if the function is to find Min or Max.
Definition: Descriptors.hpp:81
armnn::SpaceToBatchNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape value.
Definition: Descriptors.hpp:1045
armnn::StridedSliceDescriptor::m_BeginMask
int32_t m_BeginMask
Begin mask value.
Definition: Descriptors.hpp:1329
armnn::LstmInputParams::m_CellToOutputWeights
const ConstTensor * m_CellToOutputWeights
Definition: LstmParams.hpp:50
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputTensorNames
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
Definition: TfLiteParser.cpp:5876
armnnUtils::ProcessConcatInputTensorInfo
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
Definition: ParserHelper.cpp:19
armnn::Convolution3dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:647
armnn::LstmInputParams::m_InputToCellWeights
const ConstTensor * m_InputToCellWeights
Definition: LstmParams.hpp:42
armnn::Convolution3dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:635
armnn::MaxNumOfTensorDimensions
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromFile
static ModelPtr LoadModelFromFile(const char *fileName)
Definition: TfLiteParser.cpp:5255
armnn::DataType::QAsymmU8
@ QAsymmU8
armnn::ArgMinMaxFunction
ArgMinMaxFunction
Definition: Types.hpp:102
armnn::DetectionPostProcessDescriptor::m_ScaleW
float m_ScaleW
Center size encoding scale weight.
Definition: Descriptors.hpp:763
armnn::LstmDescriptor::m_InputIntermediateScale
float m_InputIntermediateScale
Input intermediate quantization scale.
Definition: Descriptors.hpp:1135
armnn::DataType::QSymmS8
@ QSymmS8
armnn::StackDescriptor
A StackDescriptor for the StackLayer.
Definition: Descriptors.hpp:1230
armnn::IConnectableLayer::InferOutputShapes
virtual std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const =0
Infer the shape of the output(s) based on the provided input shape(s)
armnnUtils::Permute
void Permute(const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
Definition: Permute.cpp:131
armnn::IOutputSlot::GetTensorInfo
virtual const TensorInfo & GetTensorInfo() const =0
IgnoreUnused.hpp
armnn::NormalizationDescriptor::m_NormSize
uint32_t m_NormSize
Depth radius value.
Definition: Descriptors.hpp:797
armnnUtils::Permuted
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:98
armnnTfLiteParser::TfLiteParserImpl::BufferRawPtr
const tflite::BufferT * BufferRawPtr
Definition: TfLiteParser.hpp:40
armnnTfLiteParser::TfLiteParserImpl::OutputShapeOfReshape
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
Definition: TfLiteParser.cpp:3183
armnn::Pooling2dDescriptor::m_PoolWidth
uint32_t m_PoolWidth
Pooling width value.
Definition: Descriptors.hpp:415
armnn::UnaryOperation::Neg
@ Neg
armnn::Convolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:566
armnn::BatchToSpaceNdDescriptor::m_Crops
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
Definition: Descriptors.hpp:900
armnn::DepthwiseConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:702
armnnOnnxParser::ModelPtr
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
armnn::CheckLocation::AsString
std::string AsString() const
Definition: Exceptions.hpp:29
armnn::Convolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:580
armnn::DetectionPostProcessDescriptor::m_MaxClassesPerDetection
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
Definition: Descriptors.hpp:747
armnn::IConnectableLayer::GetNumOutputSlots
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
armnn::DataType::QSymmS16
@ QSymmS16
armnn::NormalizationDescriptor::m_NormMethodType
NormalizationAlgorithmMethod m_NormMethodType
Normalization method algorithm to use (LocalBrightness, LocalContrast).
Definition: Descriptors.hpp:795
armnn::TransposeConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:1454
tflite
Definition: armnn_external_delegate.cpp:12
NumericCast.hpp
armnn::LstmInputParams::m_ForgetGateBias
const ConstTensor * m_ForgetGateBias
Definition: LstmParams.hpp:52
armnn::NormalizationAlgorithmChannel::Across
@ Across
armnn::ReduceDescriptor::m_ReduceOperation
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
Definition: Descriptors.hpp:1537
TensorUtils.hpp
ARMNN_THROW_PARSE_EXCEPTION
#define ARMNN_THROW_PARSE_EXCEPTION(msg)
Definition: TfLiteParser.cpp:41
armnn::ComparisonOperation::NotEqual
@ NotEqual
CHECK_TENSOR_PTR
#define CHECK_TENSOR_PTR(TENSOR_PTR)
Definition: TfLiteParser.cpp:216
armnn::LstmInputParams::m_CellToInputWeights
const ConstTensor * m_CellToInputWeights
Definition: LstmParams.hpp:48
armnn::ComparisonOperation::GreaterOrEqual
@ GreaterOrEqual
armnn::Exception::what
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
armnn::MeanDescriptor::m_KeepDims
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept.
Definition: Descriptors.hpp:1171
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::DataLayout::NDHWC
@ NDHWC
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
Definition: TfLiteParser.cpp:5285
Assert.hpp
CHECKED_NON_NEGATIVE
#define CHECKED_NON_NEGATIVE(VALUE)
Definition: VerificationHelpers.hpp:35
armnn::ResizeDescriptor::m_Method
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
Definition: Descriptors.hpp:991
armnn::SpaceToBatchNdDescriptor::m_PadList
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left,...
Definition: Descriptors.hpp:1048
armnn::LstmInputParams::m_InputToOutputWeights
const ConstTensor * m_InputToOutputWeights
Definition: LstmParams.hpp:43
armnn::LstmDescriptor::m_PeepholeEnabled
bool m_PeepholeEnabled
Enable/disable peephole.
Definition: Descriptors.hpp:1127
armnn::TensorShape
Definition: Tensor.hpp:20
armnnTfLiteParser::TfLiteParserImpl::GetNetworkInputBindingInfo
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 ...
Definition: TfLiteParser.cpp:5794
armnn::Convolution3dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:631
VerificationHelpers.hpp
armnn::NetworkOptions
std::vector< BackendOptions > NetworkOptions
Definition: BackendOptions.hpp:16
armnn::NormalizationDescriptor::m_NormChannelType
NormalizationAlgorithmChannel m_NormChannelType
Normalization channel algorithm to use (Across, Within).
Definition: Descriptors.hpp:793
LstmParams.hpp
armnn::IOutputSlot
An output connection slot for a layer.
Definition: INetwork.hpp:53
armnn::LstmInputParams::m_CellToForgetWeights
const ConstTensor * m_CellToForgetWeights
Definition: LstmParams.hpp:49
armnn::StackDescriptor::m_NumInputs
uint32_t m_NumInputs
Number of input tensors.
Definition: Descriptors.hpp:1254
CHECK_TENSOR
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
Definition: TfLiteParser.cpp:203
armnn::DataType::Float16
@ Float16
armnn::CheckLocation
Definition: Exceptions.hpp:14
armnnTfLiteParser::BindingPointInfo
armnn::BindingPointInfo BindingPointInfo
Definition: ITfLiteParser.hpp:20
armnn::LstmInputParams::m_RecurrentToInputWeights
const ConstTensor * m_RecurrentToInputWeights
Definition: LstmParams.hpp:44
armnn::LstmDescriptor::m_ClippingThresProj
float m_ClippingThresProj
Clipping threshold value for the projection.
Definition: Descriptors.hpp:1123
armnn::StridedSliceDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1342
armnn::Pooling2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:427
armnn::UnaryOperation::Rsqrt
@ Rsqrt
armnn::LstmInputParams::m_InputToInputWeights
const ConstTensor * m_InputToInputWeights
Definition: LstmParams.hpp:40
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::DepthwiseConvolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation factor value for height dimension.
Definition: Descriptors.hpp:706
armnn::ComparisonOperation::Less
@ Less
armnn::UnaryOperation::Sqrt
@ Sqrt
armnn::UnaryOperation::LogicalNot
@ LogicalNot
armnn::Pooling2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:413
armnn::LstmInputParams::m_RecurrentToOutputWeights
const ConstTensor * m_RecurrentToOutputWeights
Definition: LstmParams.hpp:47
armnn::Pooling2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:409
armnn::FullyConnectedDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:526
Logging.hpp
armnn::PadDescriptor
A PadDescriptor for the PadLayer.
Definition: Descriptors.hpp:1175
armnn::UnaryOperation::Exp
@ Exp
armnn::IOutputSlot::SetTensorInfo
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
armnn::TransposeDescriptor
A TransposeDescriptor for the TransposeLayer.
Definition: Descriptors.hpp:1469
armnn::Convolution3dDescriptor::m_DilationZ
uint32_t m_DilationZ
Dilation along z axis.
Definition: Descriptors.hpp:651
armnn::DetectionPostProcessDescriptor::m_NumClasses
uint32_t m_NumClasses
Number of classes.
Definition: Descriptors.hpp:755
armnn_driver::Model
::android::nn::Model Model
Helper classes.
Definition: ConversionUtils.hpp:43
armnn::EmptyOptional
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
armnn::StackDescriptor::m_Axis
uint32_t m_Axis
0-based axis along which to stack the input tensors.
Definition: Descriptors.hpp:1252
armnn::SliceDescriptor
A SliceDescriptor for the SliceLayer.
Definition: Descriptors.hpp:1207
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::TensorInfo::IsQuantized
bool IsQuantized() const
Definition: Tensor.cpp:504
armnn::LstmInputParams::m_InputGateBias
const ConstTensor * m_InputGateBias
Definition: LstmParams.hpp:51
armnn::DetectionPostProcessDescriptor::m_NmsIouThreshold
float m_NmsIouThreshold
Intersection over union threshold.
Definition: Descriptors.hpp:753
armnn::TensorInfo::IsTypeSpaceMatch
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:432
armnn::Convolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:582
armnn::ReshapeDescriptor
A ReshapeDescriptor for the ReshapeLayer.
Definition: Descriptors.hpp:1002
armnn::LstmDescriptor::m_HiddenStateScale
float m_HiddenStateScale
Hidden State quantization scale.
Definition: Descriptors.hpp:1145
armnnTfLiteParser::ITfLiteParserPtr
std::unique_ptr< ITfLiteParser, void(*)(ITfLiteParser *parser)> ITfLiteParserPtr
Definition: ITfLiteParser.hpp:24
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::UnaryOperation::Sin
@ Sin
CHECK_MODEL
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:182
armnnTfLiteParser::ITfLiteParser::CreateNetworkFromBinaryFile
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
Definition: TfLiteParser.cpp:73
armnn::PadDescriptor::m_PadValue
float m_PadValue
Optional value to use for padding, defaults to 0.
Definition: Descriptors.hpp:1200
armnn::LayerBindingId
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:303
armnn::DepthwiseConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:694
armnn::Convolution3dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:629
armnn::ActivationDescriptor::m_Function
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu,...
Definition: Descriptors.hpp:59
armnnTfLiteParser::TfLiteParserImpl::TensorIdRawPtrVector
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
Definition: TfLiteParser.hpp:38
armnn::NormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:805
armnn::LstmDescriptor::m_HiddenStateZeroPoint
int32_t m_HiddenStateZeroPoint
Hidden State zero point.
Definition: Descriptors.hpp:1143
armnn::BatchMatMulDescriptor
A BatchMatMulDescriptor for the BatchMatMul operator.
Definition: Descriptors.hpp:1563
armnn::Convolution3dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:643
armnn::ReduceOperation::Sum
@ Sum
armnn::IConnectableLayer::GetType
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
armnnTfLiteParser::TfLiteParserImpl::GetNetworkOutputBindingInfo
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...
Definition: TfLiteParser.cpp:5826
armnn::Convolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:584
Filesystem.hpp
armnnTfLiteParser::TfLiteParserImpl::LoadModel
armnn::INetworkPtr LoadModel(std::unique_ptr< tflite::ModelT > model)
Definition: TfLiteParser.cpp:965
armnn::GatherDescriptor::m_Axis
int32_t m_Axis
The axis in params to gather indices from.
Definition: Descriptors.hpp:960
armnn::Convolution3dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:641
armnn::SpaceToBatchNdDescriptor
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
Definition: Descriptors.hpp:1022
armnn::Convolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:572
armnn::PermutationVector
Definition: Types.hpp:308
armnnTfLiteParser::TfLiteParserImpl::GetVersion
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
Definition: TfLiteParser.cpp:5889
armnn::Convolution3dDescriptor
A Convolution3dDescriptor for the Convolution3dLayer.
Definition: Descriptors.hpp:588
armnn::ReshapeDescriptor::m_TargetShape
TensorShape m_TargetShape
Target shape value.
Definition: Descriptors.hpp:1018
armnn::TransposeConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:1458
armnn::StridedSliceDescriptor::m_EndMask
int32_t m_EndMask
End mask value.
Definition: Descriptors.hpp:1332
armnn::BaseTensor::GetInfo
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:295
ParserHelper.hpp
armnn::Pooling2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:407
Permute.hpp
armnn::ActivationFunction
ActivationFunction
Definition: Types.hpp:86
armnn::BoostLogSeverityMapping::info
@ info
CHECK_SUPPORTED_FUSED_ACTIVATION
#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:343
armnn::StackDescriptor::m_InputShape
TensorShape m_InputShape
Required shape of all input tensors.
Definition: Descriptors.hpp:1256
armnn::UnaryOperation
UnaryOperation
Definition: Types.hpp:124
armnn::Convolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:574
armnn::Convolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:568
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:198
armnnTfLiteParser::TfLiteParserImpl
Definition: TfLiteParser.hpp:26
armnn::DetectionPostProcessDescriptor::m_DetectionsPerClass
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
Definition: Descriptors.hpp:749
armnn::DetectionPostProcessDescriptor::m_ScaleH
float m_ScaleH
Center size encoding scale height.
Definition: Descriptors.hpp:765
armnn::DataType::Signed32
@ Signed32
armnn::UnaryOperation::Ceil
@ Ceil
armnn::LstmInputParams::m_InputLayerNormWeights
const ConstTensor * m_InputLayerNormWeights
Definition: LstmParams.hpp:57
armnn::ReduceDescriptor::m_KeepDims
bool m_KeepDims
if true then output shape has no change.
Definition: Descriptors.hpp:1533
armnn::BatchToSpaceNdDescriptor
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
Definition: Descriptors.hpp:875
armnn::Convolution2dDescriptor
A Convolution2dDescriptor for the Convolution2dLayer.
Definition: Descriptors.hpp:534
armnn::ReduceOperation::Prod
@ Prod
armnn::DepthwiseConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:698
armnn::ComparisonDescriptor
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:89
armnn::DataType::QAsymmS8
@ QAsymmS8
armnn::ComparisonDescriptor::m_Operation
ComparisonOperation m_Operation
Specifies the comparison operation to execute.
Definition: Descriptors.hpp:105
armnn::LstmDescriptor::m_OutputIntermediateScale
float m_OutputIntermediateScale
Output intermediate quantization scale.
Definition: Descriptors.hpp:1141
armnn::ElementwiseUnaryDescriptor::m_Operation
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
Definition: Descriptors.hpp:145
armnn::ArgMinMaxFunction::Min
@ Min
armnn::StridedSliceDescriptor::m_ShrinkAxisMask
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1.
Definition: Descriptors.hpp:1334
armnn::StandInDescriptor
A StandInDescriptor for the StandIn layer.
Definition: Descriptors.hpp:1260
armnn::Pooling2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:419
armnn::SpaceToDepthDescriptor::m_BlockSize
unsigned int m_BlockSize
Scalar specifying the input block size. It must be >= 1.
Definition: Descriptors.hpp:1071
armnn::LstmInputParams::m_ForgetLayerNormWeights
const ConstTensor * m_ForgetLayerNormWeights
Definition: LstmParams.hpp:58
armnn::UnaryOperation::Log
@ Log
armnn::ResizeDescriptor::m_TargetWidth
uint32_t m_TargetWidth
Target width value.
Definition: Descriptors.hpp:986
armnn::IOutputSlot::GetOwningIConnectableLayer
virtual const IConnectableLayer & GetOwningIConnectableLayer() const =0
armnn::StridedSliceDescriptor::m_Stride
std::vector< int > m_Stride
Stride values for the input that will be sliced.
Definition: Descriptors.hpp:1325
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputs
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5351
armnn::BackendOptions
Struct for the users to pass backend specific options.
Definition: BackendOptions.hpp:22
armnnDeserializer::ToTensorInfo
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
Definition: Deserializer.cpp:652
armnn::PermutationVector::GetSize
SizeType GetSize() const
Definition: Types.hpp:351
armnn::LstmDescriptor
An LstmDescriptor for the LstmLayer.
Definition: Descriptors.hpp:1081
armnn::ComparisonOperation
ComparisonOperation
Definition: Types.hpp:108
armnn::StridedSliceDescriptor
A StridedSliceDescriptor for the StridedSliceLayer.
Definition: Descriptors.hpp:1282
armnn::TransposeConvolution2dDescriptor::m_OutputShape
std::vector< unsigned int > m_OutputShape
Definition: Descriptors.hpp:1465
Tensor.hpp
armnnTfLiteParser::TfLiteParserImpl::ModelPtr
std::unique_ptr< tflite::ModelT > ModelPtr
Definition: TfLiteParser.hpp:30
armnn::ResizeDescriptor::m_AlignCorners
bool m_AlignCorners
Aligned corners.
Definition: Descriptors.hpp:995
armnn::TileDescriptor::m_Multiples
std::vector< uint32_t > m_Multiples
The vector to multiply the input shape by.
Definition: Descriptors.hpp:1635
armnnOnnxParser::CreateConstTensorImpl
std::pair< armnn::ConstTensor, std::unique_ptr< T[]> > CreateConstTensorImpl(const T *bufferPtr, armnn::TensorInfo &tensorInfo, const armnn::Optional< armnn::PermutationVector & > permutationVector)
Definition: OnnxParser.cpp:586
armnn::MeanDescriptor::m_Axis
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
Definition: Descriptors.hpp:1169
armnn::LstmDescriptor::m_CifgEnabled
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
Definition: Descriptors.hpp:1125
armnn::IOutputSlot::Connect
virtual int Connect(IInputSlot &destination)=0
armnn::NormalizationDescriptor::m_Alpha
float m_Alpha
Alpha value for the normalization equation.
Definition: Descriptors.hpp:799
armnn::GetUnaryOperationAsCString
constexpr char const * GetUnaryOperationAsCString(UnaryOperation operation)
Definition: TypesUtils.hpp:91
armnn::LstmDescriptor::m_ForgetIntermediateScale
float m_ForgetIntermediateScale
Forget intermediate quantization scale.
Definition: Descriptors.hpp:1137
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:191
armnn::Convolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
armnn::Convolution3dDescriptor::m_PadBack
uint32_t m_PadBack
Padding back value in the depth dimension.
Definition: Descriptors.hpp:639
armnn::ReduceDescriptor::m_vAxis
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
Definition: Descriptors.hpp:1535
armnn::PadDescriptor::m_PadList
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
Definition: Descriptors.hpp:1197
std
Definition: BackendId.hpp:149
armnn::ParseException
Definition: Exceptions.hpp:92
armnn::LstmDescriptor::m_LayerNormEnabled
bool m_LayerNormEnabled
Enable/disable layer normalization.
Definition: Descriptors.hpp:1131
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
armnn::TransposeConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:1452
armnn::TransposeConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:1450
armnnTfLiteParser::TfLiteParserImpl::OperatorPtr
std::unique_ptr< tflite::OperatorT > OperatorPtr
Definition: TfLiteParser.hpp:32
TFLITE_PARSER_VERSION
#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
TfLiteParser.hpp
armnn::LstmInputParams::m_OutputGateBias
const ConstTensor * m_OutputGateBias
Definition: LstmParams.hpp:54
BackendOptions.hpp
armnn::Convolution3dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:649
armnn::OriginsDescriptor
An OriginsDescriptor for the ConcatLayer.
Definition: Descriptors.hpp:201
armnn::LstmInputParams::m_ProjectionWeights
const ConstTensor * m_ProjectionWeights
Definition: LstmParams.hpp:55
armnn::LstmInputParams::m_InputToForgetWeights
const ConstTensor * m_InputToForgetWeights
Definition: LstmParams.hpp:41
armnn::IConnectableLayer::GetOutputSlot
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
armnn::TensorInfo::SetShape
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:193
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ElementwiseUnaryDescriptor
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:129
armnn::TransposeConvolution2dDescriptor
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
Definition: Descriptors.hpp:1419
armnn::IConnectableLayer::GetInputSlot
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
CHECK_VALID_SIZE
#define CHECK_VALID_SIZE(ACTUAL,...)
Definition: VerificationHelpers.hpp:32
armnn::Convolution3dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:653
armnn::ArgMinMaxDescriptor::m_Axis
int m_Axis
Axis to reduce across the input tensor.
Definition: Descriptors.hpp:83
armnn::ActivationDescriptor::m_B
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
Definition: Descriptors.hpp:63
armnn::Convolution3dDescriptor::m_StrideZ
uint32_t m_StrideZ
Stride value when proceeding through input for the depth dimension.
Definition: Descriptors.hpp:645
armnn::DetectionPostProcessDescriptor::m_UseRegularNms
bool m_UseRegularNms
Use Regular NMS.
Definition: Descriptors.hpp:757
armnn::LstmInputParamsInfo
Definition: LstmParams.hpp:63
armnn::ReduceOperation
ReduceOperation
Definition: Types.hpp:156
armnn::NormalizationDescriptor::m_K
float m_K
Kappa value used for the across channel normalization equation.
Definition: Descriptors.hpp:803
armnn::UnaryOperation::Abs
@ Abs
armnn::LstmDescriptor::m_ProjectionEnabled
bool m_ProjectionEnabled
Enable/disable the projection layer.
Definition: Descriptors.hpp:1129
armnn::StridedSliceDescriptor::m_End
std::vector< int > m_End
End values for the input that will be sliced.
Definition: Descriptors.hpp:1323
armnn::ReduceOperation::Min
@ Min
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphCount
size_t GetSubgraphCount() const
Return the number of subgraphs in the parsed model.
Definition: TfLiteParser.cpp:5858
armnn::PadDescriptor::m_PaddingMode
PaddingMode m_PaddingMode
Specifies the Padding mode (Constant, Reflect or Symmetric)
Definition: Descriptors.hpp:1203
CHECK_SUBGRAPH
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
Definition: TfLiteParser.cpp:140
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:327
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:80
armnn::IInputSlot
An input connection slot for a layer.
Definition: INetwork.hpp:25
armnn::Pooling2dDescriptor::m_OutputShapeRounding
OutputShapeRounding m_OutputShapeRounding
The rounding method for the output shape. (Floor, Ceiling).
Definition: Descriptors.hpp:423
armnn::PoolingAlgorithm
PoolingAlgorithm
Definition: Types.hpp:149
armnn::TransposeConvolution2dDescriptor::m_OutputShapeEnabled
bool m_OutputShapeEnabled
Output shape if it has been specified.
Definition: Descriptors.hpp:1464
armnn::TransposeConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:1460
armnn::DetectionPostProcessDescriptor
Definition: Descriptors.hpp:713
armnn::Convolution3dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NDHWC, NCDHW).
Definition: Descriptors.hpp:655
armnn::TensorInfo::SetConstant
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:514
armnn::DataType::Signed64
@ Signed64
armnn::TransposeConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1462
armnnTfLiteParser
Definition: ITfLiteParser.hpp:17
armnn::Pooling2dDescriptor
A Pooling2dDescriptor for the Pooling2dLayer.
Definition: Descriptors.hpp:371
armnn::LstmDescriptor::m_ActivationFunc
uint32_t m_ActivationFunc
The activation function to use.
Definition: Descriptors.hpp:1119
armnn::LstmInputParamsInfo::m_InputToForgetWeights
const TensorInfo * m_InputToForgetWeights
Definition: LstmParams.hpp:90
armnn::BatchToSpaceNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:902
armnn::TensorInfo::GetQuantizationOffset
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:478
armnn::DepthwiseConvolution2dDescriptor
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
Definition: Descriptors.hpp:659
armnn::ComparisonOperation::Equal
@ Equal
armnn::ReduceDescriptor
A ReduceDescriptor for the REDUCE operators.
Definition: Descriptors.hpp:1517
armnn::DepthwiseConvolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation factor value for width dimension.
Definition: Descriptors.hpp:704
armnn::LstmDescriptor::m_ClippingThresCell
float m_ClippingThresCell
Clipping threshold value for the cell state.
Definition: Descriptors.hpp:1121
armnnTfLiteParser::TfLiteParserImpl::GetBuffer
static BufferRawPtr GetBuffer(const ModelPtr &model, size_t bufferIndex)
Definition: TfLiteParser.cpp:5616
armnn::NullPointerException
Definition: Exceptions.hpp:146
armnnUtils::ToFloatArray
std::unique_ptr< float[]> ToFloatArray(const std::vector< PrimitiveType > &data, const armnn::TensorInfo &tensorInfo)
Definition: TensorUtils.cpp:273
armnn::LstmInputParams
Definition: LstmParams.hpp:13
armnn::LstmInputParams::m_CellLayerNormWeights
const ConstTensor * m_CellLayerNormWeights
Definition: LstmParams.hpp:59
armnn::StridedSliceDescriptor::m_NewAxisMask
int32_t m_NewAxisMask
New axis mask value.
Definition: Descriptors.hpp:1339
CHECK_BUFFER
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
Definition: TfLiteParser.cpp:251
armnn::GetComparisonOperationAsCString
constexpr char const * GetComparisonOperationAsCString(ComparisonOperation operation)
Definition: TypesUtils.hpp:61
armnn::MeanDescriptor
A MeanDescriptor for the MeanLayer.
Definition: Descriptors.hpp:1151
armnn::CheckLocation::FileLine
std::string FileLine() const
Definition: Exceptions.hpp:37
armnnTfLiteParser::TfLiteParserImpl::GetInputTensorIds
static std::vector< int32_t > & GetInputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5384
armnn::OptionalReferenceSwitch< std::is_reference< T >::value, T >::value
const T & value() const
Definition: Optional.hpp:146
armnn::TileDescriptor
Definition: Descriptors.hpp:1619
armnn::SoftmaxDescriptor
A SoftmaxDescriptor for the SoftmaxLayer.
Definition: Descriptors.hpp:177
armnn::Pooling2dDescriptor::m_PoolType
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
Definition: Descriptors.hpp:405
armnnTfLiteParser::ITfLiteParser
Definition: ITfLiteParser.hpp:26
armnn::SpaceToDepthDescriptor
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
Definition: Descriptors.hpp:1054
armnn::OptionalBase::has_value
bool has_value() const noexcept
Definition: Optional.hpp:53
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnn::ReduceOperation::Max
@ Max
armnn::LstmDescriptor::m_CellIntermediateScale
float m_CellIntermediateScale
Cell intermediate quantization scale.
Definition: Descriptors.hpp:1139
armnnTfLiteParser::ComputeWrappedIndex
unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
Definition: TfLiteParser.cpp:4378
armnnTfLiteParser::TfLiteParserImpl::OutputShapeOfSqueeze
static armnn::TensorInfo OutputShapeOfSqueeze(std::vector< uint32_t > squeezeDims, const armnn::TensorInfo &inputTensorInfo)
Definition: TfLiteParser.cpp:2312
armnn::ComparisonOperation::Greater
@ Greater
armnn::DepthwiseConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:700
armnn::DepthwiseConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:696