Elffuss BraidHub

kikocis/ncnn public

master · 3.5 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, a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1, m0, m1, n0, n1, o0, o1, p0, p1):
24 a = torch.matmul(a0, a1)
25 b = torch.matmul(b0, b1)
26 c = torch.matmul(c0, c1)
27 d = torch.matmul(d0, d1)
28 e = torch.matmul(e0, e1)
29 f = torch.matmul(f0, f1)
30 g = torch.matmul(g0, g1)
31 h = torch.matmul(h0, h1)
32 i = torch.matmul(i0, i1)
33 j = torch.matmul(j0, j1)
34 k = torch.matmul(k0, k1)
35 l = torch.matmul(l0, l1)
36 m = torch.matmul(m0, m1)
37 n = torch.matmul(n0, n1)
38 o = torch.matmul(o0, o1)
39 p = torch.matmul(p0, p1)
40 return a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p
41
42def test():
43 net = Model()
44 net.eval()
45
46 torch.manual_seed(0)
47 a0 = torch.rand(13)
48 a1 = torch.rand(13)
49 b0 = torch.rand(14)
50 b1 = torch.rand(14, 6)
51 c0 = torch.rand(13)
52 c1 = torch.rand(7, 13, 4)
53 d0 = torch.rand(15)
54 d1 = torch.rand(5, 7, 15, 9)
55 e0 = torch.rand(5, 12)
56 e1 = torch.rand(12)
57 f0 = torch.rand(10, 3, 4)
58 f1 = torch.rand(4)
59 g0 = torch.rand(6, 3, 7, 14)
60 g1 = torch.rand(14)
61 h0 = torch.rand(23, 14)
62 h1 = torch.rand(14, 25)
63 i0 = torch.rand(4, 5)
64 i1 = torch.rand(10, 5, 40)
65 j0 = torch.rand(14, 6)
66 j1 = torch.rand(2, 4, 6, 20)
67 k0 = torch.rand(10, 23, 14)
68 k1 = torch.rand(14, 5)
69 l0 = torch.rand(7, 8, 13, 14)
70 l1 = torch.rand(14, 35)
71 m0 = torch.rand(10, 23, 14)
72 m1 = torch.rand(10, 14, 5)
73 n0 = torch.rand(10, 13, 18)
74 n1 = torch.rand(3, 1, 18, 8)
75 o0 = torch.rand(1, 5, 23, 11)
76 o1 = torch.rand(8, 1, 11, 9)
77 p0 = torch.rand(6, 9, 13, 14)
78 p1 = torch.rand(6, 9, 14, 15)
79
80 a = net(a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1, m0, m1, n0, n1, o0, o1, p0, p1)
81
82 # export torchscript
83 mod = torch.jit.trace(net, (a0, a1, b0, b1, c0, c1, d0, d1, e0, e1, f0, f1, g0, g1, h0, h1, i0, i1, j0, j1, k0, k1, l0, l1, m0, m1, n0, n1, o0, o1, p0, p1))
84 mod.save("test_torch_matmul.pt")
85
86 # torchscript to pnnx
87 import os
88 os.system("../src/pnnx test_torch_matmul.pt inputshape=[13],[13],[14],[14,6],[13],[7,13,4],[15],[5,7,15,9],[5,12],[12],[10,3,4],[4],[6,3,7,14],[14],[23,14],[14,25],[4,5],[10,5,40],[14,6],[2,4,6,20],[10,23,14],[14,5],[7,8,13,14],[14,35],[10,23,14],[10,14,5],[10,13,18],[3,1,18,8],[1,5,23,11],[8,1,11,9],[6,9,13,14],[6,9,14,15]")
89
90 # pnnx inference
91 import test_torch_matmul_pnnx
92 b = test_torch_matmul_pnnx.test_inference()
93
94 for a0, b0 in zip(a, b):
95 if not torch.equal(a0, b0):
96 return False
97 return True
98
99if __name__ == "__main__":
100 if test():
101 exit(0)
102 else:
103 exit(1)