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