| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2024 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 | |
| 15 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | from packaging import version |
| 19 | |
| 20 | class Model(nn.Module): |
| 21 | def __init__(self): |
| 22 | super(Model, self).__init__() |
| 23 | |
| 24 | def forward(self, x): |
| 25 | x = F.max_pool3d(x, kernel_size=3) |
| 26 | x = F.max_pool3d(x, kernel_size=4, stride=2, padding=2, dilation=1) |
| 27 | x = F.max_pool3d(x, kernel_size=(1,2,3), stride=1, padding=(0,0,1), dilation=1, return_indices=False, ceil_mode=False) |
| 28 | 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) |
| 29 | if version.parse(torch.__version__) < version.parse('1.12'): |
| 30 | x = F.max_pool3d(x, kernel_size=(2,3,3), stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False) |
| 31 | else: |
| 32 | x = F.max_pool3d(x, kernel_size=(2,3,3), stride=1, padding=1, dilation=(1,2,2), return_indices=False, ceil_mode=False) |
| 33 | x = F.max_pool3d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True) |
| 34 | x, indices = F.max_pool3d(x, kernel_size=(5,4,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False) |
| 35 | return x, indices |
| 36 | |
| 37 | def test(): |
| 38 | net = Model() |
| 39 | net.eval() |
| 40 | |
| 41 | torch.manual_seed(0) |
| 42 | x = torch.rand(1, 12, 64, 64, 64) |
| 43 | |
| 44 | a = net(x) |
| 45 | |
| 46 | # export onnx |
| 47 | torch.onnx.export(net, (x,), "test_F_max_pool3d.onnx") |
| 48 | |
| 49 | # onnx to pnnx |
| 50 | import os |
| 51 | os.system("../../src/pnnx test_F_max_pool3d.onnx inputshape=[1,12,64,64,64]") |
| 52 | |
| 53 | # pnnx inference |
| 54 | import test_F_max_pool3d_pnnx |
| 55 | b = test_F_max_pool3d_pnnx.test_inference() |
| 56 | |
| 57 | for a0, b0 in zip(a, b): |
| 58 | if not torch.equal(a0, b0): |
| 59 | return False |
| 60 | return True |
| 61 | |
| 62 | if __name__ == "__main__": |
| 63 | if test(): |
| 64 | exit(0) |
| 65 | else: |
| 66 | exit(1) |