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

5 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
4 years ago
4 years ago
5 years ago
4 years ago
4 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
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
5 years ago
5 years ago
4 years ago
4 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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/common/local_context.h"
  19. #include "graph/passes/dimension_adjust_pass.h"
  20. #include "inc/pass_manager.h"
  21. #include "init/gelib.h"
  22. namespace {
  23. const char *const kVectorCore = "VectorCore";
  24. const char *const kVectorEngine = "VectorEngine";
  25. const char *const kAicoreEngine = "AIcoreEngine";
  26. } // namespace
  27. namespace ge {
  28. GraphOptimize::GraphOptimize()
  29. : optimize_type_(domi::FrameworkType::TENSORFLOW),
  30. cal_config_(""),
  31. insert_op_config_(""),
  32. core_type_("") {}
  33. void AddNodeInputProperty(ComputeGraphPtr &compute_graph) {
  34. if (compute_graph == nullptr) {
  35. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  36. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  37. return;
  38. }
  39. for (ge::NodePtr &node : compute_graph->GetDirectNode()) {
  40. auto node_op_desc = node->GetOpDesc();
  41. GE_IF_BOOL_EXEC(node_op_desc == nullptr, GELOGW("node_op_desc is nullptr!"); return );
  42. auto in_control_anchor = node->GetInControlAnchor();
  43. vector<string> src_name_list;
  44. vector<string> input_name_list;
  45. vector<int64_t> src_index_list;
  46. GE_IF_BOOL_EXEC(
  47. in_control_anchor != nullptr, string src_name_temp; for (auto &out_control_anchor
  48. : in_control_anchor->GetPeerOutControlAnchors()) {
  49. ge::NodePtr src_node = out_control_anchor->GetOwnerNode();
  50. GE_IF_BOOL_EXEC(src_node == nullptr, GELOGW("src_node is nullptr!"); continue);
  51. src_name_temp = src_name_temp == "" ? src_node->GetName() : src_name_temp + ":" + src_node->GetName();
  52. } GE_IF_BOOL_EXEC(src_name_temp != "", src_name_list.emplace_back(src_name_temp);
  53. node_op_desc->SetSrcName(src_name_list);))
  54. for (auto &in_data_anchor : node->GetAllInDataAnchors()) {
  55. auto peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  56. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  57. ge::NodePtr src_node = peer_out_anchor->GetOwnerNode();
  58. src_index_list = node_op_desc->GetSrcIndex();
  59. src_name_list.emplace_back(src_node->GetName());
  60. src_index_list.emplace_back(peer_out_anchor->GetIdx());
  61. node_op_desc->SetSrcName(src_name_list);
  62. node_op_desc->SetSrcIndex(src_index_list);
  63. GE_IF_BOOL_EXEC(!(node_op_desc->GetType() == NETOUTPUT && GetLocalOmgContext().type == domi::TENSORFLOW),
  64. ge::NodePtr peer_owner_node = peer_out_anchor->GetOwnerNode();
  65. input_name_list.emplace_back(
  66. peer_owner_node->GetName() +
  67. (peer_out_anchor->GetIdx() == 0 ? "" : ": " + to_string(peer_out_anchor->GetIdx())));
  68. node_op_desc->SetInputName(input_name_list);)
  69. }
  70. }
  71. }
  72. Status GraphOptimize::OptimizeSubGraph(ComputeGraphPtr &compute_graph, const std::string &engine_name) {
  73. if (compute_graph == nullptr) {
  74. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  75. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  76. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  77. }
  78. Status ret = SUCCESS;
  79. vector<GraphOptimizerPtr> graph_optimizer;
  80. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  81. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  82. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s",
  83. compute_graph->GetName().c_str());
  84. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s",
  85. compute_graph->GetName().c_str());
  86. return GE_CLI_GE_NOT_INITIALIZED;
  87. }
  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. 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 ret;
  125. }
  126. Status GraphOptimize::OptimizeOriginalGraph(ComputeGraphPtr &compute_graph) {
  127. if (GetContext().GetHostExecFlag()) {
  128. // graph exec on host, no need OptimizeOriginalGraph
  129. return SUCCESS;
  130. }
  131. if (compute_graph == nullptr) {
  132. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  133. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  134. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  135. }
  136. Status ret = SUCCESS;
  137. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  138. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  139. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  140. compute_graph->GetName().c_str());
  141. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  142. compute_graph->GetName().c_str());
  143. return GE_CLI_GE_NOT_INITIALIZED;
  144. }
  145. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  146. GELOGI("optimize by opskernel in original graph optimize phase. num of graph_optimizer is %zu.",
  147. graph_optimizer.size());
  148. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  149. GELOGD("[OptimizeOriginalGraph]: engine type will exclude: %s", exclude_core_Type.c_str());
  150. if (graph_optimizer.size() != 0) {
  151. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  152. if (iter->first == exclude_core_Type) {
  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. if (GetContext().GetHostExecFlag()) {
  171. // graph exec on host, no need OptimizeOriginalGraphJudgeInsert
  172. return SUCCESS;
  173. }
  174. GE_CHECK_NOTNULL(compute_graph);
  175. Status ret = SUCCESS;
  176. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  177. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  178. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s",
  179. compute_graph->GetName().c_str());
  180. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s",
  181. compute_graph->GetName().c_str());
  182. return GE_CLI_GE_NOT_INITIALIZED;
  183. }
  184. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  185. GELOGI("optimize by opskernel in judging insert phase. num of graph_optimizer is %zu.",
  186. graph_optimizer.size());
  187. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  188. if (graph_optimizer.size() != 0) {
  189. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  190. if (iter->first == exclude_core_Type) {
  191. GELOGI("[OptimizeOriginalGraphJudgeInsert]: engine type will exclude: %s", exclude_core_Type.c_str());
  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::OptimizeGraphBeforeBuildForRts(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. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  253. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  254. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  255. compute_graph->GetName().c_str());
  256. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  257. compute_graph->GetName().c_str());
  258. return GE_CLI_GE_NOT_INITIALIZED;
  259. }
  260. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  261. GELOGD("optimize by opskernel in graph optimize before build phase. num of graph_optimizer is %zu.",
  262. graph_optimizer.size());
  263. Status ret = SUCCESS;
  264. string exclude_core_Type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  265. GELOGD("[OptimizeGraphBeforeBuildForRts]: engine type will exclude: %s, core_type_: %s",
  266. exclude_core_Type.c_str(), core_type_.c_str());
  267. if (graph_optimizer.size() != 0) {
  268. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  269. if (iter->first == exclude_core_Type || iter->second == nullptr) {
  270. continue;
  271. }
  272. ret = iter->second->OptimizeGraphBeforeBuild(*compute_graph);
  273. if (ret != SUCCESS) {
  274. REPORT_INNER_ERROR("E19999", "Call OptimizeGraphBeforeBuild failed, ret:%d, engine_name:%s, "
  275. "graph_name:%s", ret, iter->first.c_str(),
  276. compute_graph->GetName().c_str());
  277. GELOGE(ret, "[Call][OptimizeGraphBeforeBuild] failed, ret:%d, engine_name:%s, graph_name:%s",
  278. ret, iter->first.c_str(), compute_graph->GetName().c_str());
  279. return ret;
  280. }
  281. }
  282. }
  283. return ret;
  284. }
  285. Status GraphOptimize::OptimizeAfterStage1(ComputeGraphPtr &compute_graph) {
  286. GE_CHECK_NOTNULL(compute_graph);
  287. GELOGD("OptimizeAfterStage1 in");
  288. if (GetContext().GetHostExecFlag()) {
  289. // graph exec on host, no need OptimizeAfterStage1
  290. return SUCCESS;
  291. }
  292. Status ret = SUCCESS;
  293. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  294. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  295. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid");
  296. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "OptimizeAfterStage1 failed.");
  297. return GE_CLI_GE_NOT_INITIALIZED;
  298. }
  299. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  300. GELOGI("Optimize by ops kernel in after stage1 phase, num of graph_optimizer is %zu.", graph_optimizer.size());
  301. string exclude_core_type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  302. if (graph_optimizer.size() != 0) {
  303. for (auto iter = graph_optimizer.begin(); iter != graph_optimizer.end(); ++iter) {
  304. if (iter->first == exclude_core_type) {
  305. GELOGI("[OptimizeAfterStage1]: engine type will exclude:%s.", exclude_core_type.c_str());
  306. continue;
  307. }
  308. #ifndef ONLY_COMPILE_OPEN_SRC
  309. GELOGI("Begin to optimize graph after stage1 by engine %s.", iter->first.c_str());
  310. ret = (iter->second)->OptimizeAfterStage1(*compute_graph);
  311. #endif
  312. if (ret != SUCCESS) {
  313. REPORT_INNER_ERROR("E19999", "Call OptimizeAfterStage1 failed, ret:%d, engine_name:%s, "
  314. "graph_name:%s.", ret, iter->first.c_str(), compute_graph->GetName().c_str());
  315. GELOGE(ret, "[OptimizeAfterStage1]: graph optimize failed, ret:%d.", ret);
  316. return ret;
  317. }
  318. }
  319. }
  320. return ret;
  321. }
  322. Status GraphOptimize::SetOptions(const ge::GraphManagerOptions &options) {
  323. if (options.framework_type >= static_cast<int32_t>(domi::FrameworkType::FRAMEWORK_RESERVED)) {
  324. REPORT_INNER_ERROR("E19999", "Param framework_type:%d in option check invalid",
  325. options.framework_type);
  326. GELOGE(GE_GRAPH_OPTIONS_INVALID, "Optimize Type %d invalid.", options.framework_type);
  327. return GE_GRAPH_OPTIONS_INVALID;
  328. }
  329. optimize_type_ = static_cast<domi::FrameworkType>(options.framework_type);
  330. cal_config_ = options.calibration_conf_file;
  331. insert_op_config_ = options.insert_op_file;
  332. train_graph_flag_ = options.train_graph_flag;
  333. local_fmk_op_flag_ = options.local_fmk_op_flag;
  334. func_bin_path_ = options.func_bin_path;
  335. core_type_ = options.core_type;
  336. build_mode_ = options.build_mode;
  337. build_step_ = options.build_step;
  338. return SUCCESS;
  339. }
  340. void GraphOptimize::TranFrameOp(ComputeGraphPtr &compute_graph) {
  341. GE_CHECK_NOTNULL_JUST_RETURN(compute_graph);
  342. vector<string> local_framework_op_vec = {
  343. "TensorDataset", "QueueDataset", "DeviceQueueDataset", "ParallelMapDataset", "BatchDatasetV2",
  344. "IteratorV2", "MakeIterator", "IteratorGetNext", "FilterDataset", "MapAndBatchDatasetV2"};
  345. for (auto &nodePtr : compute_graph->GetAllNodes()) {
  346. OpDescPtr op = nodePtr->GetOpDesc();
  347. GE_IF_BOOL_EXEC(op == nullptr, GELOGW("op is nullptr!"); continue);
  348. // fwkop black-white sheet
  349. vector<string>::iterator iter =
  350. std::find(local_framework_op_vec.begin(), local_framework_op_vec.end(), op->GetType());
  351. if (iter != local_framework_op_vec.end()) {
  352. // set - original_type
  353. if (!AttrUtils::SetStr(op, ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE, op->GetType())) {
  354. GELOGW("TranFrameOp SetStr ATTR_NAME_FRAMEWORK_ORIGINAL_TYPE failed");
  355. }
  356. // set - framework_type
  357. // [No need to verify return value]
  358. op->SetType("FrameworkOp");
  359. if (!AttrUtils::SetInt(op, ATTR_NAME_FRAMEWORK_FWK_TYPE, domi::FrameworkType::TENSORFLOW)) {
  360. GELOGW("TranFrameOp SetInt ATTR_NAME_FRAMEWORK_FWK_TYPE failed");
  361. }
  362. }
  363. }
  364. }
  365. Status GraphOptimize::IdentifyReference(ComputeGraphPtr &compute_graph) {
  366. for (auto &node : compute_graph->GetAllNodes()) {
  367. GE_CHECK_NOTNULL(node);
  368. auto op_desc = node->GetOpDesc();
  369. GE_CHECK_NOTNULL(op_desc);
  370. auto input_name_index = op_desc->GetAllInputName();
  371. bool is_ref = false;
  372. for (const auto &name_index : input_name_index) {
  373. const int out_index = op_desc->GetOutputIndexByName(name_index.first);
  374. if (out_index != -1) {
  375. auto input_desc = op_desc->GetInputDesc(name_index.second);
  376. input_desc.SetRefPortByIndex({name_index.second});
  377. op_desc->UpdateInputDesc(name_index.second, input_desc);
  378. GELOGI("SetRefPort: set op[%s] input desc[%u-%s] ref.",
  379. op_desc->GetName().c_str(), name_index.second, name_index.first.c_str());
  380. auto output_desc = op_desc->GetOutputDesc(static_cast<uint32_t>(out_index));
  381. output_desc.SetRefPortByIndex({name_index.second});
  382. op_desc->UpdateOutputDesc(static_cast<uint32_t>(out_index), output_desc);
  383. GELOGI("SetRefPort: set op[%s] output desc[%u-%s] ref.",
  384. op_desc->GetName().c_str(), out_index, name_index.first.c_str());
  385. is_ref = true;
  386. }
  387. }
  388. if (is_ref) {
  389. AttrUtils::SetBool(op_desc, ATTR_NAME_REFERENCE, is_ref);
  390. GELOGI("param [node] %s is reference node, set attribute %s to be true.",
  391. node->GetName().c_str(), ATTR_NAME_REFERENCE.c_str());
  392. }
  393. }
  394. return SUCCESS;
  395. }
  396. Status GraphOptimize::OptimizeWholeGraph(ComputeGraphPtr &compute_graph) {
  397. if (compute_graph == nullptr) {
  398. REPORT_INNER_ERROR("E19999", "Param compute_graph is nullptr, check invalid");
  399. GELOGE(GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL, "[Check][Param] compute_graph is nullptr.");
  400. return GE_GRAPH_OPTIMIZE_COMPUTE_GRAPH_NULL;
  401. }
  402. std::shared_ptr<GELib> instance_ptr = ge::GELib::GetInstance();
  403. if (instance_ptr == nullptr || !instance_ptr->InitFlag()) {
  404. REPORT_INNER_ERROR("E19999", "Gelib not init before, check invalid, graph:%s.",
  405. compute_graph->GetName().c_str());
  406. GELOGE(GE_CLI_GE_NOT_INITIALIZED, "[Get][GELib] Gelib not init before, graph:%s.",
  407. compute_graph->GetName().c_str());
  408. return GE_CLI_GE_NOT_INITIALIZED;
  409. }
  410. auto graph_optimizer = instance_ptr->OpsKernelManagerObj().GetAllGraphOptimizerObjsByPriority();
  411. GELOGI("optimize by opskernel in OptimizeWholeGraph. num of graph_optimizer is %zu.", graph_optimizer.size());
  412. Status ret = SUCCESS;
  413. string exclude_core_type = (core_type_ == kVectorCore) ? kAicoreEngine : kVectorEngine;
  414. GELOGD("[OptimizeWholeGraph]: engine type will exclude: %s", exclude_core_type.c_str());
  415. if (!graph_optimizer.empty()) {
  416. for (auto &iter : graph_optimizer) {
  417. if (iter.first == exclude_core_type || iter.second == nullptr) {
  418. continue;
  419. }
  420. GELOGI("Begin to optimize whole graph by engine %s", iter.first.c_str());
  421. ret = iter.second->OptimizeWholeGraph(*compute_graph);
  422. GE_DUMP(compute_graph, "OptimizeWholeGraph" + iter.first);
  423. if (ret != SUCCESS) {
  424. REPORT_INNER_ERROR("E19999", "Call OptimizeWholeGraph failed, ret:%d, engine_name:%s, "
  425. "graph_name:%s", ret, iter.first.c_str(),
  426. compute_graph->GetName().c_str());
  427. GELOGE(ret, "[Call][OptimizeWholeGraph] failed, ret:%d, engine_name:%s, graph_name:%s",
  428. ret, iter.first.c_str(), compute_graph->GetName().c_str());
  429. return ret;
  430. }
  431. }
  432. }
  433. return ret;
  434. }
  435. } // namespace ge

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