Elffuss BraidHub

kikocis/ncnn public

master · 2.3 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, w):
24 x = F.pad(x, (3,4), mode='constant', value=1.3)
25 x = F.pad(x, (2,2))
26
27 y = F.pad(y, (5,6), mode='reflect')
28 y = F.pad(y, (2,1), mode='replicate')
29 y = F.pad(y, (3,4), mode='constant', value=1.3)
30 y = F.pad(y, (1,1))
31
32 z = F.pad(z, (3,4,3,4), mode='reflect')
33 z = F.pad(z, (2,1,2,0), mode='replicate')
34 z = F.pad(z, (1,0,2,0), mode='constant', value=1.3)
35 z = F.pad(z, (3,3,3,3))
36
37 #w = F.pad(w, (1,2,3,4,5,6), mode='reflect')
38 w = F.pad(w, (5,0,1,2,0,2), mode='replicate')
39 w = F.pad(w, (0,2,2,1,3,4), mode='constant', value=1.3)
40 w = F.pad(w, (2,2,2,2,2,2))
41
42 return x, y, z, w
43
44def test():
45 net = Model()
46 net.eval()
47
48 torch.manual_seed(0)
49 x = torch.rand(1, 16)
50 y = torch.rand(12, 2, 16)
51 z = torch.rand(1, 3, 12, 16)
52 w = torch.rand(1, 5, 7, 9, 11)
53
54 a0, a1, a2, a3 = net(x, y, z, w)
55
56 # export torchscript
57 mod = torch.jit.trace(net, (x, y, z, w))
58 mod.save("test_F_pad.pt")
59
60 # torchscript to pnnx
61 import os
62 os.system("../src/pnnx test_F_pad.pt inputshape=[1,16],[12,2,16],[1,3,12,16],[1,5,7,9,11]")
63
64 # pnnx inference
65 import test_F_pad_pnnx
66 b0, b1, b2, b3 = test_F_pad_pnnx.test_inference()
67
68 return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) and torch.equal(a3, b3)
69
70if __name__ == "__main__":
71 if test():
72 exit(0)
73 else:
74 exit(1)