ArmNN
 24.02
PermutationVector Class Reference

#include <Types.hpp>

Public Types

using ValueType = unsigned int
 
using SizeType = unsigned int
 
using ArrayType = std::array< ValueType, MaxNumOfTensorDimensions >
 
using ConstIterator = typename ArrayType::const_iterator
 

Public Member Functions

 PermutationVector (const ValueType *dimMappings, SizeType numDimMappings)
 
 PermutationVector (std::initializer_list< ValueType > dimMappings)
 
ValueType operator[] (SizeType i) const
 Indexing method with out-of-bounds error checking for the m_DimMappings array. More...
 
SizeType GetSize () const
 
ConstIterator begin () const
 
ConstIterator end () const
 
bool IsEqual (const PermutationVector &other) const
 
bool IsInverse (const PermutationVector &other) const
 

Detailed Description

Definition at line 314 of file Types.hpp.

Member Typedef Documentation

◆ ArrayType

Definition at line 319 of file Types.hpp.

◆ ConstIterator

using ConstIterator = typename ArrayType::const_iterator

Definition at line 320 of file Types.hpp.

◆ SizeType

using SizeType = unsigned int

Definition at line 318 of file Types.hpp.

◆ ValueType

using ValueType = unsigned int

Definition at line 317 of file Types.hpp.

Constructor & Destructor Documentation

◆ PermutationVector() [1/2]

PermutationVector ( const ValueType dimMappings,
SizeType  numDimMappings 
)
Parameters
dimMappings- Indicates how to translate tensor elements from a given source into the target destination, when source and target potentially have different memory layouts.

E.g. For a 4-d tensor laid out in a memory with the format (Batch Element, Height, Width, Channels), which is to be passed as an input to ArmNN, each source dimension is mapped to the corresponding ArmNN dimension. The Batch dimension remains the same (0 -> 0). The source Height dimension is mapped to the location of the ArmNN Height dimension (1 -> 2). Similar arguments are made for the Width and Channels (2 -> 3 and 3 -> 1). This will lead to m_DimMappings pointing to the following array: [ 0, 2, 3, 1 ].

Note that the mapping should be reversed if considering the case of ArmNN 4-d outputs (Batch Element, Channels, Height, Width) being written to a destination with the format mentioned above. We now have 0 -> 0, 2 -> 1, 3 -> 2, 1 -> 3, which, when reordered, lead to the following m_DimMappings contents: [ 0, 3, 1, 2 ].

Definition at line 20 of file Descriptors.cpp.

21 {
22  // Validation
23 
24  if (numDimMappings > MaxNumOfTensorDimensions)
25  {
26  throw InvalidArgumentException(
27  fmt::format("The number of mappings ({0}) cannot be greater "
28  "than the maximum number of dimensions supported ({1})",
29  numDimMappings,
31  }
32 
33  if ((dimMappings == nullptr) && (numDimMappings != 0))
34  {
35  throw InvalidArgumentException("Dimension mappings must not be NULL if the number of mappings is positive");
36  }
37 
38  for (SizeType i = 0; i < numDimMappings; ++i)
39  {
40  const ValueType dstIndex = dimMappings[i];
41  if (dstIndex >= numDimMappings)
42  {
43  throw InvalidArgumentException(
44  fmt::format("Dimension mapping at index {0} is invalid: "
45  "{1} is outside of the valid range [0,{2}]",
46  i,
47  dstIndex,
48  (numDimMappings - 1)));
49  }
50  }
51 
52  // Validation: Detect duplicates
53  {
54  std::array<bool, MaxNumOfTensorDimensions> observedDims;
55  observedDims.fill(false);
56 
57  for (SizeType i = 0; i < numDimMappings; ++i)
58  {
59  const ValueType dstIndex = dimMappings[i];
60  if (observedDims[dstIndex])
61  {
62  throw InvalidArgumentException("Invalid dimension mappings: Two or more source dimensions are mapped "
63  "to the same output dimension");
64  }
65  observedDims[dstIndex] = true;
66  }
67  }
68 
69  // Initialize
70  for (SizeType i = 0; i < numDimMappings; ++i)
71  {
72  m_DimMappings[i] = dimMappings[i];
73  }
74  m_NumDimMappings = numDimMappings;
75 }

References armnn::MaxNumOfTensorDimensions.

◆ PermutationVector() [2/2]

PermutationVector ( std::initializer_list< ValueType dimMappings)

Definition at line 77 of file Descriptors.cpp.

78  : PermutationVector(dimMappings.begin(), armnn::numeric_cast<SizeType>(dimMappings.size()))
79 {
80 }

Member Function Documentation

◆ begin()

ConstIterator begin ( ) const
inline

Definition at line 359 of file Types.hpp.

359 { return m_DimMappings.begin(); }

Referenced by ConvertTransposeToTosaOperator().

◆ end()

ConstIterator end ( ) const
inline
Returns
pointer one past the end of the number of mapping not the length of m_DimMappings.

Definition at line 364 of file Types.hpp.

364 { return m_DimMappings.begin() + m_NumDimMappings; }

Referenced by ConvertTransposeToTosaOperator().

◆ GetSize()

◆ IsEqual()

bool IsEqual ( const PermutationVector other) const
inline

Definition at line 366 of file Types.hpp.

367  {
368  if (m_NumDimMappings != other.m_NumDimMappings) return false;
369  for (unsigned int i = 0; i < m_NumDimMappings; ++i)
370  {
371  if (m_DimMappings[i] != other.m_DimMappings[i]) return false;
372  }
373  return true;
374  }

Referenced by TransposeLayer::IsEqual(), PermuteLayer::IsEqual(), PermuteDescriptor::operator==(), and TransposeDescriptor::operator==().

◆ IsInverse()

bool IsInverse ( const PermutationVector other) const
inline

Definition at line 376 of file Types.hpp.

377  {
378  bool isInverse = (GetSize() == other.GetSize());
379  for (SizeType i = 0; isInverse && (i < GetSize()); ++i)
380  {
381  isInverse = (m_DimMappings[other.m_DimMappings[i]] == i);
382  }
383  return isInverse;
384  }

References PermutationVector::GetSize().

Referenced by TransposeLayer::IsInverse(), and PermuteLayer::IsInverse().

◆ operator[]()

ValueType operator[] ( SizeType  i) const
inline

Indexing method with out-of-bounds error checking for the m_DimMappings array.

Parameters
i- integer value corresponding to index of m_DimMappings array to retrieve element from.
Returns
element at index i of m_DimMappings array.
Exceptions
InvalidArgumentExceptionwhen indexing out-of-bounds index of m_DimMappings array.

Definition at line 347 of file Types.hpp.

348  {
349  if (i >= GetSize())
350  {
351  throw InvalidArgumentException("Invalid indexing of PermutationVector of size " + std::to_string(GetSize())
352  + " at location [" + std::to_string(i) + "].");
353  }
354  return m_DimMappings.at(i);
355  }

References PermutationVector::GetSize().


The documentation for this class was generated from the following files:
armnn::MaxNumOfTensorDimensions
constexpr unsigned int MaxNumOfTensorDimensions
Definition: Types.hpp:31
armnn::PermutationVector::GetSize
SizeType GetSize() const
Definition: Types.hpp:357
armnn::PermutationVector::ValueType
unsigned int ValueType
Definition: Types.hpp:317
armnn::PermutationVector::PermutationVector
PermutationVector(const ValueType *dimMappings, SizeType numDimMappings)
Definition: Descriptors.cpp:20
armnn::PermutationVector::SizeType
unsigned int SizeType
Definition: Types.hpp:318