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
17 #include "src/litert/delegate/nnapi/nnapi_utils.h"
18 #include <algorithm>
19 #include <vector>
20 #include <utility>
21 #include <unordered_map>
22 #include "include/errorcode.h"
23 #include "src/common/log_adapter.h"
24 #include "nnacl/op_base.h"
25
26 namespace mindspore {
27 namespace lite {
28 static std::unordered_map<DataType, std::pair<OperandCode, OperandCode>> nnapi_data_type = {
29 {DataType::kNumberTypeBool, {ANEURALNETWORKS_BOOL, ANEURALNETWORKS_TENSOR_BOOL8}},
30 {DataType::kNumberTypeInt8, {ANEURALNETWORKS_BOOL, ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL}},
31 {DataType::kNumberTypeUInt8, {ANEURALNETWORKS_BOOL, ANEURALNETWORKS_TENSOR_QUANT8_ASYMM}},
32 {DataType::kNumberTypeInt32, {ANEURALNETWORKS_INT32, ANEURALNETWORKS_TENSOR_INT32}},
33 {DataType::kNumberTypeFloat16, {ANEURALNETWORKS_FLOAT16, ANEURALNETWORKS_TENSOR_FLOAT16}},
34 {DataType::kNumberTypeFloat32, {ANEURALNETWORKS_FLOAT32, ANEURALNETWORKS_TENSOR_FLOAT32}}};
35
ConverTensorQuantSymmToASymm(MSTensor * ms_tensor)36 void ConverTensorQuantSymmToASymm(MSTensor *ms_tensor) {
37 MS_ASSERT(ms_tensor != nullptr);
38 MS_CHECK_TRUE_RET_VOID(ms_tensor->DataType() == DataType::kNumberTypeInt8);
39 MS_CHECK_TRUE_RET_VOID(ms_tensor->QuantParams().size() == 1);
40 ms_tensor->SetDataType(DataType::kNumberTypeUInt8);
41 auto quant_param = ms_tensor->QuantParams().front();
42 quant_param.zero_point += 128;
43 ms_tensor->SetQuantParams({quant_param});
44 if (ms_tensor->IsConst()) {
45 auto data = ms_tensor->MutableData();
46 for (int idx = 0; idx < ms_tensor->ElementNum(); idx++) {
47 *(reinterpret_cast<uint8_t *>(data) + idx) = *(reinterpret_cast<int8_t *>(data) + idx) + 128;
48 }
49 }
50 }
51
AddNNAPIOperand(ANeuralNetworksModel * nnapi_model,MSTensor ms_tensor,int idx,int quant_channel_dim,bool is_scalar)52 int AddNNAPIOperand(ANeuralNetworksModel *nnapi_model, MSTensor ms_tensor, int idx, int quant_channel_dim,
53 bool is_scalar) {
54 MS_ASSERT(nnapi_model != nullptr);
55 ANeuralNetworksOperandType nnapi_tensor;
56 auto ms_data_type = ms_tensor.DataType();
57 if (nnapi_data_type.find(ms_data_type) == nnapi_data_type.end()) {
58 MS_LOG(ERROR) << "Unsupported data type: " << static_cast<int>(ms_data_type);
59 return RET_ERROR;
60 }
61 nnapi_tensor.type = is_scalar ? nnapi_data_type.at(ms_data_type).first : nnapi_data_type.at(ms_data_type).second;
62 nnapi_tensor.scale = 0.f; // These fields are used for quantized tensors
63 nnapi_tensor.zeroPoint = 0; // These fields are used for quantized tensors
64
65 ANeuralNetworksSymmPerChannelQuantParams quant_params;
66 std::vector<float> scales;
67 if (ms_tensor.QuantParams().size() == 1) {
68 nnapi_tensor.scale = ms_tensor.QuantParams().front().scale;
69 nnapi_tensor.zeroPoint = ms_tensor.QuantParams().front().zero_point;
70 } else {
71 quant_params.channelDim = quant_channel_dim;
72 quant_params.scaleCount = ms_tensor.QuantParams().size();
73 std::transform(ms_tensor.QuantParams().begin(), ms_tensor.QuantParams().end(), std::back_inserter(scales),
74 [](QuantParam param) { return static_cast<float>(param.scale); });
75 quant_params.scales = scales.data();
76 }
77
78 auto shape = ms_tensor.Shape();
79 if (shape.empty() && !is_scalar) {
80 shape.push_back(1);
81 }
82 nnapi_tensor.dimensionCount = static_cast<uint32_t>(shape.size());
83 std::vector<uint32_t> dims;
84 std::transform(shape.begin(), shape.end(), std::back_inserter(dims),
85 [](int64_t dim) { return static_cast<uint32_t>(dim); });
86 nnapi_tensor.dimensions = dims.empty() ? nullptr : dims.data();
87
88 if (nnapi_->ANeuralNetworksModel_addOperand(nnapi_model, &nnapi_tensor) != ANEURALNETWORKS_NO_ERROR) {
89 MS_LOG(ERROR) << "Add operand to NNAPI model failed: " << ms_tensor.Name();
90 return RET_ERROR;
91 }
92 if (nnapi_tensor.type == ANEURALNETWORKS_TENSOR_QUANT8_SYMM_PER_CHANNEL) {
93 nnapi_->ANeuralNetworksModel_setOperandSymmPerChannelQuantParams(nnapi_model, idx, &quant_params);
94 }
95 if (ms_tensor.IsConst() &&
96 nnapi_->ANeuralNetworksModel_setOperandValue(nnapi_model, idx, ms_tensor.MutableData(), ms_tensor.DataSize()) !=
97 ANEURALNETWORKS_NO_ERROR) {
98 MS_LOG(ERROR) << "Set operand value for NNAPI model failed: " << ms_tensor.Name();
99 return RET_ERROR;
100 }
101 return RET_OK;
102 }
103 } // namespace lite
104 } // namespace mindspore
105