Elffuss BraidHub

kikocis/ncnn public

master · 2.0 KiBRawPermalink
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
15import torch
16import torch.nn as nn
17import torch.nn.functional as F
18
19
20class Model(nn.Module):
21 def __init__(self):
22 super(Model, self).__init__()
23
24 self.glu0 = nn.GLU(dim=0)
25 self.glu1 = nn.GLU(dim=1)
26 self.glu2 = nn.GLU(dim=2)
27
28 def forward(self, x, y, z):
29 x0 = self.glu0(x)
30
31 y0 = self.glu0(y)
32 y1 = self.glu1(y)
33
34 z0 = self.glu0(z)
35 z1 = self.glu1(z)
36 z2 = self.glu2(z)
37 return x0, y0, y1, z0, z1, z2
38
39
40def test():
41 net = Model()
42 net.eval()
43
44 torch.manual_seed(0)
45 x = torch.rand(18)
46 y = torch.rand(12, 16)
47 z = torch.rand(24, 28, 34)
48
49 x0, y0, y1, z0, z1, z2 = net(x, y, z)
50
51 # export torchscript
52 mod = torch.jit.trace(net, (x, y, z))
53 mod.save("test_nn_GLU.pt")
54
55 # torchscript to pnnx
56 import os
57
58 os.system("../src/pnnx test_nn_GLU.pt inputshape=[18],[12,16],[24,28,34]")
59
60 # pnnx inference
61 import test_nn_GLU_pnnx
62
63 x0p, y0p, y1p, z0p, z1p, z2p = test_nn_GLU_pnnx.test_inference()
64
65 return (
66 torch.equal(x0, x0p)
67 and torch.equal(y0, y0p)
68 and torch.equal(y1, y1p)
69 and torch.equal(z0, z0p)
70 and torch.equal(z1, z1p)
71 and torch.equal(z2, z2p)
72 )
73
74
75if __name__ == "__main__":
76 if test():
77 exit(0)
78 else:
79 exit(1)