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 11 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
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
5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. };
  36. void GetAllNodesNoInputEdge(const ComputeGraphPtr &graph, std::deque<NodePtr> &input_edge_nodes,
  37. std::unordered_set<Node *> &nodes_seen, std::unordered_set<NodePtr> &nodes_last) {
  38. nodes_last.clear();
  39. for (auto &node : graph->GetDirectNode()) {
  40. if (node == nullptr) {
  41. continue;
  42. }
  43. size_t in_nums = node->GetInNodes().size();
  44. if (in_nums == 0) {
  45. input_edge_nodes.push_back(node);
  46. nodes_seen.insert(node.get());
  47. } else if (in_nums > kMaxOneInNodes) {
  48. nodes_last.insert(node);
  49. }
  50. }
  51. }
  52. void AddNextIterNodes(const Node::Vistor<NodePtr> &nodes, std::deque<NodePtr> &nodes_to_pass,
  53. std::unordered_set<Node *> &nodes_seen, std::unordered_set<NodePtr> &nodes_last) {
  54. for (auto &node : nodes) {
  55. if (node == nullptr) {
  56. continue;
  57. }
  58. if (nodes_last.count(node) != 0) {
  59. continue;
  60. }
  61. bool all_in_nodes_seen = node->IsAllInNodesSeen(nodes_seen);
  62. if (all_in_nodes_seen && nodes_seen.insert(node.get()).second) {
  63. nodes_to_pass.push_back(node);
  64. }
  65. }
  66. }
  67. void PushToRePassIfSeen(NodePtr &node, const std::pair<std::string, BaseNodePass *> &name_to_pass,
  68. std::unordered_set<Node *> &nodes_seen, std::unordered_set<NodePtr> &nodes_to_re_pass,
  69. std::unordered_set<NodePtr> &nodes_re_pass) {
  70. for (const auto &node_to_re_pass : nodes_to_re_pass) {
  71. if (node_to_re_pass == nullptr) {
  72. GELOGW("Found null re-pass node when executing %s on node %s type %s", name_to_pass.first.c_str(),
  73. node->GetName().c_str(), node->GetType().c_str());
  74. continue;
  75. }
  76. if (nodes_seen.count(node_to_re_pass.get()) > 0 || node_to_re_pass->IsAllInNodesSeen(nodes_seen)) {
  77. GELOGD("The node %s will be re-pass.", node_to_re_pass->GetName().c_str());
  78. nodes_re_pass.insert(node_to_re_pass);
  79. } else {
  80. GELOGD("The node %s are not all seen, don't set repass this time", node_to_re_pass->GetName().c_str());
  81. }
  82. }
  83. }
  84. Status RunPasses(NodePtr &node, const NamesToPass &names_to_passes, DuringPassNodeSets &during_pass_node_set) {
  85. if (node == nullptr) {
  86. GELOGE(FAILED, "parameter is null.");
  87. return FAILED;
  88. }
  89. GELOGD("Begin to run pass for node %s", node->GetName().c_str());
  90. for (const auto &name_to_pass : names_to_passes) {
  91. if (name_to_pass.second == nullptr) {
  92. GELOGE(INTERNAL_ERROR, "There is null pointer in passes(%s), skip it", name_to_pass.first.c_str());
  93. continue;
  94. }
  95. GELOGD("Begin to run pass %s for node %s", name_to_pass.first.c_str(), node->GetName().c_str());
  96. name_to_pass.second->init();
  97. auto result = name_to_pass.second->Run(node);
  98. if (result != SUCCESS) {
  99. GELOGE(INTERNAL_ERROR,
  100. "Failed to process pass %s on node %s, result "
  101. "%u, the passes will be terminated immediately.",
  102. name_to_pass.first.c_str(), node->GetName().c_str(), result);
  103. return result;
  104. }
  105. auto nodes_to_re_pass = name_to_pass.second->GetNodesNeedRePass();
  106. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass,
  107. during_pass_node_set.nodes_re_pass);
  108. auto nodes_to_re_pass_immediately = name_to_pass.second->GetNodesNeedRePassImmediately();
  109. PushToRePassIfSeen(node, name_to_pass, during_pass_node_set.nodes_seen, nodes_to_re_pass_immediately,
  110. during_pass_node_set.nodes_re_pass_immediately);
  111. auto nodes_deleted_by_pass = name_to_pass.second->GetNodesDeleted();
  112. during_pass_node_set.nodes_deleted.insert(nodes_deleted_by_pass.begin(), nodes_deleted_by_pass.end());
  113. if (nodes_deleted_by_pass.count(node) > 0) {
  114. GELOGD("The node %s was deleted by pass %s, stop the remain passes", node->GetName().c_str(),
  115. name_to_pass.first.c_str());
  116. break;
  117. }
  118. }
  119. return SUCCESS;
  120. }
  121. void SetFlagOption(NodePassOption option, NamesToPass names_to_pass) {
  122. for (auto &name_to_pass : names_to_pass) {
  123. name_to_pass.second->SetOption(option, "");
  124. }
  125. }
  126. void ClearOption(NamesToPass names_to_pass) {
  127. for (auto &name_to_pass : names_to_pass) {
  128. name_to_pass.second->ClearOptions();
  129. }
  130. }
  131. } // namespace
  132. Status BaseNodePass::IsolateAndDeleteNode(NodePtr &node, const std::vector<int> &io_map) {
  133. if (node == nullptr) {
  134. GELOGE(FAILED, "parameter is null.");
  135. return FAILED;
  136. }
  137. GELOGI("Prepare to isolate and delete node, name:%s, type:%s.", node->GetName().c_str(),
  138. node->GetType().c_str());
  139. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  140. if (graph == nullptr) {
  141. GELOGE(FAILED, "[%s] The owner graph must not be null.", node->GetName().c_str());
  142. return FAILED;
  143. }
  144. AddRePassNodesWithInOut(node);
  145. if (GraphUtils::IsolateNode(node, io_map) != GRAPH_SUCCESS) {
  146. GELOGE(FAILED, "[%s] IsolateNode failed.", node->GetName().c_str());
  147. return FAILED;
  148. }
  149. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  150. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  151. return FAILED;
  152. }
  153. AddNodeDeleted(node);
  154. return SUCCESS;
  155. }
  156. Status GEPass::Run(const NamesToPass &names_to_passes) {
  157. if (graph_ == nullptr) {
  158. GELOGE(INTERNAL_ERROR, "The graph is null");
  159. return INTERNAL_ERROR;
  160. }
  161. if (names_to_passes.empty()) {
  162. GELOGW("No passes input, the GEPass will do nothing");
  163. return INTERNAL_ERROR;
  164. }
  165. if (depth_ > kMaxRecursiveDepth) {
  166. GELOGE(PARAM_INVALID,
  167. "The pass for root graph %s will be terminated because too many nesting"
  168. " levels(%d) of subgraphs, last subgraph is %s",
  169. root_graph_->GetName().c_str(), depth_, graph_->GetName().c_str());
  170. return PARAM_INVALID;
  171. }
  172. return RunPassesOneGraph(names_to_passes);
  173. }
  174. Status GEPass::RunPassesOneGraph(const NamesToPass &names_to_passes) {
  175. GELOGD("Begin to run pass on graph, passes count %zu", names_to_passes.size());
  176. std::deque<NodePtr> nodes;
  177. DuringPassNodeSets during_pass_node_set;
  178. GetAllNodesNoInputEdge(graph_, nodes, during_pass_node_set.nodes_seen, during_pass_node_set.nodes_last);
  179. GELOGD("Start points count %zu", nodes.size());
  180. int re_pass_times = 0;
  181. do {
  182. for (auto &node : during_pass_node_set.nodes_re_pass) {
  183. nodes.push_back(node);
  184. during_pass_node_set.nodes_seen.insert(node.get());
  185. }
  186. during_pass_node_set.nodes_re_pass.clear();
  187. while (!nodes.empty()) {
  188. NodePtr node = nodes.front();
  189. nodes.pop_front();
  190. (void)during_pass_node_set.nodes_re_pass.erase(node);
  191. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("node is null"); continue);
  192. if (during_pass_node_set.nodes_deleted.count(node) > 0) {
  193. GELOGD("The node %s was deleted before, skip it.", node->GetName().c_str());
  194. continue;
  195. }
  196. AddNextIterNodes(node->GetOutNodes(), nodes, during_pass_node_set.nodes_seen, during_pass_node_set.nodes_last);
  197. auto ret = RunPasses(node, names_to_passes, during_pass_node_set);
  198. if (ret != SUCCESS) {
  199. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u",
  200. node->GetName().c_str(), node->GetType().c_str(), ret);
  201. return ret;
  202. }
  203. bool has_sub_graph = false;
  204. ret = RunPassesOnSubGraph(node, names_to_passes, has_sub_graph);
  205. if (ret != SUCCESS) {
  206. GELOGE(ret, "Failed to run passes on the sub graph of node %s", node->GetName().c_str());
  207. return ret;
  208. }
  209. if (has_sub_graph) {
  210. GELOGD("There are subgraphs on node %s, run passes for for the second time", node->GetName().c_str());
  211. SetFlagOption(kOptimizeAfterSubGraph, names_to_passes);
  212. ret = RunPasses(node, names_to_passes, during_pass_node_set);
  213. if (ret != SUCCESS) {
  214. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u",
  215. node->GetName().c_str(), node->GetType().c_str(), ret);
  216. return ret;
  217. }
  218. // There is only one option scene, so set and clear options around the `RunPasses` func.
  219. // if there are more than one scene to set options, the `ClearOption` function
  220. // should be called each time at the begin of the iteration
  221. ClearOption(names_to_passes);
  222. }
  223. for (const auto &node : during_pass_node_set.nodes_re_pass_immediately) {
  224. GELOGD("The node %s will be re-pass immediately.", node->GetName().c_str());
  225. nodes.push_front(node);
  226. }
  227. during_pass_node_set.nodes_re_pass_immediately.clear();
  228. }
  229. for (auto &node : during_pass_node_set.nodes_last) {
  230. bool all_in_nodes_seen = node->IsAllInNodesSeen(during_pass_node_set.nodes_seen);
  231. if (all_in_nodes_seen && during_pass_node_set.nodes_seen.insert(node.get()).second) {
  232. nodes.push_back(node);
  233. }
  234. }
  235. during_pass_node_set.nodes_last.clear();
  236. } while ((!during_pass_node_set.nodes_re_pass.empty() || !nodes.empty()) && ++re_pass_times < kMaxRePassTimes);
  237. if (re_pass_times == kMaxRePassTimes) {
  238. GELOGW("re_pass_times should not come to %d", kMaxRePassTimes);
  239. }
  240. GELOGD("All passes runs end");
  241. return SUCCESS;
  242. }
  243. Status GEPass::RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph) {
  244. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  245. has_sub_graph = false;
  246. for (const auto &name : sub_graph_names) {
  247. auto graph = root_graph_->GetSubgraph(name);
  248. if (graph == nullptr) {
  249. GELOGW("Can not find the sub graph %s from node %s, the pass-process will skip it",
  250. name.c_str(), node->GetName().c_str());
  251. continue;
  252. }
  253. has_sub_graph = true;
  254. GELOGI("Begin to run passes on the sub graph %s of node %s", name.c_str(), node->GetName().c_str());
  255. GEPass pass(graph, root_graph_, depth_ + 1);
  256. auto ret = pass.Run(names_to_passes);
  257. if (ret != SUCCESS) {
  258. GELOGE(ret, "Failed to run passes for sub graph %s from node %s", name.c_str(), node->GetName().c_str());
  259. return ret;
  260. }
  261. }
  262. return SUCCESS;
  263. }
  264. } // namespace ge

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