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 self.c0 = nn.Parameter(torch.rand(12))
24 self.c2 = nn.Parameter(torch.rand(48, 12))
25
26 def forward(self, a0, a1, a2, b0, b1, b2, c1):
27 a = torch.addmm(a0, a1, a2)
28 b = torch.addmm(b0, b1, b2, beta=1.4, alpha=0.7)
29 c = torch.addmm(self.c0, c1, self.c2, beta=1, alpha=1)
30 return a, b, c
31
32def test():
33 net = Model()
34 net.eval()
35
36 torch.manual_seed(0)
37 a0 = torch.rand(13, 1)
38 a1 = torch.rand(13, 16)
39 a2 = torch.rand(16, 23)
40 b0 = torch.rand(7, 33)
41 b1 = torch.rand(7, 26)
42 b2 = torch.rand(26, 33)
43 c1 = torch.rand(16, 48)
44
45 a = net(a0, a1, a2, b0, b1, b2, c1)
46
47 # export torchscript
48 mod = torch.jit.trace(net, (a0, a1, a2, b0, b1, b2, c1))
49 mod.save("test_torch_addmm.pt")
50
51 # torchscript to pnnx
52 import os
53 os.system("../src/pnnx test_torch_addmm.pt inputshape=[13,1],[13,16],[16,23],[7,33],[7,26],[26,33],[16,48]")
54
55 # pnnx inference
56 import test_torch_addmm_pnnx
57 b = test_torch_addmm_pnnx.test_inference()
58
59 for a0, b0 in zip(a, b):
60 if not torch.equal(a0, b0):
61 return False
62 return True
63
64if __name__ == "__main__":
65 if test():
66 exit(0)
67 else:
68 exit(1)