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