-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pnnx convert nn.RMSNorm F.rms_norm (#5628)
- Loading branch information
Showing
10 changed files
with
331 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Tencent is pleased to support the open source community by making ncnn available. | ||
// | ||
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#include "pass_level1.h" | ||
|
||
#include "../utils.h" | ||
|
||
namespace pnnx { | ||
|
||
class RMSNorm : public FuseModulePass | ||
{ | ||
public: | ||
const char* match_type_str() const | ||
{ | ||
return "__torch__.torch.nn.modules.normalization.RMSNorm"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "nn.RMSNorm"; | ||
} | ||
|
||
void write(Operator* op, const std::shared_ptr<torch::jit::Graph>& graph, const torch::jit::Module& mod) const | ||
{ | ||
const torch::jit::Node* rmsn = find_node_by_kind(graph, "aten::rms_norm"); | ||
|
||
op->params["normalized_shape"] = rmsn->namedInput("normalized_shape"); | ||
op->params["eps"] = rmsn->namedInput("eps"); | ||
op->params["elementwise_affine"] = mod.hasattr("weight") && mod.hasattr("bias"); | ||
|
||
if (mod.hasattr("weight")) | ||
{ | ||
op->attrs["weight"] = mod.attr("weight").toTensor(); | ||
} | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_FUSE_MODULE_PASS(RMSNorm) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Tencent is pleased to support the open source community by making ncnn available. | ||
// | ||
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#include "pass_level2.h" | ||
|
||
namespace pnnx { | ||
|
||
class F_rms_norm : public GraphRewriterPass | ||
{ | ||
public: | ||
const char* match_pattern_graph() const | ||
{ | ||
return R"PNNXIR(7767517 | ||
6 5 | ||
pnnx.Input input_0 0 1 input | ||
pnnx.Input input_1 0 1 weight | ||
pnnx.Input input_2 0 1 normalized_shape | ||
prim::Constant op_0 0 1 eps value=%eps | ||
aten::rms_norm op_1 4 1 input normalized_shape weight eps out | ||
pnnx.Output output 1 0 out | ||
)PNNXIR"; | ||
} | ||
|
||
const char* type_str() const | ||
{ | ||
return "F.rms_norm"; | ||
} | ||
}; | ||
|
||
REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_rms_norm, 10) | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Tencent is pleased to support the open source community by making ncnn available. | ||
// | ||
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#include "fuse_static_rmsnorm.h" | ||
|
||
#include "pass_level2.h" | ||
|
||
#include <math.h> | ||
#include <string.h> | ||
|
||
namespace pnnx { | ||
|
||
class fuse_static_Frmsnorm_pass : public GraphRewriterPass | ||
{ | ||
public: | ||
const char* match_pattern_graph() const | ||
{ | ||
return R"PNNXIR(7767517 | ||
4 3 | ||
pnnx.Input input 0 1 input | ||
pnnx.Attribute op_weight 0 1 weight @data | ||
F.rms_norm op_0 2 1 input weight out normalized_shape=%normalized_shape eps=%eps | ||
pnnx.Output output 1 0 out | ||
)PNNXIR"; | ||
} | ||
|
||
const char* replace_pattern_graph() const | ||
{ | ||
return R"PNNXIR(7767517 | ||
3 2 | ||
pnnx.Input input 0 1 input | ||
nn.RMSNorm rmsn 1 1 input out normalized_shape=%normalized_shape eps=%eps elementwise_affine=True @weight=%op_weight.data | ||
pnnx.Output output 1 0 out | ||
)PNNXIR"; | ||
} | ||
}; | ||
|
||
void fuse_static_rmsnorm(Graph& graph) | ||
{ | ||
fuse_static_Frmsnorm_pass a; | ||
int opindex = 0; | ||
|
||
pnnx_graph_rewrite(graph, &a, opindex); | ||
} | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Tencent is pleased to support the open source community by making ncnn available. | ||
// | ||
// Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
// | ||
// Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
// in compliance with the License. You may obtain a copy of the License at | ||
// | ||
// https://opensource.org/licenses/BSD-3-Clause | ||
// | ||
// Unless required by applicable law or agreed to in writing, software distributed | ||
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
// CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations under the License. | ||
|
||
#include "ir.h" | ||
|
||
namespace pnnx { | ||
|
||
void fuse_static_rmsnorm(Graph& graph); | ||
|
||
} // namespace pnnx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Tencent is pleased to support the open source community by making ncnn available. | ||
# | ||
# Copyright (C) 2024 THL A29 Limited, a Tencent company. All rights reserved. | ||
# | ||
# Licensed under the BSD 3-Clause License (the "License"); you may not use this file except | ||
# in compliance with the License. You may obtain a copy of the License at | ||
# | ||
# https://opensource.org/licenses/BSD-3-Clause | ||
# | ||
# Unless required by applicable law or agreed to in writing, software distributed | ||
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR | ||
# CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations under the License. | ||
|
||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
from packaging import version | ||
|
||
class Model(nn.Module): | ||
def __init__(self): | ||
super(Model, self).__init__() | ||
|
||
self.w3 = nn.Parameter(torch.rand(24)) | ||
self.w4 = nn.Parameter(torch.rand(12, 16)) | ||
self.w5 = nn.Parameter(torch.rand(24)) | ||
|
||
def forward(self, x, y, z, w0, w1, w2): | ||
x = F.rms_norm(x, (24,), w0) | ||
x = F.rms_norm(x, (12,24), None) | ||
x = F.rms_norm(x, (24,), self.w3) | ||
|
||
y = F.rms_norm(y, (16,), None, eps=1e-3) | ||
y = F.rms_norm(y, (12,16), w1) | ||
y = F.rms_norm(y, (12,16), self.w4) | ||
|
||
z = F.rms_norm(z, (24,), w2) | ||
z = F.rms_norm(z, (12,16,24), None, eps=1e-2) | ||
z = F.rms_norm(z, (24,), self.w5) | ||
return x, y, z | ||
|
||
def test(): | ||
if version.parse(torch.__version__) < version.parse('2.4'): | ||
return True | ||
|
||
net = Model() | ||
net.eval() | ||
|
||
torch.manual_seed(0) | ||
x = torch.rand(1, 12, 24) | ||
y = torch.rand(2, 3, 12, 16) | ||
z = torch.rand(1, 10, 12, 16, 24) | ||
w0 = torch.rand(24) | ||
w1 = torch.rand(12, 16) | ||
w2 = torch.rand(24) | ||
|
||
a0, a1, a2 = net(x, y, z, w0, w1, w2) | ||
|
||
# export torchscript | ||
mod = torch.jit.trace(net, (x, y, z, w0, w1, w2)) | ||
mod.save("test_F_rms_norm.pt") | ||
|
||
# torchscript to pnnx | ||
import os | ||
os.system("../src/pnnx test_F_rms_norm.pt inputshape=[1,12,24],[2,3,12,16],[1,10,12,16,24],[24],[12,16],[24]") | ||
|
||
# pnnx inference | ||
import test_F_rms_norm_pnnx | ||
b0, b1, b2 = test_F_rms_norm_pnnx.test_inference() | ||
|
||
return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) | ||
|
||
if __name__ == "__main__": | ||
if test(): | ||
exit(0) | ||
else: | ||
exit(1) |
Oops, something went wrong.