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_extends.cc 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "utils/graph_utils.h"
  17. #include <unordered_map>
  18. #include <unordered_set>
  19. #include <utility>
  20. #include <stack>
  21. #include <vector>
  22. #include <list>
  23. #include <string>
  24. #include <fstream>
  25. #include "ir/visitor.h"
  26. #include "ir/manager.h"
  27. #include "ir/func_graph.h"
  28. #include "debug/label.h"
  29. #include "utils/log_adapter.h"
  30. #include "common/utils.h"
  31. #include "pipeline/parse/function_block.h"
  32. #include "pipeline/parse/python_adapter.h"
  33. namespace mindspore {
  34. namespace {
  35. class DeepFirstSearcher : public AnfVisitor {
  36. public:
  37. explicit DeepFirstSearcher(const IncludeFunc &include, const FilterFunc &filter = nullptr)
  38. : include_(include), filter_(filter) {}
  39. ~DeepFirstSearcher() override = default;
  40. std::vector<AnfNodePtr> Search(const AnfNodePtr &root) {
  41. if (root == nullptr) {
  42. return res_;
  43. }
  44. seen_ = NewSeenGeneration();
  45. Visit(root);
  46. return res_;
  47. }
  48. void Visit(const AnfNodePtr &node) override {
  49. MS_EXCEPTION_IF_NULL(node);
  50. if (node->seen_ == seen_) {
  51. return;
  52. }
  53. node->seen_ = seen_;
  54. auto incl = include_(node);
  55. if (incl == EXCLUDE) {
  56. return;
  57. }
  58. if (filter_ == nullptr || !filter_(node)) {
  59. res_.push_back(node);
  60. }
  61. if (incl == FOLLOW) {
  62. AnfVisitor::Visit(node);
  63. }
  64. }
  65. private:
  66. size_t seen_{0};
  67. IncludeFunc include_;
  68. FilterFunc filter_;
  69. std::vector<AnfNodePtr> res_{};
  70. };
  71. class DeepScopedGraphSearcher : public DeepFirstSearcher {
  72. public:
  73. explicit DeepScopedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  74. ~DeepScopedGraphSearcher() override = default;
  75. void Visit(const CNodePtr &cnode) override {
  76. if (cnode->func_graph() == nullptr) {
  77. return;
  78. }
  79. AnfNodePtr ret = cnode->func_graph()->get_return();
  80. if (ret != nullptr) {
  81. DeepFirstSearcher::Visit(ret);
  82. }
  83. auto &inputs = cnode->inputs();
  84. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  85. DeepFirstSearcher::Visit(*iter);
  86. }
  87. }
  88. void Visit(const ValueNodePtr &vnode) override {
  89. if (!IsValueNode<FuncGraph>(vnode)) {
  90. return;
  91. }
  92. auto graph = GetValueNode<FuncGraphPtr>(vnode);
  93. AnfNodePtr ret = graph->get_return();
  94. if (ret != nullptr) {
  95. DeepFirstSearcher::Visit(ret);
  96. }
  97. }
  98. void Visit(const ParameterPtr &param) override {
  99. if (param->func_graph() == nullptr) {
  100. return;
  101. }
  102. AnfNodePtr ret = param->func_graph()->get_return();
  103. if (ret != nullptr) {
  104. DeepFirstSearcher::Visit(ret);
  105. }
  106. }
  107. };
  108. class DeepUsedGraphSearcher : public DeepFirstSearcher {
  109. public:
  110. explicit DeepUsedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  111. ~DeepUsedGraphSearcher() override = default;
  112. void Visit(const CNodePtr &cnode) override {
  113. auto &inputs = cnode->inputs();
  114. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  115. DeepFirstSearcher::Visit(*iter);
  116. }
  117. }
  118. void Visit(const ValueNodePtr &vnode) override {
  119. if (!IsValueNode<FuncGraph>(vnode)) {
  120. return;
  121. }
  122. auto graph = GetValueNode<FuncGraphPtr>(vnode);
  123. AnfNodePtr ret = graph->get_return();
  124. if (ret != nullptr) {
  125. DeepFirstSearcher::Visit(ret);
  126. }
  127. }
  128. };
  129. class DeepLinkedGraphSearcher : public DeepFirstSearcher {
  130. public:
  131. explicit DeepLinkedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  132. ~DeepLinkedGraphSearcher() override = default;
  133. void Visit(const CNodePtr &cnode) override {
  134. auto &inputs = cnode->inputs();
  135. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  136. DeepFirstSearcher::Visit(*iter);
  137. }
  138. }
  139. void Visit(const ValueNodePtr &) override {}
  140. };
  141. class DeepUsersSearcher : public DeepFirstSearcher {
  142. public:
  143. explicit DeepUsersSearcher(const IncludeFunc &include, const FuncGraphManagerPtr &mng)
  144. : DeepFirstSearcher(include), mng_(mng) {}
  145. ~DeepUsersSearcher() override = default;
  146. void Visit(const CNodePtr &cnode) override {
  147. auto &users = mng_->node_users()[cnode];
  148. for (auto iter = users.begin(); iter != users.end(); ++iter) {
  149. DeepFirstSearcher::Visit(iter->first);
  150. }
  151. }
  152. void Visit(const ValueNodePtr &) override {}
  153. private:
  154. FuncGraphManagerPtr mng_;
  155. };
  156. } // namespace
  157. // include for if expand the node the search, filter for if put the node to results.
  158. std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  159. return DeepScopedGraphSearcher(include).Search(root);
  160. }
  161. std::vector<AnfNodePtr> DeepScopedGraphSearchWithFilter(const AnfNodePtr &root, const IncludeFunc &include,
  162. const FilterFunc &filter) {
  163. return DeepFirstSearcher(include, filter).Search(root);
  164. }
  165. std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  166. return DeepUsedGraphSearcher(include).Search(root);
  167. }
  168. std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  169. return DeepLinkedGraphSearcher(include).Search(root);
  170. }
  171. std::vector<AnfNodePtr> DeepUsersSearch(const AnfNodePtr &root, const IncludeFunc &include,
  172. const FuncGraphManagerPtr &mng) {
  173. return DeepUsersSearcher(include, mng).Search(root);
  174. }
  175. } // namespace mindspore