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.

ge_ir_build.cc 22 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 "external/ge/ge_ir_build.h"
  17. #include <vector>
  18. #include "common/auth/file_saver.h"
  19. #include "external/register/register_types.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "framework/common/ge_inner_error_codes.h"
  22. #include "framework/common/string_util.h"
  23. #include "framework/common/types.h"
  24. #include "framework/common/util.h"
  25. #include "framework/omg/omg_inner_types.h"
  26. #include "framework/omg/omg_inner_types.h"
  27. #include "ge/ge_api_types.h"
  28. #include "generator/ge_generator.h"
  29. #include "graph/compute_graph.h"
  30. #include "graph/ge_tensor.h"
  31. #include "graph/utils/type_utils.h"
  32. #include "graph/ge_global_options.h"
  33. #include "init/gelib.h"
  34. #include "ir_build/atc_ir_common.h"
  35. #include "model/ge_model.h"
  36. #include "graph/shape_refiner.h"
  37. using std::string;
  38. using namespace std;
  39. namespace ge {
  40. namespace {
  41. const std::string IR_OPTION_TARGET = "target";
  42. const std::string IR_OPTION_MODE = "mode";
  43. const std::string IR_OP_CONF_DELIMITER = ":";
  44. const std::string IR_OPTION_LOG_LEVEL_DEFAULT = "default";
  45. const std::string IR_OPTION_BUFFER_OPTIMIZE_DEFAULT = "l2_optimize";
  46. const std::string IR_OPTION_DISABLE_REUSE_MEMORY_DEFAULT = "0";
  47. const std::string IR_OPTION_ENABLE_COMPRESS_WEIGHT_DEFAULT = "false";
  48. } // namespace
  49. static graphStatus CheckGlobalOptions(std::map<std::string, std::string> &global_options) {
  50. // check param disable_reuse_memory
  51. std::string disable_reuse_memory = global_options.find(ge::ir_option::EXEC_DISABLE_REUSED_MEMORY) ==
  52. global_options.end()
  53. ? IR_OPTION_DISABLE_REUSE_MEMORY_DEFAULT
  54. : global_options[ge::ir_option::EXEC_DISABLE_REUSED_MEMORY];
  55. GE_CHK_BOOL_EXEC(ge::CheckDisableReuseMemoryParamValid(disable_reuse_memory) == ge::SUCCESS,
  56. return ge::GRAPH_PARAM_INVALID, "check disable_reuse_memory failed!");
  57. global_options[ge::ir_option::EXEC_DISABLE_REUSED_MEMORY] = disable_reuse_memory;
  58. // check buffer_optimize
  59. std::string buffer_optimize = global_options.find(ge::ir_option::BUFFER_OPTIMIZE) == global_options.end()
  60. ? IR_OPTION_BUFFER_OPTIMIZE_DEFAULT
  61. : global_options[ge::ir_option::BUFFER_OPTIMIZE];
  62. GE_CHK_BOOL_EXEC(ge::CheckBufferOptimizeParamValid(buffer_optimize) == ge::SUCCESS,
  63. return ge::GRAPH_PARAM_INVALID, "check buffer optimize failed!");
  64. global_options[ge::ir_option::BUFFER_OPTIMIZE] = buffer_optimize;
  65. // check enable_single_stream
  66. std::string enable_single_stream = global_options.find(ge::ir_option::ENABLE_SINGLE_STREAM) == global_options.end()
  67. ? ""
  68. : global_options[ge::ir_option::ENABLE_SINGLE_STREAM];
  69. GE_CHK_BOOL_EXEC(ge::CheckEnableSingleStreamParamValid(enable_single_stream) == ge::SUCCESS,
  70. return ge::GRAPH_PARAM_INVALID, "check enable single stream failed!");
  71. // check compress_weight
  72. std::string enable_compress_weight = global_options.find(ge::ir_option::ENABLE_COMPRESS_WEIGHT) ==
  73. global_options.end()
  74. ? IR_OPTION_ENABLE_COMPRESS_WEIGHT_DEFAULT
  75. : global_options[ge::ir_option::ENABLE_COMPRESS_WEIGHT];
  76. std::string compress_weight_conf = global_options.find(ge::ir_option::COMPRESS_WEIGHT_CONF) == global_options.end()
  77. ? ""
  78. : global_options[ge::ir_option::COMPRESS_WEIGHT_CONF];
  79. GE_CHK_BOOL_EXEC(ge::CheckCompressWeightParamValid(enable_compress_weight, compress_weight_conf) == ge::SUCCESS,
  80. return ge::GRAPH_PARAM_INVALID, "check compress weight failed!");
  81. global_options[ge::ir_option::ENABLE_COMPRESS_WEIGHT] = (enable_compress_weight == "true") ?
  82. ge::kEnableCompressWeightTrue :
  83. ge::kEnableCompressWeightFalse;
  84. // check optypelist_for_implmode and op_select_implmode
  85. std::string optypelist_for_implmode = global_options.find(ge::ir_option::OPTYPELIST_FOR_IMPLMODE) ==
  86. global_options.end()
  87. ? ""
  88. : global_options[ge::ir_option::OPTYPELIST_FOR_IMPLMODE];
  89. std::string op_select_implmode = global_options.find(ge::ir_option::OP_SELECT_IMPL_MODE) ==
  90. global_options.end()
  91. ? ""
  92. : global_options[ge::ir_option::OP_SELECT_IMPL_MODE];
  93. GE_CHK_BOOL_EXEC(
  94. ge::CheckImplmodeParamValid(optypelist_for_implmode, op_select_implmode) == ge::SUCCESS,
  95. return ge::GRAPH_PARAM_INVALID, "check optypelist_for_implmode and op_select_implmode failed!");
  96. global_options[ge::ir_option::OP_SELECT_IMPL_MODE] = op_select_implmode;
  97. return GRAPH_SUCCESS;
  98. }
  99. graphStatus aclgrphBuildInitialize(std::map<std::string, std::string> global_options) {
  100. GELOGD("Enter aclgrphInitialize start!");
  101. // check global options
  102. if (CheckGlobalOptions(global_options) != GRAPH_SUCCESS) {
  103. GELOGE(GRAPH_PARAM_INVALID, "Check global options falied!");
  104. return GRAPH_PARAM_INVALID;
  105. }
  106. // print global option map
  107. ge::PrintOptionMap(global_options, "global option");
  108. std::shared_ptr<ge::GELib> instance_ptr = ge::GELib::GetInstance();
  109. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  110. GELOGI("aclgrphInitialize start!");
  111. auto ret = ge::GELib::Initialize(global_options);
  112. if (ret != ge::SUCCESS) {
  113. GELOGE(ret, "GE initialize failed!");
  114. return GRAPH_FAILED;
  115. }
  116. }
  117. GELOGW("gelib has been initialized!");
  118. return GRAPH_SUCCESS;
  119. }
  120. void aclgrphBuildFinalize() {
  121. if (ge::GELib::GetInstance() != nullptr && ge::GELib::GetInstance()->InitFlag()) {
  122. (void)ge::GELib::GetInstance()->Finalize();
  123. return;
  124. }
  125. GELOGW("[Notice] gelib has not been initialized!do nothing!");
  126. }
  127. class Impl {
  128. public:
  129. Impl() {
  130. omg_context_ = domi::GetContext();
  131. omg_context_.format = domi::DOMI_TENSOR_ND;
  132. omg_context_.input_nodes_format_map.clear();
  133. omg_context_.output_formats.clear();
  134. omg_context_.user_input_dims.clear();
  135. omg_context_.input_dims.clear();
  136. omg_context_.op_conf_map.clear();
  137. omg_context_.out_nodes_map.clear();
  138. omg_context_.user_out_nodes.clear();
  139. omg_context_.net_format = domi::DOMI_TENSOR_RESERVED;
  140. omg_context_.type = domi::FRAMEWORK_RESERVED;
  141. omg_context_.run_mode = ONLY_PRE_CHECK;
  142. omg_context_.train_flag = false;
  143. omg_context_.output_type.clear();
  144. omg_context_.is_dynamic_input = false;
  145. omg_context_.dynamic_batch_size.clear();
  146. omg_context_.dynamic_image_size.clear();
  147. omg_context_.dynamic_dims.clear();
  148. };
  149. ~Impl() { (void)generator_.Finalize(); };
  150. graphStatus CheckOptions(const std::map<std::string, std::string> &options);
  151. graphStatus CreateInputsForIRBuild(const ge::Graph &graph, vector<ge::GeTensor> &inputs);
  152. graphStatus Init(const std::map<std::string, std::string> &options);
  153. graphStatus BuildModel(const Graph &graph, const std::map<std::string, std::string> &options,
  154. ModelBufferData &ge_models);
  155. graphStatus InitDomiOmgContext(const string &input_shape, const string &input_format, const string &net_format,
  156. bool is_dynamic_input);
  157. void SetRtSocVersion();
  158. public:
  159. ge::GeGenerator generator_;
  160. std::map<std::string, std::string> options_;
  161. bool is_dynamic_input_ = false;
  162. OmgContext omg_context_;
  163. };
  164. graphStatus Impl::CheckOptions(const std::map<std::string, std::string> &options) {
  165. for (auto &ele : options) {
  166. auto it = ge::ir_option::ir_builder_suppported_options.find(ele.first);
  167. if (it == ge::ir_option::ir_builder_suppported_options.end()) {
  168. auto it_lx_fusion = ir_builder_supported_options_for_lx_fusion.find(ele.first);
  169. if (it_lx_fusion == ir_builder_supported_options_for_lx_fusion.end()) {
  170. GELOGE(GRAPH_PARAM_INVALID, "input options include unsupported option(%s).Please check!",
  171. ele.first.c_str());
  172. return GRAPH_PARAM_INVALID;
  173. }
  174. }
  175. options_.insert(ele);
  176. }
  177. // Check options build_mode and build_step.
  178. std::string build_mode;
  179. auto it = options_.find(BUILD_MODE);
  180. if (it != options_.end() && !(it->second.empty())) {
  181. if (build_mode_options.find(it->second) == build_mode_options.end()) {
  182. GELOGE(GRAPH_PARAM_INVALID, "Build mode:%s is unsupported. Please check!", it->second.c_str());
  183. return GRAPH_PARAM_INVALID;
  184. }
  185. build_mode = it->second;
  186. }
  187. it = options_.find(BUILD_STEP);
  188. if (it != options_.end() && !(it->second.empty())) {
  189. if (build_step_options.find(it->second) == build_step_options.end()) {
  190. GELOGE(GRAPH_PARAM_INVALID, "Build step:%s is unsupported. Please check!", it->second.c_str());
  191. return GRAPH_PARAM_INVALID;
  192. }
  193. } else {
  194. if (build_mode == BUILD_MODE_TUNING) {
  195. GELOGE(GRAPH_PARAM_INVALID, "Build mode tuning must specify build step. Please check!");
  196. return GRAPH_PARAM_INVALID;
  197. }
  198. }
  199. return GRAPH_SUCCESS;
  200. }
  201. graphStatus Impl::Init(const std::map<std::string, std::string> &options) {
  202. // 1. check options
  203. graphStatus ret = CheckOptions(options);
  204. if (ret != GRAPH_SUCCESS) {
  205. GELOGE(ret, "User input options are illegal! Please check!");
  206. return ret;
  207. }
  208. GetThreadLocalContext().SetGlobalOption(GetMutableGlobalOptions());
  209. GetThreadLocalContext().SetGraphOption(options_);
  210. std::string build_mode = (options_.find(BUILD_MODE) == options_.end() || options_[BUILD_MODE] == BUILD_MODE_NORMAL)
  211. ? "" : options_[BUILD_MODE];
  212. options_[BUILD_MODE] = build_mode;
  213. // set log level
  214. std::string log = options_.find(ge::ir_option::LOG_LEVEL) == options_.end()
  215. ? IR_OPTION_LOG_LEVEL_DEFAULT
  216. : options_[ge::ir_option::LOG_LEVEL];
  217. GE_CHK_BOOL_RET_STATUS_NOLOG(ge::CheckLogParamValidAndSetLogLevel(log) == 0, GRAPH_PARAM_INVALID);
  218. options_[ge::ir_option::LOG_LEVEL] = log;
  219. string input_shape = options_.find("input_shape") == options_.end() ? "" : options_["input_shape"];
  220. string input_format = options_.find("input_format") == options_.end() ? "" : options_["input_format"];
  221. string net_format = options_.find("net_format") == options_.end() ? "" : options_["net_format"];
  222. string dynamic_batch_size = options_.find(ge::ir_option::DYNAMIC_BATCH_SIZE) == options_.end()
  223. ? ""
  224. : options_[ge::ir_option::DYNAMIC_BATCH_SIZE];
  225. string dynamic_image_size = options_.find(ge::ir_option::DYNAMIC_IMAGE_SIZE) == options_.end()
  226. ? ""
  227. : options_[ge::ir_option::DYNAMIC_IMAGE_SIZE];
  228. string dynamic_dims =
  229. options_.find(ge::ir_option::DYNAMIC_DIMS) == options_.end() ? "" : options_[ge::ir_option::DYNAMIC_DIMS];
  230. auto status = CheckDynamicInputParamValid(dynamic_batch_size, dynamic_image_size, dynamic_dims, input_shape,
  231. input_format, is_dynamic_input_);
  232. if (status != ge::SUCCESS) {
  233. GELOGE(GRAPH_PARAM_INVALID, "Check dynamic input size failed!");
  234. return GRAPH_PARAM_INVALID;
  235. }
  236. GELOGD("User input dynamic_batch_size:%s, dynamic_image_size:%s, dynamic_dims:%s.", dynamic_batch_size.c_str(),
  237. dynamic_image_size.c_str(), dynamic_dims.c_str());
  238. omg_context_.dynamic_batch_size = dynamic_batch_size;
  239. omg_context_.dynamic_image_size = dynamic_image_size;
  240. omg_context_.dynamic_dims = dynamic_dims;
  241. // check output_type
  242. std::string output_type = options_.find(ge::ir_option::OUTPUT_TYPE) == options_.end()
  243. ? ""
  244. : options_[ge::ir_option::OUTPUT_TYPE];
  245. GE_CHK_BOOL_EXEC(ge::CheckOutputTypeParamValid(output_type) == ge::SUCCESS,
  246. return ge::GRAPH_PARAM_INVALID, "check output type failed!");
  247. // check insert_op_conf
  248. std::string insert_op_conf = options_.find(ge::ir_option::INSERT_OP_FILE) == options_.end()
  249. ? ""
  250. : options_[ge::ir_option::INSERT_OP_FILE];
  251. GE_CHK_BOOL_EXEC(ge::CheckInsertOpConfParamValid(std::string(insert_op_conf)) == ge::SUCCESS,
  252. return ge::GRAPH_PARAM_INVALID, "check insert op conf failed!");
  253. GE_CHK_BOOL_EXEC(insert_op_conf.empty() || dynamic_dims.empty(),
  254. return ge::GRAPH_PARAM_INVALID, "dynamic dims function does not support aipp");
  255. // for IR builder.Only support om mode, so here fixed;
  256. options_.insert(std::pair<string, string>(string(IR_OPTION_MODE), to_string(0)));
  257. options_.insert(std::pair<string, string>(string(IR_OPTION_TARGET), "mini"));
  258. options_.insert(std::pair<string, string>(string(ge::RUN_FLAG), to_string(0)));
  259. options_.insert(std::pair<string, string>(string(ge::TRAIN_FLAG), to_string(0)));
  260. options_.insert(std::pair<string, string>(string(ge::SAVE_ORIGINAL_MODEL), to_string(0)));
  261. // print ge option map
  262. ge::PrintOptionMap(options_, "ge option");
  263. SetRtSocVersion();
  264. // 3. init generator with options_
  265. ret = generator_.Initialize(options_, omg_context_);
  266. if (ret != GRAPH_SUCCESS) {
  267. GELOGE(ret, "generator Initialize failed!");
  268. return ret;
  269. }
  270. // 4.parse and init Context with input shape format and net format info
  271. return this->InitDomiOmgContext(input_shape, input_format, net_format, is_dynamic_input_);
  272. }
  273. void Impl::SetRtSocVersion() {
  274. auto &global_options = GetMutableGlobalOptions();
  275. auto it = global_options.find(ge::SOC_VERSION);
  276. if (it != global_options.end()) {
  277. const char *soc_version = it->second.c_str();
  278. rtError_t rt_ret = rtSetSocVersion(soc_version);
  279. if (rt_ret != RT_ERROR_NONE) {
  280. GELOGW("Set soc version %s failed. ret:0x%X", soc_version, rt_ret);
  281. }
  282. GELOGI("Set soc version %s success.", soc_version);
  283. }
  284. }
  285. graphStatus Impl::CreateInputsForIRBuild(const ge::Graph &graph, vector<ge::GeTensor> &inputs) {
  286. auto compute_graph = ge::GraphUtils::GetComputeGraph(graph);
  287. GE_CHECK_NOTNULL(compute_graph);
  288. int64_t index = 0;
  289. for (ge::NodePtr &input_node : compute_graph->GetDirectNode()) {
  290. GE_CHECK_NOTNULL(input_node);
  291. ge::OpDescPtr op = input_node->GetOpDesc();
  292. GE_CHECK_NOTNULL(op);
  293. if (op->GetType() == DATA) {
  294. (void)AttrUtils::SetInt(op, ATTR_NAME_INDEX, index++);
  295. GELOGI("Data op inputDesc size: %zu", op->GetAllInputsDesc().size());
  296. ge::GeTensorDesc tensor = op->GetInputDesc(0);
  297. string data_op_name = op->GetName();
  298. GELOGI("Data op name: %s", data_op_name.c_str());
  299. ge::GeShape data_shape;
  300. auto iter = omg_context_.input_dims.find(data_op_name);
  301. if (iter != omg_context_.input_dims.end()) {
  302. data_shape = ge::GeShape(iter->second);
  303. GELOGI("Data op get shape from Context.");
  304. } else {
  305. data_shape = tensor.GetShape();
  306. GELOGI("Data op get shape from InputDesc in ge ir graph.");
  307. }
  308. ge::DataType data_type = tensor.GetDataType();
  309. string data_type_str = ge::TypeUtils::DataTypeToSerialString(data_type);
  310. GELOGI("Data op get data type:%s from InputDesc in ge ir graph.", data_type_str.c_str());
  311. ge::GeTensor inputTensor;
  312. ge::GeTensorDesc desc(data_shape, ge::Format(omg_context_.format), data_type);
  313. inputTensor.SetTensorDesc(desc);
  314. inputs.push_back(inputTensor);
  315. }
  316. }
  317. GELOGD("CreateInputsForIRBuild, inputs size: %zu", inputs.size());
  318. return GRAPH_SUCCESS;
  319. }
  320. graphStatus Impl::BuildModel(const Graph &graph, const std::map<std::string, std::string> &options,
  321. ModelBufferData &model) {
  322. // 1. init GeGenerator with user optios
  323. graphStatus ret = Init(options);
  324. if (ret != GRAPH_SUCCESS) {
  325. GELOGE(ret, "Build ir model Init failed!");
  326. return ret;
  327. }
  328. // 2. construct input
  329. std::vector<GeTensor> inputs;
  330. if (!omg_context_.is_dynamic_input) { // if dynamic input , no need to creat inputs
  331. ret = CreateInputsForIRBuild(graph, inputs);
  332. if (ret != GRAPH_SUCCESS) {
  333. GELOGE(ret, "CreateInputsForIRBuild failed!");
  334. return ret;
  335. }
  336. }
  337. // 3. build IR model
  338. ret = generator_.GenerateOnlineModel(graph, inputs, model);
  339. if (ret != GRAPH_SUCCESS) {
  340. GELOGE(ret, "GenerateOnlineModel failed!");
  341. return ret;
  342. }
  343. return GRAPH_SUCCESS;
  344. }
  345. graphStatus Impl::InitDomiOmgContext(const string &input_shape, const string &input_format, const string &net_format,
  346. bool is_dynamic_input) {
  347. // Clear omgcontext data first
  348. omg_context_.input_dims.clear();
  349. omg_context_.user_input_dims.clear();
  350. omg_context_.is_dynamic_input = is_dynamic_input;
  351. // the default value is ND
  352. omg_context_.format = domi::DOMI_TENSOR_ND;
  353. if (!input_format.empty()) {
  354. auto iter = ge::input_format_str_to_geformat.find(input_format);
  355. if (iter != ge::input_format_str_to_geformat.end()) {
  356. omg_context_.format = iter->second;
  357. } else {
  358. GELOGE(GRAPH_PARAM_INVALID, "Input format %s not support , expect ND/NCHW/NHWC/CHWN/NC1HWC0/NHWC1C0.",
  359. input_format.c_str());
  360. return GRAPH_PARAM_INVALID;
  361. }
  362. }
  363. // Input is empty, do not process
  364. if (input_shape.empty()) {
  365. return GRAPH_SUCCESS;
  366. }
  367. if (!ParseInputShape(input_shape, omg_context_.input_dims, omg_context_.user_input_dims, is_dynamic_input)) {
  368. GELOGE(GRAPH_PARAM_INVALID, "Failed to parse input shape: %s", input_shape.c_str());
  369. return GRAPH_PARAM_INVALID;
  370. }
  371. return GRAPH_SUCCESS;
  372. }
  373. graphStatus aclgrphBuildModel(const ge::Graph &graph, const std::map<std::string, std::string> &build_options,
  374. ModelBufferData &model) {
  375. GELOGD("Enter aclmdlBuildModel process!");
  376. Impl builder;
  377. return builder.BuildModel(graph, build_options, model);
  378. }
  379. graphStatus aclgrphSaveModel(const string &output_file, const ModelBufferData &model) {
  380. GELOGD("Enter aclmdlSaveModel process!");
  381. if (model.data.get() == nullptr || model.length == 0) {
  382. GELOGE(GRAPH_PARAM_INVALID, "input model is illegal");
  383. return GRAPH_PARAM_INVALID;
  384. }
  385. return FileSaver::SaveToFile((output_file + ".om"), reinterpret_cast<void*>(model.data.get()),
  386. static_cast<uint32_t>(model.length));
  387. }
  388. graphStatus aclgrphGetIRVersion(int *major_version, int *minor_version, int *patch_version) {
  389. GELOGD("Enter aclgrphGetIRVersion process!");
  390. GE_CHECK_NOTNULL(major_version);
  391. GE_CHECK_NOTNULL(minor_version);
  392. GE_CHECK_NOTNULL(patch_version);
  393. *major_version = IR_MAJOR_VERSION;
  394. *minor_version = IR_MINOR_VERSION;
  395. *patch_version = IR_PATCH_VERSION;
  396. return GRAPH_SUCCESS;
  397. }
  398. graphStatus aclgrphInferShapeAndType(ge::Graph &graph) {
  399. auto compute_graph = GraphUtils::GetComputeGraph(graph);
  400. GE_CHECK_NOTNULL(compute_graph);
  401. for (auto &node: compute_graph->GetAllNodes()) {
  402. graphStatus ret = ShapeRefiner::InferShapeAndType(node);
  403. if (ret == GRAPH_PARAM_INVALID) {
  404. GELOGW("Can not find infershape func.");
  405. continue;
  406. } else if (ret != GRAPH_SUCCESS) {
  407. GELOGE(ret, "Acl infershape failed.");
  408. return ret;
  409. }
  410. }
  411. return GRAPH_SUCCESS;
  412. }
  413. graphStatus aclgrphDumpGraph(const ge::Graph &graph, const char *file, const size_t len) {
  414. GE_CHECK_NOTNULL(file);
  415. if (len > PATH_MAX || len != strlen(file) || strlen(file) == 0) {
  416. GELOGE(GRAPH_PARAM_INVALID, "File path invalid.");
  417. return GRAPH_PARAM_INVALID;
  418. }
  419. auto compute_graph = GraphUtils::GetComputeGraph(graph);
  420. GE_CHECK_NOTNULL(compute_graph);
  421. string full_path(file, len);
  422. for (size_t i = 0; i < len; i++) {
  423. if (full_path[i] == '\\') {
  424. full_path.replace(i, 1, "/");
  425. }
  426. }
  427. string suffix;
  428. string file_path;
  429. int pos = full_path.rfind("/");
  430. if (pos != -1) {
  431. suffix = full_path.substr(pos + 1, -1);
  432. file_path = full_path.substr(0, pos);
  433. } else {
  434. suffix = full_path;
  435. file_path = "./";
  436. }
  437. if (suffix.empty()) {
  438. suffix = compute_graph->GetName();
  439. if (suffix.empty()) {
  440. suffix = "graph";
  441. }
  442. }
  443. char path[PATH_MAX] = {0};
  444. if (realpath(file_path.c_str(), path) == nullptr) {
  445. GELOGE(GRAPH_PARAM_INVALID, "Dump file path:%s is invalid.", file);
  446. return GRAPH_PARAM_INVALID;
  447. }
  448. GraphUtils::DumpGEGrph(compute_graph, string(path), suffix);
  449. GraphUtils::DumpGrphToOnnx(*compute_graph, string(path), suffix);
  450. uint64_t i = 0;
  451. for (const auto &sub_graph_func : compute_graph->GetAllSubgraphs()) {
  452. auto sub_graph_func_name = suffix + std::string("_sub_graph_") + std::to_string(i++);
  453. GraphUtils::DumpGEGrph(sub_graph_func, string(path), sub_graph_func_name);
  454. GraphUtils::DumpGrphToOnnx(*sub_graph_func, string(path), sub_graph_func_name);
  455. }
  456. return GRAPH_SUCCESS;
  457. }
  458. } // namespace ge

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