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

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

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