ArmNN
 21.02
TensorShape Class Reference

#include <Tensor.hpp>

Public Member Functions

 TensorShape ()
 Empty (invalid) constructor. More...
 
 TensorShape (unsigned int numDimensions, bool initDimensionsSpecificity=true)
 Constructor for TensorShape. More...
 
 TensorShape (unsigned int numDimensions, const unsigned int *dimensionSizes)
 Constructor for TensorShape. More...
 
 TensorShape (std::initializer_list< unsigned int > dimensionSizeList)
 Constructor for TensorShape. More...
 
 TensorShape (const TensorShape &other)
 Copy Constructor for TensorShape. More...
 
 TensorShape (unsigned int numDimensions, const unsigned int *dimensionSizes, const bool *dimensionsSpecificity)
 Constructor for TensorShape. More...
 
 TensorShape (std::initializer_list< unsigned int > dimensionSizeList, std::initializer_list< bool > dimensionsSpecificityList)
 Constructor for TensorShape. More...
 
 TensorShape (Dimensionality dimensionality)
 Constructor for TensorShape. More...
 
TensorShapeoperator= (const TensorShape &other)
 Assignation function. More...
 
unsigned int operator[] (unsigned int i) const
 Read only operator. More...
 
unsigned int & operator[] (unsigned int i)
 Read and write operator. More...
 
bool operator== (const TensorShape &other) const
 Equality comparison operator. More...
 
bool operator!= (const TensorShape &other) const
 Inequality comparison operator. More...
 
unsigned int GetNumDimensions () const
 Function that returns the tensor rank. More...
 
unsigned int GetNumElements () const
 Function that calculates the tensor elements by multiplying all dimension size which are Specified. More...
 
Dimensionality GetDimensionality () const
 Function that returns the tensor type. More...
 
bool GetDimensionSpecificity (unsigned int i) const
 Gets information about if the dimension size has been specified or not. More...
 
void SetNumDimensions (unsigned int numDimensions, bool initDimensionsSpecificity=false)
 Sets the tensor rank and therefore the Dimensionality is set to Specified if it was not. More...
 
void SetDimensionSize (unsigned int i, unsigned int dimensionSize)
 Sets the size of the indicated dimension and Specificity for that dimension is set to true. More...
 
bool AreAllDimensionsSpecified () const
 Checks if there is at least one dimension not specified. More...
 
bool IsAtLeastOneDimensionSpecified () const
 Checks if there is at least one dimension specified. More...
 

Detailed Description

Definition at line 20 of file Tensor.hpp.

Constructor & Destructor Documentation

◆ TensorShape() [1/8]

Empty (invalid) constructor.

Definition at line 25 of file Tensor.cpp.

References armnn::Specified.

Referenced by TensorShape::TensorShape().

26  : m_NumDimensions(0), m_Dimensionality(Dimensionality::Specified)
27 {
28 }

◆ TensorShape() [2/8]

TensorShape ( unsigned int  numDimensions,
bool  initDimensionsSpecificity = true 
)
explicit

Constructor for TensorShape.

Parameters
numDimensions- Tensor rank.
initDimensionsSpecificity(optional) - value to initialize the specificity of each dimension size.

Definition at line 30 of file Tensor.cpp.

31  : m_NumDimensions(numDimensions), m_Dimensionality(Dimensionality::Specified)
32 {
33  CheckValidNumDimensions(numDimensions);
34 
35  std::fill(m_Dimensions.begin(), m_Dimensions.begin() + m_NumDimensions, 0);
36  std::fill(m_DimensionsSpecificity.begin(), m_DimensionsSpecificity.begin() + m_NumDimensions,
37  initDimensionsSpecificity);
38 }

◆ TensorShape() [3/8]

TensorShape ( unsigned int  numDimensions,
const unsigned int *  dimensionSizes 
)

Constructor for TensorShape.

Parameters
numDimensions- Tensor rank.
dimensionSizes- Size of each of dimension.

Definition at line 40 of file Tensor.cpp.

41  : m_NumDimensions(numDimensions), m_Dimensionality(Dimensionality::Specified)
42 {
43  CheckValidNumDimensions(numDimensions);
44 
45  if (dimensionSizes == nullptr)
46  {
47  throw InvalidArgumentException("Tensor dimensionSizes must not be NULL");
48  }
49 
50  std::copy(dimensionSizes, dimensionSizes + numDimensions, m_Dimensions.begin());
51  std::fill(m_DimensionsSpecificity.begin(), m_DimensionsSpecificity.begin() + m_NumDimensions, true);
52 }

◆ TensorShape() [4/8]

TensorShape ( std::initializer_list< unsigned int >  dimensionSizeList)

Constructor for TensorShape.

Parameters
dimensionSizeList- Size of each of dimension.

Definition at line 54 of file Tensor.cpp.

55  : TensorShape(armnn::numeric_cast<unsigned int>(dimensionSizeList.size()), dimensionSizeList.begin())
56 {
57 }
TensorShape()
Empty (invalid) constructor.
Definition: Tensor.cpp:25

◆ TensorShape() [5/8]

TensorShape ( const TensorShape other)

Copy Constructor for TensorShape.

Parameters
other- TensorShape to copy from.

Definition at line 116 of file Tensor.cpp.

117  : m_NumDimensions(other.m_NumDimensions), m_Dimensionality(other.m_Dimensionality)
118 {
119  std::copy(other.m_Dimensions.cbegin(), other.m_Dimensions.cbegin() + other.m_NumDimensions, m_Dimensions.begin());
120  std::copy(other.m_DimensionsSpecificity.cbegin(), other.m_DimensionsSpecificity.cbegin() + other.m_NumDimensions,
121  m_DimensionsSpecificity.begin());
122 }

◆ TensorShape() [6/8]

TensorShape ( unsigned int  numDimensions,
const unsigned int *  dimensionSizes,
const bool *  dimensionsSpecificity 
)

Constructor for TensorShape.

Parameters
numDimensions- Tensor rank.
dimensionSizes- Size of each of dimension.
dimensionsSpecificity- Flags to indicate which dimension has its size specified.

Definition at line 59 of file Tensor.cpp.

62  : m_NumDimensions(numDimensions), m_Dimensionality(Dimensionality::Specified)
63 {
64  CheckValidNumDimensions(numDimensions);
65 
66  if (dimensionSizes == nullptr)
67  {
68  throw InvalidArgumentException("Tensor dimensionSizes must not be NULL");
69  }
70 
71  if (dimensionsSpecificity == nullptr)
72  {
73  throw InvalidArgumentException("Tensor dimensionsSpecificity must not be NULL");
74  }
75 
76  std::copy(dimensionSizes, dimensionSizes + numDimensions, m_Dimensions.begin());
77  std::copy(dimensionsSpecificity, dimensionsSpecificity + numDimensions, m_DimensionsSpecificity.begin());
78 }

◆ TensorShape() [7/8]

TensorShape ( std::initializer_list< unsigned int >  dimensionSizeList,
std::initializer_list< bool >  dimensionsSpecificityList 
)

Constructor for TensorShape.

Parameters
dimensionSizeList- Size of each of dimension.
dimensionsSpecificityList- Flags to indicate which dimension size is specified.

Definition at line 80 of file Tensor.cpp.

References TensorShape::TensorShape().

82 {
83  auto numDimensions = static_cast<unsigned int>(dimensionSizeList.size());
84  if (dimensionsSpecificityList.size() != numDimensions)
85  {
86  throw InvalidArgumentException("Tensors dimensionSizeList and dimensionsSpecificityList must be same size");
87  }
88 
89  *this = TensorShape(numDimensions, dimensionSizeList.begin(), dimensionsSpecificityList.begin());
90 }
TensorShape()
Empty (invalid) constructor.
Definition: Tensor.cpp:25

◆ TensorShape() [8/8]

TensorShape ( Dimensionality  dimensionality)
explicit

Constructor for TensorShape.

Parameters
dimensionality- Parameter to indicate if the Tensor is a Scalar, a Tensor of known dimensionality or a Tensor of unknown dimensionality.

Definition at line 92 of file Tensor.cpp.

References armnn::NotSpecified, armnn::Scalar, and armnn::Specified.

93 : m_Dimensionality(dimensionality)
94 {
95  switch (dimensionality)
96  {
98  throw InvalidArgumentException("Use other constructor to specify the rest of the values, this one is only "
99  "for tensors that have an unknown number of dimensions or that are scalar");
100  break;
102  m_NumDimensions = 0;
103  m_Dimensions = {0};
104  m_DimensionsSpecificity = {false};
105  break;
107  m_NumDimensions = 1;
108  m_Dimensions = {1};
109  m_DimensionsSpecificity = {true};
110  break;
111  default:
112  throw InvalidArgumentException("Invalid Dimensionality value");
113  }
114 }

Member Function Documentation

◆ AreAllDimensionsSpecified()

bool AreAllDimensionsSpecified ( ) const

Checks if there is at least one dimension not specified.

AND of all array elements.

Returns
- True when all dimension sizes are specified. False when at least one dimension size is not specified.

Definition at line 241 of file Tensor.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), TensorShape::GetDimensionality(), ConstantLayer::ValidateTensorShapesFromInputs(), and Layer::VerifyShapeInferenceType().

242 {
243  CheckUnspecifiedNumDimensions();
244 
245  bool areAllDimensionsSpecified = true;
246  for (unsigned int i = 0; i < m_NumDimensions; ++i)
247  {
248  if (!m_DimensionsSpecificity[i])
249  {
250  areAllDimensionsSpecified = false;
251  break;
252  }
253  }
254  return areAllDimensionsSpecified;
255 }

◆ GetDimensionality()

Dimensionality GetDimensionality ( ) const
inline

◆ GetDimensionSpecificity()

bool GetDimensionSpecificity ( unsigned int  i) const

Gets information about if the dimension size has been specified or not.

Parameters
i- Dimension index.
Returns
- Flag to indicate if the dimension "i" has a specified size.

Definition at line 211 of file Tensor.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), TensorShape::GetDimensionality(), armnnSerializer::GetFlatBufferArgMinMaxFunction(), and Layer::ValidateAndCopyShape().

212 {
213  CheckUnspecifiedNumDimensions();
214  CheckDimensionIndex(i);
215 
216  return m_DimensionsSpecificity[i];
217 }

◆ GetNumDimensions()

unsigned int GetNumDimensions ( ) const

Function that returns the tensor rank.

Returns
- Tensor rank.

Definition at line 174 of file Tensor.cpp.

Referenced by ITfParser::TfParserImpl::AddAdditionLayer(), ITfParser::TfParserImpl::AddMaximumLayer(), armnn::BatchToSpaceNd(), BOOST_AUTO_TEST_CASE(), armnn::boost_test_print_type(), ShapesAreBroadcastCompatible::CalcInputSize(), armnnDeserializer::CheckShape(), ClStackWorkload::ClStackWorkload(), armnnUtils::ConvertTensorShapeToString(), armnn::CopyTensorContentsGeneric(), ITfParser::TfParserImpl::CreateAdditionLayer(), OnnxParserImpl::CreateNetworkFromString(), ClRankWorkload::Execute(), NeonRankWorkload::Execute(), RefL2NormalizationWorkload::Execute(), armnnUtils::ExpandDims(), ExpandTensorShapeTo3dForPermute(), armnnSerializer::GetFlatBufferArgMinMaxFunction(), armnnUtils::GetNumElementsAfter(), armnnUtils::GetNumElementsBetween(), ElementwiseBaseLayer::InferOutputShapes(), ArgMinMaxLayer::InferOutputShapes(), PreluLayer::InferOutputShapes(), LogicalBinaryLayer::InferOutputShapes(), ComparisonLayer::InferOutputShapes(), StackLayer::InferOutputShapes(), PadLayer::InferOutputShapes(), armnn::LogSoftmax(), NeonStackWorkload::NeonStackWorkload(), armnn::operator<<(), armnn::Pad(), ITfParser::TfParserImpl::ParseStack(), armnnUtils::Permuted(), ITfParser::TfParserImpl::ProcessComparisonLayer(), ITfParser::TfParserImpl::ProcessElementwiseLayer(), ParserPrototxtFixture< armnnOnnxParser::IOnnxParser >::RunTest(), ShapesAreBroadcastCompatible::ShapesAreBroadcastCompatible(), ShapesAreSameRank::ShapesAreSameRank(), armnn::Slice(), armnn::Softmax(), TensorBufferArrayView< DataType >::TensorBufferArrayView(), armnnUtils::TransposeTensorShape(), StackQueueDescriptor::Validate(), ArgMinMaxQueueDescriptor::Validate(), and Layer::ValidateAndCopyShape().

175 {
176  CheckUnspecifiedNumDimensions();
177 
178  return m_NumDimensions;
179 }

◆ GetNumElements()

unsigned int GetNumElements ( ) const

Function that calculates the tensor elements by multiplying all dimension size which are Specified.

Returns
- Total number of elements in the tensor.

Definition at line 181 of file Tensor.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), QASymm8Decoder::DecodeTensor(), QASymmS8Decoder::DecodeTensor(), QSymmS8Decoder::DecodeTensor(), QSymm16Decoder::DecodeTensor(), BFloat16Decoder::DecodeTensor(), Float16Decoder::DecodeTensor(), Float32Decoder::DecodeTensor(), ScaledInt32Decoder::DecodeTensor(), Int32Decoder::DecodeTensor(), Int32ToInt32tDecoder::DecodeTensor(), BooleanDecoder::DecodeTensor(), BooleanDecoderBool::DecodeTensor(), QSymm8PerAxisDecoder::DecodeTensor(), ScaledInt32PerAxisDecoder::DecodeTensor(), armnn::DepthToSpace(), armnn::Fill(), armnnUtils::FindMinMax(), and armnn::TransposeConvolution2dImpl().

182 {
183  CheckUnspecifiedNumDimensions();
184 
185  if (m_NumDimensions == 0)
186  {
187  return 0;
188  }
189 
190  unsigned int count = 1;
191  bool atLeastOneDimensionSpecified = false;
192  for (unsigned int i = 0; i < m_NumDimensions; ++i)
193  {
194  if (m_DimensionsSpecificity[i])
195  {
196  atLeastOneDimensionSpecified = true;
197  count *= m_Dimensions[i];
198  }
199  }
200 
201  if (atLeastOneDimensionSpecified)
202  {
203  return count;
204  }
205  else
206  {
207  return 0;
208  }
209 }

◆ IsAtLeastOneDimensionSpecified()

bool IsAtLeastOneDimensionSpecified ( ) const

Checks if there is at least one dimension specified.

OR of all array elements.

Returns
- True at least one dimension sizes is specified. False when all dimension sizes are not specified.

Definition at line 257 of file Tensor.cpp.

References CHECK_LOCATION, armnn::MaxNumOfTensorDimensions, armnn::NotSpecified, armnn::Scalar, and armnn::Specified.

Referenced by BOOST_AUTO_TEST_CASE(), and TensorShape::GetDimensionality().

258 {
259  CheckUnspecifiedNumDimensions();
260 
261  bool isAtLeastOneDimensionSpecified = false;
262  for (unsigned int i = 0; i < m_NumDimensions; ++i)
263  {
264  if (m_DimensionsSpecificity[i])
265  {
266  isAtLeastOneDimensionSpecified = true;
267  break;
268  }
269  }
270  return isAtLeastOneDimensionSpecified;
271 }

◆ operator!=()

bool operator!= ( const TensorShape other) const

Inequality comparison operator.

Parameters
other- TensorShape to compare with.

Definition at line 169 of file Tensor.cpp.

170 {
171  return !(*this == other);
172 }

◆ operator=()

TensorShape & operator= ( const TensorShape other)

Assignation function.

Parameters
other- TensorShape to copy from.

Definition at line 124 of file Tensor.cpp.

Referenced by TensorInfo::HasMultipleQuantizationScales().

125 {
126  m_NumDimensions = other.m_NumDimensions;
127  m_Dimensionality = other.m_Dimensionality;
128  std::copy(other.m_Dimensions.cbegin(), other.m_Dimensions.cbegin() + other.m_NumDimensions, m_Dimensions.begin());
129  std::copy(other.m_DimensionsSpecificity.cbegin(), other.m_DimensionsSpecificity.cbegin() + other.m_NumDimensions,
130  m_DimensionsSpecificity.begin());
131  return *this;
132 }

◆ operator==()

bool operator== ( const TensorShape other) const

Equality comparison operator.

Parameters
other- TensorShape to compare with.

Definition at line 160 of file Tensor.cpp.

Referenced by TensorInfo::HasMultipleQuantizationScales().

161 {
162  return ((m_NumDimensions == other.m_NumDimensions) &&
163  (m_Dimensionality == other.m_Dimensionality) &&
164  std::equal(m_Dimensions.cbegin(), m_Dimensions.cbegin() + m_NumDimensions, other.m_Dimensions.cbegin()) &&
165  std::equal(m_DimensionsSpecificity.cbegin(), m_DimensionsSpecificity.cbegin() + m_NumDimensions,
166  other.m_DimensionsSpecificity.cbegin()));
167 }

◆ operator[]() [1/2]

unsigned int operator[] ( unsigned int  i) const

Read only operator.

Parameters
i- Dimension index.

Definition at line 135 of file Tensor.cpp.

136 {
137  CheckUnspecifiedNumDimensions();
138  CheckDimensionIndex(i);
139  CheckDimensionSpecified(i);
140 
141  return m_Dimensions.at(i);
142 }

◆ operator[]() [2/2]

unsigned int & operator[] ( unsigned int  i)

Read and write operator.

Parameters
i- Dimension index.

Definition at line 145 of file Tensor.cpp.

References CHECK_LOCATION, and armnn::Scalar.

146 {
147  if (Dimensionality::Scalar == m_Dimensionality)
148  {
149  std::stringstream errorMessage;
150  errorMessage << "TensorShape with Dimensionality::Scalar must be const to use operator[]";
151  throw InvalidArgumentException(errorMessage.str(), CHECK_LOCATION());
152  }
153  CheckUnspecifiedNumDimensions();
154  CheckDimensionIndex(i);
155  CheckDimensionSpecified(i);
156 
157  return m_Dimensions.at(i);
158 }
#define CHECK_LOCATION()
Definition: Exceptions.hpp:197

◆ SetDimensionSize()

void SetDimensionSize ( unsigned int  i,
unsigned int  dimensionSize 
)

Sets the size of the indicated dimension and Specificity for that dimension is set to true.

Parameters
i- Dimension index.
dimensionSize- size of one dimension.

Definition at line 232 of file Tensor.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), and TensorShape::GetDimensionality().

233 {
234  CheckScalar();
235  CheckDimensionIndex(i);
236 
237  m_Dimensions[i] = dimensionSize;
238  m_DimensionsSpecificity[i] = true;
239 }

◆ SetNumDimensions()

void SetNumDimensions ( unsigned int  numDimensions,
bool  initDimensionsSpecificity = false 
)

Sets the tensor rank and therefore the Dimensionality is set to Specified if it was not.

Parameters
numDimensions- Tensor rank.
initDimensionsSpecificity(optional) - value to initialize the specificity of each dimension size.

Definition at line 219 of file Tensor.cpp.

References armnn::Specified.

Referenced by BOOST_AUTO_TEST_CASE(), and TensorShape::GetDimensionality().

220 {
221  CheckScalar();
222  CheckSpecifiedNumDimensions();
223  CheckValidNumDimensions(numDimensions);
224 
225  m_NumDimensions = numDimensions;
226  m_Dimensionality = Dimensionality::Specified;
227  std::fill(m_Dimensions.begin(), m_Dimensions.begin() + m_NumDimensions, 0);
228  std::fill(m_DimensionsSpecificity.begin(), m_DimensionsSpecificity.begin() + m_NumDimensions,
229  initDimensionsSpecificity);
230 }

The documentation for this class was generated from the following files: