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.

base_pass.cc 15 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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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/base_pass.h"
  17. #include <queue>
  18. #include <unordered_set>
  19. #include "common/debug/log.h"
  20. #include "framework/common/debug/ge_log.h"
  21. #include "graph/compute_graph.h"
  22. #include "graph/utils/graph_utils.h"
  23. namespace ge {
  24. namespace {
  25. constexpr int kMaxRePassTimes = 10000;
  26. constexpr size_t kMaxOneInNodes = 1000;
  27. // Each iteration, we take about 0.3k memory on the stack, we should change the recursion to loop later
  28. constexpr int kMaxRecursiveDepth = 20;
  29. struct DuringPassNodeSets {
  30. std::unordered_set<Node *> nodes_seen;
  31. std::unordered_set<NodePtr> nodes_deleted;
  32. std::unordered_set<NodePtr> nodes_re_pass;
  33. std::unordered_set<NodePtr> nodes_re_pass_immediately;
  34. std::unordered_set<NodePtr> nodes_last;
  35. std::unordered_set<NodePtr> nodes_suspend;
  36. std::unordered_set<NodePtr> nodes_resume;
  37. };
  38. void GetAllNodesNoInputEdge(const ComputeGraphPtr &graph, std::deque<NodePtr> &input_edge_nodes,
  39. std::unordered_set<Node *> &nodes_seen, std::unordered_set<NodePtr> &nodes_last) {
  40. nodes_last.clear();
  41. for (auto &node : graph->GetDirectNode()) {
  42. if (node == nullptr) {
  43. continue;
  44. }
  45. size_t in_nums = node->GetInNodes().size();
  46. if (in_nums == 0) {
  47. input_edge_nodes.push_back(node);
  48. nodes_seen.insert(node.get());
  49. } else if (in_nums > kMaxOneInNodes) {
  50. nodes_last.insert(node);
  51. }
  52. }
  53. }
  54. bool IsAllInNodesAlive(const Node::Vistor<NodePtr> &nodes, const std::unordered_set<NodePtr> &nodes_suspend) {
  55. return !std::any_of(nodes.begin(), nodes.end(), [&](const NodePtr &n) { return nodes_suspend.count(n) > 0; });
  56. }
  57. void AddNextIterNodes(const Node::Vistor<NodePtr> &nodes, std::deque<NodePtr> &nodes_to_pass,
  58. DuringPassNodeSets &during_pass_node_set) {
  59. auto &nodes_seen = during_pass_node_set.nodes_seen;
  60. const auto &nodes_last = during_pass_node_set.nodes_last;
  61. const auto &nodes_suspend = during_pass_node_set.nodes_suspend;
  62. for (auto &node : nodes) {
  63. if (node == nullptr) {
  64. continue;
  65. }
  66. if (nodes_last.count(node) != 0) {
  67. continue;
  68. }
  69. if (nodes_suspend.count(node) > 0) {
  70. GELOGD("The node %s has suspend by pass, skip it.", node->GetName().c_str());
  71. continue;
  72. }
  73. bool all_in_nodes_alive = IsAllInNodesAlive(node->GetInAllNodes(), nodes_suspend);
  74. bool all_in_nodes_seen = node->IsAllInNodesSeen(nodes_seen);
  75. if (all_in_nodes_seen && all_in_nodes_alive && nodes_seen.insert(node.get()).second) {
  76. nodes_to_pass.push_back(node);
  77. }
  78. }
  79. }
  80. void AddRepassNodes(DuringPassNodeSets &during_pass_node_set, std::deque<NodePtr> &nodes) {
  81. for (const auto &node : during_pass_node_set.nodes_re_pass_immediately) {
  82. GELOGD("The node %s will be re-pass immediately.", node->GetName().c_str());
  83. nodes.push_front(node);
  84. }
  85. during_pass_node_set.nodes_re_pass_immediately.clear();
  86. }
  87. void AddResumeNodes(DuringPassNodeSets &during_pass_node_set, std::deque<NodePtr> &nodes) {
  88. for (auto &node : during_pass_node_set.nodes_resume) {
  89. const auto &it = during_pass_node_set.nodes_suspend.find(node);
  90. if (it != during_pass_node_set.nodes_suspend.end()) {
  91. during_pass_node_set.nodes_suspend.erase(node);
  92. GELOGD("The node %s resumed by pass.", node->GetName().c_str());
  93. nodes.push_back(node);
  94. } else {
  95. GELOGW("The node %s not suspend, drop from resumed", node->GetName().c_str());
  96. }
  97. }
  98. during_pass_node_set.nodes_resume.clear();
  99. }
  100. void PushToSuspendNodes(DuringPassNodeSets &during_pass_node_set, const std::string &pass_name,
  101. const std::unordered_set<NodePtr> &nodes_suspend,
  102. const std::unordered_set<NodePtr> &nodes_resume) {
  103. for (const auto &node : nodes_suspend) {
  104. GELOGD("The iteration suspend of node %s has been set by pass %s", node->GetName().c_str(), pass_name.c_str());
  105. during_pass_node_set.nodes_suspend.emplace(node);
  106. }
  107. for (const auto &node : nodes_resume) {
  108. GELOGD("The iteration suspend of node %s has been resumed by pass %s", node->GetName().c_str(), pass_name.c_str());
  109. during_pass_node_set.nodes_resume.emplace(node);
  110. }
  111. }
  112. void PushToRePassIfSeen(NodePtr &node, const std::pair<std::string, BaseNodePass *> &name_to_pass,
  113. std::unordered_set<Node *> &nodes_seen, const std::unordered_set<NodePtr> &nodes_to_re_pass,
  114. std::unordered_set<NodePtr> &nodes_re_pass) {
  115. for (const auto &node_to_re_pass : nodes_to_re_pass) {
  116. if (node_to_re_pass == nullptr) {
  117. GELOGW("Found null re-pass node when executing %s on node %s type %s", name_to_pass.first.c_str(),
  118. node->GetName().c_str(), node->GetType().c_str());
  119. continue;
  120. }
  121. if (nodes_seen.count(node_to_re_pass.get()) > 0 || node_to_re_pass->IsAllInNodesSeen(nodes_seen)) {
  122. GELOGD("The node %s will be re-pass.", node_to_re_pass->GetName().c_str());
  123. nodes_re_pass.insert(node_to_re_pass);
  124. } else {
  125. GELOGD("The node %s are not all seen, don't set repass this time", node_to_re_pass->GetName().c_str());
  126. }
  127. }
  128. }
  129. Status RunPasses(NodePtr &node, const NamesToPass &names_to_passes, DuringPassNodeSets &during_pass_node_set) {
  130. if (node == nullptr) {
  131. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid.");
  132. GELOGE(FAILED, "[Check][Param] parameter node is nullptr.");
  133. return FAILED;
  134. }
  135. GELOGD("Begin to run pass for node %s", node->GetName().c_str());
  136. for (const auto &name_to_pass : names_to_passes) {
  137. if (name_to_pass.second == nullptr) {
  138. GELOGE(INTERNAL_ERROR, "[Check][Param] There is null pointer in passes(%s), skip it", name_to_pass.first.c_str());
  139. continue;
  140. }
  141. GELOGD("Begin to run pass %s for node %s", name_to_pass.first.c_str(), node->GetName().c_str());
  142. name_to_pass.second->init();
  143. auto result = name_to_pass.second->Run(node);
  144. if (result != SUCCESS) {
  145. REPORT_CALL_ERROR("E19999", "process pass %s on node:%s failed, ret:%u",
  146. name_to_pass.first.c_str(), node->GetName().c_str(), result);
  147. GELOGE(INTERNAL_ERROR, "[Process][Pass] %s on node %s failed, result "
  148. "%u, the passes will be terminated immediately.",
  149. name_to_pass.first.c_str(), node->GetName().c_str(), result);
  150. return result;
  151. }
  152. const auto &nodes_to_re_pass = name_to_pass.second->GetNodesNeedRePass();
  153. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass,
  154. during_pass_node_set.nodes_re_pass);
  155. const auto &nodes_to_re_pass_immediately = name_to_pass.second->GetNodesNeedRePassImmediately();
  156. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass_immediately,
  157. during_pass_node_set.nodes_re_pass_immediately);
  158. PushToSuspendNodes(during_pass_node_set, name_to_pass.first,
  159. name_to_pass.second->GetNodesSuspend(), name_to_pass.second->GetNodesResume());
  160. const auto &nodes_deleted_by_pass = name_to_pass.second->GetNodesDeleted();
  161. during_pass_node_set.nodes_deleted.insert(nodes_deleted_by_pass.begin(), nodes_deleted_by_pass.end());
  162. if (nodes_deleted_by_pass.count(node) > 0) {
  163. GELOGD("The node %s was deleted by pass %s, stop the remain passes", node->GetName().c_str(),
  164. name_to_pass.first.c_str());
  165. break;
  166. }
  167. }
  168. return SUCCESS;
  169. }
  170. void SetFlagOption(NodePassOption option, NamesToPass names_to_pass) {
  171. for (auto &name_to_pass : names_to_pass) {
  172. name_to_pass.second->SetOption(option, "");
  173. }
  174. }
  175. void ClearOption(NamesToPass names_to_pass) {
  176. for (auto &name_to_pass : names_to_pass) {
  177. name_to_pass.second->ClearOptions();
  178. }
  179. }
  180. } // namespace
  181. Status BaseNodePass::IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map) {
  182. if (node == nullptr) {
  183. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid.");
  184. GELOGE(FAILED, "[Check][Param] parameter node is nullptr.");
  185. return FAILED;
  186. }
  187. GELOGI("Prepare to isolate and delete node, name:%s, type:%s.", node->GetName().c_str(),
  188. node->GetType().c_str());
  189. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  190. if (graph == nullptr) {
  191. REPORT_INNER_ERROR("E19999", "The owner graph of node:%s must not be null.", node->GetName().c_str());
  192. GELOGE(FAILED, "[Get][OwnerComputeGraph] failed, The owner graph of node:%s must not be null.",
  193. node->GetName().c_str());
  194. return FAILED;
  195. }
  196. AddRePassNodesWithInOut(node);
  197. if (GraphUtils::IsolateNode(node, io_map) != GRAPH_SUCCESS) {
  198. REPORT_CALL_ERROR("E19999", "Isolate Node:%s failed", node->GetName().c_str());
  199. GELOGE(FAILED, "[Isolate][Node] %s failed.", node->GetName().c_str());
  200. return FAILED;
  201. }
  202. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  203. REPORT_CALL_ERROR("E19999", "call RemoveNodeWithoutRelink for node:%s failed.", node->GetName().c_str());
  204. GELOGE(FAILED, "[Call][RemoveNodeWithoutRelink] for node:%s failed.", node->GetName().c_str());
  205. return FAILED;
  206. }
  207. AddNodeDeleted(node);
  208. return SUCCESS;
  209. }
  210. Status GEPass::Run(const NamesToPass &names_to_passes) {
  211. if (graph_ == nullptr) {
  212. REPORT_INNER_ERROR("E19999", "graph_ is nullptr, check invalid.");
  213. GELOGE(INTERNAL_ERROR, "[Check][Param] The graph is nullptr");
  214. return INTERNAL_ERROR;
  215. }
  216. if (names_to_passes.empty()) {
  217. GELOGW("No passes input, the GEPass will do nothing");
  218. return INTERNAL_ERROR;
  219. }
  220. if (depth_ > kMaxRecursiveDepth) {
  221. GELOGE(PARAM_INVALID,
  222. "[Check][Param] The pass for root graph %s will be terminated because too many nesting"
  223. " levels(%d) of subgraphs, last subgraph is %s",
  224. root_graph_->GetName().c_str(), depth_, graph_->GetName().c_str());
  225. return PARAM_INVALID;
  226. }
  227. return RunPassesOneGraph(names_to_passes);
  228. }
  229. Status GEPass::RunPassesOneGraph(const NamesToPass &names_to_passes) {
  230. GELOGD("Begin to run pass on graph, passes count %zu", names_to_passes.size());
  231. std::deque<NodePtr> nodes;
  232. DuringPassNodeSets during_pass_node_set;
  233. GetAllNodesNoInputEdge(graph_, nodes, during_pass_node_set.nodes_seen, during_pass_node_set.nodes_last);
  234. GELOGD("Start points count %zu", nodes.size());
  235. int re_pass_times = 0;
  236. do {
  237. for (auto &node : during_pass_node_set.nodes_re_pass) {
  238. nodes.push_back(node);
  239. during_pass_node_set.nodes_seen.insert(node.get());
  240. }
  241. during_pass_node_set.nodes_re_pass.clear();
  242. while (!nodes.empty()) {
  243. NodePtr node = nodes.front();
  244. nodes.pop_front();
  245. (void)during_pass_node_set.nodes_re_pass.erase(node);
  246. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("node is null"); continue);
  247. if (during_pass_node_set.nodes_deleted.count(node) > 0) {
  248. GELOGD("The node %s was deleted before, skip it.", node->GetName().c_str());
  249. continue;
  250. }
  251. if (during_pass_node_set.nodes_suspend.count(node) > 0) {
  252. GELOGD("The node %s has been added to suspend-iteration nodes list, the iteration of it will be suspend.",
  253. node->GetName().c_str());
  254. continue;
  255. }
  256. AddNextIterNodes(node->GetOutNodes(), nodes, during_pass_node_set);
  257. auto ret = RunPasses(node, names_to_passes, during_pass_node_set);
  258. if (ret != SUCCESS) {
  259. GELOGE(ret, "[Process][Passes] on node %s type %s failed, error code:%u",
  260. node->GetName().c_str(), node->GetType().c_str(), ret);
  261. return ret;
  262. }
  263. bool has_sub_graph = false;
  264. ret = RunPassesOnSubGraph(node, names_to_passes, has_sub_graph);
  265. if (ret != SUCCESS) {
  266. GELOGE(ret, "[Run][Passes] on the sub graph of node %s failed", node->GetName().c_str());
  267. return ret;
  268. }
  269. if (has_sub_graph) {
  270. GELOGD("There are subgraphs on node %s, run passes for for the second time", node->GetName().c_str());
  271. SetFlagOption(kOptimizeAfterSubGraph, names_to_passes);
  272. ret = RunPasses(node, names_to_passes, during_pass_node_set);
  273. if (ret != SUCCESS) {
  274. GELOGE(ret, "[Process][Passes] on node %s type %s failed, error code: %u",
  275. node->GetName().c_str(), node->GetType().c_str(), ret);
  276. return ret;
  277. }
  278. // There is only one option scene, so set and clear options around the `RunPasses` func.
  279. // if there are more than one scene to set options, the `ClearOption` function
  280. // should be called each time at the begin of the iteration
  281. ClearOption(names_to_passes);
  282. }
  283. AddRepassNodes(during_pass_node_set, nodes);
  284. AddResumeNodes(during_pass_node_set, nodes);
  285. }
  286. for (auto &node : during_pass_node_set.nodes_last) {
  287. bool all_in_nodes_seen = node->IsAllInNodesSeen(during_pass_node_set.nodes_seen);
  288. if (all_in_nodes_seen && during_pass_node_set.nodes_seen.insert(node.get()).second) {
  289. nodes.push_back(node);
  290. }
  291. }
  292. during_pass_node_set.nodes_last.clear();
  293. } while ((!during_pass_node_set.nodes_re_pass.empty() || !nodes.empty()) && ++re_pass_times < kMaxRePassTimes);
  294. if (re_pass_times == kMaxRePassTimes) {
  295. GELOGW("re_pass_times should not come to %d", kMaxRePassTimes);
  296. }
  297. GELOGD("All passes runs end");
  298. return SUCCESS;
  299. }
  300. Status GEPass::RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph) {
  301. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  302. has_sub_graph = false;
  303. for (const auto &name : sub_graph_names) {
  304. auto graph = root_graph_->GetSubgraph(name);
  305. if (graph == nullptr) {
  306. GELOGW("Can not find the sub graph %s from node %s, the pass-process will skip it",
  307. name.c_str(), node->GetName().c_str());
  308. continue;
  309. }
  310. has_sub_graph = true;
  311. GELOGI("Begin to run passes on the sub graph %s of node %s", name.c_str(), node->GetName().c_str());
  312. GEPass pass(graph, root_graph_, depth_ + 1);
  313. auto ret = pass.Run(names_to_passes);
  314. if (ret != SUCCESS) {
  315. GELOGE(ret, "[Run][Passes] for sub graph:%s from node:%s failed", name.c_str(), node->GetName().c_str());
  316. return ret;
  317. }
  318. }
  319. return SUCCESS;
  320. }
  321. } // namespace ge

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