1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
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 #include "nnacl/infer/split_with_over_lap_infer.h"
18 #include "nnacl/infer/infer_register.h"
19 #include "nnacl/op_base.h"
20
SplitWithOverlapInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)21 int SplitWithOverlapInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
22 OpParameter *parameter) {
23 int check_ret = CheckAugmentWithMinSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
24 if (check_ret != NNACL_OK) {
25 return check_ret;
26 }
27
28 if (!InferFlag(inputs, inputs_size)) {
29 return NNACL_INFER_INVALID;
30 }
31 const TensorC *input = inputs[0];
32 SplitWithOverlapParameter *param = (SplitWithOverlapParameter *)parameter;
33
34 int split_dim = param->split_dim_;
35 int number_split = param->num_split_;
36 if (outputs_size != (size_t)number_split) {
37 return NNACL_ERR;
38 }
39
40 int ratio[SPLIT_MAX_SLICE_NUM];
41 int extend_top[SPLIT_MAX_SLICE_NUM];
42 int extend_bottom[SPLIT_MAX_SLICE_NUM];
43 for (int i = 0; i < number_split; ++i) {
44 ratio[i] = param->ratio_[i];
45 extend_top[i] = param->extend_top_[i];
46 extend_bottom[i] = param->extend_bottom_[i];
47 }
48
49 const int *input_shape = input->shape_;
50 int split_dim_size = input_shape[split_dim];
51 int total_block_count = 0;
52 for (int i = 0; i < number_split; i++) {
53 total_block_count += ratio[i];
54 }
55
56 int borders[MAX_SHAPE_SIZE];
57 borders[0] = 0;
58 int visited_block = 0;
59 for (int i = 0; i < number_split - 1; i++) {
60 visited_block += ratio[i];
61 NNACL_CHECK_FALSE(INT_MUL_OVERFLOW(split_dim_size, visited_block) || total_block_count == 0, NNACL_ERR);
62 int cur_border = UP_DIV(split_dim_size * visited_block, total_block_count);
63 borders[i + 1] = cur_border;
64 }
65 borders[number_split] = split_dim_size;
66
67 for (int i = 0; i < number_split; ++i) {
68 int output_shape[MAX_SHAPE_SIZE];
69 for (int dim = 0; dim < input->shape_size_; dim++) {
70 if (dim == split_dim) {
71 int splited_size = borders[i + 1] - borders[i];
72 splited_size += (extend_top[i] + extend_bottom[i]);
73 output_shape[dim] = splited_size;
74 } else {
75 output_shape[dim] = input_shape[dim];
76 }
77 }
78 SetShapeArray(outputs[i], output_shape, input->shape_size_);
79 SetDataTypeFormat(outputs[i], input);
80 }
81 return NNACL_OK;
82 }
83
84 REG_INFER(SplitWithOverlap, PrimType_SplitWithOverlap, SplitWithOverlapInferShape)
85