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

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

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