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 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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<AnfNodePtr> SuccDeeper(const AnfNodePtr &node) {
  107. std::vector<AnfNodePtr> vecs;
  108. if (node == nullptr) {
  109. return vecs;
  110. }
  111. if (IsValueNode<FuncGraph>(node)) {
  112. auto graph = GetValueNode<FuncGraphPtr>(node);
  113. auto ret = graph->get_return();
  114. if (ret != nullptr) {
  115. vecs.push_back(ret);
  116. }
  117. return vecs;
  118. } else if (node->func_graph() != nullptr) {
  119. if (node->isa<CNode>()) {
  120. auto &inputs = node->cast<CNodePtr>()->inputs();
  121. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  122. }
  123. auto graph = node->func_graph();
  124. if (graph->get_return() != nullptr) {
  125. vecs.push_back(graph->get_return());
  126. }
  127. return vecs;
  128. }
  129. return vecs;
  130. }
  131. std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node) {
  132. std::vector<AnfNodePtr> vecs;
  133. if (node == nullptr) {
  134. return vecs;
  135. }
  136. if (IsValueNode<FuncGraph>(node)) {
  137. auto graph = GetValueNode<FuncGraphPtr>(node);
  138. auto ret = graph->get_return();
  139. if (ret != nullptr) {
  140. vecs.push_back(ret);
  141. }
  142. return vecs;
  143. } else {
  144. if (node->isa<CNode>()) {
  145. auto &inputs = node->cast<CNodePtr>()->inputs();
  146. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  147. }
  148. return vecs;
  149. }
  150. }
  151. std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node) {
  152. std::vector<AnfNodePtr> vecs;
  153. if (node == nullptr) {
  154. return vecs;
  155. }
  156. if (node->isa<CNode>()) {
  157. auto &inputs = node->cast<CNodePtr>()->inputs();
  158. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  159. }
  160. return vecs;
  161. }
  162. std::vector<AnfNodePtr> SuccIncludeFV(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  163. std::vector<AnfNodePtr> vecs;
  164. if (node == nullptr) {
  165. return vecs;
  166. }
  167. if (node->isa<CNode>()) {
  168. auto cnode = node->cast<CNodePtr>();
  169. auto &inputs = cnode->inputs();
  170. // Check if free variables used.
  171. for (const auto &input : inputs) {
  172. auto input_fg = GetValueNode<FuncGraphPtr>(input);
  173. if (input_fg) {
  174. for (auto &fv : input_fg->free_variables_nodes()) {
  175. if (fv->func_graph() == fg && fg->nodes().contains(fv)) {
  176. vecs.push_back(fv);
  177. }
  178. }
  179. }
  180. }
  181. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  182. }
  183. return vecs;
  184. }
  185. IncludeType AlwaysInclude(const AnfNodePtr &) { return FOLLOW; }
  186. IncludeType IncludeBelongGraph(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  187. if (node->func_graph() == fg) {
  188. return FOLLOW;
  189. } else {
  190. return EXCLUDE;
  191. }
  192. }
  193. FuncGraphIndex::FuncGraphIndex(const FuncGraphPtr &fg, const SearchFunc &search, const IncludeFunc &include) {
  194. MS_EXCEPTION_IF_NULL(fg);
  195. Acquire(fg);
  196. auto vec = search(fg->get_return(), include);
  197. for (auto &node : vec) {
  198. MS_EXCEPTION_IF_NULL(node);
  199. Acquire(node);
  200. if (node->func_graph() != nullptr) {
  201. Acquire(node->func_graph());
  202. }
  203. }
  204. }
  205. std::set<FuncGraphPtr> FuncGraphIndex::GetFuncGraphs(const std::string &key) {
  206. std::set<FuncGraphPtr> func_graphs;
  207. if (index_func_graph_.find(key) != index_func_graph_.end()) {
  208. func_graphs = index_func_graph_[key];
  209. }
  210. return func_graphs;
  211. }
  212. std::set<AnfNodePtr> FuncGraphIndex::GetNodes(const std::string &key) {
  213. if (index_node_.find(key) != index_node_.end()) {
  214. return index_node_[key];
  215. }
  216. return std::set<AnfNodePtr>();
  217. }
  218. FuncGraphPtr FuncGraphIndex::GetFirstFuncGraph(const std::string &key) {
  219. if (GetFuncGraphs(key).empty()) {
  220. return nullptr;
  221. }
  222. auto fg = *GetFuncGraphs(key).begin();
  223. return fg;
  224. }
  225. AnfNodePtr FuncGraphIndex::GetFirstNode(const std::string &key) {
  226. if (GetNodes(key).empty()) {
  227. return nullptr;
  228. }
  229. auto node = *GetNodes(key).begin();
  230. return node;
  231. }
  232. void FuncGraphIndex::Acquire(const FuncGraphPtr &key) {
  233. std::string name = label_manage::Label(key->debug_info());
  234. if (!name.empty()) {
  235. (void)index_func_graph_[name].insert(key);
  236. }
  237. }
  238. void FuncGraphIndex::Acquire(const AnfNodePtr &key) {
  239. std::string name = label_manage::Label(key->debug_info());
  240. if (!name.empty()) {
  241. (void)index_node_[name].insert(key);
  242. }
  243. }
  244. } // namespace mindspore