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

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

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