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

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

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