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

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