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.

next_iteration_pass.cc 11 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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/next_iteration_pass.h"
  17. #include "common/ge/ge_util.h"
  18. #include "graph/common/omg_util.h"
  19. using std::string;
  20. namespace ge {
  21. Status NextIterationPass::Run(ComputeGraphPtr graph) {
  22. GELOGD("NextIterationPass Enter");
  23. /// Enter-----------+
  24. /// +-> Merge -> Switch <- LoopCond <- Cond
  25. /// NextIteration---+
  26. for (auto &node : graph->GetDirectNode()) {
  27. const std::string type = node->GetType();
  28. if ((type != ENTER) && (type != REFENTER)) {
  29. continue;
  30. }
  31. if (GroupEnterNode(node) != SUCCESS) {
  32. GELOGE(INTERNAL_ERROR, "Group enter_node %s failed.", node->GetName().c_str());
  33. return INTERNAL_ERROR;
  34. }
  35. }
  36. if (FindWhileGroups() != SUCCESS) {
  37. GELOGE(INTERNAL_ERROR, "Find while groups failed.");
  38. return INTERNAL_ERROR;
  39. }
  40. if (!VerifyWhileGroup()) {
  41. GELOGE(INTERNAL_ERROR, "Verify while groups failed.");
  42. return INTERNAL_ERROR;
  43. }
  44. if (HandleWhileGroup(graph) != SUCCESS) {
  45. GELOGE(FAILED, "Handle while groups failed.");
  46. return FAILED;
  47. }
  48. GELOGD("NextIterationPass Leave");
  49. return SUCCESS;
  50. }
  51. ///
  52. /// @brief Group Enter node
  53. /// @param [in] enter_node
  54. /// @return Status
  55. ///
  56. Status NextIterationPass::GroupEnterNode(const NodePtr &enter_node) {
  57. OpDescPtr enter_desc = enter_node->GetOpDesc();
  58. GE_CHECK_NOTNULL(enter_desc);
  59. std::string frame_name;
  60. if (!ge::AttrUtils::GetStr(enter_desc, ENTER_ATTR_FRAME_NAME, frame_name) || frame_name.empty()) {
  61. GELOGE(FAILED, "Get attr ENTER_ATTR_FRAME_NAME failed, node: %s", enter_desc->GetName().c_str());
  62. return FAILED;
  63. }
  64. string batch_label;
  65. if (ge::AttrUtils::GetStr(enter_desc, ATTR_NAME_BATCH_LABEL, batch_label)) {
  66. frame_name += batch_label;
  67. }
  68. auto iter = loop_group_map_.find(frame_name);
  69. if (iter == loop_group_map_.end()) {
  70. LoopCondGroupPtr loop_group = MakeShared<LoopCondGroup>();
  71. if (loop_group == nullptr) {
  72. GELOGE(FAILED, "MakeShared for LoopCondGroup failed.");
  73. return FAILED;
  74. }
  75. loop_group->enter_nodes.emplace_back(enter_node);
  76. loop_group_map_[frame_name] = loop_group;
  77. } else {
  78. iter->second->enter_nodes.emplace_back(enter_node);
  79. }
  80. return SUCCESS;
  81. }
  82. ///
  83. /// @brief Find while groups
  84. /// @return Status
  85. ///
  86. Status NextIterationPass::FindWhileGroups() {
  87. for (const auto &loop_group_iter : loop_group_map_) {
  88. const std::string &frame_name = loop_group_iter.first;
  89. for (const auto &enter_node : loop_group_iter.second->enter_nodes) {
  90. for (const auto &out_node : enter_node->GetOutAllNodes()) {
  91. const string &type = out_node->GetType();
  92. if ((type != MERGE) && (type != REFMERGE)) {
  93. continue;
  94. }
  95. NodePtr next_node = nullptr;
  96. if (FindTargetNode(out_node, NEXTITERATION, true, next_node) != SUCCESS) {
  97. GELOGE(INTERNAL_ERROR, "Get NextIteration node failed, frame_name: %s", frame_name.c_str());
  98. return INTERNAL_ERROR;
  99. }
  100. loop_group_iter.second->merge_next_pairs.emplace_back(std::make_pair(out_node, next_node));
  101. NodePtr switch_node = nullptr;
  102. if (FindTargetNode(out_node, SWITCH, false, switch_node) != SUCCESS) {
  103. GELOGE(INTERNAL_ERROR, "Get Switch node failed, frame_name: %s.", frame_name.c_str());
  104. return INTERNAL_ERROR;
  105. }
  106. if (switch_node == nullptr) {
  107. continue;
  108. }
  109. NodePtr loop_cond = nullptr;
  110. if (FindTargetNode(switch_node, LOOPCOND, true, loop_cond) != SUCCESS) {
  111. GELOGE(INTERNAL_ERROR, "Get LoopCond node failed, frame_name: %s.", frame_name.c_str());
  112. return INTERNAL_ERROR;
  113. }
  114. if (loop_group_iter.second->loop_cond == nullptr) {
  115. loop_group_iter.second->loop_cond = loop_cond;
  116. } else if (loop_group_iter.second->loop_cond != loop_cond) {
  117. GELOGE(FAILED, "Multi LoopCond nodes exist, frame_name: %s.", frame_name.c_str());
  118. return FAILED;
  119. }
  120. }
  121. }
  122. }
  123. return SUCCESS;
  124. }
  125. ///
  126. /// @brief Verify if valid
  127. /// @return bool
  128. ///
  129. bool NextIterationPass::VerifyWhileGroup() {
  130. // map<frame_name, LoopCondGroup>
  131. for (const auto &loop_group_iter : loop_group_map_) {
  132. const std::string &frame_name = loop_group_iter.first;
  133. if (frame_name.empty()) {
  134. GELOGE(INTERNAL_ERROR, "Verify while group failed, frame_name is empty.");
  135. return false;
  136. }
  137. if (loop_group_iter.second->loop_cond == nullptr) {
  138. GELOGE(INTERNAL_ERROR, "Verify while group failed, LoopCond is null, frame_name: %s.", frame_name.c_str());
  139. return false;
  140. }
  141. for (const auto &pair_iter : loop_group_iter.second->merge_next_pairs) {
  142. if ((pair_iter.first == nullptr) || (pair_iter.second == nullptr)) {
  143. GELOGE(INTERNAL_ERROR, "Verify while group failed, merge_node/next_node is null, frame_name: %s.",
  144. frame_name.c_str());
  145. return false;
  146. }
  147. }
  148. }
  149. return true;
  150. }
  151. ///
  152. /// @brief Handle while group
  153. /// @param [in] graph
  154. /// @return Status
  155. ///
  156. Status NextIterationPass::HandleWhileGroup(ComputeGraphPtr &graph) {
  157. for (const auto &loop_cond_iter : loop_group_map_) {
  158. const std::string &cond_name = loop_cond_iter.second->loop_cond->GetName();
  159. GELOGI("Handle while group, LoopCond node: %s.", cond_name.c_str());
  160. // Create Active node, Enter->Active->Merge, NextIteration->Active->Merge
  161. NodePtr enter_active = CreateActiveNode(graph, cond_name + "_Enter_" + STREAMACTIVE);
  162. NodePtr next_active = CreateActiveNode(graph, cond_name + "_Next_" + STREAMACTIVE);
  163. if ((enter_active == nullptr) || (next_active == nullptr)) {
  164. GELOGE(INTERNAL_ERROR, "Create active node failed, cond_name: %s.", cond_name.c_str());
  165. return INTERNAL_ERROR;
  166. }
  167. for (const auto &enter_node : loop_cond_iter.second->enter_nodes) {
  168. // Enter --> Active
  169. if (GraphUtils::AddEdge(enter_node->GetOutControlAnchor(), enter_active->GetInControlAnchor()) != GRAPH_SUCCESS) {
  170. GELOGE(INTERNAL_ERROR, "Add control edge from %s to %s failed.", enter_node->GetName().c_str(),
  171. enter_active->GetName().c_str());
  172. return INTERNAL_ERROR;
  173. }
  174. }
  175. for (const auto &pair : loop_cond_iter.second->merge_next_pairs) {
  176. NodePtr merge_node = pair.first;
  177. NodePtr next_node = pair.second;
  178. // Active --> Merge
  179. if (GraphUtils::AddEdge(enter_active->GetOutControlAnchor(), merge_node->GetInControlAnchor()) != GRAPH_SUCCESS) {
  180. GELOGE(INTERNAL_ERROR, "Add control edge failed.");
  181. return INTERNAL_ERROR;
  182. }
  183. // NextIteration --> Active
  184. if (GraphUtils::AddEdge(next_node->GetOutControlAnchor(), next_active->GetInControlAnchor()) != GRAPH_SUCCESS) {
  185. GELOGE(INTERNAL_ERROR, "Add control edge failed.");
  186. return INTERNAL_ERROR;
  187. }
  188. // break link between NextIteration and Merge
  189. if (BreakNextIteration(next_node, merge_node) != SUCCESS) {
  190. GELOGE(INTERNAL_ERROR, "Break NextIteration failed");
  191. return INTERNAL_ERROR;
  192. }
  193. }
  194. if ((SetActiveLabelList(enter_active, {cond_name}) != SUCCESS) ||
  195. (SetActiveLabelList(next_active, {cond_name}) != SUCCESS)) {
  196. GELOGE(INTERNAL_ERROR, "Set attr ACTIVE_LABEL_LIST failed.");
  197. return INTERNAL_ERROR;
  198. }
  199. }
  200. return SUCCESS;
  201. }
  202. ///
  203. /// @brief Create Active Node
  204. /// @param [in] graph
  205. /// @param [in] name
  206. /// @return ge::NodePtr
  207. ///
  208. NodePtr NextIterationPass::CreateActiveNode(ComputeGraphPtr &graph, const std::string &name) {
  209. OpDescPtr op_desc = MakeShared<OpDesc>(name, STREAMACTIVE);
  210. if (op_desc == nullptr) {
  211. return nullptr;
  212. }
  213. GELOGI("Create StreamActive op:%s.", op_desc->GetName().c_str());
  214. NodePtr active_node = graph->AddNode(op_desc);
  215. if (active_node == nullptr) {
  216. GELOGE(INTERNAL_ERROR, "Create node[%s] failed.", name.c_str());
  217. return nullptr;
  218. }
  219. if (SetSwitchBranchNodeLabel(active_node, name) != SUCCESS) {
  220. GELOGE(INTERNAL_ERROR, "Set attr SWITCH_BRANCH_NODE_LABEL for node: %s failed.", active_node->GetName().c_str());
  221. return nullptr;
  222. }
  223. return active_node;
  224. }
  225. ///
  226. /// @brief Break NextIteration Link & add name to merge attr
  227. /// @param [in] next_node
  228. /// @param [in] merge_node
  229. /// @return Status
  230. ///
  231. Status NextIterationPass::BreakNextIteration(const NodePtr &next_node, NodePtr &merge_node) {
  232. if ((merge_node == nullptr) || (next_node == nullptr)) {
  233. GELOGE(PARAM_INVALID, "merge node or next node is null.");
  234. return PARAM_INVALID;
  235. }
  236. for (const auto &in_anchor : merge_node->GetAllInDataAnchors()) {
  237. OutDataAnchorPtr out_anchor = in_anchor->GetPeerOutAnchor();
  238. if ((out_anchor == nullptr) || (out_anchor->GetOwnerNode() != next_node)) {
  239. continue;
  240. }
  241. if (GraphUtils::RemoveEdge(out_anchor, in_anchor) != SUCCESS) {
  242. GELOGE(INTERNAL_ERROR, "Remove data edge failed, %s->%s.", next_node->GetName().c_str(),
  243. merge_node->GetName().c_str());
  244. return INTERNAL_ERROR;
  245. }
  246. if (SetNextIteration(merge_node, next_node->GetName()) != SUCCESS) {
  247. GELOGE(INTERNAL_ERROR, "Set attr NEXT_ITERATION for node %s failed.", merge_node->GetName().c_str());
  248. return INTERNAL_ERROR;
  249. }
  250. }
  251. return SUCCESS;
  252. }
  253. ///
  254. /// @brief find target node
  255. /// @param [in] node
  256. /// @param [in] target_type
  257. /// @param [in] is_input
  258. /// @param [out] target_node
  259. /// @return Status
  260. ///
  261. Status NextIterationPass::FindTargetNode(const NodePtr &node, const std::string &target_type, bool is_input,
  262. NodePtr &target_node) {
  263. if (node == nullptr) {
  264. GELOGE(PARAM_INVALID, "node is null.");
  265. return PARAM_INVALID;
  266. }
  267. std::vector<NodePtr> nodes;
  268. if (is_input) {
  269. for (const auto &tmp_node : node->GetInDataNodes()) {
  270. nodes.emplace_back(tmp_node);
  271. }
  272. } else {
  273. for (const auto &tmp_node : node->GetOutDataNodes()) {
  274. nodes.emplace_back(tmp_node);
  275. }
  276. }
  277. for (const auto &tmp_node : nodes) {
  278. const std::string type = tmp_node->GetType();
  279. if ((target_type == LOOPCOND) && (type == target_type)) {
  280. target_node = tmp_node;
  281. break;
  282. } else if ((type == target_type) || (type == "Ref" + target_type)) {
  283. target_node = tmp_node;
  284. break;
  285. }
  286. }
  287. if ((target_type != SWITCH) && (target_node == nullptr)) {
  288. GELOGE(INTERNAL_ERROR, "Find node %s failed.", target_type.c_str());
  289. return INTERNAL_ERROR;
  290. }
  291. return SUCCESS;
  292. }
  293. ///
  294. /// @brief Clear Status, used for subgraph pass
  295. /// @return SUCCESS
  296. ///
  297. Status NextIterationPass::ClearStatus() {
  298. loop_group_map_.clear();
  299. return SUCCESS;
  300. }
  301. } // namespace ge

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