Elffuss BraidHub

kikocis/ncnn public

master · 6.1 KiBRawPermalink
1// Tencent is pleased to support the open source community by making ncnn available.
2//
3// Copyright (C) 2022 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_deconvolution1d(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_w)
18{
19 ncnn::Mat a = RandomMat(w, h);
20
21 if (output_w > 0 && pad != -233 && pad != -234)
22 {
23 pad = -233;
24 }
25
26 ncnn::ParamDict pd;
27 pd.set(0, outh);
28 pd.set(1, kernel);
29 pd.set(2, dilation);
30 pd.set(3, stride);
31 pd.set(4, pad);
32 pd.set(5, bias);
33 pd.set(6, outh * h * kernel);
34
35 int activation_type = RAND() % 5; // 0 1 2 3 4
36 ncnn::Mat activation_params(2);
37 activation_params[0] = RandomFloat(-1, 0); // alpha
38 activation_params[1] = RandomFloat(0, 1); // beta
39 pd.set(9, activation_type);
40 pd.set(10, activation_params);
41
42 pd.set(18, output_pad_right);
43 pd.set(20, output_w);
44
45 std::vector<ncnn::Mat> weights(2);
46 weights[0] = RandomMat(outh * h * kernel);
47 weights[1] = RandomMat(outh);
48
49 int ret = test_layer("Deconvolution1D", pd, weights, a);
50 if (ret != 0)
51 {
52 fprintf(stderr, "test_deconvolution1d failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
53 }
54
55 return ret;
56}
57
58static int test_deconvolution1d_0()
59{
60 static const int kdsp[16][4] = {
61 {1, 1, 1, 0},
62 {1, 1, 2, 0},
63 {2, 1, 1, 1},
64 {2, 1, 2, -233},
65 {3, 1, 1, 1},
66 {3, 1, 2, 1},
67 {3, 2, 1, 1},
68 {4, 1, 1, -233},
69 {4, 1, 2, -234},
70 {4, 2, 1, -234},
71 {5, 1, 1, 2},
72 {5, 1, 2, 2},
73 {5, 2, 2, 2},
74 {7, 1, 1, 3},
75 {7, 1, 2, 3},
76 {7, 2, 1, -233},
77 };
78
79 for (int i = 0; i < 16; i++)
80 {
81 const int k = kdsp[i][0];
82 const int d = kdsp[i][1];
83 const int s = kdsp[i][2];
84 const int p = kdsp[i][3];
85
86 int ret = 0
87 || test_deconvolution1d(9, 1, 1, k, d, s, p, 1, 0, 0)
88 || test_deconvolution1d(9, 4, 13, k, d, s, p, 0, 1, 7)
89 || test_deconvolution1d(9, 13, 4, k, d, s, p, 1, 1, 0)
90 || test_deconvolution1d(9, 4, 8, k, d, s, p, 0, 0, 0)
91 || test_deconvolution1d(9, 8, 4, k, d, s, p, 1, 0, 7)
92 || test_deconvolution1d(9, 8, 13, k, d, s, p, 0, 2, 0)
93 || test_deconvolution1d(9, 13, 8, k, d, s, p, 1, 2, 0)
94 || test_deconvolution1d(9, 16, 16, k, d, s, p, 0, 0, 7);
95
96 if (ret != 0)
97 return -1;
98 }
99
100 return 0;
101}
102
103static int test_deconvolution1d_dynamic(int w, int h, int outh, int kernel, int dilation, int stride, int pad, int bias, int output_pad_right, int output_w)
104{
105 ncnn::Mat a = RandomMat(w, h);
106
107 if (output_w > 0 && pad != -233 && pad != -234)
108 {
109 pad = -233;
110 }
111
112 ncnn::ParamDict pd;
113 pd.set(0, 0);
114 pd.set(1, 0);
115 pd.set(2, dilation);
116 pd.set(3, stride);
117 pd.set(4, pad);
118 pd.set(5, bias);
119 pd.set(6, 0);
120 pd.set(28, 1); // dynamic weight
121
122 int activation_type = RAND() % 5; // 0 1 2 3 4
123 ncnn::Mat activation_params(2);
124 activation_params[0] = RandomFloat(-1, 0); // alpha
125 activation_params[1] = RandomFloat(0, 1); // beta
126 pd.set(9, activation_type);
127 pd.set(10, activation_params);
128
129 pd.set(18, output_pad_right);
130 pd.set(20, output_w);
131
132 std::vector<ncnn::Mat> as(bias ? 3 : 2);
133 as[0] = a;
134 as[1] = RandomMat(kernel, outh, h);
135 if (bias)
136 as[2] = RandomMat(outh);
137
138 std::vector<ncnn::Mat> weights(0);
139
140 int ret = test_layer("Deconvolution1D", pd, weights, as);
141 if (ret != 0)
142 {
143 fprintf(stderr, "test_deconvolution1d_dynamic failed w=%d h=%d outh=%d kernel=%d dilation=%d stride=%d pad=%d bias=%d act=%d actparams=[%f,%f] output_pad_right=%d output_w=%d\n", w, h, outh, kernel, dilation, stride, pad, bias, activation_type, activation_params[0], activation_params[1], output_pad_right, output_w);
144 }
145
146 return ret;
147}
148
149static int test_deconvolution1d_1()
150{
151 static const int kdsp[16][4] = {
152 {1, 1, 1, 0},
153 {1, 1, 2, 0},
154 {2, 1, 1, 1},
155 {2, 1, 2, -233},
156 {3, 1, 1, 1},
157 {3, 1, 2, 1},
158 {3, 2, 1, 1},
159 {4, 1, 1, -233},
160 {4, 1, 2, -234},
161 {4, 2, 1, -234},
162 {5, 1, 1, 2},
163 {5, 1, 2, 2},
164 {5, 2, 2, 2},
165 {7, 1, 1, 3},
166 {7, 1, 2, 3},
167 {7, 2, 1, -233},
168 };
169
170 for (int i = 0; i < 16; i++)
171 {
172 const int k = kdsp[i][0];
173 const int d = kdsp[i][1];
174 const int s = kdsp[i][2];
175 const int p = kdsp[i][3];
176
177 int ret = 0
178 || test_deconvolution1d_dynamic(9, 1, 1, k, d, s, p, 1, 0, 0)
179 || test_deconvolution1d_dynamic(9, 4, 13, k, d, s, p, 0, 1, 7)
180 || test_deconvolution1d_dynamic(9, 13, 4, k, d, s, p, 1, 1, 0)
181 || test_deconvolution1d_dynamic(9, 4, 8, k, d, s, p, 0, 0, 0)
182 || test_deconvolution1d_dynamic(9, 8, 4, k, d, s, p, 1, 0, 7)
183 || test_deconvolution1d_dynamic(9, 8, 13, k, d, s, p, 0, 2, 0)
184 || test_deconvolution1d_dynamic(9, 13, 8, k, d, s, p, 1, 2, 0)
185 || test_deconvolution1d_dynamic(9, 16, 16, k, d, s, p, 0, 0, 7);
186
187 if (ret != 0)
188 return -1;
189 }
190
191 return 0;
192}
193
194int main()
195{
196 SRAND(7767517);
197
198 return test_deconvolution1d_0() || test_deconvolution1d_1();
199}