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
18
19class Model(nn.Module):
20 def __init__(self):
21 super(Model, self).__init__()
22
23 def forward(self, x, y):
24 x = x * 10
25 y = y * 13
26 y = y.to(dtype=x.dtype, memory_format=torch.contiguous_format)
27 x = x.to(device='cpu', dtype=torch.int, copy=True)
28 x = x + 1
29 y = y - 2
30 z = x.to(y.device)
31 return x, y, z
32
33def test():
34 net = Model()
35 net.eval()
36
37 torch.manual_seed(0)
38 x = torch.rand(3, 16)
39 y = torch.randint(10, (1, 13), dtype=torch.int)
40
41 a = net(x, y)
42
43 # export torchscript
44 mod = torch.jit.trace(net, (x, y))
45 mod.save("test_Tensor_to.pt")
46
47 # torchscript to pnnx
48 import os
49 os.system("../src/pnnx test_Tensor_to.pt inputshape=[3,16],[1,13]i32")
50
51 # pnnx inference
52 import test_Tensor_to_pnnx
53 b = test_Tensor_to_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)