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