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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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/func_graph.h"
  27. #include "debug/label.h"
  28. #include "utils/log_adapter.h"
  29. #include "common/utils.h"
  30. #include "pipeline/parse/function_block.h"
  31. #include "pipeline/parse/python_adapter.h"
  32. namespace mindspore {
  33. namespace {
  34. class DeepFirstSearcher : public AnfVisitor {
  35. public:
  36. explicit DeepFirstSearcher(const IncludeFunc &include) : include_(include) {}
  37. ~DeepFirstSearcher() override = default;
  38. std::vector<AnfNodePtr> Search(const AnfNodePtr &root) {
  39. if (root == nullptr) {
  40. return res_;
  41. }
  42. seen_ = NewSeenGeneration();
  43. Visit(root);
  44. return res_;
  45. }
  46. void Visit(const AnfNodePtr &node) override {
  47. MS_EXCEPTION_IF_NULL(node);
  48. if (node->seen_ == seen_) {
  49. return;
  50. }
  51. node->seen_ = seen_;
  52. auto incl = include_(node);
  53. if (incl == EXCLUDE) {
  54. return;
  55. }
  56. res_.push_back(node);
  57. if (incl == FOLLOW) {
  58. AnfVisitor::Visit(node);
  59. }
  60. }
  61. private:
  62. size_t seen_{0};
  63. IncludeFunc include_;
  64. std::vector<AnfNodePtr> res_{};
  65. };
  66. class DeepScopedGraphSearcher : public DeepFirstSearcher {
  67. public:
  68. explicit DeepScopedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  69. ~DeepScopedGraphSearcher() override = default;
  70. void Visit(const CNodePtr &cnode) override {
  71. if (cnode->func_graph() == nullptr) {
  72. return;
  73. }
  74. AnfNodePtr ret = cnode->func_graph()->get_return();
  75. if (ret != nullptr) {
  76. DeepFirstSearcher::Visit(ret);
  77. }
  78. auto &inputs = cnode->inputs();
  79. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  80. DeepFirstSearcher::Visit(*iter);
  81. }
  82. }
  83. void Visit(const ValueNodePtr &vnode) override {
  84. if (!IsValueNode<FuncGraph>(vnode)) {
  85. return;
  86. }
  87. auto graph = GetValueNode<FuncGraphPtr>(vnode);
  88. AnfNodePtr ret = graph->get_return();
  89. if (ret != nullptr) {
  90. DeepFirstSearcher::Visit(ret);
  91. }
  92. }
  93. void Visit(const ParameterPtr &param) override {
  94. if (param->func_graph() == nullptr) {
  95. return;
  96. }
  97. AnfNodePtr ret = param->func_graph()->get_return();
  98. if (ret != nullptr) {
  99. DeepFirstSearcher::Visit(ret);
  100. }
  101. }
  102. };
  103. class DeepUsedGraphSearcher : public DeepFirstSearcher {
  104. public:
  105. explicit DeepUsedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  106. ~DeepUsedGraphSearcher() override = default;
  107. void Visit(const CNodePtr &cnode) override {
  108. auto &inputs = cnode->inputs();
  109. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  110. DeepFirstSearcher::Visit(*iter);
  111. }
  112. }
  113. void Visit(const ValueNodePtr &vnode) override {
  114. if (!IsValueNode<FuncGraph>(vnode)) {
  115. return;
  116. }
  117. auto graph = GetValueNode<FuncGraphPtr>(vnode);
  118. AnfNodePtr ret = graph->get_return();
  119. if (ret != nullptr) {
  120. DeepFirstSearcher::Visit(ret);
  121. }
  122. }
  123. };
  124. class DeepLinkedGraphSearcher : public DeepFirstSearcher {
  125. public:
  126. explicit DeepLinkedGraphSearcher(const IncludeFunc &include) : DeepFirstSearcher(include) {}
  127. ~DeepLinkedGraphSearcher() override = default;
  128. void Visit(const CNodePtr &cnode) override {
  129. auto &inputs = cnode->inputs();
  130. for (auto iter = inputs.rbegin(); iter != inputs.rend(); ++iter) {
  131. DeepFirstSearcher::Visit(*iter);
  132. }
  133. }
  134. void Visit(const ValueNodePtr &) override {}
  135. };
  136. } // namespace
  137. std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  138. return DeepScopedGraphSearcher(include).Search(root);
  139. }
  140. std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  141. return DeepUsedGraphSearcher(include).Search(root);
  142. }
  143. std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include) {
  144. return DeepLinkedGraphSearcher(include).Search(root);
  145. }
  146. } // namespace mindspore