• 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 #ifndef MINDSPORE_LITE_TOOLS_CONVERTER_CONFIG_PARSER_CONFIG_FILE_PARSER_H_
18 #define MINDSPORE_LITE_TOOLS_CONVERTER_CONFIG_PARSER_CONFIG_FILE_PARSER_H_
19 #include <string>
20 #include <map>
21 #include <vector>
22 #include <memory>
23 #include <set>
24 #include "tools/converter/cxx_api/converter_para.h"
25 
26 namespace mindspore {
27 namespace lite {
28 struct DataPreProcessString {
29   std::string calibrate_path;
30   std::string calibrate_size;
31   std::string input_type;
32   std::string image_to_format;
33   std::string normalize_mean;
34   std::string normalize_std;
35   std::string resize_width;
36   std::string resize_height;
37   std::string resize_method;
38   std::string center_crop_width;
39   std::string center_crop_height;
40 };
41 
42 struct CommonQuantString {
43   std::string quant_type;
44   std::string bit_num;
45   std::string min_quant_weight_size;
46   std::string min_quant_weight_channel;
47   std::string skip_quant_node;
48   std::string debug_info_save_path;
49   std::string enable_encode;
50   std::string workspace;  // support for over 2G model.
51 };
52 
53 struct MixedBitWeightQuantString {
54   std::string init_scale;
55   std::string auto_tune;
56   std::string use_cv_data;
57   std::string max_iterations;
58 };
59 
60 struct WeightQuantString {
61   std::string dequant_strategy;
62   std::string quant_strategy;
63   std::string update_mindir;
64   std::string max_segments;  // Define how many segments to use for parallel FSE
65                              // It's recommended to use the max number of cores of your device
66   std::string per_channel;
67   std::string bias_correction;
68 };
69 
70 struct FullQuantString {
71   std::string activation_quant_method;
72   std::string bias_correction;
73   std::string target_device;
74   std::string per_channel;
75   std::string smooth_alpha;
76   std::string enable_smooth_shift;
77 };
78 
79 struct RegistryInfoString {
80   std::string plugin_path;
81   std::string disable_fusion;
82   std::string fusion_blacklists;
83 };
84 
85 struct AclOptionCfgString {
86   std::string device_id;
87   std::string input_format;
88   std::string input_shape_vector;
89   std::string input_shape;
90   std::string output_type;
91   std::string precision_mode;
92   std::string op_select_impl_mode;
93   std::string fusion_switch_config_file_path;
94   std::string dynamic_batch_size;
95   std::string buffer_optimize;
96   std::string insert_op_config_file_path;
97   std::string dynamic_image_size;
98   std::string dynamic_dims;
99   std::string aoe_mode;
100   std::string custom_opp_path;
101   std::map<std::string, std::string> init_options_map;
102   std::map<std::string, std::string> build_options_map;
103   std::map<std::string, std::string> aoe_global_options_map;
104   std::map<std::string, std::string> aoe_tuning_options_map;
105 };
106 
107 struct MicroParamString {
108   std::string codegen_mode;
109   std::string target;
110   std::string support_parallel;
111   std::string debug_mode;
112   std::string enable_micro;
113   std::string save_path;
114   std::string project_name;
115   std::string keep_original_weight;
116   std::string changeable_weights_name;
117   std::string inputs_shape;
118   std::string dynamic_dim_params;
119   std::map<std::string, std::vector<int>> dynamic_symbols_map;
120 };
121 
122 struct ThirdPartyModelString {
123   std::string input_dtypes;
124   std::string input_shapes;
125   std::string input_names;  // optional, default: ""
126   std::string input_formats;  // optional, default: NHWC
127   std::string output_dtypes;
128   std::string output_shapes;
129   std::string output_names;  // optional, default: ""
130   std::string output_formats;  // optional, default: NHWC
131   std::string extended_parameters;  // format: {key1:value1;ker2:value2}
132 };
133 
134 struct CpuOptionCfgString {
135   std::string architecture;
136   std::string instruction;
137 };
138 
139 struct TransformQuantString {
140   std::string export_precision_mode;
141 };
142 
143 struct DynamicQuantString {
144   std::string quant_strategy;
145 };
146 
147 struct OMConverterString {
148   std::string input_name_vector;
149   std::string input_shape_vector;
150   std::string input_data_type_vector;
151   std::string output_name_vector;
152   std::string output_shape_vector;
153   std::string output_data_type_vector;
154 };
155 using GraphKernelString = std::vector<std::string>;
156 
157 class ConfigFileParser {
158  public:
159   int ParseConfigFile(const std::string &config_file_path,
160                       std::map<int, std::map<std::string, std::string>> *model_param_infos);
161   int ParseConfigParam(std::map<std::string, std::map<std::string, std::string>> *maps);
162   bool SetParamByConfigfile(const std::shared_ptr<mindspore::ConverterPara> &param,
163                             const std::map<std::string, std::string> &ascend_map);
GetDataPreProcessString()164   DataPreProcessString GetDataPreProcessString() const { return this->data_pre_process_string_; }
GetCommonQuantString()165   CommonQuantString GetCommonQuantString() const { return this->common_quant_string_; }
GetMixedBitWeightQuantString()166   MixedBitWeightQuantString GetMixedBitWeightQuantString() const { return this->mixed_bit_quant_string_; }
GetFullQuantString()167   FullQuantString GetFullQuantString() const { return this->full_quant_string_; }
GetWeightQuantString()168   WeightQuantString GetWeightQuantString() const { return this->weight_quant_string_; }
GetRegistryInfoString()169   RegistryInfoString GetRegistryInfoString() const { return this->registry_info_string_; }
GetAclOptionCfgString()170   AclOptionCfgString GetAclOptionCfgString() { return this->acl_option_cfg_string_; }
GetMicroParamString()171   MicroParamString GetMicroParamString() { return this->micro_param_string_; }
GetThirdPartyModelString()172   lite::ThirdPartyModelString GetThirdPartyModelString() const { return this->third_party_model_string_; }
GetCpuOptionCfgString()173   CpuOptionCfgString GetCpuOptionCfgString() { return this->cpu_option_cfg_string_; }
GetTransformQuantString()174   TransformQuantString GetTransformQuantString() const { return this->transform_quant_string_; }
GetDynamicQuantString()175   DynamicQuantString GetDynamicQuantString() const { return this->dynamic_quant_string_; }
GetGraphKernelString()176   GraphKernelString GetGraphKernelString() const { return this->graph_kernel_string_; }
177 
178  private:
179   int ParseDataPreProcessString(const std::map<std::string, std::map<std::string, std::string>> &maps);
180   int ParseCommonQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
181   int ParseMixedBitQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
182   int ParseFullQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
183   int ParseWeightQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
184   int ParseRegistryInfoString(const std::map<std::string, std::map<std::string, std::string>> &maps);
185   int ParseAclOptionCfgString(const std::map<std::string, std::map<std::string, std::string>> &maps);
186   int SetMapData(const std::map<std::string, std::string> &input_map,
187                  const std::map<std::string, std::string &> &parse_map, const std::string &section,
188                  const std::set<std::string> &dynamic_key = {});
189   int ParseMicroParamString(const std::map<std::string, std::map<std::string, std::string>> &maps);
190   int ParseThirdPartyParamString(const std::map<std::string, std::map<std::string, std::string>> &sections);
191   int ParseCpuOptionCfgString(const std::map<std::string, std::map<std::string, std::string>> &maps);
192   int ParseTransformQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
193   int ParseDynamicQuantString(const std::map<std::string, std::map<std::string, std::string>> &maps);
194   int ParseGraphKernelString(const std::map<std::string, std::map<std::string, std::string>> &maps);
195   void SetVariableParams(const std::shared_ptr<mindspore::ConverterPara> &param,
196                          const std::map<std::string, std::string> &ascend_map);
197   int ProcessVariableParam(const std::vector<std::string> &variable_param, std::vector<int64_t> &variable_index);
198   int CheckVariableParm(const std::vector<int64_t> &variable_index);
199   STATUS ParseCustomPattern(const std::shared_ptr<mindspore::ConverterPara> &param, std::string custom_pattern_str);
200   int ParseOMConverterString(const std::map<std::string, std::map<std::string, std::string>> &maps);
201   bool CheckPluginCustomOps(const std::vector<std::string> &plugin_custom_ops);
202 
203  private:
204   DataPreProcessString data_pre_process_string_;
205   CommonQuantString common_quant_string_;
206   MixedBitWeightQuantString mixed_bit_quant_string_;
207   FullQuantString full_quant_string_;
208   WeightQuantString weight_quant_string_;
209   RegistryInfoString registry_info_string_;
210   AclOptionCfgString acl_option_cfg_string_;
211   MicroParamString micro_param_string_;
212   lite::ThirdPartyModelString third_party_model_string_;
213   CpuOptionCfgString cpu_option_cfg_string_;
214   TransformQuantString transform_quant_string_;
215   DynamicQuantString dynamic_quant_string_;
216   GraphKernelString graph_kernel_string_;
217   std::vector<int64_t> inputs_variable_index_;
218   std::vector<int64_t> outputs_variable_index_;
219   OMConverterString om_converter_string_;
220 };
221 
222 }  // namespace lite
223 }  // namespace mindspore
224 
225 #endif  // MINDSPORE_LITE_TOOLS_CONVERTER_CONFIG_PARSER_CONFIG_FILE_PARSER_H_
226