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