| 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.m3 = torch.rand(16) |
| 24 | self.v3 = torch.rand(16) |
| 25 | self.w3 = nn.Parameter(torch.rand(16)) |
| 26 | self.b3 = nn.Parameter(torch.rand(16)) |
| 27 | self.m4 = torch.rand(2) |
| 28 | self.v4 = torch.rand(2) |
| 29 | self.w4 = nn.Parameter(torch.rand(2)) |
| 30 | self.b4 = nn.Parameter(torch.rand(2)) |
| 31 | self.m5 = torch.rand(3) |
| 32 | self.v5 = torch.rand(3) |
| 33 | self.w5 = nn.Parameter(torch.rand(3)) |
| 34 | self.b5 = nn.Parameter(torch.rand(3)) |
| 35 | |
| 36 | def forward(self, x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2): |
| 37 | x = F.batch_norm(x, m0, v0, w0, b0) |
| 38 | x = F.batch_norm(x, m0, v0, None, None) |
| 39 | x = F.batch_norm(x, self.m3, self.v3, self.w3, self.b3) |
| 40 | |
| 41 | y = F.batch_norm(y, m1, v1, w1, b1, eps=1e-3) |
| 42 | y = F.batch_norm(y, m1, v1, None, None) |
| 43 | y = F.batch_norm(y, self.m4, self.v4, self.w4, self.b4) |
| 44 | |
| 45 | z = F.batch_norm(z, m2, v2, w2, b2) |
| 46 | z = F.batch_norm(z, m2, v2, None, None, eps=1e-2) |
| 47 | z = F.batch_norm(z, self.m5, self.v5, self.w5, self.b5) |
| 48 | return x, y, z |
| 49 | |
| 50 | def test(): |
| 51 | net = Model() |
| 52 | net.eval() |
| 53 | |
| 54 | torch.manual_seed(0) |
| 55 | x = torch.rand(1, 16) |
| 56 | y = torch.rand(12, 2, 16) |
| 57 | z = torch.rand(1, 3, 12, 16) |
| 58 | m0 = torch.rand(16) |
| 59 | v0 = torch.rand(16) |
| 60 | w0 = torch.rand(16) |
| 61 | b0 = torch.rand(16) |
| 62 | m1 = torch.rand(2) |
| 63 | v1 = torch.rand(2) |
| 64 | w1 = torch.rand(2) |
| 65 | b1 = torch.rand(2) |
| 66 | m2 = torch.rand(3) |
| 67 | v2 = torch.rand(3) |
| 68 | w2 = torch.rand(3) |
| 69 | b2 = torch.rand(3) |
| 70 | |
| 71 | a0, a1, a2 = net(x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2) |
| 72 | |
| 73 | # export torchscript |
| 74 | mod = torch.jit.trace(net, (x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2)) |
| 75 | mod.save("test_F_batch_norm.pt") |
| 76 | |
| 77 | # torchscript to pnnx |
| 78 | import os |
| 79 | os.system("../src/pnnx test_F_batch_norm.pt inputshape=[1,16],[12,2,16],[1,3,12,16],[16],[16],[16],[16],[2],[2],[2],[2],[3],[3],[3],[3]") |
| 80 | |
| 81 | # pnnx inference |
| 82 | import test_F_batch_norm_pnnx |
| 83 | b0, b1, b2 = test_F_batch_norm_pnnx.test_inference() |
| 84 | |
| 85 | return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) |
| 86 | |
| 87 | if __name__ == "__main__": |
| 88 | if test(): |
| 89 | exit(0) |
| 90 | else: |
| 91 | exit(1) |