• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_SPLITTER_WITH_PY_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_SPLITTER_WITH_PY_H_
18 #include <memory>
19 #include <string>
20 #include <map>
21 #include <nlohmann/json.hpp>
22 #include "backend/common/graph_kernel/core/graph_kernel_splitter.h"
23 
24 namespace mindspore::graphkernel {
25 class SplitByJsonSchemer : public SplitSchemer {
26  public:
27   SplitByJsonSchemer() = default;
SplitByJsonSchemer(const std::map<std::string,AnfNodePtr> & address_node_map,const std::string & json_desc_str)28   SplitByJsonSchemer(const std::map<std::string, AnfNodePtr> &address_node_map, const std::string &json_desc_str)
29       : address_node_map_(address_node_map), json_desc_str_(json_desc_str) {}
30   virtual ~SplitByJsonSchemer() = default;
Split(const FuncGraphPtr & func_graph)31   bool Split(const FuncGraphPtr &func_graph) override {
32     if (!func_graph->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL)) {
33       MS_EXCEPTION(NotSupportError) << "func_graph must be a GraphKernel node.";
34     }
35     func_graph_ = func_graph;
36     this->Run();
37     return !split_plan_.empty();
38   }
39 
40  protected:
SplitByCostModel()41   virtual bool SplitByCostModel() { return SplitByJsonStr(address_node_map_, json_desc_str_); }
42   virtual bool SplitByJsonStr(const std::map<std::string, AnfNodePtr> &address_node_map, std::string split_graphs_str);
43   void RemoveHangingNodes();
44   virtual bool DecodeJson(const std::string &json_desc, const std::map<std::string, AnfNodePtr> &address_node_map);
45   virtual void Run();
46   virtual bool IsValidKernelNode(const AnfNodePtr &node) const;
47   virtual void GetValidKernelNodes();
48   void MapNodeGroup();
49 
50   // group the return node and last MakeTuple node (if exists).
51   virtual void GroupReturnNode();
52 
53   // assign virtual node to the same group of its input.
54   virtual void GroupVirtualNodes();
55 
56   std::shared_ptr<FuncGraph> func_graph_;
57   AnfNodePtrList topo_all_nodes_;
58   AnfNodePtrList topo_valid_nodes_;
59   mindspore::HashMap<AnfNodePtr, size_t> node_group_;
60   std::map<std::string, AnfNodePtr> address_node_map_;
61   std::string json_desc_str_;
62 };
63 
64 class CostModelSplitSchemer : public SplitByJsonSchemer {
65  public:
66   CostModelSplitSchemer() = default;
67   virtual ~CostModelSplitSchemer() = default;
68 
69  protected:
70   bool SplitByCostModel() override;
71 };
72 
73 class GraphKernelSplitterWithPy : public GraphKernelSplitter {
74  public:
75   explicit GraphKernelSplitterWithPy(bool is_dynamic = false)
76       : GraphKernelSplitter(is_dynamic ? "dyn_graph_kernel_splitter" : "graph_kernel_splitter"),
77         is_dynamic_(is_dynamic) {}
78   ~GraphKernelSplitterWithPy() = default;
79   std::shared_ptr<SplitSchemer> GetSplitSchema(const std::string &processor) override;
80 
81  private:
82   bool is_dynamic_;
83 };
84 using GraphKernelSplitterWithPyPtr = std::shared_ptr<GraphKernelSplitterWithPy>;
85 }  // namespace mindspore::graphkernel
86 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_SPLITTER_WITH_PY_H_
87