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