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

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

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