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

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