Elffuss BraidHub

kikocis/ncnn public

master · 8.4 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
15#include "testutil.h"
16
17static int test_convolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
18{
19 ncnn::Mat a = RandomMat(w, h);
20
21 ncnn::ParamDict pd;
22 pd.set(0, outh); // num_output
23 pd.set(1, kernel); // kernel_w
24 pd.set(2, dilation); // dilation_w
25 pd.set(3, stride); // stride_w
26 pd.set(4, pad); // pad_w
27 pd.set(5, bias); // bias_term
28 pd.set(6, outh * h * kernel);
29
30 int activation_type = RAND() % 6; // 0 1 2 3 4 5
31 ncnn::Mat activation_params(2);
32 activation_params[0] = RandomFloat(-1, 0); // alpha
33 activation_params[1] = RandomFloat(0, 1); // beta
34 pd.set(9, activation_type);
35 pd.set(10, activation_params);
36
37 std::vector<ncnn::Mat> weights(bias ? 2 : 1);
38 weights[0] = RandomMat(outh * h * kernel);
39 if (bias)
40 weights[1] = RandomMat(outh);
41
42 int ret = test_layer("Convolution1D", pd, weights, a);
43 if (ret != 0)
44 {
45 fprintf(stderr, "test_convolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
46 }
47
48 return ret;
49}
50
51static int test_convolution1d_0()
52{
53 static const int kdsp[16][4] = {
54 {1, 1, 1, 0},
55 {1, 1, 2, 0},
56 {2, 1, 1, 1},
57 {2, 1, 2, -233},
58 {3, 1, 1, 1},
59 {3, 1, 2, 1},
60 {3, 2, 1, 1},
61 {4, 1, 1, 2},
62 {4, 1, 2, -233},
63 {4, 2, 1, -234},
64 {5, 1, 1, -234},
65 {5, 1, 2, 2},
66 {5, 2, 2, 2},
67 {7, 1, 1, 3},
68 {7, 1, 2, 3},
69 {7, 2, 1, -233},
70 };
71
72 for (int i = 0; i < 16; i++)
73 {
74 const int k = kdsp[i][0];
75 const int d = kdsp[i][1];
76 const int s = kdsp[i][2];
77 const int p = kdsp[i][3];
78 const int b0 = i % 2;
79 const int b1 = 1 - b0;
80
81 int ret = 0
82 || test_convolution1d(9, 1, 1, k, d, s, p, b0)
83 || test_convolution1d(9, 1, 3, k, d, s, p, b1)
84 || test_convolution1d(9, 1, 7, k, d, s, p, b0)
85 || test_convolution1d(9, 1, 15, k, d, s, p, b1)
86 || test_convolution1d(9, 1, 31, k, d, s, p, b0)
87 || test_convolution1d(9, 3, 1, k, d, s, p, b1)
88 || test_convolution1d(9, 3, 3, k, d, s, p, b0)
89 || test_convolution1d(9, 3, 7, k, d, s, p, b1)
90 || test_convolution1d(9, 3, 15, k, d, s, p, b0)
91 || test_convolution1d(9, 3, 31, k, d, s, p, b1)
92 || test_convolution1d(9, 7, 1, k, d, s, p, b0)
93 || test_convolution1d(9, 7, 3, k, d, s, p, b1)
94 || test_convolution1d(9, 7, 7, k, d, s, p, b0)
95 || test_convolution1d(9, 7, 15, k, d, s, p, b1)
96 || test_convolution1d(9, 7, 31, k, d, s, p, b0)
97 || test_convolution1d(9, 15, 1, k, d, s, p, b1)
98 || test_convolution1d(9, 15, 3, k, d, s, p, b0)
99 || test_convolution1d(9, 15, 7, k, d, s, p, b1)
100 || test_convolution1d(9, 15, 15, k, d, s, p, b0)
101 || test_convolution1d(9, 15, 31, k, d, s, p, b1)
102 || test_convolution1d(9, 31, 1, k, d, s, p, b0)
103 || test_convolution1d(9, 31, 3, k, d, s, p, b1)
104 || test_convolution1d(9, 31, 7, k, d, s, p, b0)
105 || test_convolution1d(9, 31, 15, k, d, s, p, b1)
106 || test_convolution1d(25, 28, 31, k, d, s, p, b0)
107 || test_convolution1d(25, 31, 28, k, d, s, p, b1)
108 || test_convolution1d(25, 28, 28, k, d, s, p, b0)
109 || test_convolution1d(25, 24, 28, k, d, s, p, b1)
110 || test_convolution1d(25, 24, 31, k, d, s, p, b0)
111 || test_convolution1d(25, 28, 24, k, d, s, p, b1)
112 || test_convolution1d(25, 31, 24, k, d, s, p, b0)
113 || test_convolution1d(25, 24, 24, k, d, s, p, b1)
114 || test_convolution1d(25, 28, 48, k, d, s, p, b0)
115 || test_convolution1d(25, 31, 48, k, d, s, p, b1)
116 || test_convolution1d(25, 24, 48, k, d, s, p, b0)
117 || test_convolution1d(25, 48, 28, k, d, s, p, b1)
118 || test_convolution1d(25, 48, 31, k, d, s, p, b0)
119 || test_convolution1d(25, 48, 24, k, d, s, p, b1)
120 || test_convolution1d(25, 31, 31, k, d, s, p, b0)
121 || test_convolution1d(25, 48, 48, k, d, s, p, b1);
122
123 if (ret != 0)
124 return -1;
125 }
126
127 return 0
128 || test_convolution1d(7, 1, 4, 3, 1, 1, 1, 1)
129 || test_convolution1d(14, 1, 4, 3, 1, 2, 1, 1)
130 || test_convolution1d(15, 4, 4, 3, 1, 1, 1, 1)
131 || test_convolution1d(15, 8, 8, 3, 1, 1, 1, 1)
132 || test_convolution1d(11, 8, 16, 3, 1, 1, 1, 1)
133 || test_convolution1d(13, 16, 24, 3, 1, 1, 1, 1)
134 || test_convolution1d(8, 16, 24, 3, 1, 1, 1, 0)
135 || test_convolution1d(4, 16, 24, 3, 1, 1, 1, 1)
136 || test_convolution1d(4, 16, 24, 3, 1, 1, 1, 0)
137 || test_convolution1d(6, 64, 64, 3, 1, 2, 0, 1);
138}
139
140static int test_convolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
141{
142 ncnn::Mat a = RandomMat(w, h);
143
144 ncnn::ParamDict pd;
145 pd.set(0, 0);
146 pd.set(1, 0);
147 pd.set(2, dilation);
148 pd.set(3, stride);
149 pd.set(4, pad);
150 pd.set(5, bias);
151 pd.set(6, 0);
152 pd.set(19, 1); // dynamic weight
153
154 int activation_type = RAND() % 7; // 0 1 2 3 4 5 6
155 ncnn::Mat activation_params(2);
156 activation_params[0] = (activation_type == 6) ? RandomFloat(0, 1) : RandomFloat(-1, 0); // alpha
157 activation_params[1] = RandomFloat(0, 1); // beta
158 pd.set(9, activation_type);
159 pd.set(10, activation_params);
160
161 std::vector<ncnn::Mat> as(bias ? 3 : 2);
162 as[0] = a;
163 as[1] = RandomMat(kernel, h, outh);
164 if (bias)
165 as[2] = RandomMat(outh);
166
167 std::vector<ncnn::Mat> weights(0);
168
169 int ret = test_layer("Convolution1D", pd, weights, as);
170 if (ret != 0)
171 {
172 fprintf(stderr, "test_convolution1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f]\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1]);
173 }
174
175 return ret;
176}
177
178static int test_convolution1d_1()
179{
180 static const int kdsp[7][4] = {
181 {1, 1, 1, 0},
182 {1, 1, 2, 0},
183 {2, 1, 1, 1},
184 {2, 1, 2, -233},
185 {3, 1, 1, 1},
186 {3, 1, 2, 1},
187 {3, 2, 1, -234},
188 };
189
190 for (int i = 0; i < 7; i++)
191 {
192 const int k = kdsp[i][0];
193 const int d = kdsp[i][1];
194 const int s = kdsp[i][2];
195 const int p = kdsp[i][3];
196
197 int ret = 0
198 || test_convolution1d_dynamic(11, 1, 1, k, d, s, p, 1)
199 || test_convolution1d_dynamic(11, 4, 13, k, d, s, p, 0)
200 || test_convolution1d_dynamic(11, 13, 4, k, d, s, p, 1)
201 || test_convolution1d_dynamic(11, 12, 12, k, d, s, p, 0)
202 || test_convolution1d_dynamic(11, 8, 12, k, d, s, p, 1)
203 || test_convolution1d_dynamic(11, 8, 13, k, d, s, p, 0)
204 || test_convolution1d_dynamic(11, 13, 8, k, d, s, p, 1)
205 || test_convolution1d_dynamic(11, 12, 16, k, d, s, p, 0)
206 || test_convolution1d_dynamic(11, 15, 15, k, d, s, p, 0)
207 || test_convolution1d_dynamic(11, 16, 16, k, d, s, p, 0);
208
209 if (ret != 0)
210 return -1;
211 }
212
213 return 0;
214}
215
216int main()
217{
218 SRAND(7767517);
219
220 return test_convolution1d_0() || test_convolution1d_1();
221}