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

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