Elffuss BraidHub

kikocis/ncnn public

master · 1.7 KiBRawPermalink
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
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 self.pad_0 = nn.ConstantPad1d(2, 0.1)
24 self.pad_1 = nn.ConstantPad1d(padding=(3,4), value=-1)
25 self.pad_2 = nn.ConstantPad1d(padding=(1,0), value=123)
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
33def test():
34 net = Model()
35 net.eval()
36
37 torch.manual_seed(0)
38 x = torch.rand(1, 12, 13)
39
40 a = net(x)
41
42 # export onnx
43 torch.onnx.export(net, (x, ), "test_nn_ConstantPad1d.onnx")
44
45 # onnx to pnnx
46 import os
47 os.system("../../src/pnnx test_nn_ConstantPad1d.onnx inputshape=[1,12,13]")
48
49 # pnnx inference
50 import test_nn_ConstantPad1d_pnnx
51 b = test_nn_ConstantPad1d_pnnx.test_inference()
52
53 return torch.equal(a, b)
54
55if __name__ == "__main__":
56 if test():
57 exit(0)
58 else:
59 exit(1)