2 * Copyright (c) 2019 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_GRAPH_SHAPE_INFERENCE_H__
18 #define __ONERT_GRAPH_SHAPE_INFERENCE_H__
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"
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"
38 namespace shape_inference
41 using Shapes = std::vector<ir::Shape>;
43 // Define shape calculation for operations. List them in alphabetic order.
45 ir::Shape inferArgMaxShape(const ir::Shape &input_shape, int axis, int rank);
47 ir::Shape inferBatchMatMulShape(const ir::Shape &lhs_shape, const ir::Shape &rhs_shape,
48 const ir::operation::BatchMatMul::Param ¶m);
50 ir::Shape inferBCQFullyConnectedShape(const ir::Shape &in_shape, const ir::Shape &cluster_shape,
51 const int32_t *cluster_buf);
53 ir::Shape inferBCQGatherShape(const ir::Shape &indices_shape, const ir::Shape &cluster_shape,
54 const int32_t *cluster_buf, int rank,
55 const ir::operation::BCQGather::Param ¶m);
57 ir::Shape inferBroadcastToShape(const ir::Shape shp_shape, const int32_t *shp_buf);
59 ir::Shape inferConcatShape(const Shapes &in_shapes, const ir::operation::Concat::Param ¶m);
61 ir::Shape inferConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
62 const ir::operation::Conv2D::Param ¶m,
63 ir::Layout layout = ir::Layout::NHWC);
65 ir::Shape inferDepthwiseConv2DShape(const ir::Shape &in_shape, const ir::Shape &ker_shape,
66 const ir::operation::DepthwiseConv2D::Param ¶m,
67 ir::Layout layout = ir::Layout::NHWC);
69 ir::Shape inferEltwiseShape(const ir::Shape &lhs_shape, const ir::Shape &rhs_shape);
71 ir::Shape inferExpandDimsShape(const ir::Shape &in_shape, int32_t axis);
73 ir::Shape inferFillShape(const ir::Shape &in_shape, const int32_t *in_buf);
75 ir::Shape inferFullyConnectedShape(const ir::Shape &in_shape, const ir::Shape &ker_shape);
77 ir::Shape inferGatherShape(const ir::Shape &input_shape, const ir::Shape &indices_shape, int axis,
80 ir::Shape inferOnehotShape(const ir::Shape &input_shape, const int depth, int axis);
82 ir::Shape inferPackShape(const ir::Shape &input_shape, int axis, int rank, int num);
84 ir::Shape inferPadShape(const ir::Shape &in_shape, const int32_t *pad_buf, const size_t num_pads);
86 ir::Shape inferPoolShape(const ir::Shape &in_shape, const ir::operation::Pool2D::Param ¶m,
87 ir::Layout layout = ir::Layout::NHWC);
89 template <typename T> ir::Shape inferRangeShape(T start_val, T limit_val, T delta_val);
91 ir::Shape inferReshapeShape(const int32_t *shape_buf, const int32_t shape_num_elements,
92 const size_t total_num_elements);
94 ir::Shape inferReduceShape(const ir::Shape &input_shape, const std::vector<int> &axes,
97 template <float *> ir::Shape inferRangeShape(float *start_val, float *limit_val, float *delta_val);
99 template <typename T> ir::Shape inferRangeShape(T start_val, T limit_val, T delta_val);
101 ir::Shape inferResizeBilinearShape(const ir::Shape &in_shape, const int32_t output_height,
102 const int32_t output_width);
104 ir::Shape inferSelectShape(const ir::Shape &input_cond_shape, const ir::Shape &input_true_shape,
105 const ir::Shape &input_false_shape);
107 ir::Shape inferSliceShape(const ir::Shape &input_shape, const int32_t *begins_buf,
108 const int32_t *sizes_buf);
110 ir::Shape inferSpaceToBatchNDShape(const ir::Shape &input_shape, const ir::Shape &block_shape_shape,
111 const ir::Shape &padding_shape, const int32_t *block_shape_buf,
112 const int32_t *padding_buf);
114 ir::Shape inferSplitShape(const ir::Shape input_shape, int axis_value, int num_splits);
116 ir::Shape inferSqueezeShape(const ir::Shape &in_shape, const ir::operation::Squeeze::Param ¶m);
118 struct StridedSliceParams
120 int8_t start_indices_count;
121 int16_t start_indices[4];
122 int8_t stop_indices_count;
123 int16_t stop_indices[4];
124 int8_t strides_count;
128 int16_t ellipsis_mask;
130 int16_t new_axis_mask;
131 int16_t shrink_axis_mask;
134 template <typename T>
135 StridedSliceParams buildStridedSliceParams(const T *begin, const T *end, const T *strides,
136 const uint32_t begin_mask, const uint32_t end_mask,
137 const uint32_t shrink_axis_mask, const uint8_t rank);
139 ir::Shape inferStridedSliceShape(const ir::Shape &input_shape, const StridedSliceParams &op_params,
142 ir::Shape inferTileShape(const ir::Shape &in_shape, const int32_t *multiplier_buf,
143 const int32_t multiplier_size);
145 ir::Shape inferTransposeShape(const ir::Shape &in_shape, const int32_t *perm_buf,
148 ir::Shape inferUnpackShape(const ir::Shape &input_shape, int axis, int rank);
150 } // namespace shape_inference
153 #endif // __ONERT_GRAPH_SHAPE_INFERENCE_H__