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_pass.cc 20 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/subgraph_pass.h"
  17. #include "graph/utils/node_utils.h"
  18. #include "graph/utils/op_desc_utils.h"
  19. #include "graph/utils/tensor_utils.h"
  20. namespace ge {
  21. /**
  22. * @ingroup ge
  23. * @brief Subgraph optimizer.
  24. * @param [in] graph: Input ComputeGraph
  25. * @return: 0 for success / others for fail
  26. */
  27. Status SubgraphPass::Run(ComputeGraphPtr graph) {
  28. const bool is_sub_graph = graph->GetParentNode() != nullptr;
  29. for (const NodePtr &node : graph->GetDirectNode()) {
  30. if (is_sub_graph && (node->GetType() == DATA)) {
  31. if (SubgraphInputNode(graph, node) != SUCCESS) {
  32. GELOGE(FAILED, "Handle input %s of subgraph failed.", node->GetName().c_str());
  33. return FAILED;
  34. }
  35. continue;
  36. }
  37. // NetOutput in subgraph
  38. if (is_sub_graph && (node->GetType() == NETOUTPUT)) {
  39. if (SubgraphOutputNode(graph, node) != SUCCESS) {
  40. GELOGE(FAILED, "Handle output %s of subgraph failed.", node->GetName().c_str());
  41. return FAILED;
  42. }
  43. continue;
  44. }
  45. if (kWhileOpTypes.count(node->GetType()) > 0) {
  46. // Input->While and Input link to other nodes
  47. if (WhileInputNodes(graph, node) != SUCCESS) {
  48. GELOGE(FAILED, "Handle input of while_body failed, while:%s.", node->GetName().c_str());
  49. return FAILED;
  50. }
  51. // body subgraph of While op
  52. if (WhileBodySubgraph(graph, node) != SUCCESS) {
  53. GELOGE(FAILED, "Handle while_body failed, while:%s.", node->GetName().c_str());
  54. return FAILED;
  55. }
  56. continue;
  57. }
  58. }
  59. return SUCCESS;
  60. }
  61. /**
  62. * @ingroup ge
  63. * @brief Check Subgraph Input node
  64. * @param [in] graph: ComputeGraph.
  65. * @param [in] node: Data node in Subgraph.
  66. * @return: 0 for SUCCESS / others for FAILED
  67. */
  68. Status SubgraphPass::SubgraphInputNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  69. GELOGD("Handle input_node %s for graph %s.", node->GetName().c_str(), graph->GetName().c_str());
  70. // Data has and only has one output
  71. bool input_continues_required_flag = false;
  72. OutDataAnchorPtr out_data_anchor = node->GetOutDataAnchor(0);
  73. std::vector<InDataAnchorPtr> in_anchors;
  74. for (const InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  75. input_continues_required_flag =
  76. input_continues_required_flag || IsInputContinuesRequired(peer_in_anchor->GetOwnerNode());
  77. in_anchors.emplace_back(peer_in_anchor);
  78. }
  79. // Data->InputContinuesRequiredOp in subgraph need memcpy.
  80. if (input_continues_required_flag) {
  81. GELOGD("Data %s output_node required continues input.", node->GetName().c_str());
  82. std::string name = node->GetName() + "_output_0_Memcpy";
  83. if (InsertMemcpyNode(graph, out_data_anchor, in_anchors, name) != SUCCESS) {
  84. GELOGE(FAILED, "Insert memcpy after %s failed.", node->GetName().c_str());
  85. return FAILED;
  86. }
  87. }
  88. uint32_t parent_index = 0;
  89. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, parent_index)) {
  90. GELOGE(FAILED, "Get attr PARENT_NODE_INDEX failed, node:%s.", node->GetName().c_str());
  91. return FAILED;
  92. }
  93. // Subgraph Data Node, check for constant input.
  94. std::string const_type;
  95. if (!NodeUtils::GetConstOpType(node, const_type)) {
  96. return SUCCESS;
  97. }
  98. const NodePtr &parent_node = graph->GetParentNode();
  99. if (kWhileOpTypes.count(parent_node->GetType()) != 0) {
  100. // Constant input to While need memcpy.
  101. const ComputeGraphPtr &parent_graph = parent_node->GetOwnerComputeGraph();
  102. GE_CHECK_NOTNULL(parent_graph);
  103. const InDataAnchorPtr &in_data_anchor = parent_node->GetInDataAnchor(parent_index);
  104. GE_CHECK_NOTNULL(in_data_anchor);
  105. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  106. GE_CHECK_NOTNULL(peer_out_anchor);
  107. GELOGD("Constant input %s links to While %s.", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  108. parent_node->GetName().c_str());
  109. std::string name = parent_node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx()) + "_Memcpy";
  110. if (InsertMemcpyNode(parent_graph, peer_out_anchor, {in_data_anchor}, name) != SUCCESS) {
  111. GELOGE(FAILED, "Insert memcpy between %s and %s failed.", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  112. parent_node->GetName().c_str());
  113. return FAILED;
  114. }
  115. }
  116. return SUCCESS;
  117. }
  118. /**
  119. * @ingroup ge
  120. * @brief Check Subgraph Output node
  121. * @param [in] graph: ComputeGraph.
  122. * @param [in] node: NetOutput node in Subgraph.
  123. * @return: 0 for SUCCESS / others for FAILED
  124. */
  125. Status SubgraphPass::SubgraphOutputNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  126. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  127. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  128. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  129. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  130. GE_CHECK_NOTNULL(in_node);
  131. // Need insert memcpy
  132. // 1. Const->NetOutput in subgraph
  133. // 2. AtomicOp->NetOutput in subgraph
  134. // 3. OutputContinuesRequiredOp->NetOutput in subgraph
  135. // 4. Data->NetOutput in subgraph but parent_node is not while
  136. // 5. While->NetOutput in known subgraph
  137. std::string op_type;
  138. bool insert_flag = NodeUtils::GetConstOpType(in_node, op_type) ||
  139. IsAtomicRequired(in_node, peer_out_anchor->GetIdx()) || IsOutputContinuesRequired(in_node) ||
  140. ((in_node->GetType() == DATA) &&
  141. (kWhileOpTypes.count(graph->GetParentNode()->GetType()) == 0)) ||
  142. (!graph->GetGraphUnknownFlag() && NodeUtils::IsDynamicShape(node) &&
  143. (kWhileOpTypes.count(in_node->GetType()) != 0));
  144. if (insert_flag) {
  145. GELOGD("Insert MemcpyAsync node between %s and %s.", in_node->GetName().c_str(), node->GetName().c_str());
  146. std::string name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx()) + "_Memcpy";
  147. if (InsertMemcpyNode(graph, peer_out_anchor, {in_data_anchor}, name) != SUCCESS) {
  148. GELOGE(FAILED, "Insert memcpy between %s and %s failed.", in_node->GetName().c_str(), node->GetName().c_str());
  149. return FAILED;
  150. }
  151. }
  152. }
  153. return SUCCESS;
  154. }
  155. /**
  156. * @ingroup ge
  157. * @brief Check is Input->While and Input link to other nodes
  158. * @param [in] graph: ComputeGraph.
  159. * @param [in] node: While node.
  160. * @return: 0 for SUCCESS / others for FAILED
  161. */
  162. Status SubgraphPass::WhileInputNodes(const ComputeGraphPtr &graph, const NodePtr &node) {
  163. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  164. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  165. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  166. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  167. GE_CHECK_NOTNULL(in_node);
  168. if (in_node->GetType() == VARIABLE || in_node->GetType() == VARHANDLEOP || in_node->GetType() == VARIABLEV2) {
  169. continue;
  170. }
  171. // Input->While and Input link to other nodes need insert memcpy
  172. if (peer_out_anchor->GetPeerInDataAnchors().size() > 1) {
  173. GELOGD("Input %s of While %s links to other nodes.", in_node->GetName().c_str(), node->GetName().c_str());
  174. std::string name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx()) + "_Memcpy";
  175. if (InsertMemcpyNode(graph, peer_out_anchor, {in_data_anchor}, name) != SUCCESS) {
  176. GELOGE(FAILED, "Insert memcpy between %s and %s failed.", in_node->GetName().c_str(), node->GetName().c_str());
  177. return FAILED;
  178. }
  179. }
  180. }
  181. return SUCCESS;
  182. }
  183. /**
  184. * @ingroup ge
  185. * @brief Check body subgraph of While op
  186. * @param [in] graph: ComputeGraph.
  187. * @param [in] node: While node.
  188. * @return: 0 for SUCCESS / others for FAILED
  189. */
  190. Status SubgraphPass::WhileBodySubgraph(const ComputeGraphPtr &graph, const NodePtr &node) {
  191. // index of body_subgraph is 1
  192. ComputeGraphPtr while_body = NodeUtils::GetSubgraph(*node, 1);
  193. if (while_body == nullptr) {
  194. GELOGE(FAILED, "while_body of %s is NULL.", node->GetName().c_str());
  195. return FAILED;
  196. }
  197. if (GraphUtils::IsUnknownShapeGraph(while_body)) {
  198. GELOGI("Unknown shape while_body graph %s no need to insert memcpy.", while_body->GetName().c_str());
  199. return SUCCESS;
  200. }
  201. // insert identity between data and labelswitch in while cond subgraph
  202. if (NodeUtils::IsDynamicShape(node)) {
  203. ComputeGraphPtr while_cond = NodeUtils::GetSubgraph(*node, 0);
  204. GE_CHECK_NOTNULL(while_cond);
  205. std::vector<NodePtr> cond_data_nodes;
  206. for (const auto &n : while_cond->GetDirectNode()) {
  207. if (n->GetType() == DATA) {
  208. cond_data_nodes.emplace_back(n);
  209. }
  210. }
  211. GE_CHK_STATUS_RET(InsertInputMemcpy(while_cond, cond_data_nodes), "InsertInputMemcpy failed.");
  212. }
  213. std::vector<NodePtr> data_nodes;
  214. std::set<uint32_t> bypass_index;
  215. NodePtr output_node = nullptr;
  216. for (const auto &n : while_body->GetDirectNode()) {
  217. const std::string &type = n->GetType();
  218. if (type == DATA) {
  219. if (CheckInsertInputMemcpy(n, bypass_index)) {
  220. data_nodes.emplace_back(n);
  221. }
  222. } else if (type == NETOUTPUT) {
  223. if (output_node == nullptr) {
  224. output_node = n;
  225. } else {
  226. GELOGE(FAILED, "while_body %s exists multi NetOutput nodes.", while_body->GetName().c_str());
  227. return FAILED;
  228. }
  229. }
  230. }
  231. if (output_node == nullptr) {
  232. GELOGE(FAILED, "while_body %s has no output.", while_body->GetName().c_str());
  233. return FAILED;
  234. }
  235. if ((InsertInputMemcpy(while_body, data_nodes) != SUCCESS) ||
  236. (InsertOutputMemcpy(while_body, output_node, bypass_index) != SUCCESS)) {
  237. GELOGE(FAILED, "Insert memcpy node in while_body %s failed.", while_body->GetName().c_str());
  238. return FAILED;
  239. }
  240. return SUCCESS;
  241. }
  242. /**
  243. * @ingroup ge
  244. * @brief Insert input memcpy node in while_body
  245. * @param [in] graph: while_body
  246. * @param [in] data_nodes: data_nodes
  247. * @return: 0 for SUCCESS / others for FAILED
  248. */
  249. Status SubgraphPass::InsertInputMemcpy(const ComputeGraphPtr &graph, const std::vector<NodePtr> &data_nodes) {
  250. if (data_nodes.empty()) {
  251. GELOGD("No need to insert input memcpy node in while_body %s.", graph->GetName().c_str());
  252. return SUCCESS;
  253. }
  254. std::string in_name = graph->GetName() + "_input_Memcpy";
  255. OpDescBuilder in_builder(in_name, IDENTITY);
  256. for (size_t i = 0; i < data_nodes.size(); i++) {
  257. // Data node has and only has one output
  258. in_builder.AddInput("x" + std::to_string(i), data_nodes[i]->GetOpDesc()->GetOutputDesc(0))
  259. .AddOutput("y" + std::to_string(i), data_nodes[i]->GetOpDesc()->GetOutputDesc(0));
  260. }
  261. GELOGD("Insert memcpy after data_nodes of while_body %s.", graph->GetName().c_str());
  262. NodePtr in_memcpy = graph->AddNode(in_builder.Build());
  263. GE_CHECK_NOTNULL(in_memcpy);
  264. for (size_t i = 0; i < data_nodes.size(); i++) {
  265. // Data node has and only has one output
  266. OutDataAnchorPtr out_data_anchor = data_nodes[i]->GetOutDataAnchor(0);
  267. std::vector<InDataAnchorPtr> in_anchors;
  268. for (const InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  269. in_anchors.emplace_back(peer_in_anchor);
  270. }
  271. if (InsertNodeBetween(out_data_anchor, in_anchors, in_memcpy, i, i) != SUCCESS) {
  272. GELOGE(FAILED, "Insert MemcpyAsync %s in while_body %s failed.", in_name.c_str(), graph->GetName().c_str());
  273. return FAILED;
  274. }
  275. }
  276. return SUCCESS;
  277. }
  278. /**
  279. * @ingroup ge
  280. * @brief Insert output memcpy node in while_body
  281. * @param [in] graph: while_body
  282. * @param [in] output_node: NetOutput
  283. * @param [in] bypass_index
  284. * @return: 0 for SUCCESS / others for FAILED
  285. */
  286. Status SubgraphPass::InsertOutputMemcpy(const ComputeGraphPtr &graph, const NodePtr &output_node,
  287. const std::set<uint32_t> &bypass_index) {
  288. if (output_node->GetAllInDataAnchorsSize() == bypass_index.size()) {
  289. GELOGD("No need to insert output memcpy node in while_body %s, output_size=%zu, bypass_num=%zu.",
  290. graph->GetName().c_str(), output_node->GetAllInDataAnchorsSize(), bypass_index.size());
  291. return SUCCESS;
  292. }
  293. std::string out_name = graph->GetName() + "_output_Memcpy";
  294. OpDescBuilder out_builder(out_name, IDENTITY);
  295. for (size_t i = 0; i < output_node->GetAllInDataAnchorsSize(); i++) {
  296. if (bypass_index.count(i) == 0) {
  297. out_builder.AddInput("x" + std::to_string(i), output_node->GetOpDesc()->GetInputDesc(i))
  298. .AddOutput("y" + std::to_string(i), output_node->GetOpDesc()->GetInputDesc(i));
  299. }
  300. }
  301. GELOGD("Insert memcpy before NetOutput of while_body %s.", graph->GetName().c_str());
  302. NodePtr out_memcpy = graph->AddNode(out_builder.Build());
  303. GE_CHECK_NOTNULL(out_memcpy);
  304. size_t cnt = 0;
  305. for (size_t i = 0; i < output_node->GetAllInDataAnchorsSize(); i++) {
  306. if (bypass_index.count(i) == 0) {
  307. InDataAnchorPtr in_data_anchor = output_node->GetInDataAnchor(i);
  308. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  309. if (InsertNodeBetween(peer_out_anchor, {in_data_anchor}, out_memcpy, cnt, cnt) != SUCCESS) {
  310. GELOGE(FAILED, "Insert MemcpyAsync %s in while_body %s failed.", out_name.c_str(), graph->GetName().c_str());
  311. return FAILED;
  312. }
  313. cnt++;
  314. }
  315. }
  316. return SUCCESS;
  317. }
  318. /**
  319. * @ingroup ge
  320. * @brief Check is data->netoutput without change in while body
  321. * @param [in] node: data node
  322. * @param [out] bypass_index
  323. * @return: false for data->netoutput without change in while body / for true for others
  324. */
  325. bool SubgraphPass::CheckInsertInputMemcpy(const NodePtr &node, std::set<uint32_t> &bypass_index) {
  326. uint32_t input_index = 0;
  327. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, input_index)) {
  328. return true;
  329. }
  330. // Data node has and only has one output
  331. OutDataAnchorPtr out_data_anchor = node->GetOutDataAnchor(0);
  332. if ((out_data_anchor == nullptr) || (out_data_anchor->GetPeerInDataAnchors().size() != 1)) {
  333. return true;
  334. }
  335. InDataAnchorPtr peer_in_anchor = out_data_anchor->GetPeerInDataAnchors().at(0);
  336. if (peer_in_anchor->GetOwnerNode()->GetType() != NETOUTPUT) {
  337. return true;
  338. }
  339. OpDescPtr op_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  340. uint32_t output_index = 0;
  341. if ((op_desc == nullptr) ||
  342. !AttrUtils::GetInt(op_desc->GetInputDesc(peer_in_anchor->GetIdx()), ATTR_NAME_PARENT_NODE_INDEX, output_index)) {
  343. return true;
  344. }
  345. if (input_index != output_index) {
  346. return true;
  347. }
  348. bypass_index.insert(peer_in_anchor->GetIdx());
  349. return false;
  350. }
  351. /**
  352. * @ingroup ge
  353. * @brief Check is AtomicOp->NetOutput
  354. * @param [in] node
  355. * @param [in] out_index
  356. * @return: true for AtomicOp->NetOutput / false for others
  357. */
  358. bool SubgraphPass::IsAtomicRequired(const NodePtr &node, int64_t out_index) {
  359. auto op_desc = node->GetOpDesc();
  360. if (op_desc != nullptr) {
  361. bool is_atomic = false;
  362. (void)ge::AttrUtils::GetBool(op_desc, ATOMIC_ATTR_IS_ATOMIC_NODE, is_atomic);
  363. if (is_atomic) {
  364. std::vector<int64_t> atomic_output_index;
  365. // If GetListInt fail, atomic_output_index is empty.
  366. (void)ge::AttrUtils::GetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_index);
  367. for (int64_t ind : atomic_output_index) {
  368. if (ind == out_index) {
  369. return true;
  370. }
  371. }
  372. }
  373. }
  374. return false;
  375. }
  376. /**
  377. * @ingroup ge
  378. * @brief Check is OutputContinuesRequiredOp->NetOutput
  379. * @param [in] node
  380. * @return: true for OutputContinuesRequiredOp->NetOutput / false for others
  381. */
  382. bool SubgraphPass::IsOutputContinuesRequired(const NodePtr &node) {
  383. OpDescPtr op_desc = node->GetOpDesc();
  384. if (op_desc != nullptr) {
  385. bool continuous_output_flag = false;
  386. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_OUTPUT, continuous_output_flag);
  387. bool no_padding_continuous_output_flag = false;
  388. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_OUTPUT, no_padding_continuous_output_flag);
  389. return continuous_output_flag || no_padding_continuous_output_flag;
  390. }
  391. return false;
  392. }
  393. /**
  394. * @ingroup ge
  395. * @brief Check is InputContinuesRequiredOp->NetOutput
  396. * @param [in] node
  397. * @return: true for InputContinuesRequiredOp->NetOutput / false for others
  398. */
  399. bool SubgraphPass::IsInputContinuesRequired(const NodePtr &node) {
  400. OpDescPtr op_desc = node->GetOpDesc();
  401. if (op_desc != nullptr) {
  402. bool continuous_input_flag = false;
  403. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT, continuous_input_flag);
  404. bool no_padding_continuous_input_flag = false;
  405. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_INPUT, no_padding_continuous_input_flag);
  406. return continuous_input_flag || no_padding_continuous_input_flag;
  407. }
  408. return false;
  409. }
  410. /**
  411. * @ingroup ge
  412. * @brief Insert memcpy node
  413. * @param [in] graph
  414. * @param [in] out_anchor
  415. * @param [in] in_anchors
  416. * @param [in] name
  417. * @return: 0 for success / others for fail
  418. */
  419. Status SubgraphPass::InsertMemcpyNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_anchor,
  420. const std::vector<InDataAnchorPtr> &in_anchors, const std::string &name) {
  421. GE_CHECK_NOTNULL(out_anchor);
  422. NodePtr in_node = out_anchor->GetOwnerNode();
  423. OpDescBuilder op_desc_builder(name, IDENTITY);
  424. OpDescPtr op_desc = op_desc_builder.AddInput("x", in_node->GetOpDesc()->GetOutputDesc(0))
  425. .AddOutput("y", in_node->GetOpDesc()->GetOutputDesc(0))
  426. .Build();
  427. (void)AttrUtils::SetBool(op_desc, ATTR_NO_NEED_CONSTANT_FOLDING, false);
  428. if (GraphUtils::InsertNodeAfter(out_anchor, in_anchors, graph->AddNode(op_desc)) != GRAPH_SUCCESS) {
  429. GELOGE(FAILED, "Insert IDENTITY node %s after %s failed.", name.c_str(), in_node->GetName().c_str());
  430. return FAILED;
  431. }
  432. return SUCCESS;
  433. }
  434. ///
  435. /// @brief Insert node: src->insert_node:input_index, insert_node:output_index->dst
  436. /// @param [in] src
  437. /// @param [in] dsts
  438. /// @param [in] insert_node
  439. /// @param [in] input_index
  440. /// @param [in] output_index
  441. /// @return Status
  442. ///
  443. Status SubgraphPass::InsertNodeBetween(const OutDataAnchorPtr &src, const std::vector<InDataAnchorPtr> &dsts,
  444. const NodePtr &insert_node, uint32_t input_index, uint32_t output_index) {
  445. if (GraphUtils::AddEdge(src, insert_node->GetInDataAnchor(input_index)) != GRAPH_SUCCESS) {
  446. GELOGE(FAILED, "Add data_edge %s:%d->%s:%u failed.",
  447. src->GetOwnerNode()->GetName().c_str(), src->GetIdx(), insert_node->GetName().c_str(), input_index);
  448. return FAILED;
  449. }
  450. for (const auto &dst : dsts) {
  451. GELOGD("Insert node %s between %s->%s.", insert_node->GetName().c_str(), src->GetOwnerNode()->GetName().c_str(),
  452. dst->GetOwnerNode()->GetName().c_str());
  453. if ((GraphUtils::RemoveEdge(src, dst) != GRAPH_SUCCESS) ||
  454. (GraphUtils::AddEdge(insert_node->GetOutDataAnchor(output_index), dst) != GRAPH_SUCCESS)) {
  455. GELOGE(FAILED, "Replace data_edge %s:%d->%s:%d by %s:%u->%s:%d failed.",
  456. src->GetOwnerNode()->GetName().c_str(), src->GetIdx(),
  457. dst->GetOwnerNode()->GetName().c_str(), dst->GetIdx(),
  458. insert_node->GetName().c_str(), output_index,
  459. dst->GetOwnerNode()->GetName().c_str(), dst->GetIdx());
  460. return FAILED;
  461. }
  462. }
  463. return SUCCESS;
  464. }
  465. } // namespace ge

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