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) 2023 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 typing import List
19
20class Model(nn.Module):
21 def __init__(self):
22 super(Model, self).__init__()
23
24 def forward(self, x, y, z):
25 return x + z[1], y[0] + y[1], y[1] - z[0] + z[1] - z[2]
26
27def test():
28 net = Model()
29 net.eval()
30
31 torch.manual_seed(0)
32 x = torch.rand(2, 3, 4)
33 y0 = torch.rand(2, 3, 4)
34 y1 = torch.rand(2, 3, 4)
35 z0 = torch.rand(2, 3, 4)
36 z1 = torch.rand(2, 3, 4)
37 z2 = torch.rand(2, 3, 4)
38 y = [y0, y1]
39 z = [z0, z1, z2]
40
41 a = net(x, y, z)
42
43 # export torchscript
44 mod = torch.jit.trace(net, (x, y, z))
45 mod.save("test_pnnx_pnnx_fuse_input_unpack.pt")
46
47 # torchscript to pnnx
48 import os
49 os.system("../src/pnnx test_pnnx_pnnx_fuse_input_unpack.pt inputshape=[2,3,4],[2,3,4],[2,3,4],[2,3,4],[2,3,4],[2,3,4]")
50
51 # pnnx inference
52 import test_pnnx_pnnx_fuse_input_unpack_pnnx
53 b = test_pnnx_pnnx_fuse_input_unpack_pnnx.test_inference()
54
55 for a0, b0 in zip(a, b):
56 if not torch.equal(a0, b0):
57 return False
58 return True
59
60if __name__ == "__main__":
61 if test():
62 exit(0)
63 else:
64 exit(1)