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

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

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