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.

graph_utils.cc 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "utils/graph_utils.h"
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <utility>
  22. #include <stack>
  23. #include <vector>
  24. #include <string>
  25. #include <fstream>
  26. #include <queue>
  27. #include <deque>
  28. #include <set>
  29. #include "common/utils.h"
  30. #include "debug/label.h"
  31. #include "ir/func_graph.h"
  32. #include "utils/log_adapter.h"
  33. namespace mindspore {
  34. std::vector<AnfNodePtr> TopoSort(const AnfNodePtr &root, const SuccFunc &succ, const IncludeFunc &include) {
  35. size_t seen = NewSeenGeneration();
  36. std::deque<AnfNodePtr> todo(1024);
  37. std::unordered_map<AnfNodePtr, size_t> rank;
  38. std::vector<AnfNodePtr> res;
  39. todo.clear();
  40. todo.push_back(root);
  41. while (!todo.empty()) {
  42. AnfNodePtr node = todo.back();
  43. if (node == nullptr || node->seen_ == seen) {
  44. todo.pop_back();
  45. continue;
  46. }
  47. if (rank.find(node) != rank.end() && rank[node] != todo.size()) {
  48. MS_LOG(EXCEPTION) << "Graph exists cycle, node " << node->DebugString();
  49. }
  50. rank[node] = todo.size();
  51. bool cont = false;
  52. auto incl = include(node);
  53. if (incl == FOLLOW) {
  54. auto succs = succ(node);
  55. for (const auto i : succs) {
  56. if ((i != nullptr && i->seen_ != seen)
  57. // Handle the case for 2 subgraphs calls each other.
  58. // If the ValueNodeGraph's return is already in the todo list, do not follow it.
  59. && !((std::find(todo.begin(), todo.end(), i) != todo.end()) && (i->func_graph() != nullptr) &&
  60. (i->func_graph()->get_return() == i))) {
  61. todo.push_back(i);
  62. cont = true;
  63. }
  64. }
  65. } else if (incl == NOFOLLOW) {
  66. // do nothing
  67. } else if (incl == EXCLUDE) {
  68. node->seen_ = seen;
  69. todo.pop_back();
  70. continue;
  71. } else {
  72. MS_LOG(EXCEPTION) << "include(node) must return one of: \"follow\", \"nofollow\", \"exclude\"";
  73. }
  74. if (cont) {
  75. continue;
  76. }
  77. node->seen_ = seen;
  78. res.push_back(node);
  79. todo.pop_back();
  80. }
  81. return res;
  82. }
  83. // search the cnodes inside this graph only
  84. std::vector<CNodePtr> BroadFirstSearchGraphCNodes(CNodePtr ret) {
  85. std::queue<CNodePtr> todo;
  86. todo.push(ret);
  87. std::vector<CNodePtr> sorted_nodes;
  88. auto seen = NewSeenGeneration();
  89. while (!todo.empty()) {
  90. CNodePtr top = todo.front();
  91. todo.pop();
  92. sorted_nodes.push_back(top);
  93. auto inputs = top->inputs();
  94. for (auto &item : inputs) {
  95. if (item->seen_ == seen) {
  96. continue;
  97. }
  98. if (item->isa<CNode>()) {
  99. todo.push(item->cast<CNodePtr>());
  100. }
  101. item->seen_ = seen;
  102. }
  103. }
  104. return sorted_nodes;
  105. }
  106. std::vector<FuncGraphPtr> BroadFirstSearchGraphUsed(FuncGraphPtr root) {
  107. std::deque<FuncGraphPtr> todo;
  108. todo.push_back(root);
  109. std::vector<FuncGraphPtr> sorted;
  110. auto seen = NewSeenGeneration();
  111. while (!todo.empty()) {
  112. FuncGraphPtr top = todo.front();
  113. todo.pop_front();
  114. sorted.push_back(top);
  115. auto used = top->func_graphs_used();
  116. for (auto &item : used) {
  117. if (item.first->seen_ == seen) {
  118. continue;
  119. }
  120. todo.push_back(item.first);
  121. item.first->seen_ = seen;
  122. }
  123. }
  124. return sorted;
  125. }
  126. std::vector<AnfNodePtr> SuccDeeper(const AnfNodePtr &node) {
  127. std::vector<AnfNodePtr> vecs;
  128. if (node == nullptr) {
  129. return vecs;
  130. }
  131. if (IsValueNode<FuncGraph>(node)) {
  132. auto graph = GetValueNode<FuncGraphPtr>(node);
  133. auto ret = graph->get_return();
  134. if (ret != nullptr) {
  135. vecs.push_back(ret);
  136. }
  137. return vecs;
  138. } else if (node->func_graph() != nullptr) {
  139. if (node->isa<CNode>()) {
  140. auto &inputs = node->cast<CNodePtr>()->inputs();
  141. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  142. }
  143. auto graph = node->func_graph();
  144. if (graph->get_return() != nullptr) {
  145. vecs.push_back(graph->get_return());
  146. }
  147. return vecs;
  148. }
  149. return vecs;
  150. }
  151. std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node) {
  152. std::vector<AnfNodePtr> vecs;
  153. if (node == nullptr) {
  154. return vecs;
  155. }
  156. if (IsValueNode<FuncGraph>(node)) {
  157. auto graph = GetValueNode<FuncGraphPtr>(node);
  158. auto ret = graph->get_return();
  159. if (ret != nullptr) {
  160. vecs.push_back(ret);
  161. }
  162. return vecs;
  163. } else {
  164. if (node->isa<CNode>()) {
  165. auto &inputs = node->cast<CNodePtr>()->inputs();
  166. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  167. }
  168. return vecs;
  169. }
  170. }
  171. std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node) {
  172. std::vector<AnfNodePtr> vecs;
  173. if (node == nullptr) {
  174. return vecs;
  175. }
  176. if (node->isa<CNode>()) {
  177. auto &inputs = node->cast<CNodePtr>()->inputs();
  178. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  179. }
  180. return vecs;
  181. }
  182. std::vector<AnfNodePtr> SuccIncludeFV(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  183. std::vector<AnfNodePtr> vecs;
  184. if (node == nullptr) {
  185. return vecs;
  186. }
  187. if (node->isa<CNode>()) {
  188. auto cnode = node->cast<CNodePtr>();
  189. auto &inputs = cnode->inputs();
  190. // Check if free variables used.
  191. for (const auto &input : inputs) {
  192. auto input_fg = GetValueNode<FuncGraphPtr>(input);
  193. if (input_fg) {
  194. for (auto &fv : input_fg->free_variables_nodes()) {
  195. if (fv->func_graph() == fg && fg->nodes().contains(fv)) {
  196. vecs.push_back(fv);
  197. }
  198. }
  199. }
  200. }
  201. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  202. }
  203. return vecs;
  204. }
  205. IncludeType AlwaysInclude(const AnfNodePtr &) { return FOLLOW; }
  206. IncludeType IncludeBelongGraph(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  207. if (node->func_graph() == fg) {
  208. return FOLLOW;
  209. } else {
  210. return EXCLUDE;
  211. }
  212. }
  213. FuncGraphIndex::FuncGraphIndex(const FuncGraphPtr &fg, const SearchFunc &search, const IncludeFunc &include) {
  214. MS_EXCEPTION_IF_NULL(fg);
  215. Acquire(fg);
  216. auto vec = search(fg->get_return(), include);
  217. for (auto &node : vec) {
  218. MS_EXCEPTION_IF_NULL(node);
  219. Acquire(node);
  220. if (node->func_graph() != nullptr) {
  221. Acquire(node->func_graph());
  222. }
  223. }
  224. }
  225. std::set<FuncGraphPtr> FuncGraphIndex::GetFuncGraphs(const std::string &key) {
  226. std::set<FuncGraphPtr> func_graphs;
  227. if (index_func_graph_.find(key) != index_func_graph_.end()) {
  228. func_graphs = index_func_graph_[key];
  229. }
  230. return func_graphs;
  231. }
  232. std::set<AnfNodePtr> FuncGraphIndex::GetNodes(const std::string &key) {
  233. if (index_node_.find(key) != index_node_.end()) {
  234. return index_node_[key];
  235. }
  236. return std::set<AnfNodePtr>();
  237. }
  238. FuncGraphPtr FuncGraphIndex::GetFirstFuncGraph(const std::string &key) {
  239. if (GetFuncGraphs(key).empty()) {
  240. return nullptr;
  241. }
  242. auto fg = *GetFuncGraphs(key).begin();
  243. return fg;
  244. }
  245. AnfNodePtr FuncGraphIndex::GetFirstNode(const std::string &key) {
  246. if (GetNodes(key).empty()) {
  247. return nullptr;
  248. }
  249. auto node = *GetNodes(key).begin();
  250. return node;
  251. }
  252. void FuncGraphIndex::Acquire(const FuncGraphPtr &key) {
  253. std::string name = label_manage::Label(key->debug_info());
  254. if (!name.empty()) {
  255. (void)index_func_graph_[name].insert(key);
  256. }
  257. }
  258. void FuncGraphIndex::Acquire(const AnfNodePtr &key) {
  259. std::string name = label_manage::Label(key->debug_info());
  260. if (!name.empty()) {
  261. (void)index_node_[name].insert(key);
  262. }
  263. }
  264. } // namespace mindspore