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) 2021 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, z):
24 x = x.reshape(1, 2, 24)
25 x = x.reshape(48)
26 y = y.reshape(1, 11, 5, 9)
27 y = y.reshape(99, 5)
28 z = z.reshape(4, 3, 30, 10, 14)
29 z = z.reshape(15, 2, 10, 7, 8, 3)
30 return x, y, z
31
32def test():
33 net = Model()
34 net.eval()
35
36 torch.manual_seed(0)
37 x = torch.rand(1, 3, 16)
38 y = torch.rand(1, 5, 9, 11)
39 z = torch.rand(14, 8, 5, 9, 10)
40
41 a = net(x, y, z)
42
43 # export torchscript
44 mod = torch.jit.trace(net, (x, y, z))
45 mod.save("test_Tensor_reshape.pt")
46
47 # torchscript to pnnx
48 import os
49 os.system("../src/pnnx test_Tensor_reshape.pt inputshape=[1,3,16],[1,5,9,11],[14,8,5,9,10]")
50
51 # pnnx inference
52 import test_Tensor_reshape_pnnx
53 b = test_Tensor_reshape_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)