| 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 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | from packaging import version |
| 19 | |
| 20 | class Model(nn.Module): |
| 21 | def __init__(self): |
| 22 | super(Model, self).__init__() |
| 23 | |
| 24 | self.w3 = nn.Parameter(torch.rand(24)) |
| 25 | self.b3 = nn.Parameter(torch.rand(24)) |
| 26 | self.w4 = nn.Parameter(torch.rand(12, 16)) |
| 27 | self.b4 = nn.Parameter(torch.rand(12, 16)) |
| 28 | self.w5 = nn.Parameter(torch.rand(24)) |
| 29 | self.b5 = nn.Parameter(torch.rand(24)) |
| 30 | |
| 31 | def forward(self, x, y, z, w0, b0, w1, b1, w2, b2): |
| 32 | x = F.layer_norm(x, (24,), w0, b0) |
| 33 | if version.parse(torch.__version__) < version.parse('2.1') or version.parse(torch.__version__) >= version.parse('2.2'): |
| 34 | x = F.layer_norm(x, (12,24), None, None) |
| 35 | x = F.layer_norm(x, (24,), self.w3, self.b3) |
| 36 | |
| 37 | if version.parse(torch.__version__) < version.parse('2.1') or version.parse(torch.__version__) >= version.parse('2.2'): |
| 38 | y = F.layer_norm(y, (16,), None, None, eps=1e-3) |
| 39 | y = F.layer_norm(y, (12,16), w1, b1) |
| 40 | y = F.layer_norm(y, (12,16), self.w4, self.b4) |
| 41 | |
| 42 | z = F.layer_norm(z, (24,), w2, b2) |
| 43 | if version.parse(torch.__version__) < version.parse('2.1') or version.parse(torch.__version__) >= version.parse('2.2'): |
| 44 | z = F.layer_norm(z, (12,16,24), None, None, eps=1e-2) |
| 45 | z = F.layer_norm(z, (24,), self.w5, self.b5) |
| 46 | return x, y, z |
| 47 | |
| 48 | def test(): |
| 49 | net = Model() |
| 50 | net.eval() |
| 51 | |
| 52 | torch.manual_seed(0) |
| 53 | x = torch.rand(1, 12, 24) |
| 54 | y = torch.rand(2, 3, 12, 16) |
| 55 | z = torch.rand(1, 10, 12, 16, 24) |
| 56 | w0 = torch.rand(24) |
| 57 | b0 = torch.rand(24) |
| 58 | w1 = torch.rand(12, 16) |
| 59 | b1 = torch.rand(12, 16) |
| 60 | w2 = torch.rand(24) |
| 61 | b2 = torch.rand(24) |
| 62 | |
| 63 | a0, a1, a2 = net(x, y, z, w0, b0, w1, b1, w2, b2) |
| 64 | |
| 65 | # export onnx |
| 66 | torch.onnx.export(net, (x, y, z, w0, b0, w1, b1, w2, b2), "test_F_layer_norm.onnx") |
| 67 | |
| 68 | # onnx to pnnx |
| 69 | import os |
| 70 | os.system("../../src/pnnx test_F_layer_norm.onnx inputshape=[1,12,24],[2,3,12,16],[1,10,12,16,24],[24],[24],[12,16],[12,16],[24],[24]") |
| 71 | |
| 72 | # pnnx inference |
| 73 | import test_F_layer_norm_pnnx |
| 74 | b0, b1, b2 = test_F_layer_norm_pnnx.test_inference() |
| 75 | |
| 76 | return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) |
| 77 | |
| 78 | if __name__ == "__main__": |
| 79 | if test(): |
| 80 | exit(0) |
| 81 | else: |
| 82 | exit(1) |