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

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