Elffuss BraidHub

kikocis/ncnn public

master · 5.0 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_layernorm(const ncnn::Mat& a, int affine_size, float eps, int affine)
18{
19 ncnn::ParamDict pd;
20 pd.set(0, affine_size);
21 pd.set(1, eps);
22 pd.set(2, affine);
23
24 std::vector<ncnn::Mat> weights(2);
25 weights[0] = RandomMat(affine_size);
26 weights[1] = RandomMat(affine_size);
27
28 int ret = test_layer("LayerNorm", pd, weights, a);
29 if (ret != 0)
30 {
31 fprintf(stderr, "test_layernorm failed a.dims=%d a=(%d %d %d) affine_size=%d eps=%f affine=%d\n", a.dims, a.w, a.h, a.c, affine_size, eps, affine);
32 }
33
34 return ret;
35}
36
37static int test_layernorm_0()
38{
39 return 0
40 || test_layernorm(RandomMat(6, 4, 2), 6, 0.01f, 0)
41 || test_layernorm(RandomMat(4, 5, 6), 4, 0.01f, 0)
42 || test_layernorm(RandomMat(3, 3, 8), 3, 0.002f, 0)
43 || test_layernorm(RandomMat(5, 6, 12), 5, 0.02f, 0)
44 || test_layernorm(RandomMat(4, 7, 16), 4, 0.02f, 0)
45 || test_layernorm(RandomMat(6, 7, 24), 6, 0.001f, 0)
46 || test_layernorm(RandomMat(5, 8, 32), 5, 0.001f, 0)
47 || test_layernorm(RandomMat(6, 4, 2), 6, 0.01f, 1)
48 || test_layernorm(RandomMat(4, 5, 6), 4, 0.01f, 1)
49 || test_layernorm(RandomMat(3, 3, 8), 3, 0.002f, 1)
50 || test_layernorm(RandomMat(5, 6, 12), 5, 0.02f, 1)
51 || test_layernorm(RandomMat(4, 7, 16), 4, 0.02f, 1)
52 || test_layernorm(RandomMat(6, 7, 24), 6, 0.001f, 1)
53 || test_layernorm(RandomMat(5, 8, 32), 5, 0.001f, 1);
54}
55
56static int test_layernorm_1()
57{
58 return 0
59 || test_layernorm(RandomMat(6, 4, 2), 24, 0.01f, 0)
60 || test_layernorm(RandomMat(4, 5, 6), 20, 0.01f, 0)
61 || test_layernorm(RandomMat(3, 3, 8), 9, 0.002f, 0)
62 || test_layernorm(RandomMat(5, 6, 12), 30, 0.02f, 0)
63 || test_layernorm(RandomMat(4, 7, 16), 28, 0.02f, 0)
64 || test_layernorm(RandomMat(6, 7, 24), 42, 0.001f, 0)
65 || test_layernorm(RandomMat(5, 8, 32), 40, 0.001f, 0)
66 || test_layernorm(RandomMat(6, 4, 2), 24, 0.01f, 1)
67 || test_layernorm(RandomMat(4, 5, 6), 20, 0.01f, 1)
68 || test_layernorm(RandomMat(3, 3, 8), 9, 0.002f, 1)
69 || test_layernorm(RandomMat(5, 6, 12), 30, 0.02f, 1)
70 || test_layernorm(RandomMat(4, 7, 16), 28, 0.02f, 1)
71 || test_layernorm(RandomMat(6, 7, 24), 42, 0.001f, 1)
72 || test_layernorm(RandomMat(5, 8, 32), 40, 0.001f, 1);
73}
74
75static int test_layernorm_2()
76{
77 return 0
78 || test_layernorm(RandomMat(4, 2), 4, 0.01f, 0)
79 || test_layernorm(RandomMat(5, 6), 5, 0.01f, 0)
80 || test_layernorm(RandomMat(3, 8), 3, 0.002f, 0)
81 || test_layernorm(RandomMat(6, 12), 6, 0.02f, 0)
82 || test_layernorm(RandomMat(4, 16), 4, 0.02f, 0)
83 || test_layernorm(RandomMat(7, 24), 7, 0.001f, 0)
84 || test_layernorm(RandomMat(8, 32), 8, 0.001f, 0)
85 || test_layernorm(RandomMat(4, 2), 4, 0.01f, 1)
86 || test_layernorm(RandomMat(5, 6), 5, 0.01f, 1)
87 || test_layernorm(RandomMat(3, 8), 3, 0.002f, 1)
88 || test_layernorm(RandomMat(6, 12), 6, 0.02f, 1)
89 || test_layernorm(RandomMat(4, 16), 4, 0.02f, 1)
90 || test_layernorm(RandomMat(7, 24), 7, 0.001f, 1)
91 || test_layernorm(RandomMat(8, 32), 8, 0.001f, 1);
92}
93
94static int test_layernorm_3()
95{
96 return 0
97 || test_layernorm(RandomMat(2), 2, 0.01f, 0)
98 || test_layernorm(RandomMat(6), 6, 0.01f, 0)
99 || test_layernorm(RandomMat(8), 8, 0.002f, 0)
100 || test_layernorm(RandomMat(12), 12, 0.02f, 0)
101 || test_layernorm(RandomMat(16), 16, 0.02f, 0)
102 || test_layernorm(RandomMat(24), 24, 0.001f, 0)
103 || test_layernorm(RandomMat(32), 32, 0.001f, 0)
104 || test_layernorm(RandomMat(2), 2, 0.01f, 1)
105 || test_layernorm(RandomMat(6), 6, 0.01f, 1)
106 || test_layernorm(RandomMat(8), 8, 0.002f, 1)
107 || test_layernorm(RandomMat(12), 12, 0.02f, 1)
108 || test_layernorm(RandomMat(16), 16, 0.02f, 1)
109 || test_layernorm(RandomMat(24), 24, 0.001f, 1)
110 || test_layernorm(RandomMat(32), 32, 0.001f, 1);
111}
112
113int main()
114{
115 SRAND(7767517);
116
117 return 0
118 || test_layernorm_0()
119 || test_layernorm_1()
120 || test_layernorm_2()
121 || test_layernorm_3();
122}