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.

replace_transshape_pass.cc 6.2 kB

4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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/replace_transshape_pass.h"
  17. #include <string>
  18. #include "common/ge/ge_util.h"
  19. #include "common/ge_inner_error_codes.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/common/omg_util.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. Status ReplaceTransShapePass::Run(ge::ComputeGraphPtr graph) {
  25. GE_CHECK_NOTNULL(graph);
  26. for (auto &node : graph->GetDirectNode()) {
  27. if (node->GetType() == TRANSSHAPE) {
  28. auto ret = ReplaceTransShapeNode(graph, node);
  29. if (ret != SUCCESS) {
  30. GELOGE(FAILED, "Trans shape node %s failed", node->GetName().c_str());
  31. return FAILED;
  32. }
  33. }
  34. }
  35. return SUCCESS;
  36. }
  37. Status ReplaceTransShapePass::ReplaceTransShapeNode(ComputeGraphPtr &graph, NodePtr &trans_shape_node) {
  38. std::string op_type;
  39. auto ret = GetOriginalType(trans_shape_node, op_type);
  40. if (ret != SUCCESS) {
  41. REPORT_CALL_ERROR("E19999", "Get OriginalType of op:%s(%s) failed",
  42. trans_shape_node->GetName().c_str(), trans_shape_node->GetType().c_str());
  43. GELOGE(FAILED, "Get node %s original type failede", trans_shape_node->GetName().c_str());
  44. return FAILED;
  45. }
  46. auto src_op_desc = trans_shape_node->GetOpDesc();
  47. GE_CHECK_NOTNULL(src_op_desc);
  48. std::string node_name = trans_shape_node->GetName() + "ToMemcpy";
  49. auto dst_op_desc = MakeShared<OpDesc>(node_name, MEMCPYASYNC);
  50. if (dst_op_desc == nullptr) {
  51. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  52. GELOGE(FAILED, "Make node %s opdesc failed", node_name.c_str());
  53. return FAILED;
  54. }
  55. GELOGI("Create memcpy Op, name=%s.", node_name.c_str());
  56. for (InDataAnchorPtr &in_anchor : trans_shape_node->GetAllInDataAnchors()) {
  57. auto ret = dst_op_desc->AddInputDesc(src_op_desc->GetInputDesc(in_anchor->GetIdx()));
  58. if (ret != GRAPH_SUCCESS) {
  59. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  60. dst_op_desc->GetName().c_str(), dst_op_desc->GetType().c_str());
  61. GELOGE(FAILED, "Add input desc failed");
  62. return FAILED;
  63. }
  64. }
  65. for (OutDataAnchorPtr &out_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  66. auto ret = dst_op_desc->AddOutputDesc(src_op_desc->GetOutputDesc(out_anchor->GetIdx()));
  67. if (ret != GRAPH_SUCCESS) {
  68. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  69. src_op_desc->GetName().c_str(), src_op_desc->GetType().c_str());
  70. GELOGE(FAILED, "Add output desc failed");
  71. return FAILED;
  72. }
  73. }
  74. NodePtr memcpy_node = graph->AddNode(dst_op_desc);
  75. GE_CHECK_NOTNULL(memcpy_node);
  76. for (InDataAnchorPtr &in_data_anchor : trans_shape_node->GetAllInDataAnchors()) {
  77. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  78. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  79. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor), "Remove Memcpy data input fail.");
  80. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, memcpy_node->GetInDataAnchor(in_data_anchor->GetIdx())),
  81. "Memcpy node add edge fail.");
  82. }
  83. for (OutDataAnchorPtr &out_data_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  84. for (InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  85. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor), "Remove Memcpy data output fail.");
  86. GE_CHK_STATUS(GraphUtils::AddEdge(memcpy_node->GetOutDataAnchor(out_data_anchor->GetIdx()), peer_in_anchor),
  87. "Memcpy node add edge fail.");
  88. }
  89. }
  90. ReplaceControlEdges(trans_shape_node, memcpy_node);
  91. return SUCCESS;
  92. }
  93. void ReplaceTransShapePass::CopyControlEdges(NodePtr &old_node, NodePtr &new_node, bool input_check_flag) {
  94. GE_CHECK_NOTNULL_JUST_RETURN(old_node);
  95. GE_CHECK_NOTNULL_JUST_RETURN(new_node);
  96. GE_IF_BOOL_EXEC(old_node == new_node, return);
  97. for (NodePtr &node : old_node->GetInControlNodes()) {
  98. auto out_control_anchor = node->GetOutControlAnchor();
  99. GE_IF_BOOL_EXEC(!out_control_anchor->IsLinkedWith(new_node->GetInControlAnchor()), {
  100. GE_CHK_STATUS(GraphUtils::AddEdge(out_control_anchor, new_node->GetInControlAnchor()), "Add in ctl edge fail.");
  101. });
  102. }
  103. for (NodePtr &node : old_node->GetOutControlNodes()) {
  104. GE_IF_BOOL_EXEC(!new_node->GetOutControlAnchor()->IsLinkedWith(node->GetInControlAnchor()), {
  105. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  106. "Add out ctl edge fail.");
  107. });
  108. }
  109. }
  110. void ReplaceTransShapePass::RemoveControlEdges(NodePtr &node) {
  111. GE_CHECK_NOTNULL_JUST_RETURN(node);
  112. for (NodePtr &in_node : node->GetInControlNodes()) {
  113. GE_CHK_STATUS(GraphUtils::RemoveEdge(in_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  114. "Remove in ctl edge fail.");
  115. }
  116. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  117. for (auto &in_ctrl_anchor : out_data_anchor->GetPeerInControlAnchors()) {
  118. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, in_ctrl_anchor), "Remove in ctl edge fail.");
  119. }
  120. }
  121. auto out_control_anchor = node->GetOutControlAnchor();
  122. GE_CHECK_NOTNULL_JUST_RETURN(out_control_anchor);
  123. for (auto &peer_anchor : out_control_anchor->GetPeerAnchors()) {
  124. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_control_anchor, peer_anchor), "Remove out ctl edge fail.");
  125. }
  126. }
  127. void ReplaceTransShapePass::ReplaceControlEdges(NodePtr &old_node, NodePtr &new_node) {
  128. GE_IF_BOOL_EXEC(old_node == new_node, return);
  129. CopyControlEdges(old_node, new_node);
  130. RemoveControlEdges(old_node);
  131. }
  132. }

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