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 "src/inner_kernel.h"
18 #include <algorithm>
19 #include "src/tensor.h"
20 #include "src/common/utils.h"
21 #include "src/runtime/infer_manager.h"
22
23 namespace mindspore::kernel {
24 using mindspore::lite::RET_ERROR;
25 using mindspore::lite::RET_OK;
26
AllocWorkspace()27 void InnerKernel::AllocWorkspace() {
28 workspace_ = malloc(workspace_size());
29 if (workspace_ == nullptr) {
30 MS_LOG(ERROR) << "fail to alloc " << workspace_size() << "in kernel" << name();
31 return;
32 }
33 ws_allocated_ = true;
34 }
35
FreeWorkspace()36 void InnerKernel::FreeWorkspace() {
37 if (ws_allocated_) {
38 free(workspace_);
39 }
40 workspace_ = nullptr;
41 ws_allocated_ = false;
42 }
43
PreProcess()44 int InnerKernel::PreProcess() {
45 if (!InferShapeDone()) {
46 auto ret = lite::KernelInferShape(in_tensors_, out_tensors_, op_parameter_);
47 if (ret != 0) {
48 MS_LOG(ERROR) << "InferShape fail!";
49 return ret;
50 }
51 ret = ReSize();
52 if (ret != 0) {
53 MS_LOG(ERROR) << "ReSize fail!ret: " << ret;
54 return ret;
55 }
56 }
57
58 for (auto *output : this->out_tensors()) {
59 MS_ASSERT(output != nullptr);
60 if (registry_data_type_ == kNumberTypeFloat16 && output->data_type() == kNumberTypeFloat32) {
61 output->set_data_type(kNumberTypeFloat16);
62 }
63 auto ret = output->MallocData();
64 if (ret != RET_OK) {
65 MS_LOG(ERROR) << "MallocData failed";
66 return ret;
67 }
68 output->ResetRefCount();
69 }
70 return RET_OK;
71 }
72
Execute()73 int InnerKernel::Execute() {
74 auto ret = PreProcess();
75 if (lite::RET_OK != ret) {
76 MS_LOG(ERROR) << "run kernel PreProcess failed, name: " << this->name();
77 return ret;
78 }
79
80 if (op_parameter_->is_zero_shape_ == false) {
81 ret = Run();
82 if (lite::RET_OK != ret) {
83 MS_LOG(ERROR) << "run kernel failed, name: " << this->name();
84 return ret;
85 }
86 }
87
88 ret = PostProcess();
89 if (lite::RET_OK != ret) {
90 MS_LOG(ERROR) << "run kernel PostProcess failed, name: " << this->name();
91 return ret;
92 }
93 return lite::RET_OK;
94 }
95 } // namespace mindspore::kernel
96