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

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

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