• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2024 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_ASCEND_IR_FUSION_MC2_FUSION_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_IR_FUSION_MC2_FUSION_H_
18 
19 #include <string>
20 #include <memory>
21 #include <vector>
22 #include <unordered_map>
23 #include "include/backend/optimizer/optimizer.h"
24 
25 namespace mindspore {
26 namespace opt {
27 class MC2FusionBase : public PatternProcessPass {
28  public:
PatternProcessPass(name,multigraph)29   explicit MC2FusionBase(const std::string &name = "", bool multigraph = true) : PatternProcessPass(name, multigraph) {}
30   ~MC2FusionBase() override = default;
31   const BaseRef DefinePattern() const override;
32   const AnfNodePtr Process(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
33                            const EquivPtr &equiv) const override;
34 
35  protected:
36   virtual const VectorRef DefineFusionPattern() const = 0;
37   virtual CNodePtr CreateFusionCNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
38                                      const EquivPtr &equiv) const = 0;
39 };
40 
41 class MatmulReduceScatterFusion : public MC2FusionBase {
42  public:
43   explicit MatmulReduceScatterFusion(const std::string &name = "matmul_reduce_scatter_fusion", bool multigraph = true)
MC2FusionBase(name,multigraph)44       : MC2FusionBase(name, multigraph) {}
45   ~MatmulReduceScatterFusion() override = default;
46 
47  private:
48   const VectorRef DefineFusionPattern() const override;
49   CNodePtr CreateFusionCNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
50                              const EquivPtr &equiv) const override;
51 };
52 
53 class AllGatherMatmulFusion : public MC2FusionBase {
54  public:
55   explicit AllGatherMatmulFusion(const std::string &name = "allgather_matmul_fusion", bool multigraph = true)
MC2FusionBase(name,multigraph)56       : MC2FusionBase(name, multigraph) {}
57   ~AllGatherMatmulFusion() override = default;
58 
59  private:
60   const VectorRef DefineFusionPattern() const override;
61   CNodePtr CreateFusionCNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node,
62                              const EquivPtr &equiv) const override;
63 };
64 }  // namespace opt
65 }  // namespace mindspore
66 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_ASCEND_IR_FUSION_MC2_FUSION_H_
67