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.

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

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