ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 68 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 77 of file Shape.h.

77: _dimensions(rank) {}

◆ Shape() [3/3]

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

Definition at line 79 of file Shape.h.

79: _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 112 of file Shape.h.

112{ _dimensions.emplace_back(d); }

◆ asFeature()

FeatureShape Shape::asFeature ( ) const

Definition at line 27 of file Shape.cc.

28{
29 assert(rank() == 4);
30
31 // Feature Map in NHWC layout
32 // - Dimension(0) -> Batch
33 // - Dimension(1) -> Height
34 // - Dimension(2) -> Width
35 // - Dimension(3) -> Depth
36 const auto batch = dim(0);
37 const auto depth = dim(3);
38 const auto height = dim(1);
39 const auto width = dim(2);
40
41 return {batch, depth, height, width};
42}
int32_t dim(int i) const
Definition Shape.h:85
int rank() const
Definition Shape.h:81

◆ dim() [1/2]

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

Definition at line 92 of file Shape.h.

92{ return _dimensions.at(i); }

◆ dim() [2/2]

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

Definition at line 85 of file Shape.h.

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

References rank().

◆ dims()

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

Definition at line 83 of file Shape.h.

83{ 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 45 of file Shape.cc.

46{
47 assert(to_rank - rank() >= 0);
48 _dimensions.insert(_dimensions.cbegin(), to_rank - rank(), 1);
49}

◆ 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 126 of file Shape.h.

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

◆ num_elements()

uint64_t Shape::num_elements ( ) const

Returns number of elements when rank or dim is specified.

Definition at line 51 of file Shape.cc.

52{
53 // if dimension is 0, it means unspecified and cannot calculate the total number of elements
54 if (std::any_of(_dimensions.begin(), _dimensions.end(),
55 [](const int32_t &v) { return v == kUnspecifiedDim; }))
56 throw std::runtime_error("num_elements() cannot calculate when any dimension is unspecified");
57
58 return std::accumulate(_dimensions.cbegin(), _dimensions.cend(), UINT64_C(1),
59 std::multiplies<uint64_t>());
60}

◆ 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 106 of file Shape.h.

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

◆ rank()

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

Definition at line 81 of file Shape.h.

81{ return _dimensions.size(); }

Referenced by dim().

Field Documentation

◆ kMaxRank

int32_t const onert::ir::Shape::kMaxRank = 6
inlinestatic

Definition at line 73 of file Shape.h.

◆ kUnspecifiedDim

int32_t const onert::ir::Shape::kUnspecifiedDim = -1
inlinestatic

Definition at line 71 of file Shape.h.


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