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
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /**
  2. * Copyright 2019-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 = 1000;
  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 (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(), node->GetType().c_str());
  123. ComputeGraphPtr graph = node->GetOwnerComputeGraph();
  124. if (graph == nullptr) {
  125. GELOGE(FAILED, "[%s] The owner graph must not be null.", node->GetName().c_str());
  126. return FAILED;
  127. }
  128. AddRePassNodesWithInOut(node);
  129. if (GraphUtils::IsolateNode(node, io_map) != GRAPH_SUCCESS) {
  130. GELOGE(FAILED, "[%s] IsolateNode failed.", node->GetName().c_str());
  131. return FAILED;
  132. }
  133. if (GraphUtils::RemoveNodeWithoutRelink(graph, node) != SUCCESS) {
  134. GELOGE(FAILED, "[%s] RemoveNodeWithoutRelink failed.", node->GetName().c_str());
  135. return FAILED;
  136. }
  137. AddNodeDeleted(node);
  138. return SUCCESS;
  139. }
  140. Status GEPass::Run(const NamesToPass &names_to_passes) {
  141. if (graph_ == nullptr) {
  142. GELOGE(INTERNAL_ERROR, "The graph is null");
  143. return INTERNAL_ERROR;
  144. }
  145. if (names_to_passes.empty()) {
  146. GELOGW("No passes input, the GEPass will do nothing");
  147. return INTERNAL_ERROR;
  148. }
  149. if (depth_ > kMaxRecursiveDepth) {
  150. GELOGE(PARAM_INVALID,
  151. "The pass for root graph %s will be terminated because too many nesting"
  152. " levels(%d) of subgraphs, last subgraph is %s",
  153. root_graph_->GetName().c_str(), depth_, graph_->GetName().c_str());
  154. return PARAM_INVALID;
  155. }
  156. return RunPassesOneGraph(names_to_passes);
  157. }
  158. Status GEPass::RunPassesOneGraph(const NamesToPass &names_to_passes) {
  159. GELOGD("Begin to run pass on graph, passes count %zu", names_to_passes.size());
  160. std::queue<NodePtr> nodes;
  161. std::unordered_set<Node *> nodes_seen;
  162. std::unordered_set<NodePtr> nodes_deleted;
  163. std::unordered_set<NodePtr> nodes_re_pass;
  164. std::unordered_set<NodePtr> nodes_last;
  165. GetAllNodesNoInputEdge(graph_, nodes, nodes_seen, nodes_last);
  166. GELOGD("Start points count %zu", nodes.size());
  167. int re_pass_times = 0;
  168. do {
  169. for (auto &node : nodes_re_pass) {
  170. nodes.push(node);
  171. nodes_seen.insert(node.get());
  172. }
  173. nodes_re_pass.clear();
  174. while (!nodes.empty()) {
  175. NodePtr node = nodes.front();
  176. nodes.pop();
  177. (void)nodes_re_pass.erase(node);
  178. GE_IF_BOOL_EXEC(node == nullptr, GELOGW("node is null"); continue);
  179. if (nodes_deleted.count(node) > 0) {
  180. GELOGD("The node %s was deleted before, skip it.", node->GetName().c_str());
  181. continue;
  182. }
  183. AddNextIterNodes(node->GetOutNodes(), nodes, nodes_seen, nodes_last);
  184. auto ret = RunPasses(node, names_to_passes, nodes_re_pass, nodes_deleted, nodes_seen);
  185. if (ret != SUCCESS) {
  186. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u", node->GetName().c_str(),
  187. node->GetType().c_str(), ret);
  188. return ret;
  189. }
  190. bool has_sub_graph = false;
  191. ret = RunPassesOnSubGraph(node, names_to_passes, has_sub_graph);
  192. if (ret != SUCCESS) {
  193. GELOGE(ret, "Failed to run passes on the sub graph of node %s", node->GetName().c_str());
  194. return ret;
  195. }
  196. if (has_sub_graph) {
  197. GELOGD("There are subgraphs on node %s, run passes for for the second time", node->GetName().c_str());
  198. SetFlagOption(kOptimizeAfterSubGraph, names_to_passes);
  199. ret = RunPasses(node, names_to_passes, nodes_re_pass, nodes_deleted, nodes_seen);
  200. if (ret != SUCCESS) {
  201. GELOGE(ret, "Failed to process passes on node %s type %s, error code: %u", node->GetName().c_str(),
  202. node->GetType().c_str(), ret);
  203. return ret;
  204. }
  205. // There is only one option scene, so set and clear options around the `RunPasses` func.
  206. // if there are more than one scene to set options, the `ClearOption` function
  207. // should be called each time at the begin of the iteration
  208. ClearOption(names_to_passes);
  209. }
  210. }
  211. for (auto &node : nodes_last) {
  212. bool all_in_nodes_seen = node->IsAllInNodesSeen(nodes_seen);
  213. if (all_in_nodes_seen && nodes_seen.insert(node.get()).second) {
  214. nodes.push(node);
  215. }
  216. }
  217. nodes_last.clear();
  218. } while ((!nodes_re_pass.empty() || !nodes.empty()) && ++re_pass_times < kMaxRePassTimes);
  219. if (re_pass_times == kMaxRePassTimes) {
  220. GELOGW("re_pass_times should not come to %d", kMaxRePassTimes);
  221. }
  222. GELOGD("All passes runs end");
  223. return SUCCESS;
  224. }
  225. Status GEPass::RunPassesOnSubGraph(const NodePtr &node, const NamesToPass &names_to_passes, bool &has_sub_graph) {
  226. auto sub_graph_names = node->GetOpDesc()->GetSubgraphInstanceNames();
  227. has_sub_graph = false;
  228. for (const auto &name : sub_graph_names) {
  229. auto graph = root_graph_->GetSubgraph(name);
  230. if (graph == nullptr) {
  231. GELOGW("Can not find the sub graph %s from node %s, the pass-process will skip it", name.c_str(),
  232. node->GetName().c_str());
  233. continue;
  234. }
  235. has_sub_graph = true;
  236. GELOGI("Begin to run passes on the sub graph %s of node %s", name.c_str(), node->GetName().c_str());
  237. GEPass pass(graph, root_graph_, depth_ + 1);
  238. auto ret = pass.Run(names_to_passes);
  239. if (ret != SUCCESS) {
  240. GELOGE(ret, "Failed to run passes for sub graph %s from node %s", name.c_str(), node->GetName().c_str());
  241. return ret;
  242. }
  243. }
  244. return SUCCESS;
  245. }
  246. } // namespace ge

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