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 59 kB

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

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