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
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
28{
29namespace ir
30{
31
36{
37 int32_t N;
38 int32_t C;
39 int32_t H;
40 int32_t W;
45 FeatureShape() = default;
52 FeatureShape(int32_t depth, int32_t height, int32_t width) : N{1}, C{depth}, H{height}, W{width}
53 {
54 // DO NOTHING
55 }
63 FeatureShape(int32_t batch, int32_t depth, int32_t height, int32_t width)
64 : N{batch}, C{depth}, H{height}, W{width}
65 {
66 // DO NOTHING
67 }
68};
69
70struct Shape
71{
72public:
73 static int32_t const kUnspecifiedDim;
74 static int32_t const kMaxRank;
75
76 Shape() = default;
77
78 explicit Shape(int rank) : _dimensions(rank) {}
79
80 Shape(std::initializer_list<int32_t> dimensions) : _dimensions(dimensions) {}
81
82 int rank() const { return _dimensions.size(); }
83
84 const std::vector<int32_t> &dims() const { return _dimensions; }
85
86 int32_t dim(int i) const
87 {
88 assert(rank() != 0 || i == 0);
89 return rank() == 0 ? 1 : _dimensions.at(i);
90 }
91
92 // TODO Fix different behavior with const version
93 int32_t &dim(int i) { return _dimensions.at(i); }
94
98 uint64_t num_elements() const;
99
100public:
101 FeatureShape asFeature() const;
102
107 void prepend(int32_t d) { _dimensions.insert(_dimensions.cbegin(), d); }
108
113 void append(int32_t d) { _dimensions.emplace_back(d); }
114
119 void extendRank(int to_rank);
120
128 {
129 return (std::find(_dimensions.begin(), _dimensions.end(), kUnspecifiedDim) !=
130 _dimensions.end());
131 }
132
133private:
134 std::vector<int32_t> _dimensions;
135};
136
137inline bool operator==(const Shape &lhs, const Shape &rhs) { return lhs.dims() == rhs.dims(); }
138inline bool operator!=(const Shape &lhs, const Shape &rhs) { return lhs.dims() != rhs.dims(); }
139
146Shape convertShape(const Shape &shape, const PermuteType &type);
147
153inline bool rankMaybeUnspecified(const ir::Shape &shape) { return (shape.rank() == 0); }
154
155} // namespace ir
156} // namespace onert
157
158#endif // __ONERT_IR_SHAPE_H__
bool operator==(const Shape &lhs, const Shape &rhs)
Definition Shape.h:137
bool operator!=(const Shape &lhs, const Shape &rhs)
Definition Shape.h:138
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:153
Shape convertShape(const Shape &shape, const PermuteType &type)
Converts shape when its rank is 4.
Definition Shape.cc:69
PermuteType
Definition Layout.h:37
Definition Shape.h:28
Structure to have values of dimensions for feature.
Definition Shape.h:36
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:52
FeatureShape(int32_t batch, int32_t depth, int32_t height, int32_t width)
Construct FeatureShape object with four values of dimensions.
Definition Shape.h:63
Shape(std::initializer_list< int32_t > dimensions)
Definition Shape.h:80
void prepend(int32_t d)
Add dimension to the beginning.
Definition Shape.h:107
int32_t dim(int i) const
Definition Shape.h:86
void append(int32_t d)
Add dimension to the end.
Definition Shape.h:113
int rank() const
Definition Shape.h:82
static int32_t const kUnspecifiedDim
Definition Shape.h:73
Shape(int rank)
Definition Shape.h:78
int32_t & dim(int i)
Definition Shape.h:93
const std::vector< int32_t > & dims() const
Definition Shape.h:84
static int32_t const kMaxRank
Definition Shape.h:74
bool hasUnspecifiedDims() const
Find out if any dimension is unspecified. If the rank is not specified, it returns false.
Definition Shape.h:127