| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2021 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 | |
| 19 | class Model(nn.Module): |
| 20 | def __init__(self): |
| 21 | super(Model, self).__init__() |
| 22 | |
| 23 | self.w3 = nn.Parameter(torch.rand(24)) |
| 24 | self.b3 = nn.Parameter(torch.rand(24)) |
| 25 | self.w4 = nn.Parameter(torch.rand(12, 16)) |
| 26 | self.b4 = nn.Parameter(torch.rand(12, 16)) |
| 27 | self.w5 = nn.Parameter(torch.rand(24)) |
| 28 | self.b5 = nn.Parameter(torch.rand(24)) |
| 29 | |
| 30 | def forward(self, x, y, z, w0, b0, w1, b1, w2, b2): |
| 31 | x = F.layer_norm(x, (24,), w0, b0) |
| 32 | x = F.layer_norm(x, (12,24), None, None) |
| 33 | x = F.layer_norm(x, (24,), self.w3, self.b3) |
| 34 | |
| 35 | y = F.layer_norm(y, (16,), None, None, eps=1e-3) |
| 36 | y = F.layer_norm(y, (12,16), w1, b1) |
| 37 | y = F.layer_norm(y, (12,16), self.w4, self.b4) |
| 38 | |
| 39 | z = F.layer_norm(z, (24,), w2, b2) |
| 40 | z = F.layer_norm(z, (12,16,24), None, None, eps=1e-2) |
| 41 | z = F.layer_norm(z, (24,), self.w5, self.b5) |
| 42 | return x, y, z |
| 43 | |
| 44 | def test(): |
| 45 | net = Model() |
| 46 | net.eval() |
| 47 | |
| 48 | torch.manual_seed(0) |
| 49 | x = torch.rand(1, 12, 24) |
| 50 | y = torch.rand(2, 3, 12, 16) |
| 51 | z = torch.rand(1, 10, 12, 16, 24) |
| 52 | w0 = torch.rand(24) |
| 53 | b0 = torch.rand(24) |
| 54 | w1 = torch.rand(12, 16) |
| 55 | b1 = torch.rand(12, 16) |
| 56 | w2 = torch.rand(24) |
| 57 | b2 = torch.rand(24) |
| 58 | |
| 59 | a0, a1, a2 = net(x, y, z, w0, b0, w1, b1, w2, b2) |
| 60 | |
| 61 | # export torchscript |
| 62 | mod = torch.jit.trace(net, (x, y, z, w0, b0, w1, b1, w2, b2)) |
| 63 | mod.save("test_F_layer_norm.pt") |
| 64 | |
| 65 | # torchscript to pnnx |
| 66 | import os |
| 67 | os.system("../src/pnnx test_F_layer_norm.pt inputshape=[1,12,24],[2,3,12,16],[1,10,12,16,24],[24],[24],[12,16],[12,16],[24],[24]") |
| 68 | |
| 69 | # pnnx inference |
| 70 | import test_F_layer_norm_pnnx |
| 71 | b0, b1, b2 = test_F_layer_norm_pnnx.test_inference() |
| 72 | |
| 73 | return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) |
| 74 | |
| 75 | if __name__ == "__main__": |
| 76 | if test(): |
| 77 | exit(0) |
| 78 | else: |
| 79 | exit(1) |