Elffuss BraidHub

kikocis/ncnn public

master · 1.4 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
17
18class Model(nn.Module):
19 def __init__(self):
20 super(Model, self).__init__()
21
22 def forward(self, x, y):
23 out = torch.mv(x, y)
24 return out
25
26def test():
27 net = Model()
28 net.eval()
29
30 torch.manual_seed(0)
31 x = torch.rand(2, 3)
32 y = torch.rand(3)
33
34 a = net(x, y)
35
36 # export torchscript
37 mod = torch.jit.trace(net, (x, y))
38 mod.save("test_torch_mv.pt")
39
40 # torchscript to pnnx
41 import os
42 os.system("../src/pnnx test_torch_mv.pt inputshape=[2,3],[3]")
43
44 # pnnx inference
45 import test_torch_mv_pnnx
46 b = test_torch_mv_pnnx.test_inference()
47
48 return torch.equal(a, b)
49
50if __name__ == "__main__":
51 if test():
52 exit(0)
53 else:
54 exit(1)