Elffuss BraidHub

kikocis/ncnn public

master · 3.1 KiBRawPermalink
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
22namespace pnnx {
23
24class fuse_rmsnorm_pass : public GraphRewriterPass
25{
26public:
27 const char* match_pattern_graph() const
28 {
29 return R"PNNXIR(7767517
306 5
31pnnx.Input input 0 1 input
32pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32
33pnnx.Expression op_1 1 1 input sq expr=pow(@0,2)
34torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True
35pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,rsqrt(add(@2,%eps))))
36pnnx.Output output 1 0 out
37)PNNXIR";
38 }
39
40 const char* replace_pattern_graph() const
41 {
42 return R"PNNXIR(7767517
433 2
44pnnx.Input input 0 1 input
45nn.RMSNorm rmsnorm 1 1 input out elementwise_affine=True eps=%eps normalized_shape=(%c) @weight=%op_0.data
46pnnx.Output output 1 0 out
47)PNNXIR";
48 }
49};
50
51class fuse_rmsnorm_pass_1 : public fuse_rmsnorm_pass
52{
53public:
54 const char* match_pattern_graph() const
55 {
56 return R"PNNXIR(7767517
576 5
58pnnx.Input input 0 1 input
59pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32
60pnnx.Expression op_1 1 1 input sq expr=pow(@0,2.000000e+00)
61torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True
62pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,reciprocal(sqrt(add(@2,%eps)))))
63pnnx.Output output 1 0 out
64)PNNXIR";
65 }
66};
67
68class fuse_rmsnorm_pass_onnx : public fuse_rmsnorm_pass
69{
70public:
71 const char* match_pattern_graph() const
72 {
73 return R"PNNXIR(7767517
746 5
75pnnx.Input input 0 1 input
76pnnx.Attribute op_0 0 1 weight @data #weight=(%c)f32
77pnnx.Expression op_1 1 1 input sq expr=pow(@0,2.000000e+00)
78torch.mean op_2 1 1 sq sqmean dim=(-1) keepdim=True
79pnnx.Expression op_3 3 1 weight input sqmean out expr=mul(@0,mul(@1,div(1.000000e+00,sqrt(add(@2,%eps)))))
80pnnx.Output output 1 0 out
81)PNNXIR";
82 }
83};
84
85void 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