| 1 | # Tencent is pleased to support the open source community by making ncnn available. |
| 2 | # |
| 3 | # Copyright (C) 2022 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, y, z): |
| 25 | x = F.fold(x, output_size=22, kernel_size=3) |
| 26 | y = F.fold(y, output_size=(17,18), kernel_size=(2,4), stride=(2,1), padding=2, dilation=1) |
| 27 | z = F.fold(z, output_size=(5,11), kernel_size=(2,3), stride=1, padding=(2,4), dilation=(1,2)) |
| 28 | |
| 29 | return x, y, z |
| 30 | |
| 31 | def test(): |
| 32 | net = Model() |
| 33 | net.eval() |
| 34 | |
| 35 | torch.manual_seed(0) |
| 36 | x = torch.rand(1, 108, 400) |
| 37 | y = torch.rand(1, 96, 190) |
| 38 | z = torch.rand(1, 36, 120) |
| 39 | |
| 40 | a0, a1, a2 = net(x, y, z) |
| 41 | |
| 42 | # export torchscript |
| 43 | mod = torch.jit.trace(net, (x, y, z)) |
| 44 | mod.save("test_F_fold.pt") |
| 45 | |
| 46 | # torchscript to pnnx |
| 47 | import os |
| 48 | os.system("../src/pnnx test_F_fold.pt inputshape=[1,108,400],[1,96,190],[1,36,120]") |
| 49 | |
| 50 | # pnnx inference |
| 51 | import test_F_fold_pnnx |
| 52 | b0, b1, b2 = test_F_fold_pnnx.test_inference() |
| 53 | |
| 54 | return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2) |
| 55 | |
| 56 | if __name__ == "__main__": |
| 57 | if test(): |
| 58 | exit(0) |
| 59 | else: |
| 60 | exit(1) |