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.

kernel_graph.h 9.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_SESSION_KERNEL_GRAPH_H
  17. #define MINDSPORE_CCSRC_SESSION_KERNEL_GRAPH_H
  18. #include <vector>
  19. #include <unordered_map>
  20. #include <memory>
  21. #include <utility>
  22. #include <string>
  23. #include <queue>
  24. #include <map>
  25. #include <set>
  26. #include <unordered_set>
  27. #include "ir/func_graph.h"
  28. #include "ir/anf.h"
  29. #include "utils/graph_utils.h"
  30. #include "utils/contract.h"
  31. #include "device/kernel_info.h"
  32. namespace mindspore {
  33. namespace session {
  34. using AnfWithOutIndex = std::pair<AnfNodePtr, size_t>;
  35. class KernelGraph : public FuncGraph {
  36. public:
  37. KernelGraph() : graph_id_(0) {
  38. inputs_ = std::make_shared<std::vector<AnfNodePtr>>();
  39. execution_order_ = {};
  40. executable_ = true;
  41. stream_distinction_label_ = kInvalidDistincLabel;
  42. }
  43. ~KernelGraph() override;
  44. MS_DECLARE_PARENT(KernelGraph, FuncGraph);
  45. const std::vector<AnfNodePtr> &inputs() const;
  46. std::vector<AnfNodePtr> *MutableInputs() const { return inputs_.get(); }
  47. std::vector<AnfNodePtr> outputs() const;
  48. CNodePtr NewCNode(const std::vector<AnfNodePtr> &inputs) override;
  49. CNodePtr NewCNode(const CNodePtr &cnode);
  50. ParameterPtr NewParameter(const ParameterPtr &parameter = nullptr);
  51. ValueNodePtr NewValueNode(const ValueNodePtr &value_node = nullptr);
  52. std::vector<AnfNodePtr> SplitTupleValueNodeToNodeList(const ValueNodePtr &value_node);
  53. void set_execution_order(const std::vector<CNodePtr> &order) { execution_order_ = order; }
  54. const std::vector<CNodePtr> &execution_order() const { return execution_order_; }
  55. void SetExecOrderByDefault();
  56. uint32_t graph_id() const { return graph_id_; }
  57. void set_graph_id(uint32_t graph_id) { graph_id_ = graph_id; }
  58. // and a new front to backend anf relation to maop
  59. void FrontBackendlMapAdd(const AnfNodePtr &front_anf, const AnfNodePtr &backend_anf);
  60. // replace old backend anf with new backend anf
  61. void FrontBackendlMapUpdate(const AnfNodePtr &old_backend_anf, const AnfNodePtr &new_backend_anf);
  62. // get backend anf by front anf
  63. AnfNodePtr GetBackendAnfByFrontAnf(const AnfNodePtr &front_anf);
  64. // check backend node whether exist in map
  65. bool BackendNodeExistInFrontBackendMap(const AnfNodePtr &backend_anf);
  66. // get value node by tensor
  67. ValueNodePtr GetValueNodeByTensor(const tensor::TensorPtr &tensor);
  68. // add value node tensor relation map
  69. void TensorValueNodeMapAdd(const tensor::TensorPtr &tensor, const ValueNodePtr &value_node);
  70. // get all value nodes of graph
  71. std::unordered_set<ValueNodePtr> graph_value_nodes() { return graph_value_nodes_; }
  72. // add value node to graph
  73. void AddValueNodeToGraph(const ValueNodePtr &value_node);
  74. // ref output is in map
  75. bool IsInRefOutputMap(const AnfWithOutIndex &pair) const;
  76. // get ref correspond pairs
  77. AnfWithOutIndex GetRefCorrespondOutput(const AnfWithOutIndex &out_pair) const;
  78. // add ref correspond pairs
  79. void AddRefCorrespondPairs(const AnfWithOutIndex &final_pair, const AnfWithOutIndex &origin_pair);
  80. // get map
  81. std::map<AnfWithOutIndex, AnfWithOutIndex> GetRefMap() const { return ref_out_in_map_; }
  82. // checkout whether loop exist in graph
  83. void CheckLoop();
  84. // check whether graph is executable
  85. bool executable() const { return executable_; }
  86. // set executable of graph
  87. void set_executable(bool executable) { executable_ = executable; }
  88. // set invalid inputs for control sink
  89. std::vector<bool> *MutableValidInputs() { return &valid_inputs_; }
  90. std::vector<bool> valid_inputs() const { return valid_inputs_; }
  91. // replace node in graph
  92. void ReplaceNode(const AnfNodePtr &old_anf_node, AnfNodePtr new_anf_node);
  93. // set stream label of graph
  94. void set_stream_distinction_label(uint32_t stream_label) { stream_distinction_label_ = stream_label; }
  95. // get stream label of graph
  96. uint32_t stream_distinction_label() { return stream_distinction_label_; }
  97. // refresh execute kernel stream label
  98. void UpdateExecuteKernelStreamLabel();
  99. // calculate the leaf graph order of root graph
  100. std::vector<std::shared_ptr<KernelGraph>> GetLeafGraphOrder();
  101. // the child graph of current graph
  102. const std::vector<std::shared_ptr<KernelGraph>> &child_graph_order() const { return child_graph_order_; }
  103. void set_child_graph_order(const std::vector<std::shared_ptr<KernelGraph>> &order) { child_graph_order_ = order; }
  104. // checkout whether current graph is leaf graph
  105. bool IsLeafGraph() const;
  106. // set input_tensors pointer of control parameter
  107. void set_input_ctrl_tensors(const std::shared_ptr<std::vector<tensor::TensorPtr>> &input_tensors_ptr) {
  108. input_ctrl_tensors_ = input_tensors_ptr;
  109. }
  110. // get input_tensors pointer of control parameter
  111. std::shared_ptr<std::vector<tensor::TensorPtr>> input_ctrl_tensors() const { return input_ctrl_tensors_; }
  112. // get parent kernel graph
  113. std::shared_ptr<KernelGraph> parent_graph() const { return parent_graph_; }
  114. // set parent kernel graph
  115. void set_parent_graph(const std::shared_ptr<KernelGraph> &parent_graph) { parent_graph_ = parent_graph; }
  116. // find anf node in graph
  117. std::vector<CNodePtr> FindNodeByPrimitive(const PrimitivePtr &primitive) const;
  118. // get real inputs
  119. const std::map<AnfNodePtr, std::set<AnfNodePtr>> &real_inputs() const { return real_inputs_; }
  120. std::set<AnfNodePtr> GetRealInput(const AnfNodePtr &parameter);
  121. void SetRealInput(const AnfNodePtr &parameter, const AnfNodePtr &arg);
  122. // used to dump ir
  123. std::string ToString() const override;
  124. // update the real input if the node is a call
  125. void UpdateCallRealInput();
  126. void set_start_label(const CNodePtr &start_label) { start_label_ = start_label; }
  127. CNodePtr get_start_label() { return start_label_; }
  128. void set_end_goto(const CNodePtr &end_goto) { end_goto_ = end_goto; }
  129. CNodePtr get_end_goto() { return end_goto_; }
  130. private:
  131. // remove value node form graph
  132. bool RemoveValueNodeFromGraph(const ValueNodePtr &value_node);
  133. void VisitNodeDescendants(const AnfNodePtr &node, std::queue<AnfNodePtr> *visit_queue,
  134. std::unordered_set<AnfNodePtr> *visited_nodes);
  135. // update node edge list
  136. void UpdateNodeEdgeList(std::queue<AnfNodePtr> *seed_nodes);
  137. // add node depend edge by data edge or control depend
  138. void AddDependEdge(const AnfNodePtr &node, const AnfNodePtr &input, size_t depend_edge_num);
  139. // handle control depend
  140. std::vector<AnfNodePtr> GetOutputNodes(const AnfNodePtr &node);
  141. bool HandleControlDependNode(const AnfNodePtr &node, std::queue<AnfNodePtr> *que,
  142. std::unordered_set<AnfNodePtr> *visited_nodes);
  143. void UpdateControlDependRelations(const std::vector<AnfNodePtr> &depends);
  144. std::shared_ptr<std::vector<AnfNodePtr>> inputs_;
  145. std::vector<CNodePtr> execution_order_;
  146. uint32_t graph_id_;
  147. uint32_t stream_distinction_label_;
  148. // record map bettween front anf and backend anf,use two map implement bidirectional map
  149. std::unordered_map<AnfNodePtr, AnfNodePtr> front_backend_anf_map_;
  150. std::unordered_map<AnfNodePtr, AnfNodePtr> backend_front_anf_map_;
  151. // there may be a tensor from ME backend ,a value ndoe will be create according the tensor,map record
  152. std::unordered_map<tensor::TensorPtr, ValueNodePtr> tensor_to_value_node_map_;
  153. // include all value nodes
  154. std::unordered_set<ValueNodePtr> graph_value_nodes_;
  155. std::unordered_map<AnfNodePtr, size_t> node_input_num_;
  156. std::unordered_map<AnfNodePtr, std::vector<std::pair<AnfNodePtr, size_t>>> node_input_edges_;
  157. // record map between ref final output anf with index and ref origin input with index
  158. std::map<AnfWithOutIndex, AnfWithOutIndex> ref_out_in_map_;
  159. std::unordered_map<AnfNodePtr, std::vector<std::pair<AnfNodePtr, size_t>>> node_output_edges_;
  160. // graph needn't execute
  161. bool executable_;
  162. // valid inputs
  163. std::vector<bool> valid_inputs_;
  164. // new members for control sink process
  165. // all child grahs refers to partial node
  166. std::map<AnfNodePtr, std::shared_ptr<KernelGraph>> node_to_child_graphs_;
  167. // child graph execute order in root graph
  168. std::vector<std::shared_ptr<KernelGraph>> child_graph_order_;
  169. // input_tensors of control parameter
  170. std::shared_ptr<std::vector<tensor::TensorPtr>> input_ctrl_tensors_;
  171. // parameter graph
  172. std::shared_ptr<KernelGraph> parent_graph_;
  173. // record real parameters,inputs_ is the formal parameters
  174. std::map<AnfNodePtr, std::set<AnfNodePtr>> real_inputs_;
  175. CNodePtr start_label_;
  176. CNodePtr end_goto_;
  177. };
  178. } // namespace session
  179. using KernelGraphPtr = std::shared_ptr<session::KernelGraph>;
  180. } // namespace mindspore
  181. #endif // MINDSPORE_CCSRC_SESSION_KERNEL_GRAPH_H