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

#include <Shape.h>

Public Member Functions

Shapeoperator= (Shape const &)=delete
 
 Shape ()
 
 Shape (int dimensions_count)
 
 Shape (int shape_size, int32_t value)
 
 Shape (int dimensions_count, const int32_t *dims_data)
 
 Shape (const std::initializer_list< int > init_list)
 
 Shape (const Shape &other)
 
 Shape (Shape &&other)=default
 
bool operator== (const Shape &comp) const
 
 ~Shape ()=default
 
int32_t DimensionsCount () const
 
int32_t Dims (int i) const
 
void SetDim (int i, int32_t val)
 
int32_t * DimsData ()
 
const int32_t * DimsData () const
 
const int32_t * DimsDataUpTo6D () const
 
void Resize (int dimensions_count)
 
void ReplaceWith (int dimensions_count, const int32_t *dims_data)
 
void ReplaceWith (const Shape &other)
 
void ReplaceWith (Shape &&other)
 
template<typename Iterable >
void BuildFrom (const Iterable &src_iterable)
 
void BuildFrom (const std::initializer_list< int > init_list)
 
int FlatSize () const
 
bool operator!= (const Shape &comp) const
 

Static Public Member Functions

static Shape ExtendedShape (int new_shape_size, const Shape &shape)
 

Static Public Attributes

static constexpr int kMaxSmallSize = 6
 

Detailed Description

Definition at line 34 of file Shape.h.

Constructor & Destructor Documentation

◆ Shape() [1/7]

nnfw::cker::Shape::Shape ( )
inline

Definition at line 45 of file Shape.h.

45: _size(0), dims_(std::array<int32_t, kMaxSmallSize>{}) {}

Referenced by ExtendedShape().

◆ Shape() [2/7]

nnfw::cker::Shape::Shape ( int  dimensions_count)
inlineexplicit

Definition at line 50 of file Shape.h.

50: _size(dimensions_count) { initStorage(dimensions_count); }

◆ Shape() [3/7]

nnfw::cker::Shape::Shape ( int  shape_size,
int32_t  value 
)
inline

Definition at line 53 of file Shape.h.

53 : _size(shape_size)
54 {
55 initStorage(shape_size);
56 for (int i = 0; i < shape_size; ++i)
57 {
58 SetDim(i, value);
59 }
60 }
void SetDim(int i, int32_t val)
Definition Shape.h:120

References SetDim().

◆ Shape() [4/7]

nnfw::cker::Shape::Shape ( int  dimensions_count,
const int32_t *  dims_data 
)
inline

Definition at line 63 of file Shape.h.

63 : _size(dimensions_count)
64 {
65 initStorage(dimensions_count);
66 ReplaceWith(dimensions_count, dims_data);
67 }
void ReplaceWith(int dimensions_count, const int32_t *dims_data)
Definition Shape.h:206

References ReplaceWith().

◆ Shape() [5/7]

nnfw::cker::Shape::Shape ( const std::initializer_list< int >  init_list)
inline

Definition at line 71 of file Shape.h.

71 : _size(0)
72 {
73 const auto size = static_cast<int>(std::distance(init_list.begin(), init_list.end()));
74 initStorage(size);
75 BuildFrom(init_list);
76 }
void BuildFrom(const Iterable &src_iterable)
Definition Shape.h:230
int32_t size[5]
Definition Slice.cpp:35

References BuildFrom(), and size.

◆ Shape() [6/7]

nnfw::cker::Shape::Shape ( const Shape other)
inline

Definition at line 79 of file Shape.h.

79 : _size(other._size)
80 {
81 if (_size <= kMaxSmallSize)
82 {
83 // When the number of dimensions is small, copy the fixed array.
84 dims_ = std::get<std::array<int32_t, kMaxSmallSize>>(other.dims_);
85 }
86 else
87 {
88 // Otherwise, copy the dynamically allocated vector.
89 dims_ = std::get<std::vector<int32_t>>(other.dims_);
90 }
91 }
static constexpr int kMaxSmallSize
Definition Shape.h:39

References kMaxSmallSize.

◆ Shape() [7/7]

nnfw::cker::Shape::Shape ( Shape &&  other)
default

◆ ~Shape()

nnfw::cker::Shape::~Shape ( )
default

Member Function Documentation

◆ BuildFrom() [1/2]

template<typename Iterable >
void nnfw::cker::Shape::BuildFrom ( const Iterable &  src_iterable)
inline

Definition at line 230 of file Shape.h.

231 {
232 const int dimensions_count =
233 static_cast<int>(std::distance(src_iterable.begin(), src_iterable.end()));
234 Resize(dimensions_count);
235 int32_t *data = DimsData();
236 for (auto it = src_iterable.begin(); it != src_iterable.end(); ++it)
237 {
238 *data++ = static_cast<int32_t>(*it);
239 }
240 }
void Resize(int dimensions_count)
Definition Shape.h:166
int32_t * DimsData()
Definition Shape.h:134

References DimsData(), and Resize().

Referenced by Shape().

◆ BuildFrom() [2/2]

void nnfw::cker::Shape::BuildFrom ( const std::initializer_list< int >  init_list)
inline

Definition at line 250 of file Shape.h.

251 {
252 BuildFrom<const std::initializer_list<int>>(init_list);
253 }

◆ DimensionsCount()

int32_t nnfw::cker::Shape::DimensionsCount ( ) const
inline

Definition at line 103 of file Shape.h.

103{ return _size; }

Referenced by nnfw::cker::train::Adam(), nnfw::cker::ArgMinMax(), nnfw::cker::AveragePool16(), nnfw::cker::train::AveragePool2DGrad(), nnfw::cker::AveragePool32(), nnfw::cker::AveragePool< float >(), nnfw::cker::AveragePool< int8_t >(), nnfw::cker::BatchToSpaceND(), nnfw::cker::bias_op::biasHelper(), nnfw::cker::functor::biasReductionHelper(), nnfw::cker::BroadcastComparison4DSlowImpl(), nnfw::cker::BroadcastComparison4DSlowWithScaling(), nnfw::cker::BroadcastSelect4DSlow(), nnfw::cker::BroadcastTo(), nnfw::cker::train::CategoricalCrossEntropyWithLogits(), nnfw::cker::checkMatching(), nnfw::cker::reference::Conv(), nnfw::cker::multithreaded::Conv(), nnfw::cker::reference::Conv(), nnfw::cker::reference::Conv(), nnfw::cker::optimized::Conv(), nnfw::cker::Tensor::copyFrom(), nnfw::cker::train::Depad(), nnfw::cker::DepthToSpace(), nnfw::cker::DepthwiseConv(), nnfw::cker::reference_integer_ops::DepthwiseConvHybridPerChannel(), nnfw::cker::optimized::DepthwiseConvImpl(), nnfw::cker::optimized_integer_ops::DepthwiseConvPerChannel(), nnfw::cker::reference_integer_ops::DepthwiseConvPerChannel(), nnfw::cker::optimized_integer_ops::DepthwiseConvWithRounding(), nnfw::cker::optimized::DepthwiseConvWithRounding(), nnfw::cker::optimized::DilatedIm2col(), nnfw::cker::optimized::ExtractPatchIntoBufferColumn(), nnfw::cker::FlatSizeSkipDim(), nnfw::cker::FloorDivBroadcast(), nnfw::cker::FloorModBroadcast(), nnfw::cker::BCast::FromShape(), nnfw::cker::FullyConnected(), nnfw::cker::train::FullyConnectedBiasGrad(), nnfw::cker::FullyConnectedSparseWeight16x1(), nnfw::cker::FullyConnectedSparseWeightRandom(), nnfw::cker::Gather(), nnfw::cker::train::GradientDescent(), nnfw::cker::reference::HybridConvPerChannel(), nnfw::cker::optimized::Im2col(), nnfw::cker::InitTensorDataForReduce(), nnfw::cker::L2NormalizeFloat32(), nnfw::cker::L2NormalizeQuant8(), nnfw::cker::LogicalAndBroadcast(), nnfw::cker::LogicalOrBroadcast(), nnfw::cker::LogSoftmax(), nnfw::cker::LogSoftmax(), nnfw::cker::MapAsMatrixWithLastDimAsRows(), nnfw::cker::MatchingFlatSizeSkipDim(), nnfw::cker::MatchingFlatSizeSkipDim(), nnfw::cker::MaximumMinimumBroadcast4DSlow(), nnfw::cker::train::MaxPool2D(), nnfw::cker::train::MaxPool2DGrad(), nnfw::cker::MaxPool< float >(), nnfw::cker::MaxPool< uint8_t >(), nnfw::cker::Mean(), nnfw::cker::MeanAxis1And2(), nnfw::cker::MeanQ8Asymm(), nnfw::cker::Offset(), nnfw::cker::Offset(), nnfw::cker::Offset(), nnfw::cker::OneHot(), nnfw::cker::DynamicUpdateSlice::operator()(), nnfw::cker::Pad(), nnfw::cker::FCTempArena::prepare(), nnfw::cker::BatchMatMul::prepare(), nnfw::cker::ReduceMean::PrepareforReduce(), nnfw::cker::ProcessBroadcastShapes(), nnfw::cker::Reduce::QuantizedMeanOrSum(), nnfw::cker::Reduce::ReduceGeneric(), nnfw::cker::ReduceImpl(), nnfw::cker::ReduceMeanImpl(), nnfw::cker::ReduceSumQuantImpl(), ReplaceWith(), nnfw::cker::ResizeBilinear(), nnfw::cker::Reverse(), nnfw::cker::RmsNorm(), nnfw::cker::reference::Softmax(), nnfw::cker::Softmax(), nnfw::cker::train::SoftMaxGrad(), nnfw::cker::SpaceToBatchND(), nnfw::cker::SpaceToDepth(), nnfw::cker::Split(), nnfw::cker::SplitV(), nnfw::cker::SqDiff(), nnfw::cker::StatelessRandomUniform(), nnfw::cker::StridedSlice(), nnfw::cker::TileOneDimension(), nnfw::cker::TopKV2(), nnfw::cker::Transpose(), nnfw::cker::Transpose2D(), nnfw::cker::TransposeConv(), nnfw::cker::TransposeImpl(), nnfw::cker::reference::TransposeImpl(), and nnfw::cker::Unpack().

◆ Dims()

int32_t nnfw::cker::Shape::Dims ( int  i) const
inline

Definition at line 106 of file Shape.h.

107 {
108 assert(i >= 0 && i < _size);
109 if (_size <= kMaxSmallSize)
110 {
111 return std::get<std::array<int32_t, kMaxSmallSize>>(dims_)[i];
112 }
113 else
114 {
115 return std::get<std::vector<int32_t>>(dims_)[i];
116 }
117 }

References kMaxSmallSize.

Referenced by nnfw::cker::ArgMinMax(), nnfw::cker::AveragePool16(), nnfw::cker::train::AveragePool2DGrad(), nnfw::cker::AveragePool32(), nnfw::cker::AveragePool< float >(), nnfw::cker::AveragePool< int8_t >(), nnfw::cker::train::backpropFilter(), nnfw::cker::train::backpropInput(), nnfw::cker::BatchMatMulParams::BatchMatMulParams(), nnfw::cker::BatchToSpaceND(), nnfw::cker::bias_op::biasHelper(), nnfw::cker::functor::biasReductionHelper(), nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow(), nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow(), nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow(), nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow(), nnfw::cker::reference::BroadcastBinaryArithmeticOpSlow(), nnfw::cker::BroadcastSelect4DSlow(), nnfw::cker::train::CategoricalCrossEntropy(), nnfw::cker::train::CategoricalCrossEntropyGrad(), nnfw::cker::train::CategoricalCrossEntropyWithLogits(), nnfw::cker::checkMatching(), nnfw::cker::Concatenation(), nnfw::cker::ConcatenationWithScaling(), nnfw::cker::reference::Conv(), nnfw::cker::multithreaded::Conv(), nnfw::cker::reference::Conv(), nnfw::cker::reference::Conv(), nnfw::cker::optimized::Conv(), nnfw::cker::train::ConvFilterGrad(), nnfw::cker::train::ConvInputGrad(), nnfw::cker::CopyDimsToDesc(), nnfw::cker::train::Depad(), nnfw::cker::DepthToSpace(), nnfw::cker::optimized_integer_ops::depthwise_conv::DepthwiseConvGeneral(), nnfw::cker::optimized::depthwise_conv::DepthwiseConvGeneral(), nnfw::cker::reference_integer_ops::DepthwiseConvHybridPerChannel(), nnfw::cker::optimized::DepthwiseConvImpl(), nnfw::cker::DepthwiseConvOp(), nnfw::cker::reference_integer_ops::DepthwiseConvPerChannel(), nnfw::cker::optimized_integer_ops::DepthwiseConvWithRounding(), nnfw::cker::optimized::DepthwiseConvWithRounding(), nnfw::cker::optimized::DilatedIm2col(), nnfw::cker::BCast::FromShape(), nnfw::cker::FullyConnected(), nnfw::cker::FullyConnected(), nnfw::cker::train::FullyConnectedBiasGrad(), nnfw::cker::FullyConnectedHybrid(), nnfw::cker::FullyConnectedSparseWeight16x1(), nnfw::cker::FullyConnectedSparseWeightRandom(), nnfw::cker::Gather(), nnfw::cker::HowManyConvThreads(), nnfw::cker::optimized_integer_ops::HowManyConvThreads(), nnfw::cker::reference::HybridConvPerChannel(), nnfw::cker::optimized::Im2col(), nnfw::cker::LogSoftmax(), nnfw::cker::LogSoftmax(), nnfw::cker::MapAsMatrixWithLastDimAsRows(), nnfw::cker::MatchingDim(), nnfw::cker::MatchingDim(), nnfw::cker::MatchingFlatSizeSkipDim(), nnfw::cker::MatchingFlatSizeSkipDim(), nnfw::cker::train::MaxPool2D(), nnfw::cker::MaxPool< float >(), nnfw::cker::MaxPool< uint8_t >(), nnfw::cker::MeanAxis1And2(), nnfw::cker::train::MSE(), nnfw::cker::train::MSEGrad(), nnfw::cker::OneHot(), nnfw::cker::DynamicUpdateSlice::operator()(), nnfw::cker::ShapeIterator::operator*(), nnfw::cker::Pad(), onert::backend::cpu::ops::ConvolutionLayer::prepare(), onert::backend::cpu::ops::FullyConnectedLayer::prepare(), nnfw::cker::FCTempArena::prepare(), nnfw::cker::BatchMatMul::prepare(), nnfw::cker::ProcessBroadcastShapes(), nnfw::cker::Reduce::QuantizedMeanOrSum(), nnfw::cker::ResizeBilinear(), nnfw::cker::ResizeBilinear(), nnfw::cker::ResizeBilinear(), nnfw::cker::ResizeBilinearKernel2x2(), nnfw::cker::Reverse(), nnfw::cker::RmsNorm(), nnfw::cker::RoPE(), nnfw::cker::Tensor::shaped(), nnfw::cker::Slice(), nnfw::cker::train::SoftMaxGrad(), nnfw::cker::SpaceToBatchND(), nnfw::cker::SpaceToDepth(), nnfw::cker::Split(), nnfw::cker::SplitV(), nnfw::cker::StartForAxis(), nnfw::cker::StopForAxis(), nnfw::cker::StridedSlice(), nnfw::cker::TileOneDimension(), nnfw::cker::TopKV2(), nnfw::cker::Transpose3D(), nnfw::cker::TransposeConv(), nnfw::cker::Unpack(), and nnfw::cker::DynamicUpdateSlice::~DynamicUpdateSlice().

◆ DimsData() [1/2]

◆ DimsData() [2/2]

const int32_t * nnfw::cker::Shape::DimsData ( ) const
inline

Definition at line 147 of file Shape.h.

148 {
149 if (_size <= kMaxSmallSize)
150 {
151 return std::get<std::array<int32_t, kMaxSmallSize>>(dims_).data();
152 }
153 else
154 {
155 return std::get<std::vector<int32_t>>(dims_).data();
156 }
157 }

References kMaxSmallSize.

◆ DimsDataUpTo6D()

const int32_t * nnfw::cker::Shape::DimsDataUpTo6D ( ) const
inline

Definition at line 160 of file Shape.h.

161 {
162 return std::get<std::array<int32_t, kMaxSmallSize>>(dims_).data();
163 }

Referenced by nnfw::cker::Offset(), nnfw::cker::Offset(), and nnfw::cker::Offset().

◆ ExtendedShape()

static Shape nnfw::cker::Shape::ExtendedShape ( int  new_shape_size,
const Shape shape 
)
inlinestatic

Definition at line 244 of file Shape.h.

245 {
246 return Shape(new_shape_size, shape, 1);
247 }

References Shape().

◆ FlatSize()

int nnfw::cker::Shape::FlatSize ( ) const
inline

Definition at line 256 of file Shape.h.

257 {
258 int buffer_size = 1;
259 const int *dims_data = DimsData();
260 for (int i = 0; i < _size; i++)
261 {
262 buffer_size *= dims_data[i];
263 }
264 return buffer_size;
265 }

References DimsData().

Referenced by nnfw::cker::optimized::AddBiasAndEvalActivationFunction(), nnfw::cker::AddN(), nnfw::cker::train::AveragePool2DGrad(), nnfw::cker::BroadcastTo(), nnfw::cker::checkMatching(), nnfw::cker::Tensor::copyFrom(), nnfw::cker::reference_integer_ops::DepthwiseConvHybridPerChannel(), nnfw::cker::optimized_integer_ops::DepthwiseConvWithRounding(), nnfw::cker::optimized::DepthwiseConvWithRounding(), nnfw::cker::Tensor::flat(), nnfw::cker::Tensor::flat(), nnfw::cker::FloorDivElementwise(), nnfw::cker::FloorModElementwise(), nnfw::cker::FullyConnected(), nnfw::cker::train::FullyConnectedBiasGrad(), onert::backend::cpu::ops::FullyConnectedLayer::fullyConnectedHybrid(), nnfw::cker::FullyConnectedHybrid(), nnfw::cker::Gather(), nnfw::cker::LogicalAndElementwise(), nnfw::cker::LogicalOrElementwise(), nnfw::cker::MapAsVector(), nnfw::cker::MatchingElementsSize(), nnfw::cker::MatchingFlatSize(), nnfw::cker::train::MaxPool2DGrad(), nnfw::cker::OneHot(), nnfw::cker::Conv::operator()(), nnfw::cker::DynamicUpdateSlice::operator()(), onert::backend::cpu::ops::FullyConnectedLayer::prepare(), nnfw::cker::FCTempArena::prepare(), nnfw::cker::BatchMatMul::prepare(), nnfw::cker::RankOneSelect(), nnfw::cker::functor::BroadcastTo< Device, T >::ReshapeAndBCast(), and nnfw::cker::Transpose().

◆ operator!=()

bool nnfw::cker::Shape::operator!= ( const Shape comp) const
inline

Definition at line 267 of file Shape.h.

267{ return !((*this) == comp); }

◆ operator=()

Shape & nnfw::cker::Shape::operator= ( Shape const &  )
delete

◆ operator==()

bool nnfw::cker::Shape::operator== ( const Shape comp) const
inline

Definition at line 94 of file Shape.h.

95 {
96 return this->_size == comp._size &&
97 std::memcmp(DimsData(), comp.DimsData(), _size * sizeof(int32_t)) == 0;
98 }

References DimsData().

◆ ReplaceWith() [1/3]

void nnfw::cker::Shape::ReplaceWith ( const Shape other)
inline

Definition at line 217 of file Shape.h.

218 {
219 ReplaceWith(other.DimensionsCount(), other.DimsData());
220 }

References DimensionsCount(), DimsData(), and ReplaceWith().

◆ ReplaceWith() [2/3]

void nnfw::cker::Shape::ReplaceWith ( int  dimensions_count,
const int32_t *  dims_data 
)
inline

Definition at line 206 of file Shape.h.

207 {
208 // Allow dims_data to be nullptr when dimensions_count is 0,
209 // because there are no dimensions to copy. For any non-zero dimensions_count,
210 // dims_data must not be nullptr to ensure valid shape data is provided.
211 assert(dimensions_count == 0 || dims_data != nullptr);
212 Resize(dimensions_count);
213 std::memcpy(DimsData(), dims_data, dimensions_count * sizeof(int32_t));
214 }

References DimsData(), and Resize().

Referenced by nnfw::cker::train::Adam(), onert::backend::train::ops::MeanLayer::backward(), nnfw::cker::BroadcastTo(), nnfw::cker::train::CategoricalCrossEntropyWithLogits(), nnfw::cker::FusedBatchNorm::copyFrom(), nnfw::cker::Tensor::copyFrom(), nnfw::cker::train::GradientDescent(), ReplaceWith(), Shape(), and nnfw::cker::StatelessRandomUniform().

◆ ReplaceWith() [3/3]

void nnfw::cker::Shape::ReplaceWith ( Shape &&  other)
inline

Definition at line 223 of file Shape.h.

224 {
225 std::swap(_size, other._size);
226 dims_ = std::move(other.dims_);
227 }

◆ Resize()

void nnfw::cker::Shape::Resize ( int  dimensions_count)
inline

Definition at line 166 of file Shape.h.

167 {
168 // If dims_ is in a valueless state (i.e. not yet initialized or lost due to an exception),
169 // initialize dims_ explicitly based on dimensions_count to ensure it is in a valid state.
170 if (dims_.valueless_by_exception())
171 {
172 initStorage(dimensions_count);
173 }
174
175 std::vector<int32_t> oldDims;
176 oldDims.reserve(_size);
177 if (_size <= kMaxSmallSize)
178 {
179 const auto &arr = std::get<std::array<int32_t, kMaxSmallSize>>(dims_);
180 oldDims.assign(arr.begin(), arr.begin() + _size);
181 }
182 else
183 {
184 oldDims = std::get<std::vector<int32_t>>(dims_);
185 }
186
187 int count = std::min(_size, dimensions_count);
188
189 if (dimensions_count <= kMaxSmallSize)
190 {
191 std::array<int32_t, kMaxSmallSize> dims = {};
192 std::copy_n(oldDims.begin(), count, dims.begin());
193 dims_ = dims;
194 }
195 else
196 {
197 std::vector<int32_t> dims(dimensions_count, 0);
198 std::copy_n(oldDims.begin(), count, dims.begin());
199 dims_ = dims;
200 }
201
202 _size = dimensions_count;
203 }
arr
Definition infer.py:92
std::vector< int > dims(const std::string &src)
Definition Utils.h:35

References kMaxSmallSize.

Referenced by BuildFrom(), nnfw::cker::BatchMatMul::prepare(), and ReplaceWith().

◆ SetDim()

void nnfw::cker::Shape::SetDim ( int  i,
int32_t  val 
)
inline

Definition at line 120 of file Shape.h.

121 {
122 assert(i >= 0 && i < _size);
123 if (_size <= kMaxSmallSize)
124 {
125 std::get<std::array<int32_t, kMaxSmallSize>>(dims_)[i] = val;
126 }
127 else
128 {
129 std::get<std::vector<int32_t>>(dims_)[i] = val;
130 }
131 }

References kMaxSmallSize.

Referenced by onert::backend::train::ops::MeanLayer::backward(), nnfw::cker::BatchToSpaceND(), nnfw::cker::BatchMatMul::prepare(), Shape(), and nnfw::cker::BCast::ToShape().

Field Documentation

◆ kMaxSmallSize

constexpr int nnfw::cker::Shape::kMaxSmallSize = 6
staticconstexpr

Definition at line 39 of file Shape.h.

Referenced by Dims(), DimsData(), DimsData(), Resize(), SetDim(), and Shape().


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