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

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