Elffuss BraidHub

kikocis/ncnn public

master · 1.8 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 torchvision
17import torchvision.models as models
18from packaging import version
19
20def test():
21 if version.parse(torchvision.__version__) < version.parse('0.13'):
22 return True
23
24 net = models.swin_t()
25 net.eval()
26
27 torch.manual_seed(0)
28 x = torch.rand(1, 3, 224, 224)
29
30 a = net(x)
31
32 # export onnx
33 torch.onnx.export(net, (x,), "test_swin_t.onnx")
34
35 # onnx to pnnx
36 import os
37 os.system("../../src/pnnx test_swin_t.onnx inputshape=[1,3,224,224]")
38
39 # pnnx inference
40 import test_swin_t_pnnx
41 b = test_swin_t_pnnx.test_inference()
42
43 if not torch.allclose(a, b, 1e-4, 1e-4):
44 return False
45
46 if version.parse(torch.__version__) < version.parse('2.5'):
47 return True
48
49 # export dynamo onnx
50 torch.onnx.dynamo_export(net, x).save("test_swin_t_dynamo.onnx")
51
52 # onnx to pnnx
53 os.system("../../src/pnnx test_swin_t_dynamo.onnx inputshape=[1,3,224,224]")
54
55 # pnnx inference
56 import test_swin_t_dynamo_pnnx
57 b = test_swin_t_dynamo_pnnx.test_inference()
58
59 return torch.allclose(a, b, 1e-4, 1e-4)
60
61if __name__ == "__main__":
62 if test():
63 exit(0)
64 else:
65 exit(1)