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

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

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