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

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

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