• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 "src/runtime/kernel/arm/fp32/matmul_fp32.h"
18 #include "include/errorcode.h"
19 #include "nnacl/fp32/matmul_fp32.h"
20 #include "src/kernel_registry.h"
21 
22 using mindspore::lite::KernelRegistrar;
23 using mindspore::lite::RET_ERROR;
24 using mindspore::lite::RET_OK;
25 using mindspore::schema::PrimitiveType_MatMulFusion;
26 
27 namespace mindspore::kernel {
InitShapeA()28 void MatmulCPUKernel::InitShapeA() {
29   auto a_shape = in_tensors_[kInputIndex]->shape();
30   int batch = 1;
31   MS_CHECK_TRUE_RET_VOID(a_shape.size() >= 2);
32   for (size_t i = 0; i < a_shape.size() - 2; ++i) {
33     batch *= a_shape[i];
34   }
35   params_->batch = batch;
36   params_->row_ = params_->a_transpose_ ? a_shape[a_shape.size() - 1] : a_shape[a_shape.size() - 2];
37   params_->deep_ = params_->a_transpose_ ? a_shape[a_shape.size() - 2] : a_shape[a_shape.size() - 1];
38 }
39 
InitShapeB()40 void MatmulCPUKernel::InitShapeB() {
41   auto b_shape = in_tensors_[kWeightIndex]->shape();
42   int batch = 1;
43   MS_CHECK_TRUE_RET_VOID(b_shape.size() >= 2);
44   for (size_t i = 0; i < b_shape.size() - 2; ++i) {
45     batch *= b_shape[i];
46   }
47   params_->batch = batch;
48   params_->col_ = params_->b_transpose_ ? b_shape[b_shape.size() - 2] : b_shape[b_shape.size() - 1];
49   params_->deep_ = params_->b_transpose_ ? b_shape[b_shape.size() - 1] : b_shape[b_shape.size() - 2];
50 }
51 
Init()52 int MatmulCPUKernel::Init() {
53   CHECK_LESS_RETURN(in_tensors_.size(), C2NUM);
54   CHECK_LESS_RETURN(out_tensors_.size(), 1);
55   MatmulFp32BaseCPUKernel::InitParameter();
56 
57   if (params_->a_const_ == true) {
58     InitShapeA();
59   }
60 
61   if (params_->b_const_ == true) {
62     InitShapeB();
63   }
64 
65   auto ret = MatmulFp32BaseCPUKernel::Init();
66   if (ret != RET_OK) {
67     return ret;
68   }
69 
70   if (!InferShapeDone()) {
71     return RET_OK;
72   }
73   return ReSize();
74 }
75 
ReSize()76 int MatmulCPUKernel::ReSize() {
77   InitShapeA();
78   InitShapeB();
79 
80   return MatmulFp32BaseCPUKernel::ReSize();
81 }
82 
Run()83 int MatmulCPUKernel::Run() {
84   auto ret = MatmulFp32BaseCPUKernel::Run();
85   if (ret != RET_OK) {
86     MS_LOG(ERROR) << "MatmulFp32BaseCPUKernel failed!";
87   }
88   return ret;
89 }
90 
91 REG_KERNEL(kCPU, kNumberTypeFloat32, PrimitiveType_MatMulFusion, LiteKernelCreator<MatmulCPUKernel>)
92 }  // namespace mindspore::kernel
93