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.

func_graph_cloner.h 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. * Copyright 2019 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. #ifndef MINDSPORE_CCSRC_IR_FUNC_GRAPH_CLONER_H_
  17. #define MINDSPORE_CCSRC_IR_FUNC_GRAPH_CLONER_H_
  18. #include <functional>
  19. #include <list>
  20. #include <memory>
  21. #include <string>
  22. #include <unordered_map>
  23. #include <unordered_set>
  24. #include <utility>
  25. #include <vector>
  26. #include "ir/anf.h"
  27. #include "ir/func_graph.h"
  28. namespace mindspore {
  29. class Cloner;
  30. using ClonerPtr = std::shared_ptr<Cloner>;
  31. enum CloneType { kBasic = 0, kInline = 1, kLifting = 2, kDropping = 3 };
  32. struct CloneInfo {
  33. FuncGraphPtr origin;
  34. FuncGraphPtr target;
  35. AnfNodePtrList params;
  36. };
  37. class Cloner {
  38. public:
  39. explicit Cloner(const FuncGraphPtrList &func_graphs = {}, bool clone_all_valuenodes = false,
  40. bool clone_all_child_graphs = true, bool clone_all_used_graphs = false,
  41. const TraceInfoPtr &relation = std::make_shared<TraceCopy>(),
  42. const TraceInfoPtr &target_relation = nullptr);
  43. ~Cloner() = default;
  44. void AddClone(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph = nullptr,
  45. const AnfNodePtrList &params = {}, CloneType type = kBasic);
  46. void Run();
  47. // Interfaces for specializer
  48. AnfNodePtr CloneDisconnected(const AnfNodePtr &root);
  49. AnfNodePtr operator[](const AnfNodePtr &node);
  50. FuncGraphPtr operator[](const FuncGraphPtr &func_graph);
  51. // Map of replicate nodes and graphs
  52. std::unordered_map<AnfNodePtr, AnfNodePtr> *cloned_node() { return &repl_node_; }
  53. std::unordered_map<FuncGraphPtr, FuncGraphPtr> cloned_func_graph() { return repl_func_graph_; }
  54. // Scope of cloned graphs
  55. void set_scope(const ScopePtr &scope) { scope_ = scope; }
  56. const ScopePtr scope() const { return scope_; }
  57. std::unordered_map<AnfNodePtr, AnfNodePtr> repl_node_;
  58. private:
  59. void CloneNodes();
  60. void LinkEdges();
  61. void SetDefaults();
  62. void CloneNode(const AnfNodePtr &node, const FuncGraphPtr &target);
  63. void CloneValueNode(const AnfNodePtr &node);
  64. void CloneValueNode(const AnfNodePtr &node, const FuncGraphPtr &target);
  65. void CloneCNode(const AnfNodePtr &node, const FuncGraphPtr &target);
  66. void CloneParameter(const AnfNodePtr &node, const FuncGraphPtr &target, bool is_add = false);
  67. void CloneValueNodes(const FuncGraphPtr &func_graph);
  68. void AddChildGraphs(const FuncGraphPtr &func_graph);
  69. void AddTotalGraphs(const FuncGraphPtr &func_graph);
  70. bool CheckStatus(const FuncGraphPtr &func_graph, bool is_inline);
  71. void CloneAllNodes(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph);
  72. void CloneFuncGraphValueNodes(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph);
  73. void CloneFuncGraphDefaultValues(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph);
  74. void InlineCloneParameters(const FuncGraphPtr &func_graph, const AnfNodePtrList &params);
  75. void SetFuncGraphInfo(const FuncGraphPtr &func_graph, FuncGraphPtr *const target_func_graph);
  76. void CloneParameters(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph);
  77. void GenParameters(const FuncGraphPtr &func_graph);
  78. void CloneParameter(const ParameterPtr &param, const AnfNodePtr &node);
  79. ParameterPtr AddParameter(const FuncGraphPtr &func_graph, const AnfNodePtr &node, bool is_add = true);
  80. void AddParameters(const FuncGraphPtr &func_graph, const AnfNodePtrList &params, AnfNodePtrList *const lift_params,
  81. AnfNodePtrList *const input_params);
  82. void AddInputs(const FuncGraphPtr &func_graph_user, const FuncGraphPtr &func_graph, const AnfNodePtrList &params);
  83. void OrderParameters(const FuncGraphPtr &func_graph, const AnfNodePtrList &inputs);
  84. void SetEdges(const FuncGraphPtr &func_graph);
  85. void LiftParameters(const FuncGraphPtr &func_graph_user, const FuncGraphPtr &func_graph,
  86. const AnfNodePtrList &params);
  87. void Lift();
  88. void LiftParameters();
  89. bool clone_all_valuenodes_;
  90. bool clone_all_child_graphs_;
  91. bool clone_all_used_graphs_;
  92. TraceInfoPtr relation_;
  93. TraceInfoPtr target_relation_;
  94. FuncGraphManagerPtr manager_;
  95. FuncGraphTransaction transaction_;
  96. FuncGraphSet graph_set_;
  97. ScopePtr scope_;
  98. CloneType type_;
  99. std::list<CloneInfo> todo_;
  100. std::list<std::pair<CNodePtr, CNodePtr>> nodes_;
  101. std::unordered_map<FuncGraphPtr, bool> status_;
  102. std::unordered_map<FuncGraphPtr, FuncGraphPtr> repl_func_graph_;
  103. std::unordered_map<FuncGraphPtr, std::unordered_map<AnfNodePtr, AnfNodePtr>> repl_map_node_;
  104. std::unordered_map<FuncGraphPtr, std::unordered_map<FuncGraphPtr, AnfNodePtr>> repl_map_func_graph_;
  105. std::unordered_map<FuncGraphPtr, AnfNodePtrList> repl_func_graph_params_;
  106. };
  107. FuncGraphPtr BasicClone(const FuncGraphPtr &func_graph);
  108. AnfNodePtr InlineClone(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph,
  109. const AnfNodePtrList &func_graph_args, const ScopePtr &scope = nullptr);
  110. FuncGraphPtr LiftingClone(const FuncGraphPtr &func_graph);
  111. ClonerPtr SpecializerClone(const FuncGraphPtr &func_graph, const TraceInfoPtr &relation);
  112. FuncGraphPtr TransformableClone(const FuncGraphPtr &func_graph,
  113. const TraceInfoPtr &relation = std::make_shared<TraceTransform>());
  114. } // namespace mindspore
  115. #endif // MINDSPORE_CCSRC_IR_FUNC_GRAPH_CLONER_H_