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

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

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