Elffuss BraidHub

kikocis/ncnn public

master · 1.6 KiBRawPermalink
1# Tencent is pleased to support the open source community by making ncnn available.
2#
3# Copyright (C) 2022 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.12'):
22 return True
23
24 net = models.vit_b_32()
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 torchscript
33 if version.parse(torch.__version__) >= version.parse('1.12.0'):
34 mod = torch.jit.trace(net, x, check_trace=False)
35 else:
36 mod = torch.jit.trace(net, x)
37 mod.save("test_vit_b_32.pt")
38
39 # torchscript to pnnx
40 import os
41 os.system("../src/pnnx test_vit_b_32.pt inputshape=[1,3,224,224]")
42
43 # pnnx inference
44 import test_vit_b_32_pnnx
45 b = test_vit_b_32_pnnx.test_inference()
46
47 return torch.allclose(a, b, 1e-4, 1e-4)
48
49if __name__ == "__main__":
50 if test():
51 exit(0)
52 else:
53 exit(1)