• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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/nnacl_kernel.h"
18 #include "nnacl/cxx_utils.h"
19 #include "src/tensor.h"
20 #include "include/errorcode.h"
21 #include "nnacl/errorcode.h"
22 
23 using mindspore::lite::RET_ERROR;
24 using mindspore::lite::RET_OK;
25 namespace mindspore::nnacl {
~NNACLKernel()26 NNACLKernel::~NNACLKernel() {
27   if (in_ != nullptr) {
28     free(in_);
29     in_ = nullptr;
30   }
31   if (out_ != nullptr) {
32     free(out_);
33     out_ = nullptr;
34   }
35 
36   if (kernel_ != nullptr) {
37     kernel_->Release(kernel_);
38 
39     free(kernel_);
40     kernel_ = nullptr;
41   }
42 }
43 
Prepare()44 int NNACLKernel::Prepare() {
45   if (kernel_ == nullptr) {
46     return RET_ERROR;
47   }
48 
49   int ret = kernel_->Prepare(kernel_);
50   if (ret != RET_OK) {
51     MS_LOG(ERROR) << "NNACL prepare failed. Kernel: " << name() << ", ret: " << ret;
52     MS_LOG(ERROR) << NNACLErrorMsg(ret);
53     return ret;
54   }
55 
56   if (!InferShapeDone()) {
57     return RET_OK;
58   }
59   return ReSize();
60 }
61 
ReSize()62 int NNACLKernel::ReSize() {
63   if (kernel_ == nullptr) {
64     return RET_ERROR;
65   }
66   UpdateTensorC();
67 
68   int ret = kernel_->Resize(kernel_);
69   if (ret != RET_OK) {
70     MS_LOG(ERROR) << "NNACL resize failed. Kernel: " << name() << ", ret: " << ret;
71     MS_LOG(ERROR) << NNACLErrorMsg(ret);
72     return ret;
73   }
74   return RET_OK;
75 }
76 
Run()77 int NNACLKernel::Run() {
78   if (kernel_ == nullptr) {
79     return RET_ERROR;
80   }
81   UpdateTensorC();
82   kernel_->workspace_ = workspace();
83 
84   int ret = kernel_->Compute(kernel_);
85   if (ret != RET_OK) {
86     MS_LOG(WARNING) << "NNACL compute failed. Kernel: " << name() << ", ret: " << ret;
87     MS_LOG(WARNING) << NNACLErrorMsg(ret);
88     return ret;
89   }
90   return RET_OK;
91 }
92 
UpdateTensorC()93 void NNACLKernel::UpdateTensorC() {
94   for (size_t i = 0; i < in_size_; i++) {
95     in_[i] = in_tensors().at(i)->ConvertToTensorC();
96   }
97   for (size_t i = 0; i < out_size_; i++) {
98     out_[i] = out_tensors().at(i)->ConvertToTensorC();
99   }
100 }
101 
OptimizeDataCopy()102 int NNACLKernel::OptimizeDataCopy() {
103   auto input_tensor = in_tensors().front();
104   CHECK_NULL_RETURN(input_tensor);
105   CHECK_NULL_RETURN(input_tensor->data());
106   auto output_tensor = out_tensors().front();
107   CHECK_NULL_RETURN(output_tensor);
108   CHECK_NULL_RETURN(output_tensor->data());
109 
110   if (input_tensor->allocator() == nullptr || input_tensor->allocator() != output_tensor->allocator() ||
111       input_tensor->allocator() != ms_context_->allocator || /* runtime allocator */
112       op_parameter_->is_train_session_ || !output_tensor->own_data()) {
113     return NNACLKernel::Run();
114   }
115 
116   output_tensor->FreeData();
117   output_tensor->ResetRefCount();
118   output_tensor->set_data(input_tensor->data());
119   if (input_tensor->IsConst()) {
120     output_tensor->set_own_data(false);
121   } else {
122     output_tensor->set_own_data(input_tensor->own_data());
123   }
124   return RET_OK;
125 }
126 
NNACLCheckArgs()127 int NNACLKernel::NNACLCheckArgs() {
128   if (op_parameter_ == nullptr) {
129     MS_LOG(ERROR) << "NNACL check failed. Invalid parameter.";
130     return RET_ERROR;
131   }
132 
133   if (in_size_ == 0 || in_size_ * sizeof(TensorC *) > MAX_MALLOC_SIZE) {
134     MS_LOG(ERROR) << "NNACL check failed. Invalid input size: " << in_size_;
135     return RET_ERROR;
136   }
137 
138   if (out_size_ == 0 || out_size_ * sizeof(TensorC *) > MAX_MALLOC_SIZE) {
139     MS_LOG(ERROR) << "NNACL check failed. Invalid output size: " << out_size_;
140     return RET_ERROR;
141   }
142 
143   if (op_parameter_->thread_num_ <= 0 || op_parameter_->thread_num_ > MAX_THREAD_NUM) {
144     MS_LOG(ERROR) << "NNACL check failed. Invalid thread number: " << op_parameter_->thread_num_;
145     return RET_ERROR;
146   }
147 
148   return RET_OK;
149 }
150 
InitKernel(const TypeId & data_type,const lite::InnerContext * ctx)151 int NNACLKernel::InitKernel(const TypeId &data_type, const lite::InnerContext *ctx) {
152   CHECK_NULL_RETURN(ctx);
153 
154   in_size_ = in_tensors_.size();
155   out_size_ = out_tensors_.size();
156 
157   int ret = NNACLCheckArgs();
158   if (ret != RET_OK) {
159     MS_LOG(ERROR) << "NNACL check args failed. Kernel: " << name();
160     return ret;
161   }
162 
163   in_ = reinterpret_cast<TensorC **>(malloc(in_size_ * sizeof(TensorC *)));
164   if (in_ == nullptr) {
165     return RET_ERROR;
166   }
167 
168   out_ = reinterpret_cast<TensorC **>(malloc(out_size_ * sizeof(TensorC *)));
169   if (out_ == nullptr) {
170     return RET_ERROR;
171   }
172 
173   UpdateTensorC();
174   kernel_ = CreateKernel(op_parameter_, in_, in_size_, out_, out_size_, data_type, exec_env_);
175   if (kernel_ == nullptr) {
176     MS_LOG(WARNING) << "NNACL create kernel failed. Kernel: " << name();
177     return RET_ERROR;
178   }
179   kernel_->UpdateThread = DefaultUpdateThreadNumPass;
180   return RET_OK;
181 }
182 }  // namespace mindspore::nnacl
183