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.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. #ifndef MINDSPORE_CCSRC_UTILS_GRAPH_UTILS_H_
  19. #define MINDSPORE_CCSRC_UTILS_GRAPH_UTILS_H_
  20. #include <unordered_map>
  21. #include <unordered_set>
  22. #include <utility>
  23. #include <memory>
  24. #include <vector>
  25. #include <map>
  26. #include <set>
  27. #include <string>
  28. #include "ir/anf.h"
  29. #include "ir/primitive_base.h"
  30. #include "ir/scalar.h"
  31. #include "ir/tensor.h"
  32. #include "debug/label.h"
  33. namespace mindspore {
  34. enum IncludeType { FOLLOW, NOFOLLOW, EXCLUDE };
  35. using IncludeFunc = std::function<IncludeType(const AnfNodePtr &)>;
  36. using FilterFunc = std::function<bool(const AnfNodePtr &)>;
  37. using SuccFunc = std::function<std::vector<AnfNodePtr>(AnfNodePtr)>;
  38. using SearchFunc = std::function<std::vector<AnfNodePtr>(const AnfNodePtr &, const IncludeFunc &)>;
  39. std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
  40. std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
  41. std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include);
  42. std::vector<AnfNodePtr> SuccDeeper(const AnfNodePtr &node);
  43. std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node);
  44. std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node);
  45. std::vector<AnfNodePtr> SuccIncludeFV(const FuncGraphPtr &fg, const AnfNodePtr &node);
  46. IncludeType AlwaysInclude(const AnfNodePtr &node);
  47. IncludeType IncludeBelongGraph(const FuncGraphPtr &fg, const AnfNodePtr &node);
  48. std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  49. std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  50. std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  51. std::vector<AnfNodePtr> DeepScopedGraphSearchWithFilter(const AnfNodePtr &root, const IncludeFunc &include,
  52. const FilterFunc &filter);
  53. class FuncGraphManager;
  54. using FuncGraphManagerPtr = std::shared_ptr<FuncGraphManager>;
  55. std::vector<AnfNodePtr> DeepUsersSearch(const AnfNodePtr &root, const IncludeFunc &include,
  56. const FuncGraphManagerPtr &mng);
  57. std::vector<AnfNodePtr> TopoSort(const AnfNodePtr &root, const SuccFunc &succ = SuccIncoming,
  58. const IncludeFunc &include = AlwaysInclude);
  59. std::vector<CNodePtr> BroadFirstSearchGraphCNodes(CNodePtr ret);
  60. class FuncGraphIndex {
  61. public:
  62. explicit FuncGraphIndex(const FuncGraphPtr &fg, const SearchFunc &search = DeepScopedGraphSearch,
  63. const IncludeFunc &include = AlwaysInclude);
  64. FuncGraphIndex(const FuncGraphIndex &) = delete;
  65. FuncGraphIndex &operator=(const FuncGraphIndex &) = delete;
  66. virtual ~FuncGraphIndex() {}
  67. std::set<FuncGraphPtr> GetFuncGraphs(const std::string &key);
  68. std::set<AnfNodePtr> GetNodes(const std::string &key);
  69. FuncGraphPtr GetFirstFuncGraph(const std::string &key);
  70. AnfNodePtr GetFirstNode(const std::string &key);
  71. private:
  72. void Acquire(const FuncGraphPtr &key);
  73. void Acquire(const AnfNodePtr &key);
  74. std::map<std::string, std::set<FuncGraphPtr>> index_func_graph_;
  75. std::map<std::string, std::set<AnfNodePtr>> index_node_;
  76. };
  77. } // namespace mindspore
  78. #endif // MINDSPORE_CCSRC_UTILS_GRAPH_UTILS_H_