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

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

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