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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. /**
  2. * Copyright 2019-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. }
  140. }
  141. for (const auto &data_nodes : graph_nodes) {
  142. if (data_nodes.second.size() != graph_nodes.begin()->second.size()) {
  143. GELOGE(FAILED, "Subgraph %s has invalid Data nodes[%zu != %zu]", data_nodes.first->GetName().c_str(),
  144. data_nodes.second.size(), graph_nodes.begin()->second.size());
  145. return FAILED;
  146. }
  147. }
  148. return SUCCESS;
  149. }
  150. ///
  151. /// @ingroup ge
  152. /// @brief Get all Data nodes for all subgraph.
  153. /// @param [in] node: Node Directly to Data.
  154. /// @param [out] inputs: parent index of Input.
  155. /// @param [out] outputs: parent index of Output.
  156. /// @return true: SUCCESS / false: FAILED
  157. ///
  158. bool SubexpressionMigrationPass::GetAssociatedNodes(const NodePtr &node, map<uint32_t, uint32_t> &inputs,
  159. map<uint32_t, uint32_t> &outputs) {
  160. for (uint32_t i = 0; i < node->GetAllOutDataAnchorsSize(); ++i) {
  161. outputs[i] = kInvalidParent;
  162. }
  163. uint32_t out_index = 0;
  164. for (uint32_t i = 0; i < node->GetAllInDataAnchorsSize(); ++i) {
  165. const auto &in_anchor = node->GetInDataAnchor(i);
  166. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  167. if (out_anchor == nullptr) {
  168. inputs[i] = kInvalidParent;
  169. continue;
  170. }
  171. // Has none Data input node, Can not move to parent.
  172. const auto &owner_node = out_anchor->GetOwnerNode();
  173. if (owner_node->GetType() != DATA) {
  174. return false;
  175. }
  176. uint32_t parent_index = 0;
  177. if (!AttrUtils::GetInt(owner_node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  178. return false;
  179. }
  180. // Input Data feed other Node, need add new Data.
  181. inputs[i] = parent_index;
  182. if ((out_index < outputs.size()) && (owner_node->GetOutDataNodesSize() == 1)) {
  183. outputs[out_index] = parent_index;
  184. ++out_index;
  185. }
  186. }
  187. return true;
  188. }
  189. ///
  190. /// @ingroup ge
  191. /// @brief Get all Data nodes for all subgraph.
  192. /// @param [in] graph_nodes: Data groups of subgraph.
  193. /// @param [in] base_node: Data Node for migration.
  194. /// @param [in] node_idx: Parent index of Data node.
  195. /// @param [in] anchor_idx: Anchor index of node.
  196. /// @return true: Same / false: not same
  197. ///
  198. bool SubexpressionMigrationPass::IsParallelNodeSame(const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  199. const NodePtr &base_node, uint32_t node_idx, uint32_t anchor_idx) {
  200. auto it = graph_nodes.begin();
  201. for (++it; it != graph_nodes.end(); ++it) {
  202. const auto &data_nodes = it->second;
  203. auto data_it = data_nodes.find(node_idx);
  204. if (data_it == data_nodes.end()) {
  205. GELOGE(FAILED, "Data: %s not fount, index: %u", base_node->GetName().c_str(), node_idx);
  206. return false;
  207. }
  208. const auto &work_data = data_it->second;
  209. const auto &out_anchor = work_data->GetOutDataAnchor(kDataOutIndex);
  210. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  211. const auto &in_anchor = in_anchors.at(anchor_idx);
  212. if (in_anchor == nullptr) {
  213. GELOGE(FAILED, "Data anchor size: %u, anchor size: %zu", anchor_idx, in_anchors.size());
  214. return false;
  215. }
  216. const auto &work_node = in_anchor->GetOwnerNode();
  217. if (work_node == nullptr) {
  218. GELOGE(FAILED, "Data: %s not found, index: %u", base_node->GetName().c_str(), node_idx);
  219. return false;
  220. }
  221. if (!IsSameOpDesc(base_node->GetOpDesc(), work_node->GetOpDesc())) {
  222. GELOGI("OpDesc diff: %s %s", base_node->GetName().c_str(), work_node->GetName().c_str());
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. ///
  229. /// @ingroup ge
  230. /// @brief Migration subgraph Node to Root
  231. /// @param [in] graph: Root compute graph.
  232. /// @param [in] func_node: functional Node of Case.
  233. /// @param [in] graph_nodes: Data groups of subgraph.
  234. /// @param [in] data_base: Data Node for migration.
  235. /// @param [in] data_idx: Data groups of subgraph.
  236. /// @return 0: SUCCESS / others: FAILED
  237. ///
  238. Status SubexpressionMigrationPass::GraphNodeMigration(const ComputeGraphPtr &graph, const NodePtr &func_node,
  239. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  240. const NodePtr &base_data, uint32_t base_idx) {
  241. bool can_extrapolation = false;
  242. do {
  243. can_extrapolation = false;
  244. const auto out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  245. const auto in_anchors = out_anchor->GetPeerInDataAnchors();
  246. for (size_t i = 0; i < in_anchors.size(); ++i) {
  247. const auto &in_anchor = in_anchors.at(i);
  248. const auto &base_node = in_anchor->GetOwnerNode();
  249. GELOGD("Get Data direct node: %s", base_node->GetName().c_str());
  250. if (!base_node->GetHostNode()) {
  251. continue;
  252. }
  253. // Get associated Data, if Data feed other nodes, need append new Data.
  254. map<uint32_t, uint32_t> inputs;
  255. map<uint32_t, uint32_t> outputs;
  256. if (!GetAssociatedNodes(base_node, inputs, outputs)) {
  257. continue;
  258. }
  259. if (!IsParallelNodeSame(graph_nodes, base_node, base_idx, i)) {
  260. continue;
  261. }
  262. GELOGI("Move to parent: %s", base_node->GetName().c_str());
  263. if (AppendParallelNode(graph_nodes, func_node, outputs) != SUCCESS) {
  264. return FAILED;
  265. }
  266. if (MoveNodeToParent(graph, func_node, graph_nodes, i, inputs, outputs) != SUCCESS) {
  267. return FAILED;
  268. }
  269. can_extrapolation = true;
  270. break;
  271. }
  272. } while (can_extrapolation);
  273. return SUCCESS;
  274. }
  275. ///
  276. /// @ingroup ge
  277. /// @brief Append Input Tensor for functional node.
  278. /// @param [in] graph_nodes: Data groups of subgraph.
  279. /// @param [in] func_node: functional Node of Case.
  280. /// @param [in] outputs: Parent index of Node output.
  281. /// @return 0: SUCCESS / others: FAILED
  282. ///
  283. Status SubexpressionMigrationPass::AppendParallelNode(map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  284. const NodePtr &func_node, map<uint32_t, uint32_t> &outputs) {
  285. // If outputs index invalid, add Data and Input Tensor.
  286. for (auto &item : outputs) {
  287. if (item.second != kInvalidParent) {
  288. continue;
  289. }
  290. // Add Data to subgraph.
  291. for (auto &groups : graph_nodes) {
  292. const auto &subgraph = groups.first;
  293. auto &data_nodes = groups.second;
  294. uint32_t data_index = data_nodes.size();
  295. item.second = data_index + kCaseInputBase; // Update to valid parent index.
  296. std::string data_name = subgraph->GetName() + "_data_" + std::to_string(item.second);
  297. OpDescBuilder op_builder(data_name, DATA);
  298. const OpDescPtr op_desc = op_builder.AddInput("x").AddOutput("y").Build();
  299. if (op_desc == nullptr) {
  300. GELOGE(OUT_OF_MEMORY, "Create multi-batch case desc failed");
  301. return OUT_OF_MEMORY;
  302. }
  303. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index)) {
  304. GELOGE(FAILED, "Parent index not found, name: %s", op_desc->GetName().c_str());
  305. return FAILED;
  306. }
  307. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, item.second)) {
  308. GELOGE(FAILED, "Parent index not found, name: %s", op_desc->GetName().c_str());
  309. return FAILED;
  310. }
  311. data_nodes[item.second] = subgraph->AddNode(op_desc);
  312. }
  313. // Add InputTensor to functional Node.
  314. NodeUtils::AppendInputAnchor(func_node, item.second + 1);
  315. migration_append_ = true;
  316. }
  317. return SUCCESS;
  318. }
  319. ///
  320. /// @ingroup ge
  321. /// @brief Delete Node from all subgraph.
  322. /// @param [in] graph_nodes: Data groups of subgraph.
  323. /// @param [in] detach: Node will move to parent.
  324. /// @param [in] outputs: Parent index of Node output.
  325. /// @return 0: SUCCESS / others: FAILED
  326. ///
  327. Status SubexpressionMigrationPass::DetachParallelNode(const map<uint32_t, NodePtr> &graph_datas, const NodePtr &detach,
  328. const map<uint32_t, uint32_t> &outputs) {
  329. // Break Data and Move node.
  330. for (const auto &in_anchor : detach->GetAllInDataAnchors()) {
  331. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  332. if (out_anchor == nullptr) {
  333. continue;
  334. }
  335. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  336. const auto &owner_node = out_anchor->GetOwnerNode();
  337. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), detach->GetName().c_str());
  338. }
  339. // Break Move and follow, Link Data and follow.
  340. for (uint32_t i = 0; i < detach->GetAllOutDataAnchorsSize(); ++i) {
  341. auto it_idx = outputs.find(i);
  342. if (it_idx == outputs.end()) {
  343. GELOGE(FAILED, "Node: %s parent index %u not found", detach->GetName().c_str(), i);
  344. return FAILED;
  345. }
  346. auto it_data = graph_datas.find(it_idx->second);
  347. if (it_data == graph_datas.end()) {
  348. GELOGE(FAILED, "Node: %s parent index %u not found", detach->GetName().c_str(), i);
  349. return FAILED;
  350. }
  351. const auto &data_node = it_data->second;
  352. const auto &out_anchor = detach->GetOutDataAnchor(i);
  353. const auto &out_desc = detach->GetOpDesc()->GetOutputDesc(i);
  354. const auto &data_desc = data_node->GetOpDesc();
  355. (void)data_desc->UpdateInputDesc(kDataOutIndex, out_desc); // Set Data Input to new connect Node.
  356. (void)data_desc->UpdateOutputDesc(kDataOutIndex, out_desc); // Set Data Output to new connect Node.
  357. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  358. if (in_anchor == nullptr) {
  359. continue;
  360. }
  361. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  362. const auto &owner_node = in_anchor->GetOwnerNode();
  363. GELOGI("Remove Edge: %s %s", detach->GetName().c_str(), owner_node->GetName().c_str());
  364. const auto &data_out_anchor = data_node->GetOutDataAnchor(kDataOutIndex);
  365. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(data_out_anchor, in_anchor), "Add edge failed");
  366. GELOGI("Add Edge: %s %s", data_node->GetName().c_str(), owner_node->GetName().c_str());
  367. }
  368. }
  369. return SUCCESS;
  370. }
  371. ///
  372. /// @ingroup ge
  373. /// @brief Move Node to Parent Graph.
  374. /// @param [in] graph: Parent compute graph.
  375. /// @param [in] func_node: functional Node of Case.
  376. /// @param [in] attach: Node will move to parent.
  377. /// @param [in] inputs: Parent index of Node input.
  378. /// @param [in] outputs: Parent index of Node output.
  379. /// @return 0: SUCCESS / others: FAILED
  380. ///
  381. Status SubexpressionMigrationPass::AttachParallelNode(const ComputeGraphPtr &graph, const NodePtr &func_node,
  382. const NodePtr &attach, const map<uint32_t, uint32_t> &inputs,
  383. const map<uint32_t, uint32_t> &outputs) {
  384. GE_CHECK_NOTNULL(attach);
  385. for (uint32_t i = 0; i < attach->GetAllInDataAnchorsSize(); ++i) {
  386. auto it_idx = inputs.find(i);
  387. if (it_idx == inputs.end()) {
  388. GELOGE(FAILED, "Node: %s parent index %u not found", attach->GetName().c_str(), i);
  389. return FAILED;
  390. }
  391. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  392. continue;
  393. }
  394. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  395. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  396. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, attach->GetInDataAnchor(i)), "Add edge failed");
  397. const auto &owner_node = out_anchor->GetOwnerNode();
  398. GELOGI("Add Edge: %s %s", owner_node->GetName().c_str(), attach->GetName().c_str());
  399. }
  400. for (uint32_t i = 0; i < attach->GetAllOutDataAnchorsSize(); ++i) {
  401. auto it_idx = outputs.find(i);
  402. if (it_idx == outputs.end()) {
  403. return FAILED;
  404. }
  405. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  406. continue;
  407. }
  408. const auto &out_desc = attach->GetOpDesc()->GetOutputDesc(i);
  409. const auto &func_desc = func_node->GetOpDesc();
  410. (void)func_desc->UpdateInputDesc(it_idx->second, out_desc); // Set Data Input to new connect Node.
  411. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  412. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  413. if (out_anchor != nullptr) {
  414. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor), "Remove edge failed");
  415. const auto &owner_node = out_anchor->GetOwnerNode();
  416. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), func_node->GetName().c_str());
  417. }
  418. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(attach->GetOutDataAnchor(i), in_anchor), "Add edge failed");
  419. GELOGI("Add Edge: %s %s", attach->GetName().c_str(), func_node->GetName().c_str());
  420. }
  421. (void)graph->AddNode(attach);
  422. (void)attach->SetOwnerComputeGraph(graph);
  423. GELOGI("Add Node: %s %s", graph->GetName().c_str(), attach->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] anchor_idx: 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 SubexpressionMigrationPass::MoveNodeToParent(const ComputeGraphPtr &graph, const NodePtr &func_node,
  438. const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  439. uint32_t anchor_idx, const map<uint32_t, uint32_t> &inputs,
  440. const map<uint32_t, uint32_t> &outputs) {
  441. if (inputs.empty()) {
  442. GELOGE(FAILED, "Graph: %s, inputs is empty", graph->GetName().c_str());
  443. return FAILED;
  444. }
  445. NodePtr move_node;
  446. uint32_t base_index = inputs.begin()->second;
  447. for (auto &groups : graph_nodes) {
  448. const auto &subgraph = groups.first;
  449. const auto &subnodes = groups.second;
  450. auto it = subnodes.find(base_index);
  451. if (it == subnodes.end()) {
  452. GELOGE(FAILED, "Graph: %s, Data: %u node not found", subgraph->GetName().c_str(), base_index);
  453. return FAILED;
  454. }
  455. const auto &base_data = it->second;
  456. const auto &out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  457. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  458. const auto &in_anchor = in_anchors.at(anchor_idx);
  459. if (in_anchor == nullptr) {
  460. GELOGE(FAILED, "Data anchor index: %u, anchor size: %zu", anchor_idx, in_anchors.size());
  461. return FAILED;
  462. }
  463. move_node = in_anchor->GetOwnerNode();
  464. if (move_node == nullptr) {
  465. GELOGE(FAILED, "Data: %s not found, index: %u", base_data->GetName().c_str(), base_index);
  466. return FAILED;
  467. }
  468. if (DetachParallelNode(subnodes, move_node, outputs) != SUCCESS) {
  469. GELOGE(FAILED, "Data: %s not found, index: %u", base_data->GetName().c_str(), base_index);
  470. return FAILED;
  471. }
  472. GE_CHK_GRAPH_STATUS_RET(subgraph->RemoveNode(move_node), "Remove node failed");
  473. GELOGI("Remove Node: %s %s", subgraph->GetName().c_str(), move_node->GetName().c_str());
  474. }
  475. if (AttachParallelNode(graph, func_node, move_node, inputs, outputs) != SUCCESS) {
  476. return FAILED;
  477. }
  478. return SUCCESS;
  479. }
  480. } // namespace ge

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