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

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