Elffuss BraidHub

kikocis/ncnn public

master · 3.1 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
19class Model(nn.Module):
20 def __init__(self):
21 super(Model, self).__init__()
22
23 self.m3 = torch.rand(12)
24 self.v3 = torch.rand(12)
25 self.w3 = nn.Parameter(torch.rand(12))
26 self.b3 = nn.Parameter(torch.rand(12))
27 self.m4 = torch.rand(3)
28 self.v4 = torch.rand(3)
29 self.w4 = nn.Parameter(torch.rand(3))
30 self.b4 = nn.Parameter(torch.rand(3))
31 self.m5 = torch.rand(10)
32 self.v5 = torch.rand(10)
33 self.w5 = nn.Parameter(torch.rand(10))
34 self.b5 = nn.Parameter(torch.rand(10))
35
36 def forward(self, x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2):
37 x = F.instance_norm(x, m0, v0, w0, b0)
38 x = F.instance_norm(x, m0, v0, None, None)
39 x = F.instance_norm(x, self.m3, self.v3, self.w3, self.b3)
40
41 y = F.instance_norm(y, m1, v1, w1, b1, eps=1e-3)
42 y = F.instance_norm(y, m1, v1, None, None)
43 y = F.instance_norm(y, self.m4, self.v4, self.w4, self.b4)
44
45 z = F.instance_norm(z, m2, v2, w2, b2)
46 z = F.instance_norm(z, m2, v2, None, None, eps=1e-2)
47 z = F.instance_norm(z, self.m5, self.v5, self.w5, self.b5)
48 return x, y, z
49
50def test():
51 net = Model()
52 net.eval()
53
54 torch.manual_seed(0)
55 x = torch.rand(1, 12, 24)
56 y = torch.rand(2, 3, 12, 16)
57 z = torch.rand(1, 10, 12, 16, 24)
58 m0 = torch.rand(12)
59 v0 = torch.rand(12)
60 w0 = torch.rand(12)
61 b0 = torch.rand(12)
62 m1 = torch.rand(3)
63 v1 = torch.rand(3)
64 w1 = torch.rand(3)
65 b1 = torch.rand(3)
66 m2 = torch.rand(10)
67 v2 = torch.rand(10)
68 w2 = torch.rand(10)
69 b2 = torch.rand(10)
70
71 a0, a1, a2 = net(x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2)
72
73 # export torchscript
74 mod = torch.jit.trace(net, (x, y, z, m0, v0, w0, b0, m1, v1, w1, b1, m2, v2, w2, b2))
75 mod.save("test_F_instance_norm.pt")
76
77 # torchscript to pnnx
78 import os
79 os.system("../src/pnnx test_F_instance_norm.pt inputshape=[1,12,24],[2,3,12,16],[1,10,12,16,24],[12],[12],[12],[12],[3],[3],[3],[3],[10],[10],[10],[10]")
80
81 # pnnx inference
82 import test_F_instance_norm_pnnx
83 b0, b1, b2 = test_F_instance_norm_pnnx.test_inference()
84
85 return torch.equal(a0, b0) and torch.equal(a1, b1) and torch.equal(a2, b2)
86
87if __name__ == "__main__":
88 if test():
89 exit(0)
90 else:
91 exit(1)