| 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 | |
| 15 | import torch |
| 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F |
| 18 | |
| 19 | class Model(nn.Module): |
| 20 | def __init__(self): |
| 21 | super(Model, self).__init__() |
| 22 | |
| 23 | self.pad_0 = nn.ReplicationPad2d(2) |
| 24 | self.pad_1 = nn.ReplicationPad2d(padding=(3,4,5,6)) |
| 25 | self.pad_2 = nn.ReplicationPad2d(padding=(1,0,2,0)) |
| 26 | |
| 27 | def forward(self, x): |
| 28 | x = self.pad_0(x) |
| 29 | x = self.pad_1(x) |
| 30 | x = self.pad_2(x) |
| 31 | return x |
| 32 | |
| 33 | def test(): |
| 34 | net = Model() |
| 35 | net.eval() |
| 36 | |
| 37 | torch.manual_seed(0) |
| 38 | x = torch.rand(1, 12, 13, 13) |
| 39 | |
| 40 | a = net(x) |
| 41 | |
| 42 | # export torchscript |
| 43 | mod = torch.jit.trace(net, x) |
| 44 | mod.save("test_nn_ReplicationPad2d.pt") |
| 45 | |
| 46 | # torchscript to pnnx |
| 47 | import os |
| 48 | os.system("../src/pnnx test_nn_ReplicationPad2d.pt inputshape=[1,12,13,13]") |
| 49 | |
| 50 | # pnnx inference |
| 51 | import test_nn_ReplicationPad2d_pnnx |
| 52 | b = test_nn_ReplicationPad2d_pnnx.test_inference() |
| 53 | |
| 54 | return torch.equal(a, b) |
| 55 | |
| 56 | if __name__ == "__main__": |
| 57 | if test(): |
| 58 | exit(0) |
| 59 | else: |
| 60 | exit(1) |