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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <list>
  25. #include <string>
  26. #include <fstream>
  27. #include <queue>
  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::list<AnfNodePtr> todo(1, root);
  37. std::unordered_map<AnfNodePtr, size_t> rank;
  38. std::vector<AnfNodePtr> res;
  39. while (!todo.empty()) {
  40. AnfNodePtr node = todo.back();
  41. if (node == nullptr || node->seen_ == seen) {
  42. todo.pop_back();
  43. continue;
  44. }
  45. if (rank.find(node) != rank.end() && rank[node] != todo.size()) {
  46. MS_LOG(EXCEPTION) << "Graph exists cycle, node " << node->DebugString();
  47. }
  48. rank[node] = todo.size();
  49. bool cont = false;
  50. auto incl = include(node);
  51. if (incl == FOLLOW) {
  52. auto succs = succ(node);
  53. for (const auto i : succs) {
  54. if ((i != nullptr && i->seen_ != seen)
  55. // Handle the case for 2 subgraphs calls each other.
  56. // If the ValueNodeGraph's return is already in the todo list, do not follow it.
  57. && !((std::find(todo.begin(), todo.end(), i) != todo.end()) && (i->func_graph() != nullptr) &&
  58. (i->func_graph()->get_return() == i))) {
  59. todo.push_back(i);
  60. cont = true;
  61. }
  62. }
  63. } else if (incl == NOFOLLOW) {
  64. // do nothing
  65. } else if (incl == EXCLUDE) {
  66. node->seen_ = seen;
  67. todo.pop_back();
  68. continue;
  69. } else {
  70. MS_LOG(EXCEPTION) << "include(node) must return one of: \"follow\", \"nofollow\", \"exclude\"";
  71. }
  72. if (cont) {
  73. continue;
  74. }
  75. node->seen_ = seen;
  76. res.push_back(node);
  77. todo.pop_back();
  78. }
  79. return res;
  80. }
  81. // search the cnodes inside this graph only
  82. std::vector<CNodePtr> BroadFirstSearchGraphCNodes(CNodePtr ret) {
  83. std::queue<CNodePtr> todo;
  84. todo.push(ret);
  85. std::vector<CNodePtr> sorted_nodes;
  86. auto seen = NewSeenGeneration();
  87. while (!todo.empty()) {
  88. CNodePtr top = todo.front();
  89. todo.pop();
  90. sorted_nodes.push_back(top);
  91. auto inputs = top->inputs();
  92. for (auto &item : inputs) {
  93. if (item->seen_ == seen) {
  94. continue;
  95. }
  96. if (item->isa<CNode>()) {
  97. todo.push(item->cast<CNodePtr>());
  98. }
  99. item->seen_ = seen;
  100. }
  101. }
  102. return sorted_nodes;
  103. }
  104. std::vector<AnfNodePtr> SuccDeeper(const AnfNodePtr &node) {
  105. std::vector<AnfNodePtr> vecs;
  106. if (node == nullptr) {
  107. return vecs;
  108. }
  109. if (IsValueNode<FuncGraph>(node)) {
  110. auto graph = GetValueNode<FuncGraphPtr>(node);
  111. auto ret = graph->get_return();
  112. if (ret != nullptr) {
  113. vecs.push_back(ret);
  114. }
  115. return vecs;
  116. } else if (node->func_graph() != nullptr) {
  117. if (node->isa<CNode>()) {
  118. auto &inputs = node->cast<CNodePtr>()->inputs();
  119. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  120. }
  121. auto graph = node->func_graph();
  122. if (graph->get_return() != nullptr) {
  123. vecs.push_back(graph->get_return());
  124. }
  125. return vecs;
  126. }
  127. return vecs;
  128. }
  129. std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node) {
  130. std::vector<AnfNodePtr> vecs;
  131. if (node == nullptr) {
  132. return vecs;
  133. }
  134. if (IsValueNode<FuncGraph>(node)) {
  135. auto graph = GetValueNode<FuncGraphPtr>(node);
  136. auto ret = graph->get_return();
  137. if (ret != nullptr) {
  138. vecs.push_back(ret);
  139. }
  140. return vecs;
  141. } else {
  142. if (node->isa<CNode>()) {
  143. auto &inputs = node->cast<CNodePtr>()->inputs();
  144. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  145. }
  146. return vecs;
  147. }
  148. }
  149. std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node) {
  150. std::vector<AnfNodePtr> vecs;
  151. if (node == nullptr) {
  152. return vecs;
  153. }
  154. if (node->isa<CNode>()) {
  155. auto &inputs = node->cast<CNodePtr>()->inputs();
  156. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  157. }
  158. return vecs;
  159. }
  160. std::vector<AnfNodePtr> SuccIncludeFV(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  161. std::vector<AnfNodePtr> vecs;
  162. if (node == nullptr) {
  163. return vecs;
  164. }
  165. if (node->isa<CNode>()) {
  166. auto cnode = node->cast<CNodePtr>();
  167. auto &inputs = cnode->inputs();
  168. // Check if free variables used.
  169. for (const auto &input : inputs) {
  170. auto input_fg = GetValueNode<FuncGraphPtr>(input);
  171. if (input_fg) {
  172. for (auto &fv : input_fg->free_variables_nodes()) {
  173. if (fv->func_graph() == fg && fg->nodes().contains(fv)) {
  174. vecs.push_back(fv);
  175. }
  176. }
  177. }
  178. }
  179. (void)vecs.insert(vecs.end(), inputs.begin(), inputs.end());
  180. }
  181. return vecs;
  182. }
  183. IncludeType AlwaysInclude(const AnfNodePtr &) { return FOLLOW; }
  184. IncludeType IncludeBelongGraph(const FuncGraphPtr &fg, const AnfNodePtr &node) {
  185. if (node->func_graph() == fg) {
  186. return FOLLOW;
  187. } else {
  188. return EXCLUDE;
  189. }
  190. }
  191. FuncGraphIndex::FuncGraphIndex(const FuncGraphPtr &fg, const SearchFunc &search, const IncludeFunc &include) {
  192. MS_EXCEPTION_IF_NULL(fg);
  193. Acquire(fg);
  194. auto vec = search(fg->get_return(), include);
  195. for (auto &node : vec) {
  196. MS_EXCEPTION_IF_NULL(node);
  197. Acquire(node);
  198. if (node->func_graph() != nullptr) {
  199. Acquire(node->func_graph());
  200. }
  201. }
  202. }
  203. std::set<FuncGraphPtr> FuncGraphIndex::GetFuncGraphs(const std::string &key) {
  204. std::set<FuncGraphPtr> func_graphs;
  205. if (index_func_graph_.find(key) != index_func_graph_.end()) {
  206. func_graphs = index_func_graph_[key];
  207. }
  208. return func_graphs;
  209. }
  210. std::set<AnfNodePtr> FuncGraphIndex::GetNodes(const std::string &key) {
  211. if (index_node_.find(key) != index_node_.end()) {
  212. return index_node_[key];
  213. }
  214. return std::set<AnfNodePtr>();
  215. }
  216. FuncGraphPtr FuncGraphIndex::GetFirstFuncGraph(const std::string &key) {
  217. if (GetFuncGraphs(key).empty()) {
  218. return nullptr;
  219. }
  220. auto fg = *GetFuncGraphs(key).begin();
  221. return fg;
  222. }
  223. AnfNodePtr FuncGraphIndex::GetFirstNode(const std::string &key) {
  224. if (GetNodes(key).empty()) {
  225. return nullptr;
  226. }
  227. auto node = *GetNodes(key).begin();
  228. return node;
  229. }
  230. void FuncGraphIndex::Acquire(const FuncGraphPtr &key) {
  231. std::string name = label_manage::Label(key->debug_info());
  232. if (!name.empty()) {
  233. (void)index_func_graph_[name].insert(key);
  234. }
  235. }
  236. void FuncGraphIndex::Acquire(const AnfNodePtr &key) {
  237. std::string name = label_manage::Label(key->debug_info());
  238. if (!name.empty()) {
  239. (void)index_node_[name].insert(key);
  240. }
  241. }
  242. } // namespace mindspore