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.

attach_stream_label_pass.cc 8.8 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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/attach_stream_label_pass.h"
  17. #include "ge/ge_api_types.h"
  18. #include "graph/common/omg_util.h"
  19. using std::string;
  20. namespace ge {
  21. Status AttachStreamLabelPass::Run(ComputeGraphPtr graph) {
  22. GELOGD("AttachStreamLabelPass Enter.");
  23. FindNodes(graph);
  24. for (const auto &node : need_label_nodes_) {
  25. GE_CHK_STATUS_RET(UpdateCondBranch(node), "Update cond branch failed, start node:%s.", node->GetName().c_str());
  26. }
  27. GE_CHK_STATUS_RET(UpdateEnterNode(), "UpdateEnterNode failed.");
  28. GELOGD("AttachStreamLabelPass Leave.");
  29. return SUCCESS;
  30. }
  31. ///
  32. /// @brief Clear Status, used for subgraph pass
  33. /// @return
  34. ///
  35. Status AttachStreamLabelPass::ClearStatus() {
  36. stream_switch_nodes_.clear();
  37. need_label_nodes_.clear();
  38. enter_nodes_.clear();
  39. branch_head_nodes_.clear();
  40. return SUCCESS;
  41. }
  42. ///
  43. /// @brief Find StreamSwitch / StreamMerge / Enter node
  44. /// @param [in] graph
  45. /// @return void
  46. ///
  47. void AttachStreamLabelPass::FindNodes(const ComputeGraphPtr &graph) {
  48. for (const NodePtr &node : graph->GetDirectNode()) {
  49. const auto &op_desc = node->GetOpDesc();
  50. if (op_desc == nullptr) {
  51. continue;
  52. }
  53. const std::string &type = op_desc->GetType();
  54. if ((type == STREAMSWITCH) && op_desc->HasAttr(ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG)) {
  55. stream_switch_nodes_.emplace_back(node);
  56. } else if ((type == STREAMMERGE) && !op_desc->HasAttr(ATTR_NAME_NEXT_ITERATION)) {
  57. need_label_nodes_.emplace_back(node);
  58. } else if ((type == ENTER) || (type == REFENTER)) {
  59. enter_nodes_.emplace_back(node);
  60. }
  61. }
  62. for (const auto &node : stream_switch_nodes_) {
  63. for (const auto &out_ctrl_node : node->GetOutControlNodes()) {
  64. GELOGD("branch_head_node %s of stream_switch %s.", out_ctrl_node->GetName().c_str(), node->GetName().c_str());
  65. branch_head_nodes_[out_ctrl_node] = node;
  66. }
  67. need_label_nodes_.emplace_back(node);
  68. }
  69. }
  70. ///
  71. /// @brief update cond branch
  72. /// @param [in] node
  73. /// @return Status
  74. ///
  75. Status AttachStreamLabelPass::UpdateCondBranch(const NodePtr &node) {
  76. std::string stream_label;
  77. if (AttachFlag(node, stream_label) != SUCCESS) {
  78. GELOGE(FAILED, "Attach flag for node %s failed.", node->GetName().c_str());
  79. return FAILED;
  80. }
  81. std::unordered_set<NodePtr> branch_nodes;
  82. std::unordered_set<NodePtr> visited;
  83. std::stack<NodePtr> nodes;
  84. nodes.push(node);
  85. static const std::set<std::string> end_type_set = {STREAMSWITCH, STREAMMERGE, MERGE};
  86. while (!nodes.empty()) {
  87. NodePtr cur_node = nodes.top();
  88. nodes.pop();
  89. if (visited.count(cur_node) > 0) {
  90. continue;
  91. }
  92. const std::string &type = cur_node->GetType();
  93. for (const auto &out_node : cur_node->GetOutAllNodes()) {
  94. const std::string &out_type = out_node->GetType();
  95. bool stop_flag = (end_type_set.count(out_type) > 0) ||
  96. ((branch_head_nodes_.count(out_node) > 0) && (branch_head_nodes_[out_node] != node)) ||
  97. (((type == ENTER) || (type == REFENTER)) && (out_type != STREAMACTIVE));
  98. if (!stop_flag) {
  99. nodes.push(out_node);
  100. GELOGD("Insert branch node %s.", out_node->GetName().c_str());
  101. branch_nodes.insert(out_node);
  102. }
  103. }
  104. visited.insert(cur_node);
  105. }
  106. for (const NodePtr &tmp_node : branch_nodes) {
  107. GELOGD("Attach label %s to node: %s.", stream_label.c_str(), tmp_node->GetName().c_str());
  108. GE_CHK_STATUS_RET(SetStreamLabel(tmp_node, stream_label), "Set stream label failed.");
  109. }
  110. return SUCCESS;
  111. }
  112. ///
  113. /// @brief attach flag
  114. /// @param [in] node
  115. /// @param [out] stream_label
  116. /// @return Status
  117. ///
  118. Status AttachStreamLabelPass::AttachFlag(const NodePtr &node, std::string &stream_label) {
  119. const std::string &type = node->GetType();
  120. if (type == STREAMSWITCH) {
  121. if (node->GetInDataNodes().empty()) {
  122. GELOGE(INTERNAL_ERROR, "node %s has no input_data_node.", node->GetName().c_str());
  123. return INTERNAL_ERROR;
  124. }
  125. stream_label = node->GetInDataNodes().at(0)->GetName();
  126. bool value = false;
  127. OpDescPtr op_desc = node->GetOpDesc();
  128. GE_CHECK_NOTNULL(op_desc);
  129. GE_CHK_BOOL_EXEC(AttrUtils::GetBool(op_desc, ATTR_NAME_SWITCH_TRUE_BRANCH_FLAG, value), return FAILED,
  130. "StreamSwitch get attr TRUE_BRANCH_STREAM failed.");
  131. stream_label += (value ? "_t" : "_f");
  132. GE_CHK_STATUS_RET(SetActiveLabelList(node, {stream_label}), "set active_label_list failed.");
  133. } else if (type == STREAMMERGE) {
  134. stream_label = node->GetName();
  135. GE_CHK_STATUS_RET(SetStreamLabel(node, stream_label), "Set stream label failed.");
  136. }
  137. return SUCCESS;
  138. }
  139. ///
  140. /// @brief Update stream_label start with enter nodes
  141. /// @return Status
  142. ///
  143. Status AttachStreamLabelPass::UpdateEnterNode() {
  144. std::unordered_map<NodePtr, std::vector<NodePtr>> enter_active_map;
  145. for (const auto &enter_node : enter_nodes_) {
  146. for (const auto &out_ctrl_node : enter_node->GetOutControlNodes()) {
  147. if (out_ctrl_node->GetType() != STREAMACTIVE) {
  148. continue;
  149. }
  150. if (enter_active_map.find(out_ctrl_node) == enter_active_map.end()) {
  151. enter_active_map[out_ctrl_node] = {enter_node};
  152. } else {
  153. enter_active_map[out_ctrl_node].emplace_back(enter_node);
  154. }
  155. }
  156. }
  157. for (const auto &pair : enter_active_map) {
  158. if (SetEnterLabel(pair.second, pair.first) != SUCCESS) {
  159. GELOGE(FAILED, "Set stream_label for enter_nodes failed.");
  160. return FAILED;
  161. }
  162. NodePtr active_node = pair.first;
  163. GE_CHECK_NOTNULL(active_node);
  164. std::vector<std::string> active_label_list;
  165. bool get_attr = AttrUtils::GetListStr(active_node->GetOpDesc(), ATTR_NAME_ACTIVE_LABEL_LIST, active_label_list) &&
  166. (active_label_list.size() == 1) && !active_label_list[0].empty();
  167. if (!get_attr) {
  168. GELOGE(INTERNAL_ERROR, "Get attr ATTR_NAME_ACTIVE_LABEL_LIST failed, node: %s.", active_node->GetName().c_str());
  169. return INTERNAL_ERROR;
  170. }
  171. std::stack<NodePtr> enter_nodes;
  172. for (const auto &enter_node : pair.second) {
  173. enter_nodes.emplace(enter_node);
  174. }
  175. if (UpdateLoopBranch(enter_nodes, active_label_list[0]) != SUCCESS) {
  176. GELOGE(FAILED, "Update stream_label for loop_branch failed.");
  177. return FAILED;
  178. }
  179. }
  180. return SUCCESS;
  181. }
  182. ///
  183. /// @brief Set stream_label for enter_nodes
  184. /// @param [in] enter_nodes
  185. /// @param [in] active_node
  186. /// @return Status
  187. ///
  188. Status AttachStreamLabelPass::SetEnterLabel(const std::vector<NodePtr> &enter_nodes, const NodePtr &active_node) {
  189. std::string stream_label;
  190. GE_CHECK_NOTNULL(active_node);
  191. (void)AttrUtils::GetStr(active_node->GetOpDesc(), ATTR_NAME_STREAM_LABEL, stream_label);
  192. if (stream_label.empty()) {
  193. GELOGD("stream_label of enter_active %s is empty.", active_node->GetName().c_str());
  194. return SUCCESS;
  195. }
  196. for (const auto &enter_node : enter_nodes) {
  197. GE_CHK_STATUS_RET(SetStreamLabel(enter_node, stream_label), "Set stream label failed.");
  198. }
  199. return SUCCESS;
  200. }
  201. ///
  202. /// @brief Update stream_label for loop_branch
  203. /// @param [in] enter_nodes
  204. /// @param [in] stream_label
  205. /// @param [in] batch_label
  206. /// @return Status
  207. ///
  208. Status AttachStreamLabelPass::UpdateLoopBranch(const std::stack<NodePtr> &enter_nodes, const string &stream_label) {
  209. std::stack<NodePtr> nodes(enter_nodes);
  210. NodePtr cur_node = nullptr;
  211. while (!nodes.empty()) {
  212. cur_node = nodes.top();
  213. nodes.pop();
  214. for (const NodePtr &out_node : cur_node->GetOutAllNodes()) {
  215. OpDescPtr out_desc = out_node->GetOpDesc();
  216. GE_CHECK_NOTNULL(out_desc);
  217. std::string out_type = out_desc->GetType();
  218. bool need_skip =
  219. out_desc->HasAttr(ATTR_NAME_STREAM_LABEL) || (out_type == ENTER) || (out_type == REFENTER) ||
  220. (((cur_node->GetType() == ENTER) || (cur_node->GetType() == REFENTER)) && (out_type == STREAMACTIVE));
  221. if (need_skip) {
  222. continue;
  223. }
  224. GELOGD("Attach label %s to node: %s.", stream_label.c_str(), out_node->GetName().c_str());
  225. GE_CHK_STATUS_RET(SetStreamLabel(out_node, stream_label), "Set stream label failed.");
  226. nodes.push(out_node);
  227. }
  228. }
  229. return SUCCESS;
  230. }
  231. } // namespace ge

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