• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2023 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_BACKEND_SESSION_KERNEL_GRAPH_MGR_H
17 #define MINDSPORE_CCSRC_BACKEND_SESSION_KERNEL_GRAPH_MGR_H
18 
19 #include <vector>
20 #include <string>
21 #include <memory>
22 #include <map>
23 #include <set>
24 #include "utils/hash_map.h"
25 #include "include/backend/kernel_graph.h"
26 #include "include/common/utils/anfalgo.h"
27 #include "ir/anf.h"
28 #include "ir/tensor.h"
29 #include "include/backend/kernel_info.h"
30 #include "utils/ms_context.h"
31 #include "runtime/hardware/device_context.h"
32 #include "include/backend/visible.h"
33 
34 namespace mindspore {
35 using GraphId = uint32_t;
36 using GraphInfo = std::string;
37 
38 namespace session {
39 #ifndef ENABLE_SECURITY
40 bool ExistSummaryNode(const KernelGraph *graph);
41 #endif
42 ParamInfoPtr GetParamDefaultValue(const AnfNodePtr &node);
43 
44 struct PartialFuncInfo {
45   AbstractBasePtr abstract;
46   AnfNodePtr sub_graph;
47   size_t param_begin;
48   size_t param_end;
49   size_t multi_tuple;
50 };
51 
52 class BACKEND_EXPORT KernelGraphMgr {
53  public:
KernelGraphMgr()54   KernelGraphMgr() {}
~KernelGraphMgr()55   virtual ~KernelGraphMgr() {}
56 
57   // The parameter is_enable_zero_copy means if the parameter in graph can avoid copy when it is executed, and it is
58   // true in subgraph sink mode, and the device address shared for partial parameters and internal parameters in graph
59   // would be disabled.
60   std::shared_ptr<KernelGraph> ConstructKernelGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs,
61                                                     DeviceType device_target = DeviceType::kUnknown,
62                                                     bool common_opt = true, bool is_enable_zero_copy = false);
63 
64   std::shared_ptr<KernelGraph> ConstructKernelGraph(const FuncGraphPtr &func_graph,
65                                                     std::vector<KernelGraphPtr> *all_out_graph,
66                                                     DeviceType device_target);
67 
68   std::shared_ptr<KernelGraph> ConstructKernelGraph(std::vector<KernelGraphPtr> *all_out_graph);
69   std::shared_ptr<KernelGraph> ConstructPackKernelGraph(const FuncGraphPtr &func_graph,
70                                                         std::vector<KernelGraphPtr> *all_out_graph,
71                                                         DeviceType device_target);
72 
73   void SetInputNodeUsage(const KernelGraphPtr &graph, const FuncGraphManagerPtr &manager) const;
74 
75   CNodePtr CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph,
76                           mindspore::HashMap<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
77 
78   // get graph id in child graphs by ME front anf node pointer
79   virtual GraphId GetGraphIdByNode(const AnfNodePtr &) const;
80 
81   // Get graph by graph id, if not exist return null ptr
82   KernelGraphPtr GetGraph(GraphId graph_id) const;
83   void ClearGraph();
84   virtual void UnifyMindIR(const KernelGraphPtr &graph);
85   virtual ParameterPtr CreateNewParameterFromParameter(const AnfNodePtr &anf, KernelGraph *graph);
86   // create a new kernel graph and update the graph sum
87   KernelGraphPtr NewKernelGraph();
88   KernelGraphPtr NewPynativeKernelGraph();
89   void SetKernelGraphId(const KernelGraphPtr &kernel_graph);
90   AnfNodePtr CreateParameterFromTuple(const AnfNodePtr &node, KernelGraph *graph) const;
91 
92   AnfNodePtr CreateNewParameterFromCNode(const AnfNodePtr &anf, KernelGraph *graph);
93   ValueNodePtr CreateNewValueNode(const AnfNodePtr &anf, KernelGraph *graph) const;
94   bool CreateCNodeOfKernelGraph(const AnfNodePtr &node, KernelGraph *graph);
95   CNodePtr CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph);
96 
GraphSum()97   GraphId GraphSum() const { return graph_sum_; }
ClearPartialParameterMap()98   void ClearPartialParameterMap() { partial_parameters_map_.clear(); }
99 
GetFrontBackendGraphMap()100   mindspore::HashMap<FuncGraph *, KernelGraphPtr> GetFrontBackendGraphMap() const { return front_backend_graph_map_; }
101   void CacheKernelGraph(const KernelGraphPtr &kg);
102   // do inline
103   static AnfNodePtr DoInline(const FuncGraphPtr &func_graph, const FuncGraphPtr &target_func_graph,
104                              const AnfNodePtrList &func_graph_args, const ScopePtr &scope,
105                              const uint32_t &target_graph_id,
106                              const std::map<session::AnfWithOutIndex, session::AnfWithOutIndex> &ref_map,
107                              const KernelGraphPtr &graph, bool is_switch_inline);
108 
109  private:
110   void GetCNodeInfo(const CNodePtr &cnode, std::vector<AnfNodePtr> *cnode_inputs) const;
111   void GetNewCNodeInputs(const CNodePtr &cnode, KernelGraph *graph, std::vector<AnfNodePtr> *cnode_inputs,
112                          mindspore::HashMap<AnfNodePtr, AnfNodePtr> *other_graph_cnode);
113   AnfNodePtr GetChildGraph(KernelGraph *graph, const AnfNodePtr &child_func_graph);
114   void HandleInternalOutput(const AnfNodePtr &input_front_node, const AnfNodePtr &backend_node,
115                             const FuncGraphManagerPtr &front_func_graph_manager,
116                             const std::shared_ptr<KernelGraph> &backend_graph);
117   std::string AddPartialParametersMap(const AnfNodePtr &partial_node);
118 
119   CNodePtr CreateSwitchInput(const CNodePtr &cnode, const AnfNodePtr &node_input, KernelGraph *graph);
120   std::vector<AnfNodePtr> CreateSwitchOrPartialNode(const CNodePtr &cnode, KernelGraph *graph);
121   std::vector<AnfNodePtr> CreateValueNode(const CNodePtr &cnode, KernelGraph *graph);
122   void CreateCNodeInputs(const CNodePtr &cnode, KernelGraph *graph, std::vector<AnfNodePtr> *cnode_inputs);
123   std::vector<AnfNodePtr> CreateCallSwitchInputs(const CNodePtr &cnode, KernelGraph *graph) const;
124 
125   std::vector<AnfNodePtr> CreateCallSwitchLayerInputs(const CNodePtr &cnode, KernelGraph *graph);
126   void ProcessNodeRetFunc(const CNodePtr &cnode, KernelGraph *graph, const std::vector<AnfNodePtr> &real_inputs);
127 
128   ValueNodePtr CreateValueNodeKernelGraph(const AnfNodePtr &anf, KernelGraph *graph);
129   ParameterPtr CreateNewParameter(const AnfNodePtr &anf, KernelGraph *graph) const;
130   void AddParameterToGraphInputs(const std::vector<AnfNodePtr> &parameters, KernelGraph *graph) const;
131   void SetReturnNode(const AnfNodePtr &node, KernelGraph *graph);
132   void FlattenTuple(const CNodePtr &node);
133   bool ParseKernelGraphNodesAndAttrs(const nlohmann::json &model_json);
134 
135  protected:
136   CNodePtr ConstructOutput(const AnfNodePtrList &outputs, const std::shared_ptr<KernelGraph> &graph);
137 
138   void InitInternalOutputParameter(const AnfNodePtr &out_node, const AnfNodePtr &parameter) const;
139   void ConstructKernelGraphInner(const FuncGraphPtr &func_graph, std::vector<KernelGraphPtr> *all_out_graph,
140                                  DeviceType device_target, const KernelGraphPtr &graph);
141 
142   mindspore::HashMap<GraphId, std::shared_ptr<KernelGraph>> graphs_;
143   mindspore::HashMap<AnfNodePtr, AnfNodePtr> partial_parameters_map_;
144   mindspore::HashMap<AnfNodePtr, std::string> partial_target_map_;
145   mindspore::HashMap<AnfNodePtr, ParameterPtr> default_param_map_;
146   mindspore::HashMap<FuncGraph *, KernelGraphPtr> front_backend_graph_map_;
147   mindspore::HashMap<KernelGraph *, PartialFuncInfo> kernel_graph_partial_map_;
148   mindspore::HashSet<AnfNodePtr> need_flatten_;
149   mindspore::HashMap<AnfNodePtr, AnfNodePtr> need_flatten_tuple_map_;
150   static GraphId graph_sum_;
151   static GraphId pynative_graph_sum_;
152   // record all graphs's backend params unique name to its weak_ptr
153   // graph can come from different frontend graph
154   static mindspore::HashMap<std::string, std::weak_ptr<AnfNode>> name_to_params_;
155 };
156 }  // namespace session
157 }  // namespace mindspore
158 #endif  // MINDSPORE_CCSRC_BACKEND_SESSION_KERNEL_GRAPH_MGR_H
159