• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 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_GRAPH_KERNEL_CLUSTER_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_GRAPH_KERNEL_CLUSTER_H_
18 
19 #include <vector>
20 #include <string>
21 #include <unordered_map>
22 #include <set>
23 #include <memory>
24 
25 #include "ir/anf.h"
26 #include "backend/optimizer/common/optimizer.h"
27 
28 namespace mindspore {
29 namespace prim {
30 inline const PrimitivePtr kPrimUserDefined = std::make_shared<Primitive>("UserDefined");
31 }
32 
33 namespace opt {
34 class Graph;
35 using GraphPtr = std::shared_ptr<Graph>;
36 class GraphKernelCluster : public Pass {
37  public:
GraphKernelCluster()38   GraphKernelCluster() : Pass("graph_kernel_cluster") {}
39   ~GraphKernelCluster() override = default;
40   bool Run(const FuncGraphPtr &func_graph) override;
41 
42  private:
43   std::vector<PrimitivePtr> GetClusterableOpList();
44   bool IsClusterableOp(const AnfNodePtr &node);
45   void Init(const FuncGraphPtr &func_graph);
46   bool Process(const FuncGraphPtr &func_graph);
47   std::vector<size_t> FindCandidates(size_t basenode_id);
48   void RemoveWildGetitem(std::vector<size_t> *candidates);
49   void CreateFuncGraph(const FuncGraphPtr &func_graph, const std::vector<size_t> &nodes_id);
50   void DumpClusterInfo(const AnfNodePtrList &old_nodes, const AnfNodePtr &new_node);
51   void DumpToFile();
Clean()52   void Clean() {
53     std::vector<AnfNodePtr>().swap(nodes_);
54     node_idx_map_.clear();
55     graph_ = nullptr;
56   }
57 
58   GraphPtr graph_{nullptr};
59   std::vector<AnfNodePtr> nodes_;
60   std::unordered_map<AnfNodePtr, size_t> node_idx_map_;
61   std::stringstream dump_buf_;
62   std::vector<PrimitivePtr> op_list_;
63 };
64 }  // namespace opt
65 }  // namespace mindspore
66 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_GRAPH_KERNEL_CLUSTER_H_
67