You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cc 54 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217
  1. /**
  2. * Copyright 2019-2020 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 <dirent.h>
  17. #include <dlfcn.h>
  18. #include <gflags/gflags.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <cctype>
  22. #include <climits>
  23. #include <cstdlib>
  24. #include <iostream>
  25. #include "common/gflags_util.h"
  26. #include "common/util.h"
  27. #include "common/util/error_manager/error_manager.h"
  28. #include "framework/common/debug/ge_log.h"
  29. #include "ge/ge_api.h"
  30. #include "generator/ge_generator.h"
  31. #include "graph/anchor.h"
  32. #include "graph/debug/ge_attr_define.h"
  33. #include "graph/graph.h"
  34. #include "graph/op_desc.h"
  35. #include "graph/utils/graph_utils.h"
  36. #include "graph/utils/type_utils.h"
  37. #include "init/gelib.h"
  38. #include "ir_build/atc_ir_common.h"
  39. #include "omg/omg.h"
  40. #include "omg/parser/parser_factory.h"
  41. #include "omg/parser/parser_inner_ctx.h"
  42. #include "parser/common/register_tbe.h"
  43. #include "register/op_registry.h"
  44. #include "single_op_parser.h"
  45. using domi::BuildMode;
  46. using domi::GetContext;
  47. using domi::OpRegistrationData;
  48. using domi::OpRegistry;
  49. using domi::Status;
  50. using domi::SUCCESS;
  51. using ge::GEN_OM_MODEL;
  52. using ge::GflagsUtils;
  53. using ge::MODEL_TO_JSON;
  54. using ge::ONLY_PRE_CHECK;
  55. using ge::ParseInputShape;
  56. using ge::PBTXT_TO_JSON;
  57. using std::map;
  58. using std::pair;
  59. using std::shared_ptr;
  60. using std::string;
  61. using std::vector;
  62. static bool is_dynamic_input = false;
  63. // 310 limited 8G size
  64. const char *const kGraphMemoryManagerMallocMaxSize = "8*1024*1024*1024";
  65. const char *const kModeSupport =
  66. "only support 0(model to framework model), "
  67. "1(framework model to json), 3(only pre-check), 5(pbtxt to json)";
  68. const char *const kModelToJsonSupport = "only support 0(Caffe) 3(TensorFlow)";
  69. DEFINE_string(model, "", "The model file.");
  70. DEFINE_string(output, "", "The output file path&name.");
  71. DEFINE_int32(framework, -1, "Framework type(0:Caffe; 1:MindSpore; 3:Tensorflow).");
  72. DEFINE_string(weight, "", "Optional; weight file. Required when framework is Caffe.");
  73. DEFINE_string(input_shape, "",
  74. "Optional; shape of input data. Required when framework is caffe "
  75. "or TensorFLow or MindSpore."
  76. "Format: \"input_name1:n1,c1,h1,w1;input_name2:n2,c2,h2,w2\"");
  77. DEFINE_bool(h, false, "show this help message");
  78. DEFINE_string(cal_conf, "", "Optional; the calibration config file.");
  79. DEFINE_string(insert_op_conf, "", "Optional; the config file to insert new op, for example AIPP op.");
  80. DEFINE_string(op_name_map, "", "Optional; custom op name mapping file.");
  81. DEFINE_string(target, "", "Optional; mini.");
  82. DEFINE_string(om, "", "The model file to be converted to json.");
  83. DEFINE_string(json, "", "The output json file path&name which is converted from a model.");
  84. DEFINE_int32(mode, 0,
  85. "Optional; run mode, 0(default): model => framework model; 1: "
  86. "framework model => json; 3: only pre-check; 5: pbtxt => json.");
  87. #if !defined(__ANDROID__) && !defined(ANDROID)
  88. DEFINE_int32(encrypt_mode, -1, "Optional; the encrypt flag. 0: encrypt; -1(default): not encrypt");
  89. DEFINE_string(encrypt_key, "", "Optional; the encrypt_key file.");
  90. DEFINE_string(certificate, "", "Optional; the certificate file.");
  91. DEFINE_string(hardware_key, "", "Optional; the ISV key file.");
  92. DEFINE_string(private_key, "", "Optional; the private key file.");
  93. #endif
  94. DEFINE_string(out_nodes, "",
  95. "Optional; output nodes designated by users."
  96. "Format: \"node_name1:0;node_name1:1;node_name2:0\"");
  97. DEFINE_string(precision_mode, "",
  98. "Optional; precision mode."
  99. "Support force_fp16, allow_mix_precision, allow_fp32_to_fp16, must_keep_origin_dtype.");
  100. DEFINE_string(input_format, "",
  101. "Optional; input_format, format of input data, NCHW;NHWC."
  102. "Format:\"NHWC\"");
  103. DEFINE_string(check_report, "check_result.json", "Optional; the pre-checking report file.");
  104. DEFINE_string(input_fp16_nodes, "",
  105. "Optional; input node datatype is fp16 and format is NC1HWC0."
  106. "Format:\"node_name1;node_name2\"");
  107. DEFINE_string(is_output_adjust_hw_layout, "",
  108. "Optional; Net output node's datatype is fp16 and format is "
  109. "NC1HWC0, or not."
  110. "Format:\"false,true,false,true\"");
  111. DEFINE_string(is_input_adjust_hw_layout, "",
  112. "Optional; Intput node's datatype is fp16 and format is "
  113. "NC1HWC0, or not."
  114. "Format:\"false,true,false,true\"");
  115. DEFINE_string(output_type, "",
  116. "Optional; output type! "
  117. "Support FP32,FP16,INT8,INT16,UINT16,UINT8,INT32,INT64,UINT32,UINT64,DOUBLE.");
  118. DEFINE_string(op_select_implmode, "",
  119. "Optional; op select implmode! "
  120. "Support high_precision, high_performance.");
  121. DEFINE_string(optypelist_for_implmode, "",
  122. "Optional; Nodes need use implmode selected in op_select_implmode "
  123. "Format:\"node_name1,node_name2\"");
  124. DEFINE_string(singleop, "", "Optional; If set, generate single op model with the given json file.");
  125. DEFINE_int32(disable_reuse_memory, 0, "Optional; If set to 1, disable reuse memory when generating if.");
  126. DEFINE_string(auto_tune_mode, "", "Optional; Set tune mode.");
  127. DEFINE_string(soc_version, "", "The soc version.");
  128. DEFINE_string(core_type, "AiCore", "Optional; If set to VectorCore, only use vector core.");
  129. DEFINE_string(aicore_num, "", "Optional; Set aicore num");
  130. DEFINE_string(buffer_optimize, "l2_optimize", "Optional; buffer optimize");
  131. DEFINE_string(fusion_switch_file, "", "Optional; Set fusion switch file path");
  132. DEFINE_string(save_original_model, "", "Optional; enable output original offline model. false(default)");
  133. DEFINE_string(dynamic_batch_size, "",
  134. "Optional; If set, generate dynamic multi batch model. "
  135. "Different batch sizes are split by ','."
  136. "dynamic_batch_size and dynamic_imagesize can only be set one.");
  137. DEFINE_string(dynamic_image_size, "",
  138. "Optional; If set, generate dynamic multi image size model."
  139. "Different groups of image size are split by ';',"
  140. "while different dimensions of each group are split by ','."
  141. "dynamic_batch_size and dynamic_imagesize can only be set one.");
  142. DEFINE_string(enable_small_channel, "0", "Optional; If set to 1, small channel is enabled.");
  143. DEFINE_string(enable_compress_weight, "false",
  144. "Optional; enable compress weight. true: enable; false(default): disable");
  145. DEFINE_string(compress_weight_conf, "", "Optional; the config file to compress weight");
  146. DEFINE_string(enable_single_stream, "", "Optional; enable single stream. true: enable; false(default): disable");
  147. DEFINE_string(log, "default", "Optional; generate atc log. Support debug, info, warning, error, null");
  148. DEFINE_string(dump_mode, "0", "Optional; generate infershape json,only support 1 , 0.");
  149. DEFINE_int32(op_debug_level, 0,
  150. "Optional; configure debug level of compiler. 0(default): close debug;"
  151. "1: open TBE compiler, export ccec file and TBE instruction mapping file; 2: open ccec compiler");
  152. class GFlagUtils {
  153. public:
  154. /**
  155. * @name InitGFlag
  156. * @brief initialize gflag
  157. * @return void
  158. */
  159. static void InitGFlag(int argc, char *argv[]) {
  160. // -help
  161. gflags::SetUsageMessage(
  162. "usage: ./atc <args>\n"
  163. "generate offline model example:\n"
  164. "./atc --model=./alexnet.prototxt --weight=./alexnet.caffemodel \n"
  165. "--framework=0 --output=./domi \n"
  166. "generate offline model for single op example:\n"
  167. "./atc --singleop=./op_list.json --output=./op_model \n"
  168. "arguments explain:\n"
  169. " --model Model file\n"
  170. " --singleop Single op definition file. atc will generate offline "
  171. "model(s) for single op if --singleop is set. \n"
  172. " Note: Only output, soc_verion, core_type, aicore_num, auto_tune_mode, precision_mode, "
  173. "op_select_implmode, enable_small_channel, enable_compress_weight, compress_weight_conf "
  174. "enable_single_stream and log are valid in this mode \n"
  175. " --weight Weight file. Required when framework is Caffe\n"
  176. " --framework Framework type(0:Caffe; 1:MindSpore; 3:Tensorflow)\n"
  177. " --output Output file path&name(needn't suffix, will add "
  178. ".om automatically). \n"
  179. " If --singleop is set, this arg specifies the directory to "
  180. "which the single op offline model will be generated\n"
  181. " --input_shape Shape of input data. Separate multiple nodes with semicolons (;)."
  182. "Use double quotation marks (\") to enclose each argument."
  183. "E.g.: \"input_name1:n1,c1,h1,w1;input_name2:n2,c2,h2,w2\"\n"
  184. " --h/help Show this help message\n"
  185. " --log Generate atc log. Support debug, info, warning, error, null\n"
  186. " --insert_op_conf Config file to insert new op\n"
  187. " --op_name_map Custom op name mapping file\n"
  188. " Note: A semicolon(;) cannot be included in each "
  189. "path, otherwise the resolved path will not match the expected one.\n"
  190. " --precision_mode precision mode, support force_fp16, allow_mix_precision, "
  191. "allow_fp32_to_fp16, must_keep_origin_dtype.\n"
  192. " --om The model file to be converted to json\n"
  193. " --json The output json file path&name which is "
  194. "converted from a model\n"
  195. " --mode Run mode. 0(default): model => framework model 1: "
  196. "framework model => json; 3: only pre-check; 5: pbtxt => json\n"
  197. " --dump_mode The switch of dump json with shape, to be used with mode 1.Default value is : 0."
  198. "0 means disable, 1 means enable .\n"
  199. " --input_format Format of input data. E.g.: \"NCHW\"\n"
  200. " --check_report The pre-checking report file. Default value is: "
  201. "\"check_result.json\"\n"
  202. " --disable_reuse_memory The switch of reuse memory. Default value is : 0."
  203. "0 means reuse memory, 1 means do not reuse memory.\n"
  204. " --input_fp16_nodes Input node datatype is fp16. Separate multiple nodes with semicolons "
  205. "(;)."
  206. "Use double quotation marks (\") to enclose each argument."
  207. "E.g.: \"node_name1;node_name2\"\n"
  208. " --is_input_adjust_hw_layout Intput node datatype is fp16 and format is "
  209. "NC1HWC0, used with input_fp16_nodes E.g.: \"true,true,false,true\"\n"
  210. " --out_nodes Output nodes designated by users. Separate multiple nodes with semicolons (;)."
  211. "Use double quotation marks (\") to enclose each argument."
  212. "E.g.: \"node_name1:0;node_name1:1;node_name2:0\"\n"
  213. " --is_output_adjust_hw_layout Net output node datatype is fp16 and format is "
  214. "NC1HWC0, used with out_nodes. E.g.: \"true,true,false,true\"\n"
  215. " --output_type Set net output type. Support FP32, FP16, UINT8."
  216. "E.g.: FP16, means all out nodes set datatype FP16."
  217. "\"node_name1:0:FP16;node_name2:1:FP32\", means multiple out nodes set corresponding datatype.\n"
  218. " --op_select_implmode Set op select implmode. Support high_precision, high_performance."
  219. "default: high_performance\n"
  220. "disable\n"
  221. " --optypelist_for_implmode Appoint which op to use op_select_implmode, used with op_select_implmode ."
  222. "Separate multiple nodes with commas (,). Use double quotation marks (\") to enclose each argument."
  223. "E.g.: \"node_name1,node_name2\"\n"
  224. " --soc_version The soc version. E.g.: \"Ascend310\"\n"
  225. " --core_type Set core type AiCore or VectorCore. VectorCore: use vector core. "
  226. "Default value is: AiCore\n"
  227. " --enable_compress_weight Enable compress weight. true: enable; false(default): disable\n"
  228. " --compress_weight_conf Config file to compress weight\n"
  229. " --aicore_num Set aicore num\n"
  230. " --buffer_optimize Set buffer optimize. default enabled, set \"off_optimize\" to close \n"
  231. " --enable_small_channel Set enable small channel. 0(default): disable; 1: enable\n"
  232. " --fusion_switch_file Set fusion switch file path\n"
  233. " --save_original_model Control whether to output original model. "
  234. "E.g.: true: output original model\"\n"
  235. " --dynamic_batch_size Set dynamic batch size. E.g: \"batchsize1,batchsize2,batchsize3\"\n"
  236. " --dynamic_image_size Set dynamic image size. Separate multiple nodes with semicolons (;)."
  237. "Use double quotation marks (\") to enclose each argument."
  238. "E.g: \"imagesize1_height,imagesize1_width;imagesize2_height,imagesize2_width\"\n"
  239. " --auto_tune_mode Set tune mode. E.g.: \"GA,RL\", support configure multiple, spit by ,\n"
  240. " --enable_single_stream Enable single stream. true: enable; false(default): disable\n");
  241. gflags::ParseCommandLineNonHelpFlags(&argc, &argv, true);
  242. // Using gflags to analyze input parameters
  243. GflagsUtils::ChangeHelpFlags(FLAGS_h);
  244. gflags::HandleCommandLineHelpFlags();
  245. }
  246. static Status CheckDumpInfershapeJsonFlags() {
  247. Status ret = CheckFrameWorkValid(FLAGS_framework, FLAGS_weight);
  248. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED, "check custom aicpu run so failed!");
  249. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_weight != "" && !ge::CheckInputPathValid(FLAGS_weight, "--weight"),
  250. return domi::FAILED, "Input parameter[--weight]'s value[%s] is invalid!",
  251. FLAGS_weight.c_str());
  252. return domi::SUCCESS;
  253. }
  254. static Status CheckFlags() {
  255. // No model file information passed in
  256. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_model == "",
  257. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {"model"});
  258. return domi::PARAM_INVALID, "Input parameter[--model]'s value is empty!");
  259. // check param disable_reuse_memory
  260. GE_CHK_BOOL_EXEC(ge::CheckDisableReuseMemoryParamValid(to_string(FLAGS_disable_reuse_memory)) == ge::SUCCESS,
  261. return ge::FAILED, "check disable_reuse_memory failed!");
  262. // check optypelist_for_implmode and op_select_implmode
  263. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  264. ge::CheckImplmodeParamValid(FLAGS_optypelist_for_implmode, FLAGS_op_select_implmode) != ge::SUCCESS,
  265. return ge::FAILED, "check optypelist_for_implmode and op_select_implmode failed!");
  266. // No output file information passed in
  267. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_mode == GEN_OM_MODEL && FLAGS_output == "",
  268. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {"output"});
  269. return domi::PARAM_INVALID, "Input parameter[--output]'s value is empty!");
  270. Status ret = CheckFrameWorkValid(FLAGS_framework, FLAGS_weight);
  271. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED, "CheckFrameWorkValid failed");
  272. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(ge::CheckDynamicBatchSizeOrImageSizeParamValid(
  273. FLAGS_dynamic_batch_size, FLAGS_dynamic_image_size, FLAGS_input_shape,
  274. FLAGS_input_format, is_dynamic_input) != ge::SUCCESS,
  275. return ge::FAILED, "check dynamic batch size or image size failed!");
  276. #if !defined(__ANDROID__) && !defined(ANDROID)
  277. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!CheckEncryptModeValid(FLAGS_encrypt_mode), return domi::FAILED,
  278. "encrypt_mode %d not valid!!", FLAGS_encrypt_mode);
  279. if (FLAGS_encrypt_mode == 0) { // Encryption mode
  280. GELOGI("domi will run with encrypt!");
  281. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_encrypt_key), return domi::FAILED,
  282. "encrypt_key file not found!!");
  283. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_certificate), return domi::FAILED,
  284. "certificate file not found!!");
  285. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_hardware_key), return domi::FAILED,
  286. "hardware_key file not found!!");
  287. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_private_key), return domi::FAILED,
  288. "private_key file not found!!");
  289. } else { // No encryption
  290. GELOGI("domi will run without encrypt!");
  291. }
  292. #endif
  293. /**
  294. * Check the validity of the I / O file path
  295. */
  296. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_model, "--model"), return domi::FAILED,
  297. "model file %s not found!!", FLAGS_model.c_str());
  298. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_weight != "" && !ge::CheckInputPathValid(FLAGS_weight, "--weight"),
  299. return domi::FAILED, "weight file %s not found!!", FLAGS_weight.c_str());
  300. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_cal_conf != "" && !ge::CheckInputPathValid(FLAGS_cal_conf, "--cal_conf"),
  301. return domi::FAILED, "calibration config file %s not found!!",
  302. FLAGS_cal_conf.c_str());
  303. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  304. FLAGS_op_name_map != "" && !ge::CheckInputPathValid(FLAGS_op_name_map, "--op_name_map"), return domi::FAILED,
  305. "op config file %s not found!!", FLAGS_op_name_map.c_str());
  306. GE_CHK_BOOL_EXEC(ge::CheckInsertOpConfParamValid(std::string(FLAGS_insert_op_conf)) == ge::SUCCESS,
  307. return ge::FAILED, "check insert op conf failed!");
  308. GE_CHK_BOOL_EXEC(
  309. ge::CheckCompressWeightParamValid(FLAGS_enable_compress_weight, FLAGS_compress_weight_conf) == ge::SUCCESS,
  310. return ge::FAILED, "check compress weight failed!");
  311. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckOutputPathValid(FLAGS_check_report, "--check_report"), return domi::FAILED,
  312. "check_report file %s not found!!", FLAGS_check_report.c_str());
  313. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_mode == GEN_OM_MODEL && (!ge::CheckOutputPathValid(FLAGS_output, "--output") ||
  314. !CheckPathWithName(FLAGS_output)),
  315. return domi::FAILED, "output path %s is not valid!!", FLAGS_output.c_str());
  316. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  317. FLAGS_save_original_model != "" && FLAGS_save_original_model != "true" && FLAGS_save_original_model != "false",
  318. ErrorManager::GetInstance().ATCReportErrMessage("E10005", {"parameter", "value"},
  319. {"save_original_model", FLAGS_save_original_model});
  320. return domi::FAILED, "Input parameter[--save_original_model]'s value[%s] must be true or false.",
  321. FLAGS_save_original_model.c_str());
  322. GE_CHK_BOOL_EXEC(ge::CheckBufferOptimizeParamValid(FLAGS_buffer_optimize) == ge::SUCCESS, return ge::FAILED,
  323. "check output type failed!");
  324. GE_CHK_BOOL_EXEC(ge::CheckEnableSingleStreamParamValid(std::string(FLAGS_enable_single_stream)) == ge::SUCCESS,
  325. return ge::FAILED, "check enable single stream failed!");
  326. return domi::SUCCESS;
  327. }
  328. /**
  329. * Verifying the parameters of converting model to JSON
  330. * 1. Fmk_model
  331. * 2. out_json
  332. **/
  333. static Status CheckConverJsonParamFlags() {
  334. // No model path passed in
  335. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_om == "",
  336. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {"om"});
  337. return domi::PARAM_INVALID, "Input parameter[--om]'s value is empty!!");
  338. // JSON path not passed in
  339. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(FLAGS_json == "",
  340. ErrorManager::GetInstance().ATCReportErrMessage("E10004", {"parameter"}, {"json"});
  341. return domi::PARAM_INVALID, "Input parameter[--json]'s value is empty!!");
  342. // Check if the model path is valid
  343. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckInputPathValid(FLAGS_om, "--om"), return domi::PARAM_INVALID,
  344. "model file path is invalid: %s.", FLAGS_om.c_str());
  345. // Check whether the JSON path is valid
  346. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(!ge::CheckOutputPathValid(FLAGS_json, "--json"), return domi::PARAM_INVALID,
  347. "json file path is invalid: %s.", FLAGS_json.c_str());
  348. return domi::SUCCESS;
  349. }
  350. /**
  351. * Check command line parameters for explicit settings
  352. * true: Explicit setup
  353. * false: Not set up
  354. * */
  355. static bool CheckFlagSet(string flag) {
  356. gflags::CommandLineFlagInfo info;
  357. return !(gflags::GetCommandLineFlagInfo(flag.c_str(), &info) && info.is_default);
  358. }
  359. private:
  360. static bool CheckEncryptModeValid(const int encrypt_mode) {
  361. #if !defined(__ANDROID__) && !defined(ANDROID)
  362. if (encrypt_mode != 0 && encrypt_mode != -1) {
  363. DOMI_LOGE("encrypt mode must be 0 or -1");
  364. return false;
  365. }
  366. #else
  367. if (encrypt_mode != -1) {
  368. DOMI_LOGE("encrypt mode must be -1");
  369. return false;
  370. }
  371. #endif
  372. return true;
  373. }
  374. static Status CheckFrameWorkValid(int framework, const std::string weight_file) {
  375. if (framework != (int32_t)domi::CAFFE && framework != (int32_t)domi::TENSORFLOW &&
  376. framework != (int32_t)domi::MINDSPORE && framework != (int32_t)domi::ONNX) {
  377. // No framework information was passed in or the entered framework is illegal
  378. ErrorManager::GetInstance().ATCReportErrMessage("E10007", {"parameter", "support"},
  379. {"framework", "0(Caffe) or 1(MindSpore) or 3(TensorFlow)"});
  380. DOMI_LOGE(
  381. "Input parameter[--framework] is mandatory and it's value must be: "
  382. "0(Caffe) or 1(MindSpore) or 3(TensorFlow).");
  383. return domi::PARAM_INVALID;
  384. }
  385. if ((framework == (int32_t)domi::CAFFE) && (weight_file == "")) {
  386. ErrorManager::GetInstance().ATCReportErrMessage("E10008", {"parameter"}, {"weight"});
  387. DOMI_LOGE("Input parameter[--weight]'s value is empty when framework is 0(CAFFE)!");
  388. return domi::PARAM_INVALID;
  389. }
  390. if ((framework == (int32_t)domi::TENSORFLOW) && (weight_file != "")) {
  391. GELOGW("Parameter weight is ignored for TensorFlow.");
  392. }
  393. if ((framework == (int32_t)domi::ONNX) && (weight_file != "")) {
  394. GELOGW("Parameter weight is ignored for Onnx.");
  395. }
  396. return domi::SUCCESS;
  397. }
  398. static bool CheckPathWithName(const std::string &fileName) {
  399. // Determine file path length
  400. if (fileName.size() > static_cast<int>(PATH_MAX)) {
  401. ErrorManager::GetInstance().ATCReportErrMessage("E10021", {"parameter", "size"},
  402. {"output", std::to_string(PATH_MAX)});
  403. GELOGE(ge::FAILED, "Input parameter[--output]'s path is too long, it must be less than %d", PATH_MAX);
  404. return false;
  405. }
  406. // Find the last separator
  407. int slashPosition = fileName.size() - 1;
  408. for (; slashPosition >= 0; slashPosition--) {
  409. if (fileName[slashPosition] == '\\' || fileName[slashPosition] == '/') {
  410. break;
  411. }
  412. }
  413. // Failure if no filename follows the path
  414. if (slashPosition == static_cast<int>(fileName.size() - 1)) {
  415. ErrorManager::GetInstance().ATCReportErrMessage("E10022", {"parameter", "filename"}, {"output", fileName});
  416. DOMI_LOGE("Input parameter[--output]'s path[%s] not include file name", fileName.c_str());
  417. return false;
  418. }
  419. return true;
  420. }
  421. };
  422. void SetDynamicBatchSizeOrImagesizeOptions() {
  423. if (!FLAGS_dynamic_batch_size.empty()) {
  424. domi::GetContext().dynamic_batch_size = FLAGS_dynamic_batch_size;
  425. }
  426. if (!FLAGS_dynamic_image_size.empty()) {
  427. domi::GetContext().dynamic_image_size = FLAGS_dynamic_image_size;
  428. }
  429. }
  430. static bool CheckInputFormat() {
  431. if (FLAGS_input_format.empty()) {
  432. // Set default format
  433. if (FLAGS_framework == static_cast<int32_t>(domi::TENSORFLOW)) {
  434. FLAGS_input_format = "NHWC";
  435. } else {
  436. FLAGS_input_format = "NCHW";
  437. }
  438. return true;
  439. } else if ((FLAGS_framework == static_cast<int32_t>(domi::CAFFE))) { // caffe
  440. if (ge::caffe_support_input_format.find(FLAGS_input_format) != ge::caffe_support_input_format.end()) {
  441. return true;
  442. }
  443. // only support NCHW ND
  444. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  445. {"--input_format", FLAGS_input_format, ge::kCaffeFormatSupport});
  446. GELOGE(ge::FAILED, "Invalid value for --input_format[%s], %s.", FLAGS_input_format.c_str(),
  447. ge::kCaffeFormatSupport);
  448. return false;
  449. } else if ((FLAGS_framework == static_cast<int32_t>(domi::TENSORFLOW))) { // tf
  450. if (ge::tf_support_input_format.find(FLAGS_input_format) != ge::tf_support_input_format.end()) {
  451. return true;
  452. }
  453. // only support NCHW NHWC ND NCDHW NDHWC
  454. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  455. {"--input_format", FLAGS_input_format, ge::kTFFormatSupport});
  456. GELOGE(ge::FAILED, "Invalid value for --input_format[%s], %s.", FLAGS_input_format.c_str(), ge::kTFFormatSupport);
  457. return false;
  458. } else if (FLAGS_framework == static_cast<int32_t>(domi::ONNX)) {
  459. if (ge::onnx_support_input_format.find(FLAGS_input_format) != ge::onnx_support_input_format.end()) {
  460. return true;
  461. }
  462. // only support NCHW ND
  463. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  464. {"--input_format", FLAGS_input_format, ge::kONNXFormatSupport});
  465. GELOGE(ge::FAILED, "Invalid value for --input_format[%s], %s.", FLAGS_input_format.c_str(), ge::kONNXFormatSupport);
  466. return false;
  467. }
  468. return true;
  469. }
  470. #if !defined(__ANDROID__) && !defined(ANDROID)
  471. static void GetCustomOpPath(std::string &customop_path) {
  472. GELOGI("Enter get custom op path schedule");
  473. std::string fmk_type = ge::TypeUtils::FmkTypeToSerialString(static_cast<domi::FrameworkType>(FLAGS_framework));
  474. GELOGI("Framework type is %s.", fmk_type.c_str());
  475. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  476. if (path_env != nullptr) {
  477. std::string path = path_env;
  478. customop_path = (path + "/framework/custom" + "/:") + (path + "/framework/built-in/" + fmk_type);
  479. GELOGI("Get custom so path from env : %s", path_env);
  480. return;
  481. }
  482. std::string path_base = ge::GELib::GetPath();
  483. GELOGI("path_base is %s", path_base.c_str());
  484. path_base = path_base.substr(0, path_base.rfind('/'));
  485. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  486. customop_path = (path_base + "ops/framework/custom" + "/:") + (path_base + "ops/framework/built-in/" + fmk_type);
  487. return;
  488. }
  489. void GetPluginSoFileList(const string &path, vector<string> &fileList, string &caffe_parser_path) {
  490. // Support to split multiple so directories by ":"
  491. GELOGI("path is %s", path.c_str());
  492. vector<string> v_path = ge::StringUtils::Split(path, ':');
  493. for (size_t i = 0; i < v_path.size(); ++i) {
  494. ge::FindParserSo(v_path[i], fileList, caffe_parser_path);
  495. GELOGI("CustomOpLib full name = %s", v_path[i].c_str());
  496. }
  497. }
  498. void LoadModelParserLib(std::string caffe_parser_path) {
  499. if (FLAGS_framework == static_cast<int32_t>(domi::TENSORFLOW)) {
  500. void *tf_handle = dlopen("libfmk_tensorflow_parser.so", RTLD_NOW | RTLD_GLOBAL);
  501. if (tf_handle == nullptr) {
  502. GELOGW("dlopen fmk library [libfmk_tensorflow_parser.so] failed.");
  503. return;
  504. }
  505. GELOGI("plugin load libfmk_tensorflow_parser.so success.");
  506. } else if (FLAGS_framework == static_cast<int32_t>(domi::CAFFE)) {
  507. // What we are dealing with here is that the user modifies the caffe.proto scenario.
  508. // If no lib_Caffe_Parser.so is found under the plugin path, use the default lib_Caffe_Parser.so path.
  509. caffe_parser_path = caffe_parser_path.empty() ? "lib_caffe_parser.so" : caffe_parser_path;
  510. void *handle = dlopen(caffe_parser_path.c_str(), RTLD_NOW | RTLD_GLOBAL);
  511. if (handle == nullptr) {
  512. GELOGW("dlopen failed, plugin name:%s. Message(%s).", caffe_parser_path.c_str(), dlerror());
  513. return;
  514. }
  515. GELOGI("plugin load %s success.", caffe_parser_path.c_str());
  516. // According to the dependency, the Caffe parsing module of the framework is loaded here( libfmk_caffe_parser.so).
  517. // (depend on the lib_caffe_parser.so)
  518. void *fmk_handle = dlopen("libfmk_caffe_parser.so", RTLD_NOW | RTLD_GLOBAL);
  519. if (fmk_handle == nullptr) {
  520. GELOGW("dlopen fmk library [libfmk_caffe_parser.so] failed.");
  521. if (dlclose(handle) != 0) {
  522. GELOGW("dlclose lib_caffe_parser.so failed.");
  523. }
  524. return;
  525. }
  526. GELOGI("plugin load libfmk_caffe_parser.so success.");
  527. } else if (FLAGS_framework == static_cast<int32_t>(domi::ONNX)) {
  528. void *handle = dlopen("libfmk_onnx_parser.so", RTLD_NOW | RTLD_GLOBAL);
  529. if (handle == nullptr) {
  530. GELOGW("dlopen fmk library [libfmk_onnx_parser.so] failed.");
  531. return;
  532. }
  533. GELOGI("plugin load libfmk_onnx_parser.so success.");
  534. } else {
  535. GELOGW("Framework:%s is not support.",
  536. ge::TypeUtils::FmkTypeToSerialString(static_cast<domi::FrameworkType>(FLAGS_framework)).c_str());
  537. return;
  538. }
  539. return;
  540. }
  541. void LoadCustomOpLib(bool need_load_ops_plugin) {
  542. std::string plugin_path;
  543. GetCustomOpPath(plugin_path);
  544. vector<string> fileList;
  545. string caffe_parser_path = "";
  546. // whether there are files in the plugin so path
  547. GetPluginSoFileList(plugin_path, fileList, caffe_parser_path);
  548. // no file
  549. if (fileList.empty() && caffe_parser_path.empty()) {
  550. GELOGW("can not find any plugin file in plugin_path: %s", plugin_path.c_str());
  551. }
  552. LoadModelParserLib(caffe_parser_path);
  553. if (!need_load_ops_plugin) {
  554. GELOGI("No need to load ops plugin so.");
  555. return;
  556. }
  557. OpRegistry::Instance()->registrationDatas.clear();
  558. // load other so files except lib_caffe_parser.so in the plugin so path
  559. for (auto elem : fileList) {
  560. ge::StringUtils::Trim(elem);
  561. void *handle = dlopen(elem.c_str(), RTLD_NOW | RTLD_GLOBAL);
  562. if (handle == nullptr) {
  563. GELOGW("dlopen failed, plugin name:%s. Message(%s).", elem.c_str(), dlerror());
  564. } else {
  565. GELOGI("plugin load %s success.", elem.c_str());
  566. }
  567. }
  568. std::vector<OpRegistrationData> registrationDatas = OpRegistry::Instance()->registrationDatas;
  569. for (OpRegistrationData reg_data : registrationDatas) {
  570. (void)ge::OpRegistrationTbe::Instance()->Finalize(reg_data);
  571. (void)OpRegistry::Instance()->Register(reg_data);
  572. }
  573. }
  574. void SaveCustomCaffeProtoPath() {
  575. GELOGI("Enter save custom caffe proto path.");
  576. std::string path_base = ge::GELib::GetPath();
  577. GELOGI("path_base is %s", path_base.c_str());
  578. path_base = path_base.substr(0, path_base.rfind('/'));
  579. path_base = path_base.substr(0, path_base.rfind('/') + 1);
  580. ge::GetParserContext().caffe_proto_path = path_base + "include/proto/";
  581. string customop_path;
  582. const char *path_env = std::getenv("ASCEND_OPP_PATH");
  583. if (path_env != nullptr) {
  584. std::string path = path_env;
  585. customop_path = path + "/framework/custom/caffe/";
  586. GELOGI("Get custom proto path from env : %s", path_env);
  587. ge::GetParserContext().custom_proto_path = customop_path;
  588. return;
  589. }
  590. customop_path = path_base + "ops/framework/custom/caffe/";
  591. ge::GetParserContext().custom_proto_path = customop_path;
  592. return;
  593. }
  594. #endif
  595. Status CreateInputsForInference(const ge::Graph &graph, vector<ge::GeTensor> &inputs) {
  596. auto compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  597. GE_CHECK_NOTNULL(compute_graph);
  598. for (ge::NodePtr &input_node : compute_graph->GetAllNodes()) {
  599. GE_CHECK_NOTNULL(input_node);
  600. ge::OpDescPtr op = input_node->GetOpDesc();
  601. GE_CHECK_NOTNULL(op);
  602. if (op->GetType() == ge::DATA) {
  603. GELOGI("Data op inputDesc size is: %zu", op->GetAllInputsDesc().size());
  604. ge::GeTensorDesc tensor = op->GetInputDesc(0);
  605. string data_op_name = op->GetName();
  606. GELOGI("Data op name is: %s", data_op_name.c_str());
  607. ge::GeShape data_shape;
  608. auto iter = GetContext().input_dims.find(data_op_name);
  609. if (iter != GetContext().input_dims.end()) {
  610. data_shape = ge::GeShape(iter->second);
  611. GELOGI("Data op get shape from Context.");
  612. } else {
  613. data_shape = tensor.GetShape();
  614. GELOGI("Data op get shape from InputDesc in geir graph.");
  615. }
  616. ge::DataType data_type = tensor.GetDataType();
  617. string data_type_str = ge::TypeUtils::DataTypeToSerialString(data_type);
  618. GELOGI("Data op get data type:%s from InputDesc in geir graph.", data_type_str.c_str());
  619. ge::GeTensor input_tensor;
  620. ge::GeTensorDesc desc(data_shape, ge::Format(GetContext().format), data_type);
  621. input_tensor.SetTensorDesc(desc);
  622. inputs.push_back(input_tensor);
  623. }
  624. }
  625. GELOGI("Build ME model, inputs size is: %zu", inputs.size());
  626. return ge::SUCCESS;
  627. }
  628. domi::Status GenerateInfershapeJson() {
  629. if (!CheckInputFormat()) {
  630. GELOGE(ge::FAILED, "Check input_format failed");
  631. return domi::FAILED;
  632. }
  633. Status ret = GFlagUtils::CheckDumpInfershapeJsonFlags();
  634. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED, "Check flags failed!");
  635. ge::GeGenerator ge_generator;
  636. std::map<string, string> options;
  637. ge::Status geRet = ge_generator.Initialize(options);
  638. if (geRet != ge::SUCCESS) {
  639. DOMI_LOGE("GeGenerator initialize failed!");
  640. return domi::FAILED;
  641. }
  642. ge::Graph graph;
  643. std::map<string, string> atc_params;
  644. atc_params.insert(std::pair<string, string>("input_format", FLAGS_input_format));
  645. ret = ParseGraph(graph, atc_params, FLAGS_om.c_str(), FLAGS_weight.c_str(), (domi::FrameworkType)FLAGS_framework, "",
  646. FLAGS_target.c_str(), (ge::RunMode)FLAGS_mode, false);
  647. if (ret != ge::SUCCESS) {
  648. DOMI_LOGE("ATC Parse graph domi::FAILED");
  649. (void)ge_generator.Finalize();
  650. return domi::FAILED;
  651. }
  652. geRet = ge_generator.GenerateInfershapeGraph(graph);
  653. if (geRet != ge::SUCCESS) {
  654. DOMI_LOGE("ATC GenerateInfershapeJson failed");
  655. (void)ge_generator.Finalize();
  656. return domi::FAILED;
  657. }
  658. if (DumpInfershapeJson(graph, FLAGS_json.c_str()) != SUCCESS) {
  659. DOMI_LOGE("ATC DumpInfershapeJson failed");
  660. (void)ge_generator.Finalize();
  661. return domi::FAILED;
  662. }
  663. (void)ge_generator.Finalize();
  664. return ge::SUCCESS;
  665. }
  666. static Status ConvertModelToJson(int fwk_type, const string &model_file, const string &json_file) {
  667. Status ret = domi::SUCCESS;
  668. if (fwk_type == -1) {
  669. ret = ge::ConvertOmModelToJson(model_file.c_str(), json_file.c_str());
  670. return ret;
  671. }
  672. if ((fwk_type != domi::TENSORFLOW) && (fwk_type != domi::CAFFE) && (fwk_type != domi::ONNX)) {
  673. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  674. {"--framework", std::to_string(fwk_type), kModelToJsonSupport});
  675. GELOGE(ge::FAILED, "Invalid value for --framework[%d], %s.", fwk_type, kModelToJsonSupport);
  676. return ge::FAILED;
  677. }
  678. if (FLAGS_dump_mode == "0") {
  679. // Caffe or tf model to json depend on lib_caffe_parser.so or libfmk_tensorflow_parser.so.
  680. LoadCustomOpLib(false);
  681. ret = ge::ConvertFwkModelToJson((domi::FrameworkType)fwk_type, model_file.c_str(), json_file.c_str());
  682. return ret;
  683. } else if (FLAGS_dump_mode == "1") {
  684. // Caffe or tf model to json depend on lib_caffe_parser.so or libfmk_tensorflow_parser.so and ops plugin so.
  685. LoadCustomOpLib(true);
  686. ret = GenerateInfershapeJson();
  687. return ret;
  688. } else {
  689. ErrorManager::GetInstance().ATCReportErrMessage("E10006", {"parameter"}, {"dump_mode"});
  690. GELOGE(ge::FAILED, "Input parameter[--dump_mode]'s value must be 1 or 0.");
  691. return ge::FAILED;
  692. }
  693. }
  694. domi::Status GenerateModel(std::map<string, string> &options, std::string output) {
  695. ge::GeGenerator ge_generator;
  696. ge::Status geRet = ge::SUCCESS;
  697. std::shared_ptr<ge::GELib> instance_ptr = ge::GELib::GetInstance();
  698. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  699. geRet = ge::GELib::Initialize(options);
  700. if (geRet != ge::SUCCESS) {
  701. DOMI_LOGE("GE initialize failed!");
  702. return domi::FAILED;
  703. }
  704. }
  705. geRet = ge_generator.Initialize(options);
  706. if (geRet != ge::SUCCESS) {
  707. DOMI_LOGE("GeGenerator initialize failed!");
  708. (void)ge::GELib::GetInstance()->Finalize();
  709. return domi::FAILED;
  710. }
  711. ge::Graph graph;
  712. std::vector<ge::GeTensor> inputs;
  713. if (FLAGS_framework == domi::MINDSPORE) {
  714. // load model from file
  715. ge::Model load_model = ge::Model("loadmodel", "version2");
  716. auto ret1 = load_model.LoadFromFile(FLAGS_model);
  717. if (ret1 != ge::GRAPH_SUCCESS) {
  718. ErrorManager::GetInstance().ATCReportErrMessage("E10041", {"parameter"}, {FLAGS_model});
  719. DOMI_LOGE(
  720. "Load model from %s failed, please check model file or "
  721. "input parameter[--framework] is correct",
  722. FLAGS_model.c_str());
  723. (void)ge_generator.Finalize();
  724. (void)ge::GELib::GetInstance()->Finalize();
  725. return domi::FAILED;
  726. }
  727. graph = load_model.GetGraph();
  728. GE_CHK_STATUS_EXEC(ge::InitDomiOmgContext(FLAGS_input_shape, FLAGS_input_format, "", is_dynamic_input),
  729. GELOGE(ge::FAILED, "ATC Generate call InitDomiOmgContext ret fail");
  730. (void)ge_generator.Finalize(); (void)ge::GELib::GetInstance()->Finalize(); return domi::FAILED);
  731. Status ret = CreateInputsForInference(graph, inputs);
  732. if (ret != ge::SUCCESS) {
  733. GELOGE(ge::FAILED, "create inputs for inference failed.");
  734. (void)ge_generator.Finalize();
  735. (void)ge::GELib::GetInstance()->Finalize();
  736. return domi::FAILED;
  737. }
  738. if (SetOutputNodeInfo(graph, "", "") != domi::SUCCESS) {
  739. GELOGE(ge::FAILED, "Set output node info fail.");
  740. (void)ge_generator.Finalize();
  741. (void)ge::GELib::GetInstance()->Finalize();
  742. return domi::FAILED;
  743. }
  744. } else {
  745. std::map<string, string> atc_params;
  746. atc_params.insert(std::pair<string, string>("input_shape", FLAGS_input_shape));
  747. atc_params.insert(std::pair<string, string>("out_nodes", FLAGS_out_nodes));
  748. atc_params.insert(std::pair<string, string>("input_format", FLAGS_input_format));
  749. atc_params.insert(std::pair<string, string>("check_report", FLAGS_check_report));
  750. atc_params.insert(std::pair<string, string>("input_fp16_nodes", FLAGS_input_fp16_nodes));
  751. atc_params.insert(std::pair<string, string>("is_input_adjust_hw_layout", FLAGS_is_input_adjust_hw_layout));
  752. atc_params.insert(std::pair<string, string>("is_output_adjust_hw_layout", FLAGS_is_output_adjust_hw_layout));
  753. atc_params.insert(std::pair<string, string>("compress_weight_conf", FLAGS_compress_weight_conf));
  754. atc_params.insert(std::pair<string, string>(string(ge::OUTPUT_DATATYPE), FLAGS_output_type));
  755. atc_params.insert(std::pair<string, string>("output", output));
  756. Status ret =
  757. ParseGraph(graph, atc_params, FLAGS_model.c_str(), FLAGS_weight.c_str(), (domi::FrameworkType)FLAGS_framework,
  758. FLAGS_op_name_map.c_str(), FLAGS_target.c_str(), (ge::RunMode)FLAGS_mode, is_dynamic_input);
  759. // in ONLY_PRE_CHECK mode, pre-checking report has already saved in ParseGraph
  760. if (FLAGS_mode == ge::ONLY_PRE_CHECK) {
  761. (void)ge_generator.Finalize();
  762. (void)ge::GELib::GetInstance()->Finalize();
  763. if (ret != ge::SUCCESS) {
  764. DOMI_LOGE("ATC precheck fail.");
  765. return domi::FAILED;
  766. }
  767. return domi::SUCCESS;
  768. }
  769. if (ret != ge::SUCCESS) {
  770. DOMI_LOGE("ATC Parse graph domi::FAILED");
  771. DOMI_LOGE("ATC Generate execute failed"); // Duplicate log. (for test case
  772. (void)ge_generator.Finalize();
  773. (void)ge::GELib::GetInstance()->Finalize();
  774. return domi::FAILED;
  775. }
  776. if (SetOutputNodeInfo(graph, FLAGS_output_type, "") != domi::SUCCESS) {
  777. DOMI_LOGE("Set output node info fail.");
  778. (void)ge_generator.Finalize();
  779. (void)ge::GELib::GetInstance()->Finalize();
  780. return domi::FAILED;
  781. }
  782. }
  783. geRet = ge_generator.GenerateOfflineModel(graph, output, inputs);
  784. if (geRet != ge::SUCCESS) {
  785. DOMI_LOGE("GE GenerateOfflineModel execute failed");
  786. DOMI_LOGE("ATC Generate execute failed"); // Duplicate log. (for test case
  787. // checking error log)
  788. (void)ge_generator.Finalize();
  789. (void)ge::GELib::GetInstance()->Finalize();
  790. return domi::FAILED;
  791. }
  792. (void)ge_generator.Finalize();
  793. (void)ge::GELib::GetInstance()->Finalize();
  794. return ge::SUCCESS;
  795. }
  796. static void SetEnvForSingleOp(std::map<string, string> &options) {
  797. string flag_on = "1";
  798. string flag_off = "0";
  799. options.emplace(ge::GE_FE_FLAG, flag_on);
  800. options.emplace(ge::STREAM_NUM, "1"); // single op only use one stream
  801. options.emplace(ge::RUN_FLAG, flag_off);
  802. options.emplace(ge::OPTION_GRAPH_RUN_MODE, flag_off);
  803. options.emplace(ge::SINGLE_OP_FLAG, flag_on);
  804. options.emplace(ge::PRECISION_MODE, FLAGS_precision_mode);
  805. options.emplace(ge::SOC_VERSION, FLAGS_soc_version);
  806. options.emplace(ge::CORE_TYPE, FLAGS_core_type);
  807. options.emplace(ge::AICORE_NUM, FLAGS_aicore_num);
  808. options.emplace(ge::OP_SELECT_IMPL_MODE, FLAGS_op_select_implmode);
  809. options.emplace(ge::OPTYPELIST_FOR_IMPLMODE, FLAGS_optypelist_for_implmode);
  810. options.emplace(ge::AUTO_TUNE_MODE, FLAGS_auto_tune_mode);
  811. options.emplace(ge::GRAPH_MEMORY_MAX_SIZE, kGraphMemoryManagerMallocMaxSize);
  812. options.emplace(ge::OP_DEBUG_LEVEL, to_string(FLAGS_op_debug_level));
  813. }
  814. domi::Status GenerateSingleOp(const std::string &json_file_path) {
  815. if (!FLAGS_output.empty() && !ge::CheckOutputPathValid(FLAGS_output, "--output")) {
  816. DOMI_LOGE("output path %s is not valid!", FLAGS_output.c_str());
  817. return domi::FAILED;
  818. }
  819. // check optypelist_for_implmode and op_select_implmode
  820. GE_CHK_BOOL_TRUE_EXEC_WITH_LOG(
  821. ge::CheckImplmodeParamValid(FLAGS_optypelist_for_implmode, FLAGS_op_select_implmode) != ge::SUCCESS,
  822. return ge::FAILED, "check optypelist_for_implmode and op_select_implmode failed!");
  823. std::map<string, string> options;
  824. // need to be changed when ge.ini plan is done
  825. SetEnvForSingleOp(options);
  826. vector<ge::SingleOpBuildParam> build_params;
  827. if (ge::SingleOpParser::ParseSingleOpList(json_file_path, build_params) != ge::SUCCESS) {
  828. DOMI_LOGE("parse single op json file failed");
  829. return domi::FAILED;
  830. }
  831. auto ret = ge::GELib::Initialize(options);
  832. if (ret != ge::SUCCESS) {
  833. DOMI_LOGE("GE initialize failed!");
  834. return domi::FAILED;
  835. }
  836. ge::GeGenerator generator;
  837. ret = generator.Initialize(options);
  838. if (ret != SUCCESS) {
  839. DOMI_LOGE("GeGenerator initialize failed!");
  840. (void)ge::GELib::GetInstance()->Finalize();
  841. return domi::FAILED;
  842. }
  843. int index = 0;
  844. for (auto &param : build_params) {
  845. string output_path;
  846. if (!FLAGS_output.empty()) {
  847. output_path = FLAGS_output + "/";
  848. }
  849. output_path += param.file_name;
  850. ret = generator.BuildSingleOpModel(param.op_desc, param.inputs, param.outputs, output_path);
  851. if (ret != SUCCESS) {
  852. DOMI_LOGE("Compile op failed. ge ret = %u, op index = %d", ret, index);
  853. ret = domi::FAILED;
  854. break;
  855. }
  856. GELOGI("Compile op success. op index = %d, output = %s", index, output_path.c_str());
  857. index += 1;
  858. }
  859. (void)generator.Finalize();
  860. (void)ge::GELib::GetInstance()->Finalize();
  861. return ret;
  862. }
  863. domi::Status GenerateOmModel() {
  864. if (!CheckInputFormat()) {
  865. GELOGE(ge::FAILED, "Check input_format failed");
  866. return domi::FAILED;
  867. }
  868. Status ret = GFlagUtils::CheckFlags();
  869. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED,
  870. "Check flags failed! Please check whether some atc params that include semicolons[;] use double "
  871. "quotation marks (\") to enclose each argument such as out_nodes, input_shape, dynamic_image_size");
  872. #if !defined(__ANDROID__) && !defined(ANDROID)
  873. // Load custom operator Library
  874. LoadCustomOpLib(true);
  875. SaveCustomCaffeProtoPath();
  876. ret = ge::CheckCustomAiCpuOpLib();
  877. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED, "check custom aicpu run so failed!");
  878. #endif
  879. const int f_stream_num = 1;
  880. std::map<string, string> options;
  881. options.insert(std::pair<string, string>(string(ge::FRAMEWORK_TYPE), to_string(FLAGS_framework)));
  882. options.insert(std::pair<string, string>(string(ge::STREAM_NUM), to_string(f_stream_num)));
  883. options.insert(std::pair<string, string>(string(ge::CALIBRATION_CONF_FILE), FLAGS_cal_conf));
  884. options.insert(std::pair<string, string>(string(ge::ENCRYPT_MODE), to_string(FLAGS_encrypt_mode)));
  885. options.insert(std::pair<string, string>(string(ge::EK_FILE), FLAGS_encrypt_key));
  886. options.insert(std::pair<string, string>(string(ge::CERT_FILE), FLAGS_certificate));
  887. options.insert(std::pair<string, string>(string(ge::HW_KEY_FILE), FLAGS_hardware_key));
  888. options.insert(std::pair<string, string>(string(ge::PRIVATE_KEY_FILE), FLAGS_private_key));
  889. options.insert(std::pair<string, string>(string(ge::OUTPUT_NODE_NAME), FLAGS_out_nodes));
  890. options.insert(std::pair<string, string>(string(ge::INSERT_OP_FILE), FLAGS_insert_op_conf));
  891. options.insert(std::pair<string, string>(string(ge::PRECISION_MODE), FLAGS_precision_mode));
  892. options.insert(std::pair<string, string>(string(ge::RUN_FLAG), to_string(0)));
  893. options.insert(std::pair<string, string>(string(ge::TRAIN_FLAG), to_string(0)));
  894. if (!FLAGS_output_type.empty()) {
  895. options.insert(std::pair<string, string>(string(ge::OUTPUT_DATATYPE), FLAGS_output_type));
  896. }
  897. options.insert(std::pair<string, string>(string(ge::OP_SELECT_IMPL_MODE), FLAGS_op_select_implmode));
  898. options.insert(std::pair<string, string>(string(ge::OPTYPELIST_FOR_IMPLMODE), FLAGS_optypelist_for_implmode));
  899. if (!FLAGS_input_fp16_nodes.empty()) {
  900. GELOGI("FLAGS_input_fp16_nodes : %s .", FLAGS_input_fp16_nodes.c_str());
  901. options.insert(std::pair<string, string>(ge::INPUT_FP16_NODES, FLAGS_input_fp16_nodes));
  902. }
  903. options.insert(std::pair<string, string>(string(ge::AUTO_TUNE_MODE), FLAGS_auto_tune_mode));
  904. options.insert(
  905. std::pair<string, string>(string(ge::OPTION_EXEC_DISABLE_REUSED_MEMORY), to_string(FLAGS_disable_reuse_memory)));
  906. options.insert(std::pair<string, string>(string(ge::SOC_VERSION), FLAGS_soc_version));
  907. options.insert(std::pair<string, string>(string(ge::CORE_TYPE), FLAGS_core_type));
  908. options.insert(std::pair<string, string>(string(ge::AICORE_NUM), FLAGS_aicore_num));
  909. options.insert(std::pair<string, string>(string(ge::BUFFER_OPTIMIZE), FLAGS_buffer_optimize));
  910. options.insert(std::pair<string, string>(string(ge::ENABLE_SMALL_CHANNEL), FLAGS_enable_small_channel));
  911. options.insert(std::pair<string, string>(string(ge::FUSION_SWITCH_FILE), FLAGS_fusion_switch_file));
  912. options.insert(std::pair<string, string>(string(ge::ENABLE_COMPRESS_WEIGHT), (FLAGS_enable_compress_weight == "true")
  913. ? ge::kEnableCompressWeightTrue
  914. : ge::kEnableCompressWeightFalse));
  915. options.insert(std::pair<string, string>(string(ge::GRAPH_MEMORY_MAX_SIZE), kGraphMemoryManagerMallocMaxSize));
  916. options.insert(std::pair<string, string>(string(ge::ENABLE_SINGLE_STREAM), FLAGS_enable_single_stream));
  917. SetDynamicBatchSizeOrImagesizeOptions();
  918. if (!FLAGS_save_original_model.empty()) {
  919. options.insert(std::pair<string, string>(string(ge::SAVE_ORIGINAL_MODEL), FLAGS_save_original_model));
  920. options.insert(std::pair<string, string>(string(ge::ORIGINAL_MODEL_FILE), FLAGS_output + "_original.om"));
  921. }
  922. options.insert(std::pair<string, string>(string(ge::OP_DEBUG_LEVEL), to_string(FLAGS_op_debug_level)));
  923. // print atc option map
  924. ge::PrintOptionMap(options, "atc option");
  925. // When the ATC module is transferred to a model, the suffix ".om" is automatically added to the model name
  926. FLAGS_output = FLAGS_output + ".om";
  927. ret = GenerateModel(options, FLAGS_output);
  928. if (ret != domi::SUCCESS) {
  929. return domi::FAILED;
  930. }
  931. return domi::SUCCESS;
  932. }
  933. domi::Status ConvertModelToJson() {
  934. Status ret = GFlagUtils::CheckConverJsonParamFlags();
  935. GE_CHK_BOOL_EXEC(ret == domi::SUCCESS, return domi::FAILED, "Check convert json params flags failed!");
  936. ret = ConvertModelToJson(FLAGS_framework, FLAGS_om, FLAGS_json);
  937. GE_IF_BOOL_EXEC(ret != domi::SUCCESS, return domi::FAILED);
  938. return domi::SUCCESS;
  939. }
  940. bool CheckRet(domi::Status ret) {
  941. if (ret != domi::SUCCESS) {
  942. if (FLAGS_mode == ONLY_PRE_CHECK) {
  943. GELOGW("ATC precheck failed.");
  944. } else if (FLAGS_mode == GEN_OM_MODEL) {
  945. GELOGW("ATC generate offline model failed.");
  946. } else if (FLAGS_mode == MODEL_TO_JSON) {
  947. GELOGW("ATC convert model to json file failed.");
  948. } else if (FLAGS_mode == PBTXT_TO_JSON) {
  949. GELOGW("ATC convert pbtxt to json file failed.");
  950. } else {
  951. return false;
  952. }
  953. return false;
  954. }
  955. if (FLAGS_mode == ONLY_PRE_CHECK) {
  956. GELOGI("ATC precheck success.");
  957. } else if (FLAGS_mode == GEN_OM_MODEL) {
  958. GELOGI("ATC generate offline model success.");
  959. } else if (FLAGS_mode == MODEL_TO_JSON) {
  960. GELOGI("ATC convert model to json file success.");
  961. } else if (FLAGS_mode == PBTXT_TO_JSON) {
  962. GELOGI("ATC convert pbtxt to json file success.");
  963. }
  964. return true;
  965. }
  966. domi::Status ConvertPbtxtToJson() {
  967. Status ret = GFlagUtils::CheckConverJsonParamFlags();
  968. if (ret != domi::SUCCESS) {
  969. GELOGE(ge::FAILED, "Check convert json params flags failed!");
  970. return domi::FAILED;
  971. }
  972. ret = ge::ConvertPbtxtToJson(FLAGS_om.c_str(), FLAGS_json.c_str());
  973. if (ret != domi::SUCCESS) {
  974. GELOGE(ge::FAILED, "ConvertPbtxtToJson fail.");
  975. return domi::FAILED;
  976. }
  977. return domi::SUCCESS;
  978. }
  979. int init(int argc, char *argv[]) {
  980. GFlagUtils::InitGFlag(argc, argv);
  981. // set log level
  982. int ret = -1;
  983. const std::set<string> log_level = {"default", "null", "debug", "info", "warning", "error"};
  984. if (log_level.count(FLAGS_log) == 0) {
  985. std::cout << "E10010: invalid value for --log:" << FLAGS_log << ", only support debug, info, warning, error, null"
  986. << std::endl;
  987. return ret;
  988. }
  989. ret = ge::CheckLogParamValidAndSetLogLevel(FLAGS_log);
  990. if (ret != 0) {
  991. return ret;
  992. }
  993. std::string path_base = ge::GELib::GetPath();
  994. ret = ErrorManager::GetInstance().Init(path_base);
  995. if (ret != 0) {
  996. DOMI_LOGE("ErrorManager init fail !");
  997. return ret;
  998. }
  999. return 0;
  1000. }
  1001. int main(int argc, char *argv[]) {
  1002. std::cout << "hello world" << std::endl;
  1003. Status ret = domi::SUCCESS;
  1004. std::cout << "ATC start working now, please wait for a moment." << std::endl;
  1005. try {
  1006. // Initialize
  1007. if (init(argc, argv) != 0) {
  1008. std::cout << "ATC run failed, Please check the detail log, Try \'atc --help\' for more information" << std::endl;
  1009. return -1;
  1010. }
  1011. do {
  1012. if (!FLAGS_singleop.empty()) {
  1013. ret = GenerateSingleOp(FLAGS_singleop);
  1014. break;
  1015. }
  1016. // default mode(mode:0), Open source model to model
  1017. if (GEN_OM_MODEL == FLAGS_mode || ONLY_PRE_CHECK == FLAGS_mode) {
  1018. GE_IF_BOOL_EXEC(GenerateOmModel() != domi::SUCCESS, ret = domi::FAILED; break);
  1019. } else if (MODEL_TO_JSON == FLAGS_mode) { // Mode 1, transfer model to JSON
  1020. GE_CHK_BOOL_EXEC(ConvertModelToJson() == domi::SUCCESS, ret = domi::FAILED;
  1021. break, "ATC ConvertJson execute failed!!");
  1022. } else if (FLAGS_mode == ge::RunMode::PBTXT_TO_JSON) {
  1023. GE_CHK_BOOL_EXEC(ConvertPbtxtToJson() == domi::SUCCESS, ret = domi::FAILED;
  1024. break, "ATC convert pbtxt to json execute failed!!");
  1025. } else {
  1026. ErrorManager::GetInstance().ATCReportErrMessage("E10001", {"parameter", "value", "reason"},
  1027. {"--mode", std::to_string(FLAGS_mode), kModeSupport});
  1028. GELOGE(ge::PARAM_INVALID, "Invalid value for --mode[%d], %s.", FLAGS_mode, kModeSupport);
  1029. ret = domi::FAILED;
  1030. break;
  1031. }
  1032. } while (0);
  1033. } catch (std::bad_alloc) {
  1034. ret = domi::FAILED;
  1035. DOMI_LOGE("ATC run failed, bad memory allocation occur !");
  1036. std::cout << "ATC run failed, bad memory allocation occur !" << std::endl;
  1037. } catch (...) {
  1038. ret = domi::FAILED;
  1039. DOMI_LOGE("ATC run failed, some exceptions occur !");
  1040. std::cout << "ATC run failed, some exceptions occur !" << std::endl;
  1041. }
  1042. if (!CheckRet(ret)) {
  1043. std::cout << "ATC run failed, Please check the detail log, Try \'atc --help\' for more information" << std::endl;
  1044. int result = ErrorManager::GetInstance().OutputErrMessage(STDOUT_FILENO);
  1045. if (result != 0) {
  1046. DOMI_LOGE("ErrorManager outputErrMessage fail !");
  1047. }
  1048. return ret;
  1049. } else {
  1050. std::cout << "ATC run success, welcome to the next use." << std::endl;
  1051. return 0;
  1052. }
  1053. }

图引擎模块(GE)是MindSpore的一个子模块,其代码由C++实现,位于前端模块ME和底层硬件之间,起到承接作用。图引擎模块以ME下发的图作为输入,然后进行一系列的深度图优化操作,最后输出一张可以在底层硬件上高效运行的图。GE针对昇腾AI处理器的硬件结构特点,做了特定的优化工作,以此来充分发挥出昇腾AI处理器的强大算力。在进行模型训练/推理时,GE会被自动调用而用户并不感知。GE主要由GE API和GE Core两部分组成,详细的架构图如下所示