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

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