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

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