| 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.prelu_0 = nn.PReLU(num_parameters=12) |
| 24 | self.prelu_1 = nn.PReLU(num_parameters=1, init=0.12) |
| 25 | |
| 26 | def forward(self, x, y, z, w): |
| 27 | x = x * 2 - 1 |
| 28 | y = y * 2 - 1 |
| 29 | z = z * 2 - 1 |
| 30 | w = w * 2 - 1 |
| 31 | |
| 32 | x = self.prelu_0(x) |
| 33 | x = self.prelu_1(x) |
| 34 | |
| 35 | y = self.prelu_0(y) |
| 36 | y = self.prelu_1(y) |
| 37 | |
| 38 | z = self.prelu_0(z) |
| 39 | z = self.prelu_1(z) |
| 40 | |
| 41 | w = self.prelu_0(w) |
| 42 | w = self.prelu_1(w) |
| 43 | return x, y, z, w |
| 44 | |
| 45 | def test(): |
| 46 | net = Model() |
| 47 | net.eval() |
| 48 | |
| 49 | torch.manual_seed(0) |
| 50 | x = torch.rand(1, 12) |
| 51 | y = torch.rand(1, 12, 64) |
| 52 | z = torch.rand(1, 12, 24, 64) |
| 53 | w = torch.rand(1, 12, 24, 32, 64) |
| 54 | |
| 55 | a0, a1, a2, a3 = net(x, y, z, w) |
| 56 | |
| 57 | # export torchscript |
| 58 | mod = torch.jit.trace(net, (x, y, z, w)) |
| 59 | mod.save("test_nn_PReLU.pt") |
| 60 | |
| 61 | # torchscript to pnnx |
| 62 | import os |
| 63 | os.system("../src/pnnx test_nn_PReLU.pt inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]") |
| 64 | |
| 65 | # pnnx inference |
| 66 | import test_nn_PReLU_pnnx |
| 67 | b0, b1, b2, b3 = test_nn_PReLU_pnnx.test_inference() |
| 68 | |
| 69 | return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) and torch.equal(a3, b3) |
| 70 | |
| 71 | if __name__ == "__main__": |
| 72 | if test(): |
| 73 | exit(0) |
| 74 | else: |
| 75 | exit(1) |