Elffuss BraidHub

kikocis/ncnn public

master · 4.7 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2020 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#include "testutil.h"
16
17static int test_groupnorm(const ncnn::Mat& a, int group, float eps, int affine)
18{
19 int channels = a.c;
20 if (a.dims == 1)
21 {
22 channels = a.w;
23 }
24 else if (a.dims == 2)
25 {
26 channels = a.h;
27 }
28
29 ncnn::ParamDict pd;
30 pd.set(0, group);
31 pd.set(1, channels);
32 pd.set(2, eps);
33 pd.set(3, affine);
34
35 std::vector<ncnn::Mat> weights(2);
36 weights[0] = RandomMat(channels);
37 weights[1] = RandomMat(channels);
38
39 int ret = test_layer("GroupNorm", pd, weights, a);
40 if (ret != 0)
41 {
42 fprintf(stderr, "test_groupnorm failed a.dims=%d a=(%d %d %d) group=%d eps=%f\n", a.dims, a.w, a.h, a.c, group, eps);
43 }
44
45 return ret;
46}
47
48static int test_groupnorm_0()
49{
50 return 0
51 || test_groupnorm(RandomMat(3, 6, 4, 2), 1, 0.01f, 0)
52 || test_groupnorm(RandomMat(2, 3, 3, 8), 2, 0.002f, 0)
53 || test_groupnorm(RandomMat(3, 4, 5, 6), 3, 0.01f, 0)
54 || test_groupnorm(RandomMat(4, 5, 6, 12), 4, 0.02f, 0)
55 || test_groupnorm(RandomMat(5, 6, 7, 24), 2, 0.001f, 0)
56 || test_groupnorm(RandomMat(2, 8, 9, 24), 3, 0.0001f, 0)
57 || test_groupnorm(RandomMat(3, 6, 4, 2), 1, 0.01f, 1)
58 || test_groupnorm(RandomMat(2, 3, 3, 8), 2, 0.002f, 1)
59 || test_groupnorm(RandomMat(3, 4, 5, 6), 3, 0.01f, 1)
60 || test_groupnorm(RandomMat(4, 5, 6, 12), 4, 0.02f, 1)
61 || test_groupnorm(RandomMat(5, 6, 7, 24), 2, 0.001f, 1)
62 || test_groupnorm(RandomMat(2, 8, 9, 24), 3, 0.0001f, 1);
63}
64
65static int test_groupnorm_1()
66{
67 return 0
68 || test_groupnorm(RandomMat(6, 4, 2), 1, 0.01f, 0)
69 || test_groupnorm(RandomMat(3, 3, 8), 2, 0.002f, 0)
70 || test_groupnorm(RandomMat(4, 5, 6), 3, 0.01f, 0)
71 || test_groupnorm(RandomMat(5, 6, 12), 4, 0.02f, 0)
72 || test_groupnorm(RandomMat(6, 7, 24), 2, 0.001f, 0)
73 || test_groupnorm(RandomMat(8, 9, 24), 3, 0.0001f, 0)
74 || test_groupnorm(RandomMat(6, 4, 2), 1, 0.01f, 1)
75 || test_groupnorm(RandomMat(3, 3, 8), 2, 0.002f, 1)
76 || test_groupnorm(RandomMat(4, 5, 6), 3, 0.01f, 1)
77 || test_groupnorm(RandomMat(5, 6, 12), 4, 0.02f, 1)
78 || test_groupnorm(RandomMat(6, 7, 24), 2, 0.001f, 1)
79 || test_groupnorm(RandomMat(8, 9, 24), 3, 0.0001f, 1);
80}
81
82static int test_groupnorm_2()
83{
84 return 0
85 || test_groupnorm(RandomMat(24, 2), 1, 0.01f, 0)
86 || test_groupnorm(RandomMat(23, 8), 2, 0.002f, 0)
87 || test_groupnorm(RandomMat(25, 6), 3, 0.01f, 0)
88 || test_groupnorm(RandomMat(26, 12), 4, 0.02f, 0)
89 || test_groupnorm(RandomMat(27, 24), 2, 0.001f, 0)
90 || test_groupnorm(RandomMat(29, 24), 3, 0.0001f, 0)
91 || test_groupnorm(RandomMat(24, 2), 1, 0.01f, 1)
92 || test_groupnorm(RandomMat(23, 8), 2, 0.002f, 1)
93 || test_groupnorm(RandomMat(25, 6), 3, 0.01f, 1)
94 || test_groupnorm(RandomMat(26, 12), 4, 0.02f, 1)
95 || test_groupnorm(RandomMat(27, 24), 2, 0.001f, 1)
96 || test_groupnorm(RandomMat(29, 24), 3, 0.0001f, 1);
97}
98
99static int test_groupnorm_3()
100{
101 return 0
102 || test_groupnorm(RandomMat(12), 1, 0.01f, 0)
103 || test_groupnorm(RandomMat(18), 2, 0.002f, 0)
104 || test_groupnorm(RandomMat(36), 3, 0.01f, 0)
105 || test_groupnorm(RandomMat(212), 4, 0.02f, 0)
106 || test_groupnorm(RandomMat(124), 2, 0.001f, 0)
107 || test_groupnorm(RandomMat(324), 3, 0.0001f, 0)
108 || test_groupnorm(RandomMat(12), 1, 0.01f, 1)
109 || test_groupnorm(RandomMat(18), 2, 0.002f, 1)
110 || test_groupnorm(RandomMat(36), 3, 0.01f, 1)
111 || test_groupnorm(RandomMat(212), 4, 0.02f, 1)
112 || test_groupnorm(RandomMat(124), 2, 0.001f, 1)
113 || test_groupnorm(RandomMat(324), 3, 0.0001f, 1);
114}
115
116int main()
117{
118 SRAND(7767517);
119
120 return 0
121 || test_groupnorm_0()
122 || test_groupnorm_1()
123 || test_groupnorm_2()
124 || test_groupnorm_3();
125}