Elffuss BraidHub

kikocis/ncnn public

master · 2.0 KiBRawPermalink
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
15import torch
16import torch.nn as nn
17import torch.nn.functional as F
18
19class 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
45def 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 a = net(x, y, z, w)
56
57 # export onnx
58 torch.onnx.export(net, (x, y, z, w), "test_nn_PReLU.onnx")
59
60 # onnx to pnnx
61 import os
62 os.system("../../src/pnnx test_nn_PReLU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
63
64 # pnnx inference
65 import test_nn_PReLU_pnnx
66 b = test_nn_PReLU_pnnx.test_inference()
67
68 for a0, b0 in zip(a, b):
69 if not torch.equal(a0, b0):
70 return False
71 return True
72
73if __name__ == "__main__":
74 if test():
75 exit(0)
76 else:
77 exit(1)