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