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 14 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. GELOGE(FAILED, "parameter is null.");
  132. return FAILED;
  133. }
  134. GELOGD("Begin to run pass for node %s", node->GetName().c_str());
  135. for (const auto &name_to_pass : names_to_passes) {
  136. if (name_to_pass.second == nullptr) {
  137. GELOGE(INTERNAL_ERROR, "There is null pointer in passes(%s), skip it", name_to_pass.first.c_str());
  138. continue;
  139. }
  140. GELOGD("Begin to run pass %s for node %s", name_to_pass.first.c_str(), node->GetName().c_str());
  141. name_to_pass.second->init();
  142. auto result = name_to_pass.second->Run(node);
  143. if (result != SUCCESS) {
  144. GELOGE(INTERNAL_ERROR,
  145. "Failed to process pass %s on node %s, result "
  146. "%u, the passes will be terminated immediately.",
  147. name_to_pass.first.c_str(), node->GetName().c_str(), result);
  148. return result;
  149. }
  150. const auto &nodes_to_re_pass = name_to_pass.second->GetNodesNeedRePass();
  151. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass,
  152. during_pass_node_set.nodes_re_pass);
  153. const auto &nodes_to_re_pass_immediately = name_to_pass.second->GetNodesNeedRePassImmediately();
  154. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass_immediately,
  155. during_pass_node_set.nodes_re_pass_immediately);
  156. PushToSuspendNodes(during_pass_node_set, name_to_pass.first,
  157. name_to_pass.second->GetNodesSuspend(), name_to_pass.second->GetNodesResume());
  158. const auto &nodes_deleted_by_pass = name_to_pass.second->GetNodesDeleted();
  159. during_pass_node_set.nodes_deleted.insert(nodes_deleted_by_pass.begin(), nodes_deleted_by_pass.end());
  160. if (nodes_deleted_by_pass.count(node) > 0) {
  161. GELOGD("The node %s was deleted by pass %s, stop the remain passes", node->GetName().c_str(),
  162. name_to_pass.first.c_str());
  163. break;
  164. }
  165. }
  166. return SUCCESS;
  167. }
  168. void SetFlagOption(NodePassOption option, NamesToPass names_to_pass) {
  169. for (auto &name_to_pass : names_to_pass) {
  170. name_to_pass.second->SetOption(option, "");
  171. }
  172. }
  173. void ClearOption(NamesToPass names_to_pass) {
  174. for (auto &name_to_pass : names_to_pass) {
  175. name_to_pass.second->ClearOptions();
  176. }
  177. }
  178. } // namespace
  179. Status BaseNodePass::IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map) {
  180. if (node == nullptr) {
  181. GELOGE(FAILED, "parameter is null.");
  182. return FAILED;
  183. }
  184. GELOGI("Prepare to isolate and delete node, name:%s, type:%s.", node->GetName().c_str(),
  185. node->GetType().c_str());
  186. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  187. if (graph == nullptr) {
  188. GELOGE(FAILED, "[%s] The owner graph must not be null.", node->GetName().c_str());
  189. return FAILED;
  190. }
  191. AddRePassNodesWithInOut(node);
  192. if (GraphUtils::IsolateNode(node, io_map) != GRAPH_SUCCESS) {
  193. GELOGE(FAILED, "[%s] IsolateNode failed.", node->GetName().c_str());
  194. return FAILED;
  195. }
  196. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  197. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  198. return FAILED;
  199. }
  200. AddNodeDeleted(node);
  201. return SUCCESS;
  202. }
  203. Status GEPass::Run(const NamesToPass &names_to_passes) {
  204. if (graph_ == nullptr) {
  205. GELOGE(INTERNAL_ERROR, "The graph is null");
  206. return INTERNAL_ERROR;
  207. }
  208. if (names_to_passes.empty()) {
  209. GELOGW("No passes input, the GEPass will do nothing");
  210. return INTERNAL_ERROR;
  211. }
  212. if (depth_ > kMaxRecursiveDepth) {
  213. GELOGE(PARAM_INVALID,
  214. "The pass for root graph %s will be terminated because too many nesting"
  215. " levels(%d) of subgraphs, last subgraph is %s",
  216. root_graph_->GetName().c_str(), depth_, graph_->GetName().c_str());
  217. return PARAM_INVALID;
  218. }
  219. return RunPassesOneGraph(names_to_passes);
  220. }
  221. Status GEPass::RunPassesOneGraph(const NamesToPass &names_to_passes) {
  222. GELOGD("Begin to run pass on graph, passes count %zu", names_to_passes.size());
  223. std::deque<NodePtr> nodes;
  224. DuringPassNodeSets during_pass_node_set;
  225. GetAllNodesNoInputEdge(graph_, nodes, during_pass_node_set.nodes_seen, during_pass_node_set.nodes_last);
  226. GELOGD("Start points count %zu", nodes.size());
  227. int re_pass_times = 0;
  228. do {
  229. for (auto &node : during_pass_node_set.nodes_re_pass) {
  230. nodes.push_back(node);
  231. during_pass_node_set.nodes_seen.insert(node.get());
  232. }
  233. during_pass_node_set.nodes_re_pass.clear();
  234. while (!nodes.empty()) {
  235. NodePtr node = nodes.front();
  236. nodes.pop_front();
  237. (void)during_pass_node_set.nodes_re_pass.erase(node);
  238. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("node is null"); continue);
  239. if (during_pass_node_set.nodes_deleted.count(node) > 0) {
  240. GELOGD("The node %s was deleted before, skip it.", node->GetName().c_str());
  241. continue;
  242. }
  243. if (during_pass_node_set.nodes_suspend.count(node) > 0) {
  244. GELOGD("The node %s has been added to suspend-iteration nodes list, the iteration of it will be suspend.",
  245. node->GetName().c_str());
  246. continue;
  247. }
  248. AddNextIterNodes(node->GetOutNodes(), nodes, during_pass_node_set);
  249. auto ret = RunPasses(node, names_to_passes, during_pass_node_set);
  250. if (ret != SUCCESS) {
  251. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u",
  252. node->GetName().c_str(), node->GetType().c_str(), ret);
  253. return ret;
  254. }
  255. bool has_sub_graph = false;
  256. ret = RunPassesOnSubGraph(node, names_to_passes, has_sub_graph);
  257. if (ret != SUCCESS) {
  258. GELOGE(ret, "Failed to run passes on the sub graph of node %s", node->GetName().c_str());
  259. return ret;
  260. }
  261. if (has_sub_graph) {
  262. GELOGD("There are subgraphs on node %s, run passes for for the second time", node->GetName().c_str());
  263. SetFlagOption(kOptimizeAfterSubGraph, names_to_passes);
  264. ret = RunPasses(node, names_to_passes, during_pass_node_set);
  265. if (ret != SUCCESS) {
  266. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u",
  267. node->GetName().c_str(), node->GetType().c_str(), ret);
  268. return ret;
  269. }
  270. // There is only one option scene, so set and clear options around the `RunPasses` func.
  271. // if there are more than one scene to set options, the `ClearOption` function
  272. // should be called each time at the begin of the iteration
  273. ClearOption(names_to_passes);
  274. }
  275. AddRepassNodes(during_pass_node_set, nodes);
  276. AddResumeNodes(during_pass_node_set, nodes);
  277. }
  278. for (auto &node : during_pass_node_set.nodes_last) {
  279. bool all_in_nodes_seen = node->IsAllInNodesSeen(during_pass_node_set.nodes_seen);
  280. if (all_in_nodes_seen && during_pass_node_set.nodes_seen.insert(node.get()).second) {
  281. nodes.push_back(node);
  282. }
  283. }
  284. during_pass_node_set.nodes_last.clear();
  285. } while ((!during_pass_node_set.nodes_re_pass.empty() || !nodes.empty()) && ++re_pass_times < kMaxRePassTimes);
  286. if (re_pass_times == kMaxRePassTimes) {
  287. GELOGW("re_pass_times should not come to %d", kMaxRePassTimes);
  288. }
  289. GELOGD("All passes runs end");
  290. return SUCCESS;
  291. }
  292. Status GEPass::RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph) {
  293. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  294. has_sub_graph = false;
  295. for (const auto &name : sub_graph_names) {
  296. auto graph = root_graph_->GetSubgraph(name);
  297. if (graph == nullptr) {
  298. GELOGW("Can not find the sub graph %s from node %s, the pass-process will skip it",
  299. name.c_str(), node->GetName().c_str());
  300. continue;
  301. }
  302. has_sub_graph = true;
  303. GELOGI("Begin to run passes on the sub graph %s of node %s", name.c_str(), node->GetName().c_str());
  304. GEPass pass(graph, root_graph_, depth_ + 1);
  305. auto ret = pass.Run(names_to_passes);
  306. if (ret != SUCCESS) {
  307. GELOGE(ret, "Failed to run passes for sub graph %s from node %s", name.c_str(), node->GetName().c_str());
  308. return ret;
  309. }
  310. }
  311. return SUCCESS;
  312. }
  313. } // namespace ge

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