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
4 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
4 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
4 years ago
4 years ago
5 years ago
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. bool CheckNode(const NodePtr &node, const DuringPassNodeSets &during_pass_node_set) {
  181. if (node == nullptr) {
  182. GELOGW("node is null");
  183. return false;
  184. }
  185. if (during_pass_node_set.nodes_deleted.count(node) > 0) {
  186. GELOGD("The node %s was deleted before, skip it.", node->GetName().c_str());
  187. return false;
  188. }
  189. if (during_pass_node_set.nodes_suspend.count(node) > 0) {
  190. GELOGD("The node %s has been added to suspend-iteration nodes list, the iteration of it will be suspend.",
  191. node->GetName().c_str());
  192. return false;
  193. }
  194. return true;
  195. }
  196. } // namespace
  197. Status BaseNodePass::IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map) {
  198. if (node == nullptr) {
  199. REPORT_INNER_ERROR("E19999", "Param node is nullptr, check invalid.");
  200. GELOGE(FAILED, "[Check][Param] parameter node is nullptr.");
  201. return FAILED;
  202. }
  203. GELOGI("Prepare to isolate and delete node, name:%s, type:%s.", node->GetName().c_str(),
  204. node->GetType().c_str());
  205. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  206. if (graph == nullptr) {
  207. REPORT_INNER_ERROR("E19999", "The owner graph of node:%s must not be null.", node->GetName().c_str());
  208. GELOGE(FAILED, "[Get][OwnerComputeGraph] failed, The owner graph of node:%s must not be null.",
  209. node->GetName().c_str());
  210. return FAILED;
  211. }
  212. AddRePassNodesWithInOut(node);
  213. if (GraphUtils::IsolateNode(node, io_map) != GRAPH_SUCCESS) {
  214. REPORT_CALL_ERROR("E19999", "Isolate Node:%s failed", node->GetName().c_str());
  215. GELOGE(FAILED, "[Isolate][Node] %s failed.", node->GetName().c_str());
  216. return FAILED;
  217. }
  218. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  219. REPORT_CALL_ERROR("E19999", "call RemoveNodeWithoutRelink for node:%s failed.", node->GetName().c_str());
  220. GELOGE(FAILED, "[Call][RemoveNodeWithoutRelink] for node:%s failed.", node->GetName().c_str());
  221. return FAILED;
  222. }
  223. AddNodeDeleted(node);
  224. return SUCCESS;
  225. }
  226. Status GEPass::Run(const NamesToPass &names_to_passes) {
  227. if (graph_ == nullptr) {
  228. REPORT_INNER_ERROR("E19999", "graph_ is nullptr, check invalid.");
  229. GELOGE(INTERNAL_ERROR, "[Check][Param] The graph is nullptr");
  230. return INTERNAL_ERROR;
  231. }
  232. if (names_to_passes.empty()) {
  233. GELOGW("No passes input, the GEPass will do nothing");
  234. return INTERNAL_ERROR;
  235. }
  236. if (depth_ > kMaxRecursiveDepth) {
  237. GELOGE(PARAM_INVALID,
  238. "[Check][Param] The pass for root graph %s will be terminated because too many nesting"
  239. " levels(%d) of subgraphs, last subgraph is %s",
  240. root_graph_->GetName().c_str(), depth_, graph_->GetName().c_str());
  241. return PARAM_INVALID;
  242. }
  243. return RunPassesOneGraph(names_to_passes);
  244. }
  245. Status GEPass::RunPassesOneGraph(const NamesToPass &names_to_passes) {
  246. GELOGD("Begin to run pass on graph, passes count %zu", names_to_passes.size());
  247. std::deque<NodePtr> nodes;
  248. DuringPassNodeSets during_pass_node_set;
  249. GetAllNodesNoInputEdge(graph_, nodes, during_pass_node_set.nodes_seen, during_pass_node_set.nodes_last);
  250. GELOGD("Start points count %zu", nodes.size());
  251. int re_pass_times = 0;
  252. do {
  253. for (auto &node : during_pass_node_set.nodes_re_pass) {
  254. nodes.push_back(node);
  255. during_pass_node_set.nodes_seen.insert(node.get());
  256. }
  257. during_pass_node_set.nodes_re_pass.clear();
  258. while (!nodes.empty()) {
  259. NodePtr node = nodes.front();
  260. nodes.pop_front();
  261. (void)during_pass_node_set.nodes_re_pass.erase(node);
  262. if (!CheckNode(node, during_pass_node_set)) {
  263. continue;
  264. }
  265. AddNextIterNodes(node->GetOutNodes(), nodes, during_pass_node_set);
  266. auto ret = RunPasses(node, names_to_passes, during_pass_node_set);
  267. if (ret != SUCCESS) {
  268. GELOGE(ret, "[Process][Passes] on node %s type %s failed, error code:%u",
  269. node->GetName().c_str(), node->GetType().c_str(), ret);
  270. return ret;
  271. }
  272. bool has_sub_graph = false;
  273. ret = RunPassesOnSubGraph(node, names_to_passes, has_sub_graph);
  274. if (ret != SUCCESS) {
  275. GELOGE(ret, "[Run][Passes] on the sub graph of node %s failed", node->GetName().c_str());
  276. return ret;
  277. }
  278. if (has_sub_graph) {
  279. GELOGD("There are subgraphs on node %s, run passes for for the second time", node->GetName().c_str());
  280. SetFlagOption(kOptimizeAfterSubGraph, names_to_passes);
  281. ret = RunPasses(node, names_to_passes, during_pass_node_set);
  282. if (ret != SUCCESS) {
  283. GELOGE(ret, "[Process][Passes] on node %s type %s failed, error code: %u",
  284. node->GetName().c_str(), node->GetType().c_str(), ret);
  285. return ret;
  286. }
  287. // There is only one option scene, so set and clear options around the `RunPasses` func.
  288. // if there are more than one scene to set options, the `ClearOption` function
  289. // should be called each time at the begin of the iteration
  290. ClearOption(names_to_passes);
  291. }
  292. AddRepassNodes(during_pass_node_set, nodes);
  293. AddResumeNodes(during_pass_node_set, nodes);
  294. }
  295. for (auto &node : during_pass_node_set.nodes_last) {
  296. bool all_in_nodes_seen = node->IsAllInNodesSeen(during_pass_node_set.nodes_seen);
  297. if (all_in_nodes_seen && during_pass_node_set.nodes_seen.insert(node.get()).second) {
  298. nodes.push_back(node);
  299. }
  300. }
  301. during_pass_node_set.nodes_last.clear();
  302. } while ((!during_pass_node_set.nodes_re_pass.empty() || !nodes.empty()) && ++re_pass_times < kMaxRePassTimes);
  303. if (re_pass_times == kMaxRePassTimes) {
  304. GELOGW("re_pass_times should not come to %d", kMaxRePassTimes);
  305. }
  306. GELOGD("All passes runs end");
  307. return SUCCESS;
  308. }
  309. Status GEPass::RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph) {
  310. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  311. has_sub_graph = false;
  312. for (const auto &name : sub_graph_names) {
  313. auto graph = root_graph_->GetSubgraph(name);
  314. if (graph == nullptr) {
  315. GELOGW("Can not find the sub graph %s from node %s, the pass-process will skip it",
  316. name.c_str(), node->GetName().c_str());
  317. continue;
  318. }
  319. has_sub_graph = true;
  320. GELOGI("Begin to run passes on the sub graph %s of node %s", name.c_str(), node->GetName().c_str());
  321. GEPass pass(graph, root_graph_, depth_ + 1);
  322. auto ret = pass.Run(names_to_passes);
  323. if (ret != SUCCESS) {
  324. GELOGE(ret, "[Run][Passes] for sub graph:%s from node:%s failed", name.c_str(), node->GetName().c_str());
  325. return ret;
  326. }
  327. }
  328. return SUCCESS;
  329. }
  330. } // namespace ge

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