• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "tools/converter/config_parser/acl_option_param_parser.h"
18 #include <map>
19 #include <vector>
20 #include "tools/common/string_util.h"
21 #include "src/common/log_adapter.h"
22 #include "src/common/log_util.h"
23 #include "nnacl/op_base.h"
24 
25 namespace mindspore {
26 namespace lite {
27 static const std::map<std::string, DataType> kSupportedDtypeOptionMap = {{"FP16", DataType::kNumberTypeFloat16},
28                                                                          {"FP32", DataType::kNumberTypeFloat32},
29                                                                          {"UINT8", DataType::kNumberTypeUInt8}};
30 
ParseAclOptionCfg(const AclOptionCfgString & acl_option_string,acl::AclModelOptionCfg * acl_option_cfg)31 STATUS AclOptionParamParser::ParseAclOptionCfg(const AclOptionCfgString &acl_option_string,
32                                                acl::AclModelOptionCfg *acl_option_cfg) {
33   CHECK_NULL_RETURN(acl_option_cfg);
34 
35   if (ParseCommon(acl_option_string, acl_option_cfg) != RET_OK) {
36     MS_LOG(ERROR) << "Parse common failed.";
37     return RET_INPUT_PARAM_INVALID;
38   }
39   if (!acl_option_string.device_id.empty()) {
40     if (ParseDeviceId(acl_option_string.device_id, acl_option_cfg) != RET_OK) {
41       MS_LOG(ERROR) << "Parse device id failed, val: " << acl_option_string.device_id;
42       return RET_INPUT_PARAM_INVALID;
43     }
44   }
45   if (!acl_option_string.output_type.empty()) {
46     if (ParseOutputType(acl_option_string.output_type, acl_option_cfg) != RET_OK) {
47       MS_LOG(ERROR) << "Parse output type failed, val; " << acl_option_string.output_type;
48       return RET_INPUT_PARAM_INVALID;
49     }
50   }
51   if (!acl_option_string.dynamic_batch_size.empty()) {
52     if (ParseDynamicBatchSize(acl_option_string.dynamic_batch_size, acl_option_cfg) != RET_OK) {
53       MS_LOG(ERROR) << "Parse dynamic batch size failed, val: " << acl_option_string.dynamic_batch_size;
54       return RET_INPUT_PARAM_INVALID;
55     }
56   }
57   if (!acl_option_string.input_shape_vector.empty()) {
58     if (ParseInputShapeVector(acl_option_string.input_shape_vector, acl_option_cfg) != RET_OK) {
59       MS_LOG(ERROR) << "Parse input shape vector failed, val: " << acl_option_string.input_shape_vector;
60       return RET_INPUT_PARAM_INVALID;
61     }
62   }
63   return RET_OK;
64 }
65 
ParseCommon(const AclOptionCfgString & acl_option_string,acl::AclModelOptionCfg * acl_option_cfg)66 STATUS AclOptionParamParser::ParseCommon(const AclOptionCfgString &acl_option_string,
67                                          acl::AclModelOptionCfg *acl_option_cfg) {
68   MS_LOG(DEBUG) << "Input format: " << acl_option_string.input_format
69                 << ", Input shape: " << acl_option_string.input_shape
70                 << ", Precision_mode: " << acl_option_string.precision_mode
71                 << ", Op_select_impl_mode: " << acl_option_string.op_select_impl_mode
72                 << ", Fusion_switch_config_file_path" << acl_option_string.fusion_switch_config_file_path
73                 << ", Buffer_optimize: " << acl_option_string.buffer_optimize
74                 << ", Insert_op_config_file_path: " << acl_option_string.insert_op_config_file_path
75                 << ", Dynamic image size: " << acl_option_string.dynamic_image_size
76                 << ", Dynamic dims: " << acl_option_string.dynamic_dims << ", Aoe mode: " << acl_option_string.aoe_mode;
77   acl_option_cfg->input_format = acl_option_string.input_format;
78   acl_option_cfg->input_shape = acl_option_string.input_shape;
79   acl_option_cfg->precision_mode = acl_option_string.precision_mode;
80   acl_option_cfg->op_select_impl_mode = acl_option_string.op_select_impl_mode;
81   acl_option_cfg->fusion_switch_config_file_path = acl_option_string.fusion_switch_config_file_path;
82   acl_option_cfg->buffer_optimize = acl_option_string.buffer_optimize;
83   acl_option_cfg->insert_op_config_file_path = acl_option_string.insert_op_config_file_path;
84   acl_option_cfg->dynamic_image_size = acl_option_string.dynamic_image_size;
85   acl_option_cfg->dynamic_dims = acl_option_string.dynamic_dims;
86   acl_option_cfg->aoe_mode = acl_option_string.aoe_mode;
87   acl_option_cfg->custom_opp_path = acl_option_string.custom_opp_path;
88   acl_option_cfg->init_options_map = acl_option_string.init_options_map;
89   acl_option_cfg->build_options_map = acl_option_string.build_options_map;
90   acl_option_cfg->aoe_global_options_map = acl_option_string.aoe_global_options_map;
91   acl_option_cfg->aoe_tuning_options_map = acl_option_string.aoe_tuning_options_map;
92   return RET_OK;
93 }
94 
ParseDeviceId(const std::string & device_id,acl::AclModelOptionCfg * acl_option_cfg)95 STATUS AclOptionParamParser::ParseDeviceId(const std::string &device_id, acl::AclModelOptionCfg *acl_option_cfg) {
96   MS_LOG(DEBUG) << "Acl option device id: " << device_id;
97   acl_option_cfg->device_id = 0;  // default
98   int32_t device_id_num;
99   if (ConvertIntNum(device_id, &device_id_num)) {
100     acl_option_cfg->device_id = device_id_num;
101   }
102   return RET_OK;
103 }
104 
ParseOutputType(const std::string & output_type,acl::AclModelOptionCfg * acl_option_cfg)105 STATUS AclOptionParamParser::ParseOutputType(const std::string &output_type, acl::AclModelOptionCfg *acl_option_cfg) {
106   MS_LOG(DEBUG) << "Acl option output type: " << output_type;
107   acl_option_cfg->output_type = DataType::kInvalidType;
108   if (kSupportedDtypeOptionMap.find(output_type) != kSupportedDtypeOptionMap.end()) {
109     acl_option_cfg->output_type = kSupportedDtypeOptionMap.at(output_type);
110   }
111   return RET_OK;
112 }
113 
114 // dynamic_batch_size="1,2,4,8"
ParseDynamicBatchSize(const std::string & dynamic_batch_size,acl::AclModelOptionCfg * acl_option_cfg)115 STATUS AclOptionParamParser::ParseDynamicBatchSize(const std::string &dynamic_batch_size,
116                                                    acl::AclModelOptionCfg *acl_option_cfg) {
117   MS_LOG(DEBUG) << "Acl option dynamic batch size: " << dynamic_batch_size;
118   std::vector<std::string> batch_size_string = SplitStringToVector(dynamic_batch_size, ',');
119   for (const auto &item : batch_size_string) {
120     int32_t val;
121     if (ConvertIntNum(item, &val)) {
122       size_t tmp_val = static_cast<size_t>(val);
123       acl_option_cfg->dynamic_batch_size.push_back(tmp_val);
124     }
125   }
126   return RET_OK;
127 }
128 
129 // input shape vector=[1,2,3];[4,5,6]
ParseInputShapeVector(const std::string & input_shape_vector,acl::AclModelOptionCfg * acl_option_cfg)130 STATUS AclOptionParamParser::ParseInputShapeVector(const std::string &input_shape_vector,
131                                                    acl::AclModelOptionCfg *acl_option_cfg) {
132   MS_LOG(DEBUG) << "Acl option input shape vector: " << input_shape_vector;
133   std::vector<std::string> intput_shape_str = SplitStringToVector(input_shape_vector, ';');
134   int32_t idx = 0;
135   std::map<int32_t, std::vector<int32_t>> input_shape_map;
136   for (auto &item : intput_shape_str) {
137     if (item.size() < DIMENSION_2D || item[0] != '[' || item[item.size() - 1] != ']') {
138       MS_LOG(ERROR) << "Input param is invalid, val: " << item << ", the format should be [a, b].";
139       return RET_ERROR;
140     }
141     std::string tmp = item.substr(1, item.size() - DIMENSION_2D);
142     if (tmp.find("[") != std::string::npos) {
143       MS_LOG(ERROR) << "Input param is invalid, value: " << item << ", multi input shape should be split by ;.";
144       return RET_ERROR;
145     }
146     std::vector<std::string> intput_shape = SplitStringToVector(tmp, ',');
147     std::vector<int32_t> input_shape_int;
148     for (auto &shape : intput_shape) {
149       int32_t val;
150       if (ConvertIntNum(shape, &val)) {
151         input_shape_int.push_back(val);
152       }
153     }
154     input_shape_map[idx] = input_shape_int;
155     idx++;
156   }
157 
158   acl_option_cfg->input_shape_map = input_shape_map;
159   return RET_OK;
160 }
161 }  // namespace lite
162 }  // namespace mindspore
163