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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /**
  2. * Copyright 2019-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. std::string op_type;
  137. bool insert_flag = NodeUtils::GetConstOpType(in_node, op_type) ||
  138. IsAtomicRequired(in_node, peer_out_anchor->GetIdx()) || IsOutputContinuesRequired(in_node) ||
  139. ((in_node->GetType() == DATA) && (kWhileOpTypes.count(graph->GetParentNode()->GetType()) == 0));
  140. if (insert_flag) {
  141. GELOGD("Insert MemcpyAsync node between %s and %s.", in_node->GetName().c_str(), node->GetName().c_str());
  142. std::string name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx()) + "_Memcpy";
  143. if (InsertMemcpyNode(graph, peer_out_anchor, {in_data_anchor}, name) != SUCCESS) {
  144. GELOGE(FAILED, "Insert memcpy between %s and %s failed.", in_node->GetName().c_str(), node->GetName().c_str());
  145. return FAILED;
  146. }
  147. }
  148. }
  149. return SUCCESS;
  150. }
  151. /**
  152. * @ingroup ge
  153. * @brief Check is Input->While and Input link to other nodes
  154. * @param [in] graph: ComputeGraph.
  155. * @param [in] node: While node.
  156. * @return: 0 for SUCCESS / others for FAILED
  157. */
  158. Status SubgraphPass::WhileInputNodes(const ComputeGraphPtr &graph, const NodePtr &node) {
  159. for (InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  160. const OutDataAnchorPtr &peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  161. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  162. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  163. GE_CHECK_NOTNULL(in_node);
  164. if (in_node->GetType() == VARIABLE || in_node->GetType() == VARHANDLEOP || in_node->GetType() == VARIABLEV2) {
  165. continue;
  166. }
  167. // Input->While and Input link to other nodes need insert memcpy
  168. if (peer_out_anchor->GetPeerInDataAnchors().size() > 1) {
  169. GELOGD("Input %s of While %s links to other nodes.", in_node->GetName().c_str(), node->GetName().c_str());
  170. std::string name = node->GetName() + "_input_" + std::to_string(in_data_anchor->GetIdx()) + "_Memcpy";
  171. if (InsertMemcpyNode(graph, peer_out_anchor, {in_data_anchor}, name) != SUCCESS) {
  172. GELOGE(FAILED, "Insert memcpy between %s and %s failed.", in_node->GetName().c_str(), node->GetName().c_str());
  173. return FAILED;
  174. }
  175. }
  176. }
  177. return SUCCESS;
  178. }
  179. /**
  180. * @ingroup ge
  181. * @brief Check body subgraph of While op
  182. * @param [in] graph: ComputeGraph.
  183. * @param [in] node: While node.
  184. * @return: 0 for SUCCESS / others for FAILED
  185. */
  186. Status SubgraphPass::WhileBodySubgraph(const ComputeGraphPtr &graph, const NodePtr &node) {
  187. // index of body_subgraph is 1
  188. ComputeGraphPtr while_body = NodeUtils::GetSubgraph(*node, 1);
  189. if (while_body == nullptr) {
  190. GELOGE(FAILED, "while_body of %s is NULL.", node->GetName().c_str());
  191. return FAILED;
  192. }
  193. if (GraphUtils::IsUnknownShapeGraph(while_body)) {
  194. GELOGI("Unknown shape while_body graph %s no need to insert memcpy.", while_body->GetName().c_str());
  195. return SUCCESS;
  196. }
  197. std::vector<NodePtr> data_nodes;
  198. std::set<uint32_t> bypass_index;
  199. NodePtr output_node = nullptr;
  200. for (const auto &n : while_body->GetDirectNode()) {
  201. const std::string &type = n->GetType();
  202. if (type == DATA) {
  203. if (CheckInsertInputMemcpy(n, bypass_index)) {
  204. data_nodes.emplace_back(n);
  205. }
  206. } else if (type == NETOUTPUT) {
  207. if (output_node == nullptr) {
  208. output_node = n;
  209. } else {
  210. GELOGE(FAILED, "while_body %s exists multi NetOutput nodes.", while_body->GetName().c_str());
  211. return FAILED;
  212. }
  213. }
  214. }
  215. if (output_node == nullptr) {
  216. GELOGE(FAILED, "while_body %s has no output.", while_body->GetName().c_str());
  217. return FAILED;
  218. }
  219. if ((InsertInputMemcpy(while_body, data_nodes) != SUCCESS) ||
  220. (InsertOutputMemcpy(while_body, output_node, bypass_index) != SUCCESS)) {
  221. GELOGE(FAILED, "Insert memcpy node in while_body %s failed.", while_body->GetName().c_str());
  222. return FAILED;
  223. }
  224. return SUCCESS;
  225. }
  226. /**
  227. * @ingroup ge
  228. * @brief Insert input memcpy node in while_body
  229. * @param [in] graph: while_body
  230. * @param [in] data_nodes: data_nodes
  231. * @return: 0 for SUCCESS / others for FAILED
  232. */
  233. Status SubgraphPass::InsertInputMemcpy(const ComputeGraphPtr &graph, const std::vector<NodePtr> &data_nodes) {
  234. if (data_nodes.empty()) {
  235. GELOGD("No need to insert input memcpy node in while_body %s.", graph->GetName().c_str());
  236. return SUCCESS;
  237. }
  238. std::string in_name = graph->GetName() + "_input_Memcpy";
  239. OpDescBuilder in_builder(in_name, IDENTITY);
  240. for (size_t i = 0; i < data_nodes.size(); i++) {
  241. // Data node has and only has one output
  242. in_builder.AddInput("x" + std::to_string(i), data_nodes[i]->GetOpDesc()->GetOutputDesc(0))
  243. .AddOutput("y" + std::to_string(i), data_nodes[i]->GetOpDesc()->GetOutputDesc(0));
  244. }
  245. GELOGD("Insert memcpy after data_nodes of while_body %s.", graph->GetName().c_str());
  246. NodePtr in_memcpy = graph->AddNode(in_builder.Build());
  247. GE_CHECK_NOTNULL(in_memcpy);
  248. for (size_t i = 0; i < data_nodes.size(); i++) {
  249. // Data node has and only has one output
  250. OutDataAnchorPtr out_data_anchor = data_nodes[i]->GetOutDataAnchor(0);
  251. std::vector<InDataAnchorPtr> in_anchors;
  252. for (const InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  253. in_anchors.emplace_back(peer_in_anchor);
  254. }
  255. if (InsertNodeBetween(out_data_anchor, in_anchors, in_memcpy, i, i) != SUCCESS) {
  256. GELOGE(FAILED, "Insert MemcpyAsync %s in while_body %s failed.", in_name.c_str(), graph->GetName().c_str());
  257. return FAILED;
  258. }
  259. }
  260. return SUCCESS;
  261. }
  262. /**
  263. * @ingroup ge
  264. * @brief Insert output memcpy node in while_body
  265. * @param [in] graph: while_body
  266. * @param [in] output_node: NetOutput
  267. * @param [in] bypass_index
  268. * @return: 0 for SUCCESS / others for FAILED
  269. */
  270. Status SubgraphPass::InsertOutputMemcpy(const ComputeGraphPtr &graph, const NodePtr &output_node,
  271. const std::set<uint32_t> &bypass_index) {
  272. if (output_node->GetAllInDataAnchorsSize() == bypass_index.size()) {
  273. GELOGD("No need to insert output memcpy node in while_body %s, output_size=%zu, bypass_num=%zu.",
  274. graph->GetName().c_str(), output_node->GetAllInDataAnchorsSize(), bypass_index.size());
  275. return SUCCESS;
  276. }
  277. std::string out_name = graph->GetName() + "_output_Memcpy";
  278. OpDescBuilder out_builder(out_name, IDENTITY);
  279. for (size_t i = 0; i < output_node->GetAllInDataAnchorsSize(); i++) {
  280. if (bypass_index.count(i) == 0) {
  281. out_builder.AddInput("x" + std::to_string(i), output_node->GetOpDesc()->GetInputDesc(i))
  282. .AddOutput("y" + std::to_string(i), output_node->GetOpDesc()->GetInputDesc(i));
  283. }
  284. }
  285. GELOGD("Insert memcpy before NetOutput of while_body %s.", graph->GetName().c_str());
  286. NodePtr out_memcpy = graph->AddNode(out_builder.Build());
  287. GE_CHECK_NOTNULL(out_memcpy);
  288. size_t cnt = 0;
  289. for (size_t i = 0; i < output_node->GetAllInDataAnchorsSize(); i++) {
  290. if (bypass_index.count(i) == 0) {
  291. InDataAnchorPtr in_data_anchor = output_node->GetInDataAnchor(i);
  292. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  293. if (InsertNodeBetween(peer_out_anchor, {in_data_anchor}, out_memcpy, cnt, cnt) != SUCCESS) {
  294. GELOGE(FAILED, "Insert MemcpyAsync %s in while_body %s failed.", out_name.c_str(), graph->GetName().c_str());
  295. return FAILED;
  296. }
  297. cnt++;
  298. }
  299. }
  300. return SUCCESS;
  301. }
  302. /**
  303. * @ingroup ge
  304. * @brief Check is data->netoutput without change in while body
  305. * @param [in] node: data node
  306. * @param [out] bypass_index
  307. * @return: false for data->netoutput without change in while body / for true for others
  308. */
  309. bool SubgraphPass::CheckInsertInputMemcpy(const NodePtr &node, std::set<uint32_t> &bypass_index) {
  310. uint32_t input_index = 0;
  311. if (!AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_PARENT_NODE_INDEX, input_index)) {
  312. return true;
  313. }
  314. // Data node has and only has one output
  315. OutDataAnchorPtr out_data_anchor = node->GetOutDataAnchor(0);
  316. if ((out_data_anchor == nullptr) || (out_data_anchor->GetPeerInDataAnchors().size() != 1)) {
  317. return true;
  318. }
  319. InDataAnchorPtr peer_in_anchor = out_data_anchor->GetPeerInDataAnchors().at(0);
  320. if (peer_in_anchor->GetOwnerNode()->GetType() != NETOUTPUT) {
  321. return true;
  322. }
  323. OpDescPtr op_desc = peer_in_anchor->GetOwnerNode()->GetOpDesc();
  324. uint32_t output_index = 0;
  325. if ((op_desc == nullptr) ||
  326. !AttrUtils::GetInt(op_desc->GetInputDesc(peer_in_anchor->GetIdx()), ATTR_NAME_PARENT_NODE_INDEX, output_index)) {
  327. return true;
  328. }
  329. if (input_index != output_index) {
  330. return true;
  331. }
  332. bypass_index.insert(peer_in_anchor->GetIdx());
  333. return false;
  334. }
  335. /**
  336. * @ingroup ge
  337. * @brief Check is AtomicOp->NetOutput
  338. * @param [in] node
  339. * @param [in] out_index
  340. * @return: true for AtomicOp->NetOutput / false for others
  341. */
  342. bool SubgraphPass::IsAtomicRequired(const NodePtr &node, int64_t out_index) {
  343. auto op_desc = node->GetOpDesc();
  344. if (op_desc != nullptr) {
  345. bool is_atomic = false;
  346. (void)ge::AttrUtils::GetBool(op_desc, ATOMIC_ATTR_IS_ATOMIC_NODE, is_atomic);
  347. if (is_atomic) {
  348. std::vector<int64_t> atomic_output_index;
  349. // If GetListInt fail, atomic_output_index is empty.
  350. (void)ge::AttrUtils::GetListInt(op_desc, ATOMIC_ATTR_OUTPUT_INDEX, atomic_output_index);
  351. for (int64_t ind : atomic_output_index) {
  352. if (ind == out_index) {
  353. return true;
  354. }
  355. }
  356. }
  357. }
  358. return false;
  359. }
  360. /**
  361. * @ingroup ge
  362. * @brief Check is OutputContinuesRequiredOp->NetOutput
  363. * @param [in] node
  364. * @return: true for OutputContinuesRequiredOp->NetOutput / false for others
  365. */
  366. bool SubgraphPass::IsOutputContinuesRequired(const NodePtr &node) {
  367. OpDescPtr op_desc = node->GetOpDesc();
  368. if (op_desc != nullptr) {
  369. bool continuous_output_flag = false;
  370. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_OUTPUT, continuous_output_flag);
  371. bool no_padding_continuous_output_flag = false;
  372. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_OUTPUT, no_padding_continuous_output_flag);
  373. return continuous_output_flag || no_padding_continuous_output_flag;
  374. }
  375. return false;
  376. }
  377. /**
  378. * @ingroup ge
  379. * @brief Check is InputContinuesRequiredOp->NetOutput
  380. * @param [in] node
  381. * @return: true for InputContinuesRequiredOp->NetOutput / false for others
  382. */
  383. bool SubgraphPass::IsInputContinuesRequired(const NodePtr &node) {
  384. OpDescPtr op_desc = node->GetOpDesc();
  385. if (op_desc != nullptr) {
  386. bool continuous_input_flag = false;
  387. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_CONTINUOUS_INPUT, continuous_input_flag);
  388. bool no_padding_continuous_input_flag = false;
  389. (void)ge::AttrUtils::GetBool(op_desc, ATTR_NAME_NOPADDING_CONTINUOUS_INPUT, no_padding_continuous_input_flag);
  390. return continuous_input_flag || no_padding_continuous_input_flag;
  391. }
  392. return false;
  393. }
  394. /**
  395. * @ingroup ge
  396. * @brief Insert memcpy node
  397. * @param [in] graph
  398. * @param [in] out_anchor
  399. * @param [in] in_anchors
  400. * @param [in] name
  401. * @return: 0 for success / others for fail
  402. */
  403. Status SubgraphPass::InsertMemcpyNode(const ComputeGraphPtr &graph, const OutDataAnchorPtr &out_anchor,
  404. const std::vector<InDataAnchorPtr> &in_anchors, const std::string &name) {
  405. GE_CHECK_NOTNULL(out_anchor);
  406. NodePtr in_node = out_anchor->GetOwnerNode();
  407. OpDescBuilder op_desc_builder(name, IDENTITY);
  408. OpDescPtr op_desc = op_desc_builder.AddInput("x", in_node->GetOpDesc()->GetOutputDesc(0))
  409. .AddOutput("y", in_node->GetOpDesc()->GetOutputDesc(0))
  410. .Build();
  411. (void)AttrUtils::SetBool(op_desc, ATTR_NO_NEED_CONSTANT_FOLDING, false);
  412. if (graph->GetParentNode() != nullptr && !graph->GetGraphUnknownFlag()) {
  413. (void)AttrUtils::SetBool(op_desc, "_zero_copy_identity_reserved", true);
  414. }
  415. if (GraphUtils::InsertNodeAfter(out_anchor, in_anchors, graph->AddNode(op_desc)) != GRAPH_SUCCESS) {
  416. GELOGE(FAILED, "Insert IDENTITY node %s after %s failed.", name.c_str(), in_node->GetName().c_str());
  417. return FAILED;
  418. }
  419. return SUCCESS;
  420. }
  421. ///
  422. /// @brief Insert node: src->insert_node:input_index, insert_node:output_index->dst
  423. /// @param [in] src
  424. /// @param [in] dsts
  425. /// @param [in] insert_node
  426. /// @param [in] input_index
  427. /// @param [in] output_index
  428. /// @return Status
  429. ///
  430. Status SubgraphPass::InsertNodeBetween(const OutDataAnchorPtr &src, const std::vector<InDataAnchorPtr> &dsts,
  431. const NodePtr &insert_node, uint32_t input_index, uint32_t output_index) {
  432. if (GraphUtils::AddEdge(src, insert_node->GetInDataAnchor(input_index)) != GRAPH_SUCCESS) {
  433. GELOGE(FAILED, "Add data_edge %s:%d->%s:%u failed.", src->GetOwnerNode()->GetName().c_str(), src->GetIdx(),
  434. insert_node->GetName().c_str(), input_index);
  435. return FAILED;
  436. }
  437. for (const auto &dst : dsts) {
  438. GELOGD("Insert node %s between %s->%s.", insert_node->GetName().c_str(), src->GetOwnerNode()->GetName().c_str(),
  439. dst->GetOwnerNode()->GetName().c_str());
  440. if ((GraphUtils::RemoveEdge(src, dst) != GRAPH_SUCCESS) ||
  441. (GraphUtils::AddEdge(insert_node->GetOutDataAnchor(output_index), dst) != GRAPH_SUCCESS)) {
  442. GELOGE(FAILED, "Replace data_edge %s:%d->%s:%d by %s:%u->%s:%d failed.", src->GetOwnerNode()->GetName().c_str(),
  443. src->GetIdx(), dst->GetOwnerNode()->GetName().c_str(), dst->GetIdx(), insert_node->GetName().c_str(),
  444. output_index, dst->GetOwnerNode()->GetName().c_str(), dst->GetIdx());
  445. return FAILED;
  446. }
  447. }
  448. return SUCCESS;
  449. }
  450. } // namespace ge

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