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

4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. GELOGE(FAILED, "Get node %s original type failede", trans_shape_node->GetName().c_str());
  42. return FAILED;
  43. }
  44. auto src_op_desc = trans_shape_node->GetOpDesc();
  45. GE_CHECK_NOTNULL(src_op_desc);
  46. std::string node_name = trans_shape_node->GetName() + "ToMemcpy";
  47. auto dst_op_desc = MakeShared<OpDesc>(node_name, MEMCPYASYNC);
  48. if (dst_op_desc == nullptr) {
  49. GELOGE(FAILED, "Make node %s opdesc failed", node_name.c_str());
  50. return FAILED;
  51. }
  52. GELOGI("Create memcpy Op, name=%s.", node_name.c_str());
  53. for (InDataAnchorPtr &in_anchor : trans_shape_node->GetAllInDataAnchors()) {
  54. auto ret = dst_op_desc->AddInputDesc(src_op_desc->GetInputDesc(in_anchor->GetIdx()));
  55. if (ret != GRAPH_SUCCESS) {
  56. GELOGE(FAILED, "Add input desc failed");
  57. return FAILED;
  58. }
  59. }
  60. for (OutDataAnchorPtr &out_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  61. auto ret = dst_op_desc->AddOutputDesc(src_op_desc->GetOutputDesc(out_anchor->GetIdx()));
  62. if (ret != GRAPH_SUCCESS) {
  63. GELOGE(FAILED, "Add output desc failed");
  64. return FAILED;
  65. }
  66. }
  67. NodePtr memcpy_node = graph->AddNode(dst_op_desc);
  68. GE_CHECK_NOTNULL(memcpy_node);
  69. for (InDataAnchorPtr &in_data_anchor : trans_shape_node->GetAllInDataAnchors()) {
  70. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  71. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  72. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor), "Remove Memcpy data input fail.");
  73. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, memcpy_node->GetInDataAnchor(in_data_anchor->GetIdx())),
  74. "Memcpy node add edge fail.");
  75. }
  76. for (OutDataAnchorPtr &out_data_anchor : trans_shape_node->GetAllOutDataAnchors()) {
  77. for (InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  78. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor), "Remove Memcpy data output fail.");
  79. GE_CHK_STATUS(GraphUtils::AddEdge(memcpy_node->GetOutDataAnchor(out_data_anchor->GetIdx()), peer_in_anchor),
  80. "Memcpy node add edge fail.");
  81. }
  82. }
  83. ReplaceControlEdges(trans_shape_node, memcpy_node);
  84. return SUCCESS;
  85. }
  86. void ReplaceTransShapePass::CopyControlEdges(NodePtr &old_node, NodePtr &new_node, bool input_check_flag) {
  87. GE_CHECK_NOTNULL_JUST_RETURN(old_node);
  88. GE_CHECK_NOTNULL_JUST_RETURN(new_node);
  89. GE_IF_BOOL_EXEC(old_node == new_node, return);
  90. for (NodePtr &node : old_node->GetInControlNodes()) {
  91. auto out_control_anchor = node->GetOutControlAnchor();
  92. GE_IF_BOOL_EXEC(!out_control_anchor->IsLinkedWith(new_node->GetInControlAnchor()), {
  93. GE_CHK_STATUS(GraphUtils::AddEdge(out_control_anchor, new_node->GetInControlAnchor()), "Add in ctl edge fail.");
  94. });
  95. }
  96. for (NodePtr &node : old_node->GetOutControlNodes()) {
  97. GE_IF_BOOL_EXEC(!new_node->GetOutControlAnchor()->IsLinkedWith(node->GetInControlAnchor()), {
  98. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  99. "Add out ctl edge fail.");
  100. });
  101. }
  102. }
  103. void ReplaceTransShapePass::RemoveControlEdges(NodePtr &node) {
  104. GE_CHECK_NOTNULL_JUST_RETURN(node);
  105. for (NodePtr &in_node : node->GetInControlNodes()) {
  106. GE_CHK_STATUS(GraphUtils::RemoveEdge(in_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  107. "Remove in ctl edge fail.");
  108. }
  109. for (auto &out_data_anchor : node->GetAllOutDataAnchors()) {
  110. for (auto &in_ctrl_anchor : out_data_anchor->GetPeerInControlAnchors()) {
  111. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, in_ctrl_anchor), "Remove in ctl edge fail.");
  112. }
  113. }
  114. auto out_control_anchor = node->GetOutControlAnchor();
  115. GE_CHECK_NOTNULL_JUST_RETURN(out_control_anchor);
  116. for (auto &peer_anchor : out_control_anchor->GetPeerAnchors()) {
  117. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_control_anchor, peer_anchor), "Remove out ctl edge fail.");
  118. }
  119. }
  120. void ReplaceTransShapePass::ReplaceControlEdges(NodePtr &old_node, NodePtr &new_node) {
  121. GE_IF_BOOL_EXEC(old_node == new_node, return);
  122. CopyControlEdges(old_node, new_node);
  123. RemoveControlEdges(old_node);
  124. }
  125. }

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