Elffuss BraidHub

kikocis/ncnn public

master · 1.9 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 def forward(self, x, y, z, w):
25 x = x * 2 - 1
26 y = y * 2 - 1
27 z = z * 2 - 1
28 w = w * 2 - 1
29 x = F.hardshrink(x)
30 y = F.hardshrink(y, 0.1)
31 z = F.hardshrink(z, 0.22)
32 w = F.hardshrink(w, 0)
33 return x, y, z, w
34
35def test():
36 if version.parse(torch.__version__) < version.parse('1.11'):
37 return True
38
39 net = Model()
40 net.eval()
41
42 torch.manual_seed(0)
43 x = torch.rand(1, 16)
44 y = torch.rand(12, 2, 16)
45 z = torch.rand(1, 3, 12, 16)
46 w = torch.rand(1, 5, 7, 9, 11)
47
48 a = net(x, y, z, w)
49
50 # export onnx
51 torch.onnx.export(net, (x, y, z, w), "test_F_hardshrink.onnx")
52
53 # onnx to pnnx
54 import os
55 os.system("../../src/pnnx test_F_hardshrink.onnx inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
56
57 # pnnx inference
58 import test_F_hardshrink_pnnx
59 b = test_F_hardshrink_pnnx.test_inference()
60
61 for a0, b0 in zip(a, b):
62 if not torch.equal(a0, b0):
63 return False
64 return True
65
66if __name__ == "__main__":
67 if test():
68 exit(0)
69 else:
70 exit(1)