| 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_pool2d(x, kernel_size=3) |
| 26 | x = F.max_pool2d(x, kernel_size=4, stride=2, padding=2, dilation=1) |
| 27 | x = F.max_pool2d(x, kernel_size=(1,3), stride=1, padding=(0,1), dilation=1, return_indices=False, ceil_mode=False) |
| 28 | x = F.max_pool2d(x, kernel_size=(4,5), stride=(1,2), padding=(1,2), dilation=1, return_indices=False, ceil_mode=True) |
| 29 | if version.parse(torch.__version__) < version.parse('1.12'): |
| 30 | x = F.max_pool2d(x, kernel_size=(2,3), stride=1, padding=1, dilation=1, return_indices=False, ceil_mode=False) |
| 31 | else: |
| 32 | x = F.max_pool2d(x, kernel_size=(2,3), stride=1, padding=1, dilation=(1,2), return_indices=False, ceil_mode=False) |
| 33 | x = F.max_pool2d(x, kernel_size=2, stride=1, padding=0, dilation=1, return_indices=False, ceil_mode=True) |
| 34 | x, indices1 = F.max_pool2d(x, kernel_size=2, padding=0, dilation=1, return_indices=True, ceil_mode=False) |
| 35 | x, indices2 = F.max_pool2d(x, kernel_size=(5,4), stride=1, padding=2, dilation=1, return_indices=True, ceil_mode=False) |
| 36 | return x, indices1, indices2 |
| 37 | |
| 38 | def test(): |
| 39 | net = Model() |
| 40 | net.eval() |
| 41 | |
| 42 | torch.manual_seed(0) |
| 43 | x = torch.rand(1, 12, 128, 127) |
| 44 | |
| 45 | a = net(x) |
| 46 | |
| 47 | # export onnx |
| 48 | torch.onnx.export(net, (x,), "test_F_max_pool2d.onnx") |
| 49 | |
| 50 | # onnx to pnnx |
| 51 | import os |
| 52 | os.system("../../src/pnnx test_F_max_pool2d.onnx inputshape=[1,12,128,127]") |
| 53 | |
| 54 | # pnnx inference |
| 55 | import test_F_max_pool2d_pnnx |
| 56 | b = test_F_max_pool2d_pnnx.test_inference() |
| 57 | |
| 58 | for a0, b0 in zip(a, b): |
| 59 | if not torch.equal(a0, b0): |
| 60 | return False |
| 61 | return True |
| 62 | |
| 63 | if __name__ == "__main__": |
| 64 | if test(): |
| 65 | exit(0) |
| 66 | else: |
| 67 | exit(1) |