ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::misc::tensor::Shape Class Reference

Class to represent shape of a tensor. More...

#include <Shape.h>

Public Member Functions

 Shape (uint32_t rank)
 Construct a new Shape object.
 
 Shape (const std::initializer_list< int32_t > &dimensions)
 Construct a new Shape object.
 
 Shape (const Shape &origin)=default
 Construct a new Shape object.
 
void prepend (int32_t d)
 Add dimension to the beginning.
 
void append (int32_t d)
 Add dimension to the back.
 
uint32_t rank (void) const
 Get the rank of this shape.
 
int32_t dim (uint32_t n) const
 Get specific dimension.
 
int32_t & dim (uint32_t n)
 Get the reference of specific dimension.
 
const std::deque< int32_t > & dims () const
 
uint64_t num_elements () const
 Get the number of elements specified by this shape.
 

Static Public Member Functions

static Shape from (const std::string &s)
 Get a Shape object after parsing string.
 

Detailed Description

Class to represent shape of a tensor.

Definition at line 44 of file Shape.h.

Constructor & Destructor Documentation

◆ Shape() [1/3]

nnfw::misc::tensor::Shape::Shape ( uint32_t  rank)
inline

Construct a new Shape object.

Parameters
[in]rankRank of a tensor

Definition at line 51 of file Shape.h.

51{ _dimensions.resize(rank); }
uint32_t rank(void) const
Get the rank of this shape.
Definition Shape.h:92

References rank().

◆ Shape() [2/3]

nnfw::misc::tensor::Shape::Shape ( const std::initializer_list< int32_t > &  dimensions)
inline

Construct a new Shape object.

Parameters
[in]dimensionsinitializer_list<int32_t> of dimensions of tensor

Definition at line 58 of file Shape.h.

58 : _dimensions{dimensions}
59 {
60 // Check overflow because initializer_list type can be larger size than max of uint32_t
61 assert(dimensions.size() <= 0xFFFFFFFF);
62 }

◆ Shape() [3/3]

nnfw::misc::tensor::Shape::Shape ( const Shape origin)
default

Construct a new Shape object.

Parameters
[in]originShape object to copy

Member Function Documentation

◆ append()

void nnfw::misc::tensor::Shape::append ( int32_t  d)
inline

Add dimension to the back.

Parameters
[in]ddimension to add to the back
Returns
N/A

Definition at line 83 of file Shape.h.

83{ _dimensions.emplace_back(d); }

Referenced by from().

◆ dim() [1/2]

int32_t & nnfw::misc::tensor::Shape::dim ( uint32_t  n)
inline

Get the reference of specific dimension.

Parameters
[in]nIndex of dimension
Returns
Reference of n'th dimension

Definition at line 107 of file Shape.h.

107{ return _dimensions.at(n); }

◆ dim() [2/2]

int32_t nnfw::misc::tensor::Shape::dim ( uint32_t  n) const
inline

Get specific dimension.

Parameters
[in]nIndex of dimension
Returns
n'th dimension

Definition at line 100 of file Shape.h.

100{ return _dimensions.at(n); }

Referenced by nnfw::misc::tensor::IndexEnumerator::advance(), nnfw::misc::tensor::IndexEnumerator::IndexEnumerator(), nnfw::misc::tensor::NonIncreasingStride::init(), nnfw::misc::tensor::Object< T >::Object(), nnfw::misc::tensor::operator<<(), and nnfw::misc::tensor::operator==().

◆ dims()

const std::deque< int32_t > & nnfw::misc::tensor::Shape::dims ( ) const
inline

Definition at line 109 of file Shape.h.

109{ return _dimensions; }

◆ from()

Shape Shape::from ( const std::string &  s)
static

Get a Shape object after parsing string.

Parameters
[in]sString of dimension list. Accepted format is numbers separated by comma.
Returns
Shape object

Definition at line 48 of file Shape.cpp.

49{
50 Shape shape(0);
51
52 bool pending = false;
53 int value = 0;
54
55 for (const char *cur = str.c_str(); true; ++cur)
56 {
57 if (*cur == ',' || *cur == '\0')
58 {
59 if (pending)
60 {
61 shape.append(value);
62 }
63
64 if (*cur == '\0')
65 {
66 break;
67 }
68
69 pending = false;
70 value = 0;
71 continue;
72 }
73
74 assert(*cur >= '0' && *cur <= '9');
75
76 pending = true;
77 value *= 10;
78 value += *cur - '0';
79 }
80
81 return shape;
82}
std::string str(Args &&...args)
Definition Shape.h:28

References append(), and nnfw::misc::str().

◆ num_elements()

uint64_t Shape::num_elements ( ) const

Get the number of elements specified by this shape.

Returns
The number of elements

Definition at line 84 of file Shape.cpp.

85{
86 return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1),
87 std::multiplies<uint64_t>());
88}

◆ prepend()

void nnfw::misc::tensor::Shape::prepend ( int32_t  d)
inline

Add dimension to the beginning.

Parameters
[in]ddimension to add to the beginning
Returns
N/A

Definition at line 76 of file Shape.h.

76{ _dimensions.emplace_front(d); }

◆ rank()

uint32_t nnfw::misc::tensor::Shape::rank ( void  ) const
inline

Get the rank of this shape.

Returns
rank
Note
We can use static_cast
because we don't support larger than max of uint32_t on constructor

Definition at line 92 of file Shape.h.

92{ return static_cast<uint32_t>(_dimensions.size()); }

Referenced by nnfw::misc::tensor::IndexEnumerator::advance(), nnfw::misc::tensor::IndexEnumerator::IndexEnumerator(), nnfw::misc::tensor::NonIncreasingStride::init(), nnfw::misc::tensor::Object< T >::Object(), nnfw::misc::tensor::operator<<(), nnfw::misc::tensor::operator==(), Shape(), and nnfw::misc::tensor::IndexEnumerator::valid().


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