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
18from packaging import version
19
20class Model(nn.Module):
21 def __init__(self):
22 super(Model, self).__init__()
23
24 self.act_0 = nn.CELU()
25 self.act_1 = nn.CELU(alpha=2.0)
26
27 def forward(self, x, y, z, w):
28 x = x * 2 - 1
29 y = y * 2 - 1
30 z = z * 2 - 1
31 w = w * 2 - 1
32 x = self.act_0(x)
33 y = self.act_0(y)
34 z = self.act_1(z)
35 w = self.act_1(w)
36 return x, y, z, w
37
38def test():
39 if version.parse(torch.__version__) < version.parse('1.12'):
40 return True
41
42 net = Model()
43 net.eval()
44
45 torch.manual_seed(0)
46 x = torch.rand(1, 12)
47 y = torch.rand(1, 12, 64)
48 z = torch.rand(1, 12, 24, 64)
49 w = torch.rand(1, 12, 24, 32, 64)
50
51 a = net(x, y, z, w)
52
53 # export onnx
54 torch.onnx.export(net, (x, y, z, w), "test_nn_CELU.onnx")
55
56 # onnx to pnnx
57 import os
58 os.system("../../src/pnnx test_nn_CELU.onnx inputshape=[1,12],[1,12,64],[1,12,24,64],[1,12,24,32,64]")
59
60 # pnnx inference
61 import test_nn_CELU_pnnx
62 b = test_nn_CELU_pnnx.test_inference()
63
64 for a0, b0 in zip(a, b):
65 if not torch.equal(a0, b0):
66 return False
67 return True
68
69if __name__ == "__main__":
70 if test():
71 exit(0)
72 else:
73 exit(1)