• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/space_to_batch_infer.h"
18 #include "nnacl/infer/infer_register.h"
19 
SpaceToBatchInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)20 int SpaceToBatchInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
21                            OpParameter *parameter) {
22   int check_ret = CheckAugmentNullSize(inputs, inputs_size, outputs, outputs_size, parameter, 1, 1);
23   if (check_ret != NNACL_OK) {
24     return check_ret;
25   }
26 
27   const TensorC *input = inputs[0];
28   if (input->format_ != Format_NHWC) {
29     return NNACL_FORMAT_ERROR;
30   }
31   SetDataTypeFormat(outputs[0], input);
32   SpaceToBatchParameter *param = (SpaceToBatchParameter *)parameter;
33   if (!InferFlag(inputs, inputs_size)) {
34     return NNACL_INFER_INVALID;
35   }
36   if (input->shape_size_ != 4) {
37     return NNACL_ERR;
38   }
39 
40   int *block_shape = param->block_sizes_;
41   int block_shape_size = param->m_;
42   int *paddings = param->paddings_;
43   int padding_left = 0;
44   int padding_right = 0;
45   int block_w = 1;
46   if (block_shape_size == 2) {
47     padding_left = paddings[2];
48     padding_right = paddings[3];
49     block_w = block_shape[1];
50   }
51 
52   NNACL_CHECK_ZERO_RETURN_ERR(block_shape[0]);
53   NNACL_CHECK_ZERO_RETURN_ERR(block_w);
54   NNACL_CHECK_INT_MUL_NOT_OVERFLOW(block_shape[0], block_w, NNACL_ERR);
55   NNACL_CHECK_INT_MUL_NOT_OVERFLOW(input->shape_[kNHWC_N], block_shape[0] * block_w, NNACL_ERR);
56   outputs[0]->shape_[kNHWC_N] = input->shape_[kNHWC_N] * (block_shape[0] * block_w);
57   outputs[0]->shape_[kNHWC_H] = (input->shape_[kNHWC_H] + paddings[0] + paddings[1]) / block_shape[0];
58   outputs[0]->shape_[kNHWC_W] = (input->shape_[kNHWC_W] + padding_left + padding_right) / block_w;
59   outputs[0]->shape_[kNHWC_C] = input->shape_[kNHWC_C];
60   outputs[0]->shape_size_ = input->shape_size_;
61   return NNACL_OK;
62 }
63 
64 REG_INFER(SpaceToBatch, PrimType_SpaceToBatch, SpaceToBatchInferShape)
65