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.

subexpression_migration_pass.cc 21 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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 "subexpression_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 kDataOutIndex = 0;
  22. constexpr uint32_t kCaseInputBase = 1;
  23. constexpr uint32_t kInvalidParent = 0x7fffffffU;
  24. bool IsSameTensor(ConstGeTensorDescPtr src_tensor, ConstGeTensorDescPtr dst_tensor) {
  25. if ((src_tensor == nullptr) && (dst_tensor == nullptr)) {
  26. return true;
  27. }
  28. if ((src_tensor == nullptr) || (dst_tensor == nullptr)) {
  29. return false;
  30. }
  31. if ((src_tensor->GetDataType() != dst_tensor->GetDataType()) ||
  32. (src_tensor->GetFormat() != dst_tensor->GetFormat())) {
  33. return false;
  34. }
  35. const auto src_dims = src_tensor->GetShape().GetDims();
  36. const auto dst_dims = dst_tensor->GetShape().GetDims();
  37. if (src_dims != dst_dims) {
  38. return false;
  39. }
  40. const auto src_orig_dims = src_tensor->GetOriginShape().GetDims();
  41. const auto dst_orig_dims = dst_tensor->GetOriginShape().GetDims();
  42. if (src_orig_dims != dst_orig_dims) {
  43. return false;
  44. }
  45. return true;
  46. }
  47. bool IsSameOpDesc(const OpDescPtr &src_desc, const OpDescPtr &dst_desc) {
  48. if ((src_desc == nullptr) && (dst_desc == nullptr)) {
  49. return true;
  50. }
  51. if ((src_desc == nullptr) || (dst_desc == nullptr)) {
  52. return false;
  53. }
  54. if (src_desc->GetType() != dst_desc->GetType()) {
  55. return false;
  56. }
  57. if ((src_desc->GetInputsSize() != dst_desc->GetInputsSize()) ||
  58. (src_desc->GetOutputsSize() != dst_desc->GetOutputsSize())) {
  59. return false;
  60. }
  61. for (uint32_t i = 0; i < src_desc->GetInputsSize(); ++i) {
  62. if (!IsSameTensor(src_desc->GetInputDescPtr(i), dst_desc->GetInputDescPtr(i))) {
  63. return false;
  64. }
  65. }
  66. for (uint32_t i = 0; i < src_desc->GetOutputsSize(); ++i) {
  67. if (!IsSameTensor(src_desc->GetOutputDescPtr(i), dst_desc->GetOutputDescPtr(i))) {
  68. return false;
  69. }
  70. }
  71. return true;
  72. }
  73. Status SubexpressionMigrationPass::Run(ComputeGraphPtr graph) {
  74. GE_CHECK_NOTNULL(graph);
  75. if (graph->GetParentGraph() != nullptr) {
  76. GELOGD("Subgraph %s skip the SubexpressionMigrationPass", graph->GetName().c_str());
  77. return SUCCESS;
  78. }
  79. GELOGD("Begin to run Subexpression Migration on graph: %s", graph->GetName().c_str());
  80. for (const auto &node : graph->GetDirectNode()) {
  81. if (node->GetType() != CASE) {
  82. continue;
  83. }
  84. const auto &func_desc = node->GetOpDesc();
  85. if (!func_desc->HasAttr(ATTR_NAME_BATCH_NUM)) {
  86. GELOGD("Not multi-batch, Case: %s", node->GetName().c_str());
  87. continue;
  88. }
  89. do {
  90. migration_append_ = false;
  91. map<ComputeGraphPtr, map<uint32_t, NodePtr>> graph_nodes;
  92. if (ClassifyDataNodes(graph, func_desc, graph_nodes) != SUCCESS) {
  93. return FAILED;
  94. }
  95. if (graph_nodes.empty()) {
  96. GELOGW("Graph: %s nodes is empty", graph->GetName().c_str());
  97. break;
  98. }
  99. // {subgraph0, {{1, Data}, {2, Data}, {3, Data}, {4, Data}, ..., {n, Data}}}
  100. // {subgraph1, {{1, Data}, {2, Data}, {3, Data}, {4, Data}, ..., {n, Data}}}
  101. // {subgraph2, {{1, Data}, {2, Data}, {3, Data}, {4, Data}, ..., {n, Data}}}
  102. const auto base_nodes = graph_nodes.begin()->second; // Need copy.
  103. for (const auto &node_item : base_nodes) {
  104. if (GraphNodeMigration(graph, node, graph_nodes, node_item.second, node_item.first) != SUCCESS) {
  105. return FAILED;
  106. }
  107. }
  108. } while (migration_append_);
  109. }
  110. return SUCCESS;
  111. }
  112. ///
  113. /// @ingroup ge
  114. /// @brief Get all Data nodes for all subgraph.
  115. /// @param [in] graph: Root compute graph.
  116. /// @param [in] func_desc: functional OpDesc of Case.
  117. /// @param [out] graph_nodes: Data groups of subgraph.
  118. /// @return 0: SUCCESS / others: FAILED
  119. ///
  120. Status SubexpressionMigrationPass::ClassifyDataNodes(const ComputeGraphPtr &graph, const OpDescPtr &func_desc,
  121. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes) {
  122. for (const auto &name : func_desc->GetSubgraphInstanceNames()) {
  123. const auto &subgraph = graph->GetSubgraph(name);
  124. if (subgraph == nullptr) {
  125. GELOGE(GE_GRAPH_EMPTY_SUBGRAPH, "Subgraph not found, name: %s", name.c_str());
  126. return GE_GRAPH_EMPTY_SUBGRAPH;
  127. }
  128. auto &data_nodes = graph_nodes[subgraph];
  129. for (auto &data : subgraph->GetDirectNode()) {
  130. if (data->GetType() != DATA) {
  131. continue;
  132. }
  133. uint32_t parent_index = 0;
  134. if (!AttrUtils::GetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  135. GELOGE(FAILED, "Parent index not found, name: %s", data->GetName().c_str());
  136. return FAILED;
  137. }
  138. data_nodes[parent_index] = data;
  139. GELOGD("%s, Parent index: %u, Data: %s", subgraph->GetName().c_str(), parent_index, data->GetName().c_str());
  140. }
  141. }
  142. return SUCCESS;
  143. }
  144. ///
  145. /// @ingroup ge
  146. /// @brief Get all Data nodes for all subgraph.
  147. /// @param [in] node: Node Directly to Data.
  148. /// @param [out] inputs: parent index of Input.
  149. /// @param [out] outputs: parent index of Output.
  150. /// @return true: SUCCESS / false: FAILED
  151. ///
  152. bool SubexpressionMigrationPass::GetAssociatedNodes(const NodePtr &node, map<uint32_t, uint32_t> &inputs,
  153. map<uint32_t, uint32_t> &outputs) {
  154. for (uint32_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
  155. outputs[i] = kInvalidParent;
  156. }
  157. uint32_t out_index = 0;
  158. for (uint32_t i = 0; i < node->GetAllInDataAnchorsSize(); ++i) {
  159. const auto &in_anchor = node->GetInDataAnchor(i);
  160. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  161. if (out_anchor == nullptr) {
  162. inputs[i] = kInvalidParent;
  163. continue;
  164. }
  165. // Has none Data input node, Can not move to parent.
  166. const auto &owner_node = out_anchor->GetOwnerNode();
  167. if (owner_node->GetType() != DATA) {
  168. return false;
  169. }
  170. uint32_t parent_index = 0;
  171. if (!AttrUtils::GetInt(owner_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  172. return false;
  173. }
  174. // Input Data feed other Node, need add new Data.
  175. inputs[i] = parent_index;
  176. if ((out_index < outputs.size()) && (owner_node->GetOutDataNodesSize() == 1)) {
  177. outputs[out_index] = parent_index;
  178. ++out_index;
  179. }
  180. }
  181. return true;
  182. }
  183. ///
  184. /// @ingroup ge
  185. /// @brief Get all Data nodes for all subgraph.
  186. /// @param [in] graph_nodes: Data groups of subgraph.
  187. /// @param [in] base_node: Data Node for migration.
  188. /// @param [in] node_idx: Parent index of Data node.
  189. /// @param [in] anchor_idx: Anchor index of node.
  190. /// @return true: Same / false: not same
  191. ///
  192. bool SubexpressionMigrationPass::IsParallelNodeSame(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  193. const NodePtr &base_node, uint32_t node_idx, uint32_t anchor_idx) {
  194. auto it = graph_nodes.begin();
  195. for (++it; it != graph_nodes.end(); ++it) {
  196. const auto &data_nodes = it->second;
  197. auto data_it = data_nodes.find(node_idx);
  198. if (data_it == data_nodes.end()) {
  199. GELOGE(FAILED, "Data: %s not fount, index: %u", base_node->GetName().c_str(), node_idx);
  200. return false;
  201. }
  202. const auto &work_data = data_it->second;
  203. const auto &out_anchor = work_data->GetOutDataAnchor(kDataOutIndex);
  204. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  205. const auto &in_anchor = in_anchors.at(anchor_idx);
  206. if (in_anchor == nullptr) {
  207. GELOGE(FAILED, "Data anchor size: %u, anchor size: %zu", anchor_idx, in_anchors.size());
  208. return false;
  209. }
  210. const auto &work_node = in_anchor->GetOwnerNode();
  211. if (work_node == nullptr) {
  212. GELOGE(FAILED, "Data: %s not found, index: %u", base_node->GetName().c_str(), node_idx);
  213. return false;
  214. }
  215. if (!IsSameOpDesc(base_node->GetOpDesc(), work_node->GetOpDesc())) {
  216. GELOGI("OpDesc diff: %s %s", base_node->GetName().c_str(), work_node->GetName().c_str());
  217. return false;
  218. }
  219. }
  220. return true;
  221. }
  222. ///
  223. /// @ingroup ge
  224. /// @brief Migration subgraph Node to Root
  225. /// @param [in] graph: Root compute graph.
  226. /// @param [in] func_node: functional Node of Case.
  227. /// @param [in] graph_nodes: Data groups of subgraph.
  228. /// @param [in] data_base: Data Node for migration.
  229. /// @param [in] data_idx: Data groups of subgraph.
  230. /// @return 0: SUCCESS / others: FAILED
  231. ///
  232. Status SubexpressionMigrationPass::GraphNodeMigration(const ComputeGraphPtr &graph, const NodePtr &func_node,
  233. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  234. const NodePtr &base_data, uint32_t base_idx) {
  235. bool can_extrapolation = false;
  236. do {
  237. can_extrapolation = false;
  238. const auto out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  239. const auto in_anchors = out_anchor->GetPeerInDataAnchors();
  240. for (size_t i = 0; i < in_anchors.size(); ++i) {
  241. const auto &in_anchor = in_anchors.at(i);
  242. const auto &base_node = in_anchor->GetOwnerNode();
  243. GELOGD("Get Data direct node: %s", base_node->GetName().c_str());
  244. if (!base_node->GetHostNode() || base_node->GetType() == SWITCH) {
  245. continue;
  246. }
  247. // Get associated Data, if Data feed other nodes, need append new Data.
  248. map<uint32_t, uint32_t> inputs;
  249. map<uint32_t, uint32_t> outputs;
  250. if (!GetAssociatedNodes(base_node, inputs, outputs)) {
  251. continue;
  252. }
  253. if (!IsParallelNodeSame(graph_nodes, base_node, base_idx, i)) {
  254. continue;
  255. }
  256. GELOGI("Move to parent: %s, parent index: %u", base_node->GetName().c_str(), base_idx);
  257. if (AppendParallelNode(graph_nodes, func_node, outputs) != SUCCESS) {
  258. return FAILED;
  259. }
  260. if (MoveNodeToParent(graph, func_node, graph_nodes, i, inputs, outputs) != SUCCESS) {
  261. return FAILED;
  262. }
  263. can_extrapolation = true;
  264. break;
  265. }
  266. } while (can_extrapolation);
  267. return SUCCESS;
  268. }
  269. ///
  270. /// @ingroup ge
  271. /// @brief Append Input Tensor for functional node.
  272. /// @param [in] graph_nodes: Data groups of subgraph.
  273. /// @param [in] func_node: functional Node of Case.
  274. /// @param [in] outputs: Parent index of Node output.
  275. /// @return 0: SUCCESS / others: FAILED
  276. ///
  277. Status SubexpressionMigrationPass::AppendParallelNode(map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  278. const NodePtr &func_node, map<uint32_t, uint32_t> &outputs) {
  279. // If outputs index invalid, add Data and Input Tensor.
  280. for (auto &item : outputs) {
  281. if (item.second != kInvalidParent) {
  282. continue;
  283. }
  284. // Add Data to subgraph.
  285. map<ComputeGraphPtr, uint32_t> append_num;
  286. for (auto &groups : graph_nodes) {
  287. const auto &subgraph = groups.first;
  288. auto &data_nodes = groups.second;
  289. item.second = func_node->GetAllInDataAnchorsSize() + append_num[subgraph]; // Update to valid parent index.
  290. std::string data_name = subgraph->GetName() + "_data_" + std::to_string(item.second);
  291. OpDescBuilder op_builder(data_name, DATA);
  292. const OpDescPtr op_desc = op_builder.AddInput("x").AddOutput("y").Build();
  293. if (op_desc == nullptr) {
  294. GELOGE(OUT_OF_MEMORY, "Create multi-batch case desc failed");
  295. return OUT_OF_MEMORY;
  296. }
  297. uint32_t data_index = item.second - kCaseInputBase;
  298. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index)) {
  299. GELOGE(FAILED, "Parent index not found, name: %s", op_desc->GetName().c_str());
  300. return FAILED;
  301. }
  302. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, item.second)) {
  303. GELOGE(FAILED, "Parent index not found, name: %s", op_desc->GetName().c_str());
  304. return FAILED;
  305. }
  306. append_num[subgraph]++;
  307. data_nodes[item.second] = subgraph->AddNode(op_desc);
  308. GELOGI("Add Node: %s, parent index: %u", op_desc->GetName().c_str(), item.second);
  309. }
  310. // Add InputTensor to functional Node.
  311. GE_CHK_GRAPH_STATUS_RET(NodeUtils::AppendInputAnchor(func_node, item.second + 1), "Append input failed");
  312. migration_append_ = true;
  313. }
  314. return SUCCESS;
  315. }
  316. ///
  317. /// @ingroup ge
  318. /// @brief Delete Node from all subgraph.
  319. /// @param [in] graph_nodes: Data groups of subgraph.
  320. /// @param [in] detach: Node will move to parent.
  321. /// @param [in] outputs: Parent index of Node output.
  322. /// @return 0: SUCCESS / others: FAILED
  323. ///
  324. Status SubexpressionMigrationPass::DetachParallelNode(const map<uint32_t, NodePtr> &graph_datas, const NodePtr &detach,
  325. const map<uint32_t, uint32_t> &outputs) {
  326. // Break Data and Move node.
  327. for (const auto &in_anchor : detach->GetAllInDataAnchors()) {
  328. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  329. if (out_anchor == nullptr) {
  330. continue;
  331. }
  332. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  333. const auto &owner_node = out_anchor->GetOwnerNode();
  334. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), detach->GetName().c_str());
  335. }
  336. // Break Move and follow, Link Data and follow.
  337. for (uint32_t i = 0; i < detach->GetAllOutDataAnchorsSize(); ++i) {
  338. auto it_idx = outputs.find(i);
  339. if (it_idx == outputs.end()) {
  340. GELOGE(FAILED, "Node: %s parent index %u not found", detach->GetName().c_str(), i);
  341. return FAILED;
  342. }
  343. auto it_data = graph_datas.find(it_idx->second);
  344. if (it_data == graph_datas.end()) {
  345. GELOGE(FAILED, "Node: %s parent index %u not found", detach->GetName().c_str(), i);
  346. return FAILED;
  347. }
  348. const auto &data_node = it_data->second;
  349. const auto &out_anchor = detach->GetOutDataAnchor(i);
  350. const auto &out_desc = detach->GetOpDesc()->GetOutputDesc(i);
  351. const auto &data_desc = data_node->GetOpDesc();
  352. (void)data_desc->UpdateInputDesc(kDataOutIndex, out_desc); // Set Data Input to new connect Node.
  353. (void)data_desc->UpdateOutputDesc(kDataOutIndex, out_desc); // Set Data Output to new connect Node.
  354. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  355. if (in_anchor == nullptr) {
  356. continue;
  357. }
  358. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  359. const auto &owner_node = in_anchor->GetOwnerNode();
  360. GELOGI("Remove Edge: %s %s", detach->GetName().c_str(), owner_node->GetName().c_str());
  361. const auto &data_out_anchor = data_node->GetOutDataAnchor(kDataOutIndex);
  362. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(data_out_anchor, in_anchor), "Add edge failed");
  363. GELOGI("Add Edge: %s %s", data_node->GetName().c_str(), owner_node->GetName().c_str());
  364. }
  365. }
  366. return SUCCESS;
  367. }
  368. ///
  369. /// @ingroup ge
  370. /// @brief Move Node to Parent Graph.
  371. /// @param [in] graph: Parent compute graph.
  372. /// @param [in] func_node: functional Node of Case.
  373. /// @param [in] attach: Node will move to parent.
  374. /// @param [in] inputs: Parent index of Node input.
  375. /// @param [in] outputs: Parent index of Node output.
  376. /// @return 0: SUCCESS / others: FAILED
  377. ///
  378. Status SubexpressionMigrationPass::AttachParallelNode(const ComputeGraphPtr &graph, const NodePtr &func_node,
  379. const NodePtr &attach, const map<uint32_t, uint32_t> &inputs,
  380. const map<uint32_t, uint32_t> &outputs) {
  381. GE_CHECK_NOTNULL(attach);
  382. for (uint32_t i = 0; i < attach->GetAllInDataAnchorsSize(); ++i) {
  383. auto it_idx = inputs.find(i);
  384. if (it_idx == inputs.end()) {
  385. GELOGE(FAILED, "Node: %s parent index %u not found", attach->GetName().c_str(), i);
  386. return FAILED;
  387. }
  388. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  389. continue;
  390. }
  391. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  392. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  393. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, attach->GetInDataAnchor(i)), "Add edge failed");
  394. const auto &owner_node = out_anchor->GetOwnerNode();
  395. GELOGI("Add Edge: %s %s", owner_node->GetName().c_str(), attach->GetName().c_str());
  396. }
  397. for (uint32_t i = 0; i < attach->GetAllOutDataAnchorsSize(); ++i) {
  398. auto it_idx = outputs.find(i);
  399. if (it_idx == outputs.end()) {
  400. return FAILED;
  401. }
  402. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  403. continue;
  404. }
  405. const auto &out_desc = attach->GetOpDesc()->GetOutputDesc(i);
  406. const auto &func_desc = func_node->GetOpDesc();
  407. (void)func_desc->UpdateInputDesc(it_idx->second, out_desc); // Set Data Input to new connect Node.
  408. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  409. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  410. if (out_anchor != nullptr) {
  411. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  412. const auto &owner_node = out_anchor->GetOwnerNode();
  413. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), func_node->GetName().c_str());
  414. }
  415. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(attach->GetOutDataAnchor(i), in_anchor), "Add edge failed");
  416. GELOGI("Add Edge: %s %s", attach->GetName().c_str(), func_node->GetName().c_str());
  417. }
  418. (void)graph->AddNode(attach);
  419. (void)attach->SetOwnerComputeGraph(graph);
  420. GELOGI("Add Node: %s %s", graph->GetName().c_str(), attach->GetName().c_str());
  421. return SUCCESS;
  422. }
  423. ///
  424. /// @ingroup ge
  425. /// @brief Move node to Parent graph.
  426. /// @param [in] graph: Root compute graph.
  427. /// @param [in] func_node: functional Node of Case.
  428. /// @param [in] graph_nodes: Data groups of subgraph.
  429. /// @param [in] anchor_idx: anchor index of move Node.
  430. /// @param [in] inputs: Parent index of Node input.
  431. /// @param [in] outputs: Parent index of Node output.
  432. /// @return 0: SUCCESS / others: FAILED
  433. ///
  434. Status SubexpressionMigrationPass::MoveNodeToParent(const ComputeGraphPtr &graph, const NodePtr &func_node,
  435. const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  436. uint32_t anchor_idx, const map<uint32_t, uint32_t> &inputs,
  437. const map<uint32_t, uint32_t> &outputs) {
  438. if (inputs.empty()) {
  439. GELOGE(FAILED, "Graph: %s, inputs is empty", graph->GetName().c_str());
  440. return FAILED;
  441. }
  442. NodePtr move_node;
  443. uint32_t base_index = inputs.begin()->second;
  444. for (auto &groups : graph_nodes) {
  445. const auto &subgraph = groups.first;
  446. const auto &subnodes = groups.second;
  447. auto it = subnodes.find(base_index);
  448. if (it == subnodes.end()) {
  449. GELOGE(FAILED, "Graph: %s, Data: %u node not found", subgraph->GetName().c_str(), base_index);
  450. return FAILED;
  451. }
  452. const auto &base_data = it->second;
  453. const auto &out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  454. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  455. const auto &in_anchor = in_anchors.at(anchor_idx);
  456. if (in_anchor == nullptr) {
  457. GELOGE(FAILED, "Data anchor index: %u, anchor size: %zu", anchor_idx, in_anchors.size());
  458. return FAILED;
  459. }
  460. move_node = in_anchor->GetOwnerNode();
  461. if (move_node == nullptr) {
  462. GELOGE(FAILED, "Data: %s not found, index: %u", base_data->GetName().c_str(), base_index);
  463. return FAILED;
  464. }
  465. if (DetachParallelNode(subnodes, move_node, outputs) != SUCCESS) {
  466. GELOGE(FAILED, "Data: %s not found, index: %u", base_data->GetName().c_str(), base_index);
  467. return FAILED;
  468. }
  469. GE_CHK_GRAPH_STATUS_RET(subgraph->RemoveNode(move_node), "Remove node failed");
  470. GELOGI("Remove Node: %s %s", subgraph->GetName().c_str(), move_node->GetName().c_str());
  471. }
  472. if (AttachParallelNode(graph, func_node, move_node, inputs, outputs) != SUCCESS) {
  473. return FAILED;
  474. }
  475. return SUCCESS;
  476. }
  477. } // namespace ge

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