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/conv2d_grad_input_infer.h"
18 #include "nnacl/infer/infer_register.h"
19
Conv2dGradInputInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)20 int Conv2dGradInputInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
21 OpParameter *parameter) {
22 int ret = CheckAugmentNull(inputs, inputs_size, outputs, outputs_size, parameter);
23 if (ret != NNACL_OK) {
24 return ret;
25 }
26 if (inputs_size < 3 || outputs_size != 1) {
27 return NNACL_ERR;
28 }
29 const TensorC *in0 = inputs[0];
30 TensorC *out = outputs[0];
31
32 if (in0 == NULL || out == NULL) {
33 return NNACL_NULL_PTR;
34 }
35 if (in0->format_ != Format_NHWC) {
36 return NNACL_FORMAT_ERROR;
37 }
38 SetDataTypeFormat(out, in0);
39
40 if (inputs[THIRD_INPUT]->shape_size_ < 1 || inputs[THIRD_INPUT]->data_ == NULL) {
41 return NNACL_ERR;
42 }
43 size_t data_size = (size_t)inputs[2]->shape_[0];
44 if (data_size != 4) {
45 return NNACL_ERR;
46 }
47
48 int shape[MAX_SHAPE_SIZE];
49 if (inputs[THIRD_INPUT]->format_ == Format_NCHW || inputs[THIRD_INPUT]->format_ == Format_KCHW) {
50 const int nchw2nhwc[4] = {kNCHW_N, kNCHW_H, kNCHW_W, kNCHW_C};
51 for (size_t i = 0; i < data_size; i++) {
52 shape[i] = *((int *)(inputs[THIRD_INPUT]->data_) + nchw2nhwc[i]);
53 }
54 } else if (inputs[THIRD_INPUT]->format_ == Format_NHWC || inputs[THIRD_INPUT]->format_ == Format_KHWC) {
55 memcpy(shape, inputs[THIRD_INPUT]->data_, data_size * sizeof(int));
56 } else {
57 return NNACL_ERR;
58 }
59 SetShapeArray(out, shape, data_size);
60 return NNACL_OK;
61 }
62
63 REG_INFER(Conv2DBackpropInputFusion, PrimType_Conv2DBackpropInputFusion, Conv2dGradInputInferShape)
64