ArmNN
 20.08
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 26 of file Tensor.cpp.

References armnn::Specified.

Referenced by TensorShape::TensorShape().

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

◆ 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 31 of file Tensor.cpp.

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

◆ 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 41 of file Tensor.cpp.

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

◆ TensorShape() [4/8]

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

Constructor for TensorShape.

Parameters
dimensionSizeList- Size of each of dimension.

Definition at line 55 of file Tensor.cpp.

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

◆ TensorShape() [5/8]

TensorShape ( const TensorShape other)

Copy Constructor for TensorShape.

Parameters
other- TensorShape to copy from.

Definition at line 117 of file Tensor.cpp.

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

◆ 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 60 of file Tensor.cpp.

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

◆ 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 81 of file Tensor.cpp.

References TensorShape::TensorShape().

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

◆ 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 93 of file Tensor.cpp.

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

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

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 242 of file Tensor.cpp.

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

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

◆ 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 212 of file Tensor.cpp.

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

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

◆ GetNumDimensions()

unsigned int GetNumDimensions ( ) const

Function that returns the tensor rank.

Returns
- Tensor rank.

Definition at line 175 of file Tensor.cpp.

Referenced by armnn::BatchToSpaceNd(), BOOST_AUTO_TEST_CASE(), armnn::boost_test_print_type(), ShapesAreBroadcastCompatible::CalcInputSize(), armnnDeserializer::CheckShape(), ClStackWorkload::ClStackWorkload(), armnnUtils::ConvertTensorShapeToString(), armnn::CopyTensorContentsGeneric(), OnnxParser::CreateNetworkFromString(), RefL2NormalizationWorkload::Execute(), armnnUtils::ExpandDims(), ExpandTensorShapeTo3dForPermute(), armnnUtils::GetNumElementsAfter(), armnnUtils::GetNumElementsBetween(), ArgMinMaxLayer::InferOutputShapes(), ComparisonLayer::InferOutputShapes(), PreluLayer::InferOutputShapes(), StackLayer::InferOutputShapes(), PadLayer::InferOutputShapes(), armnn::LogSoftmax(), NeonStackWorkload::NeonStackWorkload(), armnn::operator<<(), armnnTfParser::OutputShapeOfExpandDims(), armnnTfParser::OutputShapeOfSqueeze(), armnn::Pad(), armnnUtils::Permuted(), ParserPrototxtFixture< armnnOnnxParser::IOnnxParser >::RunTest(), ShapesAreBroadcastCompatible::ShapesAreBroadcastCompatible(), ShapesAreSameRank::ShapesAreSameRank(), armnn::Slice(), armnn::Softmax(), TensorBufferArrayView< DataType >::TensorBufferArrayView(), TfParser::TfParser(), armnnUtils::TransposeTensorShape(), StackQueueDescriptor::Validate(), ArgMinMaxQueueDescriptor::Validate(), Layer::ValidateAndCopyShape(), SerializerVisitor::VisitQuantizedLstmLayer(), SerializerVisitor::VisitReshapeLayer(), and SerializerVisitor::VisitStackLayer().

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

◆ 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 182 of file Tensor.cpp.

Referenced by BOOST_AUTO_TEST_CASE(), armnn::DepthToSpace(), armnn::Fill(), armnnUtils::FindMinMax(), and armnn::TransposeConvolution2dImpl().

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

◆ 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 258 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().

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

◆ operator!=()

bool operator!= ( const TensorShape other) const

Inequality comparison operator.

Parameters
other- TensorShape to compare with.

Definition at line 170 of file Tensor.cpp.

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

◆ operator=()

TensorShape & operator= ( const TensorShape other)

Assignation function.

Parameters
other- TensorShape to copy from.

Definition at line 125 of file Tensor.cpp.

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

◆ operator==()

bool operator== ( const TensorShape other) const

Equality comparison operator.

Parameters
other- TensorShape to compare with.

Definition at line 161 of file Tensor.cpp.

Referenced by TensorInfo::HasMultipleQuantizationScales().

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

◆ operator[]() [1/2]

unsigned int operator[] ( unsigned int  i) const

Read only operator.

Parameters
i- Dimension index.

Definition at line 136 of file Tensor.cpp.

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

◆ operator[]() [2/2]

unsigned int & operator[] ( unsigned int  i)

Read and write operator.

Parameters
i- Dimension index.

Definition at line 146 of file Tensor.cpp.

References CHECK_LOCATION, and armnn::Scalar.

147 {
148  if (Dimensionality::Scalar == m_Dimensionality)
149  {
150  std::stringstream errorMessage;
151  errorMessage << "TensorShape with Dimensionality::Scalar must be const to use operator[]";
152  throw InvalidArgumentException(errorMessage.str(), CHECK_LOCATION());
153  }
154  CheckUnspecifiedNumDimensions();
155  CheckDimensionIndex(i);
156  CheckDimensionSpecified(i);
157 
158  return m_Dimensions.at(i);
159 }
#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 233 of file Tensor.cpp.

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

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

◆ 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 220 of file Tensor.cpp.

References armnn::Specified.

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

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

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