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

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