2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_IR_SHAPE_H__
18 #define __ONERT_IR_SHAPE_H__
20 #include "ir/Layout.h"
33 * @brief Structure to have values of dimensions for feature
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 */
43 * @brief Construct FeatureShape object using default constrcutor
45 FeatureShape() = default;
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
52 FeatureShape(int32_t depth, int32_t height, int32_t width) : N{1}, C{depth}, H{height}, W{width}
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
63 FeatureShape(int32_t batch, int32_t depth, int32_t height, int32_t width)
64 : N{batch}, C{depth}, H{height}, W{width}
73 static int32_t const UNSPECIFIED_DIM;
74 static int32_t const MAX_RANK;
78 explicit Shape(int rank) : _dimensions(rank) {}
80 Shape(std::initializer_list<int32_t> dimensions) : _dimensions(dimensions) {}
82 int rank() const { return _dimensions.size(); }
84 const std::vector<int32_t> &dims() const { return _dimensions; }
86 int32_t dim(int i) const
88 assert(rank() != 0 || i == 0);
89 return rank() == 0 ? 1 : _dimensions.at(i);
92 int32_t &dim(int i) { return _dimensions.at(i); }
95 * @brief Returns number of elements when rank or dim is specified
97 uint64_t num_elements() const;
100 FeatureShape asFeature(Layout layout) const;
103 * @brief Add dimension to the beginning
104 * @param[in] d dimension to add to the beginning
106 void prepend(int32_t d) { _dimensions.insert(_dimensions.cbegin(), d); }
109 * @brief Add dimension to the end
110 * @param[in] d dimension to add to the end
112 void append(int32_t d) { _dimensions.emplace_back(d); }
115 * @brief Extend rank of Shape object for operand with param.
116 * @param[in] to_rank The rank value to be extended to
118 void extendRank(int to_rank);
121 * @brief Find out if any dimension is unspecified. If the rank is not specified, it returns
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
126 bool hasUnspecifiedDims() const
128 return (std::find(_dimensions.begin(), _dimensions.end(), UNSPECIFIED_DIM) !=
133 std::vector<int32_t> _dimensions;
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(); }
139 Shape permuteShape(const Shape &shape, Layout frontend_layout, Layout backend_layout);
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
146 inline bool rankMaybeUnspecified(const ir::Shape &shape) { return (shape.rank() == 0); }
151 #endif // __ONERT_IR_SHAPE_H__