Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / Shape.h
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
27 namespace onert
28 {
29 namespace ir
30 {
31
32 /**
33  * @brief  Structure to have values of dimensions for feature
34  */
35 struct FeatureShape
36 {
37   int32_t N; /**< The batch value  */
38   int32_t C; /**< The depth value  */
39   int32_t H; /**< The height value */
40   int32_t W; /**< The width value  */
41
42   /**
43    * @brief  Construct FeatureShape object using default constrcutor
44    */
45   FeatureShape() = default;
46   /**
47    * @brief  Construct FeatureShape object with three values of dimensions
48    * @param[in]  depth  The depth value
49    * @param[in]  height The height value
50    * @param[in]  width  The width value
51    */
52   FeatureShape(int32_t depth, int32_t height, int32_t width) : N{1}, C{depth}, H{height}, W{width}
53   {
54     // DO NOTHING
55   }
56   /**
57    * @brief  Construct FeatureShape object with four values of dimensions
58    * @param[in]  batch  The batch value
59    * @param[in]  depth  The depth value
60    * @param[in]  height The height value
61    * @param[in]  width  The width value
62    */
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
70 struct Shape
71 {
72 public:
73   static int32_t const UNSPECIFIED_DIM;
74   static int32_t const MAX_RANK;
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   int32_t &dim(int i) { return _dimensions.at(i); }
93
94   /**
95    * @brief Returns number of elements when rank or dim is specified
96    */
97   uint64_t num_elements() const;
98
99 public:
100   FeatureShape asFeature(Layout layout) const;
101
102   /**
103    * @brief Add dimension to the beginning
104    * @param[in] d dimension to add to the beginning
105    */
106   void prepend(int32_t d) { _dimensions.insert(_dimensions.cbegin(), d); }
107
108   /**
109    * @brief Add dimension to the end
110    * @param[in] d dimension to add to the end
111    */
112   void append(int32_t d) { _dimensions.emplace_back(d); }
113
114   /**
115    * @brief Extend rank of Shape object for operand with param.
116    * @param[in] to_rank The rank value to be extended to
117    */
118   void extendRank(int to_rank);
119
120   /**
121    * @brief Find out if any dimension is unspecified. If the rank is not specified, it returns
122    * false.
123    * \see https://developer.android.com/ndk/reference/struct/a-neural-networks-operand-type
124    * @note  base_loader set dim to -1 when there is unknown dim in input tensor
125    */
126   bool hasUnspecifiedDims() const
127   {
128     return (std::find(_dimensions.begin(), _dimensions.end(), UNSPECIFIED_DIM) !=
129             _dimensions.end());
130   }
131
132 private:
133   std::vector<int32_t> _dimensions;
134 };
135
136 inline bool operator==(const Shape &lhs, const Shape &rhs) { return lhs.dims() == rhs.dims(); }
137 inline bool operator!=(const Shape &lhs, const Shape &rhs) { return lhs.dims() != rhs.dims(); }
138
139 Shape permuteShape(const Shape &shape, Layout frontend_layout, Layout backend_layout);
140
141 /**
142 * @brief Find out if tha rank in this shape is "maybe" unspecified.
143 *        Note that when rank == 0, shape could represent scalar or unspecified rank
144 * \see https://developer.android.com/ndk/reference/struct/a-neural-networks-operand-type
145 */
146 inline bool rankMaybeUnspecified(const ir::Shape &shape) { return (shape.rank() == 0); }
147
148 } // namespace ir
149 } // namespace onert
150
151 #endif // __ONERT_IR_SHAPE_H__