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

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

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