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) 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 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 def forward(self, x):
24 x0 = torch.select(x, 0, 0)
25 x1 = torch.select(x, 0, 1)
26 x2 = torch.select(x, 0, 2)
27
28 z4 = torch.select(x, 2, 4)
29 z3 = torch.select(x, 2, 3)
30
31 y0 = torch.select(x, 1, 0)
32 y1 = torch.select(x, 1, 1)
33 y2 = torch.select(x, 1, 2)
34 y3 = torch.select(x, 1, 3)
35
36 z2 = torch.select(x, 2, 2)
37 z1 = torch.select(x, 2, 1)
38 z0 = torch.select(x, 2, 0)
39
40 return x0, x1, x2, y0, y1, y2, y3, z0, z1, z2, z3, z4
41
42def test():
43 net = Model()
44 net.eval()
45
46 torch.manual_seed(0)
47 x = torch.rand(3, 4, 5)
48
49 a = net(x)
50
51 # export torchscript
52 mod = torch.jit.trace(net, x)
53 mod.save("test_pnnx_fuse_select_to_unbind.pt")
54
55 # torchscript to pnnx
56 import os
57 os.system("../src/pnnx test_pnnx_fuse_select_to_unbind.pt inputshape=[3,4,5]")
58
59 # pnnx inference
60 import test_pnnx_fuse_select_to_unbind_pnnx
61 b = test_pnnx_fuse_select_to_unbind_pnnx.test_inference()
62
63 for a0, b0 in zip(a, b):
64 if not torch.equal(a0, b0):
65 return False
66 return True
67
68if __name__ == "__main__":
69 if test():
70 exit(0)
71 else:
72 exit(1)