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 11 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
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "ge/ge_api_types.h"
  19. #include "graph/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), "Get node type failed.");
  27. if ((type != MERGE) && (type != REFMERGE)) {
  28. continue;
  29. }
  30. OpDescPtr merge_op_desc = node->GetOpDesc();
  31. GE_CHECK_NOTNULL(merge_op_desc);
  32. if (merge_op_desc->HasAttr(ATTR_INSERT_BY_MBATCH)) {
  33. GE_CHK_STATUS_RET(AddActiveNodes(graph, node), "Merge add active node failed.");
  34. auto status = SetStreamLabel(node, node->GetName());
  35. if (status != ge::SUCCESS) {
  36. REPORT_CALL_ERROR("E19999", "Set stream_label:%s to op:%s(%s) failed",
  37. node->GetName().c_str(), node->GetName().c_str(), node->GetType().c_str());
  38. GELOGE(status, "Set stream label failed.");
  39. return status;
  40. }
  41. } else {
  42. GE_CHK_STATUS_RET(ReplaceMergeNode(graph, node), "Add StreamMerge node failed.");
  43. }
  44. }
  45. for (const auto &node : bypass_nodes_) {
  46. GE_CHK_BOOL_EXEC(GraphUtils::RemoveNodeWithoutRelink(graph, node) == GRAPH_SUCCESS,
  47. REPORT_CALL_ERROR("E19999", "Remove node:%s(%s) without relink in graph:%s failed",
  48. node->GetName().c_str(),
  49. node->GetType().c_str(), graph->GetName().c_str());
  50. return FAILED,
  51. "Remove merge node failed.");
  52. }
  53. GELOGD("MergeToStreamMergePass Leave");
  54. return SUCCESS;
  55. }
  56. ///
  57. /// @brief Replace Merge Op
  58. /// @param [in] graph
  59. /// @param [in] merge_node
  60. /// @return Status
  61. ///
  62. Status MergeToStreamMergePass::ReplaceMergeNode(const ComputeGraphPtr &graph, const NodePtr &merge_node) {
  63. OpDescPtr merge_op_desc = merge_node->GetOpDesc();
  64. GE_CHECK_NOTNULL(merge_op_desc);
  65. const std::string &node_name = merge_node->GetName();
  66. GELOGI("Create StreamMerge Op, name=%s.", node_name.c_str());
  67. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, STREAMMERGE);
  68. if (op_desc == nullptr) {
  69. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  70. GELOGE(FAILED, "Create op_desc failed, StreamMerge:%s.", node_name.c_str());
  71. return FAILED;
  72. }
  73. for (const InDataAnchorPtr &in_anchor : merge_node->GetAllInDataAnchors()) {
  74. GE_CHK_BOOL_EXEC(op_desc->AddInputDesc(merge_op_desc->GetInputDesc(in_anchor->GetIdx())) == GRAPH_SUCCESS,
  75. REPORT_CALL_ERROR("E19999", "Add input desc to op:%s(%s) failed",
  76. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  77. return FAILED, "Create StreamMerge op: add input desc failed.");
  78. }
  79. for (const OutDataAnchorPtr &out_anchor : merge_node->GetAllOutDataAnchors()) {
  80. GE_CHK_BOOL_EXEC(op_desc->AddOutputDesc(merge_op_desc->GetOutputDesc(out_anchor->GetIdx())) == GRAPH_SUCCESS,
  81. REPORT_CALL_ERROR("E19999", "Add ouput desc to op:%s(%s) failed",
  82. op_desc->GetName().c_str(), op_desc->GetType().c_str());
  83. return FAILED, "Create StreamMerge op: add output desc failed.");
  84. }
  85. NodePtr stream_merge = graph->AddNode(op_desc);
  86. GE_CHK_BOOL_EXEC(stream_merge != nullptr,
  87. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  88. op_desc->GetName().c_str(), op_desc->GetType().c_str(),
  89. graph->GetName().c_str());
  90. return FAILED, "Insert StreamMerge node failed.");
  91. GE_CHK_STATUS_RET(MoveEdges(merge_node, stream_merge), "Move edges failed.");
  92. bypass_nodes_.insert(merge_node);
  93. if (merge_op_desc->HasAttr(ATTR_NAME_NEXT_ITERATION)) {
  94. std::string next_iteration_name;
  95. GE_IF_BOOL_EXEC(!AttrUtils::GetStr(merge_op_desc, ATTR_NAME_NEXT_ITERATION, next_iteration_name),
  96. REPORT_CALL_ERROR("E19999", "Get Attr:%s from op:%s(%s) failed",
  97. ATTR_NAME_NEXT_ITERATION.c_str(),
  98. merge_op_desc->GetName().c_str(), merge_op_desc->GetType().c_str());
  99. GELOGE(INTERNAL_ERROR, "Get ATTR_NAME_NEXT_ITERATION failed");
  100. return INTERNAL_ERROR);
  101. GE_CHK_STATUS_RET(SetNextIteration(stream_merge, next_iteration_name), "Set next iteration failed");
  102. }
  103. return AddActiveNodes(graph, stream_merge);
  104. }
  105. ///
  106. /// @brief Add StreamActive Op before StreamMerge/Merge
  107. /// @param [in] graph
  108. /// @param [in] node
  109. /// @return Status
  110. ///
  111. Status MergeToStreamMergePass::AddActiveNodes(const ComputeGraphPtr &graph, const NodePtr &node) {
  112. GE_CHK_BOOL_EXEC(node != nullptr,
  113. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid");
  114. return FAILED, "Param of pre node is null.");
  115. for (const InDataAnchorPtr &in_data_anchor : node->GetAllInDataAnchors()) {
  116. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  117. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  118. NodePtr in_node = peer_out_anchor->GetOwnerNode();
  119. const std::string &type = in_node->GetType();
  120. // For WhileLoop, no need to add active nodes here, since which have been added in NextIterationPass.
  121. GE_IF_BOOL_EXEC((type == ENTER) || (type == REFENTER) || (type == NEXTITERATION) || (type == REFNEXTITERATION),
  122. continue);
  123. NodePtr active_node = CreateActiveNode(graph, in_node);
  124. GE_CHK_BOOL_EXEC(active_node != nullptr, return FAILED, "Create StreamActive node failed.");
  125. GE_CHK_STATUS(GraphUtils::AddEdge(active_node->GetOutControlAnchor(), node->GetInControlAnchor()),
  126. "StreamActive add ctrl edge failed.");
  127. if (SetActiveLabelList(active_node, { node->GetName() }) != SUCCESS) {
  128. GELOGE(FAILED, "SetActiveLabelList for node %s failed.", active_node->GetName().c_str());
  129. return FAILED;
  130. }
  131. }
  132. return SUCCESS;
  133. }
  134. ///
  135. /// @brief Create Active Op
  136. /// @param [in] graph
  137. /// @param [in] node
  138. /// @return ge::NodePtr
  139. ///
  140. NodePtr MergeToStreamMergePass::CreateActiveNode(const ComputeGraphPtr &graph, const NodePtr &node) {
  141. const std::string &node_name = node->GetName() + "_" + STREAMACTIVE;
  142. GELOGI("Create StreamActive op:%s.", node_name.c_str());
  143. OpDescPtr op_desc = MakeShared<OpDesc>(node_name, STREAMACTIVE);
  144. if (op_desc == nullptr) {
  145. REPORT_CALL_ERROR("E19999", "New GeTensor failed");
  146. GELOGE(FAILED, "Create op_desc failed, StreamActive:%s.", node_name.c_str());
  147. return nullptr;
  148. }
  149. NodePtr active_node = graph->AddNode(op_desc);
  150. GE_CHK_BOOL_EXEC(active_node != nullptr,
  151. REPORT_CALL_ERROR("E19999", "Add node:%s(%s) to graph:%s failed",
  152. op_desc->GetName().c_str(), op_desc->GetType().c_str(), graph->GetName().c_str());
  153. return nullptr, "Create StreamActive node failed.");
  154. GE_IF_BOOL_EXEC(GraphUtils::AddEdge(node->GetOutControlAnchor(), active_node->GetInControlAnchor()) != SUCCESS,
  155. REPORT_CALL_ERROR("E19999", "Add control edge between op:%s(%s) and op:%s(%s) failed",
  156. node->GetName().c_str(), node->GetType().c_str(),
  157. active_node->GetName().c_str(), active_node->GetType().c_str());
  158. GELOGE(INTERNAL_ERROR, "add edge failed");
  159. return nullptr);
  160. GE_IF_BOOL_EXEC(SetSwitchBranchNodeLabel(active_node, node_name) != SUCCESS,
  161. GELOGE(INTERNAL_ERROR, "set switch branch node label failed");
  162. return nullptr);
  163. return active_node;
  164. }
  165. ///
  166. /// @brief move edges from old_node to new_node
  167. /// @param [in] old_node
  168. /// @param [in] new_node
  169. /// @return Status
  170. ///
  171. Status MergeToStreamMergePass::MoveEdges(const NodePtr &old_node, const NodePtr &new_node) {
  172. for (const InDataAnchorPtr &in_data_anchor : old_node->GetAllInDataAnchors()) {
  173. OutDataAnchorPtr peer_out_anchor = in_data_anchor->GetPeerOutAnchor();
  174. GE_IF_BOOL_EXEC(peer_out_anchor == nullptr, continue);
  175. GE_CHK_STATUS(GraphUtils::RemoveEdge(peer_out_anchor, in_data_anchor),
  176. "Merge remove in data edge failed.");
  177. GE_CHK_STATUS(GraphUtils::AddEdge(peer_out_anchor, new_node->GetInDataAnchor(in_data_anchor->GetIdx())),
  178. "StreamMerge add in data edge failed.");
  179. }
  180. for (const OutDataAnchorPtr &out_data_anchor : old_node->GetAllOutDataAnchors()) {
  181. for (const InDataAnchorPtr &peer_in_anchor : out_data_anchor->GetPeerInDataAnchors()) {
  182. GE_CHK_STATUS(GraphUtils::RemoveEdge(out_data_anchor, peer_in_anchor),
  183. "Merge remove out data edge failed.");
  184. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutDataAnchor(out_data_anchor->GetIdx()), peer_in_anchor),
  185. "StreamMerge add out data edge failed.");
  186. }
  187. }
  188. for (const NodePtr &in_ctrl_node : old_node->GetInControlNodes()) {
  189. GE_CHK_STATUS(GraphUtils::RemoveEdge(in_ctrl_node->GetOutControlAnchor(), old_node->GetInControlAnchor()),
  190. "Merge remove in ctrl edge failed.");
  191. GE_CHK_STATUS(GraphUtils::AddEdge(in_ctrl_node->GetOutControlAnchor(), new_node->GetInControlAnchor()),
  192. "StreamMerge add in ctrl edge failed.");
  193. }
  194. for (const NodePtr &out_ctrl_node : old_node->GetOutControlNodes()) {
  195. GE_CHK_STATUS(GraphUtils::RemoveEdge(old_node->GetOutControlAnchor(), out_ctrl_node->GetInControlAnchor()),
  196. "Merge remove out ctrl edge failed.");
  197. GE_CHK_STATUS(GraphUtils::AddEdge(new_node->GetOutControlAnchor(), out_ctrl_node->GetInControlAnchor()),
  198. "StreamMerge add out ctrl edge failed.");
  199. }
  200. return SUCCESS;
  201. }
  202. } // namespace ge

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