| 1 | // Tencent is pleased to support the open source community by making ncnn available. |
| 2 | // |
| 3 | // Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. |
| 4 | // |
| 5 | // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except |
| 6 | // in compliance with the License. You may obtain a copy of the License at |
| 7 | // |
| 8 | // https://opensource.org/licenses/BSD-3-Clause |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software distributed |
| 11 | // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | // CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | // specific language governing permissions and limitations under the License. |
| 14 | |
| 15 | #include "fuse_rmsnorm.h" |
| 16 | |
| 17 | #include "pass_level2.h" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | namespace pnnx { |
| 23 | |
| 24 | class fuse_rmsnorm_pass : public GraphRewriterPass |
| 25 | { |
| 26 | public: |
| 27 | const char* match_pattern_graph() const |
| 28 | { |
| 29 | return R"PNNXIR(7767517 |
| 30 | 6 5 |
| 31 | pnnx.Input input 0 1 input |
| 32 | pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32 |
| 33 | pnnx.Expression op_1 1 1 input sq expr=pow(@0,2) |
| 34 | torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True |
| 35 | pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,rsqrt(add(@2,%eps)))) |
| 36 | pnnx.Output output 1 0 out |
| 37 | )PNNXIR"; |
| 38 | } |
| 39 | |
| 40 | const char* replace_pattern_graph() const |
| 41 | { |
| 42 | return R"PNNXIR(7767517 |
| 43 | 3 2 |
| 44 | pnnx.Input input 0 1 input |
| 45 | nn.RMSNorm rmsnorm 1 1 input out elementwise_affine=True eps=%eps normalized_shape=(%c) @weight=%op_0.data |
| 46 | pnnx.Output output 1 0 out |
| 47 | )PNNXIR"; |
| 48 | } |
| 49 | }; |
| 50 | |
| 51 | class fuse_rmsnorm_pass_1 : public fuse_rmsnorm_pass |
| 52 | { |
| 53 | public: |
| 54 | const char* match_pattern_graph() const |
| 55 | { |
| 56 | return R"PNNXIR(7767517 |
| 57 | 6 5 |
| 58 | pnnx.Input input 0 1 input |
| 59 | pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32 |
| 60 | pnnx.Expression op_1 1 1 input sq expr=pow(@0,2.000000e+00) |
| 61 | torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True |
| 62 | pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,reciprocal(sqrt(add(@2,%eps))))) |
| 63 | pnnx.Output output 1 0 out |
| 64 | )PNNXIR"; |
| 65 | } |
| 66 | }; |
| 67 | |
| 68 | class fuse_rmsnorm_pass_onnx : public fuse_rmsnorm_pass |
| 69 | { |
| 70 | public: |
| 71 | const char* match_pattern_graph() const |
| 72 | { |
| 73 | return R"PNNXIR(7767517 |
| 74 | 6 5 |
| 75 | pnnx.Input input 0 1 input |
| 76 | pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32 |
| 77 | pnnx.Expression op_1 1 1 input sq expr=pow(@0,2.000000e+00) |
| 78 | torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True |
| 79 | pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,div(1.000000e+00,sqrt(add(@2,%eps))))) |
| 80 | pnnx.Output output 1 0 out |
| 81 | )PNNXIR"; |
| 82 | } |
| 83 | }; |
| 84 | |
| 85 | void fuse_rmsnorm(Graph& graph) |
| 86 | { |
| 87 | fuse_rmsnorm_pass a; |
| 88 | fuse_rmsnorm_pass_1 a1; |
| 89 | fuse_rmsnorm_pass_onnx b; |
| 90 | int opindex = 0; |
| 91 | |
| 92 | pnnx_graph_rewrite(graph, &a, opindex); |
| 93 | pnnx_graph_rewrite(graph, &a1, opindex); |
| 94 | pnnx_graph_rewrite(graph, &b, opindex); |
| 95 | } |
| 96 | |
| 97 | } // namespace pnnx |