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.

subgraph_const_migration_pass.cc 27 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "subgraph_const_migration_pass.h"
  17. #include "graph/utils/node_utils.h"
  18. #include "ge_local_engine/engine/host_cpu_engine.h"
  19. #include "graph/passes/folding_pass.h"
  20. namespace ge {
  21. constexpr uint32_t kZeroIndex = 0;
  22. constexpr uint32_t kCaseInputBase = 1;
  23. constexpr uint32_t kInvalidParent = 0x7fffffffU;
  24. const string kMbatchNodeNameMark = "_ascend_mbatch_batch_";
  25. bool IsSameConstNode(const NodePtr &src_node, const NodePtr &dst_node) {
  26. if ((src_node == nullptr) && (dst_node == nullptr)) {
  27. return true;
  28. }
  29. if ((src_node == nullptr) || (dst_node == nullptr)) {
  30. return false;
  31. }
  32. if (src_node->GetType() != dst_node->GetType()) {
  33. return false;
  34. }
  35. const GeTensorDesc &src_desc = src_node->GetOpDesc()->GetOutputDesc(kZeroIndex);
  36. const GeTensorDesc &dst_desc = dst_node->GetOpDesc()->GetOutputDesc(kZeroIndex);
  37. return (src_desc == dst_desc);
  38. }
  39. /***********************************************************************************************************************
  40. +-----------+
  41. | Data |
  42. +-----------+
  43. |
  44. |
  45. +-----------+
  46. | Cast |
  47. +-----------+
  48. |
  49. |
  50. +-----------+ +-----------+ +-----------+
  51. | TransData | | Data | | Data |
  52. +-----------+ +-----------+ +-----------+
  53. \ | /
  54. \ | /
  55. \ | /
  56. \ | /
  57. +-----------+ +-----------+ +-----------+ +-----------+ +-----------+ +-----------+ +-----------+
  58. | Data | | Data | | Data | | Data | | Data | | Data | | Conv2D |
  59. +-----------+ +-----------+ +-----------+ +-----------+ +-----------+ +-----------+ +-----------+
  60. \ \ | / / | | +-----------+
  61. \ \ | / / | | | Const |
  62. \ \ | / / | | +-----------+
  63. \ \ | / / | | /
  64. \ +-----------+ / | +-----------+ /
  65. +---------------| Const |----------------+ | | Pooling |-----+
  66. +-----------+ | +-----------+
  67. \ | /
  68. \ | /
  69. \ +-----------+ /
  70. +-----------------------------------| Conv2D |------+
  71. +-----------+
  72. |
  73. |
  74. +-----------+
  75. | Node |
  76. +-----------+
  77. ***********************************************************************************************************************/
  78. Status SubgraphConstMigrationPass::Run(ComputeGraphPtr graph) {
  79. GE_CHECK_NOTNULL(graph);
  80. if (graph->GetParentGraph() != nullptr) {
  81. GELOGD("Subgraph %s skip the SubgraphConstMigrationPass", graph->GetName().c_str());
  82. return SUCCESS;
  83. }
  84. GELOGD("Begin to run Subgraph Const Migration on graph: %s", graph->GetName().c_str());
  85. for (const auto &node : graph->GetDirectNode()) {
  86. if (node->GetType() != CASE) {
  87. continue;
  88. }
  89. const auto &func_desc = node->GetOpDesc();
  90. if (!func_desc->HasAttr(ATTR_NAME_BATCH_NUM)) {
  91. GELOGD("Not multi-batch, Skip Case: %s", node->GetName().c_str());
  92. continue;
  93. }
  94. map<ComputeGraphPtr, map<string, NodePtr>> all_const_nodes;
  95. map<ComputeGraphPtr, map<uint32_t, NodePtr>> all_data_nodes;
  96. if (ClassifyGraphNodes(graph, func_desc, all_const_nodes, all_data_nodes) != SUCCESS) {
  97. return FAILED;
  98. }
  99. if (all_const_nodes.empty()) {
  100. GELOGW("Graph: %s subgraph is empty", graph->GetName().c_str());
  101. break;
  102. }
  103. // {subgraph0, {{key1, Const}, {key2, Const}, {key3, Const}, {key4, Const}, ..., {keyn, Const}}}
  104. // {subgraph1, {{key1, Const}, {key2, Const}, {key3, Const}, {key4, Const}, ..., {keyn, Const}}}
  105. // {subgraph2, {{key1, Const}, {key2, Const}, {key3, Const}, {key4, Const}, ..., {keyn, Const}}}
  106. const auto &const_nodes = all_const_nodes.begin()->second;
  107. for (const auto &item : const_nodes) {
  108. if (GraphNodeMigration(graph, node, all_const_nodes, all_data_nodes, item.second, item.first) != SUCCESS) {
  109. return FAILED;
  110. }
  111. }
  112. }
  113. return SUCCESS;
  114. }
  115. ///
  116. /// @ingroup ge
  117. /// @brief Get all Const/Data nodes for all subgraph.
  118. /// @param [in] graph: Root compute graph.
  119. /// @param [in] func_desc: functional OpDesc of Case.
  120. /// @param [out] all_const_nodes: Const groups of subgraph.
  121. /// @param [out] all_data_nodes: Data groups of subgraph.
  122. /// @return 0: SUCCESS / others: FAILED
  123. ///
  124. Status SubgraphConstMigrationPass::ClassifyGraphNodes(const ComputeGraphPtr &graph, const OpDescPtr &func_desc,
  125. map<ComputeGraphPtr, map<string, NodePtr>> &all_const_nodes,
  126. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &all_data_nodes) {
  127. for (const auto &name : func_desc->GetSubgraphInstanceNames()) {
  128. const auto &subgraph = graph->GetSubgraph(name);
  129. if (subgraph == nullptr) {
  130. REPORT_INNER_ERROR("E19999", "Get subgraph from graph:%s by name:%s failed",
  131. graph->GetName().c_str(), name.c_str());
  132. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "[Get][SubGraph] from graph:%s by name:%s failed",
  133. graph->GetName().c_str(), name.c_str());
  134. return GE_GRAPH_EMPTY_SUBGRAPH;
  135. }
  136. set<NodePtr> ctrl_only_const_nodes;
  137. auto &data_nodes = all_data_nodes[subgraph];
  138. auto &const_nodes = all_const_nodes[subgraph];
  139. for (auto &node : subgraph->GetDirectNode()) {
  140. if (node->GetType() == DATA) {
  141. uint32_t parent_index = kInvalidParent;
  142. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  143. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  144. node->GetName().c_str(), node->GetType().c_str());
  145. GELOGE(FAILED, "[Get][Attr] %s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  146. node->GetName().c_str(), node->GetType().c_str());
  147. return FAILED;
  148. }
  149. data_nodes[parent_index] = node;
  150. GELOGD("%s, index: %u, Data: %s", subgraph->GetName().c_str(), parent_index, node->GetName().c_str());
  151. } else if ((node->GetType() == CONSTANT) && (node->GetOutDataAnchor(kZeroIndex) != nullptr)) {
  152. set<string> peer_name_list;
  153. GetPeerNameList(node, peer_name_list);
  154. if (peer_name_list.empty()) {
  155. GELOGI("%s, Const: %s, no data output", subgraph->GetName().c_str(), node->GetName().c_str());
  156. const auto in_all_nodes = node->GetInAllNodes();
  157. if (in_all_nodes.empty() || std::all_of(in_all_nodes.begin(), in_all_nodes.end(),
  158. [](const NodePtr &n) { return n->GetType() == DATA; })) {
  159. ctrl_only_const_nodes.insert(node);
  160. }
  161. continue;
  162. }
  163. string key_of_const;
  164. for (const string &name : peer_name_list) {
  165. key_of_const += (key_of_const.empty() ? name : "_" + name);
  166. }
  167. const_nodes[key_of_const] = node;
  168. GELOGD("%s, Const: %s, Key: %s", subgraph->GetName().c_str(), node->GetName().c_str(), key_of_const.c_str());
  169. }
  170. }
  171. for (auto &node : ctrl_only_const_nodes) {
  172. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(subgraph, node),
  173. "[Remove][Node] without relink failed, node:%s, graph:%s",
  174. node->GetName().c_str(), subgraph->GetName().c_str());
  175. }
  176. }
  177. return SUCCESS;
  178. }
  179. void SubgraphConstMigrationPass::GetPeerNameList(const NodePtr &node, set<string> &peer_name_list) {
  180. const auto &out_anchor = node->GetOutDataAnchor(kZeroIndex);
  181. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  182. const auto &peer_node = in_anchor->GetOwnerNode();
  183. // Trim subgraph node name prefix.
  184. string node_full_name = peer_node->GetName();
  185. size_t pos = node_full_name.find(kMbatchNodeNameMark);
  186. if (pos == string::npos) {
  187. GELOGI("Can not find: %s of multi-batch in node: %s", kMbatchNodeNameMark.c_str(), node_full_name.c_str());
  188. continue;
  189. }
  190. string fixed_name = node_full_name.substr(0, pos);
  191. pos = node_full_name.find("_", pos + kMbatchNodeNameMark.length());
  192. if (pos != string::npos) {
  193. fixed_name += node_full_name.substr(pos);
  194. }
  195. peer_name_list.insert(fixed_name + ":" + std::to_string(in_anchor->GetIdx()));
  196. }
  197. }
  198. ///
  199. /// @ingroup ge
  200. /// @brief Get parent_index for Const node migration.
  201. /// @param [in] all_data_nodes: Data groups of subgraph.
  202. /// @param [in] const_node: Const node will process.
  203. /// @param [out] parent_index: parent index for replace Data.
  204. /// @return true: SUCCESS / false: FAILED
  205. ///
  206. bool SubgraphConstMigrationPass::GetAssociatedNodes(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &all_data_nodes,
  207. const NodePtr &const_node, uint32_t &parent_index) {
  208. for (const auto in_node : const_node->GetInAllNodes()) {
  209. if (in_node->GetType() != DATA) {
  210. return false;
  211. }
  212. uint32_t node_index = 0;
  213. if (!AttrUtils::GetInt(in_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, node_index)) {
  214. return false;
  215. }
  216. // Input Data feed other Node, need add new Data.
  217. if ((parent_index == kInvalidParent) && in_node->GetOutDataNodes().empty()) {
  218. parent_index = node_index;
  219. }
  220. }
  221. return true;
  222. }
  223. ///
  224. /// @ingroup ge
  225. /// @brief Check parallel node is same for all subgraph.
  226. /// @param [in] all_const_nodes: Const groups of subgraph.
  227. /// @param [in] const_node: Const Node for migration.
  228. /// @param [in] node_key: Key of Const node.
  229. /// @return true: Same / false: not same
  230. ///
  231. bool SubgraphConstMigrationPass::IsParallelNodeSame(const map<ComputeGraphPtr, map<string, NodePtr>> &all_const_nodes,
  232. const NodePtr &const_node, const string &node_key) {
  233. auto it = all_const_nodes.begin();
  234. for (++it; it != all_const_nodes.end(); ++it) {
  235. const auto &const_nodes = it->second;
  236. auto node_it = const_nodes.find(node_key);
  237. if (node_it == const_nodes.end()) {
  238. GELOGW("Const node: %s not fount, key: %s", const_node->GetName().c_str(), node_key.c_str());
  239. return false;
  240. }
  241. const auto &work_node = node_it->second;
  242. if (!IsSameConstNode(const_node, work_node)) {
  243. GELOGI("Not same: %s %s, key: %s", const_node->GetName().c_str(), work_node->GetName().c_str(), node_key.c_str());
  244. return false;
  245. }
  246. }
  247. return true;
  248. }
  249. ///
  250. /// @ingroup ge
  251. /// @brief Migration subgraph Node to Root
  252. /// @param [in] graph: Root compute graph.
  253. /// @param [in] func_node: functional Node of Case.
  254. /// @param [in] all_const_nodes: Const groups of subgraph.
  255. /// @param [in] all_data_nodes: Data groups of subgraph.
  256. /// @param [in] const_node: Const Node for migration.
  257. /// @param [in] node_key: Key of Const node for migration.
  258. /// @return 0: SUCCESS / others: FAILED
  259. ///
  260. Status SubgraphConstMigrationPass::GraphNodeMigration(const ComputeGraphPtr &graph, const NodePtr &func_node,
  261. const map<ComputeGraphPtr, map<string, NodePtr>> &all_const_nodes,
  262. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &all_data_nodes,
  263. const NodePtr &const_node, const string &node_key) {
  264. if (!IsParallelNodeSame(all_const_nodes, const_node, node_key)) {
  265. return SUCCESS;
  266. }
  267. // Get associated Data, if Data feed other nodes, need append new Data.
  268. uint32_t parent_index = kInvalidParent;
  269. if (!GetAssociatedNodes(all_data_nodes, const_node, parent_index)) {
  270. return SUCCESS;
  271. }
  272. GELOGI("Move node: %s, parent index: %u", const_node->GetName().c_str(), parent_index);
  273. if (AppendParallelNode(func_node, parent_index, all_data_nodes) != SUCCESS) {
  274. return FAILED;
  275. }
  276. if (MoveNodeToParent(graph, func_node, all_const_nodes, all_data_nodes, node_key, parent_index) != SUCCESS) {
  277. return FAILED;
  278. }
  279. return SUCCESS;
  280. }
  281. ///
  282. /// @ingroup ge
  283. /// @brief Append Input Tensor for functional node.
  284. /// @param [in] func_node: functional Node of Case.
  285. /// @param [in/out] parent_index: Parent index for migration.
  286. /// @param [in/out] all_data_nodes: Data groups of subgraph.
  287. /// @return 0: SUCCESS / others: FAILED
  288. ///
  289. Status SubgraphConstMigrationPass::AppendParallelNode(const NodePtr &func_node, uint32_t &parent_index,
  290. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &all_data_nodes) {
  291. // If outputs index invalid, add Data and Input Tensor.
  292. if (parent_index != kInvalidParent) {
  293. return SUCCESS;
  294. }
  295. // Add Data to subgraph.
  296. parent_index = func_node->GetAllInDataAnchorsSize(); // Update to valid parent index.
  297. for (auto &item : all_data_nodes) {
  298. const auto &subgraph = item.first;
  299. const auto data_name = subgraph->GetName() + "_data_" + std::to_string(parent_index);
  300. OpDescBuilder op_builder(data_name, DATA);
  301. const auto op_desc = op_builder.AddInput("x").AddOutput("y").Build();
  302. if (op_desc == nullptr) {
  303. REPORT_CALL_ERROR("E19999", "Build op:%s(%s) failed", data_name.c_str(), DATA);
  304. GELOGE(OUT_OF_MEMORY, "[Build][OpDesc] %s(%s) failed", data_name.c_str(), DATA);
  305. return OUT_OF_MEMORY;
  306. }
  307. uint32_t data_index = parent_index - kCaseInputBase;
  308. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index)) {
  309. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_INDEX.c_str(),
  310. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  311. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_NAME_INDEX.c_str(),
  312. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  313. return FAILED;
  314. }
  315. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  316. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  317. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  318. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  319. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  320. return FAILED;
  321. }
  322. item.second[parent_index] = subgraph->AddNode(op_desc);
  323. GELOGI("Add Node: %s, parent index: %u", op_desc->GetName().c_str(), parent_index);
  324. }
  325. // Add InputTensor to functional Node.
  326. NodeUtils::AppendInputAnchor(func_node, parent_index + 1);
  327. return SUCCESS;
  328. }
  329. ///
  330. /// @ingroup ge
  331. /// @brief Delete Node from subgraph.
  332. /// @param [in] graph: subgraph for process.
  333. /// @param [in] const_node: Node will move to parent.
  334. /// @param [in] data_node: Place holder for Const.
  335. /// @return 0: SUCCESS / others: FAILED
  336. ///
  337. Status SubgraphConstMigrationPass::DetachParallelNode(const ComputeGraphPtr &graph, const NodePtr &const_node,
  338. const NodePtr &data_node) {
  339. // Break Data and Move node.
  340. const auto &in_anchor = const_node->GetInControlAnchor();
  341. const auto out_anchors = in_anchor->GetPeerOutControlAnchors();
  342. for (const auto out_anchor : out_anchors) {
  343. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  344. "[Remove][Edge] between %s and %s failed", out_anchor->GetOwnerNode()->GetName().c_str(),
  345. const_node->GetName().c_str());
  346. const auto owner_node = out_anchor->GetOwnerNode();
  347. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), const_node->GetName().c_str());
  348. if (owner_node->GetInAllNodes().empty() && owner_node->GetOutAllNodes().empty() && owner_node != data_node) {
  349. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(graph, owner_node),
  350. "[Remove][Node] without relink failed, node:%s", owner_node->GetName().c_str());
  351. }
  352. }
  353. const auto &ctrl_anchor = const_node->GetOutControlAnchor();
  354. const auto ctrl_anchors = ctrl_anchor->GetPeerInControlAnchors();
  355. for (const auto in_anchor : ctrl_anchors) {
  356. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(ctrl_anchor, in_anchor),
  357. "[Remove][ControlEdge] between %s and %s failed",
  358. const_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  359. GELOGI("Remove Edge: %s %s", const_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  360. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(data_node->GetOutControlAnchor(), in_anchor),
  361. "[Add][ControlEdge] between %s and %s failed",
  362. data_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  363. GELOGI("Add Edge: %s %s", data_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  364. }
  365. // Break Move and follow, Link Data and follow.
  366. const auto &out_anchor = const_node->GetOutDataAnchor(kZeroIndex);
  367. const auto in_anchors = out_anchor->GetPeerInDataAnchors();
  368. for (const auto in_anchor : in_anchors) {
  369. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  370. "[Remove][Edge] between %s and %s failed",
  371. const_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  372. GELOGI("Remove Edge: %s %s", const_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  373. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(data_node->GetOutDataAnchor(kZeroIndex), in_anchor),
  374. "[Add][Edge] between %s and %s failed",
  375. data_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  376. GELOGI("Add Edge: %s %s", data_node->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  377. }
  378. // Update Data op DataType.
  379. const auto &const_desc = const_node->GetOpDesc();
  380. const auto &tensor_desc = const_desc->GetOutputDesc(kZeroIndex);
  381. const auto &data_desc = data_node->GetOpDesc();
  382. (void)data_desc->UpdateInputDesc(kZeroIndex, tensor_desc); // Set Data Input to new connect Node.
  383. (void)data_desc->UpdateOutputDesc(kZeroIndex, tensor_desc); // Set Data Output to new connect Node.
  384. return SUCCESS;
  385. }
  386. ///
  387. /// @ingroup ge
  388. /// @brief Move Node to Parent Graph.
  389. /// @param [in] graph: Parent compute graph.
  390. /// @param [in] func_node: functional Node of Case.
  391. /// @param [in] const_node: Node will move to parent.
  392. /// @param [in] parent_index: Parent index of Node input.
  393. /// @return 0: SUCCESS / others: FAILED
  394. ///
  395. Status SubgraphConstMigrationPass::AttachParallelNode(const ComputeGraphPtr &graph, const NodePtr &func_node,
  396. const NodePtr &const_node, uint32_t parent_index) {
  397. GE_CHECK_NOTNULL(const_node);
  398. if (parent_index == kInvalidParent) {
  399. return INTERNAL_ERROR;
  400. }
  401. const auto &func_desc = func_node->GetOpDesc();
  402. const auto &tensor_desc = const_node->GetOpDesc()->GetOutputDesc(kZeroIndex);
  403. (void)func_desc->UpdateInputDesc(parent_index, tensor_desc); // Set Data Input to new connect Node.
  404. const auto &in_anchor = func_node->GetInDataAnchor(parent_index);
  405. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  406. if (out_anchor != nullptr) { // Break useless old link.
  407. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  408. "[Remove][Edge] between %s and %s failed",
  409. out_anchor->GetOwnerNode()->GetName().c_str(), func_node->GetName().c_str());
  410. const auto owner_node = out_anchor->GetOwnerNode();
  411. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), func_node->GetName().c_str());
  412. if (owner_node->GetInAllNodes().empty() && owner_node->GetOutAllNodes().empty()) {
  413. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(graph, owner_node),
  414. "[Remove][Node] without relink failed, node:%s", owner_node->GetName().c_str());
  415. }
  416. }
  417. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(const_node->GetOutDataAnchor(kZeroIndex), in_anchor),
  418. "[Add][Edge] between %s and %s failed",
  419. const_node->GetName().c_str(), func_node->GetName().c_str());
  420. GELOGI("Add Edge: %s %s, index: %u", const_node->GetName().c_str(), func_node->GetName().c_str(), parent_index);
  421. (void)graph->AddNode(const_node);
  422. (void)const_node->SetOwnerComputeGraph(graph);
  423. GELOGI("Add Node: %s %s", graph->GetName().c_str(), const_node->GetName().c_str());
  424. return SUCCESS;
  425. }
  426. ///
  427. /// @ingroup ge
  428. /// @brief Move node to Parent graph.
  429. /// @param [in] graph: Root compute graph.
  430. /// @param [in] func_node: functional Node of Case.
  431. /// @param [in] graph_nodes: Data groups of subgraph.
  432. /// @param [in] index: anchor index of move Node.
  433. /// @param [in] inputs: Parent index of Node input.
  434. /// @param [in] outputs: Parent index of Node output.
  435. /// @return 0: SUCCESS / others: FAILED
  436. ///
  437. Status SubgraphConstMigrationPass::MoveNodeToParent(const ComputeGraphPtr &graph, const NodePtr &func_node,
  438. const map<ComputeGraphPtr, map<string, NodePtr>> &all_const_nodes,
  439. const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &all_data_nodes,
  440. const string &node_key, uint32_t parent_index) {
  441. if (node_key.empty() || parent_index == kInvalidParent) {
  442. REPORT_INNER_ERROR("E19999", "Param node_key is empty or param parent_index is 0x%X, check invalid",
  443. kInvalidParent);
  444. GELOGE(FAILED, "[Check][Param] Param node_key is empty or param parent_index is 0x%X", kInvalidParent);
  445. return FAILED;
  446. }
  447. NodePtr move_node;
  448. for (auto &item : all_const_nodes) {
  449. const auto &subgraph = item.first;
  450. const auto it_const = item.second.find(node_key);
  451. if (it_const == item.second.end()) {
  452. REPORT_INNER_ERROR("E19999", "Const node name:%s not found in graph:%s, check invalid",
  453. node_key.c_str(), subgraph->GetName().c_str());
  454. GELOGE(FAILED, "[Check][Param] Const node name:%s not found in graph:%s",
  455. node_key.c_str(), subgraph->GetName().c_str());
  456. return FAILED;
  457. }
  458. move_node = it_const->second;
  459. const auto it_nodes = all_data_nodes.find(subgraph);
  460. if (it_nodes == all_data_nodes.end()) {
  461. REPORT_INNER_ERROR("E19999", "Const node name:%s not found in graph:%s, check invalid",
  462. node_key.c_str(), subgraph->GetName().c_str());
  463. GELOGE(FAILED, "[Check][Param] Const node name:%s not found in graph:%s",
  464. node_key.c_str(), subgraph->GetName().c_str());
  465. return FAILED;
  466. }
  467. const auto it_data = it_nodes->second.find(parent_index);
  468. if (it_data == it_nodes->second.end()) {
  469. REPORT_INNER_ERROR("E19999", "Const node name:%s not found in graph:%s, check invalid",
  470. node_key.c_str(), subgraph->GetName().c_str());
  471. GELOGE(FAILED, "[Check][Param] Const node name:%s not found in graph:%s",
  472. node_key.c_str(), subgraph->GetName().c_str());
  473. return FAILED;
  474. }
  475. if (DetachParallelNode(subgraph, move_node, it_data->second) != SUCCESS) {
  476. GELOGE(FAILED, "[Detach][ParallelNode] Data:%s not found, index:%u", move_node->GetName().c_str(), parent_index);
  477. return FAILED;
  478. }
  479. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveNodeWithoutRelink(subgraph, move_node),
  480. "[Remove][Node] without relink failed, node:%s, graph:%s",
  481. move_node->GetName().c_str(), subgraph->GetName().c_str());
  482. GELOGI("Remove Node: %s %s", subgraph->GetName().c_str(), move_node->GetName().c_str());
  483. }
  484. if (AttachParallelNode(graph, func_node, move_node, parent_index) != SUCCESS) {
  485. return FAILED;
  486. }
  487. return SUCCESS;
  488. }
  489. } // namespace ge

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