ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Shape.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
23#ifndef __NNFW_MISC_TENSOR_SHAPE_H__
24#define __NNFW_MISC_TENSOR_SHAPE_H__
25
26#include <cstdint>
27#include <cstddef>
28#include <deque>
29#include <initializer_list>
30#include <ostream>
31#include <string>
32#include <cassert>
33
34namespace nnfw
35{
36namespace misc
37{
38namespace tensor
39{
40
44class Shape
45{
46public:
51 Shape(uint32_t rank) { _dimensions.resize(rank); }
52
53public:
58 Shape(const std::initializer_list<int32_t> &dimensions) : _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 }
63
68 Shape(const Shape &origin) = default;
69
70public:
76 void prepend(int32_t d) { _dimensions.emplace_front(d); }
77
83 void append(int32_t d) { _dimensions.emplace_back(d); }
84
85public:
92 uint32_t rank(void) const { return static_cast<uint32_t>(_dimensions.size()); }
93
94public:
100 int32_t dim(uint32_t n) const { return _dimensions.at(n); }
101
107 int32_t &dim(uint32_t n) { return _dimensions.at(n); }
108
109 const std::deque<int32_t> &dims() const { return _dimensions; }
110
111public:
116 uint64_t num_elements() const;
117
118private:
119 std::deque<int32_t> _dimensions;
120
121public:
127 static Shape from(const std::string &s);
128};
129
136bool operator==(const Shape &, const Shape &);
137
144std::ostream &operator<<(std::ostream &os, const Shape &shape);
145
146} // namespace tensor
147} // namespace misc
148} // namespace nnfw
149
150#endif // __NNFW_MISC_TENSOR_SHAPE_H__
Class to represent shape of a tensor.
Definition Shape.h:45
const std::deque< int32_t > & dims() const
Definition Shape.h:109
int32_t dim(uint32_t n) const
Get specific dimension.
Definition Shape.h:100
void append(int32_t d)
Add dimension to the back.
Definition Shape.h:83
static Shape from(const std::string &s)
Get a Shape object after parsing string.
Definition Shape.cpp:48
void prepend(int32_t d)
Add dimension to the beginning.
Definition Shape.h:76
Shape(const Shape &origin)=default
Construct a new Shape object.
uint32_t rank(void) const
Get the rank of this shape.
Definition Shape.h:92
Shape(uint32_t rank)
Construct a new Shape object.
Definition Shape.h:51
uint64_t num_elements() const
Get the number of elements specified by this shape.
Definition Shape.cpp:84
Shape(const std::initializer_list< int32_t > &dimensions)
Construct a new Shape object.
Definition Shape.h:58
int32_t & dim(uint32_t n)
Get the reference of specific dimension.
Definition Shape.h:107
bool operator==(const Shape &, const Shape &)
Check equality of two Shape.
Definition Shape.cpp:30
std::ostream & operator<<(std::ostream &os, const IndexFormatter &fmt)
Send IndexFormatter object to output stream.
Definition topk_v2.h:30
Definition Shape.h:28