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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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][OriginalType] of op:%s(%s) failed",
  44. trans_shape_node->GetName().c_str(), trans_shape_node->GetType().c_str());
  45. return FAILED;
  46. }
  47. auto src_op_desc = trans_shape_node->GetOpDesc();
  48. GE_CHECK_NOTNULL(src_op_desc);
  49. std::string node_name = trans_shape_node->GetName() + "ToMemcpy";
  50. auto dst_op_desc = MakeShared<OpDesc>(node_name, MEMCPYASYNC);
  51. if (dst_op_desc == nullptr) {
  52. REPORT_CALL_ERROR("E19999", "New OpDesc failed");
  53. GELOGE(FAILED, "[New][OpDesc] failed");
  54. return FAILED;
  55. }
  56. GELOGI("Create memcpy Op, name=%s.", node_name.c_str());
  57. for (InDataAnchorPtr &in_anchor : trans_shape_node->GetAllInDataAnchors()) {
  58. auto ret = dst_op_desc->AddInputDesc(src_op_desc->GetInputDesc(in_anchor->GetIdx()));
  59. if (ret != GRAPH_SUCCESS) {
  60. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  61. dst_op_desc->GetName().c_str(), dst_op_desc->GetType().c_str());
  62. GELOGE(FAILED, "[Add][InputDesc] to op:%s(%s) failed",
  63. dst_op_desc->GetName().c_str(), dst_op_desc->GetType().c_str());
  64. return FAILED;
  65. }
  66. }
  67. for (OutDataAnchorPtr &out_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  68. auto ret = dst_op_desc->AddOutputDesc(src_op_desc->GetOutputDesc(out_anchor->GetIdx()));
  69. if (ret != GRAPH_SUCCESS) {
  70. REPORT_CALL_ERROR("E19999", "Add output desc to op:%s(%s) failed",
  71. src_op_desc->GetName().c_str(), src_op_desc->GetType().c_str());
  72. GELOGE(FAILED, "[Add][OutputDesc] to op:%s(%s) failed",
  73. src_op_desc->GetName().c_str(), src_op_desc->GetType().c_str());
  74. return FAILED;
  75. }
  76. }
  77. NodePtr memcpy_node = graph->AddNode(dst_op_desc);
  78. GE_CHECK_NOTNULL(memcpy_node);
  79. for (InDataAnchorPtr &in_data_anchor : trans_shape_node->GetAllInDataAnchors()) {
  80. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  81. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  82. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor),
  83. "[Remove][Edge] between %s and %s failed.",
  84. peer_out_anchor->GetOwnerNode()->GetName().c_str(), trans_shape_node->GetName().c_str());
  85. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, memcpy_node->GetInDataAnchor(in_data_anchor->GetIdx())),
  86. "[Add][Edge] between %s and %s failed.",
  87. peer_out_anchor->GetOwnerNode()->GetName().c_str(), memcpy_node->GetName().c_str());
  88. }
  89. for (OutDataAnchorPtr &out_data_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  90. for (InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  91. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor),
  92. "[Remove][Edge] between %s and %s failed.",
  93. trans_shape_node->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  94. GE_CHK_STATUS(GraphUtils::AddEdge(memcpy_node->GetOutDataAnchor(out_data_anchor->GetIdx()), peer_in_anchor),
  95. "[Add][Edge] between %s and %s failed.",
  96. memcpy_node->GetName().c_str(), peer_in_anchor->GetOwnerNode()->GetName().c_str());
  97. }
  98. }
  99. ReplaceControlEdges(trans_shape_node, memcpy_node);
  100. return SUCCESS;
  101. }
  102. void ReplaceTransShapePass::CopyControlEdges(NodePtr &old_node, NodePtr &new_node, bool input_check_flag) {
  103. GE_CHECK_NOTNULL_JUST_RETURN(old_node);
  104. GE_CHECK_NOTNULL_JUST_RETURN(new_node);
  105. GE_IF_BOOL_EXEC(old_node == new_node, return);
  106. for (NodePtr &node : old_node->GetInControlNodes()) {
  107. auto out_control_anchor = node->GetOutControlAnchor();
  108. GE_IF_BOOL_EXEC(!out_control_anchor->IsLinkedWith(new_node->GetInControlAnchor()), {
  109. GE_CHK_STATUS(GraphUtils::AddEdge(out_control_anchor, new_node->GetInControlAnchor()),
  110. "[Add][ControlEdge] between %s and %s failed.",
  111. node->GetName().c_str(), new_node->GetName().c_str());
  112. });
  113. }
  114. for (NodePtr &node : old_node->GetOutControlNodes()) {
  115. GE_IF_BOOL_EXEC(!new_node->GetOutControlAnchor()->IsLinkedWith(node->GetInControlAnchor()), {
  116. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  117. "[Add][ControlEdge] between %s and %s failed.",
  118. new_node->GetName().c_str(), node->GetName().c_str());
  119. });
  120. }
  121. }
  122. void ReplaceTransShapePass::RemoveControlEdges(NodePtr &node) {
  123. GE_CHECK_NOTNULL_JUST_RETURN(node);
  124. for (NodePtr &in_node : node->GetInControlNodes()) {
  125. GE_CHK_STATUS(GraphUtils::RemoveEdge(in_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  126. "[Remove][ControlEdge] between %s and %s failed.",
  127. in_node->GetName().c_str(), node->GetName().c_str());
  128. }
  129. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  130. for (auto &in_ctrl_anchor : out_data_anchor->GetPeerInControlAnchors()) {
  131. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, in_ctrl_anchor),
  132. "[Remove][Edge] between %s and %s failed.",
  133. node->GetName().c_str(), in_ctrl_anchor->GetOwnerNode()->GetName().c_str());
  134. }
  135. }
  136. auto out_control_anchor = node->GetOutControlAnchor();
  137. GE_CHECK_NOTNULL_JUST_RETURN(out_control_anchor);
  138. for (auto &peer_anchor : out_control_anchor->GetPeerAnchors()) {
  139. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_control_anchor, peer_anchor),
  140. "[Remove][OutCtlEdge] between %s and %s failed.",
  141. node->GetName().c_str(), peer_anchor->GetOwnerNode()->GetName().c_str());
  142. }
  143. }
  144. void ReplaceTransShapePass::ReplaceControlEdges(NodePtr &old_node, NodePtr &new_node) {
  145. GE_IF_BOOL_EXEC(old_node == new_node, return);
  146. CopyControlEdges(old_node, new_node);
  147. RemoveControlEdges(old_node);
  148. }
  149. }

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