• 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 
17 #ifndef MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_FFN_FUSION_H_
18 #define MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_FFN_FUSION_H_
19 
20 #include <vector>
21 #include <memory>
22 #include <string>
23 #include <unordered_map>
24 #include "tools/optimizer/common/multiple_pattern_process_pass.h"
25 
26 namespace mindspore {
27 namespace opt {
28 enum PatternType {
29   kDynamicDims,
30   kConstFold,
31   kMaxPatternNum,
32 };
33 
34 class FFNFusion : public MultiplePatternProcessPass {
35  public:
36   explicit FFNFusion(const std::string &name = "FFNFusion", bool multigraph = true)
MultiplePatternProcessPass(name,multigraph)37       : MultiplePatternProcessPass(name, multigraph) {}
38 
39   ~FFNFusion() override = default;
40 
41   std::unordered_map<std::string, VectorRef> DefinePatterns() const override;
42 
43   AnfNodePtr Process(const std::string &pattern_name, const FuncGraphPtr &, const AnfNodePtr &,
44                      const EquivPtr &) const override;
45 
46  private:
47   bool Init() const;
48   bool CheckPattern(const std::string &pattern_name, const EquivPtr &equiv) const;
49 
50   const VectorRef DefineFFNPatternForConstFolding() const;
51   const VectorRef DefineFFNPatternForDynamicDims() const;
52 
53   CNodePtr CreateFFNFusionNode(const FuncGraphPtr &func_graph, const AnfNodePtr &node, const EquivPtr &equiv,
54                                int index) const;
55 
56   mutable VarPtr input_[kMaxPatternNum] = {nullptr};
57 
58   mutable VarPtr gather_y_{nullptr};
59   mutable VarPtr add2_y_{nullptr};
60   mutable VarPtr div1_y_{nullptr};
61   mutable VarPtr mul1_y_{nullptr};
62   mutable VarPtr mul2_y_{nullptr};
63 
64   mutable VarPtr div2_y_[kMaxPatternNum] = {nullptr};
65   mutable VarPtr add3_y_[kMaxPatternNum] = {nullptr};
66   mutable VarPtr mul4_y_[kMaxPatternNum] = {nullptr};
67   mutable VarPtr matmul1_b_[kMaxPatternNum] = {nullptr};
68   mutable VarPtr add1_x_[kMaxPatternNum] = {nullptr};
69   mutable VarPtr matmul2_b_[kMaxPatternNum] = {nullptr};
70 };
71 }  // namespace opt
72 }  // namespace mindspore
73 
74 #endif  // MINDSPORE_LITE_TOOLS_OPTIMIZER_FUSION_FFN_FUSION_H_
75