1 /**
2 * Copyright 2022 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 #include <include/errorcode.h>
17 #include "nnrt_model_kernel.h"
Prepare()18 int mindspore::NNRTModelKernel::Prepare() { return 0; }
Execute()19 int mindspore::NNRTModelKernel::Execute() {
20 lite::STATUS ret_val = PrepareInputs();
21 if (ret_val != lite::RET_OK) {
22 MS_LOG(ERROR) << "NNRTModelKernel PrepareInputs failed, STATUS is " << ret_val;
23 return ret_val;
24 }
25 ret_val = TransferOutputs();
26 if (ret_val != lite::RET_OK) {
27 MS_LOG(ERROR) << "NNRTModelKernel TransferOutputs failed, STATUS is " << ret_val;
28 return ret_val;
29 }
30 MS_LOG(INFO) << "Running NNRtModel Kernel...";
31 OH_NN_ReturnCode ret_code;
32 ret_code = OH_NNExecutor_Run(this->oh_nn_executor);
33
34 if (ret_code != OH_NN_SUCCESS) {
35 MS_LOG(ERROR) << "NNExecutor Run failed, OH_NN_ReturnCode = " << ret_code;
36 return lite::RET_ERROR;
37 }
38 MS_LOG(INFO) << "Run NNRtModel Kernel success.";
39
40 return lite::RET_OK;
41 }
42
ConvertDataType(mindspore::DataType data_type)43 OH_NN_DataType mindspore::NNRTModelKernel::ConvertDataType(mindspore::DataType data_type) {
44 OH_NN_DataType oh_data_type;
45 switch (data_type) {
46 case DataType::kTypeUnknown:
47 case DataType::kObjectTypeString:
48 case DataType::kObjectTypeList:
49 case DataType::kObjectTypeTuple:
50 case DataType::kObjectTypeTensorType:
51 case DataType::kNumberTypeBegin:
52 case DataType::kNumberTypeEnd:
53 case DataType::kInvalidType:
54 oh_data_type = OH_NN_UNKNOWN;
55 break;
56 case DataType::kNumberTypeBool:
57 oh_data_type = OH_NN_BOOL;
58 break;
59 case DataType::kNumberTypeInt8:
60 oh_data_type = OH_NN_INT8;
61 break;
62 case DataType::kNumberTypeInt16:
63 oh_data_type = OH_NN_INT16;
64 break;
65 case DataType::kNumberTypeInt32:
66 oh_data_type = OH_NN_INT32;
67 break;
68 case DataType::kNumberTypeInt64:
69 oh_data_type = OH_NN_INT64;
70 break;
71 case DataType::kNumberTypeUInt8:
72 oh_data_type = OH_NN_UINT8;
73 break;
74 case DataType::kNumberTypeUInt16:
75 oh_data_type = OH_NN_UINT16;
76 break;
77 case DataType::kNumberTypeUInt32:
78 oh_data_type = OH_NN_UINT32;
79 break;
80 case DataType::kNumberTypeUInt64:
81 oh_data_type = OH_NN_UINT64;
82 break;
83 case DataType::kNumberTypeFloat16:
84 oh_data_type = OH_NN_FLOAT16;
85 break;
86 case DataType::kNumberTypeFloat32:
87 oh_data_type = OH_NN_FLOAT32;
88 break;
89 case DataType::kNumberTypeFloat64:
90 oh_data_type = OH_NN_FLOAT64;
91 break;
92 default: {
93 oh_data_type = OH_NN_UNKNOWN;
94 }
95 }
96 return oh_data_type;
97 }
PrepareInputs()98 int mindspore::NNRTModelKernel::PrepareInputs() {
99 auto input_tensors = this->inputs();
100 for (int i = 0; i < input_tensors.size(); i++) {
101 auto tensor = input_tensors[i];
102 auto tensor_shape = tensor.Shape();
103 auto tmp_quant_param = tensor.QuantParams();
104 OH_NN_QuantParam *quant_param = nullptr;
105 std::vector<uint32_t> bit_num;
106 std::vector<double> scale;
107 std::vector<int32_t> zero_point;
108 if (!tmp_quant_param.empty()) {
109 quant_param = (new (std::nothrow) OH_NN_QuantParam);
110 if (quant_param == nullptr) {
111 MS_LOG(ERROR) << "new OH_NN_QuantParam failed.";
112 return lite::RET_NULL_PTR;
113 }
114 for (auto qparam : tmp_quant_param) {
115 bit_num.emplace_back(qparam.bit_num);
116 scale.emplace_back(qparam.scale);
117 zero_point.emplace_back(qparam.zero_point);
118 }
119 quant_param->quantCount = tmp_quant_param.size();
120 quant_param->numBits = bit_num.data();
121 quant_param->scale = scale.data();
122 quant_param->zeroPoint = zero_point.data();
123 }
124 auto oprend = new (std::nothrow) OH_NN_Tensor;
125 if (oprend == nullptr) {
126 MS_LOG(ERROR) << "new OH_NN_Tensor Failed";
127 return lite::RET_ERROR;
128 }
129 oprend->dataType = ConvertDataType(tensor.DataType());
130 oprend->dimensionCount = tensor_shape.size();
131
132 std::vector<int32_t> dimensions_list;
133 for (auto shape : tensor_shape) {
134 if (shape < INT32_MAX) {
135 dimensions_list.emplace_back(static_cast<int32_t>(shape));
136 } else {
137 MS_LOG(ERROR) << "NNExecutor SetInput failed,tensor dimension is is too large, max dim = " << INT32_MAX
138 << ", but get dimension = " << shape;
139 return lite::RET_ERROR;
140 }
141 }
142 oprend->dimensions = dimensions_list.data();
143 oprend->quantParam = quant_param;
144 oprend->type = OH_NN_TENSOR;
145 OH_NN_ReturnCode ret_code =
146 OH_NNExecutor_SetInput(oh_nn_executor, i, oprend, tensor.MutableData(), tensor.DataSize());
147 delete (oprend);
148
149 if (!tmp_quant_param.empty()) {
150 free(quant_param);
151 quant_param = nullptr;
152 }
153
154 if (ret_code != OH_NN_SUCCESS) {
155 MS_LOG(ERROR) << "NNExecutor SetInput failed, current input tensor is" << tensor.Name()
156 << "OH_NN_ReturnCode = " << ret_code;
157 return lite::RET_ERROR;
158 }
159 }
160
161 return lite::RET_OK;
162 }
TransferOutputs()163 int mindspore::NNRTModelKernel::TransferOutputs() {
164 auto output_tensors = this->outputs();
165 for (size_t i = 0; i < output_tensors.size(); i++) {
166 auto tensor = output_tensors[i];
167 OH_NN_ReturnCode ret_code = OH_NNExecutor_SetOutput(oh_nn_executor, i, tensor.MutableData(), tensor.DataSize());
168 if (ret_code != OH_NN_SUCCESS) {
169 MS_LOG(ERROR) << "NNExecutor SetOutput failed, current out tensor is" << tensor.Name()
170 << ", OH_NN_ReturnCode = " << ret_code;
171 return lite::RET_ERROR;
172 }
173 }
174 return lite::RET_OK;
175 }
176