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.

merge_to_stream_merge_pass.cc 10 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/merge_to_stream_merge_pass.h"
  17. #include "common/ge/ge_util.h"
  18. #include "external/ge/ge_api_types.h"
  19. #include "common/omg_util.h"
  20. namespace ge {
  21. Status MergeToStreamMergePass::Run(ComputeGraphPtr graph) {
  22. GELOGD("MergeToStreamMergePass Enter");
  23. bypass_nodes_.clear();
  24. for (const auto &node : graph->GetDirectNode()) {
  25. std::string type;
  26. GE_CHK_STATUS_RET(GetOriginalType(node, type),
  27. "[Get][OriginalType] of node in graph:%s failed.", graph->GetName().c_str());
  28. if ((type != MERGE) && (type != REFMERGE)) {
  29. continue;
  30. }
  31. OpDescPtr merge_op_desc = node->GetOpDesc();
  32. GE_CHECK_NOTNULL(merge_op_desc);
  33. if (merge_op_desc->HasAttr(ATTR_INSERT_BY_MBATCH)) {
  34. GE_CHK_STATUS_RET(AddActiveNodes(graph, node), "Merge add active node failed.");
  35. auto status = SetStreamLabel(node, node->GetName());
  36. if (status != ge::SUCCESS) {
  37. REPORT_CALL_ERROR("E19999", "Set stream_label:%s to op:%s(%s) failed",
  38. node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  39. GELOGE(status, "[Set][StreamLabel] %s to op:%s(%s) failed",
  40. node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  41. return status;
  42. }
  43. } else {
  44. GE_CHK_STATUS_RET(ReplaceMergeNode(graph, node),
  45. "[Replace][MergeNode] %s in graph:%s failed.", node->GetName().c_str(),
  46. graph->GetName().c_str());
  47. }
  48. }
  49. for (const auto &node : bypass_nodes_) {
  50. GE_CHK_BOOL_EXEC(GraphUtils::RemoveNodeWithoutRelink(graph, node) == GRAPH_SUCCESS,
  51. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  52. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  53. return FAILED,
  54. "[Remove][Node] %s(%s) without relink in graph:%s failed",
  55. node->GetName().c_str(), node->GetType().c_str(), graph->GetName().c_str());
  56. }
  57. GELOGD("MergeToStreamMergePass Leave");
  58. return SUCCESS;
  59. }
  60. ///
  61. /// @brief Replace Merge Op
  62. /// @param [in] graph
  63. /// @param [in] merge_node
  64. /// @return Status
  65. ///
  66. Status MergeToStreamMergePass::ReplaceMergeNode(const ComputeGraphPtr &graph, const NodePtr &merge_node) {
  67. OpDescPtr merge_op_desc = merge_node->GetOpDesc();
  68. GE_CHECK_NOTNULL(merge_op_desc);
  69. merge_op_desc->SetType(STREAMMERGE);
  70. return AddActiveNodes(graph, merge_node);
  71. }
  72. ///
  73. /// @brief Add StreamActive Op before StreamMerge/Merge
  74. /// @param [in] graph
  75. /// @param [in] node
  76. /// @return Status
  77. ///
  78. Status MergeToStreamMergePass::AddActiveNodes(const ComputeGraphPtr &graph, const NodePtr &node) {
  79. GE_CHK_BOOL_EXEC(node != nullptr,
  80. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  81. return FAILED, "[Check][Param] Param of pre node is nullptr.");
  82. int64_t group_index = -1;
  83. (void)AttrUtils::GetInt(node->GetOpDesc(), ATTR_NAME_CONTROL_FLOW_GROUP, group_index);
  84. for (const InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  85. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  86. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  87. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  88. const std::string &type = in_node->GetType();
  89. // For WhileLoop, no need to add active nodes here, since which have been added in NextIterationPass.
  90. GE_IF_BOOL_EXEC((type == ENTER) || (type == REFENTER) || (type == NEXTITERATION) || (type == REFNEXTITERATION),
  91. continue);
  92. NodePtr active_node = CreateActiveNode(graph, in_node);
  93. GE_CHK_BOOL_EXEC(active_node != nullptr, return FAILED,
  94. "[Create][StreamActiveNode] failed, in_node:%s.", in_node->GetName().c_str());
  95. GE_CHK_STATUS(GraphUtils::AddEdge(active_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  96. "[Add][CtrlEdge] between %s and %s failed.",
  97. active_node->GetName().c_str(), node->GetName().c_str());
  98. if (SetActiveLabelList(active_node, { node->GetName() }) != SUCCESS) {
  99. GELOGE(FAILED, "[Set][ActiveLabelList] for node %s failed.", active_node->GetName().c_str());
  100. return FAILED;
  101. }
  102. SetControlFlowGroup(active_node, group_index);
  103. }
  104. return SUCCESS;
  105. }
  106. ///
  107. /// @brief Create Active Op
  108. /// @param [in] graph
  109. /// @param [in] node
  110. /// @return ge::NodePtr
  111. ///
  112. NodePtr MergeToStreamMergePass::CreateActiveNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  113. const std::string &node_name = node->GetName() + "_" + STREAMACTIVE;
  114. GELOGI("Create StreamActive op:%s.", node_name.c_str());
  115. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, STREAMACTIVE);
  116. if (op_desc == nullptr) {
  117. REPORT_CALL_ERROR("E19999", "New OpDesc failed, name:%s, type:%s.", node_name.c_str(), STREAMACTIVE);
  118. GELOGE(FAILED, "[New][OpDesc] failed, name:%s, type:%s.", node_name.c_str(), STREAMACTIVE);
  119. return nullptr;
  120. }
  121. NodePtr active_node = graph->AddNode(op_desc);
  122. GE_CHK_BOOL_EXEC(active_node != nullptr,
  123. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  124. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  125. return nullptr,
  126. "[Add][Node] %s(%s) to graph:%s failed",
  127. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  128. GE_IF_BOOL_EXEC(GraphUtils::AddEdge(node->GetOutControlAnchor(), active_node->GetInControlAnchor()) != SUCCESS,
  129. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  130. node->GetName().c_str(), node->GetType().c_str(),
  131. active_node->GetName().c_str(), active_node->GetType().c_str());
  132. GELOGE(INTERNAL_ERROR, "[Add][ControlEdge] between op:%s(%s) and op:%s(%s) failed",
  133. node->GetName().c_str(), node->GetType().c_str(),
  134. active_node->GetName().c_str(), active_node->GetType().c_str());
  135. return nullptr);
  136. GE_IF_BOOL_EXEC(SetSwitchBranchNodeLabel(active_node, node_name) != SUCCESS,
  137. GELOGE(INTERNAL_ERROR, "[Set][SwitchBranchNodeLabel] failed, node:%s, label:%s",
  138. active_node->GetName().c_str(), node_name.c_str());
  139. return nullptr);
  140. return active_node;
  141. }
  142. ///
  143. /// @brief move edges from old_node to new_node
  144. /// @param [in] old_node
  145. /// @param [in] new_node
  146. /// @return Status
  147. ///
  148. Status MergeToStreamMergePass::MoveEdges(const NodePtr &old_node, const NodePtr &new_node) {
  149. for (const InDataAnchorPtr &in_data_anchor : old_node->GetAllInDataAnchors()) {
  150. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  151. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  152. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor),
  153. "[Remove][Edge] between %s and %s failed.", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  154. old_node->GetName().c_str());
  155. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, new_node->GetInDataAnchor(in_data_anchor->GetIdx())),
  156. "[Add][Edge] between %s and %s failed.", peer_out_anchor->GetOwnerNode()->GetName().c_str(),
  157. new_node->GetName().c_str());
  158. }
  159. for (const OutDataAnchorPtr &out_data_anchor : old_node->GetAllOutDataAnchors()) {
  160. for (const InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  161. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor),
  162. "[Remove][Edge] between %s and %s failed.", old_node->GetName().c_str(),
  163. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  164. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutDataAnchor(out_data_anchor->GetIdx()), peer_in_anchor),
  165. "[Add][Edge] between %s and %s failed.", new_node->GetName().c_str(),
  166. peer_in_anchor->GetOwnerNode()->GetName().c_str());
  167. }
  168. }
  169. for (const NodePtr &in_ctrl_node : old_node->GetInControlNodes()) {
  170. GE_CHK_STATUS(GraphUtils::RemoveEdge(in_ctrl_node->GetOutControlAnchor(), old_node->GetInControlAnchor()),
  171. "[Remove][CtrlEdge] between %s and %s failed.", in_ctrl_node->GetName().c_str(),
  172. old_node->GetName().c_str());
  173. GE_CHK_STATUS(GraphUtils::AddEdge(in_ctrl_node->GetOutControlAnchor(), new_node->GetInControlAnchor()),
  174. "[Add][CtrlEdge] between %s and %s failed.", in_ctrl_node->GetName().c_str(),
  175. new_node->GetName().c_str());
  176. }
  177. for (const NodePtr &out_ctrl_node : old_node->GetOutControlNodes()) {
  178. GE_CHK_STATUS(GraphUtils::RemoveEdge(old_node->GetOutControlAnchor(), out_ctrl_node->GetInControlAnchor()),
  179. "[Remove][CtrlEdge] between %s and %s failed.", old_node->GetName().c_str(),
  180. out_ctrl_node->GetName().c_str());
  181. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutControlAnchor(), out_ctrl_node->GetInControlAnchor()),
  182. "[Add][CtrlEdge] between %s and %s failed.", new_node->GetName().c_str(),
  183. out_ctrl_node->GetName().c_str());
  184. }
  185. return SUCCESS;
  186. }
  187. } // namespace ge

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