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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 "graph/passes/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, "[Get][SubGraph] from graph:%s by name:%s failed",
  128. graph->GetName().c_str(), name.c_str());
  129. return GE_GRAPH_EMPTY_SUBGRAPH;
  130. }
  131. auto &data_nodes = graph_nodes[subgraph];
  132. for (auto &data : subgraph->GetDirectNode()) {
  133. if (data->GetType() != DATA) {
  134. continue;
  135. }
  136. uint32_t parent_index = 0;
  137. if (!AttrUtils::GetInt(data->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  138. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  139. data->GetName().c_str(), data->GetType().c_str());
  140. GELOGE(FAILED, "[Get][Attr] %s from op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  141. data->GetName().c_str(), data->GetType().c_str());
  142. return FAILED;
  143. }
  144. data_nodes[parent_index] = data;
  145. GELOGD("%s, Parent index: %u, Data: %s", subgraph->GetName().c_str(), parent_index, data->GetName().c_str());
  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. REPORT_INNER_ERROR("E19999", "Find node in data_nodes by index:%u failed", node_idx);
  206. GELOGE(FAILED, "[Check][Param] Find node in data_nodes by index:%u failed", node_idx);
  207. return false;
  208. }
  209. const auto &work_data = data_it->second;
  210. const auto &out_anchor = work_data->GetOutDataAnchor(kDataOutIndex);
  211. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  212. const auto &in_anchor = in_anchors.at(anchor_idx);
  213. if (in_anchor == nullptr) {
  214. REPORT_INNER_ERROR("E19999", "Index:%u anchor not exist in out:%u data anchor's peer of node:%s(%s)",
  215. node_idx, kDataOutIndex, work_data->GetName().c_str(), work_data->GetType().c_str());
  216. GELOGE(FAILED, "[Check][Param] Index:%u anchor not exist in out:%u data anchor's peer of node:%s(%s)",
  217. node_idx, kDataOutIndex, work_data->GetName().c_str(), work_data->GetType().c_str());
  218. return false;
  219. }
  220. const auto &work_node = in_anchor->GetOwnerNode();
  221. if (work_node == nullptr) {
  222. REPORT_INNER_ERROR("E19999", "Owner node of anchor is nullptr, check invalid");
  223. GELOGE(FAILED, "[Check][Param] Owner node of anchor is nullptr");
  224. return false;
  225. }
  226. if (!IsSameOpDesc(base_node->GetOpDesc(), work_node->GetOpDesc())) {
  227. GELOGI("OpDesc diff: %s %s", base_node->GetName().c_str(), work_node->GetName().c_str());
  228. return false;
  229. }
  230. }
  231. return true;
  232. }
  233. ///
  234. /// @ingroup ge
  235. /// @brief Migration subgraph Node to Root
  236. /// @param [in] graph: Root compute graph.
  237. /// @param [in] func_node: functional Node of Case.
  238. /// @param [in] graph_nodes: Data groups of subgraph.
  239. /// @param [in] data_base: Data Node for migration.
  240. /// @param [in] data_idx: Data groups of subgraph.
  241. /// @return 0: SUCCESS / others: FAILED
  242. ///
  243. Status SubexpressionMigrationPass::GraphNodeMigration(const ComputeGraphPtr &graph, const NodePtr &func_node,
  244. map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  245. const NodePtr &base_data, uint32_t base_idx) {
  246. bool can_extrapolation = false;
  247. do {
  248. can_extrapolation = false;
  249. const auto out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  250. const auto in_anchors = out_anchor->GetPeerInDataAnchors();
  251. for (size_t i = 0; i < in_anchors.size(); ++i) {
  252. const auto &in_anchor = in_anchors.at(i);
  253. const auto &base_node = in_anchor->GetOwnerNode();
  254. GELOGD("Get Data direct node: %s", base_node->GetName().c_str());
  255. if (!base_node->GetHostNode() || base_node->GetType() == SWITCH) {
  256. continue;
  257. }
  258. // Get associated Data, if Data feed other nodes, need append new Data.
  259. map<uint32_t, uint32_t> inputs;
  260. map<uint32_t, uint32_t> outputs;
  261. if (!GetAssociatedNodes(base_node, inputs, outputs)) {
  262. continue;
  263. }
  264. if (!IsParallelNodeSame(graph_nodes, base_node, base_idx, i)) {
  265. continue;
  266. }
  267. GELOGI("Move to parent: %s, parent index: %u", base_node->GetName().c_str(), base_idx);
  268. if (AppendParallelNode(graph_nodes, func_node, outputs) != SUCCESS) {
  269. return FAILED;
  270. }
  271. if (MoveNodeToParent(graph, func_node, graph_nodes, i, inputs, outputs) != SUCCESS) {
  272. return FAILED;
  273. }
  274. can_extrapolation = true;
  275. break;
  276. }
  277. } while (can_extrapolation);
  278. return SUCCESS;
  279. }
  280. ///
  281. /// @ingroup ge
  282. /// @brief Append Input Tensor for functional node.
  283. /// @param [in] graph_nodes: Data groups of subgraph.
  284. /// @param [in] func_node: functional Node of Case.
  285. /// @param [in] outputs: Parent index of Node output.
  286. /// @return 0: SUCCESS / others: FAILED
  287. ///
  288. Status SubexpressionMigrationPass::AppendParallelNode(map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  289. const NodePtr &func_node, map<uint32_t, uint32_t> &outputs) {
  290. // If outputs index invalid, add Data and Input Tensor.
  291. for (auto &item : outputs) {
  292. if (item.second != kInvalidParent) {
  293. continue;
  294. }
  295. // Add Data to subgraph.
  296. map<ComputeGraphPtr, uint32_t> append_num;
  297. for (auto &groups : graph_nodes) {
  298. const auto &subgraph = groups.first;
  299. auto &data_nodes = groups.second;
  300. item.second = func_node->GetAllInDataAnchorsSize() + append_num[subgraph]; // Update to valid parent index.
  301. std::string data_name = subgraph->GetName() + "_data_" + std::to_string(item.second);
  302. OpDescBuilder op_builder(data_name, DATA);
  303. const OpDescPtr op_desc = op_builder.AddInput("x").AddOutput("y").Build();
  304. if (op_desc == nullptr) {
  305. REPORT_CALL_ERROR("E19999", "Build op:%s(%s) failed", data_name.c_str(), DATA);
  306. GELOGE(OUT_OF_MEMORY, "[Build][Op] %s(%s) failed", data_name.c_str(), DATA);
  307. return OUT_OF_MEMORY;
  308. }
  309. uint32_t data_index = item.second - kCaseInputBase;
  310. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_INDEX, data_index)) {
  311. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_INDEX.c_str(),
  312. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  313. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_NAME_INDEX.c_str(),
  314. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  315. return FAILED;
  316. }
  317. if (!AttrUtils::SetInt(op_desc, ATTR_NAME_PARENT_NODE_INDEX, item.second)) {
  318. REPORT_CALL_ERROR("E19999", "Set Attr:%s to op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  319. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  320. GELOGE(FAILED, "[Set][Attr] %s to op:%s(%s) failed", ATTR_NAME_PARENT_NODE_INDEX.c_str(),
  321. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  322. return FAILED;
  323. }
  324. append_num[subgraph]++;
  325. data_nodes[item.second] = subgraph->AddNode(op_desc);
  326. GELOGI("Add Node: %s, parent index: %u", op_desc->GetName().c_str(), item.second);
  327. }
  328. // Add InputTensor to functional Node.
  329. GE_CHK_GRAPH_STATUS_RET(NodeUtils::AppendInputAnchor(func_node, item.second + 1),
  330. "[Append][InputAnchor] for node:%s failed", func_node->GetName().c_str());
  331. migration_append_ = true;
  332. }
  333. return SUCCESS;
  334. }
  335. ///
  336. /// @ingroup ge
  337. /// @brief Delete Node from all subgraph.
  338. /// @param [in] graph_nodes: Data groups of subgraph.
  339. /// @param [in] detach: Node will move to parent.
  340. /// @param [in] outputs: Parent index of Node output.
  341. /// @return 0: SUCCESS / others: FAILED
  342. ///
  343. Status SubexpressionMigrationPass::DetachParallelNode(const map<uint32_t, NodePtr> &graph_datas, const NodePtr &detach,
  344. const map<uint32_t, uint32_t> &outputs) {
  345. // Break Data and Move node.
  346. for (const auto &in_anchor : detach->GetAllInDataAnchors()) {
  347. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  348. if (out_anchor == nullptr) {
  349. continue;
  350. }
  351. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  352. "[Remove][Edge] between %s and %s failed",
  353. out_anchor->GetOwnerNode()->GetName().c_str(), detach->GetName().c_str());
  354. const auto &owner_node = out_anchor->GetOwnerNode();
  355. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), detach->GetName().c_str());
  356. }
  357. // Break Move and follow, Link Data and follow.
  358. for (uint32_t i = 0; i < detach->GetAllOutDataAnchorsSize(); ++i) {
  359. auto it_idx = outputs.find(i);
  360. if (it_idx == outputs.end()) {
  361. REPORT_INNER_ERROR("E19999", "Node:%s parent index %u not found, check invalid", detach->GetName().c_str(), i);
  362. GELOGE(FAILED, "[Check][Param] Node:%s parent index %u not found", detach->GetName().c_str(), i);
  363. return FAILED;
  364. }
  365. auto it_data = graph_datas.find(it_idx->second);
  366. if (it_data == graph_datas.end()) {
  367. REPORT_INNER_ERROR("E19999", "Node:%s parent index %u not found, check invalid", detach->GetName().c_str(), i);
  368. GELOGE(FAILED, "[Check][Param] Node:%s parent index %u not found", detach->GetName().c_str(), i);
  369. return FAILED;
  370. }
  371. const auto &data_node = it_data->second;
  372. const auto &out_anchor = detach->GetOutDataAnchor(i);
  373. const auto &out_desc = detach->GetOpDesc()->GetOutputDesc(i);
  374. const auto &data_desc = data_node->GetOpDesc();
  375. (void)data_desc->UpdateInputDesc(kDataOutIndex, out_desc); // Set Data Input to new connect Node.
  376. (void)data_desc->UpdateOutputDesc(kDataOutIndex, out_desc); // Set Data Output to new connect Node.
  377. for (const auto &in_anchor : out_anchor->GetPeerInDataAnchors()) {
  378. if (in_anchor == nullptr) {
  379. continue;
  380. }
  381. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  382. "[Remove][Edge] between %s and %s failed",
  383. detach->GetName().c_str(), in_anchor->GetOwnerNode()->GetName().c_str());
  384. const auto &owner_node = in_anchor->GetOwnerNode();
  385. GELOGI("Remove Edge: %s %s", detach->GetName().c_str(), owner_node->GetName().c_str());
  386. const auto &data_out_anchor = data_node->GetOutDataAnchor(kDataOutIndex);
  387. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(data_out_anchor, in_anchor),
  388. "[Add][Edge] between %s and %s failed",
  389. data_node->GetName().c_str(), owner_node->GetName().c_str());
  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 SubexpressionMigrationPass::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 (uint32_t i = 0; i < attach->GetAllInDataAnchorsSize(); ++i) {
  410. auto it_idx = inputs.find(i);
  411. if (it_idx == inputs.end()) {
  412. REPORT_INNER_ERROR("E19999", "Node:%s parent index %u not found, check invalid", attach->GetName().c_str(), i);
  413. GELOGE(FAILED, "[Check][Param] Node:%s parent index %u not found", attach->GetName().c_str(), i);
  414. return FAILED;
  415. }
  416. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  417. continue;
  418. }
  419. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  420. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  421. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(out_anchor, attach->GetInDataAnchor(i)),
  422. "[Add][Edge] between %s and %s failed",
  423. out_anchor->GetOwnerNode()->GetName().c_str(), attach->GetName().c_str());
  424. const auto &owner_node = out_anchor->GetOwnerNode();
  425. GELOGI("Add Edge: %s %s", owner_node->GetName().c_str(), attach->GetName().c_str());
  426. }
  427. for (uint32_t i = 0; i < attach->GetAllOutDataAnchorsSize(); ++i) {
  428. auto it_idx = outputs.find(i);
  429. if (it_idx == outputs.end()) {
  430. return FAILED;
  431. }
  432. if (it_idx->second == kInvalidParent) { // Not connect, Skip.
  433. continue;
  434. }
  435. const auto &out_desc = attach->GetOpDesc()->GetOutputDesc(i);
  436. const auto &func_desc = func_node->GetOpDesc();
  437. (void)func_desc->UpdateInputDesc(it_idx->second, out_desc); // Set Data Input to new connect Node.
  438. const auto &in_anchor = func_node->GetInDataAnchor(it_idx->second);
  439. const auto &out_anchor = in_anchor->GetPeerOutAnchor();
  440. if (out_anchor != nullptr) {
  441. GE_CHK_GRAPH_STATUS_RET(GraphUtils::RemoveEdge(out_anchor, in_anchor),
  442. "[Remove][Edge] between %s and %s failed",
  443. out_anchor->GetOwnerNode()->GetName().c_str(), func_node->GetName().c_str());
  444. const auto &owner_node = out_anchor->GetOwnerNode();
  445. GELOGI("Remove Edge: %s %s", owner_node->GetName().c_str(), func_node->GetName().c_str());
  446. }
  447. GE_CHK_GRAPH_STATUS_RET(GraphUtils::AddEdge(attach->GetOutDataAnchor(i), in_anchor),
  448. "[Add][Edge] between %s and %s failed",
  449. attach->GetName().c_str(), func_node->GetName().c_str());
  450. GELOGI("Add Edge: %s %s", attach->GetName().c_str(), func_node->GetName().c_str());
  451. }
  452. (void)graph->AddNode(attach);
  453. (void)attach->SetOwnerComputeGraph(graph);
  454. GELOGI("Add Node: %s %s", graph->GetName().c_str(), attach->GetName().c_str());
  455. return SUCCESS;
  456. }
  457. ///
  458. /// @ingroup ge
  459. /// @brief Move node to Parent graph.
  460. /// @param [in] graph: Root compute graph.
  461. /// @param [in] func_node: functional Node of Case.
  462. /// @param [in] graph_nodes: Data groups of subgraph.
  463. /// @param [in] anchor_idx: anchor index of move Node.
  464. /// @param [in] inputs: Parent index of Node input.
  465. /// @param [in] outputs: Parent index of Node output.
  466. /// @return 0: SUCCESS / others: FAILED
  467. ///
  468. Status SubexpressionMigrationPass::MoveNodeToParent(const ComputeGraphPtr &graph, const NodePtr &func_node,
  469. const map<ComputeGraphPtr, map<uint32_t, NodePtr>> &graph_nodes,
  470. uint32_t anchor_idx, const map<uint32_t, uint32_t> &inputs,
  471. const map<uint32_t, uint32_t> &outputs) {
  472. if (inputs.empty()) {
  473. REPORT_INNER_ERROR("E19999", "Param inputs is empty, check invalid");
  474. GELOGE(FAILED, "[Check][Param] Param inputs is empty");
  475. return FAILED;
  476. }
  477. NodePtr move_node;
  478. uint32_t base_index = inputs.begin()->second;
  479. for (auto &groups : graph_nodes) {
  480. const auto &subgraph = groups.first;
  481. const auto &subnodes = groups.second;
  482. auto it = subnodes.find(base_index);
  483. if (it == subnodes.end()) {
  484. REPORT_INNER_ERROR("E19999", "Index:%u data node not found in graph:%s, check invalid",
  485. base_index, subgraph->GetName().c_str());
  486. GELOGE(FAILED, "[Check][Param] Index:%u data node not found in graph:%s",
  487. base_index, subgraph->GetName().c_str());
  488. return FAILED;
  489. }
  490. const auto &base_data = it->second;
  491. const auto &out_anchor = base_data->GetOutDataAnchor(kDataOutIndex);
  492. const auto &in_anchors = out_anchor->GetPeerInDataAnchors();
  493. const auto &in_anchor = in_anchors.at(anchor_idx);
  494. if (in_anchor == nullptr) {
  495. REPORT_INNER_ERROR("E19999", "Index:%u anchor not exist in out:%u data anchor's peer of node:%s(%s)",
  496. anchor_idx, kDataOutIndex, base_data->GetName().c_str(), base_data->GetType().c_str());
  497. GELOGE(FAILED, "[Check][Param] Index:%u anchor not exist in out:%u data anchor's peer of node:%s(%s)",
  498. anchor_idx, kDataOutIndex, base_data->GetName().c_str(), base_data->GetType().c_str());
  499. return FAILED;
  500. }
  501. move_node = in_anchor->GetOwnerNode();
  502. if (move_node == nullptr) {
  503. REPORT_INNER_ERROR("E19999", "Owner node of anchor is nullptr, check invalid");
  504. GELOGE(FAILED, "[Check][Param] Owner node of anchor is nullptr");
  505. return FAILED;
  506. }
  507. if (DetachParallelNode(subnodes, move_node, outputs) != SUCCESS) {
  508. GELOGE(FAILED, "[Detach][ParallelNode] failed, move_node:%s", move_node->GetName().c_str());
  509. return FAILED;
  510. }
  511. GE_CHK_GRAPH_STATUS_RET(subgraph->RemoveNode(move_node),
  512. "[Remove][Node] %s from graph:%s failed",
  513. move_node->GetName().c_str(), graph->GetName().c_str());
  514. GELOGI("Remove Node: %s %s", subgraph->GetName().c_str(), move_node->GetName().c_str());
  515. }
  516. if (AttachParallelNode(graph, func_node, move_node, inputs, outputs) != SUCCESS) {
  517. return FAILED;
  518. }
  519. return SUCCESS;
  520. }
  521. } // namespace ge

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