ArmNN
 24.05
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("Div:{}:{}", subgraphIndex, operatorIndex);
2752  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Div, 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 =
3477  TfLiteParserImpl::OutputShapeOfReshape(inputTensorInfo, targetShape);
3478 
3479  // Check for valid input size and that reshape parameters equal output shape
3480  // The output shape can be provided to us in 2 ways:
3481  // 1. through the normal 'shape' parameter given by outputs[indx]->shape
3482  // 2. through additional parameter 'shape_signature' given by outputs[indx]->buffer.
3483  // This parameter can sometimes contain -1 value not visible in the 'shape' parameter.
3484  const armnn::TensorShape& reshapeOutputTensorShape = reshapeOutputTensorInfo.GetShape();
3485  if (inputs.size() > 1 && !CheckShape(reshapeOutputTensorShape, outputs[0]->shape))
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  break;
3578  }
3579  case ResizeMethod::NearestNeighbor:
3580  {
3581  layerName += fmt::format("NEARESTNEIGHBOR:{}:{}", subgraphIndex, operatorIndex);
3582  break;
3583  }
3584  default:
3585  {
3586  throw ParseException(
3587  fmt::format("Unexpected ResizeMethod[{}] when creating layerName {} ",
3588  static_cast<int>(resizeMethod), CHECK_LOCATION().AsString()));
3589  }
3590  }
3591 
3592  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3593 
3594  IConnectableLayer* layer = m_Network->AddResizeLayer(desc, layerName.c_str());
3595 
3596  if (!layer)
3597  {
3598  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3599  operatorIndex, CHECK_LOCATION().AsString()));
3600  }
3601 
3602  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
3603  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
3604  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3605 
3606  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3607  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3608 
3609  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3610  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
3611 }
3612 
3613 void TfLiteParserImpl::ParseReverseV2(size_t subgraphIndex, size_t operatorIndex)
3614 {
3615  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3616 
3617  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3618  CHECK_VALID_SIZE(inputs.size(), 2);
3619 
3620  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3621  CHECK_VALID_SIZE(outputs.size(), 1);
3622 
3623  auto layerName = fmt::format("ReverseV2:{}:{}", subgraphIndex, operatorIndex);
3624 
3625  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3626  TensorInfo axisTensorInfo = ToTensorInfo(inputs[1]);
3627  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3628 
3629  IConnectableLayer* layer = m_Network->AddReverseV2Layer(layerName.c_str());
3630  ARMNN_ASSERT(layer != nullptr);
3631 
3632  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3633 
3634  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3635  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3636 
3637  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3638  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3639 }
3640 
3641 void TfLiteParserImpl::ParseTile(size_t subgraphIndex, size_t operatorIndex)
3642 {
3643  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3644 
3645  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3646  CHECK_VALID_SIZE(inputs.size(), 2);
3647 
3648  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3649  CHECK_VALID_SIZE(outputs.size(), 1);
3650 
3651  TensorInfo inputTensorInfo = ToTensorInfo(inputs[0]);
3652  TensorInfo multiplesTensorInfo = ToTensorInfo(inputs[1]);
3653  TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
3654 
3655  auto layerName = fmt::format("Tile:{}:{}", subgraphIndex, operatorIndex);
3656 
3657  TileDescriptor descriptor;
3658 
3659  BufferRawPtr multiplesBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
3660  if (multiplesBufferPtr != nullptr)
3661  {
3662  std::vector<int32_t> multiplesData(multiplesTensorInfo.GetNumElements());
3663  ::memcpy(multiplesData.data(), multiplesBufferPtr->data.data(), multiplesTensorInfo.GetNumBytes());
3664  descriptor.m_Multiples.assign(multiplesData.begin(), multiplesData.end());
3665  }
3666  else
3667  {
3668  ARMNN_THROW_PARSE_EXCEPTION("For Tile layer, Multiples data was not found in the buffer.");
3669  }
3670 
3671  IConnectableLayer* layer = m_Network->AddTileLayer(descriptor, layerName.c_str());
3672  ARMNN_ASSERT(layer != nullptr);
3673 
3674  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3675 
3676  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3677  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
3678 
3679  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3680  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3681 }
3682 
3683 void TfLiteParserImpl::ParseConcatenation(size_t subgraphIndex, size_t operatorIndex)
3684 {
3685  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3686 
3687  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3688  const auto* options = operatorPtr->builtin_options.AsConcatenationOptions();
3689 
3690  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3691 
3692  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3693  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3694  auto inputTensorIds = GetInputTensorIds(m_Model, subgraphIndex, operatorIndex);
3695 
3696  CHECK_VALID_SIZE(outputs.size(), 1);
3697 
3698  unsigned int numConcatView = static_cast<unsigned int>(inputs.size());
3699  uint32_t inputRank = InputTensorInfo(subgraphIndex, operatorIndex, 0).GetNumDimensions();
3700 
3701  const unsigned int concatDimInput = static_cast<unsigned int>(
3702  (static_cast<int>(inputRank) + options->axis) % static_cast<int>(inputRank));
3703 
3704  OriginsDescriptor concatDescriptor(static_cast<uint32_t>(numConcatView), inputRank);
3705  concatDescriptor.SetConcatAxis(concatDimInput);
3706  unsigned int mergeDimOrigin = 0;
3707 
3708  for (unsigned int viewIndex = 0; viewIndex < numConcatView; ++viewIndex)
3709  {
3710  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, viewIndex);
3711 
3712  // This set up concatDescriptor view origin
3714  inputTensorInfo, concatDescriptor, concatDimInput, viewIndex, mergeDimOrigin);
3715  }
3716 
3717  auto layerName = fmt::format("Concatenation:{}:{}", subgraphIndex, operatorIndex);
3718 
3719  IConnectableLayer* layer = m_Network->AddConcatLayer(concatDescriptor, layerName.c_str());
3720 
3721  if (!layer)
3722  {
3723  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3724  operatorIndex, CHECK_LOCATION().AsString()));
3725  }
3726 
3727  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
3728  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3729 
3730  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3731  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
3732 
3733  // add fused activation layer
3734  layer = AddFusedActivationLayer(layer, 0, options->fused_activation_function);
3735 
3736  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3737  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
3738 }
3739 
3740 void TfLiteParserImpl::ParseFullyConnected(size_t subgraphIndex, size_t operatorIndex)
3741 {
3742  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3743 
3744  const auto& operatorRfr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3745  const auto options = operatorRfr->builtin_options.AsFullyConnectedOptions();
3746 
3747  CHECK_SUPPORTED_FUSED_ACTIVATION(options, subgraphIndex, operatorIndex);
3748 
3750  desc.m_BiasEnabled = false;
3751  desc.m_TransposeWeightMatrix = true;
3752 
3753  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3754  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3755  CHECK_VALID_SIZE(outputs.size(), 1);
3756 
3757  armnn::TensorInfo filterTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
3758 
3759  // Fully Connected Layer accepts two dimensional weights input
3760  int32_t weightsDimension = static_cast<int32_t>(filterTensorInfo.GetNumDimensions());
3761  if (weightsDimension != 2)
3762  {
3763  throw ParseException(
3764  fmt::format("Dimension {} for Fully Connected weights is not supported by Armnn. "
3765  "Node {}",
3766  weightsDimension,
3767  CHECK_LOCATION().AsString()));
3768  }
3769 
3770  armnn::IConnectableLayer* layer = nullptr;
3771  auto layerName = fmt::format("FullyConnected:{}:{}", subgraphIndex, operatorIndex);
3772 
3773  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3774  // Add the first input tensor to the registration list
3775  std::vector<unsigned int> tensorIndexesToRegister = {inputTensorIndexes[0]};
3776  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3777 
3778  desc.m_ConstantWeights = IsConstTensor(inputs[1]);
3779 
3780  // Add the weights input to the registration list, constant layers will be added by SetupConstantLayers if constant.
3781  tensorIndexesToRegister.emplace_back(inputTensorIndexes[1]);
3782 
3783  if (ShouldConstantTensorBeConverted(inputs[1], inputTensorInfo.GetDataType(), filterTensorInfo.GetDataType()))
3784  {
3785  m_ConstantsToDequantize.emplace_back(inputs[1]->buffer);
3786  }
3787 
3788  if (inputs.size() == 3)
3789  {
3790  desc.m_BiasEnabled = true;
3791  armnn::TensorInfo biasTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3792 
3793  // Add the biases input to the registration list, constant layer will be added by SetupConstantLayers.
3794  tensorIndexesToRegister.emplace_back(inputTensorIndexes[2]);
3795 
3796  if (ShouldConstantTensorBeConverted(inputs[2], inputTensorInfo.GetDataType(), biasTensorInfo.GetDataType()))
3797  {
3798  m_ConstantsToDequantize.emplace_back(inputs[2]->buffer);
3799  }
3800  }
3801 
3802  // Filters and biases are always passed to fully connected as inputs
3803  layer = m_Network->AddFullyConnectedLayer(desc, layerName.c_str());
3804 
3805  if (!layer)
3806  {
3807  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3808  operatorIndex, CHECK_LOCATION().AsString()));
3809  }
3810 
3811  unsigned int startingSlotIndex = 0;
3812  if (inputTensorInfo.GetNumDimensions() > 2)
3813  {
3814  // Add reshape to flatten to 2D [batch_size, input_size],
3815  // where "input_size" corresponds to the number of inputs to the layer,
3816  // matching the second dimension of weights,
3817  // and "batch_size" is calculated by dividing the number of elements by "input_size".
3818  std::vector<unsigned int> reshapedDimensions(2);
3819  reshapedDimensions[1] = filterTensorInfo.GetShape()[1];
3820  reshapedDimensions[0] = inputTensorInfo.GetNumElements() / reshapedDimensions[1];
3821 
3822  if (inputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3823  {
3824  throw ParseException(
3825  fmt::format("Failed to deduce input tensor shape from filter size {} {}",
3826  reshapedDimensions[1],
3827  CHECK_LOCATION().AsString()));
3828  }
3829 
3830  armnn::TensorInfo reshapedTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3831  reshapedTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3832  inputTensorInfo = reshapedTensorInfo;
3833 
3834  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
3835  armnn::ReshapeDescriptor reshapeDescriptor;
3836  reshapeDescriptor.m_TargetShape = reshapedTensorInfo.GetShape();
3837  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(reshapeDescriptor,
3838  reshapeLayerName.c_str());
3839 
3840  reshapeLayer->GetOutputSlot(0).SetTensorInfo(reshapedTensorInfo);
3841  reshapeLayer->GetOutputSlot(0).Connect(layer->GetInputSlot(0));
3842 
3843  RegisterInputSlots(subgraphIndex, operatorIndex, reshapeLayer, {inputTensorIndexes[0]});
3844  // Fc layer connects to the reshape layer, so we skip the first input slot when registering fc's input slots
3845  tensorIndexesToRegister.erase(tensorIndexesToRegister.begin());
3846  startingSlotIndex = 1;
3847  }
3848 
3849  RegisterInputSlots(subgraphIndex, operatorIndex, layer, tensorIndexesToRegister, startingSlotIndex);
3850 
3851  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromShapes(subgraphIndex, operatorIndex, layer, 0,
3852  { inputTensorInfo.GetShape(),
3853  filterTensorInfo.GetShape() });
3854 
3855  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
3856 
3857  if (outputTensorInfo.GetNumDimensions() > 2)
3858  {
3859  // Calculate reshape to flatten to 2D [batch_size, input_size]
3860  std::vector<unsigned int> reshapedDimensions(2);
3861  reshapedDimensions[1] = filterTensorInfo.GetShape()[0];
3862  reshapedDimensions[0] = outputTensorInfo.GetNumElements() / reshapedDimensions[1];
3863  armnn::TensorInfo reshapedOutputTensorInfo = outputTensorInfo;
3864  if (outputTensorInfo.GetNumElements() % reshapedDimensions[1] != 0)
3865  {
3866  throw ParseException(
3867  fmt::format("Failed to deduce output tensor shape from filter size {} {}",
3868  reshapedDimensions[1],
3869  CHECK_LOCATION().AsString()));
3870  }
3871  reshapedOutputTensorInfo.SetShape(armnn::TensorShape{ 2, reshapedDimensions.data() });
3872  layer->GetOutputSlot(0).SetTensorInfo(reshapedOutputTensorInfo);
3873 
3874  std::string reshapeLayerName = fmt::format("ExpandDims:{}:{}", subgraphIndex, operatorIndex);
3875  layer = AddReshapeLayer(layer, 0, reshapeLayerName, outputTensorInfo);
3876  }
3877 
3878  // we need to add the activation layer and fortunately we don't need to care about the data layout
3879  armnn::IConnectableLayer* fusedActivationLayer = AddFusedActivationLayer(layer, 0,
3880  options->fused_activation_function);
3881 
3882  // register the output connection slots for the layer, connections are made after all layers have been created
3883  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3884  RegisterOutputSlots(subgraphIndex, operatorIndex, fusedActivationLayer, {outputTensorIndexes[0]});
3885 
3886  m_TensorInfos[outputTensorIndexes[0]] = layer->GetOutputSlot(0).GetTensorInfo();
3887 }
3888 
3889 void TfLiteParserImpl::ParseDetectionPostProcess(size_t subgraphIndex, size_t operatorIndex)
3890 {
3891  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3892 
3893  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3894 
3895  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3896  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3897  CHECK_VALID_SIZE(outputs.size(), 4);
3898 
3899  // Obtain custom options from flexbuffers
3900  auto custom_options = operatorPtr->custom_options;
3901  const flexbuffers::Map& m = flexbuffers::GetRoot(custom_options.data(), custom_options.size()).AsMap();
3902 
3903  // Obtain descriptor information from tf lite
3905  desc.m_MaxDetections = m["max_detections"].AsUInt32();
3906  desc.m_MaxClassesPerDetection = m["max_classes_per_detection"].AsUInt32();
3907  desc.m_NmsScoreThreshold = m["nms_score_threshold"].AsFloat();
3908  desc.m_NmsIouThreshold = m["nms_iou_threshold"].AsFloat();
3909  desc.m_NumClasses = m["num_classes"].AsUInt32();
3910  desc.m_ScaleH = m["h_scale"].AsFloat();
3911  desc.m_ScaleW = m["w_scale"].AsFloat();
3912  desc.m_ScaleX = m["x_scale"].AsFloat();
3913  desc.m_ScaleY = m["y_scale"].AsFloat();
3914 
3915  if (!(m["use_regular_nms"].IsNull()))
3916  {
3917  desc.m_UseRegularNms = m["use_regular_nms"].AsBool();
3918  }
3919  if (!(m["detections_per_class"].IsNull()))
3920  {
3921  desc.m_DetectionsPerClass = m["detections_per_class"].AsUInt32();
3922  }
3923 
3924  if (desc.m_NmsIouThreshold <= 0.0f || desc.m_NmsIouThreshold > 1.0f)
3925  {
3926  throw InvalidArgumentException("DetectionPostProcessTFLiteParser: Intersection over union threshold "
3927  "must be positive and less than or equal to 1.");
3928  }
3929 
3930  armnn::TensorInfo anchorTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 2);
3931  auto anchorTensorAndData = CreateConstTensorNonPermuted(inputs[2], anchorTensorInfo);
3932 
3933  auto layerName = fmt::format("DetectionPostProcess:{}:{}", subgraphIndex, operatorIndex);
3934  IConnectableLayer* layer = m_Network->AddDetectionPostProcessLayer(desc, anchorTensorAndData,
3935  layerName.c_str());
3936 
3937  if (!layer)
3938  {
3939  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
3940  operatorIndex, CHECK_LOCATION().AsString()));
3941  }
3942 
3943  // The model does not specify the output shapes.
3944  // The output shapes are calculated from the max_detection and max_classes_per_detection.
3945  unsigned int numDetectedBox = desc.m_MaxDetections * desc.m_MaxClassesPerDetection;
3946  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox, 4 });
3947  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3948  m_OverriddenOutputShapes.push_back({ 1, numDetectedBox });
3949  m_OverriddenOutputShapes.push_back({ 1 });
3950 
3951  for (unsigned int i = 0 ; i < outputs.size() ; ++i)
3952  {
3953  armnn::TensorInfo detectionBoxOutputTensorInfo = ToTensorInfo(outputs[i], m_OverriddenOutputShapes[i]);
3954  layer->GetOutputSlot(i).SetTensorInfo(detectionBoxOutputTensorInfo);
3955  }
3956 
3957  // Register the input connection slots for the layer, connections are made after all layers have been created
3958  // only the tensors for the inputs are relevant, exclude the const tensors
3959  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
3960  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
3961 
3962  // Register the output connection slots for the layer, connections are made after all layers have been created
3963  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
3964  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0],
3965  outputTensorIndexes[1],
3966  outputTensorIndexes[2],
3967  outputTensorIndexes[3]});
3968 }
3969 
3970 /// The TfLite Pack operator is equivalent to the ArmNN Stack operator
3971 void TfLiteParserImpl::ParsePack(size_t subgraphIndex, size_t operatorIndex)
3972 {
3973  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
3974 
3975  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
3976  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
3977  CHECK_VALID_SIZE(outputs.size(), 1);
3978 
3979  if (inputs.size() < 1)
3980  {
3981  throw ParseException("Pack must have at least one input.");
3982  }
3983 
3984  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
3985  const auto* options = operatorPtr->builtin_options.AsPackOptions();
3986 
3987  StackDescriptor desc;
3988  desc.m_Axis = static_cast<uint32_t>(options->axis);
3989  desc.m_NumInputs = static_cast<uint32_t>(inputs.size());
3990 
3991  // Use the tensor shape of the first input as the "correct" input shape in the descriptor
3992  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
3993  desc.m_InputShape = inputTensorInfo.GetShape();
3994 
3995  auto layerName = fmt::format("Pack:{}:{}", subgraphIndex, operatorIndex);
3996  IConnectableLayer* layer = m_Network->AddStackLayer(desc, layerName.c_str());
3997 
3998  if (!layer)
3999  {
4000  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4001  operatorIndex, CHECK_LOCATION().AsString()));
4002  }
4003 
4004  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {});
4005  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4006 
4007  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4008  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes});
4009 
4010  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4011  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4012 }
4013 
4014 void TfLiteParserImpl::ParseUnidirectionalSequenceLSTM(size_t subgraphIndex, size_t operatorIndex)
4015 {
4016  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4017 
4018  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4019  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4020 
4021  if (inputs.size() < 2)
4022  {
4023  throw ParseException("UnidirectionalSequenceLSTM must have at least 2 input.");
4024  }
4025 
4026  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4027  const auto& subgraphPtr = m_Model->subgraphs[subgraphIndex];
4028  const auto nodeParams = operatorPtr->builtin_options.AsUnidirectionalSequenceLSTMOptions();
4029  CHECK_SUPPORTED_FUSED_ACTIVATION(nodeParams, subgraphIndex, operatorIndex);
4030  auto inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4031  auto outputTensorInfo = ToTensorInfo(outputs[0]);
4032 
4033  // Set the params structure for the AddUnidirectionalSequenceLstmLayer call
4034  // Please refer to each operand at
4035  // https://www.tensorflow.org/mlir/tfl_ops#tflunidirectional_sequence_lstm_tflunidirectionalsequencelstmop
4036  armnn::LstmInputParams params;
4037 
4038  if (IsOptionalOperandPresent(operatorPtr->inputs[1]))
4039  {
4040  params.m_InputToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[1]].get(),
4041  inputTensorInfo).first;
4042  }
4043 
4044  params.m_InputToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[2]].get(),
4045  inputTensorInfo).first;
4046  params.m_InputToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[3]].get(),
4047  inputTensorInfo).first;
4048  params.m_InputToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[4]].get(),
4049  inputTensorInfo).first;
4050 
4051  // Recurrent weight tensors of size {n_cell, n_output}
4052  if (IsOptionalOperandPresent(operatorPtr->inputs[5]))
4053  {
4054  params.m_RecurrentToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[5]].get(),
4055  inputTensorInfo).first;
4056  }
4057 
4058  params.m_RecurrentToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[6]].get(),
4059  inputTensorInfo).first;
4060  params.m_RecurrentToCellWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[7]].get(),
4061  inputTensorInfo).first;
4062  params.m_RecurrentToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[8]].get(),
4063  inputTensorInfo).first;
4064 
4065  // Peephole weights tensors of size {n_cell}, representing a diagonal matrix.
4066  if (IsOptionalOperandPresent(operatorPtr->inputs[9]))
4067  {
4068  params.m_CellToInputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[9]].get(),
4069  inputTensorInfo).first;
4070  }
4071 
4072  if (IsOptionalOperandPresent(operatorPtr->inputs[10]))
4073  {
4074  params.m_CellToForgetWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[10]].get(),
4075  inputTensorInfo).first;
4076  }
4077 
4078  if (IsOptionalOperandPresent(operatorPtr->inputs[11]))
4079  {
4080  params.m_CellToOutputWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[11]].get(),
4081  inputTensorInfo).first;
4082  }
4083 
4084  // Gates bias tensors of size {n_cell}
4085  if (IsOptionalOperandPresent(operatorPtr->inputs[12]))
4086  {
4087  params.m_InputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[12]].get(),
4088  inputTensorInfo).first;
4089  }
4090 
4091  params.m_ForgetGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[13]].get(),
4092  inputTensorInfo).first;
4093  params.m_CellBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[14]].get(),
4094  inputTensorInfo).first;
4095  params.m_OutputGateBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[15]].get(),
4096  inputTensorInfo).first;
4097 
4098  // Projection weight tensor of size {n_output, n_cell}
4099  if (IsOptionalOperandPresent(operatorPtr->inputs[16]))
4100  {
4101  params.m_ProjectionWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[16]].get(),
4102  inputTensorInfo).first;
4103  }
4104  // Projection bias tensor of size {n_output}
4105  if (IsOptionalOperandPresent(operatorPtr->inputs[17]))
4106  {
4107  params.m_ProjectionBias = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[17]].get(),
4108  inputTensorInfo).first;
4109  }
4110 
4111  // These state tensors are defined as variable tensors, and will be modified by this op.
4112  armnn::TensorInfo outputStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[18]].get());
4113  m_ConstantsToBeCreated.push_back(operatorPtr->inputs[18]);
4114  armnn::TensorInfo cellStateInInfo = ToTensorInfo(subgraphPtr->tensors[operatorPtr->inputs[19]].get());
4115  m_ConstantsToBeCreated.push_back(operatorPtr->inputs[19]);
4116 
4117  // Layer norm coefficient tensors of size {n_cell}, representing a diagonal matrix.
4118  if (inputs.size() >= 21 && IsOptionalOperandPresent(operatorPtr->inputs[20]))
4119  {
4120  params.m_InputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[20]].get(),
4121  inputTensorInfo).first;
4122  }
4123 
4124  if (inputs.size() >= 22 && IsOptionalOperandPresent(operatorPtr->inputs[21]))
4125  {
4126  params.m_ForgetLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[21]].get(),
4127  inputTensorInfo).first;
4128  }
4129 
4130  if (inputs.size() >= 23 && IsOptionalOperandPresent(operatorPtr->inputs[22]))
4131  {
4132  params.m_CellLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[22]].get(),
4133  inputTensorInfo).first;
4134  }
4135 
4136  if (inputs.size() >= 24 && IsOptionalOperandPresent(operatorPtr->inputs[23]))
4137  {
4138  params.m_OutputLayerNormWeights = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->inputs[23]].get(),
4139  inputTensorInfo).first;
4140  }
4141 
4142  // set the layer descriptor
4144  desc.m_ActivationFunc = nodeParams->fused_activation_function;
4145  desc.m_ClippingThresCell = nodeParams->cell_clip;
4146  desc.m_ClippingThresProj = nodeParams->proj_clip;
4147  desc.m_CifgEnabled = (params.m_InputToInputWeights == nullptr
4148  || params.m_RecurrentToInputWeights == nullptr
4149  || params.m_InputGateBias == nullptr);
4150  desc.m_PeepholeEnabled = (params.m_CellToForgetWeights != nullptr || params.m_CellToOutputWeights != nullptr);
4151  desc.m_ProjectionEnabled = (params.m_ProjectionWeights != nullptr);
4152  desc.m_LayerNormEnabled = (params.m_InputLayerNormWeights != nullptr
4153  || params.m_ForgetLayerNormWeights != nullptr
4154  || params.m_CellLayerNormWeights != nullptr
4155  || params.m_OutputLayerNormWeights != nullptr);
4156  desc.m_TimeMajor = nodeParams->time_major;
4157 
4158  if (operatorPtr->intermediates.size() > 3 && desc.m_LayerNormEnabled)
4159  {
4160  auto inputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[0]].get(),
4161  inputTensorInfo).first;
4162  auto inputIntermediateTensorInfo = inputIntermediate->GetInfo();
4163  desc.m_InputIntermediateScale = inputIntermediateTensorInfo.GetQuantizationScale();
4164 
4165  auto forgetIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[1]].get(),
4166  inputTensorInfo).first;
4167  auto forgetIntermediateTensorInfo = forgetIntermediate->GetInfo();
4168  desc.m_ForgetIntermediateScale = forgetIntermediateTensorInfo.GetQuantizationScale();
4169 
4170  auto cellIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[2]].get(),
4171  inputTensorInfo).first;
4172  auto cellIntermediateTensorInfo = cellIntermediate->GetInfo();
4173  desc.m_CellIntermediateScale = cellIntermediateTensorInfo.GetQuantizationScale();
4174 
4175  auto outputIntermediate = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[3]].get(),
4176  inputTensorInfo).first;
4177  auto outputIntermediateTensorInfo = outputIntermediate->GetInfo();
4178  desc.m_OutputIntermediateScale = outputIntermediateTensorInfo.GetQuantizationScale();
4179  }
4180  else
4181  {
4182  float defaultIntermediate = std::pow(2, -12);
4183  desc.m_InputIntermediateScale = defaultIntermediate;
4184  desc.m_ForgetIntermediateScale = defaultIntermediate;
4185  desc.m_CellIntermediateScale = defaultIntermediate;
4186  desc.m_OutputIntermediateScale = defaultIntermediate;
4187  }
4188 
4189  if (operatorPtr->intermediates.size() > 4)
4190  {
4191  auto hiddentensor = CreateConstTensorPtr(subgraphPtr->tensors[operatorPtr->intermediates[4]].get(),
4192  inputTensorInfo).first;
4193 
4194  desc.m_HiddenStateScale = hiddentensor->GetInfo().GetQuantizationScale();
4195  desc.m_HiddenStateZeroPoint = hiddentensor->GetInfo().GetQuantizationOffset();
4196  }
4197  unsigned int batchSize = desc.m_TimeMajor ? inputTensorInfo.GetShape()[1] : inputTensorInfo.GetShape()[0];
4198  unsigned int outputSize = outputTensorInfo.GetShape()[2];
4199  unsigned int numUnits = cellStateInInfo.GetShape()[1];
4200 
4201  armnn::DataType dataType = inputTensorInfo.GetDataType();
4202  float qScale = inputTensorInfo.GetQuantizationScale();
4203  float qOffset = inputTensorInfo.GetQuantizationOffset();
4204 
4205  armnn::TensorInfo scratchBufferTensorInfo({batchSize, numUnits * 3}, dataType, qScale, qOffset);
4206  if (!desc.m_CifgEnabled)
4207  {
4208  scratchBufferTensorInfo = armnn::TensorInfo({batchSize, numUnits * 4}, dataType, qScale, qOffset);
4209  }
4210  armnn::TensorInfo cellStateOutTensorInfo({batchSize, numUnits},
4211  cellStateInInfo.GetDataType(),
4212  cellStateInInfo.GetQuantizationScale(),
4213  cellStateInInfo.GetQuantizationOffset());
4214  armnn::TensorInfo outputStateOutTensorInfo({batchSize, outputSize}, dataType, qScale, qOffset);
4215 
4216  armnn::LstmInputParamsInfo paramsInfo;
4217  paramsInfo.m_InputToForgetWeights = &(params.m_InputToForgetWeights->GetInfo());
4218  paramsInfo.m_InputToCellWeights = &(params.m_InputToCellWeights->GetInfo());
4219  paramsInfo.m_InputToOutputWeights = &(params.m_InputToOutputWeights->GetInfo());
4220  paramsInfo.m_RecurrentToForgetWeights = &(params.m_RecurrentToForgetWeights->GetInfo());
4221  paramsInfo.m_RecurrentToCellWeights = &(params.m_RecurrentToCellWeights->GetInfo());
4222  paramsInfo.m_RecurrentToOutputWeights = &(params.m_RecurrentToOutputWeights->GetInfo());
4223  paramsInfo.m_ForgetGateBias = &(params.m_ForgetGateBias->GetInfo());
4224  paramsInfo.m_CellBias = &(params.m_CellBias->GetInfo());
4225  paramsInfo.m_OutputGateBias = &(params.m_OutputGateBias->GetInfo());
4226 
4227  if (!desc.m_CifgEnabled)
4228  {
4229  paramsInfo.m_InputToInputWeights = &(params.m_InputToInputWeights->GetInfo());
4230  paramsInfo.m_RecurrentToInputWeights = &(params.m_RecurrentToInputWeights->GetInfo());
4231  if (params.m_CellToInputWeights != nullptr)
4232  {
4233  paramsInfo.m_CellToInputWeights = &(params.m_CellToInputWeights->GetInfo());
4234  }
4235  paramsInfo.m_InputGateBias = &(params.m_InputGateBias->GetInfo());
4236  }
4237 
4238  if (desc.m_ProjectionEnabled)
4239  {
4240  paramsInfo.m_ProjectionWeights = &(params.m_ProjectionWeights->GetInfo());
4241  if (params.m_ProjectionBias != nullptr)
4242  {
4243  paramsInfo.m_ProjectionBias = &(params.m_ProjectionBias->GetInfo());
4244  }
4245  }
4246 
4247  if (desc.m_PeepholeEnabled)
4248  {
4249  paramsInfo.m_CellToForgetWeights = &(params.m_CellToForgetWeights->GetInfo());
4250  paramsInfo.m_CellToOutputWeights = &(params.m_CellToOutputWeights->GetInfo());
4251  }
4252 
4253  if (desc.m_LayerNormEnabled)
4254  {
4255  if(!desc.m_CifgEnabled)
4256  {
4257  paramsInfo.m_InputLayerNormWeights = &(params.m_InputLayerNormWeights->GetInfo());
4258  }
4259  paramsInfo.m_ForgetLayerNormWeights = &(params.m_ForgetLayerNormWeights->GetInfo());
4260  paramsInfo.m_CellLayerNormWeights = &(params.m_CellLayerNormWeights->GetInfo());
4261  paramsInfo.m_OutputLayerNormWeights = &(params.m_OutputLayerNormWeights->GetInfo());
4262  }
4263 
4264  auto layerName = fmt::format("UnidirectionalSequenceLSTM:{}:{}", subgraphIndex, operatorIndex);
4265  armnn::IConnectableLayer* layer = m_Network->AddUnidirectionalSequenceLstmLayer(desc, params);
4266 
4267  if (!layer)
4268  {
4269  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4270  operatorIndex, CHECK_LOCATION().AsString()));
4271  }
4272 
4273  // register the input connection slots for the layer, connections are made after all layers have been created
4274  // only the tensors for the inputs are relevant, exclude the const tensors
4275  auto inputTensorIndexes = AsUnsignedVector({operatorPtr->inputs[0],
4276  operatorPtr->inputs[18],
4277  operatorPtr->inputs[19]});
4278  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0],
4279  inputTensorIndexes[1],
4280  inputTensorIndexes[2]});
4281 
4282  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4283 
4284  layer->GetOutputSlot(0).SetTensorInfo(outputStateOutTensorInfo);
4285  layer->GetOutputSlot(1).SetTensorInfo(cellStateOutTensorInfo);
4286  layer->GetOutputSlot(2).SetTensorInfo(outputTensorInfo);
4287 
4288  unsigned int tensorIndex = outputTensorIndexes[0];
4289  armnn::IOutputSlot* slot = &(layer->GetOutputSlot(2));
4290  RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
4291 }
4292 
4293 void TfLiteParserImpl::ParseUnpack(size_t subgraphIndex, size_t operatorIndex)
4294 {
4295  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4296 
4297  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4298  const auto* options = operatorPtr->builtin_options.AsUnpackOptions();
4299 
4300  // This unpackAxis indicates the axis to unpack
4301  const unsigned int unpackAxis = CHECKED_NON_NEGATIVE(options->axis);
4302 
4303  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4304  CHECK_VALID_SIZE(inputs.size(), 1);
4305 
4306  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4307 
4308  if (unpackAxis >= inputTensorInfo.GetNumDimensions())
4309  {
4310  throw ParseException(
4311  fmt::format("The unpack axis: {} cannot be greater than or equal to "
4312  "the number of input dimension {} {}",
4313  unpackAxis,
4314  inputTensorInfo.GetNumDimensions(),
4315  CHECK_LOCATION().AsString()));
4316  }
4317 
4318  unsigned int unpackNum = CHECKED_NON_NEGATIVE(options->num);
4319  // If num is not defined, automatically infer from the length of the dimension axis.
4320  if(unpackNum == 0)
4321  {
4322  unpackNum = inputTensorInfo.GetShape()[unpackAxis];
4323  }
4324 
4325  // If unpack number cannot be inferred and is still zero, throw ParseException.
4326  if(unpackNum == 0)
4327  {
4328  throw ParseException("Number to unpack must greater than zero.");
4329  }
4330 
4331  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4332  CHECK_VALID_SIZE(outputs.size(), unpackNum);
4333 
4334  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4335  std::vector<unsigned int> unpackDimSizes(inputDimSize);
4336 
4337  // Add current input shape to unpackDimSizes
4338  for (unsigned int i = 0; i < inputDimSize; ++i)
4339  {
4340  unpackDimSizes[i] = inputTensorInfo.GetShape()[i];
4341  }
4342 
4343  if (unpackDimSizes[unpackAxis] != unpackNum)
4344  {
4345  throw ParseException("Number to unpack must be the same as length of the dimension to "
4346  "unpack along.");
4347  }
4348 
4349  unpackDimSizes[unpackAxis] /= unpackNum;
4350 
4351  SplitterDescriptor splitDesc(unpackNum, static_cast<unsigned int>(unpackDimSizes.size()));
4352  for (unsigned int j = 0; j < unpackNum; ++j)
4353  {
4354  // Set the size of the views.
4355  for (unsigned int dimIdx = 0; dimIdx < unpackDimSizes.size(); ++dimIdx)
4356  {
4357  splitDesc.SetViewSize(j, dimIdx, unpackDimSizes[dimIdx]);
4358  }
4359  splitDesc.SetViewOriginCoord(j, unpackAxis, unpackDimSizes[unpackAxis] * j);
4360  }
4361  splitDesc.SetAxis(unpackAxis);
4362  auto layerName = fmt::format("Unpack:{}:{}", subgraphIndex, operatorIndex);
4363  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4364 
4365  if (!layer)
4366  {
4367  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4368  operatorIndex, CHECK_LOCATION().AsString()));
4369  }
4370 
4371  TensorShape splitOutShape = TensorShape(static_cast<unsigned int>(unpackDimSizes.size()),
4372  unpackDimSizes.data());
4373 
4374  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4375  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4376 
4377  std::vector<unsigned int> reshapeDims;
4378  for (unsigned int axis = 0; axis < splitOutShape.GetNumDimensions(); ++axis)
4379  {
4380  if (axis != unpackAxis)
4381  {
4382  reshapeDims.push_back(splitOutShape[axis]);
4383  }
4384  }
4385 
4386  TensorShape reshapeOutputShape(splitOutShape.GetNumDimensions() -1, reshapeDims.data());
4387 
4388  // Create reshape to remove the unpacked dimension for unpack operator of each output from Splitter.
4389  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4390  {
4391  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[k], true);
4392  std::string reshapeLayerName = fmt::format("Reshape_for:{}", layer->GetName());
4394  desc.m_TargetShape = reshapeOutputShape;
4395  armnn::IConnectableLayer* reshapeLayer = m_Network->AddReshapeLayer(desc, layerName.c_str());
4396 
4397  layer->GetOutputSlot(k).SetTensorInfo(armnn::TensorInfo(splitOutShape,
4398  outputTensorInfo.GetDataType(),
4399  outputTensorInfo.GetQuantizationScale(),
4400  outputTensorInfo.GetQuantizationOffset()));
4401  layer->GetOutputSlot(k).Connect(reshapeLayer->GetInputSlot(0));
4402 
4403  reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4404 
4405  uint32_t reshapedOutputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[k]);
4406  armnn::IOutputSlot* slot = &(reshapeLayer->GetOutputSlot(0));
4407  RegisterProducerOfTensor(subgraphIndex, reshapedOutputId, slot);
4408  }
4409 }
4410 
4411 void TfLiteParserImpl::ParseSplit(size_t subgraphIndex, size_t operatorIndex)
4412 {
4413  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4414 
4415  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4416  const auto* options = operatorPtr->builtin_options.AsSplitOptions();
4417 
4418  const unsigned int numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4419 
4420  // If number of splits cannot be inferred and is zero, throw ParseException.
4421  if(numSplits == 0)
4422  {
4423  throw ParseException("Number to splits must greater than zero.");
4424  }
4425 
4426  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4427  CHECK_VALID_SIZE(inputs.size(), 2);
4428  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4429  CHECK_VALID_SIZE(outputs.size(), numSplits);
4430 
4431  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4432  armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4433 
4434  if (axisTensorInfo.GetNumElements() != 1)
4435  {
4436  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4437  CHECK_LOCATION().AsString()));
4438  }
4439 
4440  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
4441  if (axisBufferPtr == nullptr)
4442  {
4443  throw ParseException(
4444  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4445  CHECK_LOCATION().AsString()));
4446  }
4447 
4448  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4449  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4450  int32_t axis = axisData[0];
4451 
4452  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4453  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4454  {
4455  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4456  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4457  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4458  throw ParseException(
4459  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4460  axis,
4461  CHECK_LOCATION().AsString()));
4462  }
4463 
4464  const unsigned int splitDim = armnnUtils::GetUnsignedAxis(inputTensorInfo.GetNumDimensions(), axis);
4465 
4466  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4467  if (inputDimSize > MaxNumOfTensorDimensions)
4468  {
4469  throw ParseException(
4470  fmt::format("The number of dimensions: {} for input tensors of the split op cannot be greater than {} {}",
4471  inputTensorInfo.GetNumDimensions(),
4473  CHECK_LOCATION().AsString()));
4474  }
4475 
4476  std::vector<unsigned int> splitterDimSizes(inputDimSize);
4477 
4478  // Add current input shape to splitterDimSizes
4479  for (unsigned int i = 0; i < inputDimSize; ++i)
4480  {
4481  splitterDimSizes[i] = inputTensorInfo.GetShape()[i];
4482  }
4483 
4484  if (splitterDimSizes[splitDim] % numSplits != 0)
4485  {
4486  throw ParseException("Number of splits must evenly divide the dimension");
4487  }
4488  splitterDimSizes[splitDim] /= numSplits;
4489 
4490  SplitterDescriptor splitDesc(numSplits, inputDimSize);
4491  for (unsigned int j = 0; j < numSplits; ++j)
4492  {
4493  // Set the size of the views.
4494  for (unsigned int dimIdx = 0; dimIdx < splitterDimSizes.size(); ++dimIdx)
4495  {
4496  splitDesc.SetViewSize(j, dimIdx, splitterDimSizes[dimIdx]);
4497  }
4498  splitDesc.SetViewOriginCoord(j, splitDim, splitterDimSizes[splitDim] * j);
4499  }
4500  if (axisTensorInfo.GetNumElements() == 1)
4501  {
4502  splitDesc.SetAxis(axis);
4503  }
4504  auto layerName = fmt::format("Split:{}:{}", subgraphIndex, operatorIndex);
4505  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4506 
4507  if (!layer)
4508  {
4509  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4510  operatorIndex, CHECK_LOCATION().AsString()));
4511  }
4512 
4513  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4514  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[1]});
4515 
4516  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4517  {
4518  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
4519  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4520  }
4521 
4522  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4523  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4524 }
4525 
4526 unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
4527 {
4528  int numDims = armnn::numeric_cast<int>(numDimsIn);
4529  int v = idx < 0 ? numDims + idx : idx;
4530 
4531  if (v < 0 || v > numDims)
4532  {
4533  throw ParseException(fmt::format("Unable to compute index {}", CHECK_LOCATION().AsString()));
4534  }
4535 
4536  return static_cast<unsigned int>(v);
4537 }
4538 
4539 void TfLiteParserImpl::ParseSplitV(size_t subgraphIndex, size_t operatorIndex)
4540 {
4541  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4542 
4543  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4544  const auto* options = operatorPtr->builtin_options.AsSplitVOptions();
4545 
4546  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4547  CHECK_VALID_SIZE(inputs.size(), 3);
4548 
4549  auto& inputTensor = inputs[0];
4550  auto& splitsTensor = inputs[1];
4551  auto& axisTensor = inputs[2];
4552 
4553  armnn::TensorInfo inputTensorInfo = ToTensorInfo(inputTensor);
4554  armnn::TensorInfo splitsInfo = ToTensorInfo(splitsTensor);
4555  armnn::TensorInfo axisTensorInfo = ToTensorInfo(axisTensor);
4556 
4557  if (axisTensorInfo.GetNumElements() != 1)
4558  {
4559  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4560  CHECK_LOCATION().AsString()));
4561  }
4562 
4563  // Inputs
4564  auto inputDimSize = inputTensorInfo.GetNumDimensions();
4565  if (inputDimSize > MaxNumOfTensorDimensions)
4566  {
4567  throw ParseException(
4568  fmt::format("The number of dimensions: {} for input tensors of the "
4569  "SplitV op cannot be greater than {} {}",
4570  inputTensorInfo.GetNumDimensions(),
4572  CHECK_LOCATION().AsString()));
4573  }
4574 
4575  // Get split axis
4576  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, axisTensor->buffer);
4577  if (axisBufferPtr == nullptr)
4578  {
4579  throw ParseException(
4580  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4581  CHECK_LOCATION().AsString()));
4582  }
4583 
4584  std::vector<int> axisData(axisTensorInfo.GetNumElements());
4585  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4586  int32_t axis = axisData[0];
4587 
4588  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4589  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4590  {
4591  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4592  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4593  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4594  throw ParseException(
4595  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4596  axis,
4597  CHECK_LOCATION().AsString()));
4598  }
4599  const unsigned int splitDim = ComputeWrappedIndex(axis, inputTensorInfo.GetNumDimensions());
4600 
4601  // Set split sizes
4602  CHECK_VALID_SIZE(splitsInfo.GetNumDimensions(), 1);
4603  unsigned int numSplits{0};
4604 
4605  if(options)
4606  {
4607  numSplits = CHECKED_NON_NEGATIVE(options->num_splits);
4608  }
4609  else
4610  {
4611  numSplits = splitsInfo.GetNumElements();
4612  }
4613 
4614  if (numSplits <=0)
4615  {
4616  throw ParseException("SplitV has invalid number of splits");
4617  }
4618 
4619  std::vector<int> splitsData(numSplits);
4620  BufferRawPtr splitsBufferPtr = GetBuffer(m_Model, splitsTensor->buffer);
4621  ::memcpy(splitsData.data(), splitsBufferPtr->data.data(), splitsInfo.GetNumBytes());
4622 
4623  unsigned int idx = 0;
4624  int numInferred{0};
4625  unsigned int inferIdx{0};
4626  int splitSum{0};
4627  for (auto split : splitsData)
4628  {
4629  if (split < 0)
4630  {
4631  numInferred++;
4632  inferIdx = idx;
4633  }
4634  else
4635  {
4636  splitSum += split;
4637  }
4638  idx++;
4639  }
4640  // Check for inferred Axis
4641  if (numInferred == 0)
4642  {
4643  if (splitSum != armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]))
4644  {
4645  throw ParseException("SplitV split_sizes does not sum to the dimension of value along split_dim.");
4646  }
4647  }
4648  else if (numInferred == 1)
4649  {
4650  splitsData[inferIdx] = armnn::numeric_cast<int>(inputTensorInfo.GetShape()[splitDim]) - splitSum;
4651  }
4652  else
4653  {
4654  throw ParseException("Cannot infer split size for more than one split");
4655  }
4656 
4657  //Ouput size validation
4658  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4659  CHECK_VALID_SIZE(outputs.size(), numSplits);
4660 
4661  // Setup Armnn descriptor
4662  SplitterDescriptor splitDesc(numSplits, inputDimSize);
4663  unsigned int accumSplit = 0;
4664  for (unsigned int j = 0; j < numSplits; ++j)
4665  {
4666  unsigned int splitSize = armnn::numeric_cast<unsigned int>(splitsData[j]);
4667 
4668  // Set the size of the views.
4669  for (unsigned int dimIdx = 0; dimIdx < inputTensorInfo.GetNumDimensions(); ++dimIdx)
4670  {
4671  unsigned int dimSize = inputTensorInfo.GetShape()[dimIdx];
4672  if (dimIdx == splitDim)
4673  {
4674  dimSize = splitSize;
4675  }
4676  splitDesc.SetViewSize(j, dimIdx, dimSize);
4677  }
4678 
4679  splitDesc.SetViewOriginCoord(j, splitDim, accumSplit);
4680  accumSplit += splitSize;
4681  }
4682  splitDesc.SetAxis(axis);
4683 
4684  auto layerName = fmt::format("SplitV:{}:{}", subgraphIndex, operatorIndex);
4685  IConnectableLayer* layer = m_Network->AddSplitterLayer(splitDesc, layerName.c_str());
4686 
4687  if (!layer)
4688  {
4689  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4690  operatorIndex, CHECK_LOCATION().AsString()));
4691  }
4692 
4693  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4694  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4695 
4696  for (unsigned int k = 0; k < layer->GetNumOutputSlots(); ++k)
4697  {
4698  armnn::TensorInfo tensorInfo = ToTensorInfo(outputs[k], true);
4699  layer->GetOutputSlot(k).SetTensorInfo(tensorInfo);
4700  }
4701 
4702  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4703  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4704 }
4705 
4706 void TfLiteParserImpl::ParseArgMin(size_t subgraphIndex, size_t operatorIndex)
4707 {
4708  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Min);
4709 }
4710 
4711 void TfLiteParserImpl::ParseArgMax(size_t subgraphIndex, size_t operatorIndex)
4712 {
4713  ParseArgMinMax(subgraphIndex, operatorIndex, armnn::ArgMinMaxFunction::Max);
4714 }
4715 
4716 void TfLiteParserImpl::ParseArgMinMax(size_t subgraphIndex, size_t operatorIndex, ArgMinMaxFunction argMinMaxFunction)
4717 {
4718  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4719  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4720  CHECK_VALID_SIZE(inputs.size(), 2);
4721 
4722  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4723  CHECK_VALID_SIZE(outputs.size(), 1);
4724 
4725  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4726  armnn::TensorInfo axisTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4727  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4728 
4729  if (axisTensorInfo.GetNumElements() != 1)
4730  {
4731  throw ParseException(fmt::format("Axis tensor can only have 1 element {}",
4732  CHECK_LOCATION().AsString()));
4733  }
4734 
4735  // Check if output tensor type is Signed32 or Signed64
4736  if (outputTensorInfo.GetDataType() != armnn::DataType::Signed32 &&
4737  outputTensorInfo.GetDataType() != armnn::DataType::Signed64)
4738  {
4739  throw ParseException(
4740  fmt::format(
4741  "Output tensor data type is not supported. (Supported types: Signed32 & Signed64) {}",
4742  CHECK_LOCATION().AsString()));
4743  }
4744 
4745  // Get const axis value from model and set it to descriptor.
4746  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4747  if (axisBufferPtr == nullptr)
4748  {
4749  throw ParseException(
4750  fmt::format("Operation has invalid inputs. Failed to read axis. {}",
4751  CHECK_LOCATION().AsString()));
4752  }
4753 
4754  std::vector<int32_t> axisData(axisTensorInfo.GetNumElements());
4755  ::memcpy(axisData.data(), axisBufferPtr->data.data(), axisTensorInfo.GetNumBytes());
4756  int32_t axis = axisData.front();
4757 
4758  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4759  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4760  {
4761  // Square bracket denotes inclusive n while parenthesis denotes exclusive n
4762  // E.g. Rank 4 tensor can have axis in range [-4, 3)
4763  // -1 == 3, -2 == 2, -3 == 1, -4 == 0
4764  throw ParseException(
4765  fmt::format("Operation has invalid axis: {}. Axis must be in range [-n, n) {}",
4766  axis,
4767  CHECK_LOCATION().AsString()));
4768  }
4769 
4770  ArgMinMaxDescriptor desc;
4771  desc.m_Axis = axis;
4772  desc.m_Function = argMinMaxFunction;
4773 
4774  // Register a ArgMin/ArgMax layer.
4775  auto layerName = argMinMaxFunction == ArgMinMaxFunction::Max ? "ArgMax:{}:{}" : "ArgMin:{}:{}";
4776  auto layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
4777  IConnectableLayer *layer = m_Network->AddArgMinMaxLayer(desc, layerNameFormatted.c_str());
4778 
4779  if (!layer)
4780  {
4781  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4782  operatorIndex, CHECK_LOCATION().AsString()));
4783  }
4784 
4785  outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4786  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4787 
4788  // Register input tensor to the layer.
4789  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4790  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4791 
4792  // Register output tensor to the layer.
4793  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4794  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
4795 }
4796 
4797 void TfLiteParserImpl::ParseGather(size_t subgraphIndex, size_t operatorIndex)
4798 {
4799  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4800 
4801  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4802  CHECK_VALID_SIZE(inputs.size(), 2);
4803  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4804  CHECK_VALID_SIZE(outputs.size(), 1);
4805 
4806  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4807  armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4808  armnn::TensorInfo outputTensorInfo = ToTensorInfo(outputs[0]);
4809 
4810  armnn::GatherDescriptor gatherDescriptor;
4811 
4812  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4813  const auto* options = operatorPtr->builtin_options.AsGatherOptions();
4814  auto axis = options->axis;
4815 
4816  auto layerName = fmt::format("Gather:{}:{}", subgraphIndex, operatorIndex);
4817 
4818  auto inputDimensions = static_cast<int32_t>(inputTensorInfo.GetNumDimensions());
4819  auto indicesDimensions = indicesTensorInfo.GetNumDimensions();
4820  auto outputDimensions = outputTensorInfo.GetNumDimensions();
4821  if (((axis < -inputDimensions) && (axis < 0)) || ((axis >= inputDimensions) && (axis > 0)))
4822  {
4823  throw ParseException(
4824  fmt::format("Operation has invalid axis: {} It is out of bounds [ -{}, {} ) {}",
4825  axis,
4826  inputDimensions, inputDimensions,
4827  CHECK_LOCATION().AsString()));
4828  }
4829  if (outputDimensions != static_cast<unsigned int>(inputDimensions) + indicesDimensions - 1)
4830  {
4831  throw ParseException(
4832  fmt::format("Operation has invalid output dimensions: {} Output must be an ({} + {} - 1) -D tensor {}",
4833  outputDimensions,
4834  inputDimensions, indicesDimensions,
4835  CHECK_LOCATION().AsString()));
4836  }
4837 
4838  gatherDescriptor.m_Axis = axis;
4839 
4840  IConnectableLayer* layer = m_Network->AddGatherLayer(gatherDescriptor, layerName.c_str());
4841 
4842  if (!layer)
4843  {
4844  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4845  operatorIndex, CHECK_LOCATION().AsString()));
4846  }
4847 
4848  outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4849  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4850 
4851  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4852  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4853 
4854  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4855  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4856 }
4857 
4858 void TfLiteParserImpl::ParseGatherNd(size_t subgraphIndex, size_t operatorIndex)
4859 {
4860  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4861 
4862  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4863  CHECK_VALID_SIZE(inputs.size(), 2);
4864  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4865  CHECK_VALID_SIZE(outputs.size(), 1);
4866 
4867  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4868  armnn::TensorInfo indicesTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4869 
4870  auto layerName = fmt::format("GatherNd:{}:{}", subgraphIndex, operatorIndex);
4871  IConnectableLayer* layer = m_Network->AddGatherNdLayer(layerName.c_str());
4872 
4873  if (!layer)
4874  {
4875  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4876  operatorIndex, CHECK_LOCATION().AsString()));
4877  }
4878 
4879  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
4880  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4881 
4882  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4883  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
4884 
4885  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4886  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4887 }
4888 
4889 void TfLiteParserImpl::ParseDepthToSpace(size_t subgraphIndex, size_t operatorIndex)
4890 {
4891  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4892 
4893  TfLiteParserImpl::TensorRawPtrVector inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4894  CHECK_VALID_SIZE(inputs.size(), 1);
4895  TfLiteParserImpl::TensorRawPtrVector outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4896  CHECK_VALID_SIZE(outputs.size(), 1);
4897 
4898  armnn::DepthToSpaceDescriptor descriptor;
4899 
4900  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4901  const auto* options = operatorPtr->builtin_options.AsDepthToSpaceOptions();
4902  auto blockSize = options->block_size;
4903  if (blockSize < 2)
4904  {
4905  throw ParseException(
4906  fmt::format("Operation has invalid block size: {} Block size should be >= 2 {}",
4907  blockSize,
4908  CHECK_LOCATION().AsString()));
4909  }
4910  descriptor.m_BlockSize = armnn::numeric_cast<uint32_t>(blockSize);
4911 
4912  auto layerName = fmt::format("DepthToSpace:{}:{}", subgraphIndex, operatorIndex);
4913  IConnectableLayer* layer = m_Network->AddDepthToSpaceLayer(descriptor, layerName.c_str());
4914 
4915  if (!layer)
4916  {
4917  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
4918  operatorIndex, CHECK_LOCATION().AsString()));
4919  }
4920 
4921  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
4922  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
4923 
4924  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
4925  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
4926 
4927  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
4928  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
4929 }
4930 
4931 void TfLiteParserImpl::ParseSum(size_t subgraphIndex, size_t operatorIndex)
4932 {
4933  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Sum);
4934 }
4935 
4936 void TfLiteParserImpl::ParseReduceProd(size_t subgraphIndex, size_t operatorIndex)
4937 {
4938  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Prod);
4939 }
4940 
4941 void TfLiteParserImpl::ParseReduceMax(size_t subgraphIndex, size_t operatorIndex)
4942 {
4943  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Max);
4944 }
4945 
4946 void TfLiteParserImpl::ParseReduceMin(size_t subgraphIndex, size_t operatorIndex)
4947 {
4948  ParseReduce(subgraphIndex, operatorIndex, armnn::ReduceOperation::Min);
4949 }
4950 
4951 void TfLiteParserImpl::ParseReduce(size_t subgraphIndex, size_t operatorIndex, ReduceOperation reduceOperation)
4952 {
4953  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
4954 
4955  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
4956  const auto* options = operatorPtr->builtin_options.AsReducerOptions();
4957 
4958  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
4959  CHECK_VALID_SIZE(inputs.size(), 2);
4960 
4961  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
4962  CHECK_VALID_SIZE(outputs.size(), 1);
4963 
4964  auto layerName = fmt::format("Reduce:{}:{}", subgraphIndex, operatorIndex);
4965 
4966  armnn::TensorInfo inputTensorInfo0 = InputTensorInfo(subgraphIndex, operatorIndex, 0);
4967  armnn::TensorInfo inputTensorInfo1 = InputTensorInfo(subgraphIndex, operatorIndex, 1);
4968 
4969  ReduceDescriptor desc;
4970  BufferRawPtr axisBufferPtr = GetBuffer(m_Model, inputs[1]->buffer);
4971  // Get const axis value from model and set it to descriptor.
4972  if (axisBufferPtr != nullptr)
4973  {
4974  std::vector<int32_t> axisData(inputTensorInfo1.GetNumElements());
4975  ::memcpy(axisData.data(), axisBufferPtr->data.data(), inputTensorInfo1.GetNumBytes());
4976 
4977  // Convert the axis to unsigned int and remove duplicates.
4978  auto rank = static_cast<int32_t>(inputTensorInfo0.GetNumDimensions());
4979  std::set<unsigned int> uniqueAxis;
4980  std::transform(axisData.begin(),
4981  axisData.end(),
4982  std::inserter(uniqueAxis, uniqueAxis.begin()),
4983  [rank](int i)->unsigned int{
4984  return static_cast<uint32_t>(((i + rank) % rank)); });
4985  desc.m_vAxis.assign(uniqueAxis.begin(), uniqueAxis.end());
4986  }
4987  else
4988  {
4989  for (uint32_t i = 0; i < inputTensorInfo0.GetNumDimensions(); ++i)
4990  {
4991  desc.m_vAxis.push_back(i);
4992  }
4993  }
4994 
4995  desc.m_KeepDims = options->keep_dims;
4996  desc.m_ReduceOperation = reduceOperation;
4997 
4998  // Register a new layer object, Sum.
4999  IConnectableLayer* layer = m_Network->AddReduceLayer(desc, layerName.c_str());
5000 
5001  armnn::TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5002  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5003 
5004  // Register input tensor to the layer.
5005  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5006  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5007 
5008  // Register output tensor to the layer.
5009  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5010  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5011 }
5012 
5013 void TfLiteParserImpl::ParseLocalResponseNormalization(size_t subgraphIndex, size_t operatorIndex)
5014 {
5015  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5016 
5017  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5018  CHECK_VALID_SIZE(inputs.size(), 1);
5019 
5020  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5021  CHECK_VALID_SIZE(outputs.size(), 1);
5022 
5023  auto layerName = fmt::format("LRN:{}:{}", subgraphIndex, operatorIndex);
5024  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5025 
5026  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5027 
5028  const auto& operatorPtr = m_Model->subgraphs[subgraphIndex]->operators[operatorIndex];
5029  const auto* options = operatorPtr->builtin_options.AsLocalResponseNormalizationOptions();
5030 
5031  armnn::NormalizationDescriptor descriptor;
5035  descriptor.m_NormSize = static_cast<uint32_t>(options->radius);
5036  descriptor.m_K = options->bias;
5037  descriptor.m_Alpha = options->alpha;
5038  descriptor.m_Beta = options->beta;
5039 
5040  // ArmNN expects normSize to be the full size of the normalization
5041  // window rather than the radius as in TfLite.
5042  descriptor.m_NormSize = 1 + (2 * descriptor.m_NormSize);
5043 
5044  IConnectableLayer* layer = m_Network->AddNormalizationLayer(descriptor, layerNameFormatted.c_str());
5045 
5046  if (!layer)
5047  {
5048  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5049  operatorIndex, CHECK_LOCATION().AsString()));
5050  }
5051 
5052  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5053  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5054 
5055  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5056  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5057 
5058  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5059  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5060 }
5061 
5062 void TfLiteParserImpl::ParseAbs(size_t subgraphIndex, size_t operatorIndex)
5063 {
5064  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Abs);
5065 }
5066 
5067 void TfLiteParserImpl::ParseCeil(size_t subgraphIndex, size_t operatorIndex)
5068 {
5069  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Ceil);
5070 }
5071 
5072 void TfLiteParserImpl::ParseExp(size_t subgraphIndex, size_t operatorIndex)
5073 {
5074  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Exp);
5075 }
5076 
5077 void TfLiteParserImpl::ParseLog(size_t subgraphIndex, size_t operatorIndex)
5078 {
5079  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Log);
5080 }
5081 
5082 void TfLiteParserImpl::ParseLogicalNot(size_t subgraphIndex, size_t operatorIndex)
5083 {
5084  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::LogicalNot);
5085 }
5086 
5087 void TfLiteParserImpl::ParseNeg(size_t subgraphIndex, size_t operatorIndex)
5088 {
5089  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Neg);
5090 }
5091 
5092 void TfLiteParserImpl::ParsePower(size_t subgraphIndex, size_t operatorIndex)
5093 {
5094  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5095 
5096  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5097  CHECK_VALID_SIZE(inputs.size(), 2);
5098 
5099  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5100  CHECK_VALID_SIZE(outputs.size(), 1);
5101 
5102  auto layerName = fmt::format("Power:{}:{}", subgraphIndex, operatorIndex);
5103 
5104  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5105  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5106  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerName, "Input 0", "Input 1");
5107 
5108  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Power, layerName.c_str());
5109 
5110  if (!layer)
5111  {
5112  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5113  operatorIndex, CHECK_LOCATION().AsString()));
5114  }
5115 
5116  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5117  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5118  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5119 
5120  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5121  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5122 
5123  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5124  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5125 }
5126 
5127 void TfLiteParserImpl::ParseRsqrt(size_t subgraphIndex, size_t operatorIndex)
5128 {
5129  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Rsqrt);
5130 }
5131 
5132 void TfLiteParserImpl::ParseSin(size_t subgraphIndex, size_t operatorIndex)
5133 {
5134  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sin);
5135 }
5136 
5137 void TfLiteParserImpl::ParseSqrt(size_t subgraphIndex, size_t operatorIndex)
5138 {
5139  ParseElementwiseUnary(subgraphIndex, operatorIndex, armnn::UnaryOperation::Sqrt);
5140 }
5141 
5142 void TfLiteParserImpl::ParseSquare(size_t subgraphIndex, size_t operatorIndex)
5143 {
5144  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5145 
5146  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5147  CHECK_VALID_SIZE(inputs.size(), 1);
5148 
5149  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5150  CHECK_VALID_SIZE(outputs.size(), 1);
5151 
5152  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5153 
5154  auto layerName = fmt::format("Square:{}:{}", subgraphIndex, operatorIndex);
5155  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::Mul, layerName.c_str());
5156  ARMNN_ASSERT(layer != nullptr);
5157 
5158  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 0});
5159  CheckMatchingQuantization(inputTensorInfo, outputTensorInfo, layerName, "Input 0", "Output 0");
5160  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5161 
5162  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5163  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[0]});
5164 
5165  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5166  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5167 }
5168 
5169 void TfLiteParserImpl::ParseSquaredDifference(size_t subgraphIndex, size_t operatorIndex)
5170 {
5171  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5172 
5173  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5174  CHECK_VALID_SIZE(inputs.size(), 2);
5175 
5176  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5177  CHECK_VALID_SIZE(outputs.size(), 1);
5178 
5179  auto layerName = fmt::format("SquaredDifference:{}:{}", subgraphIndex, operatorIndex);
5180 
5181  TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5182  TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5183 
5184  IConnectableLayer* layer = m_Network->AddElementwiseBinaryLayer(BinaryOperation::SqDiff, layerName.c_str());
5185 
5186  if (!layer)
5187  {
5188  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5189  operatorIndex, CHECK_LOCATION().AsString()));
5190  }
5191 
5192  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5193  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5194 
5195  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5196  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5197 
5198  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5199  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5200 }
5201 
5202 void TfLiteParserImpl::ParseElementwiseUnary(size_t subgraphIndex, size_t operatorIndex, UnaryOperation unaryOperation)
5203 {
5204  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5205 
5206  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5207  CHECK_VALID_SIZE(inputs.size(), 1);
5208 
5209  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5210  CHECK_VALID_SIZE(outputs.size(), 1);
5211 
5212  std::string layerName = std::string(GetUnaryOperationAsCString(unaryOperation)) + ":{}:{}";
5213  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5214 
5216  desc.m_Operation = unaryOperation;
5217  IConnectableLayer* layer = m_Network->AddElementwiseUnaryLayer(desc, layerNameFormatted.c_str());
5218 
5219  if (!layer)
5220  {
5221  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5222  operatorIndex, CHECK_LOCATION().AsString()));
5223  }
5224 
5225  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0});
5226  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5227 
5228  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5229  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0]});
5230 
5231  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5232  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, outputTensorIndexes);
5233 }
5234 
5235 void TfLiteParserImpl::ParseEqual(size_t subgraphIndex, size_t operatorIndex)
5236 {
5237  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Equal);
5238 }
5239 
5240 void TfLiteParserImpl::ParseNotEqual(size_t subgraphIndex, size_t operatorIndex)
5241 {
5242  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::NotEqual);
5243 }
5244 
5245 void TfLiteParserImpl::ParseGreater(size_t subgraphIndex, size_t operatorIndex)
5246 {
5247  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Greater);
5248 }
5249 
5250 void TfLiteParserImpl::ParseGreaterOrEqual(size_t subgraphIndex, size_t operatorIndex)
5251 {
5252  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::GreaterOrEqual);
5253 }
5254 
5255 void TfLiteParserImpl::ParseLess(size_t subgraphIndex, size_t operatorIndex)
5256 {
5257  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::Less);
5258 }
5259 
5260 void TfLiteParserImpl::ParseLessOrEqual(size_t subgraphIndex, size_t operatorIndex)
5261 {
5262  ParseComparison(subgraphIndex, operatorIndex, armnn::ComparisonOperation::LessOrEqual);
5263 }
5264 
5265 void TfLiteParserImpl::ParseComparison(size_t subgraphIndex, size_t operatorIndex,
5266  ComparisonOperation comparisonOperation)
5267 {
5268  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5269 
5270  auto inputs = GetInputs(m_Model, subgraphIndex, operatorIndex);
5271  CHECK_VALID_SIZE(inputs.size(), 2);
5272 
5273  auto outputs = GetOutputs(m_Model, subgraphIndex, operatorIndex);
5274  CHECK_VALID_SIZE(outputs.size(), 1);
5275 
5276  auto layerName = std::string(GetComparisonOperationAsCString(comparisonOperation)) + ":{}:{}";
5277  std::string layerNameFormatted = fmt::format(layerName, subgraphIndex, operatorIndex);
5278 
5279  armnn::TensorInfo inputTensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 0);
5280  armnn::TensorInfo input1TensorInfo = InputTensorInfo(subgraphIndex, operatorIndex, 1);
5281  CheckMatchingQuantization(inputTensorInfo, input1TensorInfo, layerNameFormatted, "Input 0", "Input 1");
5282 
5283  ComparisonDescriptor desc;
5284  desc.m_Operation = comparisonOperation;
5285  IConnectableLayer* layer = m_Network->AddComparisonLayer(desc, layerNameFormatted.c_str());
5286 
5287  if (!layer)
5288  {
5289  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5290  operatorIndex, CHECK_LOCATION().AsString()));
5291  }
5292 
5293  TensorInfo outputTensorInfo = OutputTensorInfoFromInputs(subgraphIndex, operatorIndex, layer, 0, {0, 1});
5294  layer->GetOutputSlot(0).SetTensorInfo(outputTensorInfo);
5295 
5296  auto inputTensorIndexes = AsUnsignedVector(GetInputTensorIds(m_Model, subgraphIndex, operatorIndex));
5297  RegisterInputSlots(subgraphIndex, operatorIndex, layer, {inputTensorIndexes[0], inputTensorIndexes[1]});
5298 
5299  auto outputTensorIndexes = AsUnsignedVector(GetOutputTensorIds(m_Model, subgraphIndex, operatorIndex));
5300  RegisterOutputSlots(subgraphIndex, operatorIndex, layer, {outputTensorIndexes[0]});
5301 }
5302 
5303 armnn::IConnectableLayer* TfLiteParserImpl::AddReshapeLayer(armnn::IConnectableLayer* layer,
5304  unsigned int outputSlot,
5305  std::string reshapeLayerName,
5306  armnn::TensorInfo outputShape)
5307 {
5308  ReshapeDescriptor desc;
5309  desc.m_TargetShape = outputShape.GetShape();
5310 
5311  IConnectableLayer* reshapeLayer =
5312  m_Network->AddReshapeLayer(desc, reshapeLayerName.c_str());
5313 
5314  auto & prevOutputSlot = layer->GetOutputSlot(outputSlot);
5315  prevOutputSlot.Connect(reshapeLayer->GetInputSlot(0));
5316  reshapeLayer->GetOutputSlot(0).SetTensorInfo(outputShape);
5317  return reshapeLayer;
5318 }
5319 
5320 armnn::IConnectableLayer* TfLiteParserImpl::AddFusedActivationLayer(armnn::IConnectableLayer* prevLayer,
5321  unsigned int outputSlot,
5322  tflite::ActivationFunctionType activationType)
5323 {
5324  ActivationDescriptor activationDesc;
5325  std::string layerName = prevLayer->GetName();
5326 
5327  switch(activationType)
5328  {
5329  case tflite::ActivationFunctionType_NONE:
5330  {
5331  // this is a no-op: return previous layer
5332  return prevLayer;
5333  }
5334  case tflite::ActivationFunctionType_RELU:
5335  {
5336  activationDesc.m_Function = ActivationFunction::ReLu;
5337  layerName += ":RELU";
5338  break;
5339  }
5340  case tflite::ActivationFunctionType_RELU6:
5341  {
5342  activationDesc.m_Function = ActivationFunction::BoundedReLu;
5343  activationDesc.m_A = 6.0f;
5344  activationDesc.m_B = 0.0f;
5345  layerName += ":RELU6";
5346  break;
5347  }
5348  case tflite::ActivationFunctionType_TANH:
5349  {
5350  activationDesc.m_Function = ActivationFunction::TanH;
5351  activationDesc.m_A = 1.0f;
5352  activationDesc.m_B = 1.0f;
5353  layerName += ":TANH";
5354  break;
5355  }
5356 
5357  // I only put these here as a reminder what others we could support
5358  case tflite::ActivationFunctionType_RELU_N1_TO_1:
5359  case tflite::ActivationFunctionType_SIGN_BIT:
5360  default:
5361  {
5362  throw ParseException(
5363  fmt::format("TfLite parser doesn't support fused activation: "
5364  "{}/{} {} ",
5365  activationType,
5366  tflite::EnumNameActivationFunctionType(activationType),
5367  CHECK_LOCATION().AsString()));
5368 
5369  }
5370  }
5371 
5372  IConnectableLayer* activationLayer =
5373  m_Network->AddActivationLayer(activationDesc, layerName.c_str());
5374 
5375  auto & prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5376  prevOutputSlot.Connect(activationLayer->GetInputSlot(0));
5377  activationLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
5378  return activationLayer;
5379 }
5380 
5381 armnn::IConnectableLayer* TfLiteParserImpl::AddFusedFloorLayer(armnn::IConnectableLayer* prevLayer,
5382  unsigned int outputSlot)
5383 {
5384 
5385  auto& prevOutputSlot = prevLayer->GetOutputSlot(outputSlot);
5386  DataType dataType = prevOutputSlot.GetTensorInfo().GetDataType();
5387 
5388  if (dataType == DataType::Signed32)
5389  {
5390  return prevLayer;
5391  }
5392 
5393  std::string layerName = prevLayer->GetName();
5394  IConnectableLayer* floorLayer = m_Network->AddFloorLayer(layerName.c_str());
5395 
5396  prevOutputSlot.Connect(floorLayer->GetInputSlot(0));
5397  floorLayer->GetOutputSlot(0).SetTensorInfo(prevOutputSlot.GetTensorInfo());
5398 
5399  return floorLayer;
5400 }
5401 
5403 {
5404  if (fileName == nullptr)
5405  {
5406  throw InvalidArgumentException(fmt::format("Invalid (null) file name {}",
5407  CHECK_LOCATION().AsString()));
5408  }
5409  std::error_code errorCode;
5410  fs::path pathToFile(fileName);
5411  if (!fs::exists(pathToFile, errorCode))
5412  {
5413  //fmt::format() could not be used here (format error)
5414  std::stringstream msg;
5415  msg << "Cannot find the file (" << fileName << ") errorCode: " << errorCode
5416  << " " << CHECK_LOCATION().AsString();
5417  throw FileNotFoundException(msg.str());
5418  }
5419  if (!fs::is_regular_file(pathToFile))
5420  {
5421  // Exclude non regular files.
5422  throw InvalidArgumentException(fmt::format("File \"{}\" is not a regular file and cannot be loaded.",
5423  pathToFile.c_str()));
5424  }
5425 
5426  std::ifstream file(fileName, std::ios::binary);
5427  std::string fileContent((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
5428  return LoadModelFromBinary(reinterpret_cast<const uint8_t *>(fileContent.c_str()),
5429  fileContent.size());
5430 }
5431 
5433 {
5434  if (binaryContent == nullptr)
5435  {
5436  throw InvalidArgumentException(fmt::format("Invalid (null) binary content {}",
5437  CHECK_LOCATION().AsString()));
5438  }
5439  flatbuffers::Verifier verifier(binaryContent, len);
5440  if (verifier.VerifyBuffer<tflite::Model>() == false)
5441  {
5442  throw ParseException(
5443  fmt::format("Buffer doesn't conform to the expected Tensorflow Lite "
5444  "flatbuffers format. size:{} {}",
5445  len,
5446  CHECK_LOCATION().AsString()));
5447  }
5448  return tflite::UnPackModel(binaryContent);
5449 }
5450 
5452  size_t subgraphIndex,
5453  size_t operatorIndex)
5454 {
5455  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5456 
5457  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5458  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5459 
5460  size_t inputCount = operatorPtr->inputs.size();
5461  TensorRawPtrVector result;
5462  for (size_t i = 0; i < inputCount; ++i)
5463  {
5464  // If the input location is -1 then assume input is turned off.
5465  if (operatorPtr->inputs[i] == -1)
5466  {
5467  continue;
5468  }
5469  else
5470  {
5471  uint32_t inputId = CHECKED_NON_NEGATIVE(operatorPtr->inputs[i]);
5472  result.push_back(subgraphPtr->tensors[inputId].get());
5473  }
5474  }
5475  return result;
5476 }
5477 
5479  size_t subgraphIndex,
5480  size_t operatorIndex)
5481 {
5482  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5483 
5484  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5485  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5486 
5487  size_t outputCount = operatorPtr->outputs.size();
5488  TensorRawPtrVector result(outputCount);
5489  for (size_t i = 0; i < outputCount; ++i)
5490  {
5491  uint32_t outputId = CHECKED_NON_NEGATIVE(operatorPtr->outputs[i]);
5492  CHECK_TENSOR(model, subgraphIndex, outputId);
5493  result[i] = subgraphPtr->tensors[outputId].get();
5494  }
5495  return result;
5496 }
5497 
5499  size_t subgraphIndex)
5500 {
5501  CHECK_SUBGRAPH(model, subgraphIndex);
5502  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5503 
5504  size_t inputCount = subgraphPtr->inputs.size();
5505  TensorIdRawPtrVector result(inputCount);
5506  for (size_t i = 0; i < inputCount; ++i)
5507  {
5508  uint32_t inputId = CHECKED_NON_NEGATIVE(subgraphPtr->inputs[i]);
5509  CHECK_TENSOR(model, subgraphIndex, inputId);
5510  result[i] = std::make_pair(inputId, subgraphPtr->tensors[inputId].get());
5511  }
5512  return result;
5513 }
5514 
5516  size_t subgraphIndex)
5517 {
5518  CHECK_SUBGRAPH(model, subgraphIndex);
5519  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5520 
5521  size_t outputCount = subgraphPtr->outputs.size();
5522  TensorIdRawPtrVector result(outputCount);
5523  for (size_t i = 0; i < outputCount; ++i)
5524  {
5525  uint32_t outputId = CHECKED_NON_NEGATIVE(subgraphPtr->outputs[i]);
5526  result[i] = std::make_pair(outputId, subgraphPtr->tensors[outputId].get());
5527  }
5528  return result;
5529 }
5530 
5531 std::vector<int32_t>& TfLiteParserImpl::GetInputTensorIds(const ModelPtr& model,
5532  size_t subgraphIndex,
5533  size_t operatorIndex)
5534 {
5535  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5536  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5537  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5538  return operatorPtr->inputs;
5539 }
5540 
5541 std::vector<int32_t>& TfLiteParserImpl::GetOutputTensorIds(const ModelPtr& model,
5542  size_t subgraphIndex,
5543  size_t operatorIndex)
5544 {
5545  CHECK_MODEL(model, subgraphIndex, operatorIndex);
5546  const auto& subgraphPtr = model->subgraphs[subgraphIndex];
5547  const auto& operatorPtr = subgraphPtr->operators[operatorIndex];
5548  return operatorPtr->outputs;
5549 }
5550 
5551 void TfLiteParserImpl::RegisterInputSlots(size_t subgraphIndex,
5552  size_t operatorIndex,
5553  IConnectableLayer* layer,
5554  const std::vector<unsigned int>& tensorIndexes,
5555  unsigned int startingSlotIndex)
5556 {
5557  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5558 
5559  if (!layer)
5560  {
5561  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5562  operatorIndex, CHECK_LOCATION().AsString()));
5563  }
5564 
5565  if (tensorIndexes.size() + startingSlotIndex != layer->GetNumInputSlots())
5566  {
5567  throw ParseException(
5568  fmt::format("The number of tensor inputs ({}) does not match the number expected ({})"
5569  " for subgraph:{} operator index:{} {}",
5570  tensorIndexes.size(),
5571  layer->GetNumInputSlots(),
5572  subgraphIndex,
5573  operatorIndex,
5574  CHECK_LOCATION().AsString()));
5575  }
5576 
5577  for (unsigned int index = 0; index < tensorIndexes.size() ; ++index)
5578  {
5579  unsigned int tensorIndex = tensorIndexes[index];
5580  armnn::IInputSlot* slot = &(layer->GetInputSlot(startingSlotIndex + index));
5581  RegisterConsumerOfTensor(subgraphIndex, tensorIndex, slot);
5582  }
5583 }
5584 
5585 void TfLiteParserImpl::RegisterOutputSlots(size_t subgraphIndex,
5586  size_t operatorIndex,
5587  IConnectableLayer* layer,
5588  const std::vector<unsigned int>& tensorIndexes)
5589 {
5590  CHECK_MODEL(m_Model, subgraphIndex, operatorIndex);
5591 
5592  if (!layer)
5593  {
5594  throw NullPointerException(fmt::format("Layer {} pointer is null {}",
5595  operatorIndex, CHECK_LOCATION().AsString()));
5596  }
5597 
5598  if (tensorIndexes.size() != layer->GetNumOutputSlots())
5599  {
5600  throw ParseException(
5601  fmt::format("The number of tensor outputs ({}) does not match the number expected ({})"
5602  " for subgraph:{} operator index:{} {}",
5603  tensorIndexes.size(),
5604  layer->GetNumOutputSlots(),
5605  subgraphIndex,
5606  operatorIndex,
5607  CHECK_LOCATION().AsString()));
5608  }
5609 
5610  for (unsigned int slotIndex = 0; slotIndex < layer->GetNumOutputSlots(); ++slotIndex)
5611  {
5612  unsigned int tensorIndex = tensorIndexes[slotIndex];
5613  armnn::IOutputSlot* slot = &(layer->GetOutputSlot(slotIndex));
5614  RegisterProducerOfTensor(subgraphIndex, tensorIndex, slot);
5615  }
5616 }
5617 
5618 void TfLiteParserImpl::SetupInputLayerTensorInfos(size_t subgraphIndex)
5619 {
5620  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5621 
5622  auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5623  for (auto const& tensorIdAndPtr : inputs)
5624  {
5625  auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5626  m_TensorInfos.insert({tensorIdAndPtr.first, tensorInfo});
5627  }
5628 }
5629 
5630 void TfLiteParserImpl::SetupInputLayers(size_t subgraphIndex)
5631 {
5632  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5633 
5634  auto inputs = GetSubgraphInputs(m_Model, subgraphIndex);
5635  for (auto const& tensorIdAndPtr : inputs)
5636  {
5637  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5638  IConnectableLayer* layer =
5639  m_Network->AddInputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5640 
5641  auto tensorInfo = ToTensorInfo(tensorIdAndPtr.second);
5642  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5643 
5644  RegisterOutputSlots(subgraphIndex,
5645  VIRTUAL_OPERATOR_ID,
5646  layer,
5647  { static_cast<uint32_t>(tensorIdAndPtr.first) });
5648  }
5649 }
5650 
5651 void TfLiteParserImpl::SetupOutputLayers(size_t subgraphIndex)
5652 {
5653  CHECK_SUBGRAPH(m_Model, subgraphIndex);
5654 
5655  auto outputs = GetSubgraphOutputs(m_Model, subgraphIndex);
5656  for (auto const& tensorIdAndPtr : outputs)
5657  {
5658  auto bindingId = GenerateLayerBindingId(subgraphIndex, tensorIdAndPtr.first);
5659  IConnectableLayer* layer =
5660  m_Network->AddOutputLayer(bindingId, tensorIdAndPtr.second->name.c_str());
5661 
5662  RegisterInputSlots(subgraphIndex,
5663  VIRTUAL_OPERATOR_ID,
5664  layer,
5665  { static_cast<uint32_t>(tensorIdAndPtr.first) });
5666  }
5667 }
5668 
5669 void TfLiteParserImpl::SetupConstantLayerTensorInfos(size_t subgraph)
5670 {
5671  CHECK_SUBGRAPH(m_Model, subgraph);
5672 
5673  const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5674  for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5675  {
5676  for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5677  {
5678  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5679  m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5680  {
5681  TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5682 
5683  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5684 
5685  m_TensorInfos.insert({tensorIndex, tensorInfo});
5686  }
5687  }
5688  }
5689 }
5690 
5691 void TfLiteParserImpl::SetupConstantLayers(size_t subgraph)
5692 {
5693  CHECK_SUBGRAPH(m_Model, subgraph);
5694 
5695  const auto & subgraphPtr = m_Model->subgraphs[subgraph];
5696  for (unsigned int subgraphIndex = 0; subgraphIndex < m_SubgraphConnections.size(); ++subgraphIndex)
5697  {
5698  for (unsigned int tensorIndex = 0; tensorIndex < m_SubgraphConnections[subgraphIndex].size(); ++tensorIndex)
5699  {
5700  if (m_SubgraphConnections[subgraphIndex][tensorIndex].outputSlot == nullptr &&
5701  m_SubgraphConnections[subgraphIndex][tensorIndex].inputSlots.size() > 0)
5702  {
5703  TensorRawPtr tensorPtr = subgraphPtr->tensors[tensorIndex].get();
5704 
5705  if (IsConstTensor(tensorPtr))
5706  {
5707  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5708  armnn::DataType dataType = tensorInfo.GetDataType();
5709 
5710  if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5711  != m_ConstantsToDequantize.end())
5712  {
5713  dataType = DataType::Float32;
5714  }
5715  auto tensorAndData = CreateConstTensorNonPermuted(tensorPtr, tensorInfo, dataType);
5716 
5717  std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5718  IConnectableLayer *layer = m_Network->AddConstantLayer(tensorAndData.first, layerName.c_str());
5719 
5720  layer->GetOutputSlot(0).SetTensorInfo(tensorAndData.first.GetInfo());
5721  RegisterOutputSlots(subgraphIndex,
5722  VIRTUAL_OPERATOR_ID,
5723  layer,
5724  { tensorIndex });
5725  }
5726  else if (ShouldConstantTensorBeCreated(tensorIndex))
5727  {
5728  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5729  armnn::DataType dataType = tensorInfo.GetDataType();
5730 
5731  if (std::find(m_ConstantsToDequantize.begin(), m_ConstantsToDequantize.end(), tensorPtr->buffer)
5732  != m_ConstantsToDequantize.end())
5733  {
5734  dataType = DataType::Float32;
5735  }
5736  // Make sure isConstant flag is set.
5737  tensorInfo.SetConstant();
5738  tensorInfo.SetDataType(dataType);
5739 
5740  auto tensorAndData = ConstTensor(tensorInfo, std::vector<uint8_t>(tensorInfo.GetNumBytes()));
5741 
5742  std::string layerName = fmt::format("Constant:{}", tensorPtr->name);
5743  IConnectableLayer* layer = m_Network->AddConstantLayer(tensorAndData, layerName.c_str());
5744 
5745  layer->GetOutputSlot(0).SetTensorInfo(tensorInfo);
5746  RegisterOutputSlots(subgraphIndex,
5747  VIRTUAL_OPERATOR_ID,
5748  layer,
5749  {tensorIndex});
5750  }
5751  else
5752  {
5753  throw ParseException(
5754  fmt::format("Invalid Tensor: Tensor should be constant. {}",
5755  CHECK_LOCATION().AsString()));
5756  }
5757  }
5758  }
5759  }
5760 }
5761 
5762 // example usage: BufferRawPtr bufferPtr = GetBuffer(m_Model, inputs[0]->buffer);
5764 {
5765  CHECK_BUFFER(model, bufferIndex);
5766  return model->buffers[bufferIndex].get();
5767 }
5768 
5769 template<typename T>
5770 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5771 TfLiteParserImpl::CreateConstTensorAndStoreData(TfLiteParserImpl::BufferRawPtr bufferPtr,
5773  armnn::TensorInfo& tensorInfo,
5775 {
5776  // Make sure isConstant flag is set.
5777  tensorInfo.SetConstant();
5778 
5779  auto constData = CreateConstTensorImpl<T>(bufferPtr,
5780  tensorPtr,
5781  tensorInfo,
5782  permutationVector);
5783  TfLiteParserImpl::SupportedDataStorage storage(std::move(constData.second));
5784  return std::make_pair(constData.first, std::move(storage));
5785 }
5786 
5787 bool TfLiteParserImpl::ShouldConstantTensorBeCreated(unsigned int tensorIndex)
5788 {
5789  // If the TensorIndex appears in the list of ConstantsToBeCreated then return true
5790  return (std::find(m_ConstantsToBeCreated.begin(), m_ConstantsToBeCreated.end(), tensorIndex)
5791  != m_ConstantsToBeCreated.end());
5792 }
5793 
5794 bool TfLiteParserImpl::IsConstTensor(TensorRawPtr tensorPtr)
5795 {
5796  CHECK_TENSOR_PTR(tensorPtr);
5797  bool isConst = true;
5798 
5799  auto buffer = GetBuffer(m_Model, tensorPtr->buffer);
5800  if (buffer->data.size() == 0)
5801  {
5802  isConst = false;
5803  }
5804 
5805  return isConst;
5806 }
5807 
5808 std::pair<armnn::ConstTensor, TfLiteParserImpl::SupportedDataStorage>
5809 TfLiteParserImpl::CreateConstTensorPermuted(TensorRawPtr tensorPtr,
5810  armnn::TensorInfo& tensorInfo,
5812 {
5813  CHECK_TENSOR_PTR(tensorPtr);
5814  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5815  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5816 
5817  // Make sure isConstant flag is set.
5818  tensorInfo.SetConstant();
5819 
5820  switch (tensorInfo.GetDataType())
5821  {
5823  return CreateConstTensorAndStoreData<float>(bufferPtr,
5824  tensorPtr,
5825  tensorInfo,
5826  permutationVector);
5828  return CreateConstTensorAndStoreData<uint8_t>(bufferPtr,
5829  tensorPtr,
5830  tensorInfo,
5831  permutationVector);
5833  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5834  tensorPtr,
5835  tensorInfo,
5836  permutationVector);
5838  return CreateConstTensorAndStoreData<int8_t>(bufferPtr,
5839  tensorPtr,
5840  tensorInfo,
5841  permutationVector);
5843  return CreateConstTensorAndStoreData<int32_t>(bufferPtr,
5844  tensorPtr,
5845  tensorInfo,
5846  permutationVector);
5847  default:
5848  {
5849  std::stringstream errString;
5850  errString << "Unexpected datatype when creating const tensor: "
5851  << armnn::GetDataTypeName(tensorInfo.GetDataType())
5852  << " shape:" << tensorInfo.GetShape()
5853  << CHECK_LOCATION().AsString();
5854  throw ParseException(errString.str());
5855  }
5856  }
5857 }
5858 
5859 armnn::ConstTensor TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5860  armnn::TensorInfo& tensorInfo)
5861 {
5862  CHECK_TENSOR_PTR(tensorPtr);
5863  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5864  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5865 
5866  // Make sure isConstant flag is set.
5867  tensorInfo.SetConstant();
5868 
5869  return ConstTensor(tensorInfo, bufferPtr->data.data());
5870 }
5871 
5872 std::pair<armnn::ConstTensor, std::unique_ptr<float[]>>
5873 TfLiteParserImpl::CreateConstTensorNonPermuted(TensorRawPtr tensorPtr,
5874  armnn::TensorInfo& tensorInfo,
5875  armnn::DataType inputDataType)
5876 {
5877  CHECK_TENSOR_PTR(tensorPtr);
5878  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5879  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5880 
5881  // Make sure isConstant flag is set.
5882  tensorInfo.SetConstant();
5883 
5884  if (inputDataType == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5885  {
5886  try
5887  {
5888  TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5889  std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5890  return std::make_pair(ConstTensor(constTensorInfo, data.get()), std::move(data));
5891  }
5892  catch (InvalidArgumentException&)
5893  {
5894  throw ParseException(
5895  fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5896  GetDataTypeName(DataType::Float32),
5897  GetDataTypeName(tensorInfo.GetDataType()),
5898  CHECK_LOCATION().AsString()));
5899  }
5900  }
5901  else
5902  {
5903  return std::make_pair(ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5904  }
5905 }
5906 
5907 std::pair<armnn::ConstTensor*, std::unique_ptr<float[]>>
5908 TfLiteParserImpl::CreateConstTensorPtr(TensorRawPtr tensorPtr, armnn::TensorInfo& inputTensorInfo)
5909 {
5910  CHECK_TENSOR_PTR(tensorPtr);
5911  armnn::TensorInfo tensorInfo = ToTensorInfo(tensorPtr);
5912  auto bufferPtr = GetBuffer(m_Model, tensorPtr->buffer);
5913  CHECK_BUFFER_SIZE(bufferPtr, tensorInfo, tensorPtr->buffer);
5914 
5915  // Make sure isConstant flag is set.
5916  tensorInfo.SetConstant();
5917 
5918  if (inputTensorInfo.GetDataType() == DataType::Float32 && tensorInfo.GetDataType() != DataType::Float32)
5919  {
5920  try
5921  {
5922  TensorInfo constTensorInfo(tensorInfo.GetShape(), DataType::Float32, 0.0f, 0, true);
5923  std::unique_ptr<float[]> data = armnnUtils::ToFloatArray(bufferPtr->data, tensorInfo);
5924  return std::make_pair(new ConstTensor(constTensorInfo, data.get()), std::move(data));
5925  }
5926  catch (InvalidArgumentException&)
5927  {
5928  throw ParseException(
5929  fmt::format("Unsupported input/weights combination: Input {} not supported with Weights {}",
5930  GetDataTypeName(DataType::Float32),
5931  GetDataTypeName(tensorInfo.GetDataType()),
5932  CHECK_LOCATION().AsString()));
5933  }
5934  }
5935  else
5936  {
5937  return std::make_pair(new ConstTensor(tensorInfo, bufferPtr->data.data()), std::unique_ptr<float[]>());
5938  }
5939 }
5940 
5942  const std::string& name) const
5943 {
5944  CHECK_SUBGRAPH(m_Model, subgraphId);
5945  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
5946  for (auto const& input : inputs)
5947  {
5948  if (input.second->name == name)
5949  {
5950  auto bindingId = GenerateLayerBindingId(subgraphId, input.first);
5951  auto inputTensorInfo = ToTensorInfo(input.second);
5952  // Input tensors are always treated as constant tensors during network execution.
5953  inputTensorInfo.SetConstant(true);
5954  return std::make_pair(bindingId, inputTensorInfo);
5955  }
5956  }
5957 
5958  std::stringstream bindings;
5959  for (auto const& input : inputs)
5960  {
5961  bindings << "'" << input.second->name << "' ";
5962  }
5963 
5964  throw ParseException(
5965  fmt::format("No input binding found for subgraph:{} and name:{}. "
5966  "Possible inputs are: [{}] {}",
5967  subgraphId,
5968  name,
5969  bindings.str(),
5970  CHECK_LOCATION().AsString()));
5971 }
5972 
5974  const std::string& name) const
5975 {
5976  CHECK_SUBGRAPH(m_Model, subgraphId);
5977  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
5978  for (unsigned int i = 0; i < outputs.size(); ++i)
5979  {
5980  auto const output = outputs[i];
5981  if (output.second->name == name)
5982  {
5983  auto bindingId = GenerateLayerBindingId(subgraphId, output.first);
5984  std::vector<unsigned int> shape = m_OverriddenOutputShapes.size() > 0 ?
5985  m_OverriddenOutputShapes[i] : AsUnsignedVector(output.second->shape);
5986  return std::make_pair(bindingId, ToTensorInfo(output.second, shape));
5987  }
5988  }
5989 
5990  std::stringstream bindings;
5991  for (auto const& output : outputs)
5992  {
5993  bindings << "'" << output.second->name << "' ";
5994  }
5995 
5996  throw ParseException(
5997  fmt::format("No output binding found for subgraph:{} and name:{}. "
5998  "Possible outputs are: [{}] {}",
5999  subgraphId,
6000  name,
6001  bindings.str(),
6002  CHECK_LOCATION().AsString()));
6003 }
6004 
6006 {
6007  return m_Model->subgraphs.size();
6008 }
6009 
6010 std::vector<std::string> TfLiteParserImpl::GetSubgraphInputTensorNames(size_t subgraphId) const
6011 {
6012  CHECK_SUBGRAPH(m_Model, subgraphId);
6013  auto inputs = GetSubgraphInputs(m_Model, subgraphId);
6014  std::vector<std::string> result;
6015  result.reserve(inputs.size());
6016  for (auto const& input : inputs)
6017  {
6018  result.push_back(input.second->name);
6019  }
6020  return result;
6021 }
6022 
6023 std::vector<std::string> TfLiteParserImpl::GetSubgraphOutputTensorNames(size_t subgraphId) const
6024 {
6025  CHECK_SUBGRAPH(m_Model, subgraphId);
6026  auto outputs = GetSubgraphOutputs(m_Model, subgraphId);
6027  std::vector<std::string> result;
6028  result.reserve(outputs.size());
6029  for (auto const& output : outputs)
6030  {
6031  result.push_back(output.second->name);
6032  }
6033  return result;
6034 }
6035 
6036 const std::string TfLiteParserImpl::GetVersion()
6037 {
6038  return TFLITE_PARSER_VERSION;
6039 }
6040 
6041 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<float[]>&& data)
6042 : m_FloatData(std::move(data))
6043 , m_Uint8Data(nullptr)
6044 , m_Int8Data(nullptr)
6045 , m_Int32Data(nullptr)
6046 {
6047 }
6048 
6049 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<uint8_t[]>&& data)
6050 : m_FloatData(nullptr)
6051 , m_Uint8Data(std::move(data))
6052 , m_Int8Data(nullptr)
6053 , m_Int32Data(nullptr)
6054 {
6055 }
6056 
6057 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int8_t[]>&& data)
6058 : m_FloatData(nullptr)
6059 , m_Uint8Data(nullptr)
6060 , m_Int8Data(std::move(data))
6061 , m_Int32Data(nullptr)
6062 {
6063 }
6064 
6065 TfLiteParserImpl::SupportedDataStorage::SupportedDataStorage(std::unique_ptr<int32_t[]>&& data)
6066 : m_FloatData(nullptr)
6067 , m_Uint8Data(nullptr)
6068 , m_Int8Data(nullptr)
6069 , m_Int32Data(std::move(data))
6070 {
6071 }
6072 
6073 } // armnnTfLiteParser
ARMNN_ASSERT
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
armnn::Convolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:570
armnn::ArgMinMaxFunction::Max
@ Max
armnn::INetworkPtr
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:339
armnnTfLiteParser::TfLiteParserImpl::GetInputs
static TensorRawPtrVector GetInputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5451
armnn::LstmInputParams::m_RecurrentToForgetWeights
const ConstTensor * m_RecurrentToForgetWeights
Definition: LstmParams.hpp:45
armnnTfLiteParser::TfLiteParserImpl::GetOutputs
static TensorRawPtrVector GetOutputs(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5478
armnn::DataType::Boolean
@ Boolean
armnn::FullyConnectedDescriptor::m_ConstantWeights
bool m_ConstantWeights
Enable/disable constant weights and biases.
Definition: Descriptors.hpp:530
armnn::Pooling2dDescriptor::m_PaddingMethod
PaddingMethod m_PaddingMethod
The padding method to be used. (Exclude, IgnoreValue).
Definition: Descriptors.hpp:425
armnn::ViewsDescriptor
A ViewsDescriptor for the SplitterLayer.
Definition: Descriptors.hpp:244
armnn::TensorInfo::GetNumElements
unsigned int GetNumElements() const
Definition: Tensor.hpp:198
armnn::LstmInputParams::m_OutputLayerNormWeights
const ConstTensor * m_OutputLayerNormWeights
Definition: LstmParams.hpp:60
armnn::DetectionPostProcessDescriptor::m_NmsScoreThreshold
float m_NmsScoreThreshold
NMS score threshold.
Definition: Descriptors.hpp:751
armnn::ActivationDescriptor
An ActivationDescriptor for the ActivationLayer.
Definition: Descriptors.hpp:36
armnn::TransposeConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:1469
armnnDeserializer::TensorRawPtr
const armnnSerializer::TensorInfo * TensorRawPtr
Definition: Deserializer.hpp:20
CHECK_BUFFER_SIZE
#define CHECK_BUFFER_SIZE(BUFFER_PTR, TENSOR_INFO, BUFFER_ID)
Definition: TfLiteParser.cpp:326
armnn::FullyConnectedDescriptor
A FullyConnectedDescriptor for the FullyConnectedLayer.
Definition: Descriptors.hpp:507
armnn::DetectionPostProcessDescriptor::m_ScaleX
float m_ScaleX
Center size encoding scale x.
Definition: Descriptors.hpp:759
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtrVector
std::vector< TensorRawPtr > TensorRawPtrVector
Definition: TfLiteParser.hpp:36
armnn::ComparisonOperation::LessOrEqual
@ LessOrEqual
armnnDeserializer::CheckShape
bool CheckShape(const armnn::TensorShape &actual, const std::vector< uint32_t > &expected)
Definition: Deserializer.cpp:188
armnn::TensorInfo::GetNumBytes
unsigned int GetNumBytes() const
Definition: Tensor.cpp:427
armnn::TransposeConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:1477
armnnTfLiteParser::TfLiteParserImpl::TensorRawPtr
const tflite::TensorT * TensorRawPtr
Definition: TfLiteParser.hpp:35
armnnTfLiteParser::TfLiteParserImpl::CreateNetworkFromBinaryFile
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
Definition: TfLiteParser.cpp:957
armnn::Optional
Definition: Optional.hpp:270
armnn::IConnectableLayer::GetNumInputSlots
virtual unsigned int GetNumInputSlots() const =0
Returns the number of connectable input slots.
armnn::LstmDescriptor::m_TimeMajor
bool m_TimeMajor
Enable/disable time major.
Definition: Descriptors.hpp:1154
armnn::ResizeMethod
ResizeMethod
Definition: Types.hpp:166
Descriptors.hpp
armnn::SpaceToBatchNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1071
armnn::FullyConnectedDescriptor::m_TransposeWeightMatrix
bool m_TransposeWeightMatrix
Enable/disable transpose weight matrix.
Definition: Descriptors.hpp:528
armnn::ResizeDescriptor::m_TargetHeight
uint32_t m_TargetHeight
Target height value.
Definition: Descriptors.hpp:1009
armnn::DepthwiseConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:708
armnn::Pooling2dDescriptor::m_PoolHeight
uint32_t m_PoolHeight
Pooling height value.
Definition: Descriptors.hpp:417
armnn::StridedSliceDescriptor::m_Begin
std::vector< int > m_Begin
Begin values for the input that will be sliced.
Definition: Descriptors.hpp:1342
armnn::LstmInputParams::m_ProjectionBias
const ConstTensor * m_ProjectionBias
Definition: LstmParams.hpp:56
armnn::DetectionPostProcessDescriptor::m_ScaleY
float m_ScaleY
Center size encoding scale y.
Definition: Descriptors.hpp:761
armnn::DetectionPostProcessDescriptor::m_MaxDetections
uint32_t m_MaxDetections
Maximum numbers of detections.
Definition: Descriptors.hpp:745
armnn::LstmInputParams::m_RecurrentToCellWeights
const ConstTensor * m_RecurrentToCellWeights
Definition: LstmParams.hpp:46
armnn::DataLayout::NHWC
@ NHWC
armnnTfLiteParser::TfLiteParserImpl::CreateNetworkFromBinary
armnn::INetworkPtr CreateNetworkFromBinary(const std::vector< uint8_t > &binaryContent)
Create the network from a flatbuffers binary.
Definition: TfLiteParser.cpp:964
armnn::LstmInputParams::m_CellBias
const ConstTensor * m_CellBias
Definition: LstmParams.hpp:53
armnn::Convolution3dDescriptor::m_PadFront
uint32_t m_PadFront
Padding front value in the depth dimension.
Definition: Descriptors.hpp:637
armnn::ResizeDescriptor
A ResizeDescriptor for the ResizeLayer.
Definition: Descriptors.hpp:985
armnn::ArgMinMaxDescriptor
An ArgMinMaxDescriptor for ArgMinMaxLayer.
Definition: Descriptors.hpp:67
armnn::IConnectableLayer::GetName
virtual const char * GetName() const =0
Returns the name of the layer.
armnnTfLiteParser::TfLiteParserImpl::SubgraphPtr
std::unique_ptr< tflite::SubGraphT > SubgraphPtr
Definition: TfLiteParser.hpp:31
armnn::ActivationDescriptor::m_A
float m_A
Alpha upper bound value used by the activation functions. (BoundedReLu, Linear, TanH,...
Definition: Descriptors.hpp:61
armnn::CheckLocation::m_Function
const char * m_Function
Definition: Exceptions.hpp:16
armnn::StridedSliceDescriptor::m_EllipsisMask
int32_t m_EllipsisMask
Ellipsis mask value.
Definition: Descriptors.hpp:1357
armnnUtils::GetUnsignedAxis
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis)
Definition: TensorUtils.cpp:236
armnn::SoftmaxDescriptor::m_Beta
float m_Beta
Exponentiation value.
Definition: Descriptors.hpp:190
armnn::GatherDescriptor
A GatherDescriptor for the GatherLayer.
Definition: Descriptors.hpp:965
armnn::TensorInfo::GetQuantizationScale
float GetQuantizationScale() const
Definition: Tensor.cpp:461
TypesUtils.hpp
armnn::DepthwiseConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:710
armnn::L2NormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:824
armnn::TensorInfo
Definition: Tensor.hpp:152
armnn::L2NormalizationDescriptor
A L2NormalizationDescriptor for the L2NormalizationLayer.
Definition: Descriptors.hpp:809
armnn::NormalizationAlgorithmMethod::LocalBrightness
@ LocalBrightness
Krichevsky 2012: Local Brightness Normalization.
armnn::NormalizationDescriptor::m_Beta
float m_Beta
Beta value for the normalization equation.
Definition: Descriptors.hpp:801
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputs
static TensorIdRawPtrVector GetSubgraphOutputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5515
armnn::GetDataTypeName
constexpr const char * GetDataTypeName(DataType dataType)
Definition: TypesUtils.hpp:233
armnn::TensorInfo::SetDataType
void SetDataType(DataType type)
Definition: Tensor.hpp:201
armnn::NormalizationDescriptor
A NormalizationDescriptor for the NormalizationLayer.
Definition: Descriptors.hpp:769
armnn::Pooling2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:421
armnn::TensorInfo::GetNumDimensions
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:197
armnn::BatchToSpaceNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape values.
Definition: Descriptors.hpp:898
CHECK_LOCATION
#define CHECK_LOCATION()
Definition: Exceptions.hpp:203
armnn::DataType::Float32
@ Float32
armnn::ResizeDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1014
Version.hpp
armnn::DepthwiseConvolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:692
armnn::Convolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:576
armnn::Pooling2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:411
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputTensorNames
std::vector< std::string > GetSubgraphInputTensorNames(size_t subgraphId) const
Return the input tensor names for a given subgraph.
Definition: TfLiteParser.cpp:6010
armnn::Convolution3dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:633
armnnTfLiteParser::TfLiteParserImpl::GetOutputTensorIds
static std::vector< int32_t > & GetOutputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5541
armnn::ArgMinMaxDescriptor::m_Function
ArgMinMaxFunction m_Function
Specify if the function is to find Min or Max.
Definition: Descriptors.hpp:81
armnn::SpaceToBatchNdDescriptor::m_BlockShape
std::vector< unsigned int > m_BlockShape
Block shape value.
Definition: Descriptors.hpp:1066
armnn::StridedSliceDescriptor::m_BeginMask
int32_t m_BeginMask
Begin mask value.
Definition: Descriptors.hpp:1350
armnn::LstmInputParams::m_CellToOutputWeights
const ConstTensor * m_CellToOutputWeights
Definition: LstmParams.hpp:50
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphOutputTensorNames
std::vector< std::string > GetSubgraphOutputTensorNames(size_t subgraphId) const
Return the output tensor names for a given subgraph.
Definition: TfLiteParser.cpp:6023
armnnUtils::ProcessConcatInputTensorInfo
void ProcessConcatInputTensorInfo(armnn::TensorInfo &inputTensorInfo, armnn::OriginsDescriptor &concatDescriptor, const unsigned int &concatAxis, unsigned int inputIndex, unsigned int &mergeDimOrigin)
Definition: ParserHelper.cpp:19
armnn::Convolution3dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:647
armnn::LstmInputParams::m_InputToCellWeights
const ConstTensor * m_InputToCellWeights
Definition: LstmParams.hpp:42
armnn::Convolution3dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:635
armnn::MaxNumOfTensorDimensions
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromFile
static ModelPtr LoadModelFromFile(const char *fileName)
Definition: TfLiteParser.cpp:5402
armnn::DataType::QAsymmU8
@ QAsymmU8
armnn::ArgMinMaxFunction
ArgMinMaxFunction
Definition: Types.hpp:103
armnn::DetectionPostProcessDescriptor::m_ScaleW
float m_ScaleW
Center size encoding scale weight.
Definition: Descriptors.hpp:763
armnn::LstmDescriptor::m_InputIntermediateScale
float m_InputIntermediateScale
Input intermediate quantization scale.
Definition: Descriptors.hpp:1156
armnn::DataType::QSymmS8
@ QSymmS8
armnn::StackDescriptor
A StackDescriptor for the StackLayer.
Definition: Descriptors.hpp:1251
armnn::IConnectableLayer::InferOutputShapes
virtual std::vector< TensorShape > InferOutputShapes(const std::vector< TensorShape > &inputShapes) const =0
Infer the shape of the output(s) based on the provided input shape(s)
armnnUtils::Permute
void Permute(const armnn::TensorShape &dstShape, const armnn::PermutationVector &mappings, const void *src, void *dst, size_t dataTypeSize)
Definition: Permute.cpp:164
armnn::IOutputSlot::GetTensorInfo
virtual const TensorInfo & GetTensorInfo() const =0
IgnoreUnused.hpp
armnn::NormalizationDescriptor::m_NormSize
uint32_t m_NormSize
Depth radius value.
Definition: Descriptors.hpp:797
armnn::BroadcastToDescriptor::m_BroadcastToShape
TensorShape m_BroadcastToShape
Target shape value.
Definition: Descriptors.hpp:1675
armnnUtils::Permuted
armnn::TensorShape Permuted(const armnn::TensorShape &srcShape, const armnn::PermutationVector &mappings)
Definition: Permute.cpp:125
armnnTfLiteParser::TfLiteParserImpl::BufferRawPtr
const tflite::BufferT * BufferRawPtr
Definition: TfLiteParser.hpp:40
armnnTfLiteParser::TfLiteParserImpl::OutputShapeOfReshape
static armnn::TensorInfo OutputShapeOfReshape(const armnn::TensorInfo &inputTensorInfo, const std::vector< int32_t > &targetDimsIn)
Definition: TfLiteParser.cpp:3311
armnn::Pooling2dDescriptor::m_PoolWidth
uint32_t m_PoolWidth
Pooling width value.
Definition: Descriptors.hpp:415
armnn::UnaryOperation::Neg
@ Neg
armnn::Convolution2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:566
armnn::BatchToSpaceNdDescriptor::m_Crops
std::vector< std::pair< unsigned int, unsigned int > > m_Crops
The values to crop from the input dimension.
Definition: Descriptors.hpp:900
armnn::DepthwiseConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:702
armnnOnnxParser::ModelPtr
std::unique_ptr< onnx::ModelProto > ModelPtr
Definition: OnnxParser.hpp:23
armnn::CheckLocation::AsString
std::string AsString() const
Definition: Exceptions.hpp:29
armnn::Convolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:580
armnn::DetectionPostProcessDescriptor::m_MaxClassesPerDetection
uint32_t m_MaxClassesPerDetection
Maximum numbers of classes per detection, used in Fast NMS.
Definition: Descriptors.hpp:747
armnn::IConnectableLayer::GetNumOutputSlots
virtual unsigned int GetNumOutputSlots() const =0
Returns the number of connectable output slots.
armnn::DataType::QSymmS16
@ QSymmS16
armnn::NormalizationDescriptor::m_NormMethodType
NormalizationAlgorithmMethod m_NormMethodType
Normalization method algorithm to use (LocalBrightness, LocalContrast).
Definition: Descriptors.hpp:795
armnn::TransposeConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:1475
tflite
Definition: armnn_external_delegate.cpp:12
NumericCast.hpp
armnn::LstmInputParams::m_ForgetGateBias
const ConstTensor * m_ForgetGateBias
Definition: LstmParams.hpp:52
armnn::NormalizationAlgorithmChannel::Across
@ Across
armnn::ReduceDescriptor::m_ReduceOperation
ReduceOperation m_ReduceOperation
Specifies the reduction operation to execute.
Definition: Descriptors.hpp:1558
TensorUtils.hpp
ARMNN_THROW_PARSE_EXCEPTION
#define ARMNN_THROW_PARSE_EXCEPTION(msg)
Definition: TfLiteParser.cpp:45
armnn::ComparisonOperation::NotEqual
@ NotEqual
CHECK_TENSOR_PTR
#define CHECK_TENSOR_PTR(TENSOR_PTR)
Definition: TfLiteParser.cpp:220
armnn::LstmInputParams::m_CellToInputWeights
const ConstTensor * m_CellToInputWeights
Definition: LstmParams.hpp:48
armnn::ComparisonOperation::GreaterOrEqual
@ GreaterOrEqual
armnn::Exception::what
virtual const char * what() const noexcept override
Definition: Exceptions.cpp:32
armnn::MeanDescriptor::m_KeepDims
bool m_KeepDims
Enable/disable keep dimensions. If true, then the reduced dimensions that are of length 1 are kept.
Definition: Descriptors.hpp:1192
ARMNN_LOG
#define ARMNN_LOG(severity)
Definition: Logging.hpp:212
armnn::DataLayout::NDHWC
@ NDHWC
armnnTfLiteParser::TfLiteParserImpl::LoadModelFromBinary
static ModelPtr LoadModelFromBinary(const uint8_t *binaryContent, size_t len)
Definition: TfLiteParser.cpp:5432
Assert.hpp
CHECKED_NON_NEGATIVE
#define CHECKED_NON_NEGATIVE(VALUE)
Definition: VerificationHelpers.hpp:35
armnn::ResizeDescriptor::m_Method
ResizeMethod m_Method
The Interpolation method to use (Bilinear, NearestNeighbor).
Definition: Descriptors.hpp:1012
armnn::SpaceToBatchNdDescriptor::m_PadList
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding values for the input dimension: heightPad{top, bottom} widthPad{left,...
Definition: Descriptors.hpp:1069
armnn::LstmInputParams::m_InputToOutputWeights
const ConstTensor * m_InputToOutputWeights
Definition: LstmParams.hpp:43
armnn::LstmDescriptor::m_PeepholeEnabled
bool m_PeepholeEnabled
Enable/disable peephole.
Definition: Descriptors.hpp:1148
armnn::TensorShape
Definition: Tensor.hpp:20
armnnTfLiteParser::TfLiteParserImpl::GetNetworkInputBindingInfo
BindingPointInfo GetNetworkInputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network input identified by the given layer ...
Definition: TfLiteParser.cpp:5941
armnn::Convolution3dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:631
VerificationHelpers.hpp
armnn::NetworkOptions
std::vector< BackendOptions > NetworkOptions
Definition: BackendOptions.hpp:16
armnn::NormalizationDescriptor::m_NormChannelType
NormalizationAlgorithmChannel m_NormChannelType
Normalization channel algorithm to use (Across, Within).
Definition: Descriptors.hpp:793
LstmParams.hpp
armnn::IOutputSlot
An output connection slot for a layer.
Definition: INetwork.hpp:53
armnn::LstmInputParams::m_CellToForgetWeights
const ConstTensor * m_CellToForgetWeights
Definition: LstmParams.hpp:49
armnn::StackDescriptor::m_NumInputs
uint32_t m_NumInputs
Number of input tensors.
Definition: Descriptors.hpp:1275
CHECK_TENSOR
#define CHECK_TENSOR(MODEL, SUBGRAPH_INDEX, TENSOR_INDEX)
Definition: TfLiteParser.cpp:207
armnn::DataType::Float16
@ Float16
armnn::CheckLocation
Definition: Exceptions.hpp:14
armnnTfLiteParser::BindingPointInfo
armnn::BindingPointInfo BindingPointInfo
Definition: ITfLiteParser.hpp:20
armnn::LstmInputParams::m_RecurrentToInputWeights
const ConstTensor * m_RecurrentToInputWeights
Definition: LstmParams.hpp:44
armnn::LstmDescriptor::m_ClippingThresProj
float m_ClippingThresProj
Clipping threshold value for the projection.
Definition: Descriptors.hpp:1144
armnn::StridedSliceDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1363
armnn::Pooling2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:427
armnn::UnaryOperation::Rsqrt
@ Rsqrt
armnn::LstmInputParams::m_InputToInputWeights
const ConstTensor * m_InputToInputWeights
Definition: LstmParams.hpp:40
armnn::TensorShape::GetNumDimensions
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:174
armnn::DepthwiseConvolution2dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation factor value for height dimension.
Definition: Descriptors.hpp:706
armnn::ComparisonOperation::Less
@ Less
armnn::UnaryOperation::Sqrt
@ Sqrt
armnn::UnaryOperation::LogicalNot
@ LogicalNot
armnn::Pooling2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:413
armnn::LstmInputParams::m_RecurrentToOutputWeights
const ConstTensor * m_RecurrentToOutputWeights
Definition: LstmParams.hpp:47
armnn::Pooling2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:409
armnn::FullyConnectedDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:526
Logging.hpp
armnn::PadDescriptor
A PadDescriptor for the PadLayer.
Definition: Descriptors.hpp:1196
armnn::UnaryOperation::Exp
@ Exp
armnn::IOutputSlot::SetTensorInfo
virtual void SetTensorInfo(const TensorInfo &tensorInfo)=0
armnn::TransposeDescriptor
A TransposeDescriptor for the TransposeLayer.
Definition: Descriptors.hpp:1490
armnn::Convolution3dDescriptor::m_DilationZ
uint32_t m_DilationZ
Dilation along z axis.
Definition: Descriptors.hpp:651
armnn::DetectionPostProcessDescriptor::m_NumClasses
uint32_t m_NumClasses
Number of classes.
Definition: Descriptors.hpp:755
armnn_driver::Model
::android::nn::Model Model
Helper classes.
Definition: ConversionUtils.hpp:45
armnn::EmptyOptional
EmptyOptional is used to initialize the Optional class in case we want to have default value for an O...
Definition: Optional.hpp:32
armnn::StackDescriptor::m_Axis
uint32_t m_Axis
0-based axis along which to stack the input tensors.
Definition: Descriptors.hpp:1273
armnn::SliceDescriptor
A SliceDescriptor for the SliceLayer.
Definition: Descriptors.hpp:1228
armnn::DataType
DataType
Definition: Types.hpp:48
armnn::TensorInfo::IsQuantized
bool IsQuantized() const
Definition: Tensor.cpp:508
armnn::LstmInputParams::m_InputGateBias
const ConstTensor * m_InputGateBias
Definition: LstmParams.hpp:51
armnn::DetectionPostProcessDescriptor::m_NmsIouThreshold
float m_NmsIouThreshold
Intersection over union threshold.
Definition: Descriptors.hpp:753
armnn::TensorInfo::IsTypeSpaceMatch
bool IsTypeSpaceMatch(const TensorInfo &other) const
Check that the types are the same and, if quantize, that the quantization parameters are the same.
Definition: Tensor.cpp:432
armnn::Convolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:582
armnn::ReshapeDescriptor
A ReshapeDescriptor for the ReshapeLayer.
Definition: Descriptors.hpp:1023
armnn::LstmDescriptor::m_HiddenStateScale
float m_HiddenStateScale
Hidden State quantization scale.
Definition: Descriptors.hpp:1166
armnnTfLiteParser::ITfLiteParserPtr
std::unique_ptr< ITfLiteParser, void(*)(ITfLiteParser *parser)> ITfLiteParserPtr
Definition: ITfLiteParser.hpp:24
armnn::InvalidArgumentException
Definition: Exceptions.hpp:80
armnn::UnaryOperation::Sin
@ Sin
CHECK_MODEL
#define CHECK_MODEL(MODEL, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:186
armnnTfLiteParser::ITfLiteParser::CreateNetworkFromBinaryFile
armnn::INetworkPtr CreateNetworkFromBinaryFile(const char *graphFile)
Create the network from a flatbuffers binary file on disk.
Definition: TfLiteParser.cpp:77
armnn::PadDescriptor::m_PadValue
float m_PadValue
Optional value to use for padding, defaults to 0.
Definition: Descriptors.hpp:1221
armnn::LayerBindingId
int LayerBindingId
Type of identifiers for bindable layers (inputs, outputs).
Definition: Types.hpp:309
armnn::DepthwiseConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:694
armnn::Convolution3dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:629
armnn::ActivationDescriptor::m_Function
ActivationFunction m_Function
The activation function to use (Sigmoid, TanH, Linear, ReLu, BoundedReLu, SoftReLu,...
Definition: Descriptors.hpp:59
armnnTfLiteParser::TfLiteParserImpl::TensorIdRawPtrVector
std::vector< TensorIdRawPtr > TensorIdRawPtrVector
Definition: TfLiteParser.hpp:38
armnn::NormalizationDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:805
armnn::LstmDescriptor::m_HiddenStateZeroPoint
int32_t m_HiddenStateZeroPoint
Hidden State zero point.
Definition: Descriptors.hpp:1164
armnn::BatchMatMulDescriptor
A BatchMatMulDescriptor for the BatchMatMul operator.
Definition: Descriptors.hpp:1584
armnn::Convolution3dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:643
armnn::ReduceOperation::Sum
@ Sum
armnn::IConnectableLayer::GetType
virtual LayerType GetType() const =0
Returns the armnn::LayerType of this layer.
armnnTfLiteParser::TfLiteParserImpl::GetNetworkOutputBindingInfo
BindingPointInfo GetNetworkOutputBindingInfo(size_t subgraphId, const std::string &name) const
Retrieve binding info (layer id and tensor info) for the network output identified by the given layer...
Definition: TfLiteParser.cpp:5973
armnn::Convolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:584
Filesystem.hpp
armnnTfLiteParser::TfLiteParserImpl::LoadModel
armnn::INetworkPtr LoadModel(std::unique_ptr< tflite::ModelT > model)
Definition: TfLiteParser.cpp:972
armnn::GatherDescriptor::m_Axis
int32_t m_Axis
The axis in params to gather indices from.
Definition: Descriptors.hpp:981
armnn::Convolution3dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:641
armnn::SpaceToBatchNdDescriptor
A SpaceToBatchNdDescriptor for the SpaceToBatchNdLayer.
Definition: Descriptors.hpp:1043
armnn::Convolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:572
armnn::PermutationVector
Definition: Types.hpp:314
armnnTfLiteParser::TfLiteParserImpl::GetVersion
static const std::string GetVersion()
Retrieve version in X.Y.Z form.
Definition: TfLiteParser.cpp:6036
armnn::Convolution3dDescriptor
A Convolution3dDescriptor for the Convolution3dLayer.
Definition: Descriptors.hpp:588
armnn::ReshapeDescriptor::m_TargetShape
TensorShape m_TargetShape
Target shape value.
Definition: Descriptors.hpp:1039
armnn::TransposeConvolution2dDescriptor::m_StrideY
uint32_t m_StrideY
Stride value when proceeding through input for the height dimension.
Definition: Descriptors.hpp:1479
armnn::StridedSliceDescriptor::m_EndMask
int32_t m_EndMask
End mask value.
Definition: Descriptors.hpp:1353
armnn::BaseTensor::GetInfo
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:297
ParserHelper.hpp
armnn::Pooling2dDescriptor::m_PadLeft
uint32_t m_PadLeft
Padding left value in the width dimension.
Definition: Descriptors.hpp:407
Permute.hpp
armnn::ActivationFunction
ActivationFunction
Definition: Types.hpp:86
armnn::BoostLogSeverityMapping::info
@ info
CHECK_SUPPORTED_FUSED_ACTIVATION
#define CHECK_SUPPORTED_FUSED_ACTIVATION(OPTION, SUBGRAPH_INDEX, OPERATOR_INDEX)
Definition: TfLiteParser.cpp:347
armnn::StackDescriptor::m_InputShape
TensorShape m_InputShape
Required shape of all input tensors.
Definition: Descriptors.hpp:1277
armnn::UnaryOperation
UnaryOperation
Definition: Types.hpp:125
armnn::Convolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:574
armnn::Convolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:568
armnn::TensorInfo::GetDataType
DataType GetDataType() const
Definition: Tensor.hpp:200
armnnTfLiteParser::TfLiteParserImpl
Definition: TfLiteParser.hpp:26
armnn::DetectionPostProcessDescriptor::m_DetectionsPerClass
uint32_t m_DetectionsPerClass
Detections per classes, used in Regular NMS.
Definition: Descriptors.hpp:749
armnn::DetectionPostProcessDescriptor::m_ScaleH
float m_ScaleH
Center size encoding scale height.
Definition: Descriptors.hpp:765
armnn::DataType::Signed32
@ Signed32
armnn::UnaryOperation::Ceil
@ Ceil
armnn::LstmInputParams::m_InputLayerNormWeights
const ConstTensor * m_InputLayerNormWeights
Definition: LstmParams.hpp:57
armnn::ReduceDescriptor::m_KeepDims
bool m_KeepDims
if true then output shape has no change.
Definition: Descriptors.hpp:1554
armnn::BatchToSpaceNdDescriptor
A BatchToSpaceNdDescriptor for the BatchToSpaceNdLayer.
Definition: Descriptors.hpp:875
armnn::Convolution2dDescriptor
A Convolution2dDescriptor for the Convolution2dLayer.
Definition: Descriptors.hpp:534
armnn::ReduceOperation::Prod
@ Prod
armnn::DepthwiseConvolution2dDescriptor::m_PadBottom
uint32_t m_PadBottom
Padding bottom value in the height dimension.
Definition: Descriptors.hpp:698
armnn::ComparisonDescriptor
A ComparisonDescriptor for the ComparisonLayer.
Definition: Descriptors.hpp:89
armnn::DataType::QAsymmS8
@ QAsymmS8
armnn::ComparisonDescriptor::m_Operation
ComparisonOperation m_Operation
Specifies the comparison operation to execute.
Definition: Descriptors.hpp:105
armnn::LstmDescriptor::m_OutputIntermediateScale
float m_OutputIntermediateScale
Output intermediate quantization scale.
Definition: Descriptors.hpp:1162
armnn::ElementwiseUnaryDescriptor::m_Operation
UnaryOperation m_Operation
Specifies the elementwiseUnary operation to execute.
Definition: Descriptors.hpp:145
armnn::ArgMinMaxFunction::Min
@ Min
armnn::StridedSliceDescriptor::m_ShrinkAxisMask
int32_t m_ShrinkAxisMask
Shrink axis mask value. If set, the nth specification shrinks the dimensionality by 1.
Definition: Descriptors.hpp:1355
armnn::StandInDescriptor
A StandInDescriptor for the StandIn layer.
Definition: Descriptors.hpp:1281
armnn::Pooling2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:419
armnn::SpaceToDepthDescriptor::m_BlockSize
unsigned int m_BlockSize
Scalar specifying the input block size. It must be >= 1.
Definition: Descriptors.hpp:1092
armnn::LstmInputParams::m_ForgetLayerNormWeights
const ConstTensor * m_ForgetLayerNormWeights
Definition: LstmParams.hpp:58
armnn::UnaryOperation::Log
@ Log
armnn::ResizeDescriptor::m_TargetWidth
uint32_t m_TargetWidth
Target width value.
Definition: Descriptors.hpp:1007
armnn::IOutputSlot::GetOwningIConnectableLayer
virtual const IConnectableLayer & GetOwningIConnectableLayer() const =0
armnn::StridedSliceDescriptor::m_Stride
std::vector< int > m_Stride
Stride values for the input that will be sliced.
Definition: Descriptors.hpp:1346
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphInputs
static TensorIdRawPtrVector GetSubgraphInputs(const ModelPtr &model, size_t subgraphIndex)
Definition: TfLiteParser.cpp:5498
armnn::BackendOptions
Struct for the users to pass backend specific options.
Definition: BackendOptions.hpp:22
armnnDeserializer::ToTensorInfo
armnn::TensorInfo ToTensorInfo(TensorRawPtr tensorPtr)
Definition: Deserializer.cpp:676
armnn::PermutationVector::GetSize
SizeType GetSize() const
Definition: Types.hpp:357
armnn::LstmDescriptor
An LstmDescriptor for the LstmLayer.
Definition: Descriptors.hpp:1102
armnn::ComparisonOperation
ComparisonOperation
Definition: Types.hpp:109
armnn::StridedSliceDescriptor
A StridedSliceDescriptor for the StridedSliceLayer.
Definition: Descriptors.hpp:1303
armnn::TransposeConvolution2dDescriptor::m_OutputShape
std::vector< unsigned int > m_OutputShape
Definition: Descriptors.hpp:1486
Tensor.hpp
armnnTfLiteParser::TfLiteParserImpl::ModelPtr
std::unique_ptr< tflite::ModelT > ModelPtr
Definition: TfLiteParser.hpp:30
armnn::ResizeDescriptor::m_AlignCorners
bool m_AlignCorners
Aligned corners.
Definition: Descriptors.hpp:1016
armnn::TileDescriptor::m_Multiples
std::vector< uint32_t > m_Multiples
The vector to multiply the input shape by.
Definition: Descriptors.hpp:1656
armnnOnnxParser::CreateConstTensorImpl
std::pair< armnn::ConstTensor, std::unique_ptr< T[]> > CreateConstTensorImpl(const T *bufferPtr, armnn::TensorInfo &tensorInfo, const armnn::Optional< armnn::PermutationVector & > permutationVector)
Definition: OnnxParser.cpp:604
armnn::MeanDescriptor::m_Axis
std::vector< unsigned int > m_Axis
Values for the dimensions to reduce.
Definition: Descriptors.hpp:1190
armnn::LstmDescriptor::m_CifgEnabled
bool m_CifgEnabled
Enable/disable cifg (coupled input & forget gate).
Definition: Descriptors.hpp:1146
armnn::IOutputSlot::Connect
virtual int Connect(IInputSlot &destination)=0
armnn::NormalizationDescriptor::m_Alpha
float m_Alpha
Alpha value for the normalization equation.
Definition: Descriptors.hpp:799
armnn::GetUnaryOperationAsCString
constexpr char const * GetUnaryOperationAsCString(UnaryOperation operation)
Definition: TypesUtils.hpp:92
armnn::LstmDescriptor::m_ForgetIntermediateScale
float m_ForgetIntermediateScale
Forget intermediate quantization scale.
Definition: Descriptors.hpp:1158
armnn::TensorInfo::GetShape
const TensorShape & GetShape() const
Definition: Tensor.hpp:193
armnn::Convolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation along x axis.
Definition: Descriptors.hpp:578
armnn::Convolution3dDescriptor::m_PadBack
uint32_t m_PadBack
Padding back value in the depth dimension.
Definition: Descriptors.hpp:639
armnn::ReduceDescriptor::m_vAxis
std::vector< uint32_t > m_vAxis
The indices of the dimensions to reduce.
Definition: Descriptors.hpp:1556
armnn::PadDescriptor::m_PadList
std::vector< std::pair< unsigned int, unsigned int > > m_PadList
Specifies the padding for input dimension.
Definition: Descriptors.hpp:1218
std
Definition: BackendId.hpp:149
armnn::ParseException
Definition: Exceptions.hpp:92
armnn::LstmDescriptor::m_LayerNormEnabled
bool m_LayerNormEnabled
Enable/disable layer normalization.
Definition: Descriptors.hpp:1152
armnn::IgnoreUnused
void IgnoreUnused(Ts &&...)
Definition: IgnoreUnused.hpp:14
armnn::TransposeConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:1473
armnn::TransposeConvolution2dDescriptor::m_PadRight
uint32_t m_PadRight
Padding right value in the width dimension.
Definition: Descriptors.hpp:1471
armnnTfLiteParser::TfLiteParserImpl::OperatorPtr
std::unique_ptr< tflite::OperatorT > OperatorPtr
Definition: TfLiteParser.hpp:32
TFLITE_PARSER_VERSION
#define TFLITE_PARSER_VERSION
TFLITE_PARSER_VERSION: "X.Y.Z" where: X = Major version number Y = Minor version number Z = Patch ver...
Definition: Version.hpp:25
TfLiteParser.hpp
armnn::LstmInputParams::m_OutputGateBias
const ConstTensor * m_OutputGateBias
Definition: LstmParams.hpp:54
armnn::ScatterNdFunction::Update
@ Update
BackendOptions.hpp
armnn::Convolution3dDescriptor::m_DilationY
uint32_t m_DilationY
Dilation along y axis.
Definition: Descriptors.hpp:649
armnn::OriginsDescriptor
An OriginsDescriptor for the ConcatLayer.
Definition: Descriptors.hpp:201
armnn::LstmInputParams::m_ProjectionWeights
const ConstTensor * m_ProjectionWeights
Definition: LstmParams.hpp:55
armnn::LstmInputParams::m_InputToForgetWeights
const ConstTensor * m_InputToForgetWeights
Definition: LstmParams.hpp:41
armnn::IConnectableLayer::GetOutputSlot
virtual const IOutputSlot & GetOutputSlot(unsigned int index) const =0
Get the const output slot handle by slot index.
armnn::TensorInfo::SetShape
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:195
Exceptions.hpp
armnn
Copyright (c) 2021 ARM Limited and Contributors.
Definition: 01_00_quick_start.dox:6
armnn::ElementwiseUnaryDescriptor
A ElementwiseUnaryDescriptor for the ElementwiseUnaryLayer.
Definition: Descriptors.hpp:129
armnn::TransposeConvolution2dDescriptor
A TransposeConvolution2dDescriptor for the TransposeConvolution2dLayer.
Definition: Descriptors.hpp:1440
armnn::IConnectableLayer::GetInputSlot
virtual const IInputSlot & GetInputSlot(unsigned int index) const =0
Get a const input slot handle by slot index.
CHECK_VALID_SIZE
#define CHECK_VALID_SIZE(ACTUAL,...)
Definition: VerificationHelpers.hpp:32
armnn::Convolution3dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:653
armnn::ArgMinMaxDescriptor::m_Axis
int m_Axis
Axis to reduce across the input tensor.
Definition: Descriptors.hpp:83
armnn::ActivationDescriptor::m_B
float m_B
Beta lower bound value used by the activation functions. (BoundedReLu, Linear, TanH).
Definition: Descriptors.hpp:63
armnn::Convolution3dDescriptor::m_StrideZ
uint32_t m_StrideZ
Stride value when proceeding through input for the depth dimension.
Definition: Descriptors.hpp:645
armnn::DetectionPostProcessDescriptor::m_UseRegularNms
bool m_UseRegularNms
Use Regular NMS.
Definition: Descriptors.hpp:757
armnn::LstmInputParamsInfo
Definition: LstmParams.hpp:63
armnn::ReduceOperation
ReduceOperation
Definition: Types.hpp:157
armnn::NormalizationDescriptor::m_K
float m_K
Kappa value used for the across channel normalization equation.
Definition: Descriptors.hpp:803
armnn::UnaryOperation::Abs
@ Abs
armnn::LstmDescriptor::m_ProjectionEnabled
bool m_ProjectionEnabled
Enable/disable the projection layer.
Definition: Descriptors.hpp:1150
armnn::StridedSliceDescriptor::m_End
std::vector< int > m_End
End values for the input that will be sliced.
Definition: Descriptors.hpp:1344
armnn::ReduceOperation::Min
@ Min
armnnTfLiteParser::TfLiteParserImpl::GetSubgraphCount
size_t GetSubgraphCount() const
Return the number of subgraphs in the parsed model.
Definition: TfLiteParser.cpp:6005
armnn::PadDescriptor::m_PaddingMode
PaddingMode m_PaddingMode
Specifies the Padding mode (Constant, Reflect or Symmetric)
Definition: Descriptors.hpp:1224
CHECK_SUBGRAPH
#define CHECK_SUBGRAPH(MODEL, SUBGRAPH_INDEX)
Definition: TfLiteParser.cpp:144
armnn::ConstTensor
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:329
armnn::IConnectableLayer
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:80
armnn::IInputSlot
An input connection slot for a layer.
Definition: INetwork.hpp:25
armnn::Pooling2dDescriptor::m_OutputShapeRounding
OutputShapeRounding m_OutputShapeRounding
The rounding method for the output shape. (Floor, Ceiling).
Definition: Descriptors.hpp:423
armnn::PoolingAlgorithm
PoolingAlgorithm
Definition: Types.hpp:150
armnn::TransposeConvolution2dDescriptor::m_OutputShapeEnabled
bool m_OutputShapeEnabled
Output shape if it has been specified.
Definition: Descriptors.hpp:1485
armnn::TransposeConvolution2dDescriptor::m_BiasEnabled
bool m_BiasEnabled
Enable/disable bias.
Definition: Descriptors.hpp:1481
armnn::DetectionPostProcessDescriptor
Definition: Descriptors.hpp:713
armnn::Convolution3dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NDHWC, NCDHW).
Definition: Descriptors.hpp:655
armnn::TensorInfo::SetConstant
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
Definition: Tensor.cpp:518
armnn::DataType::Signed64
@ Signed64
armnn::TransposeConvolution2dDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:1483
armnnTfLiteParser
Definition: ITfLiteParser.hpp:17
armnn::Pooling2dDescriptor
A Pooling2dDescriptor for the Pooling2dLayer.
Definition: Descriptors.hpp:371
armnn::LstmDescriptor::m_ActivationFunc
uint32_t m_ActivationFunc
The activation function to use.
Definition: Descriptors.hpp:1140
armnn::LstmInputParamsInfo::m_InputToForgetWeights
const TensorInfo * m_InputToForgetWeights
Definition: LstmParams.hpp:90
armnn::BatchToSpaceNdDescriptor::m_DataLayout
DataLayout m_DataLayout
The data layout to be used (NCHW, NHWC).
Definition: Descriptors.hpp:902
armnn::BroadcastToDescriptor
Definition: Descriptors.hpp:1659
armnn::TensorInfo::GetQuantizationOffset
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:482
armnn::DepthwiseConvolution2dDescriptor
A DepthwiseConvolution2dDescriptor for the DepthwiseConvolution2dLayer.
Definition: Descriptors.hpp:659
armnn::ComparisonOperation::Equal
@ Equal
armnn::ScatterNdDescriptor
A ScatterNdDescriptor for the ScatterNdLayer.
Definition: Descriptors.hpp:1679
armnn::ReduceDescriptor
A ReduceDescriptor for the REDUCE operators.
Definition: Descriptors.hpp:1538
armnn::DepthwiseConvolution2dDescriptor::m_DilationX
uint32_t m_DilationX
Dilation factor value for width dimension.
Definition: Descriptors.hpp:704
armnn::LstmDescriptor::m_ClippingThresCell
float m_ClippingThresCell
Clipping threshold value for the cell state.
Definition: Descriptors.hpp:1142
armnnTfLiteParser::TfLiteParserImpl::GetBuffer
static BufferRawPtr GetBuffer(const ModelPtr &model, size_t bufferIndex)
Definition: TfLiteParser.cpp:5763
armnn::NullPointerException
Definition: Exceptions.hpp:146
armnnUtils::ToFloatArray
std::unique_ptr< float[]> ToFloatArray(const std::vector< PrimitiveType > &data, const armnn::TensorInfo &tensorInfo)
Definition: TensorUtils.cpp:307
armnn::LstmInputParams
Definition: LstmParams.hpp:13
armnn::LstmInputParams::m_CellLayerNormWeights
const ConstTensor * m_CellLayerNormWeights
Definition: LstmParams.hpp:59
armnn::StridedSliceDescriptor::m_NewAxisMask
int32_t m_NewAxisMask
New axis mask value.
Definition: Descriptors.hpp:1360
CHECK_BUFFER
#define CHECK_BUFFER(MODEL, BUFFER_INDEX)
Definition: TfLiteParser.cpp:255
armnn::GetComparisonOperationAsCString
constexpr char const * GetComparisonOperationAsCString(ComparisonOperation operation)
Definition: TypesUtils.hpp:62
armnn::TensorShape::GetNumElements
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified.
Definition: Tensor.cpp:181
armnn::MeanDescriptor
A MeanDescriptor for the MeanLayer.
Definition: Descriptors.hpp:1172
armnn::CheckLocation::FileLine
std::string FileLine() const
Definition: Exceptions.hpp:37
armnnTfLiteParser::TfLiteParserImpl::GetInputTensorIds
static std::vector< int32_t > & GetInputTensorIds(const ModelPtr &model, size_t subgraphIndex, size_t operatorIndex)
Definition: TfLiteParser.cpp:5531
armnn::OptionalReferenceSwitch< std::is_reference< T >::value, T >::value
const T & value() const
Definition: Optional.hpp:146
armnn::TileDescriptor
Definition: Descriptors.hpp:1640
armnn::SoftmaxDescriptor
A SoftmaxDescriptor for the SoftmaxLayer.
Definition: Descriptors.hpp:177
armnn::Pooling2dDescriptor::m_PoolType
PoolingAlgorithm m_PoolType
The pooling algorithm to use (Max. Average, L2).
Definition: Descriptors.hpp:405
armnnTfLiteParser::ITfLiteParser
Definition: ITfLiteParser.hpp:26
armnn::SpaceToDepthDescriptor
A SpaceToDepthDescriptor for the SpaceToDepthLayer.
Definition: Descriptors.hpp:1075
armnn::OptionalBase::has_value
bool has_value() const noexcept
Definition: Optional.hpp:53
armnn::FileNotFoundException
Definition: Exceptions.hpp:86
armnn::ReduceOperation::Max
@ Max
armnn::LstmDescriptor::m_CellIntermediateScale
float m_CellIntermediateScale
Cell intermediate quantization scale.
Definition: Descriptors.hpp:1160
armnnTfLiteParser::ComputeWrappedIndex
unsigned int ComputeWrappedIndex(int idx, unsigned int numDimsIn)
Definition: TfLiteParser.cpp:4526
armnnTfLiteParser::TfLiteParserImpl::OutputShapeOfSqueeze
static armnn::TensorInfo OutputShapeOfSqueeze(std::vector< uint32_t > squeezeDims, const armnn::TensorInfo &inputTensorInfo)
Definition: TfLiteParser.cpp:2430
armnn::ComparisonOperation::Greater
@ Greater
armnn::DepthwiseConvolution2dDescriptor::m_StrideX
uint32_t m_StrideX
Stride value when proceeding through input for the width dimension.
Definition: Descriptors.hpp:700
armnn::DepthwiseConvolution2dDescriptor::m_PadTop
uint32_t m_PadTop
Padding top value in the height dimension.
Definition: Descriptors.hpp:696