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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 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.h"
  30. #include "ir/scalar.h"
  31. #include "ir/meta_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 SuccFunc = std::function<std::vector<AnfNodePtr>(AnfNodePtr)>;
  37. using SearchFunc = std::function<std::vector<AnfNodePtr>(const AnfNodePtr &, const IncludeFunc &)>;
  38. std::vector<AnfNodePtr> SuccDeeper(const AnfNodePtr &node);
  39. std::vector<AnfNodePtr> SuccDeeperSimple(const AnfNodePtr &node);
  40. std::vector<AnfNodePtr> SuccIncoming(const AnfNodePtr &node);
  41. std::vector<AnfNodePtr> SuccIncludeFV(const FuncGraphPtr &fg, const AnfNodePtr &node);
  42. IncludeType AlwaysInclude(const AnfNodePtr &node);
  43. IncludeType IncludeBelongGraph(const FuncGraphPtr &fg, const AnfNodePtr &node);
  44. std::vector<AnfNodePtr> DeepScopedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  45. std::vector<AnfNodePtr> DeepUsedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  46. std::vector<AnfNodePtr> DeepLinkedGraphSearch(const AnfNodePtr &root, const IncludeFunc &include = AlwaysInclude);
  47. std::vector<AnfNodePtr> TopoSort(const AnfNodePtr &root, const SuccFunc &succ = SuccIncoming,
  48. const IncludeFunc &include = AlwaysInclude);
  49. class FuncGraphIndex {
  50. public:
  51. explicit FuncGraphIndex(const FuncGraphPtr &fg, const SearchFunc &search = DeepScopedGraphSearch,
  52. const IncludeFunc &include = AlwaysInclude);
  53. FuncGraphIndex(const FuncGraphIndex &) = delete;
  54. FuncGraphIndex &operator=(const FuncGraphIndex &) = delete;
  55. virtual ~FuncGraphIndex() {}
  56. std::set<FuncGraphPtr> GetFuncGraphs(const std::string &key);
  57. std::set<AnfNodePtr> GetNodes(const std::string &key);
  58. FuncGraphPtr GetFirstFuncGraph(const std::string &key);
  59. AnfNodePtr GetFirstNode(const std::string &key);
  60. private:
  61. void Acquire(const FuncGraphPtr &key);
  62. void Acquire(const AnfNodePtr &key);
  63. std::map<std::string, std::set<FuncGraphPtr>> index_func_graph_;
  64. std::map<std::string, std::set<AnfNodePtr>> index_node_;
  65. };
  66. // Isomorphism
  67. struct PairHasher {
  68. template <class T1, class T2>
  69. std::size_t operator()(const std::pair<T1, T2> &p) const {
  70. auto h1 = std::hash<T1>{}(p.first);
  71. auto h2 = std::hash<T2>{}(p.second);
  72. return h1 ^ h2;
  73. }
  74. };
  75. enum EquivState { kNotEquiv = 0, kEquiv = 1, kPending = 2 };
  76. using FuncGraphPairMapEquiv = std::unordered_map<std::pair<FuncGraphPtr, FuncGraphPtr>, EquivState, PairHasher>;
  77. using NodeMapEquiv = std::unordered_map<AnfNodePtr, AnfNodePtr>;
  78. bool Isomorphic(FuncGraphPtr g1, FuncGraphPtr g2, FuncGraphPairMapEquiv *equiv_func_graph, NodeMapEquiv *equiv_node);
  79. tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar);
  80. } // namespace mindspore
  81. #endif // MINDSPORE_CCSRC_UTILS_GRAPH_UTILS_H_