33 CheckValidNumDimensions(numDimensions);
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);
43 CheckValidNumDimensions(numDimensions);
45 if (dimensionSizes ==
nullptr)
50 std::copy(dimensionSizes, dimensionSizes + numDimensions, m_Dimensions.begin());
51 std::fill(m_DimensionsSpecificity.begin(), m_DimensionsSpecificity.begin() + m_NumDimensions,
true);
60 const unsigned int*
const dimensionSizes,
61 const bool*
const dimensionsSpecificity)
64 CheckValidNumDimensions(numDimensions);
66 if (dimensionSizes ==
nullptr)
71 if (dimensionsSpecificity ==
nullptr)
76 std::copy(dimensionSizes, dimensionSizes + numDimensions, m_Dimensions.begin());
77 std::copy(dimensionsSpecificity, dimensionsSpecificity + numDimensions, m_DimensionsSpecificity.begin());
81 std::initializer_list<bool> dimensionsSpecificityList)
83 auto numDimensions =
static_cast<unsigned int>(dimensionSizeList.size());
84 if (dimensionsSpecificityList.size() != numDimensions)
89 *
this =
TensorShape(numDimensions, dimensionSizeList.begin(), dimensionsSpecificityList.begin());
93 : m_Dimensionality(dimensionality)
95 switch (dimensionality)
99 "for tensors that have an unknown number of dimensions or that are scalar");
104 m_DimensionsSpecificity = {
false};
109 m_DimensionsSpecificity = {
true};
117 : m_NumDimensions(other.m_NumDimensions), m_Dimensionality(other.m_Dimensionality)
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());
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());
137 CheckUnspecifiedNumDimensions();
138 CheckDimensionIndex(i);
139 CheckDimensionSpecified(i);
141 return m_Dimensions.at(i);
149 std::stringstream errorMessage;
150 errorMessage <<
"TensorShape with Dimensionality::Scalar must be const to use operator[]";
153 CheckUnspecifiedNumDimensions();
154 CheckDimensionIndex(i);
155 CheckDimensionSpecified(i);
157 return m_Dimensions.at(i);
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()));
171 return !(*
this == other);
176 CheckUnspecifiedNumDimensions();
178 return m_NumDimensions;
183 CheckUnspecifiedNumDimensions();
185 if (m_NumDimensions == 0)
190 unsigned int count = 1;
191 bool atLeastOneDimensionSpecified =
false;
192 for (
unsigned int i = 0; i < m_NumDimensions; ++i)
194 if (m_DimensionsSpecificity[i])
196 atLeastOneDimensionSpecified =
true;
197 count *= m_Dimensions[i];
201 if (atLeastOneDimensionSpecified)
213 CheckUnspecifiedNumDimensions();
214 CheckDimensionIndex(i);
216 return m_DimensionsSpecificity[i];
222 CheckSpecifiedNumDimensions();
223 CheckValidNumDimensions(numDimensions);
225 m_NumDimensions = numDimensions;
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);
235 CheckDimensionIndex(i);
237 m_Dimensions[i] = dimensionSize;
238 m_DimensionsSpecificity[i] =
true;
243 CheckUnspecifiedNumDimensions();
245 bool areAllDimensionsSpecified =
true;
246 for (
unsigned int i = 0; i < m_NumDimensions; ++i)
248 if (!m_DimensionsSpecificity[i])
250 areAllDimensionsSpecified =
false;
254 return areAllDimensionsSpecified;
259 CheckUnspecifiedNumDimensions();
261 bool isAtLeastOneDimensionSpecified =
false;
262 for (
unsigned int i = 0; i < m_NumDimensions; ++i)
264 if (m_DimensionsSpecificity[i])
266 isAtLeastOneDimensionSpecified =
true;
270 return isAtLeastOneDimensionSpecified;
273 void TensorShape::CheckDimensionIndex(
unsigned int i)
const
275 if (i >= m_NumDimensions)
277 std::stringstream errorMessage;
278 errorMessage <<
"Invalid dimension index: " << i <<
" (number of dimensions is " << m_NumDimensions <<
")";
283 void TensorShape::CheckValidNumDimensions(
unsigned int numDimensions)
285 if (numDimensions < 1)
297 void TensorShape::CheckDimensionSpecified(
unsigned int i)
const
299 if (!m_DimensionsSpecificity[i])
301 std::stringstream errorMessage;
302 errorMessage <<
"Dimension index: " << i <<
" not specified. Tensor shape not inferred yet.";
307 void TensorShape::CheckScalar()
const
311 std::stringstream errorMessage;
312 errorMessage <<
"Invalid action on a tensor shape that holds a scalar value.";
313 throw InvalidArgumentException(errorMessage.str(),
CHECK_LOCATION());
317 void TensorShape::CheckUnspecifiedNumDimensions()
const
321 std::stringstream errorMessage;
322 errorMessage <<
"Invalid action on a tensor shape that has unknown number of dimensions.";
323 throw InvalidArgumentException(errorMessage.str(),
CHECK_LOCATION());
327 void TensorShape::CheckSpecifiedNumDimensions()
const
331 std::stringstream errorMessage;
332 errorMessage <<
"Invalid action on a tensor shape that has known number of dimensions.";
333 throw InvalidArgumentException(errorMessage.str(),
CHECK_LOCATION());
348 float quantizationScale,
349 int32_t quantizationOffset,
352 , m_DataType(dataType)
353 , m_IsConstant(isConstant)
360 const unsigned int* dimensionSizes,
362 float quantizationScale,
363 int32_t quantizationOffset,
365 : m_Shape(numDimensions, dimensionSizes), m_DataType(dataType), m_IsConstant(isConstant)
373 const std::vector<float>& quantizationScales,
374 unsigned int quantizationDim,
377 , m_DataType(dataType)
378 , m_IsConstant(isConstant)
385 const unsigned int* dimensionSizes,
387 const std::vector<float>& quantizationScales,
388 unsigned int quantizationDim,
390 : m_Shape(numDimensions, dimensionSizes)
391 , m_DataType(dataType)
392 , m_IsConstant(isConstant)
399 : m_Shape(other.m_Shape)
400 , m_DataType(other.m_DataType)
401 , m_IsConstant(other.m_IsConstant)
402 , m_Quantization(other.m_Quantization)
407 m_Shape = other.m_Shape;
408 m_DataType = other.m_DataType;
409 m_Quantization = other.m_Quantization;
410 m_IsConstant = other.m_IsConstant;
416 return ((m_Shape == other.m_Shape) &&
417 (m_DataType == other.m_DataType) &&
418 (m_Quantization == other.m_Quantization) &&
419 (m_IsConstant == other.m_IsConstant));
424 return !(*
this == other);
436 match &= m_DataType == other.m_DataType;
453 return m_Quantization.m_Scales;
458 m_Quantization.m_Scales = scales;
463 if (m_Quantization.m_Scales.empty())
471 throw RuntimeException(
"Invalid call to GetQuantizationScale on a tensor with multiple scale values. Use "
472 "GetQuantizationScales instead.");
474 return m_Quantization.m_Scales[0];
479 m_Quantization.m_Scales = { scale };
484 if (!m_Quantization.m_Offset.has_value())
490 return m_Quantization.m_Offset.value();
495 m_Quantization.m_Offset = MakeOptional<int32_t>(offset);
500 return m_Quantization.m_QuantizationDim;
505 m_Quantization.m_QuantizationDim = quantizationDim;
527 template<
typename MemoryType>
529 : m_MemoryArea(nullptr)
533 template<
typename MemoryType>
535 : m_MemoryArea(memoryArea)
540 template<
typename MemoryType>
542 : m_MemoryArea(other.m_MemoryArea)
543 , m_Info(other.GetInfo())
547 template<
typename MemoryType>
550 m_Info = other.m_Info;
BaseTensor & operator=(const BaseTensor &)
Tensors are copyable.
BaseTensor()
Empty (invalid) constructor.
float GetQuantizationScale() const
void SetQuantizationScales(const std::vector< float > &scales)
TensorInfo()
Empty (invalid) constructor.
bool IsTypeSpaceMatch(const TensorInfo &other) const
Check that the types are the same and, if quantize, that the quantization parameters are the same.
bool operator!=(const TensorInfo &other) const
void SetQuantizationDim(const Optional< unsigned int > &quantizationDim)
bool operator==(const TensorInfo &other) const
void SetQuantizationOffset(int32_t offset)
void SetQuantizationScale(float scale)
int32_t GetQuantizationOffset() const
unsigned int GetNumElements() const
Optional< unsigned int > GetQuantizationDim() const
std::vector< float > GetQuantizationScales() const
void SetConstant(const bool IsConstant=true)
Marks the data corresponding to this tensor info as constant.
bool HasPerAxisQuantization() const
unsigned int GetNumBytes() const
TensorInfo & operator=(const TensorInfo &other)
bool HasMultipleQuantizationScales() const
bool operator==(const TensorShape &other) const
Equality comparison operator.
TensorShape & operator=(const TensorShape &other)
Assignation function.
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
bool IsAtLeastOneDimensionSpecified() const
Checks if there is at least one dimension specified.
bool GetDimensionSpecificity(unsigned int i) const
Gets information about if the dimension size has been specified or not.
unsigned int operator[](unsigned int i) const
Read only operator.
TensorShape()
Empty (invalid) constructor.
bool operator!=(const TensorShape &other) const
Inequality comparison operator.
void SetNumDimensions(unsigned int numDimensions, bool initDimensionsSpecificity=false)
Sets the tensor rank and therefore the Dimensionality is set to Specified if it was not.
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified.
bool AreAllDimensionsSpecified() const
Checks if there is at least one dimension not specified.
void SetDimensionSize(unsigned int i, unsigned int dimensionSize)
Sets the size of the indicated dimension and Specificity for that dimension is set to true.
Copyright (c) 2021 ARM Limited and Contributors.
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
constexpr unsigned int GetDataTypeSize(DataType dataType)
constexpr unsigned int MaxNumOfTensorDimensions
constexpr bool IsQuantizedType()