ONE - On-device Neural Engine
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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
17#ifndef __ONERT_IR_SHAPE_H__
18#define __ONERT_IR_SHAPE_H__
19
20#include "ir/Layout.h"
21
22#include <cassert>
23#include <cstdint>
24#include <vector>
25#include <algorithm>
26
27namespace onert::ir
28{
29
34{
35 int32_t N;
36 int32_t C;
37 int32_t H;
38 int32_t W;
43 FeatureShape() = default;
50 FeatureShape(int32_t depth, int32_t height, int32_t width) : N{1}, C{depth}, H{height}, W{width}
51 {
52 // DO NOTHING
53 }
61 FeatureShape(int32_t batch, int32_t depth, int32_t height, int32_t width)
62 : N{batch}, C{depth}, H{height}, W{width}
63 {
64 // DO NOTHING
65 }
66};
67
68struct Shape
69{
70public:
71 static inline int32_t const kUnspecifiedDim = -1;
72 // NNFW_MAX_RANK is 6
73 static inline int32_t const kMaxRank = 6;
74
75 Shape() = default;
76
77 explicit Shape(int rank) : _dimensions(rank) {}
78
79 Shape(std::initializer_list<int32_t> dimensions) : _dimensions(dimensions) {}
80
81 int rank() const { return _dimensions.size(); }
82
83 const std::vector<int32_t> &dims() const { return _dimensions; }
84
85 int32_t dim(int i) const
86 {
87 assert(rank() != 0 || i == 0);
88 return rank() == 0 ? 1 : _dimensions.at(i);
89 }
90
91 // TODO Fix different behavior with const version
92 int32_t &dim(int i) { return _dimensions.at(i); }
93
97 uint64_t num_elements() const;
98
99public:
100 FeatureShape asFeature() const;
101
106 void prepend(int32_t d) { _dimensions.insert(_dimensions.cbegin(), d); }
107
112 void append(int32_t d) { _dimensions.emplace_back(d); }
113
118 void extendRank(int to_rank);
119
127 {
128 return (std::find(_dimensions.begin(), _dimensions.end(), kUnspecifiedDim) !=
129 _dimensions.end());
130 }
131
132private:
133 std::vector<int32_t> _dimensions;
134};
135
136inline bool operator==(const Shape &lhs, const Shape &rhs) { return lhs.dims() == rhs.dims(); }
137inline bool operator!=(const Shape &lhs, const Shape &rhs) { return lhs.dims() != rhs.dims(); }
138
145Shape convertShape(const Shape &shape, const PermuteType &type);
146
152inline bool rankMaybeUnspecified(const ir::Shape &shape) { return (shape.rank() == 0); }
153
154} // namespace onert::ir
155
156#endif // __ONERT_IR_SHAPE_H__
bool operator==(const Shape &lhs, const Shape &rhs)
Definition Shape.h:136
bool operator!=(const Shape &lhs, const Shape &rhs)
Definition Shape.h:137
bool rankMaybeUnspecified(const ir::Shape &shape)
Find out if tha rank in this shape is "maybe" unspecified. Note that when rank == 0,...
Definition Shape.h:152
Shape convertShape(const Shape &shape, const PermuteType &type)
Converts shape when its rank is 4.
Definition Shape.cc:62
PermuteType
Definition Layout.h:35
Definition Shape.h:28
Structure to have values of dimensions for feature.
Definition Shape.h:34
FeatureShape()=default
Construct FeatureShape object using default constrcutor.
FeatureShape(int32_t depth, int32_t height, int32_t width)
Construct FeatureShape object with three values of dimensions.
Definition Shape.h:50
FeatureShape(int32_t batch, int32_t depth, int32_t height, int32_t width)
Construct FeatureShape object with four values of dimensions.
Definition Shape.h:61
Shape(std::initializer_list< int32_t > dimensions)
Definition Shape.h:79
void prepend(int32_t d)
Add dimension to the beginning.
Definition Shape.h:106
static int32_t const kUnspecifiedDim
Definition Shape.h:71
int32_t dim(int i) const
Definition Shape.h:85
void append(int32_t d)
Add dimension to the end.
Definition Shape.h:112
int rank() const
Definition Shape.h:81
Shape(int rank)
Definition Shape.h:77
int32_t & dim(int i)
Definition Shape.h:92
const std::vector< int32_t > & dims() const
Definition Shape.h:83
static int32_t const kMaxRank
Definition Shape.h:73
bool hasUnspecifiedDims() const
Find out if any dimension is unspecified. If the rank is not specified, it returns false.
Definition Shape.h:126