1 /**
2 * Copyright 2021-2023 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/matmul_infer.h"
18 #include <math.h>
19 #include "nnacl/infer/infer_register.h"
20
21 #define MIN_SHAPE_SIZE 2
22
CheckMatmulInputShape(int * a_shape,size_t a_shape_size,int * b_shape,size_t b_shape_size,const int * bias_shape,size_t bias_shape_size,const MatMulParameter * param)23 int CheckMatmulInputShape(int *a_shape, size_t a_shape_size, int *b_shape, size_t b_shape_size, const int *bias_shape,
24 size_t bias_shape_size, const MatMulParameter *param) {
25 if (a_shape_size < MIN_SHAPE_SIZE || b_shape_size < MIN_SHAPE_SIZE) {
26 return NNACL_PARAM_INVALID;
27 }
28 for (size_t i = 0; i < (a_shape_size - 2) && i < (b_shape_size - 2); ++i) {
29 int min_value = MSMIN(a_shape[i], b_shape[i]);
30 int max_value = MSMAX(a_shape[i], b_shape[i]);
31 if (min_value != 0 && max_value % min_value != 0) {
32 NNACL_LOG_ERROR("min_value != 0 && max_value is not multiple of min_value");
33 return NNACL_INPUT_TENSOR_ERROR;
34 }
35 }
36 if (param->a_transpose_) {
37 iswap(&a_shape[a_shape_size - 1], &a_shape[a_shape_size - DIMENSION_2D]);
38 }
39 if (param->b_transpose_) {
40 iswap(&b_shape[b_shape_size - 1], &b_shape[b_shape_size - 2]);
41 }
42 if (bias_shape_size == DIMENSION_1D && bias_shape[0] != b_shape[b_shape_size - 1]) {
43 NNACL_LOG_ERROR("bias_shape_size == DIMENSION_1D && bias_shape[0] != b_shape[b_shape_size - 1]");
44 return NNACL_ERR;
45 }
46 if (a_shape[a_shape_size - 1] != b_shape[b_shape_size - 2]) {
47 NNACL_LOG_ERROR("a_shape[a_shape_size - 1] != b_shape[b_shape_size - 2]");
48 return NNACL_ERR;
49 }
50 return NNACL_OK;
51 }
52
CheckMatMulBias(int * shape,size_t dim_size)53 int CheckMatMulBias(int *shape, size_t dim_size) {
54 if (dim_size > 1) {
55 for (size_t i = 0; i < dim_size - 1; i++) {
56 if (shape[i] != DIMENSION_1D) {
57 NNACL_LOG_ERROR("shape[%zu] != DIMENSION_1D", i);
58 return NNACL_ERR;
59 }
60 }
61 }
62 return NNACL_OK;
63 }
64
SetShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)65 int SetShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
66 OpParameter *parameter) {
67 TensorC *input0 = (TensorC *)inputs[0];
68 TensorC *input1 = (TensorC *)inputs[1];
69 TensorC *output = outputs[0];
70 MatMulParameter *param = (MatMulParameter *)parameter;
71 int a_shape[MAX_SHAPE_SIZE] = {0};
72 size_t a_shape_size = 0;
73 ShapeSet(a_shape, &a_shape_size, input0->shape_, input0->shape_size_);
74 int b_shape[MAX_SHAPE_SIZE] = {0};
75 size_t b_shape_size = 0;
76 ShapeSet(b_shape, &b_shape_size, input1->shape_, input1->shape_size_);
77 int *shape_align = a_shape_size > b_shape_size ? b_shape : a_shape;
78 size_t *shape_size_align = a_shape_size > b_shape_size ? &b_shape_size : &a_shape_size;
79 int diff = abs((int)a_shape_size - (int)b_shape_size);
80 for (int i = 0; i < diff; ++i) {
81 ShapeInsert(shape_align, shape_size_align, 0, 1);
82 }
83 int bias_shape[MAX_AXIS_SIZE] = {0};
84 size_t bias_shape_size = 0;
85 if (inputs_size == kInputSize2) {
86 TensorC *bias = (TensorC *)inputs[2];
87 ShapeSet(bias_shape, &bias_shape_size, bias->shape_, bias->shape_size_);
88 NNACL_CHECK_TRUE_RET(CheckMatMulBias(bias_shape, bias_shape_size) == NNACL_OK, NNACL_ERR);
89 }
90
91 bool del_start = false;
92 bool del_end = false;
93 if (a_shape_size == 1) {
94 int insert_ret = ShapeInsert(a_shape, &a_shape_size, 0, 1);
95 if (insert_ret != NNACL_OK) {
96 NNACL_LOG_ERROR("ShapeInsert failed,insert_ret != NNACL_OK");
97 return NNACL_ERR;
98 }
99 del_start = true;
100 }
101 if (b_shape_size == 1) {
102 ShapePush(b_shape, &b_shape_size, 1);
103 del_end = true;
104 }
105 int ret = CheckMatmulInputShape(a_shape, a_shape_size, b_shape, b_shape_size, bias_shape, bias_shape_size, param);
106 if (ret != NNACL_OK) {
107 NNACL_LOG_ERROR("CheckMatmulInputShape failed,ret != NNACL_OK");
108 return NNACL_ERR;
109 }
110 int c_shape[MAX_SHAPE_SIZE];
111 size_t c_shape_size = 0;
112 ShapeSet(c_shape, &c_shape_size, a_shape, a_shape_size);
113 c_shape[c_shape_size - 1] = b_shape[b_shape_size - 1];
114 if (del_start) {
115 int erase_ret = ShapeErase(c_shape, &c_shape_size, 0);
116 if (erase_ret != NNACL_OK) {
117 NNACL_LOG_ERROR("ShapeErase failed,erase_ret != NNACL_OK");
118 return NNACL_ERR;
119 }
120 }
121 if (del_end) {
122 c_shape_size--;
123 }
124
125 for (size_t i = 0; i < (a_shape_size - 2) && i < (b_shape_size - 2); ++i) {
126 c_shape[i] = MSMAX(a_shape[i], b_shape[i]);
127 }
128
129 SetShapeArray(output, c_shape, c_shape_size);
130 return NNACL_OK;
131 }
132
MatmulInferShape(const TensorC * const * inputs,size_t inputs_size,TensorC ** outputs,size_t outputs_size,OpParameter * parameter)133 int MatmulInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
134 OpParameter *parameter) {
135 int check_ret = CheckAugmentNullSizeInputTwo(inputs, inputs_size, outputs, outputs_size, parameter, 2, 3, 1);
136 if (check_ret != NNACL_OK) {
137 return check_ret;
138 }
139
140 TensorC *input0 = (TensorC *)inputs[0];
141 TensorC *input1 = (TensorC *)inputs[1];
142 TensorC *output = outputs[0];
143
144 TensorC *input = input1->data_ == NULL ? input1 : input0; // transfer the input which comes from the other node.
145 SetDataTypeFormat(output, input);
146 if (input->data_type_ == kNumberTypeInt8 && parameter->quant_type_ == Quant_QuantDynamic) {
147 output->data_type_ = kNumberTypeFloat32;
148 }
149 if (!InferFlag(inputs, inputs_size)) {
150 return NNACL_INFER_INVALID;
151 }
152 return SetShape(inputs, inputs_size, outputs, outputs_size, parameter);
153 }
154
155 REG_INFER(MatMul, PrimType_MatMulFusion, MatmulInferShape)
156