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.

graph_optimize.cc 23 kB

5 years ago
5 years ago
5 years ago
3 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
3 years ago
5 years ago
4 years ago
4 years ago
5 years ago
3 years ago
5 years ago
4 years ago
4 years ago
5 years ago
3 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
3 years ago
5 years ago
4 years ago
5 years ago
3 years ago
5 years ago
5 years ago
4 years ago
5 years ago
3 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 years ago
5 years ago
3 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 "graph/optimize/graph_optimize.h"
  17. #include "graph/ge_context.h"
  18. #include "graph/passes/dimension_adjust_pass.h"
  19. #include "inc/pass_manager.h"
  20. #include "init/gelib.h"
  21. #include "graph/partition/engine_place.h"
  22. namespace {
  23. const char *const kVectorCore = "VectorCore";
  24. const char *const kVectorEngine = "VectorEngine";
  25. const char *const kAicoreEngine = "AIcoreEngine";
  26. const char *const kHostCpuEngine = "DNN_VM_HOST_CPU";
  27. } // namespace
  28. namespace ge {
  29. GraphOptimize::GraphOptimize()
  30. : optimize_type_(domi::FrameworkType::TENSORFLOW),
  31. cal_config_(""),
  32. insert_op_config_(""),
  33. core_type_("") {}
  34. void AddNodeInputProperty(ComputeGraphPtr &compute_graph) {
  35. if (compute_graph == nullptr) {
  36. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  37. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  38. return;
  39. }
  40. for (ge::NodePtr &node : compute_graph->GetDirectNode()) {
  41. auto node_op_desc = node->GetOpDesc();
  42. GE_IF_BOOL_EXEC(node_op_desc == nullptr, GELOGW("node_op_desc is nullptr!"); return );
  43. auto in_control_anchor = node->GetInControlAnchor();
  44. vector<string> src_name_list;
  45. vector<string> input_name_list;
  46. vector<int64_t> src_index_list;
  47. GE_IF_BOOL_EXEC(
  48. in_control_anchor != nullptr, string src_name_temp; for (auto &out_control_anchor
  49. : in_control_anchor->GetPeerOutControlAnchors()) {
  50. ge::NodePtr src_node = out_control_anchor->GetOwnerNode();
  51. GE_IF_BOOL_EXEC(src_node == nullptr, GELOGW("src_node is nullptr!"); continue);
  52. src_name_temp = src_name_temp == "" ? src_node->GetName() : src_name_temp + ":" + src_node->GetName();
  53. } GE_IF_BOOL_EXEC(src_name_temp != "", src_name_list.emplace_back(src_name_temp);
  54. node_op_desc->SetSrcName(src_name_list);))
  55. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  56. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  57. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  58. ge::NodePtr src_node = peer_out_anchor->GetOwnerNode();
  59. src_index_list = node_op_desc->GetSrcIndex();
  60. src_name_list.emplace_back(src_node->GetName());
  61. src_index_list.emplace_back(peer_out_anchor->GetIdx());
  62. node_op_desc->SetSrcName(src_name_list);
  63. node_op_desc->SetSrcIndex(src_index_list);
  64. GE_IF_BOOL_EXEC(!(node_op_desc->GetType() == NETOUTPUT && GetLocalOmgContext().type == domi::TENSORFLOW),
  65. ge::NodePtr peer_owner_node = peer_out_anchor->GetOwnerNode();
  66. input_name_list.emplace_back(
  67. peer_owner_node->GetName() +
  68. (peer_out_anchor->GetIdx() == 0 ? "" : ": " + to_string(peer_out_anchor->GetIdx())));
  69. node_op_desc->SetInputName(input_name_list);)
  70. }
  71. }
  72. }
  73. Status GraphOptimize::OptimizeSubGraph(ComputeGraphPtr &compute_graph, const std::string &engine_name) {
  74. if (compute_graph == nullptr) {
  75. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  76. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  77. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  78. }
  79. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  80. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  81. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s",
  82. compute_graph->GetName().c_str());
  83. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s",
  84. compute_graph->GetName().c_str());
  85. return GE_CLI_GE_NOT_INITIALIZED;
  86. }
  87. vector<GraphOptimizerPtr> graph_optimizer;
  88. if (instance_ptr->DNNEngineManagerObj().IsEngineRegistered(engine_name)) {
  89. instance_ptr->OpsKernelManagerObj().GetGraphOptimizerByEngine(engine_name, graph_optimizer);
  90. AddNodeInputProperty(compute_graph);
  91. if (compute_graph->GetDirectNode().size() == 0) {
  92. GELOGW("[OptimizeSubGraph] compute_graph do not has any node.");
  93. return SUCCESS;
  94. }
  95. if (build_mode_ == BUILD_MODE_TUNING && (build_step_ == BUILD_STEP_AFTER_UB_MATCH
  96. || build_step_ == BUILD_STEP_AFTER_MERGE)) {
  97. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  98. Status ret = (*iter)->OptimizeFusedGraphAfterGraphSlice(*(compute_graph));
  99. if (ret != SUCCESS) {
  100. REPORT_INNER_ERROR("E19999", "Call OptimizeFusedGraphAfterGraphSlice failed, ret:%d, engine_name:%s, "
  101. "graph_name:%s", ret, engine_name.c_str(),
  102. compute_graph->GetName().c_str());
  103. GELOGE(ret, "[Call][OptimizeFusedGraphAfterGraphSlice] failed, ret:%d, engine_name:%s, graph_name:%s",
  104. ret, engine_name.c_str(), compute_graph->GetName().c_str());
  105. return ret;
  106. }
  107. }
  108. return SUCCESS;
  109. }
  110. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  111. Status ret = (*iter)->OptimizeFusedGraph(*(compute_graph));
  112. if (ret != SUCCESS) {
  113. REPORT_INNER_ERROR("E19999", "Call OptimizeFusedGraph failed, ret:%d, engine_name:%s, "
  114. "graph_name:%s", ret, engine_name.c_str(),
  115. compute_graph->GetName().c_str());
  116. GELOGE(ret, "[Optimize][FusedGraph] failed, ret:%d, engine_name:%s, graph_name:%s",
  117. ret, engine_name.c_str(), compute_graph->GetName().c_str());
  118. return ret;
  119. }
  120. }
  121. } else {
  122. GELOGI("Engine: %s is not registered. do nothing in subGraph Optimize by ATC.", engine_name.c_str());
  123. }
  124. return SUCCESS;
  125. }
  126. Status GraphOptimize::OptimizeOriginalGraph(ComputeGraphPtr &compute_graph) {
  127. if (compute_graph == nullptr) {
  128. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  129. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  130. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  131. }
  132. Status ret = SUCCESS;
  133. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  134. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  135. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  136. compute_graph->GetName().c_str());
  137. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  138. compute_graph->GetName().c_str());
  139. return GE_CLI_GE_NOT_INITIALIZED;
  140. }
  141. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  142. GELOGI("optimize by opskernel in original graph optimize phase. num of graph_optimizer is %zu.",
  143. graph_optimizer.size());
  144. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  145. GELOGD("[OptimizeOriginalGraph]: engine type will exclude: %s", exclude_core_Type.c_str());
  146. if (graph_optimizer.size() != 0) {
  147. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  148. if (iter->first == exclude_core_Type) {
  149. continue;
  150. }
  151. if (GetContext().GetHostExecFlag() && iter->first != kHostCpuEngine) {
  152. // graph exec on host, no need OptimizeOriginalGraph for other engine.
  153. continue;
  154. }
  155. ret = (iter->second)->OptimizeOriginalGraph(*compute_graph);
  156. if (ret != SUCCESS) {
  157. REPORT_INNER_ERROR("E19999", "Call OptimizeOriginalGraph failed, ret:%d, engine_name:%s, "
  158. "graph_name:%s", ret, iter->first.c_str(),
  159. compute_graph->GetName().c_str());
  160. GELOGE(ret, "[Optimize][OriginalGraph] failed, ret:%d, engine_name:%s, graph_name:%s",
  161. ret, iter->first.c_str(), compute_graph->GetName().c_str());
  162. return ret;
  163. }
  164. }
  165. }
  166. return ret;
  167. }
  168. Status GraphOptimize::OptimizeOriginalGraphJudgeInsert(ComputeGraphPtr &compute_graph) {
  169. GELOGD("OptimizeOriginalGraphJudgeInsert in");
  170. GE_CHECK_NOTNULL(compute_graph);
  171. Status ret = SUCCESS;
  172. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  173. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  174. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s",
  175. compute_graph->GetName().c_str());
  176. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s",
  177. compute_graph->GetName().c_str());
  178. return GE_CLI_GE_NOT_INITIALIZED;
  179. }
  180. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  181. GELOGI("optimize by opskernel in judging insert phase. num of graph_optimizer is %zu.",
  182. graph_optimizer.size());
  183. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  184. if (graph_optimizer.size() != 0) {
  185. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  186. if (iter->first == exclude_core_Type) {
  187. GELOGI("[OptimizeOriginalGraphJudgeInsert]: engine type will exclude: %s", exclude_core_Type.c_str());
  188. continue;
  189. }
  190. if (GetContext().GetHostExecFlag() && iter->first != kHostCpuEngine) {
  191. // graph exec on host, no need OptimizeOriginalGraphJudgeInsert for other engine.
  192. continue;
  193. }
  194. GELOGI("Begin to refine running format by engine %s", iter->first.c_str());
  195. ret = (iter->second)->OptimizeOriginalGraphJudgeInsert(*compute_graph);
  196. if (ret != SUCCESS) {
  197. REPORT_INNER_ERROR("E19999", "Call OptimizeOriginalGraphJudgeInsert failed, ret:%d, engine_name:%s, "
  198. "graph_name:%s", ret, iter->first.c_str(),
  199. compute_graph->GetName().c_str());
  200. GELOGE(ret, "[Call][OptimizeOriginalGraphJudgeInsert] failed, ret:%d, engine_name:%s, graph_name:%s",
  201. ret, iter->first.c_str(), compute_graph->GetName().c_str());
  202. return ret;
  203. }
  204. }
  205. }
  206. return ret;
  207. }
  208. Status GraphOptimize::OptimizeOriginalGraphForQuantize(ComputeGraphPtr &compute_graph) {
  209. if (compute_graph == nullptr) {
  210. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  211. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  212. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  213. }
  214. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  215. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  216. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  217. compute_graph->GetName().c_str());
  218. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][Gelib] Gelib not init before, graph:%s.",
  219. compute_graph->GetName().c_str());
  220. return GE_CLI_GE_NOT_INITIALIZED;
  221. }
  222. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  223. GELOGI("optimize by opskernel in original graph optimize quantize phase. num of graph_optimizer is %zu.",
  224. graph_optimizer.size());
  225. Status ret = SUCCESS;
  226. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  227. GELOGD("[OptimizeOriginalGraphForQuantize]: engine type will exclude: %s", exclude_core_Type.c_str());
  228. if (graph_optimizer.size() != 0) {
  229. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  230. if (iter->first == exclude_core_Type || iter->second == nullptr) {
  231. continue;
  232. }
  233. ret = iter->second->OptimizeGraphPrepare(*compute_graph);
  234. if (ret != SUCCESS) {
  235. REPORT_INNER_ERROR("E19999", "Call OptimizeGraphPrepare failed, ret:%d, engine_name:%s, "
  236. "graph_name:%s", ret, iter->first.c_str(),
  237. compute_graph->GetName().c_str());
  238. GELOGE(ret, "[Call][OptimizeGraphPrepare] failed, ret:%d, engine_name:%s, graph_name:%s",
  239. ret, iter->first.c_str(), compute_graph->GetName().c_str());
  240. return ret;
  241. }
  242. }
  243. }
  244. return ret;
  245. }
  246. Status GraphOptimize::OptimizeGraphBeforeBuild(ComputeGraphPtr &compute_graph) {
  247. if (compute_graph == nullptr) {
  248. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  249. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  250. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  251. }
  252. EnginePlacer engine_place(compute_graph);
  253. Status ret = engine_place.Run();
  254. if (ret != SUCCESS) {
  255. REPORT_CALL_ERROR("E19999", "Assign atomic engine for graph %s failed", compute_graph->GetName().c_str());
  256. GELOGE(ret, "[Assign][Engine] Assign atomic engine for graph %s failed", compute_graph->GetName().c_str());
  257. return ret;
  258. }
  259. ret = engine_place.AssignCompoundEngine();
  260. if (ret != SUCCESS) {
  261. REPORT_CALL_ERROR("E19999", "Assign compound engine for graph %s failed", compute_graph->GetName().c_str());
  262. GELOGE(ret, "[Assign][Engine] Assign compound engine for graph %s failed", compute_graph->GetName().c_str());
  263. return ret;
  264. }
  265. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  266. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  267. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  268. compute_graph->GetName().c_str());
  269. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  270. compute_graph->GetName().c_str());
  271. return GE_CLI_GE_NOT_INITIALIZED;
  272. }
  273. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  274. GELOGD("optimize by opskernel in graph optimize before build phase. num of graph_optimizer is %zu.",
  275. graph_optimizer.size());
  276. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  277. GELOGD("[OptimizeGraphBeforeBuild]: engine type will exclude: %s, core_type_: %s",
  278. exclude_core_Type.c_str(), core_type_.c_str());
  279. if (graph_optimizer.size() != 0) {
  280. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  281. if (iter->first == exclude_core_Type || iter->second == nullptr) {
  282. continue;
  283. }
  284. ret = iter->second->OptimizeGraphBeforeBuild(*compute_graph);
  285. if (ret != SUCCESS) {
  286. REPORT_INNER_ERROR("E19999", "Call OptimizeGraphBeforeBuild failed, ret:%d, engine_name:%s, "
  287. "graph_name:%s", ret, iter->first.c_str(),
  288. compute_graph->GetName().c_str());
  289. GELOGE(ret, "[Call][OptimizeGraphBeforeBuild] failed, ret:%d, engine_name:%s, graph_name:%s",
  290. ret, iter->first.c_str(), compute_graph->GetName().c_str());
  291. return ret;
  292. }
  293. }
  294. }
  295. return SUCCESS;
  296. }
  297. Status GraphOptimize::OptimizeAfterStage1(ComputeGraphPtr &compute_graph) {
  298. GE_CHECK_NOTNULL(compute_graph);
  299. GELOGD("OptimizeAfterStage1 in");
  300. if (GetContext().GetHostExecFlag()) {
  301. // graph exec on host, no need OptimizeAfterStage1
  302. return SUCCESS;
  303. }
  304. Status ret = SUCCESS;
  305. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  306. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  307. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid");
  308. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeAfterStage1 failed.");
  309. return GE_CLI_GE_NOT_INITIALIZED;
  310. }
  311. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  312. GELOGI("Optimize by ops kernel in after stage1 phase, num of graph_optimizer is %zu.", graph_optimizer.size());
  313. string exclude_core_type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  314. if (graph_optimizer.size() != 0) {
  315. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  316. if (iter->first == exclude_core_type) {
  317. GELOGI("[OptimizeAfterStage1]: engine type will exclude:%s.", exclude_core_type.c_str());
  318. continue;
  319. }
  320. GELOGI("Begin to optimize graph after stage1 by engine %s.", iter->first.c_str());
  321. ret = (iter->second)->OptimizeAfterStage1(*compute_graph);
  322. if (ret != SUCCESS) {
  323. REPORT_INNER_ERROR("E19999", "Call OptimizeAfterStage1 failed, ret:%d, engine_name:%s, "
  324. "graph_name:%s.", ret, iter->first.c_str(), compute_graph->GetName().c_str());
  325. GELOGE(ret, "[OptimizeAfterStage1]: graph optimize failed, ret:%d.", ret);
  326. return ret;
  327. }
  328. }
  329. }
  330. return ret;
  331. }
  332. Status GraphOptimize::SetOptions(const ge::GraphManagerOptions &options) {
  333. if (options.framework_type >= static_cast<int32_t>(domi::FrameworkType::FRAMEWORK_RESERVED)) {
  334. REPORT_INNER_ERROR("E19999", "Param framework_type:%d in option check invalid",
  335. options.framework_type);
  336. GELOGE(GE_GRAPH_OPTIONS_INVALID, "Optimize Type %d invalid.", options.framework_type);
  337. return GE_GRAPH_OPTIONS_INVALID;
  338. }
  339. optimize_type_ = static_cast<domi::FrameworkType>(options.framework_type);
  340. cal_config_ = options.calibration_conf_file;
  341. insert_op_config_ = options.insert_op_file;
  342. train_graph_flag_ = options.train_graph_flag;
  343. local_fmk_op_flag_ = options.local_fmk_op_flag;
  344. func_bin_path_ = options.func_bin_path;
  345. core_type_ = options.core_type;
  346. build_mode_ = options.build_mode;
  347. build_step_ = options.build_step;
  348. return SUCCESS;
  349. }
  350. void GraphOptimize::TranFrameOp(ComputeGraphPtr &compute_graph) {
  351. GE_CHECK_NOTNULL_JUST_RETURN(compute_graph);
  352. vector<string> local_framework_op_vec = {
  353. "TensorDataset", "QueueDataset", "DeviceQueueDataset", "ParallelMapDataset", "BatchDatasetV2",
  354. "IteratorV2", "MakeIterator", "IteratorGetNext", "FilterDataset", "MapAndBatchDatasetV2"};
  355. for (auto &nodePtr : compute_graph->GetAllNodes()) {
  356. OpDescPtr op = nodePtr->GetOpDesc();
  357. GE_IF_BOOL_EXEC(op == nullptr, GELOGW("op is nullptr!"); continue);
  358. // fwkop black-white sheet
  359. vector<string>::iterator iter =
  360. std::find(local_framework_op_vec.begin(), local_framework_op_vec.end(), op->GetType());
  361. if (iter != local_framework_op_vec.end()) {
  362. // set - original_type
  363. if (!AttrUtils::SetStr(op, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, op->GetType())) {
  364. GELOGW("TranFrameOp SetStr ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE failed");
  365. }
  366. // set - framework_type
  367. // [No need to verify return value]
  368. op->SetType("FrameworkOp");
  369. if (!AttrUtils::SetInt(op, ATTR_NAME_FRAMEWORK_FWK_TYPE, domi::FrameworkType::TENSORFLOW)) {
  370. GELOGW("TranFrameOp SetInt ATTR_NAME_FRAMEWORK_FWK_TYPE failed");
  371. }
  372. }
  373. }
  374. }
  375. Status GraphOptimize::IdentifyReference(ComputeGraphPtr &compute_graph) {
  376. for (auto &node : compute_graph->GetAllNodes()) {
  377. GE_CHECK_NOTNULL(node);
  378. auto op_desc = node->GetOpDesc();
  379. GE_CHECK_NOTNULL(op_desc);
  380. auto input_name_index = op_desc->GetAllInputName();
  381. bool is_ref = false;
  382. for (const auto &name_index : input_name_index) {
  383. const int out_index = op_desc->GetOutputIndexByName(name_index.first);
  384. if (out_index != -1) {
  385. auto input_desc = op_desc->GetInputDesc(name_index.second);
  386. input_desc.SetRefPortByIndex({name_index.second});
  387. op_desc->UpdateInputDesc(name_index.second, input_desc);
  388. GELOGI("SetRefPort: set op[%s] input desc[%u-%s] ref.",
  389. op_desc->GetName().c_str(), name_index.second, name_index.first.c_str());
  390. auto output_desc = op_desc->GetOutputDesc(static_cast<uint32_t>(out_index));
  391. output_desc.SetRefPortByIndex({name_index.second});
  392. op_desc->UpdateOutputDesc(static_cast<uint32_t>(out_index), output_desc);
  393. GELOGI("SetRefPort: set op[%s] output desc[%u-%s] ref.",
  394. op_desc->GetName().c_str(), out_index, name_index.first.c_str());
  395. is_ref = true;
  396. }
  397. }
  398. if (is_ref) {
  399. AttrUtils::SetBool(op_desc, ATTR_NAME_REFERENCE, is_ref);
  400. GELOGI("param [node] %s is reference node, set attribute %s to be true.",
  401. node->GetName().c_str(), ATTR_NAME_REFERENCE.c_str());
  402. }
  403. }
  404. return SUCCESS;
  405. }
  406. Status GraphOptimize::OptimizeWholeGraph(ComputeGraphPtr &compute_graph) {
  407. if (compute_graph == nullptr) {
  408. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  409. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  410. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  411. }
  412. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  413. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  414. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  415. compute_graph->GetName().c_str());
  416. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  417. compute_graph->GetName().c_str());
  418. return GE_CLI_GE_NOT_INITIALIZED;
  419. }
  420. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  421. GELOGI("optimize by opskernel in OptimizeWholeGraph. num of graph_optimizer is %zu.", graph_optimizer.size());
  422. Status ret = SUCCESS;
  423. string exclude_core_type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  424. GELOGD("[OptimizeWholeGraph]: engine type will exclude: %s", exclude_core_type.c_str());
  425. if (!graph_optimizer.empty()) {
  426. for (auto &iter : graph_optimizer) {
  427. if (iter.first == exclude_core_type || iter.second == nullptr) {
  428. continue;
  429. }
  430. GELOGI("Begin to optimize whole graph by engine %s", iter.first.c_str());
  431. ret = iter.second->OptimizeWholeGraph(*compute_graph);
  432. GE_DUMP(compute_graph, "OptimizeWholeGraph" + iter.first);
  433. if (ret != SUCCESS) {
  434. REPORT_INNER_ERROR("E19999", "Call OptimizeWholeGraph failed, ret:%d, engine_name:%s, "
  435. "graph_name:%s", ret, iter.first.c_str(),
  436. compute_graph->GetName().c_str());
  437. GELOGE(ret, "[Call][OptimizeWholeGraph] failed, ret:%d, engine_name:%s, graph_name:%s",
  438. ret, iter.first.c_str(), compute_graph->GetName().c_str());
  439. return ret;
  440. }
  441. }
  442. }
  443. return ret;
  444. }
  445. } // namespace ge

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