1
// Tencent is pleased to support the open source community by making ncnn available.
3
// Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
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
8
// https://opensource.org/licenses/BSD-3-Clause
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.
17
static int test_convolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
19
ncnn::Mat a = RandomMat(w, h);
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);
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);
37
std::vector<ncnn::Mat> weights(bias ? 2 : 1);
38
weights[0] = RandomMat(outh * h * kernel);
40
weights[1] = RandomMat(outh);
42
int ret = test_layer("Convolution1D", pd, weights, a);
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]);
51
static int test_convolution1d_0()
53
static const int kdsp[16][4] = {
72
for (int i = 0; i < 16; i++)
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];
79
const int b1 = 1 - b0;
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);
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);
140
static int test_convolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias)
142
ncnn::Mat a = RandomMat(w, h);
152
pd.set(19, 1); // dynamic weight
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);
161
std::vector<ncnn::Mat> as(bias ? 3 : 2);
163
as[1] = RandomMat(kernel, h, outh);
165
as[2] = RandomMat(outh);
167
std::vector<ncnn::Mat> weights(0);
169
int ret = test_layer("Convolution1D", pd, weights, as);
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]);
178
static int test_convolution1d_1()
180
static const int kdsp[7][4] = {
190
for (int i = 0; i < 7; i++)
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];
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);
220
return test_convolution1d_0() || test_convolution1d_1();