Elffuss BraidHub

kikocis/ncnn public

master · 2.9 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):
24 x = F.max_pool1d(x, kernel_size=3)
25 x = F.max_pool1d(x, kernel_size=4, stride=2, padding=2, dilation=1)
26 x = F.max_pool1d(x, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
27 x = F.max_pool1d(x, kernel_size=5, stride=2, padding=2, dilation=1, return_indices=False, ceil_mode=True)
28 x = F.max_pool1d(x, kernel_size=3, stride=1, padding=1, dilation=2, return_indices=False, ceil_mode=False)
29 x = F.max_pool1d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
30 x, indices1 = F.max_pool1d(x, kernel_size=2, padding=1, dilation=1, return_indices=True, ceil_mode=False)
31 x, indices2 = F.max_pool1d(x, kernel_size=5, stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=True)
32
33 y = F.max_pool1d(y, kernel_size=3)
34 y = F.max_pool1d(y, kernel_size=4, stride=2, padding=2, dilation=1)
35 y = F.max_pool1d(y, kernel_size=3, stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False)
36 y = F.max_pool1d(y, kernel_size=5, stride=2, padding=2, dilation=1, return_indices=False, ceil_mode=True)
37 y = F.max_pool1d(y, kernel_size=3, stride=1, padding=1, dilation=2, return_indices=False, ceil_mode=False)
38 y = F.max_pool1d(y, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True)
39
40 return x, indices1, indices2, y
41
42def test():
43 net = Model()
44 net.eval()
45
46 torch.manual_seed(0)
47 x = torch.rand(1, 12, 128)
48 y = torch.rand(12, 128)
49
50 a = net(x, y)
51
52 # export torchscript
53 mod = torch.jit.trace(net, (x, y))
54 mod.save("test_F_max_pool1d.pt")
55
56 # torchscript to pnnx
57 import os
58 os.system("../src/pnnx test_F_max_pool1d.pt inputshape=[1,12,128],[12,128]")
59
60 # pnnx inference
61 import test_F_max_pool1d_pnnx
62 b = test_F_max_pool1d_pnnx.test_inference()
63
64 for a0, b0 in zip(a, b):
65 if not torch.equal(a0, b0):
66 return False
67 return True
68
69if __name__ == "__main__":
70 if test():
71 exit(0)
72 else:
73 exit(1)