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

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