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

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