ONE - On-device Neural Engine
Loading...
Searching...
No Matches
onert::ir::Shape Struct Reference

#include <Shape.h>

Collaboration diagram for onert::ir::Shape:

Public Member Functions

 Shape ()=default
 
 Shape (int rank)
 
 Shape (std::initializer_list< int32_t > dimensions)
 
int rank () const
 
const std::vector< int32_t > & dims () const
 
int32_t dim (int i) const
 
int32_t & dim (int i)
 
uint64_t num_elements () const
 Returns number of elements when rank or dim is specified.
 
FeatureShape asFeature () const
 
void prepend (int32_t d)
 Add dimension to the beginning.
 
void append (int32_t d)
 Add dimension to the end.
 
void extendRank (int to_rank)
 Extend rank of Shape object for operand with param.
 
bool hasUnspecifiedDims () const
 Find out if any dimension is unspecified. If the rank is not specified, it returns false.
 

Static Public Attributes

static int32_t const kUnspecifiedDim = -1
 
static int32_t const kMaxRank = 6
 

Detailed Description

Definition at line 70 of file Shape.h.

Constructor & Destructor Documentation

◆ Shape() [1/3]

onert::ir::Shape::Shape ( )
default

◆ Shape() [2/3]

onert::ir::Shape::Shape ( int  rank)
inlineexplicit

Definition at line 78 of file Shape.h.

78: _dimensions(rank) {}

◆ Shape() [3/3]

onert::ir::Shape::Shape ( std::initializer_list< int32_t >  dimensions)
inline

Definition at line 80 of file Shape.h.

80: _dimensions(dimensions) {}

Member Function Documentation

◆ append()

void onert::ir::Shape::append ( int32_t  d)
inline

Add dimension to the end.

Parameters
[in]ddimension to add to the end

Definition at line 113 of file Shape.h.

113{ _dimensions.emplace_back(d); }

◆ asFeature()

FeatureShape Shape::asFeature ( ) const

Definition at line 34 of file Shape.cc.

35{
36 assert(rank() == 4);
37
38 // Feature Map in NHWC layout
39 // - Dimension(0) -> Batch
40 // - Dimension(1) -> Height
41 // - Dimension(2) -> Width
42 // - Dimension(3) -> Depth
43 const auto batch = dim(0);
44 const auto depth = dim(3);
45 const auto height = dim(1);
46 const auto width = dim(2);
47
48 return {batch, depth, height, width};
49}
int32_t dim(int i) const
Definition Shape.h:86
int rank() const
Definition Shape.h:82

◆ dim() [1/2]

int32_t & onert::ir::Shape::dim ( int  i)
inline

Definition at line 93 of file Shape.h.

93{ return _dimensions.at(i); }

◆ dim() [2/2]

int32_t onert::ir::Shape::dim ( int  i) const
inline

Definition at line 86 of file Shape.h.

87 {
88 assert(rank() != 0 || i == 0);
89 return rank() == 0 ? 1 : _dimensions.at(i);
90 }

References rank().

◆ dims()

const std::vector< int32_t > & onert::ir::Shape::dims ( ) const
inline

Definition at line 84 of file Shape.h.

84{ return _dimensions; }

◆ extendRank()

void Shape::extendRank ( int  to_rank)

Extend rank of Shape object for operand with param.

Parameters
[in]to_rankThe rank value to be extended to

Definition at line 52 of file Shape.cc.

53{
54 assert(to_rank - rank() >= 0);
55 _dimensions.insert(_dimensions.cbegin(), to_rank - rank(), 1);
56}

◆ hasUnspecifiedDims()

bool onert::ir::Shape::hasUnspecifiedDims ( ) const
inline

Find out if any dimension is unspecified. If the rank is not specified, it returns false.

See also
https://developer.android.com/ndk/reference/struct/a-neural-networks-operand-type
Note
base_loader set dim to -1 when there is unknown dim in input tensor

Definition at line 127 of file Shape.h.

128 {
129 return (std::find(_dimensions.begin(), _dimensions.end(), kUnspecifiedDim) !=
130 _dimensions.end());
131 }

◆ num_elements()

uint64_t Shape::num_elements ( ) const

Returns number of elements when rank or dim is specified.

Definition at line 58 of file Shape.cc.

59{
60 // if dimension is 0, it means unspecified and cannot calculate the total number of elements
61 if (std::any_of(_dimensions.begin(), _dimensions.end(),
62 [](const int32_t &v) { return v == kUnspecifiedDim; }))
63 throw std::runtime_error("num_elements() cannot calculate when any dimension is unspecified");
64
65 return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1),
66 std::multiplies<uint64_t>());
67}

◆ prepend()

void onert::ir::Shape::prepend ( int32_t  d)
inline

Add dimension to the beginning.

Parameters
[in]ddimension to add to the beginning

Definition at line 107 of file Shape.h.

107{ _dimensions.insert(_dimensions.cbegin(), d); }

◆ rank()

int onert::ir::Shape::rank ( ) const
inline

Definition at line 82 of file Shape.h.

82{ return _dimensions.size(); }

Referenced by dim().

Field Documentation

◆ kMaxRank

int32_t const Shape::kMaxRank = 6
static

Definition at line 74 of file Shape.h.

◆ kUnspecifiedDim

int32_t const Shape::kUnspecifiedDim = -1
static

Definition at line 73 of file Shape.h.


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