• 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/full_connection_infer.h"
18 #include "nnacl/infer/infer_register.h"
19 
FullConnectionInferPreJudge(const MatMulParameter * param,size_t inputs_size,const TensorC * input0)20 int FullConnectionInferPreJudge(const MatMulParameter *param, size_t inputs_size, const TensorC *input0) {
21   if ((param->has_bias_ && inputs_size != 3) || (!param->has_bias_ && inputs_size != 2)) {
22     return NNACL_INPUT_TENSOR_ERROR;
23   }
24   if (param->use_axis_ && (param->axis_ < 1 || param->axis_ > (int)(input0->shape_size_))) {
25     return NNACL_ERR;
26   }
27   return NNACL_OK;
28 }
29 
FullConnectionInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)30 int FullConnectionInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
31                              OpParameter *parameter) {
32   int check_ret = CheckAugmentWithMinSize(inputs, inputs_size, outputs, outputs_size, parameter, 2, 1);
33   if (check_ret != NNACL_OK) {
34     return check_ret;
35   }
36 
37   const TensorC *input0 = inputs[0];
38   const TensorC *input1 = inputs[1];
39   TensorC *output = outputs[0];
40   MatMulParameter *param = (MatMulParameter *)parameter;
41   SetDataTypeFormat(output, input0);
42   if (!InferFlag(inputs, inputs_size)) {
43     return NNACL_INFER_INVALID;
44   }
45   int pre_judge = FullConnectionInferPreJudge(param, inputs_size, input0);
46   if (pre_judge != NNACL_OK) {
47     return pre_judge;
48   }
49   int new_k = 1;
50   if (param->use_axis_) {
51     for (size_t i = (size_t)(param->axis_); i < input0->shape_size_; ++i) {
52       new_k *= input0->shape_[i];
53     }
54     if (new_k != input1->shape_[1]) {
55       return NNACL_INPUT_TENSOR_ERROR;
56     }
57   } else {
58     new_k = input1->shape_[1];
59   }
60   if (param->has_bias_) {
61     if (inputs[2]->shape_[0] != input1->shape_[0]) {
62       return NNACL_INPUT_TENSOR_ERROR;
63     }
64   }
65   if (inputs[0]->shape_size_ > MAX_SHAPE_SIZE) {
66     return NNACL_INPUT_TENSOR_ERROR;
67   }
68   int out_shape[MAX_SHAPE_SIZE];
69   size_t out_shape_size = 0;
70   ShapeSet(out_shape, &out_shape_size, inputs[0]->shape_, inputs[0]->shape_size_);
71   if (param->use_axis_) {
72     out_shape_size = (size_t)(param->axis_) + 1;
73     out_shape[param->axis_] = input1->shape_[0];
74   } else {
75     int total = 1;
76     for (size_t i = 0; i < input0->shape_size_; ++i) {
77       total *= input0->shape_[i];
78     }
79     out_shape_size = 2;
80     if (new_k == 0) {
81       return NNACL_ERR;
82     }
83     int batch_size = total / new_k;
84     out_shape[0] = batch_size;
85     out_shape[1] = input1->shape_[0];
86   }
87   SetShapeArray(output, out_shape, out_shape_size);
88 
89   return NNACL_OK;
90 }
91 
92 REG_INFER(FullConnection, PrimType_FullConnection, FullConnectionInferShape)
93