Imported Upstream version 1.9.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / util / ShapeInference.h
1 /*
2  * Copyright (c) 2019 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_GRAPH_SHAPE_INFERENCE_H__
18 #define __ONERT_GRAPH_SHAPE_INFERENCE_H__
19
20 #include "Utils.h"
21
22 #include "ir/operation/Concat.h"
23 #include "ir/operation/Conv2D.h"
24 #include "ir/operation/DepthwiseConv2D.h"
25 #include "ir/operation/Pool2D.h"
26 #include "ir/operation/Reshape.h"
27 #include "ir/operation/StridedSlice.h"
28 #include "compiler/LoweredGraph.h"
29 #include "ir/Index.h"
30 #include "ir/Layout.h"
31 #include "ir/OperationVisitor.h"
32 #include "backend/IDynamicTensorManager.h"
33 #include "backend/ITensor.h"
34 #include "backend/ITensorRegistry.h"
35
36 namespace onert
37 {
38 namespace shape_inference
39 {
40
41 using Shapes = std::vector<ir::Shape>;
42
43 // Define shape calculation for operations. List them in alphabetic order.
44
45 ir::Shape inferArgMaxShape(const ir::Shape &input_shape, int axis, int rank);
46
47 ir::Shape inferBatchMatMulShape(const ir::Shape &lhs_shape, const ir::Shape &rhs_shape,
48                                 const ir::operation::BatchMatMul::Param &param);
49
50 ir::Shape inferBroadcastToShape(const ir::Shape wshape, const int32_t *shape_buffer);
51
52 ir::Shape inferConcatShape(const Shapes &in_shapes, const ir::operation::Concat::Param &param);
53
54 ir::Shape inferConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
55                            const ir::operation::Conv2D::Param &param,
56                            ir::Layout layout = ir::Layout::NHWC);
57
58 ir::Shape inferDepthwiseConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
59                                     const ir::operation::DepthwiseConv2D::Param &param,
60                                     ir::Layout layout = ir::Layout::NHWC);
61
62 ir::Shape inferEltwiseShape(const ir::Shape &lhs_shape, const ir::Shape &rhs_shape);
63
64 ir::Shape inferExpandDimsShape(const ir::Shape &in_shape, int32_t axis);
65
66 ir::Shape inferFillShape(const ir::Shape &in_shape, const int32_t *buf);
67
68 ir::Shape inferFullyConnectedShape(const ir::Shape &in_shape, const ir::Shape &ker_shape);
69
70 ir::Shape inferGatherShape(const ir::Shape &input_shape, const ir::Shape &indices_shape, int axis,
71                            int rank);
72
73 ir::Shape inferOnehotShape(const ir::Shape &input_shape, const int depth, int axis);
74
75 ir::Shape inferPackShape(const ir::Shape &input_shape, int axis, int rank, int num);
76
77 ir::Shape inferPadShape(const ir::Shape &in_shape, const int32_t *pad_buf, const size_t num_pads);
78
79 ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param &param,
80                          ir::Layout layout = ir::Layout::NHWC);
81
82 template <typename T> ir::Shape inferRangeShape(T start_val, T limit_val, T delta_val);
83
84 ir::Shape inferReshapeShape(const int32_t *shape_buf, const int32_t shape_num_elements,
85                             const size_t total_num_elements);
86
87 ir::Shape inferReduceShape(const ir::Shape &input_shape, const std::vector<int> &axes,
88                            bool keep_dims);
89
90 template <float *> ir::Shape inferRangeShape(float *start_val, float *limit_val, float *delta_val);
91
92 template <typename T> ir::Shape inferRangeShape(T start_val, T limit_val, T delta_val);
93
94 ir::Shape inferResizeBilinearShape(const ir::Shape &in_shape, const int32_t output_height,
95                                    const int32_t output_width);
96
97 ir::Shape inferSelectShape(const ir::Shape &input_cond_shape, const ir::Shape &input_true_shape,
98                            const ir::Shape &input_false_shape);
99
100 ir::Shape inferSliceShape(const ir::Shape &input_shape, const int32_t *begins,
101                           const int32_t *sizes);
102
103 ir::Shape inferSpaceToBatchNDShape(const ir::Shape &input_shape, const ir::Shape &block_shape_shape,
104                                    const ir::Shape &padding_shape, const int32_t *block_shape_data,
105                                    const int32_t *padding_data);
106
107 ir::Shape inferSplitShape(const ir::Shape input_shape, int axis_value, int num_splits);
108
109 ir::Shape inferSqueezeShape(const ir::Shape &in_shape, const ir::operation::Squeeze::Param &param);
110
111 struct StridedSliceParams
112 {
113   int8_t start_indices_count;
114   int16_t start_indices[4];
115   int8_t stop_indices_count;
116   int16_t stop_indices[4];
117   int8_t strides_count;
118   int16_t strides[4];
119
120   int16_t begin_mask;
121   int16_t ellipsis_mask;
122   int16_t end_mask;
123   int16_t new_axis_mask;
124   int16_t shrink_axis_mask;
125 };
126
127 template <typename T>
128 StridedSliceParams buildStridedSliceParams(const T *begin, const T *end, const T *strides,
129                                            const uint32_t begin_mask, const uint32_t end_mask,
130                                            const uint32_t shrink_axis_mask, const uint8_t rank);
131
132 ir::Shape inferStridedSliceShape(const ir::Shape &input_shape, const StridedSliceParams &op_params,
133                                  uint32_t rank);
134
135 ir::Shape inferTileShape(const ir::Shape &in_shape, const int32_t *multiplier);
136
137 ir::Shape inferTransposeShape(const ir::Shape &in_shape, const std::vector<int> &perm);
138
139 ir::Shape inferUnpackShape(const ir::Shape &input_shape, int axis, int rank);
140
141 } // namespace shape_inference
142 } // namespace onert
143
144 #endif // __ONERT_GRAPH_SHAPE_INFERENCE_H__